tool.js 12 KB

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