This commit is contained in:
commie
2025-11-23 01:01:52 +08:00
parent 6ec389ff41
commit 0783e46b4b
21 changed files with 6409 additions and 1264 deletions
+181 -179
View File
@@ -1,213 +1,215 @@
import PinYin from "./pinyin";
export const html2Text = (html) => {
if (!html) {
return "";
}
return html
.replace(/<\/p><p>/g, "\n")
.replace(/\&nbsp;/g, " ")
.replace(/<p>/g, "")
.replace(/<\/p>/g, "")
.replace(/<br>/g, "")
.trim();
if (!html) {
return "";
}
return html
.replace(/<\/p><p>/g, "\n")
.replace(/\&nbsp;/g, " ")
.replace(/<p>/g, "")
.replace(/<\/p>/g, "")
.replace(/<br>/g, "")
.trim();
};
export const parseBr = (content) => {
if (!content) {
return "";
}
return content.replace(/\n/g, "\\n").trim();
if (!content) {
return "";
}
return content.replace(/\n/g, "\\n").trim();
};
export const getEl = (el) => {
return new Promise((resolve) => {
const query = uni.createSelectorQuery().in(this);
query
.select(el)
.boundingClientRect((data) => {
// 存在data,且存在宽和高,视为渲染完毕
resolve(data);
})
.exec();
});
return new Promise((resolve) => {
const query = uni.createSelectorQuery().in(this);
query
.select(el)
.boundingClientRect((data) => {
// 存在data,且存在宽和高,视为渲染完毕
resolve(data);
})
.exec();
});
};
export const getDbDir = () => {
return new Promise((resolve, reject) => {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
fs.root.getDirectory(
"user",
{
create: true,
},
(entry) => {
resolve(entry.fullPath);
},
(error) => {
reject(error);
},
);
});
});
return new Promise((resolve, reject) => {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
fs.root.getDirectory(
"user", {
create: true,
},
(entry) => {
resolve(entry.fullPath);
},
(error) => {
reject(error);
},
);
});
});
};
export const formatChooseData = (data, key = "nickname") => {
const ucfirst = (l1) => {
if (l1.length > 0) {
var first = l1.substr(0, 1).toUpperCase();
var spare = l1.substr(1, l1.length);
return first + spare;
}
};
const ucfirst = (l1) => {
if (l1.length > 0) {
var first = l1.substr(0, 1).toUpperCase();
var spare = l1.substr(1, l1.length);
return first + spare;
}
};
const arraySearch = (l1, l2) => {
for (var name in PinYin) {
if (PinYin[name].indexOf(l1) != -1) {
return ucfirst(name);
break;
}
}
return false;
};
const arraySearch = (l1, l2) => {
for (var name in PinYin) {
if (PinYin[name].indexOf(l1) != -1) {
return ucfirst(name);
break;
}
}
return false;
};
const codefans = (l1) => {
l1 = l1 ?? "unkown";
var l2 = l1.length;
var I1 = "";
var reg = new RegExp("[a-zA-Z0-9- ]");
for (var i = 0; i < l2; i++) {
var val = l1.substr(i, 1);
var name = arraySearch(val, PinYin);
if (reg.test(val)) {
I1 += val;
} else if (name !== false) {
I1 += name;
}
}
I1 = I1.replace(/ /g, "-");
while (I1.indexOf("--") > 0) {
I1 = I1.replace("--", "-");
}
return I1;
};
const codefans = (l1) => {
l1 = l1 ?? "unkown";
var l2 = l1.length;
var I1 = "";
var reg = new RegExp("[a-zA-Z0-9- ]");
for (var i = 0; i < l2; i++) {
var val = l1.substr(i, 1);
var name = arraySearch(val, PinYin);
if (reg.test(val)) {
I1 += val;
} else if (name !== false) {
I1 += name;
}
}
I1 = I1.replace(/ /g, "-");
while (I1.indexOf("--") > 0) {
I1 = I1.replace("--", "-");
}
return I1;
};
var arr = [],
firstName;
var arr = [],
firstName;
for (var i = 0; i < data.length; i++) {
firstName = data[i].initial = codefans(data[i][key]).substr(0, 1);
arr.push(firstName.toUpperCase());
}
for (var i = 0; i < data.length; i++) {
firstName = data[i].initial = codefans(data[i][key]).substr(0, 1);
arr.push(firstName.toUpperCase());
}
var arrlist = [];
for (i = 0; i < arr.length; i++) {
if (arrlist.indexOf(arr[i]) == -1) {
arrlist.push(arr[i]);
}
}
var arrlist = [];
for (i = 0; i < arr.length; i++) {
if (arrlist.indexOf(arr[i]) == -1) {
arrlist.push(arr[i]);
}
}
var dataSort = [];
for (var i = 0; i < arrlist.length; i++) {
dataSort[i] = {
initial: arrlist[i],
};
dataSort[i].data = [];
for (var j = 0; j < data.length; j++) {
if (data[j].initial.toUpperCase() == dataSort[i].initial) {
dataSort[i].data.push(data[j]);
}
}
}
for (var i = 0; i < dataSort.length - 1; i++) {
for (var j = 1; j < dataSort.length - i; j++) {
if (dataSort[j - 1].initial > dataSort[j].initial) {
var a = dataSort[j];
dataSort[j] = dataSort[j - 1];
dataSort[j - 1] = a;
}
}
}
const NomalInitial = "QWERTYUIOPLKJHGFDSAZXCVBNM".split("");
const special = {
initial: "#",
data: [],
};
const newFilterData = dataSort.filter((d) => {
if (!NomalInitial.includes(d.initial)) {
special.data = [...special.data, ...d.data];
} else {
return d;
}
});
if (special.data.length > 0) {
newFilterData.push(special);
}
const indexList = newFilterData.map((item) => item.initial);
const dataList = newFilterData.map((item) => item.data);
return {
indexList,
dataList,
};
var dataSort = [];
for (var i = 0; i < arrlist.length; i++) {
dataSort[i] = {
initial: arrlist[i],
};
dataSort[i].data = [];
for (var j = 0; j < data.length; j++) {
if (data[j].initial.toUpperCase() == dataSort[i].initial) {
dataSort[i].data.push(data[j]);
}
}
}
for (var i = 0; i < dataSort.length - 1; i++) {
for (var j = 1; j < dataSort.length - i; j++) {
if (dataSort[j - 1].initial > dataSort[j].initial) {
var a = dataSort[j];
dataSort[j] = dataSort[j - 1];
dataSort[j - 1] = a;
}
}
}
const NomalInitial = "QWERTYUIOPLKJHGFDSAZXCVBNM".split("");
const special = {
initial: "#",
data: [],
};
const newFilterData = dataSort.filter((d) => {
if (!NomalInitial.includes(d.initial)) {
special.data = [...special.data, ...d.data];
} else {
return d;
}
});
if (special.data.length > 0) {
newFilterData.push(special);
}
const indexList = newFilterData.map((item) => item.initial);
const dataList = newFilterData.map((item) => item.data);
return {
indexList,
dataList,
};
};
export const getPurePath = (path) => {
const prefix = "file://";
const relativeRrefix = "_doc/";
if (path.includes(prefix)) {
path = path.replace(prefix, "");
}
if (path.includes(relativeRrefix)) {
path = plus.io.convertLocalFileSystemURL(path);
}
return path;
const prefix = "file://";
const relativeRrefix = "_doc/";
if (path.includes(prefix)) {
path = path.replace(prefix, "");
}
if (path.includes(relativeRrefix)) {
path = plus.io.convertLocalFileSystemURL(path);
}
return path;
};
export const filterEmptyValue = (obj) => {
for (let key in obj) {
if (obj[key] === "") {
delete obj[key];
}
}
for (let key in obj) {
if (obj[key] === "") {
delete obj[key];
}
}
};
export const toastWithCallback = (message, callBack, duration = 1000) => {
uni.$u.toast(message);
setTimeout(callBack, duration);
uni.$u.toast(message);
setTimeout(callBack, duration);
};
export const checkLoginError = (error) => {
if (!error?.errCode) {
return "操作失败";
}
switch (error.errCode) {
case 20001:
return "密码错误";
case 20002:
return "账号不存在";
case 20003:
return "手机号已经注册";
case 20004:
return "账号已注册";
case 20005:
return "操作过于频繁,请稍后再试";
case 20006:
return "验证码错误";
case 20007:
return "验证码过期";
case 20008:
return "验证码错误次数超过限制,请稍后再试";
case 20009:
return "验证码已被使用";
case 20010:
return "邀请码已被使用";
case 20011:
return "邀请码不存在";
case 20012:
return "操作限制";
case 20014:
return "账号已注册";
default:
return "操作失败";
}
if (error?.msg) {
return error.msg
}
if (!error?.errCode) {
return "操作失败";
}
switch (error.errCode) {
case 20001:
return "密码错误";
case 20002:
return "账号不存在";
case 20003:
return "手机号已经注册";
case 20004:
return "账号已注册";
case 20005:
return "操作过于频繁,请稍后再试";
case 20006:
return "验证码错误";
case 20007:
return "验证码过期";
case 20008:
return "验证码错误次数超过限制,请稍后再试";
case 20009:
return "验证码已被使用";
case 20010:
return "邀请码已被使用";
case 20011:
return "邀请码不存在";
case 20012:
return "操作限制";
case 20014:
return "账号已注册";
default:
return "操作失败";
}
};
+6 -6
View File
@@ -2,12 +2,12 @@
import config from "@/common/config";
// 初始化请求配置
uni.$u.http.setConfig((defaultConfig) => {
/* defaultConfig 为默认全局配置 */
defaultConfig.baseURL = config.getRegisterUrl(); /* 根域名 */
return defaultConfig;
/* defaultConfig 为默认全局配置 */
defaultConfig.baseURL = config.getRegisterUrl(); /* 根域名 */
return defaultConfig;
});
module.exports = (vm) => {
require("./requestInterceptors")(vm);
require("./responseInterceptors")(vm);
};
require("./requestInterceptors")(vm);
require("./responseInterceptors")(vm);
};
+24 -24
View File
@@ -1,30 +1,30 @@
import appConfig from "@/common/config";
import { v4 as uuidV4 } from "uuid";
import {
v4 as uuidV4
} from "uuid";
/**
* 请求拦截
* @param {Object} http
*/
module.exports = (vm) => {
uni.$u.http.interceptors.request.use(
(config) => {
// 可使用async await 做异步操作
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
config.data = config.data || {};
if (config.custom.isIMApi) {
config.baseURL = appConfig.getApiUrl();
}
if (config.custom.isPgyerApi) {
config.baseURL = "https://www.pgyer.com";
}
config.header = {
...config.header,
operationID: uuidV4(),
};
// 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
return config;
},
(
config, // 可使用async await 做异步操作
) => Promise.reject(config),
);
};
uni.$u.http.interceptors.request.use(
(config) => {
// 可使用async await 做异步操作
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
config.data = config.data || {};
if (config.custom.isIMApi) {
config.baseURL = appConfig.getApiUrl();
}
config.header = {
...config.header,
token:uni.getStorageSync("BusinessToken"),
operationID: uuidV4(),
};
// 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
return config;
},
(
config, // 可使用async await 做异步操作
) => Promise.reject(config),
);
};
+39 -29
View File
@@ -3,32 +3,42 @@
* @param {Object} http
*/
module.exports = (vm) => {
uni.$u.http.interceptors.response.use(
(response) => {
/* 对响应成功做点什么 可使用async await 做异步操作*/
const data = response.data;
// 自定义参数
const custom = response.config?.custom;
if (data.errCode !== 0 && data.code !== 0) {
// 服务端返回的状态码不等于200,则reject()
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
// if (custom.toast !== false) {
// uni.$u.toast(data.message)
// }
// 如果需要catch返回,则进行reject
// if (custom?.catch) {
console.error('http catch rejected', data)
return Promise.reject(data);
// } else {
// // 否则返回一个pending中的promise
// return new Promise(() => { })
// }
}
return data.data || {};
},
(response) => {
/* 对响应错误做点什么 statusCode !== 200*/
return Promise.reject(response);
},
);
};
uni.$u.http.interceptors.response.use((response) => {
console.log("\n-"
+"\n============================================================="
+"\nurl:"+response.config.fullPath
+"\nparams:"+JSON.stringify(response.config.params)
+"\ndata:"+JSON.stringify(response.config.data)
+"\nheader:"+JSON.stringify(response.config.header)
+"\nresponse:"+JSON.stringify(response.data)
+"\n=============================================================\n");
/* 对响应成功做点什么 可使用async await 做异步操作*/
const data = response.data;
// 自定义参数
const custom = response.config?.custom;
if (data.errCode !== 0 && data.code !== 0) {
if(data?.msg){
uni.$u.toast(data.msg)
}
// 服务端返回的状态码不等于200,则reject()
// 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
// if (custom.toast !== false) {
// uni.$u.toast(data.message)
// }
// 如果需要catch返回,则进行reject
// if (custom?.catch) {
console.error('http catch rejected', data)
return Promise.reject(data);
// } else {
// // 否则返回一个pending中的promise
// return new Promise(() => { })
// }
}
return data.data || {};
},
(response) => {
/* 对响应错误做点什么 statusCode !== 200*/
return Promise.reject(response);
},
);
};