tool.js 12 KB

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