update 增加云函数
This commit is contained in:
parent
b5b4d3c90d
commit
f54aacadfc
@ -10,9 +10,13 @@
|
|||||||
"default" : {
|
"default" : {
|
||||||
"launchtype" : "local"
|
"launchtype" : "local"
|
||||||
},
|
},
|
||||||
|
"h5" : {
|
||||||
|
"launchtype" : "local"
|
||||||
|
},
|
||||||
"mp-weixin" : {
|
"mp-weixin" : {
|
||||||
"launchtype" : "local"
|
"launchtype" : "local"
|
||||||
},
|
},
|
||||||
|
"provider" : "aliyun",
|
||||||
"type" : "uniCloud"
|
"type" : "uniCloud"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -65,20 +65,21 @@
|
|||||||
"sdkConfigs" : {
|
"sdkConfigs" : {
|
||||||
"maps" : {},
|
"maps" : {},
|
||||||
"push" : {
|
"push" : {
|
||||||
"unipush" : {
|
|
||||||
"offline" : true,
|
|
||||||
"fcm" : {},
|
|
||||||
"honor" : {},
|
|
||||||
"meizu" : {},
|
|
||||||
"mi" : {},
|
|
||||||
"vivo" : {},
|
|
||||||
"oppo" : {},
|
|
||||||
"hms" : {}
|
|
||||||
},
|
|
||||||
"igexin" : {
|
"igexin" : {
|
||||||
"appid" : "KXgzOaKSzd5HG3p9IPaVa8",
|
"appid" : "KXgzOaKSzd5HG3p9IPaVa8",
|
||||||
"appkey" : "neXXX9r1Tc7gMxN2PIcHA1",
|
"appkey" : "neXXX9r1Tc7gMxN2PIcHA1",
|
||||||
"appsecret" : "aQQys9eufd8KHH1Y0kfQm6"
|
"appsecret" : "aQQys9eufd8KHH1Y0kfQm6"
|
||||||
|
},
|
||||||
|
"unipush" : {
|
||||||
|
"version" : "2",
|
||||||
|
"offline" : true,
|
||||||
|
"hms" : {},
|
||||||
|
"oppo" : {},
|
||||||
|
"vivo" : {},
|
||||||
|
"mi" : {},
|
||||||
|
"meizu" : {},
|
||||||
|
"honor" : {},
|
||||||
|
"fcm" : {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@ -468,6 +468,9 @@ import {
|
|||||||
import navBar from "@/components/uni-nav-bar/uni-nav-bar";
|
import navBar from "@/components/uni-nav-bar/uni-nav-bar";
|
||||||
import tuiRadio from "@/components/tui-radio/tui-radio.vue";
|
import tuiRadio from "@/components/tui-radio/tui-radio.vue";
|
||||||
import tuiRadioGroup from "@/components/tui-radio-group/tui-radio-group.vue";
|
import tuiRadioGroup from "@/components/tui-radio-group/tui-radio-group.vue";
|
||||||
|
import { compressImg } from '@/utils/tool.js'
|
||||||
|
|
||||||
|
|
||||||
const orcImgTypeConf = {
|
const orcImgTypeConf = {
|
||||||
FR_ID_CARD_FRONT: "FR_ID_CARD_FRONT",
|
FR_ID_CARD_FRONT: "FR_ID_CARD_FRONT",
|
||||||
FR_ID_CARD_BEHIND: "FR_ID_CARD_BEHIND",
|
FR_ID_CARD_BEHIND: "FR_ID_CARD_BEHIND",
|
||||||
@ -546,6 +549,10 @@ export default {
|
|||||||
license_type_name: "",
|
license_type_name: "",
|
||||||
orcTimeout: null,
|
orcTimeout: null,
|
||||||
action: "",
|
action: "",
|
||||||
|
limitType: ['png', 'jpg', 'jpeg'], //允许的图片后缀
|
||||||
|
fileMaxSize: 1 * 1024 * 1024, // 超出1M开启压缩
|
||||||
|
maxSize:5 * 1024 * 1024, //图片最大不能超过20M
|
||||||
|
fileMinSize: 5 * 1024, // 最小为5KB
|
||||||
fileList: [],
|
fileList: [],
|
||||||
fileList2: [],
|
fileList2: [],
|
||||||
fileList3: [],
|
fileList3: [],
|
||||||
@ -711,8 +718,46 @@ export default {
|
|||||||
overSize(e) {
|
overSize(e) {
|
||||||
uni.$u.toast("上传图片大小不能超过8MB!");
|
uni.$u.toast("上传图片大小不能超过8MB!");
|
||||||
},
|
},
|
||||||
|
compressImage(url) {
|
||||||
|
return new Promise((reslove, reject) => {
|
||||||
|
const tempFilePath = url //url是选中图片的路径
|
||||||
|
uni.compressImage({
|
||||||
|
src: tempFilePath,
|
||||||
|
quality: 90, //压缩的程度
|
||||||
|
success: (res) => {
|
||||||
|
reslove(res.tempFilePath) //压缩成功返回的路径
|
||||||
|
},
|
||||||
|
fail: (error) => {
|
||||||
|
console.log('压缩失败', error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
async handerCompressImg(source, compressionRatio) {
|
||||||
|
let that = this;
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
compressImg(source.url, compressionRatio, source.type, compressRes => {
|
||||||
|
resolve(compressRes);
|
||||||
|
})
|
||||||
|
}).then((res) => {
|
||||||
|
source.size = res.size
|
||||||
|
// window.URL.revokeObjectURL(source.url) // 删除被压缩的缓存文件,这里注意,如果是相册选择上传,可能会删除相册的图片
|
||||||
|
source.url = res.source
|
||||||
|
source.thumb = res.source
|
||||||
|
return source
|
||||||
|
}).catch(err => {
|
||||||
|
console.log('图片压缩失败', err)
|
||||||
|
})
|
||||||
|
},
|
||||||
async getOcrText(filePath, file, type) {
|
async getOcrText(filePath, file, type) {
|
||||||
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
|
||||||
|
//#ifdef APP-PLUS
|
||||||
|
filePath = await this.compressImage(filePath);
|
||||||
|
//#endif
|
||||||
|
|
||||||
|
|
||||||
const batchNoRes = await batchNoApi(filePath, file, type);
|
const batchNoRes = await batchNoApi(filePath, file, type);
|
||||||
const batchNo = batchNoRes.batchNo;
|
const batchNo = batchNoRes.batchNo;
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
@ -739,10 +784,125 @@ export default {
|
|||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
//营业执照
|
/**判断文件类型是否正确 */
|
||||||
async afterRead(e, type) {
|
isAssetTypeAnImage(ext) {
|
||||||
const item = e.file;
|
const str = ext.split('.')[1];
|
||||||
const imgUrl = item.url;
|
return this.limitType.indexOf(str.toLowerCase()) !== -1;
|
||||||
|
},
|
||||||
|
getCompressionRatio(fileSize) {
|
||||||
|
const multiple = (fileSize / this.fileMaxSize).toFixed(2);
|
||||||
|
let compressionRatio = 1;
|
||||||
|
if (multiple > 5) {
|
||||||
|
compressionRatio = 0.5
|
||||||
|
} else if (multiple > 4) {
|
||||||
|
compressionRatio = 0.6
|
||||||
|
} else if (multiple > 3) {
|
||||||
|
compressionRatio = 0.7
|
||||||
|
} else if (multiple > 2) {
|
||||||
|
compressionRatio = 0.8
|
||||||
|
} else if (multiple > 1) {
|
||||||
|
compressionRatio = 0.9
|
||||||
|
} else {
|
||||||
|
compressionRatio = 2
|
||||||
|
}
|
||||||
|
return compressionRatio;
|
||||||
|
},
|
||||||
|
translate(imgSrc, scale) {
|
||||||
|
//imgSrc:图片的路径
|
||||||
|
//scale:缩放比例 0-1之间
|
||||||
|
return new Promise((reslove, reject) => {
|
||||||
|
var img = new Image(); //创建Image对象生成一个<img>标签
|
||||||
|
img.src = imgSrc; //将图片路径赋给<img>标签的src
|
||||||
|
img.onload = () => {//onload在图片加载成功后触发,在onload中完成压缩功能
|
||||||
|
var h = img.height/2; // 获取原本图片的宽高
|
||||||
|
var w = img.width/2; //默认按比例压缩,根据需求修改
|
||||||
|
var canvas = document.createElement('canvas');//创建画布
|
||||||
|
var ctx = canvas.getContext('2d'); //设置为2d效果
|
||||||
|
var width = document.createAttribute("width"); //创建属性节点
|
||||||
|
width.nodeValue = w; //设置属性值
|
||||||
|
var height = document.createAttribute("height");
|
||||||
|
height.nodeValue = h;
|
||||||
|
canvas.setAttributeNode(width); //设置画布宽高
|
||||||
|
canvas.setAttributeNode(height);
|
||||||
|
ctx.drawImage(img, 0, 0, w,h);//将图片贴到画布上
|
||||||
|
//img:图片 0,0:粘贴的位置 w,h:粘贴图片的大小
|
||||||
|
var base64 = canvas.toDataURL('image/png', scale);
|
||||||
|
//'image/png':压缩返回图片的类型 scale:图片质量
|
||||||
|
//如果要base64的流,可以直接将结果返回了
|
||||||
|
canvas = null; //清除画布
|
||||||
|
var blob = this.base64ToBlob(base64); //需要二进制流调用该方法拿到
|
||||||
|
let blobUrl = window.URL.createObjectURL(blob);//blob地址
|
||||||
|
reslove(blobUrl)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
// base64转Blob
|
||||||
|
base64ToBlob(base64) {
|
||||||
|
var arr = base64.split(','),
|
||||||
|
mime = arr[0].match(/:(.*?);/)[1],
|
||||||
|
bstr = atob(arr[1]),
|
||||||
|
n = bstr.length,
|
||||||
|
u8arr = new Uint8Array(n);
|
||||||
|
while (n--) {
|
||||||
|
u8arr[n] = bstr.charCodeAt(n);
|
||||||
|
}
|
||||||
|
return new Blob([u8arr], {
|
||||||
|
type: mime,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// 上传图片
|
||||||
|
async afterRead(event, type) {
|
||||||
|
var item = event.file;
|
||||||
|
|
||||||
|
console.log("压缩前:",item.url);
|
||||||
|
|
||||||
|
|
||||||
|
var a = await this.translate(item.url,0.9)
|
||||||
|
|
||||||
|
item.url = a;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// let lists = [].concat(event.file);
|
||||||
|
// let fileListLen = this[`fileList${event.name}`].length;
|
||||||
|
// for (let index in lists) {
|
||||||
|
// const fileItem = lists[index];
|
||||||
|
// const fileSize = fileItem.size;
|
||||||
|
// const fileName = fileItem.name ?? '';
|
||||||
|
// if(!this.isAssetTypeAnImage(fileItem.name)) {
|
||||||
|
// this.$.msg('不允许该文件类型上传');
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// console.log('文件大小',fileSize);
|
||||||
|
// if (fileSize > this.fileMaxSize) {
|
||||||
|
// const compressionRatio = this.getCompressionRatio(fileSize);
|
||||||
|
// if (compressionRatio > 1) {
|
||||||
|
// uni.$u.toast("上传图片大小不能超过5MB!");
|
||||||
|
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// // 超出1M自动压缩图片
|
||||||
|
// item = await this.handerCompressImg(fileItem, compressionRatio)
|
||||||
|
|
||||||
|
// if (fileItem.size > this.maxSize) {
|
||||||
|
// uni.$u.toast("上传图片大小不能超过5MB!");
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if (fileItem.size < this.fileMinSize) {
|
||||||
|
// uni.$u.toast("上传图片大小不能超过5MB!");
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
console.log("压缩后:",item.url);
|
||||||
|
|
||||||
|
var imgUrl = item.url;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const group = {
|
const group = {
|
||||||
url: imgUrl,
|
url: imgUrl,
|
||||||
@ -769,7 +929,7 @@ export default {
|
|||||||
this.fileList6.push(group);
|
this.fileList6.push(group);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
console.log(imgUrl)
|
||||||
let res = await UploadFilePromise(imgUrl);
|
let res = await UploadFilePromise(imgUrl);
|
||||||
|
|
||||||
if (res && res.status == 200) {
|
if (res && res.status == 200) {
|
||||||
|
|||||||
@ -172,6 +172,7 @@ import { batchNoApi, imgOcrResultApi } from "../../api/upload";
|
|||||||
import { mapState } from "vuex";
|
import { mapState } from "vuex";
|
||||||
import { throttle, debounce } from "lodash";
|
import { throttle, debounce } from "lodash";
|
||||||
import navBar from "@/components/uni-nav-bar/uni-nav-bar";
|
import navBar from "@/components/uni-nav-bar/uni-nav-bar";
|
||||||
|
import { compressImg } from '@/utils/tool.js'
|
||||||
|
|
||||||
const orcImgTypeConf = {
|
const orcImgTypeConf = {
|
||||||
FR_ID_CARD_FRONT: "FR_ID_CARD_FRONT",
|
FR_ID_CARD_FRONT: "FR_ID_CARD_FRONT",
|
||||||
@ -230,6 +231,10 @@ export default {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
limitType: ['png', 'jpg', 'jpeg'], //允许的图片后缀
|
||||||
|
fileMaxSize: 1 * 1024 * 1024, // 超出1M开启压缩
|
||||||
|
maxSize:5 * 1024 * 1024, //图片最大不能超过20M
|
||||||
|
fileMinSize: 5 * 1024, // 最小为5KB
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
@ -254,9 +259,68 @@ export default {
|
|||||||
uni.navigateBack();
|
uni.navigateBack();
|
||||||
},
|
},
|
||||||
overSize(e) {
|
overSize(e) {
|
||||||
uni.$u.toast("上传图片大小不能超过8MB!");
|
uni.$u.toast("上传图片大小不能超过5MB!");
|
||||||
|
},
|
||||||
|
compressImage(url) {
|
||||||
|
return new Promise((reslove, reject) => {
|
||||||
|
const tempFilePath = url //url是选中图片的路径
|
||||||
|
uni.compressImage({
|
||||||
|
src: tempFilePath,
|
||||||
|
quality: 90, //压缩的程度
|
||||||
|
success: (res) => {
|
||||||
|
reslove(res.tempFilePath) //压缩成功返回的路径
|
||||||
|
},
|
||||||
|
fail: (error) => {
|
||||||
|
console.log('压缩失败', error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
async handerCompressImg(source, compressionRatio) {
|
||||||
|
let that = this;
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
compressImg(source.url, compressionRatio, source.type, compressRes => {
|
||||||
|
resolve(compressRes);
|
||||||
|
})
|
||||||
|
}).then((res) => {
|
||||||
|
source.size = res.size
|
||||||
|
// window.URL.revokeObjectURL(source.url) // 删除被压缩的缓存文件,这里注意,如果是相册选择上传,可能会删除相册的图片
|
||||||
|
source.url = res.source
|
||||||
|
source.thumb = res.source
|
||||||
|
return source
|
||||||
|
}).catch(err => {
|
||||||
|
console.log('图片压缩失败', err)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
/**判断文件类型是否正确 */
|
||||||
|
isAssetTypeAnImage(ext) {
|
||||||
|
const str = ext.split('.')[1];
|
||||||
|
return this.limitType.indexOf(str.toLowerCase()) !== -1;
|
||||||
|
},
|
||||||
|
getCompressionRatio(fileSize) {
|
||||||
|
const multiple = (fileSize / this.fileMaxSize).toFixed(2);
|
||||||
|
let compressionRatio = 1;
|
||||||
|
if (multiple > 5) {
|
||||||
|
compressionRatio = 0.5
|
||||||
|
} else if (multiple > 4) {
|
||||||
|
compressionRatio = 0.6
|
||||||
|
} else if (multiple > 3) {
|
||||||
|
compressionRatio = 0.7
|
||||||
|
} else if (multiple > 2) {
|
||||||
|
compressionRatio = 0.8
|
||||||
|
} else if (multiple > 1) {
|
||||||
|
compressionRatio = 0.9
|
||||||
|
} else {
|
||||||
|
compressionRatio = 2
|
||||||
|
}
|
||||||
|
return compressionRatio;
|
||||||
},
|
},
|
||||||
getOcrText(filePath, file, type) {
|
getOcrText(filePath, file, type) {
|
||||||
|
|
||||||
|
//#ifdef APP-PLUS
|
||||||
|
filePath = await this.compressImage(filePath);
|
||||||
|
//#endif
|
||||||
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
|
|
||||||
@ -313,14 +377,46 @@ export default {
|
|||||||
console.log("bankCard", ocr);
|
console.log("bankCard", ocr);
|
||||||
},
|
},
|
||||||
|
|
||||||
async afterRead(e) {
|
async afterRead(event) {
|
||||||
const item = e.file;
|
var item = event.file;
|
||||||
const imgUrl = item.url;
|
const imgUrl = item.url;
|
||||||
|
|
||||||
const group = {
|
const group = {
|
||||||
url: imgUrl,
|
url: imgUrl,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let lists = [].concat(event.file);
|
||||||
|
let fileListLen = this[`fileList${event.name}`].length;
|
||||||
|
for (let index in lists) {
|
||||||
|
const group = lists[index];
|
||||||
|
const fileSize = group.size;
|
||||||
|
const fileName = group.name ?? '';
|
||||||
|
if(!this.isAssetTypeAnImage(group.name)) {
|
||||||
|
this.$.msg('不允许该文件类型上传');
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
console.log('文件大小',fileSize);
|
||||||
|
if (fileSize > this.fileMaxSize) {
|
||||||
|
const compressionRatio = this.getCompressionRatio(fileSize);
|
||||||
|
if (compressionRatio > 1) {
|
||||||
|
this.$.msg('文件' + fileName + '大于5M');
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// 超出1M自动压缩图片
|
||||||
|
item = await this.handerCompressImg(item, compressionRatio)
|
||||||
|
|
||||||
|
if (item.size > this.maxSize) {
|
||||||
|
this.$.msg('文件' + fileName + '压缩后超出20M')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (item.size < this.fileMinSize) {
|
||||||
|
this.$.msg('文件' + fileName + '不能小于5KB');
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|
||||||
this.currentBankFile = {
|
this.currentBankFile = {
|
||||||
|
|||||||
@ -161,8 +161,8 @@
|
|||||||
<view class="delivery-num">{{ item.order_pickup_num_str }}</view>
|
<view class="delivery-num">{{ item.order_pickup_num_str }}</view>
|
||||||
</view>
|
</view>
|
||||||
<view class="order-remark">
|
<view class="order-remark">
|
||||||
<text style="color: red; font-weight: bold">备注:</text>
|
<text style="color: #fff; font-weight: bold">备注:</text>
|
||||||
<text>{{ item.buyer_info.order_message }}</text>
|
<text style="color: #fff;">{{ item.buyer_info.order_message }}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="commodity-info-block">
|
<view class="commodity-info-block">
|
||||||
<view class="commodity-num">
|
<view class="commodity-num">
|
||||||
@ -962,18 +962,6 @@ export default {
|
|||||||
},
|
},
|
||||||
onLoad() {},
|
onLoad() {},
|
||||||
onShow() {
|
onShow() {
|
||||||
let accountDashboard = uni.getStorageSync("accountDashboard");
|
|
||||||
let auditInfo = uni.getStorageSync("auditInfo");
|
|
||||||
|
|
||||||
if (accountDashboard) {
|
|
||||||
this.shopNmae = accountDashboard.store_info.store_name;
|
|
||||||
this.logoUrl = accountDashboard.store_info.store_logo;
|
|
||||||
} else {
|
|
||||||
this.getAccountDashboard();
|
|
||||||
}
|
|
||||||
if (!auditInfo) {
|
|
||||||
this.getAuditInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getOrderList();
|
this.getOrderList();
|
||||||
},
|
},
|
||||||
@ -993,7 +981,22 @@ export default {
|
|||||||
// },
|
// },
|
||||||
methods: {
|
methods: {
|
||||||
async getOrderList() {
|
async getOrderList() {
|
||||||
|
|
||||||
|
let accountDashboard = uni.getStorageSync("accountDashboard");
|
||||||
|
let auditInfo = uni.getStorageSync("auditInfo");
|
||||||
|
|
||||||
|
if (accountDashboard) {
|
||||||
|
this.shopNmae = accountDashboard.store_info.store_name;
|
||||||
|
this.logoUrl = accountDashboard.store_info.store_logo;
|
||||||
|
} else {
|
||||||
|
await this.getAccountDashboard();
|
||||||
|
}
|
||||||
|
if (!auditInfo) {
|
||||||
|
await this.getAuditInfo();
|
||||||
|
}
|
||||||
|
|
||||||
this.showOrderLoading = true;
|
this.showOrderLoading = true;
|
||||||
|
console.log(this.accountInfo)
|
||||||
this.params = {
|
this.params = {
|
||||||
storeId: this.accountInfo.store_info.store_id,
|
storeId: this.accountInfo.store_info.store_id,
|
||||||
keyword: this.keyword,
|
keyword: this.keyword,
|
||||||
|
|||||||
@ -0,0 +1,160 @@
|
|||||||
|
'use strict';
|
||||||
|
const uniPush = uniCloud.getPushManager({
|
||||||
|
appId: "__UNI__95F809F"
|
||||||
|
});
|
||||||
|
const db = uniCloud.database();
|
||||||
|
const pushLogCollection = db.collection('push_logs'); // 假设存在 push_logs 集合用于记录推送日志
|
||||||
|
|
||||||
|
// 验证设备 ID 是否有效
|
||||||
|
const isValidDeviceId = (deviceId) => {
|
||||||
|
return typeof deviceId ==='string' && deviceId.trim()!== '';
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.main = async (event) => {
|
||||||
|
try {
|
||||||
|
let obj = JSON.parse(event.body);
|
||||||
|
|
||||||
|
// 检查发送者和接收者信息
|
||||||
|
if (!obj.senderId ||!obj.receiverId) {
|
||||||
|
const errorMsg = '缺少发送者或接收者信息';
|
||||||
|
// 记录错误日志
|
||||||
|
await pushLogCollection.add({
|
||||||
|
senderId: obj.senderId,
|
||||||
|
receiverId: obj.receiverId,
|
||||||
|
title: obj.title,
|
||||||
|
content: obj.content,
|
||||||
|
status: 'failed',
|
||||||
|
errorCode: 400,
|
||||||
|
errorMsg: errorMsg,
|
||||||
|
timestamp: new Date(),
|
||||||
|
requestBody: obj
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 400,
|
||||||
|
msg: errorMsg
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果 category 不是对象,将其移除
|
||||||
|
if (typeof obj.category!== 'object') {
|
||||||
|
delete obj.category;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从数据库中获取接收者的设备标识
|
||||||
|
if (typeof obj.receiverId!== 'string' || obj.receiverId.trim() === '') {
|
||||||
|
const errorMsg = '接收者 ID 无效';
|
||||||
|
// 记录错误日志
|
||||||
|
await pushLogCollection.add({
|
||||||
|
senderId: obj.senderId,
|
||||||
|
receiverId: obj.receiverId,
|
||||||
|
title: obj.title,
|
||||||
|
content: obj.content,
|
||||||
|
status: 'failed',
|
||||||
|
errorCode: 400,
|
||||||
|
errorMsg: errorMsg,
|
||||||
|
timestamp: new Date(),
|
||||||
|
requestBody: obj
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 400,
|
||||||
|
msg: errorMsg
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const receiverDevices = await db.collection('opendb-device')
|
||||||
|
.where({ userId: obj.receiverId })
|
||||||
|
.get();
|
||||||
|
|
||||||
|
console.log('接收者 ID:', obj.receiverId);
|
||||||
|
console.log('查询到的设备信息原始数据:', receiverDevices.data); // 打印原始数据
|
||||||
|
|
||||||
|
if (receiverDevices.data.length === 0) {
|
||||||
|
const errorMsg = `未找到接收者(ID: ${obj.receiverId})的设备信息`;
|
||||||
|
// 记录错误日志
|
||||||
|
await pushLogCollection.add({
|
||||||
|
senderId: obj.senderId,
|
||||||
|
receiverId: obj.receiverId,
|
||||||
|
title: obj.title,
|
||||||
|
content: obj.content,
|
||||||
|
status: 'failed',
|
||||||
|
errorCode: 404,
|
||||||
|
errorMsg: errorMsg,
|
||||||
|
timestamp: new Date(),
|
||||||
|
requestBody: obj
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 404,
|
||||||
|
msg: errorMsg
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const pushClientIds = "设备id";
|
||||||
|
// console.log(pushClientIds);
|
||||||
|
const validDeviceIds = ["设备id"];
|
||||||
|
validDeviceIds.push();
|
||||||
|
const invalidDeviceIds = [];
|
||||||
|
if (validDeviceIds.length === 0) {
|
||||||
|
const errorMsg = `接收者(ID: ${obj.receiverId})的所有设备 ID 均无效,无效的 ID 包括: ${invalidDeviceIds.join(', ')}`;
|
||||||
|
// 记录错误日志
|
||||||
|
await pushLogCollection.add({
|
||||||
|
senderId: obj.senderId,
|
||||||
|
receiverId: obj.receiverId,
|
||||||
|
title: obj.title,
|
||||||
|
content: obj.content,
|
||||||
|
status: 'failed',
|
||||||
|
errorCode: 400,
|
||||||
|
errorMsg: errorMsg,
|
||||||
|
timestamp: new Date(),
|
||||||
|
requestBody: obj
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 400,
|
||||||
|
msg: errorMsg
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const res = await uniPush.sendMessage({
|
||||||
|
"push_clientid": validDeviceIds,
|
||||||
|
"title": obj.title,
|
||||||
|
"content": obj.content,
|
||||||
|
"payload": obj.payload,
|
||||||
|
"force_notification": true,
|
||||||
|
"request_id": obj.request_id,
|
||||||
|
"options": obj.options
|
||||||
|
});
|
||||||
|
|
||||||
|
// 记录成功日志
|
||||||
|
await pushLogCollection.add({
|
||||||
|
senderId: obj.senderId,
|
||||||
|
receiverId: obj.receiverId,
|
||||||
|
title: obj.title,
|
||||||
|
content: obj.content,
|
||||||
|
status:'success',
|
||||||
|
pushResult: res,
|
||||||
|
timestamp: new Date(),
|
||||||
|
requestBody: obj
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 200,
|
||||||
|
msg: '消息推送成功',
|
||||||
|
data: res
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('消息推送出错:', error);
|
||||||
|
// 记录异常错误日志
|
||||||
|
await pushLogCollection.add({
|
||||||
|
senderId: event.senderId,
|
||||||
|
receiverId: event.receiverId,
|
||||||
|
title: event.title,
|
||||||
|
content: event.content,
|
||||||
|
status: 'failed',
|
||||||
|
errorCode: 500,
|
||||||
|
errorMsg: '消息推送失败',
|
||||||
|
errorDetail: error.message,
|
||||||
|
timestamp: new Date(),
|
||||||
|
requestBody: event.body
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 500,
|
||||||
|
msg: '消息推送失败',
|
||||||
|
error: error.message
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"name": "test",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"extensions": {
|
||||||
|
"uni-cloud-push": {}
|
||||||
|
},
|
||||||
|
"author": ""
|
||||||
|
}
|
||||||
12
java-mall-app-shop-admin/uniCloud-aliyun/database/JQL查询.jql
Normal file
12
java-mall-app-shop-admin/uniCloud-aliyun/database/JQL查询.jql
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// 本文件用于,使用JQL语法操作项目关联的uniCloud空间的数据库,方便开发调试和远程数据库管理
|
||||||
|
// 编写clientDB的js API(也支持常规js语法,比如var),可以对云数据库进行增删改查操作。不支持uniCloud-db组件写法
|
||||||
|
// 可以全部运行,也可以选中部分代码运行。点击工具栏上的运行按钮或者按下【F5】键运行代码
|
||||||
|
// 如果文档中存在多条JQL语句,只有最后一条语句生效
|
||||||
|
// 如果混写了普通js,最后一条语句需是数据库操作语句
|
||||||
|
// 此处代码运行不受DB Schema的权限控制,移植代码到实际业务中注意在schema中配好permission
|
||||||
|
// 不支持clientDB的action
|
||||||
|
// 数据库查询有最大返回条数限制,详见:https://uniapp.dcloud.net.cn/uniCloud/cf-database.html#limit
|
||||||
|
// 详细JQL语法,请参考:https://uniapp.dcloud.net.cn/uniCloud/jql.html
|
||||||
|
|
||||||
|
// 下面示例查询uni-id-users表的所有数据
|
||||||
|
db.collection('uni-id-users').get();
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
## 0.0.3(2022-11-11)
|
||||||
|
- 修复 config 方法获取根节点为数组格式配置时错误的转化为了对象的Bug
|
||||||
|
## 0.0.2(2021-04-16)
|
||||||
|
- 修改插件package信息
|
||||||
|
## 0.0.1(2021-03-15)
|
||||||
|
- 初始化项目
|
||||||
@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"id": "uni-config-center",
|
||||||
|
"displayName": "uni-config-center",
|
||||||
|
"version": "0.0.3",
|
||||||
|
"description": "uniCloud 配置中心",
|
||||||
|
"keywords": [
|
||||||
|
"配置",
|
||||||
|
"配置中心"
|
||||||
|
],
|
||||||
|
"repository": "",
|
||||||
|
"engines": {
|
||||||
|
"HBuilderX": "^3.1.0"
|
||||||
|
},
|
||||||
|
"dcloudext": {
|
||||||
|
"sale": {
|
||||||
|
"regular": {
|
||||||
|
"price": "0.00"
|
||||||
|
},
|
||||||
|
"sourcecode": {
|
||||||
|
"price": "0.00"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"contact": {
|
||||||
|
"qq": ""
|
||||||
|
},
|
||||||
|
"declaration": {
|
||||||
|
"ads": "无",
|
||||||
|
"data": "无",
|
||||||
|
"permissions": "无"
|
||||||
|
},
|
||||||
|
"npmurl": "",
|
||||||
|
"type": "unicloud-template-function"
|
||||||
|
},
|
||||||
|
"directories": {
|
||||||
|
"example": "../../../scripts/dist"
|
||||||
|
},
|
||||||
|
"uni_modules": {
|
||||||
|
"dependencies": [],
|
||||||
|
"encrypt": [],
|
||||||
|
"platforms": {
|
||||||
|
"cloud": {
|
||||||
|
"tcb": "y",
|
||||||
|
"aliyun": "y"
|
||||||
|
},
|
||||||
|
"client": {
|
||||||
|
"App": {
|
||||||
|
"app-vue": "u",
|
||||||
|
"app-nvue": "u"
|
||||||
|
},
|
||||||
|
"H5-mobile": {
|
||||||
|
"Safari": "u",
|
||||||
|
"Android Browser": "u",
|
||||||
|
"微信浏览器(Android)": "u",
|
||||||
|
"QQ浏览器(Android)": "u"
|
||||||
|
},
|
||||||
|
"H5-pc": {
|
||||||
|
"Chrome": "u",
|
||||||
|
"IE": "u",
|
||||||
|
"Edge": "u",
|
||||||
|
"Firefox": "u",
|
||||||
|
"Safari": "u"
|
||||||
|
},
|
||||||
|
"小程序": {
|
||||||
|
"微信": "u",
|
||||||
|
"阿里": "u",
|
||||||
|
"百度": "u",
|
||||||
|
"字节跳动": "u",
|
||||||
|
"QQ": "u"
|
||||||
|
},
|
||||||
|
"快应用": {
|
||||||
|
"华为": "u",
|
||||||
|
"联盟": "u"
|
||||||
|
},
|
||||||
|
"Vue": {
|
||||||
|
"vue2": "y",
|
||||||
|
"vue3": "u"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,93 @@
|
|||||||
|
# 为什么使用uni-config-center
|
||||||
|
|
||||||
|
实际开发中很多插件需要配置文件才可以正常运行,如果每个插件都单独进行配置的话就会产生下面这样的目录结构
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudfunctions
|
||||||
|
└─────common 公共模块
|
||||||
|
├─plugin-a // 插件A对应的目录
|
||||||
|
│ ├─index.js
|
||||||
|
│ ├─config.json // plugin-a对应的配置文件
|
||||||
|
│ └─other-file.cert // plugin-a依赖的其他文件
|
||||||
|
└─plugin-b // plugin-b对应的目录
|
||||||
|
├─index.js
|
||||||
|
└─config.json // plugin-b对应的配置文件
|
||||||
|
```
|
||||||
|
|
||||||
|
假设插件作者要发布一个项目模板,里面使用了很多需要配置的插件,无论是作者发布还是用户使用都是一个大麻烦。
|
||||||
|
|
||||||
|
uni-config-center就是用了统一管理这些配置文件的,使用uni-config-center后的目录结构如下
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cloudfunctions
|
||||||
|
└─────common 公共模块
|
||||||
|
├─plugin-a // 插件A对应的目录
|
||||||
|
│ └─index.js
|
||||||
|
├─plugin-b // plugin-b对应的目录
|
||||||
|
│ └─index.js
|
||||||
|
└─uni-config-center
|
||||||
|
├─index.js // config-center入口文件
|
||||||
|
├─plugin-a
|
||||||
|
│ ├─config.json // plugin-a对应的配置文件
|
||||||
|
│ └─other-file.cert // plugin-a依赖的其他文件
|
||||||
|
└─plugin-b
|
||||||
|
└─config.json // plugin-b对应的配置文件
|
||||||
|
```
|
||||||
|
|
||||||
|
使用uni-config-center后的优势
|
||||||
|
|
||||||
|
- 配置文件统一管理,分离插件主体和配置信息,更新插件更方便
|
||||||
|
- 支持对config.json设置schema,插件使用者在HBuilderX内编写config.json文件时会有更好的提示(后续HBuilderX会提供支持)
|
||||||
|
|
||||||
|
# 用法
|
||||||
|
|
||||||
|
在要使用uni-config-center的公共模块或云函数内引入uni-config-center依赖,请参考:[使用公共模块](https://uniapp.dcloud.net.cn/uniCloud/cf-common)
|
||||||
|
|
||||||
|
```js
|
||||||
|
const createConfig = require('uni-config-center')
|
||||||
|
|
||||||
|
const uniIdConfig = createConfig({
|
||||||
|
pluginId: 'uni-id', // 插件id
|
||||||
|
defaultConfig: { // 默认配置
|
||||||
|
tokenExpiresIn: 7200,
|
||||||
|
tokenExpiresThreshold: 600,
|
||||||
|
},
|
||||||
|
customMerge: function(defaultConfig, userConfig) { // 自定义默认配置和用户配置的合并规则,不设置的情况侠会对默认配置和用户配置进行深度合并
|
||||||
|
// defaudltConfig 默认配置
|
||||||
|
// userConfig 用户配置
|
||||||
|
return Object.assign(defaultConfig, userConfig)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// 以如下配置为例
|
||||||
|
// {
|
||||||
|
// "tokenExpiresIn": 7200,
|
||||||
|
// "passwordErrorLimit": 6,
|
||||||
|
// "bindTokenToDevice": false,
|
||||||
|
// "passwordErrorRetryTime": 3600,
|
||||||
|
// "app-plus": {
|
||||||
|
// "tokenExpiresIn": 2592000
|
||||||
|
// },
|
||||||
|
// "service": {
|
||||||
|
// "sms": {
|
||||||
|
// "codeExpiresIn": 300
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 获取配置
|
||||||
|
uniIdConfig.config() // 获取全部配置,注意:uni-config-center内不存在对应插件目录时会返回空对象
|
||||||
|
uniIdConfig.config('tokenExpiresIn') // 指定键值获取配置,返回:7200
|
||||||
|
uniIdConfig.config('service.sms.codeExpiresIn') // 指定键值获取配置,返回:300
|
||||||
|
uniIdConfig.config('tokenExpiresThreshold', 600) // 指定键值获取配置,如果不存在则取传入的默认值,返回:600
|
||||||
|
|
||||||
|
// 获取文件绝对路径
|
||||||
|
uniIdConfig.resolve('custom-token.js') // 获取uni-config-center/uni-id/custom-token.js文件的路径
|
||||||
|
|
||||||
|
// 引用文件(require)
|
||||||
|
uniIDConfig.requireFile('custom-token.js') // 使用require方式引用uni-config-center/uni-id/custom-token.js文件。文件不存在时返回undefined,文件内有其他错误导致require失败时会抛出错误。
|
||||||
|
|
||||||
|
// 判断是否包含某文件
|
||||||
|
uniIDConfig.hasFile('custom-token.js') // 配置目录是否包含某文件,true: 文件存在,false: 文件不存在
|
||||||
|
```
|
||||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "uni-config-center",
|
||||||
|
"version": "0.0.3",
|
||||||
|
"description": "配置中心",
|
||||||
|
"main": "index.js",
|
||||||
|
"keywords": [],
|
||||||
|
"author": "DCloud",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"origin-plugin-dev-name": "uni-config-center",
|
||||||
|
"origin-plugin-version": "0.0.3",
|
||||||
|
"plugin-dev-name": "uni-config-center",
|
||||||
|
"plugin-version": "0.0.3"
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
## 1.0.18(2024-07-08)
|
||||||
|
- checkToken时如果传入的token为空则返回uni-id-check-token-failed错误码以便uniIdRouter能正常跳转
|
||||||
|
## 1.0.17(2024-04-26)
|
||||||
|
- 兼容uni-app-x对客户端uniPlatform的调整(uni-app-x内uniPlatform区分app-android、app-ios)
|
||||||
|
## 1.0.16(2023-04-25)
|
||||||
|
- 新增maxTokenLength配置,用于限制数据库用户记录token数组的最大长度
|
||||||
|
## 1.0.15(2023-04-06)
|
||||||
|
- 修复部分语言国际化出错的Bug
|
||||||
|
## 1.0.14(2023-03-07)
|
||||||
|
- 修复 admin用户包含其他角色时未包含在token的Bug
|
||||||
|
## 1.0.13(2022-07-21)
|
||||||
|
- 修复 创建token时未传角色权限信息生成的token不正确的bug
|
||||||
|
## 1.0.12(2022-07-15)
|
||||||
|
- 提升与旧版本uni-id的兼容性(补充读取配置文件时回退平台app-plus、h5),但是仍推荐使用新平台名进行配置(app、web)
|
||||||
|
## 1.0.11(2022-07-14)
|
||||||
|
- 修复 部分情况下报`read property 'reduce' of undefined`的错误
|
||||||
|
## 1.0.10(2022-07-11)
|
||||||
|
- 将token存储在用户表的token字段内,与旧版本uni-id保持一致
|
||||||
|
## 1.0.9(2022-07-01)
|
||||||
|
- checkToken兼容token内未缓存角色权限的情况,此时将查库获取角色权限
|
||||||
|
## 1.0.8(2022-07-01)
|
||||||
|
- 修复clientDB默认依赖时部分情况下获取不到uni-id配置的Bug
|
||||||
|
## 1.0.7(2022-06-30)
|
||||||
|
- 修复config文件不合法时未抛出具体错误的Bug
|
||||||
|
## 1.0.6(2022-06-28)
|
||||||
|
- 移除插件内的数据表schema
|
||||||
|
## 1.0.5(2022-06-27)
|
||||||
|
- 修复使用多应用配置时报`Cannot read property 'appId' of undefined`的Bug
|
||||||
|
## 1.0.4(2022-06-27)
|
||||||
|
- 修复使用自定义token内容功能报错的Bug [详情](https://ask.dcloud.net.cn/question/147945)
|
||||||
|
## 1.0.2(2022-06-23)
|
||||||
|
- 对齐旧版本uni-id默认配置
|
||||||
|
## 1.0.1(2022-06-22)
|
||||||
|
- 补充对uni-config-center的依赖
|
||||||
|
## 1.0.0(2022-06-21)
|
||||||
|
- 提供uni-id token创建、校验、刷新接口,简化旧版uni-id公共模块
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
{
|
||||||
|
"id": "uni-id-common",
|
||||||
|
"displayName": "uni-id-common",
|
||||||
|
"version": "1.0.18",
|
||||||
|
"description": "包含uni-id token生成、校验、刷新功能的云函数公共模块",
|
||||||
|
"keywords": [
|
||||||
|
"uni-id-common",
|
||||||
|
"uniCloud",
|
||||||
|
"token",
|
||||||
|
"权限"
|
||||||
|
],
|
||||||
|
"repository": "https://gitcode.net/dcloud/uni-id-common",
|
||||||
|
"engines": {
|
||||||
|
},
|
||||||
|
"dcloudext": {
|
||||||
|
"sale": {
|
||||||
|
"regular": {
|
||||||
|
"price": 0
|
||||||
|
},
|
||||||
|
"sourcecode": {
|
||||||
|
"price": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"contact": {
|
||||||
|
"qq": ""
|
||||||
|
},
|
||||||
|
"declaration": {
|
||||||
|
"ads": "无",
|
||||||
|
"data": "无",
|
||||||
|
"permissions": "无"
|
||||||
|
},
|
||||||
|
"npmurl": "",
|
||||||
|
"type": "unicloud-template-function"
|
||||||
|
},
|
||||||
|
"uni_modules": {
|
||||||
|
"dependencies": ["uni-config-center"],
|
||||||
|
"encrypt": [],
|
||||||
|
"platforms": {
|
||||||
|
"cloud": {
|
||||||
|
"tcb": "y",
|
||||||
|
"aliyun": "y",
|
||||||
|
"alipay": "n"
|
||||||
|
},
|
||||||
|
"client": {
|
||||||
|
"Vue": {
|
||||||
|
"vue2": "u",
|
||||||
|
"vue3": "u"
|
||||||
|
},
|
||||||
|
"App": {
|
||||||
|
"app-vue": "u",
|
||||||
|
"app-nvue": "u"
|
||||||
|
},
|
||||||
|
"H5-mobile": {
|
||||||
|
"Safari": "u",
|
||||||
|
"Android Browser": "u",
|
||||||
|
"微信浏览器(Android)": "u",
|
||||||
|
"QQ浏览器(Android)": "u"
|
||||||
|
},
|
||||||
|
"H5-pc": {
|
||||||
|
"Chrome": "u",
|
||||||
|
"IE": "u",
|
||||||
|
"Edge": "u",
|
||||||
|
"Firefox": "u",
|
||||||
|
"Safari": "u"
|
||||||
|
},
|
||||||
|
"小程序": {
|
||||||
|
"微信": "u",
|
||||||
|
"阿里": "u",
|
||||||
|
"百度": "u",
|
||||||
|
"字节跳动": "u",
|
||||||
|
"QQ": "u",
|
||||||
|
"钉钉": "u",
|
||||||
|
"快手": "u",
|
||||||
|
"飞书": "u",
|
||||||
|
"京东": "u"
|
||||||
|
},
|
||||||
|
"快应用": {
|
||||||
|
"华为": "u",
|
||||||
|
"联盟": "u"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
# uni-id-common
|
||||||
|
|
||||||
|
文档请参考:[uni-id-common](https://uniapp.dcloud.net.cn/uniCloud/uni-id-common.html)
|
||||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "uni-id-common",
|
||||||
|
"version": "1.0.18",
|
||||||
|
"description": "uni-id token生成、校验、刷新",
|
||||||
|
"main": "index.js",
|
||||||
|
"homepage": "https:\/\/uniapp.dcloud.io\/uniCloud\/uni-id-common.html",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https:\/\/gitee.com\/dcloud\/uni-id-common.git"
|
||||||
|
},
|
||||||
|
"author": "DCloud",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"dependencies": {
|
||||||
|
"uni-config-center": "file:..\/..\/..\/..\/..\/uni-config-center\/uniCloud\/cloudfunctions\/common\/uni-config-center"
|
||||||
|
},
|
||||||
|
"origin-plugin-dev-name": "uni-id-common",
|
||||||
|
"origin-plugin-version": "1.0.18",
|
||||||
|
"plugin-dev-name": "uni-id-common",
|
||||||
|
"plugin-version": "1.0.18"
|
||||||
|
}
|
||||||
75
java-mall-app-shop-admin/utils/tool.js
Normal file
75
java-mall-app-shop-admin/utils/tool.js
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/**
|
||||||
|
* 图片压缩
|
||||||
|
* imgSrc 地址
|
||||||
|
* scale 压缩质量 0-1
|
||||||
|
* type 文件类型
|
||||||
|
*/
|
||||||
|
export function compressImg (imgSrc, scale, type, callback) {
|
||||||
|
// uni.$u.toast('压缩中')
|
||||||
|
var img = new Image();
|
||||||
|
img.src = imgSrc;
|
||||||
|
img.onload = function() {
|
||||||
|
var that = this;
|
||||||
|
var h = (img.height * scale).toFixed(0); // 默认按质量比例压缩
|
||||||
|
var w = (img.width * scale).toFixed(0);
|
||||||
|
var canvas = document.createElement('canvas');
|
||||||
|
var ctx = canvas.getContext('2d');
|
||||||
|
var width = document.createAttribute("width");
|
||||||
|
width.nodeValue = w;
|
||||||
|
var height = document.createAttribute("height");
|
||||||
|
height.nodeValue = h;
|
||||||
|
canvas.setAttributeNode(width);
|
||||||
|
canvas.setAttributeNode(height);
|
||||||
|
ctx.drawImage(that, 0, 0, w, h);
|
||||||
|
var base64 = canvas.toDataURL('image/jpeg', scale); //压缩比例
|
||||||
|
canvas = null;
|
||||||
|
if (type == 'base64') {
|
||||||
|
let data = {
|
||||||
|
size: getBase64Size(base64),
|
||||||
|
type: type,
|
||||||
|
source: base64
|
||||||
|
}
|
||||||
|
callback(base64);
|
||||||
|
} else {
|
||||||
|
let blob = base64ToBlob(base64);
|
||||||
|
console.log('压缩后的大小', blob.size);
|
||||||
|
const blobUrl = window.URL.createObjectURL(blob); //blob地址
|
||||||
|
blob.source = blobUrl
|
||||||
|
callback(blob);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**base转Blob */
|
||||||
|
export function base64ToBlob (base64) {
|
||||||
|
var arr = base64.split(','),
|
||||||
|
mime = arr[0].match(/:(.*?);/)[1],
|
||||||
|
bstr = atob(arr[1]),
|
||||||
|
n = bstr.length,
|
||||||
|
u8arr = new Uint8Array(n);
|
||||||
|
while (n--) {
|
||||||
|
u8arr[n] = bstr.charCodeAt(n);
|
||||||
|
}
|
||||||
|
return new Blob([u8arr], {
|
||||||
|
type: mime
|
||||||
|
});
|
||||||
|
};
|
||||||
|
/**获取base64的文件大小 */
|
||||||
|
export function getBase64Size () {
|
||||||
|
let size = 0;
|
||||||
|
if (base64Str) { // 获取base64图片byte大小
|
||||||
|
const equalIndex = base64Str.indexOf('='); // 获取=号下标
|
||||||
|
if (equalIndex > 0) {
|
||||||
|
const str = base64Str.substring(0, equalIndex); // 去除=号
|
||||||
|
const strLength = str.length;
|
||||||
|
const fileLength = strLength - (strLength / 8) * 2; // 真实的图片byte大小
|
||||||
|
size = Math.floor(fileLength); // 向下取整
|
||||||
|
} else {
|
||||||
|
const strLength = base64Str.length;
|
||||||
|
const fileLength = strLength - (strLength / 8) * 2;
|
||||||
|
size = Math.floor(fileLength); // 向下取整
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
size = null;
|
||||||
|
}
|
||||||
|
return size
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user