index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import {
  2. configShare
  3. } from "@/api/home.js"
  4. export default {
  5. //处理图片地址
  6. imgUrls: (val) => {
  7. if (val) {
  8. return val.indexOf('http') === -1 ? 'https://guess-shop.oss-cn-beijing.aliyuncs.com' + val : val
  9. }
  10. },
  11. // 富文本处理图片显示
  12. richTextImg: (val) => {
  13. if (val) {
  14. return val.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;" ')
  15. }
  16. },
  17. // 高德地图坐标转腾讯地图坐标的转换函数
  18. gcj02tobd09: (lng, lat) => {
  19. var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  20. var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
  21. var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
  22. var bd_lng = z * Math.cos(theta) + 0.0065;
  23. var bd_lat = z * Math.sin(theta) + 0.006;
  24. return [bd_lng, bd_lat];
  25. },
  26. // 距离显示格式处理
  27. distance: (val) => {
  28. if (val) {
  29. let distance = val / 1000
  30. if (distance < 1) {
  31. return Math.round(distance * 1000) + 'm'
  32. } else {
  33. return Math.round(distance * 100) / 100 + 'km'
  34. }
  35. }
  36. return ''
  37. },
  38. // 格式化日期为 YYYY-MM-DD
  39. formatDate: (date) => {
  40. const d = new Date(date);
  41. const year = d.getFullYear();
  42. const month = (d.getMonth() + 1).toString().padStart(2, '0');
  43. const day = d.getDate().toString().padStart(2, '0');
  44. return `${year}-${month}-${day}`;
  45. },
  46. // 格式化日期为 MM/DD
  47. formatDate1: (dateString) => {
  48. const date = new Date(dateString);
  49. const month = String(date.getMonth() + 1).padStart(2, '0');
  50. const day = String(date.getDate()).padStart(2, '0');
  51. return `${month}/${day}`;
  52. },
  53. /**
  54. * 格式化时间戳
  55. * @param {number} timestamp - 时间戳
  56. * @param {string} format - 格式类型,支持 'yyyy-MM-dd HH:mm', 'yyyy-MM-dd HH:mm:ss', 'yyyy-MM-dd'
  57. * @returns {string} 格式化后的日期字符串
  58. */
  59. mFormatDate(timestamp, format = 'yyyy-MM-dd HH:mm:ss') {
  60. if (!timestamp) return '';
  61. const date = new Date(timestamp);
  62. const year = date.getFullYear();
  63. const month = String(date.getMonth() + 1).padStart(2, '0');
  64. const day = String(date.getDate()).padStart(2, '0');
  65. const hours = String(date.getHours()).padStart(2, '0');
  66. const minutes = String(date.getMinutes()).padStart(2, '0');
  67. const seconds = String(date.getSeconds()).padStart(2, '0');
  68. switch (format) {
  69. case 'yyyy-MM-dd HH:mm':
  70. return `${year}-${month}-${day} ${hours}:${minutes}`;
  71. case 'yyyy-MM-dd':
  72. return `${year}-${month}-${day}`;
  73. case 'yyyy-MM-dd HH:mm:ss':
  74. default:
  75. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  76. }
  77. },
  78. //复制
  79. copyTxt(text) {
  80. uni.setClipboardData({
  81. data: text,
  82. success: () => {
  83. uni.showToast({
  84. title: '复制成功',
  85. icon: 'none'
  86. });
  87. },
  88. fail: (err) => {
  89. uni.showToast({
  90. title: '复制失败',
  91. icon: 'none'
  92. });
  93. console.error(err);
  94. }
  95. });
  96. },
  97. // 处理价格样式
  98. // priceStyle: (val) => {
  99. // if (val) {
  100. // let arr = val.split('.')
  101. // return '¥<span>' + arr[0] + '</span>' + arr[1]
  102. // }
  103. // return ''
  104. // }
  105. // skipWeb: (path, title) => {
  106. // if (path === "#") {
  107. // uni.showToast({
  108. // icon: 'none',
  109. // title: '敬请期待'
  110. // })
  111. // return false
  112. // }
  113. // uni.navigateTo({
  114. // url: `/pages/web-view/web?path=${encodeURIComponent(path)}&title=${title || ''}`,
  115. // complete: () => {
  116. // oldApps = null;
  117. // }
  118. // });
  119. // },
  120. openPage: (data = {}) => {
  121. switch (Number(data.jumpType)) {
  122. case 0:
  123. // 内部页面
  124. try {
  125. uni.navigateTo({
  126. url: data.innerPageCode,
  127. fail: err => {
  128. uni.switchTab({
  129. url: data.innerPageCode
  130. });
  131. }
  132. });
  133. } catch (err) {
  134. console.log('未配置页面')
  135. }
  136. break;
  137. case 1:
  138. // 外部链接
  139. // skipWeb(data.outsideAddress, data.name)
  140. if (data.outsideAddress === "#") {
  141. // uni.showToast({
  142. // icon: 'none',
  143. // title: '敬请期待'
  144. // })
  145. return false
  146. }
  147. uni.navigateTo({
  148. url: `/pages/web-view/web?path=${encodeURIComponent(data.outsideAddress)}&title=${data.name || ''}`,
  149. complete: () => {
  150. // oldApps = null;
  151. }
  152. });
  153. break;
  154. case 2:
  155. // 内部资源模型
  156. if (data.resourceModel == 1) {
  157. // 公告
  158. uni.navigateTo({
  159. url: `/pages/home/noticeDetails?noticeId=${data.resourceModelId}`
  160. })
  161. } else if (data.resourceModel == 2) {
  162. // 积分商品
  163. uni.navigateTo({
  164. url: `/pages/giftPackageCenter/goodsDetails?productId=${data.resourceModelId}`
  165. })
  166. } else if (data.resourceModel == 3) {
  167. // 商户
  168. uni.navigateTo({
  169. url: `/pages/home/scenicSpotDetails?businessId=${data.resourceModelId}`
  170. })
  171. } else {
  172. uni.showToast({
  173. icon: 'none',
  174. title: '敬请期待'
  175. })
  176. break;
  177. }
  178. }
  179. },
  180. pagination(total, currentPageNum, currentPageSize) {
  181. let totalPage = 0
  182. if (total > 0) {
  183. if (total / currentPageSize > parseInt(total / currentPageSize)) {
  184. totalPage = parseInt(total / currentPageSize) + 1
  185. } else {
  186. totalPage = parseInt(total / currentPageSize);
  187. }
  188. if (currentPageNum < totalPage) {
  189. return 'more'
  190. }
  191. }
  192. return 'noMore'
  193. },
  194. /**
  195. * @param {Function} func
  196. * @param {number} wait
  197. * @param {boolean} immediate
  198. * @return {*}
  199. */
  200. debounce(func, wait, immediate) {
  201. let timeout, args, context, timestamp, result
  202. const later = function() {
  203. // 据上一次触发时间间隔
  204. const last = +new Date() - timestamp
  205. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  206. if (last < wait && last > 0) {
  207. timeout = setTimeout(later, wait - last)
  208. } else {
  209. timeout = null
  210. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  211. if (!immediate) {
  212. result = func.apply(context, args)
  213. if (!timeout) context = args = null
  214. }
  215. }
  216. }
  217. return function(...args) {
  218. context = this
  219. timestamp = +new Date()
  220. const callNow = immediate && !timeout
  221. // 如果延时不存在,重新设定延时
  222. if (!timeout) timeout = setTimeout(later, wait)
  223. if (callNow) {
  224. result = func.apply(context, args)
  225. context = args = null
  226. }
  227. return result
  228. }
  229. },
  230. // // 获取元素高度
  231. // getElHeight (el) {
  232. // return new Promise(resolve => {
  233. // const query = uni.createSelectorQuery().in(this)
  234. // query.select(el).boundingClientRect(data => {
  235. // resolve(data.height)
  236. // }).exec()
  237. // })
  238. // },
  239. // 手机号脱敏
  240. desensitizePhoneNumber(phoneNumber) {
  241. if (typeof phoneNumber !== 'string' || phoneNumber.length !== 11) {
  242. throw new Error('请输入有效的11位手机号');
  243. }
  244. return phoneNumber.slice(0, 3) + '****' + phoneNumber.slice(7);
  245. },
  246. // 获取分享配置
  247. getConfigShare() {
  248. return new Promise((re, so) => {
  249. configShare().then(res => {
  250. re(res.data)
  251. }).catch(e => so(e))
  252. })
  253. }
  254. }