common.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import cacheManager from "./cacheManager.js"
  2. /**
  3. * 显示消息提示框
  4. * @param content 提示的标题
  5. */
  6. export function toast(content) {
  7. uni.showToast({
  8. icon: 'none',
  9. title: content
  10. })
  11. }
  12. /**
  13. * 显示模态弹窗
  14. * @param content 提示的标题
  15. */
  16. export function showConfirm(content) {
  17. return new Promise((resolve, reject) => {
  18. uni.showModal({
  19. title: '提示',
  20. content: content,
  21. cancelText: '取消',
  22. confirmText: '确定',
  23. success: function(res) {
  24. resolve(res)
  25. }
  26. })
  27. })
  28. }
  29. /**
  30. * 参数处理
  31. * @param params 参数
  32. */
  33. export function tansParams(params) {
  34. let result = ''
  35. // FIXME 拼接参数
  36. return result
  37. }
  38. /**
  39. * @summary 获取请求异常与正常返回
  40. * @param {Object} promise
  41. */
  42. export function catchError(promise) {
  43. return new Promise((resolve, reject) => {
  44. promise.then(data => {
  45. resolve([undefined, data.data])
  46. }).catch(err => {
  47. reject([err])
  48. })
  49. })
  50. }
  51. // 是否是会员
  52. const IdentityType = {
  53. VIP: 'VIP',
  54. NOT_VIP: 'Not-Vip',
  55. VISITOR: 'Visitor'
  56. };
  57. // export function getUserIdentity() {
  58. // const auth = cacheManager.get('auth');
  59. // if (!auth) return IdentityType.VISITOR;
  60. // const hasPurchased = auth.subjectId == 2 && auth.typeId == 1 ?
  61. // (auth.levelIdList || []).includes(auth.levelId.split(',')[0]) :
  62. // (auth.levelIdList || []).includes(auth.levelId);
  63. // console.log('hasPurchased',hasPurchased);
  64. // return hasPurchased ? IdentityType.VIP : IdentityType.NOT_VIP;
  65. // }
  66. export function getUserIdentity() {
  67. const auth = cacheManager.get('auth');
  68. if(auth.subjectId == 2 && auth.typeId == 1){
  69. if (auth) {
  70. const arr = auth.levelId.split(",");
  71. if ((auth.levelIdList || []).some(item => item == arr[0])) {
  72. // 购买此levelId
  73. return 'VIP'
  74. }
  75. // 无购买此levelId
  76. return 'Not-Vip';
  77. } else {
  78. // 游客
  79. return 'Visitor';
  80. }
  81. }else{
  82. //旧数学 新数学 旧英语
  83. if (auth) {
  84. if ((auth.levelIdList || []).some(item => item == auth.levelId)) {
  85. // 购买此levelId
  86. return 'VIP'
  87. }
  88. // 无购买此levelId
  89. return 'Not-Vip';
  90. } else {
  91. // 游客
  92. return 'Visitor';
  93. }
  94. }
  95. }
  96. export function hasUserIdentity() {
  97. const auth = cacheManager.get('auth');
  98. if (auth) {
  99. if (auth.cardList.length) {
  100. // VIP
  101. return 'VIP'
  102. }
  103. // 非VIP
  104. return 'Not-Vip';
  105. } else {
  106. // 游客
  107. return 'Visitor';
  108. }
  109. }
  110. export function debounce(func, wait) {
  111. let timeout;
  112. return function(...args) {
  113. // 清除之前的定时器
  114. clearTimeout(timeout);
  115. // 设置新的定时器
  116. timeout = setTimeout(() => {
  117. func.apply(this, args);
  118. }, wait);
  119. };
  120. }
  121. export function findRootNode(tree, targetId, idKey = 'id') {
  122. const path = [];
  123. function traverse(node) {
  124. if (node[idKey] === targetId) return true;
  125. if (node.children) {
  126. for (const child of node.children) {
  127. if (traverse(child)) {
  128. path.unshift(node);
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. for (const root of tree) {
  136. if (traverse(root)) {
  137. return path.length > 0 ? path[0] : root;
  138. }
  139. }
  140. return null;
  141. }
  142. export function findTreeNode(tree, targetId, childrenKey = 'children', idKey = 'id') {
  143. for (const node of tree) {
  144. if (node[idKey] === targetId) return node;
  145. if (node[childrenKey]?.length) {
  146. const found = findTreeNode(node[childrenKey], targetId, childrenKey, idKey);
  147. if (found) return found;
  148. }
  149. }
  150. return null;
  151. }
  152. export function getDataFromStr(strdata) {
  153. if (!strdata) {
  154. return []
  155. }
  156. return strdata.toString().split(',')
  157. }