utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {
  2. BrowseRecordAdd
  3. } from '@/api/applist.js'
  4. import sha256 from 'crypto-js/sha256'
  5. //防抖函数
  6. export function debounce(func, fnThis, wait) {
  7. var timer;
  8. return () => {
  9. timer && clearTimeout(timer);
  10. timer = setTimeout(() => {
  11. func.call(fnThis)
  12. timer = null
  13. }, wait);
  14. };
  15. }
  16. //节流函数
  17. export function throttling(func, fnThis, wait) {
  18. var timer;
  19. return () => {
  20. clearTimeout(timer);
  21. timer = setTimeout(func.bind(fnThis), wait);
  22. };
  23. }
  24. let goMiniAppFlag = false
  25. //跳转小程序
  26. export function goMiniApp(data, item) {
  27. if (goMiniAppFlag) return
  28. goMiniAppFlag = true
  29. let myData = JSON.parse(data)
  30. wx.navigateToMiniProgram({
  31. ...myData,
  32. success: (res) => {
  33. // 打开成功
  34. goMiniAppFlag = false
  35. item && BrowseRecordAdd(item.id)
  36. },
  37. fail: () => {
  38. goMiniAppFlag = false
  39. }
  40. })
  41. }
  42. //跳转页面
  43. export function goPage(url, type, data) {
  44. if (type == 'reLaunch') {
  45. uni.reLaunch({
  46. url
  47. });
  48. return
  49. }
  50. if (type == 'redirectTo') {
  51. uni.redirectTo({
  52. url
  53. });
  54. return
  55. }
  56. uni.navigateTo({
  57. url,
  58. success: function(res) {
  59. // 通过eventChannel向被打开页面传送数据
  60. res.eventChannel.emit('passParameters', data)
  61. }
  62. });
  63. }
  64. //接受参数
  65. export function goPageGetData() {
  66. return new Promise((res) => {
  67. const eventChannel = this.getOpenerEventChannel()
  68. eventChannel.on('passParameters', function(data) {
  69. res(data)
  70. })
  71. })
  72. }
  73. //根据类型跳转
  74. export function clickJumpType(item) {
  75. if (item.jumpUrlType == 'goMiniApp') {
  76. goMiniApp(item.jumpUrl)
  77. }
  78. if (item.jumpUrlType == 'goPage') {
  79. goPage(item.jumpUrl)
  80. }
  81. if (item.jumpUrlType == 'goWebView') {
  82. goPage(`/pages/webview/webview?src=${item.jumpUrl}`)
  83. }
  84. }
  85. function base64_encode(str) {
  86. var c1, c2, c3;
  87. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  88. var i = 0,
  89. len = str.length,
  90. string = '';
  91. while (i < len) {
  92. c1 = str.charCodeAt(i++) & 0xff;
  93. if (i == len) {
  94. string += base64EncodeChars.charAt(c1 >> 2);
  95. string += base64EncodeChars.charAt((c1 & 0x3) << 4);
  96. string += "==";
  97. break;
  98. }
  99. c2 = str.charCodeAt(i++);
  100. if (i == len) {
  101. string += base64EncodeChars.charAt(c1 >> 2);
  102. string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  103. string += base64EncodeChars.charAt((c2 & 0xF) << 2);
  104. string += "=";
  105. break;
  106. }
  107. c3 = str.charCodeAt(i++);
  108. string += base64EncodeChars.charAt(c1 >> 2);
  109. string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  110. string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  111. string += base64EncodeChars.charAt(c3 & 0x3F)
  112. }
  113. return string
  114. }
  115. //微信支付调起支付
  116. export function wxPay(data) {
  117. wx.requestPayment({
  118. ...data
  119. })
  120. }
  121. //返回星期几
  122. export function getUTCDay(date) {
  123. let UTCDay = new Date(date).getUTCDay()
  124. switch (UTCDay) {
  125. case 0:
  126. return '周天';
  127. case 1:
  128. return '周一';
  129. case 2:
  130. return '周二';
  131. case 3:
  132. return '周三';
  133. case 4:
  134. return '周四';
  135. case 5:
  136. return '周五';
  137. case 6:
  138. return '周六';
  139. }
  140. }
  141. //返回今天明天后天周几
  142. export function getDay(date) {
  143. let UTCDay = new Date(date).getUTCDay()
  144. let jetLag = new Date(date).getTime() - new Date().getTime()
  145. let dayDif = jetLag / (1000 * 60 * 60 * 24)
  146. if (dayDif < 2) {
  147. if (dayDif < 0) {
  148. return '今天'
  149. }
  150. if (dayDif < 1) {
  151. return '明天'
  152. }
  153. if (dayDif < 2) {
  154. return '后天'
  155. }
  156. }
  157. switch (UTCDay) {
  158. case 0:
  159. return '周天';
  160. case 1:
  161. return '周一';
  162. case 2:
  163. return '周二';
  164. case 3:
  165. return '周三';
  166. case 4:
  167. return '周四';
  168. case 5:
  169. return '周五';
  170. case 6:
  171. return '周六';
  172. }
  173. }
  174. export default {
  175. goPageGetData,
  176. clickJumpType,
  177. debounce,
  178. throttling,
  179. goMiniApp,
  180. goPage,
  181. wxPay,
  182. getUTCDay,
  183. getDay
  184. }