common-utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // 计算坐标之间旋转角度
  2. let calcAngle = (start, end) => {
  3. let y = Math.sin(end.longitude - start.longitude) * Math.cos(end.latitude);
  4. let x =
  5. Math.cos(start.latitude) * Math.sin(end.latitude) -
  6. Math.sin(start.latitude) * Math.cos(end.latitude) * Math.cos(end.longitude - start.longitude);
  7. let angle = Math.atan2(y, x);
  8. angle = (180 * angle) / Math.PI;
  9. if (end.latitude > start.latitude) {
  10. return angle - 90
  11. }
  12. if (end.latitude < start.latitude) {
  13. return 180 - angle
  14. }
  15. return angle - 90;
  16. }
  17. let isDuringDate = (start, end) => {
  18. let curDate = new Date().getTime();
  19. let beginDate = new Date(start.replace(/-/g, '/')).getTime();
  20. let endDate = new Date(end.replace(/-/g, '/')).getTime();
  21. if (curDate >= beginDate && curDate <= endDate) {
  22. return true;
  23. } else {
  24. return false;
  25. }
  26. }
  27. let getImagePath = (path) => {
  28. // console.log('工具类', uni.$u.http.config.baseURL);
  29. if (uni.$u.test.url(path)) {
  30. return path;
  31. } else {
  32. return uni.$u.http.config.baseURL + path;
  33. }
  34. }
  35. // 对象数组排序
  36. let compareObjArray = (p) => {
  37. return (m, n) => {
  38. var a = m[p];
  39. var b = n[p];
  40. return a - b; //升序
  41. }
  42. }
  43. let getTimeState = () => {
  44. // 获取当前时间
  45. let timeNow = new Date();
  46. // 获取当前小时
  47. let hours = timeNow.getHours();
  48. console.log('小时', hours);
  49. // 设置默认文字
  50. let text = ``;
  51. // 判断当前时间段
  52. if (hours >= 0 && hours < 10) {
  53. text = `早上好`;
  54. } else if (hours >= 10 && hours < 14) {
  55. text = `中午好`;
  56. } else if (hours >= 14 && hours < 18) {
  57. text = `下午好`;
  58. } else if (hours >= 18 && hours < 24) {
  59. text = `晚上好`;
  60. }
  61. console.log(text);
  62. // 返回当前时间段对应的状态
  63. return text;
  64. }
  65. let hidePhoneNumberOrName = (hideStr, way = 0) => {
  66. if (!hideStr) {
  67. return '号码有误'
  68. }
  69. // 获取当前时间
  70. let result = '';
  71. if (way === 0) {
  72. result = hideStr.toString().slice(0, 3) + '****' + hideStr.toString().slice(-4);
  73. }
  74. if (way === 1) {
  75. result = hideStr.toString().slice(0, 1) + '**';
  76. }
  77. if (way === 2) {
  78. // result = hideStr.toString().slice(0, 1) + '**';
  79. if (hideStr.length <= 7) {
  80. result = hideStr;
  81. } else {
  82. result = hideStr.toString().slice(0, 2) + '***' + hideStr.toString().slice(-2);
  83. }
  84. }
  85. if (way === 3) {
  86. result = hideStr.toString().slice(0, 1) + '***' + hideStr.toString().slice(-1);
  87. }
  88. return result;
  89. }
  90. let isDuringDateHour = (start, end) => {
  91. // console.log(start, end);
  92. // let year = new Date().getFullYear();
  93. // let month = new Date().getMonth() + 1;
  94. // let day = new Date().getDate();
  95. // let forward = '';
  96. // if (month < 10) {
  97. // forward = year + '-0' + month + '-' + day
  98. // } else {
  99. // forward = year + '-' + month + '-' + day
  100. // }
  101. // let tempStart = forward + ' ' + start;
  102. // let tempEnd = forward + ' ' + end;
  103. // tempStart = '2023-10-10 00:00:00'
  104. let curDate = new Date().getTime();
  105. let beginDate = new Date(start.replace(/-/g, '/')).getTime();
  106. let endDate = new Date(end.replace(/-/g, '/')).getTime();
  107. if (curDate < beginDate) {
  108. return 0;
  109. }
  110. if (curDate >= beginDate && curDate <= endDate) {
  111. return 1;
  112. }
  113. if (curDate > endDate) {
  114. return 2;
  115. }
  116. }
  117. // 判断是否在今天
  118. let isToday = (time) => {
  119. let year = new Date().getFullYear();
  120. let month = new Date().getMonth() + 1;
  121. let day = new Date().getDate();
  122. let target = new Date(time);
  123. let targetYear = new Date().getFullYear();
  124. let targetMonth = new Date().getMonth() + 1;
  125. let targetDay = new Date().getDate();
  126. if (year === targetYear && month === targetMonth && day === targetDay) {
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. }
  132. let getDayLater = (time) => {
  133. if (time.length === 0) {
  134. return ''
  135. }
  136. let curTime = new Date().getTime();
  137. let targetTime = new Date(time.replace(/-/g, '/')).getTime();
  138. if (curTime >= targetTime) {
  139. return '今天'
  140. }
  141. let sub = targetTime - curTime;
  142. const oneDay = 24 * 60 * 60 * 1000;
  143. let result = Math.ceil(sub / oneDay)
  144. if (result === 1) {
  145. return '明天'
  146. }
  147. if (result === 2) {
  148. return '后天'
  149. }
  150. return `${result}天后`;
  151. }
  152. let todayOrNext = (time) => {
  153. // console.log('time: ',time);
  154. let targetYear = new Date(time.replace(/-/g, '/')).getFullYear();
  155. let targetMonth = new Date(time.replace(/-/g, '/')).getMonth() + 1;
  156. let targetDay = new Date(time.replace(/-/g, '/')).getDate();
  157. let targetHour = new Date(time.replace(/-/g, '/')).getHours();
  158. let targetMin = new Date(time.replace(/-/g, '/')).getMinutes();
  159. let year = new Date().getFullYear();
  160. let month = new Date().getMonth() + 1;
  161. let day = new Date().getDate();
  162. if (targetMin < 10) {
  163. targetMin = '0' + targetMin
  164. }
  165. if (year === targetYear && month === targetMonth && day === targetDay) {
  166. return `${targetHour}:${targetMin}`;
  167. }
  168. if (year === targetYear && month === targetMonth && (targetDay - day) === 1) {
  169. return `明天${targetHour}:${targetMin}`;
  170. }
  171. return `${targetMonth}月${targetDay}日`
  172. }
  173. // 获取时间差(天)
  174. let getRange = (target) => {
  175. let targetDate = new Date(target);
  176. let currentDate = new Date(); // 获取当前日期对象
  177. let timeDiff = Math.abs(targetDate.getTime() - currentDate.getTime());
  178. let daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
  179. if (daysDiff === 1) {
  180. return '明日开抢'
  181. }
  182. return daysDiff + '天后开抢';
  183. }
  184. // 获取时间差(天)
  185. let getTimeDiffYear = (start, end) => {
  186. console.log('start: ', start);
  187. console.log('end: ', end);
  188. if (!start || !end) {
  189. return true
  190. }
  191. let startDate = new Date(start.replace(/-/g, '/')).getTime();
  192. let endDate = new Date(end.replace(/-/g, '/')).getTime();
  193. if (endDate < startDate) {
  194. uni.$u.toast('结束日期小于开始日期')
  195. return false;
  196. }
  197. const oneYearInMs = 365 * 24 * 60 * 60 * 1000; // 近似一年的毫秒数
  198. if (endDate - startDate > oneYearInMs) {
  199. uni.$u.toast('结束日期大于开始日期12个月')
  200. return false;
  201. }
  202. return true;
  203. }
  204. let isBeforeToday = (time) => {
  205. time = time + ' 00:00:00';
  206. let currentDate = new Date(); // 获取当前日期对象
  207. let targetDate = new Date(time.replace(/-/g, '/')).getTime();
  208. return targetDate < currentDate
  209. }
  210. let getWeekAndDay = (time) => {
  211. console.log(time);
  212. let targetDate = new Date(time.replace(/-/g, '/'));
  213. let temp = targetDate.getDay();
  214. let weekDay = '';
  215. switch (temp) {
  216. case 1:
  217. weekDay = '一'
  218. break;
  219. case 2:
  220. weekDay = '二'
  221. break;
  222. case 3:
  223. weekDay = '三'
  224. break;
  225. case 4:
  226. weekDay = '四'
  227. break;
  228. case 5:
  229. weekDay = '五'
  230. break;
  231. case 6:
  232. weekDay = '六'
  233. break;
  234. case 7:
  235. weekDay = '日'
  236. break;
  237. default:
  238. break;
  239. }
  240. return weekDay;
  241. }
  242. let goElemeAppOrMini = (shopId) => {
  243. console.log(shopId);
  244. // #ifdef APP-PLUS
  245. plus.share.getServices(platforms => {
  246. console.log(platforms);
  247. let sweixin = null;
  248. for (let i = 0; i < platforms.length; i++) {
  249. if (platforms[i].id === 'weixin') {
  250. sweixin = platforms[i];
  251. break;
  252. }
  253. }
  254. if (sweixin) {
  255. sweixin.launchMiniProgram({
  256. id: 'gh_6506303a12bb',
  257. path: `ele-estore/ele-takeout-index/ele-takeout-index?&shopId=${shopId}`
  258. })
  259. } else {
  260. uni.$u.toast('请先安装微信')
  261. }
  262. })
  263. // #endif
  264. // #ifdef MP-WEIXIN
  265. uni.navigateToMiniProgram({
  266. appId: 'wxece3a9a4c82f58c9',
  267. path: `ele-estore/ele-takeout-index/ele-takeout-index?&shopId=${shopId}`,
  268. envVersion: 'release',
  269. success: (e) => {
  270. console.log(e);
  271. },
  272. fail: (error) => {
  273. console.log(error);
  274. }
  275. })
  276. // #endif
  277. }
  278. let openDouyinApp = (videoId) => {
  279. if (plus.runtime.isApplicationExist({
  280. pname: 'com.ss.android.ugc.aweme',
  281. action: 'snssdk1128://'
  282. })) {
  283. plus.runtime.openURL(
  284. `snssdk1128://aweme/detail/${videoId}`, (
  285. res) => {
  286. console.log(res);
  287. })
  288. } else {
  289. uni.$u.toast('请先安装抖音App')
  290. }
  291. }
  292. let openDouyinAppUserCenter = (uid) => {
  293. console.log('uid: ', uid);
  294. if (plus.runtime.isApplicationExist({
  295. pname: 'com.ss.android.ugc.aweme',
  296. action: 'snssdk1128://'
  297. })) {
  298. plus.runtime.openURL(
  299. `snssdk1128://user/profile/${uid}`, (
  300. res) => {
  301. console.log(res);
  302. })
  303. } else {
  304. uni.$u.toast('请先安装抖音App')
  305. }
  306. }
  307. let goMeiTuanAppOrMini = (shopId) => {
  308. console.log(shopId);
  309. // #ifdef APP-PLUS
  310. if (plus.os.name === 'Android') {
  311. if (plus.runtime.isApplicationExist({
  312. pname: 'com.sankuai.meituan.takeoutnew'
  313. })) {
  314. console.log('已安装美团外卖App');
  315. plus.runtime.openURL(
  316. `meituanwaimai://waimai.meituan.com/menu?restaurant_id=${shopId}&spu_id=0`, (
  317. res) => {
  318. console.log(res);
  319. })
  320. } else {
  321. console.log('未安装美团外卖App');
  322. plus.share.getServices(platforms => {
  323. console.log(platforms);
  324. let sweixin = null;
  325. for (let i = 0; i < platforms.length; i++) {
  326. if (platforms[i].id === 'weixin') {
  327. sweixin = platforms[i];
  328. break;
  329. }
  330. }
  331. if (sweixin) {
  332. sweixin.launchMiniProgram({
  333. id: 'gh_72a4eb2d4324',
  334. path: `pages/restaurant/restaurant?poi_id=${shopId}`
  335. })
  336. } else {
  337. uni.$u.toast('请先安装微信')
  338. }
  339. })
  340. }
  341. } else {
  342. plus.share.getServices(platforms => {
  343. console.log(platforms);
  344. let sweixin = null;
  345. for (let i = 0; i < platforms.length; i++) {
  346. if (platforms[i].id === 'weixin') {
  347. sweixin = platforms[i];
  348. break;
  349. }
  350. }
  351. if (sweixin) {
  352. sweixin.launchMiniProgram({
  353. id: 'gh_72a4eb2d4324',
  354. path: `pages/restaurant/restaurant?poi_id=${shopId}`
  355. })
  356. } else {
  357. uni.$u.toast('请先安装微信')
  358. }
  359. })
  360. }
  361. // #endif
  362. // #ifdef MP-WEIXIN
  363. uni.navigateToMiniProgram({
  364. appId: 'wx2c348cf579062e56',
  365. path: `pages/restaurant/restaurant?poi_id=${shopId}`,
  366. envVersion: 'release',
  367. success: (e) => {
  368. console.log(e);
  369. },
  370. fail: (error) => {
  371. console.log(error);
  372. }
  373. })
  374. // #endif
  375. }
  376. let openMiniCouponCenter = (path, appId, originalId) => {
  377. console.log(path);
  378. console.log(appId);
  379. console.log(originalId);
  380. if (path.trim().length === 0) {
  381. return uni.showToast({
  382. title: '暂未配置跳转链接',
  383. icon: 'none'
  384. })
  385. }
  386. // #ifdef MP-WEIXIN
  387. uni.navigateToMiniProgram({
  388. appId: appId,
  389. path: path,
  390. envVersion: 'release',
  391. success: (e) => {
  392. console.log(e);
  393. },
  394. fail: (error) => {
  395. console.log(error);
  396. }
  397. })
  398. // #endif
  399. // #ifdef APP-PLUS
  400. plus.share.getServices(platforms => {
  401. let sweixin = null;
  402. for (let i = 0; i < platforms.length; i++) {
  403. if (platforms[i].id === 'weixin') {
  404. sweixin = platforms[i];
  405. break;
  406. }
  407. }
  408. if (sweixin) {
  409. sweixin.launchMiniProgram({
  410. id: originalId,
  411. path: path
  412. })
  413. } else {
  414. uni.$u.toast('请先安装微信')
  415. }
  416. })
  417. // #endif
  418. }
  419. let openWechatServer = () => {
  420. uni.share({
  421. provider: 'weixin',
  422. scene: 'WXSceneSession',
  423. openCustomerServiceChat: true,
  424. corpid: 'wwef0708802d6f526c',
  425. customerUrl: 'https://work.weixin.qq.com/kfid/kfc65b94a0a45799e77',
  426. success: (e) => {
  427. console.log(e);
  428. },
  429. fail: (error) => {
  430. console.log(error);
  431. }
  432. })
  433. // return uni.$u.toast('微信客服尚未配置')
  434. }
  435. let openCompanyWechatGroup = () => {
  436. plus.share.getServices(platforms => {
  437. console.log(platforms);
  438. let sweixin = null;
  439. for (let i = 0; i < platforms.length; i++) {
  440. if (platforms[i].id === 'weixin') {
  441. sweixin = platforms[i];
  442. break;
  443. }
  444. }
  445. if (sweixin) {
  446. sweixin.launchMiniProgram({
  447. id: uni.$u.http.config.originMiniId,
  448. path: `pages/group/pages/store-add-group/store-add-group`
  449. })
  450. } else {
  451. uni.$u.toast('请先安装微信')
  452. }
  453. })
  454. }
  455. let timestampToDate = (timestamp) => {
  456. const date = new Date(timestamp);
  457. const year = date.getFullYear();
  458. // 月份+1,因为从0开始计算,并且确保两位数
  459. const month = ('0' + (date.getMonth() + 1)).slice(-2);
  460. // 日期确保两位数
  461. const day = ('0' + date.getDate()).slice(-2);
  462. return `${year}-${month}-${day}`;
  463. }
  464. let askLogin = () => {
  465. uni.showModal({
  466. title: '暂未登录',
  467. content: '请先前往登录',
  468. showCancel: false,
  469. success: (e) => {
  470. if (e.confirm) {
  471. uni.navigateTo({
  472. url: '/pages/subpack/common/login/login'
  473. })
  474. }
  475. }
  476. })
  477. }
  478. let checkPwdPattern = (text) => {
  479. console.log('text: ', text);
  480. const upperCaseRegex = /[A-Z]/
  481. const lowerCaseRegex = /[a-z]/
  482. const numberRegex = /[0-9]/
  483. const specialCharRegex = /[~!@#\$%\^&\*\(\)-\+=\{\}:;\|\[\]/.,<>/?']/
  484. if (!upperCaseRegex.test(text) || !lowerCaseRegex.test(text) || !numberRegex.test(text) || !
  485. specialCharRegex.test(text)) {
  486. return false
  487. } else {
  488. return true
  489. }
  490. }
  491. export {
  492. // calcAngle,
  493. // isDuringDate,
  494. // getImagePath,
  495. // compareObjArray,
  496. // getTimeState,
  497. // hidePhoneNumberOrName,
  498. // isDuringDateHour,
  499. // isToday,
  500. // todayOrNext,
  501. // getRange,
  502. // goMeiTuanAppOrMini,
  503. // goElemeAppOrMini,
  504. // openDouyinApp,
  505. // openWechatServer,
  506. // openMiniCouponCenter,
  507. // openCompanyWechatGroup,
  508. // getDayLater,
  509. // timestampToDate,
  510. // askLogin,
  511. // openDouyinAppUserCenter,
  512. // getTimeDiffYear,
  513. // isBeforeToday,
  514. checkPwdPattern,
  515. getWeekAndDay
  516. }