tool.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import Vue from 'vue'
  2. function setBar() {
  3. uni.getSystemInfo({
  4. success: function (e) {
  5. // #ifndef MP
  6. Vue.prototype.usePort = 'h5';
  7. Vue.prototype.StatusBar = e.statusBarHeight;
  8. if (e.platform == 'android') {
  9. Vue.prototype.CustomBar = e.statusBarHeight + 50;
  10. } else {
  11. Vue.prototype.CustomBar = e.statusBarHeight + 45;
  12. };
  13. // #endif
  14. // #ifdef MP-WEIXIN
  15. Vue.prototype.usePort = 'wechat';
  16. Vue.prototype.StatusBar = e.statusBarHeight;
  17. let custom = wx.getMenuButtonBoundingClientRect();
  18. Vue.prototype.Custom = custom;
  19. Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
  20. // #endif
  21. // #ifdef MP-ALIPAY
  22. Vue.prototype.StatusBar = e.statusBarHeight;
  23. Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
  24. // #endif
  25. }
  26. })
  27. }
  28. function setLink(systemclient = "marketingtool") {
  29. switch (systemclient) {
  30. case "demo":
  31. Vue.prototype.qrCodePrefix = "https://www.jiushengboard.cc/";
  32. break;
  33. case "marketingtool":
  34. Vue.prototype.qrCodePrefix = "https://lsa.cnyunl.com/";
  35. break;
  36. }
  37. }
  38. function mount() {
  39. Vue.prototype.getLocation = (isHighAccuracy = false) => {
  40. return new Promise((resolve, reject) => {
  41. let that = this;
  42. handle()
  43. function handle() {
  44. uni.getLocation({
  45. isHighAccuracy,
  46. highAccuracyExpireTime: isHighAccuracy ? 8000 : '',
  47. success: res => {
  48. console.log("获取定位", res)
  49. resolve(res)
  50. },
  51. fail: err => {
  52. console.log("获取位置失败", err)
  53. uni.hideLoading();
  54. query()
  55. }
  56. })
  57. }
  58. function query() {
  59. uni.getSetting({
  60. success({
  61. authSetting
  62. }) {
  63. if (authSetting['scope.userLocation']) {
  64. handle()
  65. } else {
  66. uni.showModal({
  67. title: '提示',
  68. content: '需要获取您的地理位置,请确认授权,否则可能会定位门店不准确',
  69. cancelText: '下次再说',
  70. confirmText: '前去授权',
  71. success: ({
  72. confirm
  73. }) => {
  74. if (confirm) {
  75. uni.openSetting({
  76. success(res) {
  77. if (res.authSetting['scope.userLocation']) handle();
  78. }
  79. })
  80. } else {
  81. /* uni.showToast({
  82. title: "您未授权地理位置,",
  83. icon: "none",
  84. }) */
  85. resolve();
  86. }
  87. }
  88. })
  89. }
  90. }
  91. })
  92. }
  93. })
  94. };
  95. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  96. if (msg != '成功' || title) uni.showToast({
  97. title: msg == '成功' ? title : msg,
  98. duration,
  99. icon,
  100. mask: mask || exitTime != 0
  101. })
  102. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  103. return msg != '成功';
  104. };
  105. Vue.prototype.paging = (content, init, update) => {
  106. if (update) {
  107. let content1 = JSON.parse(JSON.stringify(content));
  108. content1.pageSize = (content1.pageNumber - 1) * content1.pageSize;
  109. content1.pageNumber = 1;
  110. return content1
  111. } else {
  112. if (content.pageTotal == undefined || !content.pageTotal) content.pageTotal = 1;
  113. if (content.pageNumber == undefined || !content.pageNumber) content.pageNumber = 1;
  114. if (content.pageSize == undefined || !content.pageSize) content.pageSize = 20;
  115. if (init) content.pageNumber = 1;
  116. return content.pageNumber > content.pageTotal;
  117. }
  118. }
  119. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  120. Vue.prototype.getCity = (obj, isAll = true) => obj.province + obj.city + obj.county + (isAll ? obj.address : '');
  121. Vue.prototype.getApps = (appRemark, route) => {
  122. const list = Object.values(uni.getStorageSync('authList')[appRemark])
  123. return route ? list.find(v => v.path == route || v.pathDetail == route) : list
  124. };
  125. Vue.prototype.getHeight = (even, that, calculate = true) => {
  126. return new Promise((resolve, reject) => {
  127. if (calculate) {
  128. uni.getSystemInfo({
  129. success(s) {
  130. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  131. }
  132. });
  133. } else {
  134. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  135. }
  136. })
  137. };
  138. //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  139. Vue.prototype.getSpecifiedImage = (item, type = 'thumbnail') => {
  140. if (!item) return "";
  141. let v = item.subfiles.find(v => v.type == type);
  142. return v ? v.url : item.url;
  143. }
  144. Vue.prototype.formatTime = (date = new Date(), j1 = '-', j2 = ':') => {
  145. const year = date.getFullYear()
  146. const month = date.getMonth() + 1
  147. const day = date.getDate()
  148. const hour = date.getHours()
  149. const minute = date.getMinutes()
  150. const second = date.getSeconds()
  151. const formatNumber = n => {
  152. n = n.toString()
  153. return n[1] ? n : `0${n}`
  154. }
  155. return `${[year, month, day].map(formatNumber).join(j1)} ${[hour, minute, second].map(formatNumber).join(j2)}`
  156. }
  157. Vue.prototype.getCustomClass = (typename) => {
  158. return new Promise((resolve, reject) => {
  159. Vue.prototype.$Http.basic({
  160. "classname": "sysmanage.develop.optiontype.optiontype",
  161. "method": "optiontypeselect",
  162. "content": {
  163. typename
  164. }
  165. }).then(res => {
  166. console.log("获取自定义分类" + typename, res)
  167. if (res.msg != '成功') return resolve([]);
  168. resolve(res.data);
  169. })
  170. })
  171. }
  172. Vue.prototype.CNY = (sum, symbol = '¥', strict = true, precision = 2) => {
  173. const currency = require("./currency.js");
  174. let num = currency(sum, {
  175. symbol,
  176. precision
  177. }).format();
  178. if (strict) {
  179. let decimals = num.split(".");
  180. decimals[1] = ('0.' + decimals[1] - 0 + '').substring(2)
  181. num = decimals[1] ? decimals.join(".") : decimals[0]
  182. }
  183. return num
  184. }
  185. Vue.prototype.callPhone = phoneNumber => {
  186. uni.makePhoneCall({
  187. phoneNumber: phoneNumber + '',
  188. complete: res => {
  189. console.log('makePhoneCall', res)
  190. }
  191. })
  192. }
  193. Vue.prototype.dye = (list, colors, num = 2) => {
  194. let count = num - 1,
  195. index = 0;
  196. return list.map((v, i) => {
  197. if (i > count) {
  198. count += num;
  199. index += 1;
  200. }
  201. v.color = colors[index % colors.length]
  202. return v
  203. })
  204. }
  205. Vue.prototype.getReg = name => {
  206. let obj = {
  207. phonenumber: {
  208. reg: '^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}$',
  209. errText: "请输入正确的11位手机号码!"
  210. },
  211. email: {
  212. reg: '^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$',
  213. errText: "请输入正确的邮箱格式!"
  214. }
  215. }
  216. return obj[name] || ''
  217. }
  218. Vue.prototype.daysAgo = num => {
  219. let now = new Date().getTime() - 0,
  220. end = now + 86400000,
  221. beg = end - (num * 86400000);
  222. return {
  223. begindate: Vue.prototype.formatTime(new Date(beg)).split(' ')[0],
  224. enddate: Vue.prototype.formatTime(new Date(end)).split(' ')[0],
  225. }
  226. }
  227. Vue.prototype.getUrlParams = urlStr => {
  228. const url = decodeURIComponent(urlStr)
  229. let obj = {}
  230. let str = url.slice(url.indexOf('?') + 1),
  231. arr = str.split('&');
  232. obj.funName = url.slice(0, url.indexOf('?')).split("/").pop();
  233. for (let j = arr.length, i = 0; i < j; i++) {
  234. let arr_temp = arr[i].split('=')
  235. obj[arr_temp[0]] = arr_temp[1]
  236. }
  237. return obj
  238. }
  239. Vue.prototype.switchPage = app => {
  240. if (app.path) {
  241. uni.navigateTo({
  242. url: app.path,
  243. fail: (fail) => {
  244. console.log("跳转失败原因", fail)
  245. }
  246. })
  247. } else {
  248. if (app.name == 'index_design') {
  249. app.name = 'design';
  250. } else if (['index_6C', 'index_freeDesign'].includes(app.name)) {
  251. app.name = 'longText';
  252. };
  253. if (app.name == 'index_design') app.name = 'design';
  254. switch (app.name) {
  255. case 'design':
  256. Vue.prototype.$Http.changePage("index", "案例", {
  257. active: '实景案例'
  258. })
  259. break;
  260. case 'index_product':
  261. Vue.prototype.$Http.changePage("cloud", "单品")
  262. break;
  263. case 'index_commodity':
  264. Vue.prototype.$Http.changePage("index", "活动")
  265. break;
  266. case 'index_imgs':
  267. Vue.prototype.$Http.changePage("index", "案例", {
  268. active: '图库'
  269. })
  270. break;
  271. case 'index_dataBank':
  272. Vue.prototype.$Http.changePage("cloud", "资料库")
  273. break;
  274. case 'index_school':
  275. Vue.prototype.$Http.changePage("cloud", "商学院")
  276. break;
  277. case 'index_video':
  278. Vue.prototype.$Http.changePage("index", "视频")
  279. break;
  280. case 'index_cloud':
  281. Vue.prototype.$Http.changePage("cloud", "工作台")
  282. break;
  283. case 'longText':
  284. if (app.forms.path) {
  285. uni.navigateTo({
  286. url: app.forms.path.formcols[0].title
  287. })
  288. } else {
  289. console.log("长图文", app)
  290. }
  291. break;
  292. default:
  293. console.log("未配置路径", app.name)
  294. break;
  295. }
  296. }
  297. console.log(app);
  298. }
  299. Vue.prototype.isInitializeLogin = (fun) => {
  300. const systemInitIsComplete = getApp().globalData.systemInitIsComplete;
  301. if (!systemInitIsComplete || typeof systemInitIsComplete == 'object') {
  302. getApp().globalData.HomePageStartRendering.push(fun)
  303. } else {
  304. fun()
  305. }
  306. }
  307. }
  308. module.exports = {
  309. mount,
  310. setBar,
  311. setLink
  312. }