tools.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import {
  2. API_URL
  3. } from '@/env'
  4. import {
  5. router
  6. } from '@/common/router'
  7. // #ifdef H5
  8. import $wxsdk from '@/common/utils/sdk-h5-weixin';
  9. // #endif
  10. export default {
  11. /**
  12. * 跳转再封装,主要是为了兼容外链。
  13. * @param {String} path - 跳转路径
  14. * @param {isTabbar} isTabbar - 是否是底部导航
  15. */
  16. routerTo(path, isTabbar) {
  17. if (path) {
  18. // 是否跳转外部链接
  19. if (~path.indexOf('http') || ~path.indexOf('www')) {
  20. // #ifdef H5
  21. window.location = path;
  22. // #endif
  23. // #ifndef H5
  24. router.push({
  25. path: '/pages/public/webview',
  26. query: {
  27. 'webviewPath': path
  28. }
  29. })
  30. // #endif
  31. return false
  32. }
  33. if (isTabbar) {
  34. router.replaceAll(path)
  35. } else {
  36. path.includes('/pages/index') && !path.includes('/pages/index/view') ? router.replaceAll(path) : router
  37. .push(path)
  38. }
  39. } else {
  40. console.log(`%cerr:没有填写跳转路径`, 'color:green;background:yellow');
  41. }
  42. },
  43. /**
  44. * 导航-打开位置
  45. * @param {Number} longitude - 经度
  46. * @param {Number} latitude - 纬度
  47. * @param {String} address - 地理位置
  48. */
  49. openLocation(longitude, latitude, address) {
  50. uni.openLocation({
  51. longitude: parseFloat(longitude),
  52. latitude: parseFloat(latitude),
  53. address: address
  54. })
  55. },
  56. /**
  57. * 图片处理-预览图片
  58. * @param {Array} urls - 图片列表
  59. * @param {Number} current - 首个预览下标
  60. */
  61. previewImage(urls = [], current = 0) {
  62. uni.previewImage({
  63. urls: urls,
  64. current: current,
  65. indicator: 'default',
  66. loop: true,
  67. fail(err) {
  68. console.log('previewImage出错', urls, err)
  69. },
  70. })
  71. },
  72. /**
  73. * 数据分组
  74. * @param {Array} oArr - 原数组列表
  75. * @param {Number} length - 单个数组长度
  76. * @return {Array} arr - 分组后的新数组
  77. */
  78. splitData(oArr = [], length = 1) {
  79. let arr = [];
  80. let minArr = [];
  81. oArr.forEach(c => {
  82. if (minArr.length === length) {
  83. minArr = [];
  84. }
  85. if (minArr.length === 0) {
  86. arr.push(minArr);
  87. }
  88. minArr.push(c);
  89. });
  90. return arr;
  91. },
  92. /**
  93. * 获取指定日期
  94. * @param {Date} date - 传入的时间Date对象
  95. * @return {Object} days - 添加的天数,可以是正数、负数或零
  96. */
  97. getDaysToDate(date, days) {
  98. console.log(date)
  99. if (!(date instanceof Date)) {
  100. throw new Error('第一个参数必须是Date对象');
  101. }
  102. if (typeof days !== 'number') {
  103. throw new Error('第二个参数必须是数字');
  104. }
  105. const newDate = new Date(date)
  106. newDate.setDate(date.getDate() + days)
  107. console.log(newDate)
  108. return newDate;
  109. },
  110. /**
  111. * 剩余时间格式化
  112. * @param {Number} t - 剩余多少秒
  113. * @return {Object} format - 格式后的天时分秒对象
  114. */
  115. format(t) {
  116. let format = {
  117. d: '00',
  118. h: '00',
  119. m: '00',
  120. s: '00'
  121. };
  122. if (t > 0) {
  123. let d = Math.floor(t / 86400);
  124. let h = Math.floor((t / 3600) % 24);
  125. let m = Math.floor((t / 60) % 60);
  126. let s = Math.floor(t % 60);
  127. format.d = d
  128. format.h = h
  129. format.m = m
  130. format.s = s
  131. }
  132. return format;
  133. },
  134. /**
  135. * 打电话
  136. * @param {String<Number>} phoneNumber - 数字字符串
  137. */
  138. callPhone(phoneNumber = '') {
  139. let num = phoneNumber.toString()
  140. uni.makePhoneCall({
  141. phoneNumber: num,
  142. fail(err) {
  143. console.log('makePhoneCall出错', err)
  144. },
  145. });
  146. },
  147. /**
  148. * 微信头像
  149. * @param {String} url -图片地址
  150. */
  151. checkMPUrl(url) {
  152. // #ifdef MP
  153. if (
  154. url.substring(0, 4) === 'http' &&
  155. url.substring(0, 5) !== 'https' &&
  156. url.substring(0, 12) !== 'http://store' &&
  157. url.substring(0, 10) !== 'http://tmp' &&
  158. url.substring(0, 10) !== 'http://usr'
  159. ) {
  160. url = 'https' + url.substring(4, url.length);
  161. }
  162. // #endif
  163. return url;
  164. },
  165. /**
  166. * 中间部分*
  167. */
  168. hiddenText(str, frontLen, endLen) {
  169. //str:要进行隐藏的变量 frontLen: 前面需要保留几位 endLen: 后面需要保留几位
  170. var len = str.length - frontLen - endLen;
  171. var xing = "";
  172. for (var i = 0; i < len; i++) {
  173. xing += "*";
  174. }
  175. return (
  176. str.substring(0, frontLen) + xing + str.substring(str.length - endLen)
  177. )
  178. },
  179. amountCN(n) {
  180. const fraction = ['角', '分']
  181. const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  182. const unit = [
  183. ['元', '万', '亿'],
  184. ['', '拾', '佰', '仟']
  185. ]
  186. const head = n < 0 ? '欠' : ''
  187. n = Math.abs(n)
  188. let s = ''
  189. for (let i = 0; i < fraction.length; i++) {
  190. s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '')
  191. }
  192. s = s || '整'
  193. n = Math.floor(n)
  194. // eslint-disable-next-line no-redeclare
  195. for (let i = 0; i < unit[0].length && n > 0; i++) {
  196. let p = ''
  197. for (let j = 0; j < unit[1].length && n > 0; j++) {
  198. p = digit[n % 10] + unit[1][j] + p
  199. n = Math.floor(n / 10)
  200. }
  201. s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
  202. }
  203. return (
  204. head +
  205. s
  206. .replace(/(零.)*零元/, '元')
  207. .replace(/(零.)+/g, '零')
  208. .replace(/^整$/, '零元整')
  209. )
  210. },
  211. // 分享复制
  212. shareCopy(page) {
  213. // #ifdef H5
  214. const url = location.href.split('/pages')[0];
  215. uni.setClipboardData({
  216. data: url + page,
  217. showToast: false,
  218. success() {
  219. uni.showToast({
  220. mask: true,
  221. title: '您已复制邀请链接,快去分享吧',
  222. icon: 'success'
  223. })
  224. // uni.showModal({
  225. // title: '温馨提示',
  226. // content: '您已复制邀请链接,快去分享吧',
  227. // showCancel: false
  228. // })
  229. },
  230. fail() {
  231. uni.$u.toast('复制失败,请重新点击')
  232. }
  233. })
  234. // #endif
  235. },
  236. // H5设置分享信息
  237. shareHandle(detail) {
  238. // #ifdef H5
  239. const url = location.href.split('/pages')[0];
  240. $wxsdk.updateShareInfo({
  241. title: detail.title,
  242. desc: detail.desc,
  243. link: url + detail.link,
  244. image: detail.image
  245. });
  246. // #endif
  247. },
  248. }