index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. // 定位授权
  255. async getLocation() {
  256. return new Promise((resolve, reject) => {
  257. let that = this;
  258. // 1、判断手机定位服务【GPS】 是否授权
  259. uni.getSystemInfo({
  260. success(res) {
  261. // console.log("判断手机定位服务是否授权:", res);
  262. let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
  263. let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
  264. // console.log("判断手机定位服务是否开启",locationEnabled);
  265. // console.log("判断定位服务是否允许微信授权",locationAuthorized);
  266. if (locationEnabled == false || locationAuthorized == false) {
  267. //手机定位服务(GPS)未授权
  268. uni.showToast({
  269. title: '请打开手机GPS',
  270. icon: 'none',
  271. });
  272. resolve(false);
  273. } else {
  274. //手机定位服务(GPS)已授权
  275. // 2、判断微信小程序是否授权位置信息
  276. // 微信小程序已授权位置信息
  277. uni.authorize({
  278. //授权请求窗口
  279. scope: 'scope.userLocation', //授权的类型
  280. success: async (res) => {
  281. resolve(await that.fnGetlocation());
  282. },
  283. fail: (err) => {
  284. err = err['errMsg'];
  285. uni.showModal({
  286. content: '需要授权位置信息',
  287. confirmText: '确认授权',
  288. }).then((obj) => {
  289. console.log('obj', obj);
  290. if (obj.confirm) {
  291. uni.openSetting({
  292. success: async (set) => {
  293. if (set.authSetting[
  294. 'scope.userLocation'
  295. ]) {
  296. // 授权成功
  297. uni.showToast({
  298. title: '授权成功',
  299. icon: 'none',
  300. });
  301. resolve(
  302. await that.fnGetlocation()
  303. );
  304. } else {
  305. // 未授权
  306. uni.showToast({
  307. title: '授权失败,请重新授权',
  308. icon: 'none',
  309. });
  310. uni.showModal({
  311. title: '授权',
  312. content: '获取授权' +
  313. '失败,是否前往授权设置?',
  314. success: function(result) {
  315. if (result.confirm) {
  316. uni.openSetting();
  317. }
  318. },
  319. fail: function() {
  320. uni.showToast({
  321. title: '系统错误!',
  322. icon: 'none',
  323. });
  324. },
  325. });
  326. }
  327. },
  328. });
  329. } else {
  330. resolve(false);
  331. // 取消授权
  332. uni.showToast({
  333. title: '你拒绝了授权,无法获得周边信息',
  334. icon: 'none',
  335. });
  336. }
  337. });
  338. },
  339. // async complete(res) {
  340. // // console.log('授权弹框', res);
  341. // if (res.errMsg == 'authorize:ok') {
  342. // resolve(await fnGetlocation());
  343. // } else {
  344. // uni.showModal({
  345. // title: '授权',
  346. // content: '获取授权失败,是否前往授权设置?',
  347. // success: function (result) {
  348. // if (result.confirm) {
  349. // uni.openSetting();
  350. // }
  351. // },
  352. // fail: function () {
  353. // uni.showToast({
  354. // title: '系统错误!',
  355. // icon: 'none',
  356. // });
  357. // },
  358. // });
  359. // }
  360. // },
  361. });
  362. }
  363. },
  364. });
  365. });
  366. },
  367. // 定位获取
  368. async fnGetlocation() {
  369. return new Promise((resolve, reject) => {
  370. let that = this;
  371. let bindList = {
  372. long: '',
  373. lat: '',
  374. longlat: '',
  375. isLocated: false,
  376. };
  377. uni.getLocation({
  378. type: 'wgs84', //默认为 wgs84 返回 gps 坐标
  379. geocode: 'true',
  380. isHighAccuracy: 'true',
  381. accuracy: 'best', // 精度值为20m
  382. success: function(res) {
  383. let platform = uni.getSystemInfoSync().platform;
  384. if (platform == 'ios') {
  385. //toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。
  386. bindList.long = res.longitude.toFixed(6);
  387. bindList.lat = res.latitude.toFixed(6);
  388. } else {
  389. bindList.long = res.longitude;
  390. bindList.lat = res.latitude;
  391. }
  392. resolve(bindList);
  393. },
  394. fail(err) {
  395. if (
  396. err.errMsg ===
  397. 'getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化'
  398. ) {
  399. uni.showToast({
  400. title: '请勿频繁定位',
  401. icon: 'none',
  402. });
  403. }
  404. if (err.errMsg === 'getLocation:fail auth deny') {
  405. // 未授权
  406. uni.showToast({
  407. title: '无法定位,请重新获取位置信息',
  408. icon: 'none',
  409. });
  410. authDenyCb && authDenyCb();
  411. bindList.isLocated = false;
  412. resolve(bindList);
  413. }
  414. if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
  415. uni.showModal({
  416. content: '请开启手机定位服务',
  417. showCancel: false,
  418. });
  419. }
  420. },
  421. });
  422. });
  423. }
  424. }