dl_uniapp/utils/common.js
2025-03-27 09:50:13 +08:00

122 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
getJSONData,
setJSONData,
removeJSONData,
setStrData,
getStrData
} from '@/utils/auth'
import constant from '@/utils/constant'
/**
* 判断某用户是否有某权益
* @param {Object} code 权益code
*/
export function hasRights(code) {
//取用户当前身份
let userType = getStrData(constant.userTypeKey)
let userRights = {}
if (constant.bz == userType) {
//博主
userRights = getJSONData(constant.userRightsKey)['bz']
} else {
//通告主
userRights = getJSONData(constant.userRightsKey)['tgz']
}
//默认无权限
let rtnData = false
if (userRights.hasOwnProperty(code)) {
rtnData = userRights[code]
}
return rtnData
}/**
* 切换用户当前身份
* @param {Object} value 身份值
*/
export function changeUserType(value) {
setStrData(constant.userTypeKey, value)
}
/**
* 显示消息提示框
* @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 = ''
for (const propName of Object.keys(params)) {
const value = params[propName]
var part = encodeURIComponent(propName) + "="
if (value !== null && value !== "" && typeof(value) !== "undefined") {
if (typeof value === 'object') {
for (const key of Object.keys(value)) {
if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
let params = propName + '[' + key + ']'
var subPart = encodeURIComponent(params) + "="
result += subPart + encodeURIComponent(value[key]) + "&"
}
}
} else {
result += part + encodeURIComponent(value) + "&"
}
}
}
return result
}
/**
* 数字单位转换
* @param {Object} num
*/
export function formatNumberWithUnits(num) {
const units = ['', 'k', 'w', 'kw'];
const absNum = Math.abs(num); // 获取绝对值以处理负数
// 处理小于1000的情况直接返回原数字的字符串形式
if (absNum < 1000) {
return absNum.toString();
}
// 寻找合适的单位
let unitIndex = 0;
if (absNum > 10000000) {
absNum /= 10000000;
unitIndex = 3;
} else if (absNum > 10000) {
absNum /= 10000;
unitIndex = 2;
} else if (absNum > 1000) {
absNum /= 1000;
unitIndex = 1;
}
// 格式化数字,并添加单位
const formattedNum = absNum.toFixed(2).replace(/\.?0+$/, ''); // 保留两位小数并去除末尾的零
return formattedNum + units[unitIndex];
}