common.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import cacheManager from "./cacheManager.js"
  2. import {nextTick} from "vue";
  3. /**
  4. * 显示消息提示框
  5. * @param content 提示的标题
  6. */
  7. export function toast(content) {
  8. uni.showToast({
  9. icon: 'none',
  10. duration:3000,
  11. title: content
  12. })
  13. }
  14. /**
  15. * 显示模态弹窗
  16. * @param content 提示的标题
  17. */
  18. export function showConfirm(content) {
  19. return new Promise((resolve, reject) => {
  20. uni.showModal({
  21. title: '提示',
  22. content: content,
  23. cancelText: '取消',
  24. confirmText: '确定',
  25. success: function(res) {
  26. resolve(res)
  27. }
  28. })
  29. })
  30. }
  31. /**
  32. * 参数处理
  33. * @param params 参数
  34. */
  35. export function tansParams(params) {
  36. let result = ''
  37. // FIXME 拼接参数
  38. return result
  39. }
  40. /**
  41. * @summary 获取请求异常与正常返回
  42. * @param {Object} promise
  43. */
  44. export function catchError(promise) {
  45. return new Promise((resolve,reject) => {
  46. promise.then(data => {
  47. resolve([undefined, data.data])
  48. }).catch(err => {
  49. reject([err])
  50. })
  51. })
  52. }
  53. // 是否是会员
  54. export function getUserIdentity() {
  55. const auth = cacheManager.get('auth');
  56. if (auth) {
  57. if ((auth.levelIdList || []).some(item => item == auth.levelId)) {
  58. // 购买此levelId
  59. return 'VIP'
  60. }
  61. // 无购买此levelId
  62. return 'Not-Vip';
  63. } else {
  64. // 游客
  65. return 'Visitor';
  66. }
  67. }
  68. export function hasUserIdentity() {
  69. const auth = cacheManager.get('auth');
  70. if (auth) {
  71. if (auth.cardList.length) {
  72. // VIP
  73. return 'VIP'
  74. }
  75. // 非VIP
  76. return 'Not-Vip';
  77. } else {
  78. // 游客
  79. return 'Visitor';
  80. }
  81. }
  82. export function debounce(func, wait) {
  83. let timeout;
  84. return function(...args) {
  85. // 清除之前的定时器
  86. clearTimeout(timeout);
  87. // 设置新的定时器
  88. timeout = setTimeout(() => {
  89. func.apply(this, args);
  90. }, wait);
  91. };
  92. }
  93. export function findRootNode(tree, targetId, idKey = 'id') {
  94. const path = [];
  95. function traverse(node) {
  96. if (node[idKey] === targetId) return true;
  97. if (node.children) {
  98. for (const child of node.children) {
  99. if (traverse(child)) {
  100. path.unshift(node);
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. for (const root of tree) {
  108. if (traverse(root)) {
  109. return path.length > 0 ? path[0] : root;
  110. }
  111. }
  112. return null;
  113. }
  114. export function findTreeNode(tree, targetId, childrenKey = 'children', idKey = 'id') {
  115. for (const node of tree) {
  116. if (node[idKey] === targetId) return node;
  117. if (node[childrenKey]?.length) {
  118. const found = findTreeNode(node[childrenKey], targetId, childrenKey, idKey);
  119. if (found) return found;
  120. }
  121. }
  122. return null;
  123. }
  124. export function convertTimeToSeconds(timeStr) {
  125. // 分割时间字符串为小时和分钟部分
  126. const parts = timeStr.split(':');
  127. // 解析小时和分钟为整数
  128. const hours = parseInt(parts[0], 10);
  129. const minutes = parseInt(parts[1], 10);
  130. // 计算总秒数:小时 × 3600 + 分钟 × 60
  131. const totalSeconds = hours * 60 + minutes;
  132. return totalSeconds.toString();
  133. }
  134. // 当前用户是否是非游客
  135. export function getUserIsYouke() {
  136. const auth = cacheManager.get('auth');
  137. console.log('user', auth)
  138. if (!auth) {
  139. throw new Error('数据异常,未发现用户信息');
  140. }
  141. if (!auth.userName) {
  142. throw new Error('数据异常,无法判定用户身份')
  143. }
  144. return auth.userName.startsWith('youke');
  145. }
  146. export function getFullTimer() {
  147. const now = new Date();
  148. const year = now.getFullYear();
  149. const month = now.getMonth() + 1;
  150. const day = now.getDate();
  151. // 格式:2026-01-29
  152. return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
  153. }
  154. export function useSelectDateForUpdate(code) {
  155. function getDate() {
  156. return cacheManager.get(code);
  157. }
  158. function updateDate() {
  159. cacheManager.set(code, getFullTimer())
  160. }
  161. function isNowDate() {
  162. const nowDate = getDate();
  163. if (!nowDate) {
  164. updateDate();
  165. return false
  166. }
  167. if (nowDate != getFullTimer()) {
  168. updateDate();
  169. }
  170. return nowDate == getFullTimer();
  171. }
  172. function resetDate() {
  173. cacheManager.remove(code)
  174. }
  175. return {
  176. isNowDate,
  177. resetDate
  178. }
  179. }
  180. export function getDataFromStr(strdata) {
  181. if (!strdata) {
  182. return []
  183. }
  184. return strdata.toString().split(',')
  185. }
  186. export function isTargetInSameGroup(arr, currentElement, targetDanyuanId) {
  187. // 1. 参数校验
  188. if (!Array.isArray(arr) || arr.length === 0) return false;
  189. if (!currentElement || !targetDanyuanId) return false;
  190. // 2. 将数组拆分为4个一组的二维数组
  191. const chunkSize = 4;
  192. const groupedArray = [];
  193. for (let i = 0; i < arr.length; i += chunkSize) {
  194. groupedArray.push(arr.slice(i, i + chunkSize));
  195. }
  196. // 3. 查找包含当前元素的子数组
  197. const currentGroup = groupedArray.find(group =>
  198. group.some(item => item.danyuanId === currentElement.danyuanId)
  199. );
  200. // 4. 判断目标jieId是否在该子数组中
  201. if (!currentGroup) return false;
  202. return currentGroup.some(item => item.danyuanId === targetDanyuanId);
  203. }
  204. export function useActiveDomIntoView(classNameParent, classNameActive) {
  205. nextTick(() => {
  206. const container = document.querySelector(classNameParent);
  207. const highlightItem = document.querySelector(classNameActive);
  208. // 1. 检查元素是否已在可视区
  209. const containerRect = container.getBoundingClientRect();
  210. const activeRect = highlightItem.getBoundingClientRect();
  211. const isVisible = activeRect.left >= containerRect.left &&
  212. activeRect.right <= containerRect.right;
  213. if (isVisible) return;
  214. // 2. 优先使用 scrollIntoView
  215. const supportsSmoothScroll = 'scrollBehavior' in document.documentElement.style;
  216. if (supportsSmoothScroll) {
  217. highlightItem.scrollIntoView({
  218. behavior: 'smooth',
  219. inline: 'center',
  220. block: 'nearest'
  221. });
  222. } else {
  223. // 3. 降级方案:瞬时滚动 + 手动动画
  224. const targetPos = highlightItem.offsetLeft - container.offsetLeft;
  225. container.scrollTo({ left: targetPos });
  226. }
  227. });
  228. }