This commit is contained in:
cansnow
2025-11-27 03:55:38 +08:00
parent 1626f0c52a
commit 198c3dd4a5
24 changed files with 2043 additions and 201 deletions
+90
View File
@@ -50,6 +50,96 @@ export const getDbDir = () => {
});
});
};
export const formatIndexListData = (list, key = "nickname") => {
// 兼容:list 为数组,数组元素可以是字符串或对象;key 为要分组的字段名(若元素为字符串则忽略 key)
if (!Array.isArray(list) || list.length === 0) return [];
const ucfirst = (l1) => {
if (!l1) return "";
if (l1.length > 0) {
var first = l1.substr(0, 1).toUpperCase();
var spare = l1.substr(1, l1.length);
return first + spare;
}
return l1;
};
const arraySearch = (l1) => {
for (var name in PinYin) {
if (PinYin[name].indexOf(l1) != -1) {
return ucfirst(name);
}
}
return false;
};
// 将字符串转为可排序的拼音/字母表示
const codefans = (l1) => {
l1 = l1 ?? "";
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);
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;
};
// 构建分组 map
const groups = {};
for (let i = 0; i < list.length; i++) {
const item = list[i];
const val = (typeof item === "string") ? item : (item && item[key]) ? item[key] : "";
const coded = codefans(String(val));
let initial = (coded && coded.length > 0) ? coded.substr(0, 1).toUpperCase() : "#";
if (!/^[A-Z]$/.test(initial)) initial = "#";
if (!groups[initial]) groups[initial] = [];
groups[initial].push({
raw: item,
sortKey: coded || String(val)
});
}
// 按字母顺序构建结果,A-Z 然后 #
const letters = Object.keys(groups).filter((l) => l !== "#").sort();
const result = [];
for (const L of letters) {
// 组内按照 sortKey 排序
groups[L].sort((a, b) => {
if (a.sortKey < b.sortKey) return -1;
if (a.sortKey > b.sortKey) return 1;
return 0;
});
result.push({
letter: L,
data: groups[L].map((x) => x.raw)
});
}
// 特殊字符组放最后
if (groups["#"] && groups["#"].length > 0) {
groups["#"].sort((a, b) => {
if (a.sortKey < b.sortKey) return -1;
if (a.sortKey > b.sortKey) return 1;
return 0;
});
result.push({
letter: "#",
data: groups["#"].map((x) => x.raw)
});
}
return result;
}
export const formatChooseData = (data, key = "nickname") => {
const ucfirst = (l1) => {
+148
View File
@@ -0,0 +1,148 @@
import _ from "lodash";
//import i18n from '@/locales'
import base from '@/common/config';
//import store from "@/store";
export default{
cdn(v){
v= v || "";
v = v.replace(/\\/ig,"/").replace('/\/\/ig',"/");
if(_.isString(v)){
if(v.substr(0,5) == 'blob:'){
return v;
}
v= v.substring(0,1) == '/' ? v : '/'+v;
return base.cdnUrl+''+v;
}
return "";
},
goto(url,type){
console.log(url);
type = type || '0'; //0 navigateTo 1 redirectTo
if(url){
if(_.isInteger(url)){
uni.navigateBack({
delta:url,
})
}else{
url+="";
if(url.substr(0,6) != '/pages'){
url='/pages'+url
}
if(type == '1'){
return uni.redirectTo({
url:url
});
}
uni.navigateTo({
url:url
})
}
}
},
showToast(msg,url,icon){
const _this = this;
//msg = i18n.t(msg);
// #ifdef APP
plus.nativeUI.closeToast();
plus.nativeUI.toast(msg,{
align:'center',
verticalAlign:"center",
style:"inline",
icon:icon=='error' ? '/static/img/common/error.png' : '/static/img/common/success.png',
iconWidth:24,
iconHeight:24
});
if(url){
setTimeout(()=>{
_this.goto(url);
},3000)
}
// #endif
// #ifndef APP
uni.showToast({
//image:icon=='error' ? '/static/img/common/error.png' : '/static/img/common/success.png',
icon:icon=='error' ? icon : 'success',
title:msg,
showToast:3000,
complete:()=>{
if(url){
setTimeout(()=>{
_this.goto(url);
},3000)
}
}
});
// #endif
},
error(msg,url){
this.showToast(msg,url,'error');
},
success(msg,url){
this.showToast(msg,url,'success');
},
scan(){
uni.scanCode({
success(res){
/**
* result 所扫码的内容
scanType 所扫码的类型 App、微信小程序、百度小程序、QQ小程序、京东小程序、支付宝小程序
charSet 所扫码的字符集 App、微信小程序、百度小程序(所扫码的字符集,仅支持 Android 系统)、QQ小程序、京东小程序
path 当所扫的码为当前应用的合法二维码时,会返回此字段,内容为二维码携带的 path。 微信小程序、QQ小程序、京东小程序
rawData 原始数据,base64 编码 微信小程序、QQ小程序、京东小程序、支付宝小程序
code 扫码所得数据 支付宝小程序
qrCode 扫描二维码时返回二维码数据 支付宝小程序
barCode 扫描条形码时返回条形码数据 支付宝小程序
imageChannel 来源 支付宝小程序
*/
if(res.result){
if(res.result.indexOf('blackcatp:/')){
uni.navigateTo({
url:res.result.substring(11)
})
}else{
this.success(res.result)
}
}
},
fail(res){
},
complete(res){
console.log(res)
}
});
},
copy(v){
let that = this;
uni.setClipboardData({
data:v+'',
success() {
that.success('复制成功');
}
})
},
toDate(time) {
var date = new Date(time * 1000);
var fmt = 'yyyy-MM-dd hh:mm:ss';
var o = {
'M+': date.getMonth() + 1, //月份
'd+': date.getDate(), //日
'h+': date.getHours(), //小时
'm+': date.getMinutes(), //分
's+': date.getSeconds(), //秒
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
S: date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
for (var k in o) if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
return fmt;
},
formatAmount(v,wei){
if(!v){return 0.00;}
v=v+"";
v = parseFloat(v).toFixed(wei || 2);
return parseFloat(v);
}
}