tool.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 = (isReverseGeocoder = false) => {
  31. return new Promise((resolve, reject) => {
  32. let that = this;
  33. handle()
  34. function handle() {
  35. /* altitude: true,
  36. highAccuracyExpireTime: 8000,
  37. isHighAccuracy: true, */
  38. uni.getLocation({
  39. success: res => {
  40. console.log("获取定位", res)
  41. if (isReverseGeocoder) {
  42. Vue.prototype.$Http.basic({
  43. "id": "10027201",
  44. "content": {
  45. "lon": res.longitude,
  46. "lat": res.latitude
  47. }
  48. }).then(s => {
  49. console.log("定位", s)
  50. res.result = s.data.result;
  51. resolve(res)
  52. })
  53. } else {
  54. resolve(res)
  55. }
  56. },
  57. fail: err => {
  58. console.log("获取位置失败", err)
  59. uni.hideLoading();
  60. query()
  61. }
  62. })
  63. }
  64. function query() {
  65. uni.getSetting({
  66. success({
  67. authSetting
  68. }) {
  69. if (authSetting['scope.userLocation']) {
  70. handle()
  71. } else {
  72. uni.showModal({
  73. title: '提示',
  74. content: '需要获取您的地理位置,请确认授权,否则可能会定位门店不准确',
  75. cancelText: '下次再说',
  76. confirmText: '前去授权',
  77. success: ({
  78. confirm
  79. }) => {
  80. if (confirm) {
  81. uni.openSetting({
  82. success(res) {
  83. if (res.authSetting['scope.userLocation']) handle();
  84. }
  85. })
  86. } else {
  87. /* uni.showToast({
  88. title: "您未授权地理位置,",
  89. icon: "none",
  90. }) */
  91. resolve();
  92. }
  93. }
  94. })
  95. }
  96. }
  97. })
  98. }
  99. })
  100. };
  101. Vue.prototype.cutoff = (msg, title = "", mask = false, exitTime = 0, icon = 'none', duration = 2000, ) => {
  102. if (msg != '成功' || title) uni.showToast({
  103. title: msg == '成功' ? title : msg,
  104. duration,
  105. icon,
  106. mask: mask || exitTime != 0
  107. })
  108. if (exitTime && msg == '成功') setTimeout(uni.navigateBack, exitTime)
  109. return msg != '成功';
  110. };
  111. Vue.prototype.paging = (content, init, update) => {
  112. if (update) {
  113. let content1 = JSON.parse(JSON.stringify(content));
  114. content1.pageSize = (content1.pageNumber - 1) * content1.pageSize;
  115. content1.pageNumber = 1;
  116. return content1
  117. } else {
  118. if (content.pageTotal == undefined || !content.pageTotal) content.pageTotal = 1;
  119. if (content.pageNumber == undefined || !content.pageNumber) content.pageNumber = 1;
  120. if (content.pageSize == undefined || !content.pageSize) content.pageSize = 20;
  121. if (init) content.pageNumber = 1;
  122. return content.pageNumber > content.pageTotal;
  123. }
  124. }
  125. Vue.prototype.tovw = (num) => (num * 100 / 375).toFixed(3) + "vw";
  126. Vue.prototype.getCity = (obj, isAll = true) => obj.province + obj.city + obj.county + (isAll ? obj.address : '');
  127. Vue.prototype.getApps = (appRemark, route) => {
  128. const list = Object.values(uni.getStorageSync('authList')[appRemark])
  129. return route ? list.find(v => v.path == route || v.pathDetail == route) : list
  130. };
  131. Vue.prototype.getHeight = (even, that, calculate = true) => {
  132. return new Promise((resolve, reject) => {
  133. if (calculate) {
  134. uni.getSystemInfo({
  135. success(s) {
  136. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(s.windowHeight - res[0].bottom))
  137. }
  138. });
  139. } else {
  140. uni.createSelectorQuery().in(that).select(even).boundingClientRect().exec(res => (!res[0]) ? reject('没有查询到元素') : resolve(res[0]))
  141. }
  142. })
  143. };
  144. //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  145. Vue.prototype.getSpecifiedImage = (item, type = 'thumbnail') => {
  146. if (!item) return "";
  147. let v = item.subfiles.find(v => v.type == type);
  148. return v ? v.url : item.url;
  149. }
  150. Vue.prototype.formatTime = (date = new Date(), j1 = '-', j2 = ':') => {
  151. const year = date.getFullYear()
  152. const month = date.getMonth() + 1
  153. const day = date.getDate()
  154. const hour = date.getHours()
  155. const minute = date.getMinutes()
  156. const second = date.getSeconds()
  157. const formatNumber = n => {
  158. n = n.toString()
  159. return n[1] ? n : `0${n}`
  160. }
  161. return `${[year, month, day].map(formatNumber).join(j1)} ${[hour, minute, second].map(formatNumber).join(j2)}`
  162. }
  163. Vue.prototype.getCustomClass = (typename) => {
  164. return new Promise((resolve, reject) => {
  165. Vue.prototype.$Http.basic({
  166. "classname": "sysmanage.develop.optiontype.optiontype",
  167. "method": "optiontypeselect",
  168. "content": {
  169. typename
  170. }
  171. }).then(res => {
  172. console.log("获取自定义分类" + typename, res)
  173. if (res.msg != '成功') return resolve([]);
  174. resolve(res.data);
  175. })
  176. })
  177. }
  178. Vue.prototype.CNY = (sum, symbol = '¥', strict = true, precision = 2) => {
  179. const currency = require("./currency.js");
  180. let num = currency(sum, {
  181. symbol,
  182. precision
  183. }).format();
  184. if (strict) {
  185. let decimals = num.split(".");
  186. decimals[1] = ('0.' + decimals[1] - 0 + '').substring(2)
  187. num = decimals[1] ? decimals.join(".") : decimals[0]
  188. }
  189. return num
  190. }
  191. Vue.prototype.callPhone = phoneNumber => {
  192. uni.makePhoneCall({
  193. phoneNumber: phoneNumber + '',
  194. complete: res => {
  195. console.log('makePhoneCall', res)
  196. }
  197. })
  198. }
  199. Vue.prototype.dye = (list, colors, num = 2) => {
  200. let count = num - 1,
  201. index = 0;
  202. return list.map((v, i) => {
  203. if (i > count) {
  204. count += num;
  205. index += 1;
  206. }
  207. v.color = colors[index % colors.length]
  208. return v
  209. })
  210. }
  211. Vue.prototype.getReg = name => {
  212. let obj = {
  213. phonenumber: {
  214. 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}$',
  215. errText: "请输入正确的11位手机号码!"
  216. },
  217. email: {
  218. reg: '^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$',
  219. errText: "请输入正确的邮箱格式!"
  220. }
  221. }
  222. return obj[name] || ''
  223. }
  224. Vue.prototype.daysAgo = num => {
  225. let now = new Date().getTime() - 0,
  226. end = now + 86400000,
  227. beg = end - (num * 86400000);
  228. return {
  229. begindate: Vue.prototype.formatTime(new Date(beg)).split(' ')[0],
  230. enddate: Vue.prototype.formatTime(new Date(end)).split(' ')[0],
  231. }
  232. }
  233. }
  234. module.exports = {
  235. mount,
  236. setBar
  237. }