common.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. 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. const IdentityType = {
  54. VIP: 'VIP',
  55. NOT_VIP: 'Not-Vip',
  56. VISITOR: 'Visitor'
  57. };
  58. // export function getUserIdentity() {
  59. // const auth = cacheManager.get('auth');
  60. // if (!auth) return IdentityType.VISITOR;
  61. // const hasPurchased = auth.subjectId == 2 && auth.typeId == 1 ?
  62. // (auth.levelIdList || []).includes(auth.levelId.split(',')[0]) :
  63. // (auth.levelIdList || []).includes(auth.levelId);
  64. // console.log('hasPurchased',hasPurchased);
  65. // return hasPurchased ? IdentityType.VIP : IdentityType.NOT_VIP;
  66. // }
  67. export function getUserIdentity() {
  68. const auth = cacheManager.get('auth');
  69. if (auth.subjectId == 2 && auth.typeId == 1) {
  70. if (auth) {
  71. const arr = getDataFromStr(auth.levelId);
  72. if ((auth.levelIdList || []).some(item => item == arr[0])) {
  73. // 购买此levelId
  74. return 'VIP'
  75. }
  76. // 无购买此levelId
  77. return 'Not-Vip';
  78. } else {
  79. // 游客
  80. return 'Visitor';
  81. }
  82. } else {
  83. //旧数学 新数学 旧英语
  84. if (auth) {
  85. if ((auth.levelIdList || []).some(item => item == auth.levelId)) {
  86. // 购买此levelId
  87. return 'VIP'
  88. }
  89. // 无购买此levelId
  90. return 'Not-Vip';
  91. } else {
  92. // 游客
  93. return 'Visitor';
  94. }
  95. }
  96. }
  97. export function hasUserIdentity() {
  98. const auth = cacheManager.get('auth');
  99. if (auth) {
  100. if (auth.cardList.length) {
  101. // VIP
  102. return 'VIP'
  103. }
  104. // 非VIP
  105. return 'Not-Vip';
  106. } else {
  107. // 游客
  108. return 'Visitor';
  109. }
  110. }
  111. export function debounce(func, wait) {
  112. let timeout;
  113. return function(...args) {
  114. // 清除之前的定时器
  115. clearTimeout(timeout);
  116. // 设置新的定时器
  117. timeout = setTimeout(() => {
  118. func.apply(this, args);
  119. }, wait);
  120. };
  121. }
  122. export function findRootNode(tree, targetId, idKey = 'id') {
  123. const path = [];
  124. function traverse(node) {
  125. if (node[idKey] == targetId) return true;
  126. if (node.children) {
  127. for (const child of node.children) {
  128. if (traverse(child)) {
  129. path.unshift(node);
  130. return true;
  131. }
  132. }
  133. }
  134. return false;
  135. }
  136. for (const root of tree) {
  137. if (traverse(root)) {
  138. return path.length > 0 ? path[0] : root;
  139. }
  140. }
  141. return null;
  142. }
  143. export function findTreeNode(tree, targetId, childrenKey = 'children', idKey = 'id') {
  144. for (const node of tree) {
  145. if (node[idKey] == targetId) return node;
  146. if (node[childrenKey]?.length) {
  147. const found = findTreeNode(node[childrenKey], targetId, childrenKey, idKey);
  148. if (found) return found;
  149. }
  150. }
  151. return null;
  152. }
  153. export function getDataFromStr(strdata) {
  154. if (!strdata) {
  155. return []
  156. }
  157. return strdata.toString().split(',')
  158. }
  159. export function useActiveDomIntoView(classNameParent, classNameActive) {
  160. nextTick(() => {
  161. const container = document.querySelector(classNameParent);
  162. const highlightItem = document.querySelector(classNameActive);
  163. // 1. 检查元素是否已在可视区
  164. const containerRect = container.getBoundingClientRect();
  165. const activeRect = highlightItem.getBoundingClientRect();
  166. const isVisible = activeRect.left >= containerRect.left &&
  167. activeRect.right <= containerRect.right;
  168. if (isVisible) return;
  169. // 2. 优先使用 scrollIntoView
  170. const supportsSmoothScroll = 'scrollBehavior' in document.documentElement.style;
  171. if (supportsSmoothScroll) {
  172. highlightItem.scrollIntoView({
  173. behavior: 'smooth',
  174. inline: 'center',
  175. block: 'nearest'
  176. });
  177. } else {
  178. // 3. 降级方案:瞬时滚动 + 手动动画
  179. const targetPos = highlightItem.offsetLeft - container.offsetLeft;
  180. container.scrollTo({ left: targetPos });
  181. }
  182. });
  183. }
  184. export function isTargetInSameGroup(arr, currentElement, targetJieId) {
  185. // 1. 参数校验
  186. if (!Array.isArray(arr) || arr.length === 0) return false;
  187. if (!currentElement || !targetJieId) return false;
  188. // 2. 将数组拆分为4个一组的二维数组
  189. const chunkSize = 4;
  190. const groupedArray = [];
  191. for (let i = 0; i < arr.length; i += chunkSize) {
  192. groupedArray.push(arr.slice(i, i + chunkSize));
  193. }
  194. // 3. 查找包含当前元素的子数组
  195. const currentGroup = groupedArray.find(group =>
  196. group.some(item => item.jieId === currentElement.jieId)
  197. );
  198. // 4. 判断目标jieId是否在该子数组中
  199. if (!currentGroup) return false;
  200. return currentGroup.some(item => item.jieId === targetJieId);
  201. }