tool.js 12 KB

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