common.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import cacheManager from "./cacheManager.js"
  2. /**
  3. * 显示消息提示框
  4. * @param content 提示的标题
  5. */
  6. export function toast(content) {
  7. uni.showToast({
  8. icon: 'none',
  9. duration:3000,
  10. title: content
  11. })
  12. }
  13. /**
  14. * 显示模态弹窗
  15. * @param content 提示的标题
  16. */
  17. export function showConfirm(content) {
  18. return new Promise((resolve, reject) => {
  19. uni.showModal({
  20. title: '提示',
  21. content: content,
  22. cancelText: '取消',
  23. confirmText: '确定',
  24. success: function(res) {
  25. resolve(res)
  26. }
  27. })
  28. })
  29. }
  30. /**
  31. * 参数处理
  32. * @param params 参数
  33. */
  34. export function tansParams(params) {
  35. let result = ''
  36. // FIXME 拼接参数
  37. return result
  38. }
  39. /**
  40. * @summary 获取请求异常与正常返回
  41. * @param {Object} promise
  42. */
  43. export function catchError(promise) {
  44. return new Promise((resolve,reject) => {
  45. promise.then(data => {
  46. resolve([undefined, data.data])
  47. }).catch(err => {
  48. reject([err])
  49. })
  50. })
  51. }
  52. // 是否是会员
  53. export function getUserIdentity() {
  54. const auth = cacheManager.get('auth');
  55. if (auth) {
  56. if ((auth.levelIdList || []).some(item => item == auth.levelId)) {
  57. // 购买此levelId
  58. return 'VIP'
  59. }
  60. // 无购买此levelId
  61. return 'Not-Vip';
  62. } else {
  63. // 游客
  64. return 'Visitor';
  65. }
  66. }
  67. export function hasUserIdentity() {
  68. const auth = cacheManager.get('auth');
  69. if (auth) {
  70. if (auth.cardList.length) {
  71. // VIP
  72. return 'VIP'
  73. }
  74. // 非VIP
  75. return 'Not-Vip';
  76. } else {
  77. // 游客
  78. return 'Visitor';
  79. }
  80. }
  81. export function debounce(func, wait) {
  82. let timeout;
  83. return function(...args) {
  84. // 清除之前的定时器
  85. clearTimeout(timeout);
  86. // 设置新的定时器
  87. timeout = setTimeout(() => {
  88. func.apply(this, args);
  89. }, wait);
  90. };
  91. }
  92. export function findRootNode(tree, targetId, idKey = 'id') {
  93. const path = [];
  94. function traverse(node) {
  95. if (node[idKey] === targetId) return true;
  96. if (node.children) {
  97. for (const child of node.children) {
  98. if (traverse(child)) {
  99. path.unshift(node);
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. for (const root of tree) {
  107. if (traverse(root)) {
  108. return path.length > 0 ? path[0] : root;
  109. }
  110. }
  111. return null;
  112. }
  113. export function findTreeNode(tree, targetId, childrenKey = 'children', idKey = 'id') {
  114. for (const node of tree) {
  115. if (node[idKey] === targetId) return node;
  116. if (node[childrenKey]?.length) {
  117. const found = findTreeNode(node[childrenKey], targetId, childrenKey, idKey);
  118. if (found) return found;
  119. }
  120. }
  121. return null;
  122. }
  123. export function convertTimeToSeconds(timeStr) {
  124. // 分割时间字符串为小时和分钟部分
  125. const parts = timeStr.split(':');
  126. // 解析小时和分钟为整数
  127. const hours = parseInt(parts[0], 10);
  128. const minutes = parseInt(parts[1], 10);
  129. // 计算总秒数:小时 × 3600 + 分钟 × 60
  130. const totalSeconds = hours * 60 + minutes;
  131. return totalSeconds.toString();
  132. }
  133. // 当前用户是否是非游客
  134. export function getUserIsYouke() {
  135. const auth = cacheManager.get('auth');
  136. if (!auth) {
  137. throw new Error('数据异常,未发现用户信息');
  138. }
  139. if (!auth.userName) {
  140. throw new Error('数据异常,无法判定用户身份')
  141. }
  142. return auth.userName.startsWith('youke');
  143. }
  144. export function getFullTimer() {
  145. const now = new Date();
  146. const year = now.getFullYear();
  147. const month = now.getMonth() + 1;
  148. const day = now.getDate();
  149. // 格式:2026-01-29
  150. return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
  151. }
  152. export function useSelectDateForUpdate(code) {
  153. function getDate() {
  154. return cacheManager.get(code);
  155. }
  156. function updateDate() {
  157. cacheManager.set(code, getFullTimer())
  158. }
  159. function isNowDate() {
  160. const nowDate = getDate();
  161. if (!nowDate) {
  162. updateDate();
  163. return false
  164. }
  165. if (nowDate != getFullTimer()) {
  166. updateDate();
  167. }
  168. return nowDate == getFullTimer();
  169. }
  170. function resetDate() {
  171. cacheManager.remove(code)
  172. }
  173. return {
  174. isNowDate,
  175. resetDate
  176. }
  177. }