123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import cacheManager from "./cacheManager.js"
- /**
- * 显示消息提示框
- * @param content 提示的标题
- */
- export function toast(content) {
- uni.showToast({
- icon: 'none',
- title: content
- })
- }
- /**
- * 显示模态弹窗
- * @param content 提示的标题
- */
- export function showConfirm(content) {
- return new Promise((resolve, reject) => {
- uni.showModal({
- title: '提示',
- content: content,
- cancelText: '取消',
- confirmText: '确定',
- success: function(res) {
- resolve(res)
- }
- })
- })
- }
- /**
- * 参数处理
- * @param params 参数
- */
- export function tansParams(params) {
- let result = ''
- // FIXME 拼接参数
- return result
- }
- /**
- * @summary 获取请求异常与正常返回
- * @param {Object} promise
- */
- export function catchError(promise) {
- return new Promise((resolve,reject) => {
- promise.then(data => {
- resolve([undefined, data.data])
- }).catch(err => {
- reject([err])
- })
- })
- }
- // 是否是会员
- export function getUserIdentity() {
- const auth = cacheManager.get('auth');
- if (auth) {
- if ((auth.levelIdList || []).some(item => item == auth.levelId)) {
- // 购买此levelId
- return 'VIP'
- }
- // 无购买此levelId
- return 'Not-Vip';
- } else {
- // 游客
- return 'Visitor';
- }
- }
- export function hasUserIdentity() {
- const auth = cacheManager.get('auth');
- if (auth) {
- if (auth.cardList.length) {
- // VIP
- return 'VIP'
- }
- // 非VIP
- return 'Not-Vip';
- } else {
- // 游客
- return 'Visitor';
- }
- }
- export function debounce(func, wait) {
- let timeout;
- return function(...args) {
- // 清除之前的定时器
- clearTimeout(timeout);
- // 设置新的定时器
- timeout = setTimeout(() => {
- func.apply(this, args);
- }, wait);
- };
- }
- export function findRootNode(tree, targetId, idKey = 'id') {
- const path = [];
-
- function traverse(node) {
- if (node[idKey] === targetId) return true;
- if (node.children) {
- for (const child of node.children) {
- if (traverse(child)) {
- path.unshift(node);
- return true;
- }
- }
- }
- return false;
- }
-
- for (const root of tree) {
- if (traverse(root)) {
- return path.length > 0 ? path[0] : root;
- }
- }
- return null;
- }
- export function findTreeNode(tree, targetId, childrenKey = 'children', idKey = 'id') {
- for (const node of tree) {
- if (node[idKey] === targetId) return node;
- if (node[childrenKey]?.length) {
- const found = findTreeNode(node[childrenKey], targetId, childrenKey, idKey);
- if (found) return found;
- }
- }
- return null;
- }
|