前端小程序
这个提交包含在:
文件差异内容过多而无法显示
加载差异
文件差异内容过多而无法显示
加载差异
@@ -0,0 +1,569 @@
|
||||
let log = console.log; // 如果在项目的APP.vue文件中的onlaunch中设置 console.log = ()=> {} 则在此也不会有打印信息
|
||||
log = ()=>{}; // 打开注释则该插件不会打印任何信息
|
||||
let _app = {
|
||||
lineFeedTags: ['<br />', '<br/>', '\r\n', '\n\t', '\r', '\n'], //换行符识别列表
|
||||
tagetLineFeedTag: `❤`, //将lineFeedTags列表中的字符串替换为该字符, 代表换行符, 只要找一个特殊符号就行,必须是单字符单字符单字符!
|
||||
//交互控制
|
||||
log(t) {
|
||||
// log(t);
|
||||
}, // 打印控制,
|
||||
showLoading(msg, ifmask) {
|
||||
uni.showLoading({
|
||||
title: msg,
|
||||
mask: ifmask || false
|
||||
})
|
||||
},
|
||||
hideLoading() {
|
||||
uni.hideLoading();
|
||||
},
|
||||
showToast(msg, icon) {
|
||||
uni.showToast({
|
||||
title: msg,
|
||||
icon: icon || 'none'
|
||||
})
|
||||
},
|
||||
getPosterUrl(objs) { // 后端获取背景图的url路径方法
|
||||
let {
|
||||
backgroundImage,
|
||||
type,
|
||||
formData
|
||||
} = objs;
|
||||
return new Promise((rs, rj) => {
|
||||
let image;
|
||||
if (backgroundImage) {
|
||||
image = backgroundImage;
|
||||
} else {
|
||||
switch (type) { //根据type获取背景图, 一般要改成request获取
|
||||
case 1:
|
||||
image = '';
|
||||
break;
|
||||
default:
|
||||
image = '';
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (image) {
|
||||
rs(image); // resolve图片的路径
|
||||
} else {
|
||||
rj('背景图片路径不存在');
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//下面一般不用动他
|
||||
shareTypeListSheetArray: {
|
||||
array: [0, 1, 2, 3, 4, 5]
|
||||
}, // 分享类型 0-图文链接 1-纯文字 2-纯图片 3-音乐 4-视频 5-小程序
|
||||
isArray(arg) {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]';
|
||||
},
|
||||
isObject(arg) {
|
||||
return Object.prototype.toString.call(arg) === '[object Object]';
|
||||
},
|
||||
isPromise(obj) {
|
||||
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
||||
},
|
||||
isNull(arg) {
|
||||
return arg === null;
|
||||
},
|
||||
isUndefined(arg) {
|
||||
return arg === undefined;
|
||||
},
|
||||
isUndef(arg) {
|
||||
return arg === undefined;
|
||||
},
|
||||
isNotNull_string(arg) {
|
||||
return arg !== null && arg !== undefined && arg !== '';
|
||||
},
|
||||
isFn(fn) {
|
||||
return fn && typeof fn === 'function';
|
||||
},
|
||||
getStorage(key, scb, fcb) {
|
||||
uni.getStorage({
|
||||
key,
|
||||
success: function(res) {
|
||||
if (res.data && res.data != "") {
|
||||
if (scb) scb(res.data);
|
||||
} else {
|
||||
if (fcb) fcb();
|
||||
}
|
||||
},
|
||||
fail: function() {
|
||||
if (fcb) fcb();
|
||||
}
|
||||
})
|
||||
},
|
||||
setStorage(key, data) {
|
||||
log('设置缓存')
|
||||
log('key:' + key)
|
||||
log('data:' + JSON.stringify(data));
|
||||
uni.setStorage({
|
||||
key,
|
||||
data
|
||||
})
|
||||
},
|
||||
setStorageSync(key, data) {
|
||||
uni.setStorageSync(key, data);
|
||||
},
|
||||
getStorageSync(key) {
|
||||
return uni.getStorageSync(key);
|
||||
},
|
||||
clearStorageSync() {
|
||||
uni.clearStorageSync();
|
||||
},
|
||||
removeStorageSync(key) {
|
||||
uni.removeStorageSync(key);
|
||||
},
|
||||
getImageInfo(url, cb, fcb) {
|
||||
url = checkMPUrl(url);
|
||||
uni.getImageInfo({
|
||||
src: url,
|
||||
success(res) {
|
||||
if (cb && typeof(cb) == 'function') cb(res);
|
||||
},
|
||||
fail(err) {
|
||||
if (fcb && typeof(fcb) == 'function') fcb(err);
|
||||
}
|
||||
})
|
||||
},
|
||||
downloadFile(url, cb) {
|
||||
url = checkMPUrl(url);
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success(res) {
|
||||
if (cb && typeof(cb) == 'function') cb(res);
|
||||
}
|
||||
})
|
||||
},
|
||||
downloadFile_PromiseFc(url) {
|
||||
return new Promise((rs, rj) => {
|
||||
if (url.substring(0, 4) !== 'http') {
|
||||
rs(url);
|
||||
} else {
|
||||
url = checkMPUrl(url);
|
||||
log('url:' + url);
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success(res) {
|
||||
if (res && res.tempFilePath)
|
||||
rs(res.tempFilePath);
|
||||
else
|
||||
rj('not find tempFilePath');
|
||||
},
|
||||
fail(err) {
|
||||
rj(err);
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
},
|
||||
saveFile(url) {
|
||||
const fs = uni.getFileSystemManager();
|
||||
fs.saveFile({
|
||||
tempFilePath: url,
|
||||
success(res) {
|
||||
log('保存成功:' + JSON.stringify(res))
|
||||
}
|
||||
})
|
||||
},
|
||||
downLoadAndSaveFile_PromiseFc(url) {
|
||||
return new Promise((rs, rj) => {
|
||||
log('准备下载并保存图片:' + url);
|
||||
if (url.substring(0, 4) === 'http') {
|
||||
url = checkMPUrl(url);
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success(d_res) {
|
||||
log('下载背景图成功:' + JSON.stringify(d_res));
|
||||
if (d_res && d_res.tempFilePath) {
|
||||
|
||||
// #ifdef H5
|
||||
rs(d_res.tempFilePath);
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
const fs = uni.getFileSystemManager();
|
||||
fs.saveFile({
|
||||
tempFilePath: d_res.tempFilePath,
|
||||
success(s_res) {
|
||||
log('保存背景图成功:' + JSON.stringify(s_res));
|
||||
if (s_res && s_res.savedFilePath)
|
||||
rs(s_res.savedFilePath);
|
||||
else
|
||||
rs(d_res.tempFilePath);
|
||||
},
|
||||
fail(err) {
|
||||
rs(d_res.tempFilePath);
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
|
||||
} else {
|
||||
rj('not find tempFilePath');
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
rj(err);
|
||||
}
|
||||
})
|
||||
} else {
|
||||
rs(url);
|
||||
}
|
||||
})
|
||||
},
|
||||
checkFile_PromiseFc(url) {
|
||||
return new Promise((rs, rj) => {
|
||||
uni.getSavedFileList({
|
||||
success(res) {
|
||||
let d = res.fileList;
|
||||
let index = d.findIndex(item => {
|
||||
return item.filePath === url;
|
||||
})
|
||||
rs(index);
|
||||
},
|
||||
fail(err) {
|
||||
rj(err);
|
||||
}
|
||||
})
|
||||
});
|
||||
},
|
||||
removeSavedFile(path) {
|
||||
uni.getSavedFileList({
|
||||
success(res) {
|
||||
let d = res.fileList;
|
||||
let index = d.findIndex(item => {
|
||||
return item.filePath === path;
|
||||
});
|
||||
if (index >= 0)
|
||||
uni.removeSavedFile({
|
||||
filePath: path
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
fileNameInPath(path) {
|
||||
let s = path.split("/");
|
||||
return s[s.length - 1];
|
||||
},
|
||||
getImageInfo_PromiseFc(imgPath) {
|
||||
return new Promise((rs, rj) => {
|
||||
log('准备获取图片信息:' + imgPath);
|
||||
imgPath = checkMPUrl(imgPath);
|
||||
console.log(imgPath, 'ddd');
|
||||
uni.getImageInfo({
|
||||
src: imgPath,
|
||||
success: res => {
|
||||
log('获取图片信息成功:' + JSON.stringify(res));
|
||||
rs(res);
|
||||
},
|
||||
fail: err => {
|
||||
log('获取图片信息失败:' + JSON.stringify(err));
|
||||
rj(err)
|
||||
}
|
||||
})
|
||||
});
|
||||
},
|
||||
previewImage(urls) {
|
||||
if (typeof(urls) == 'string')
|
||||
urls = [urls];
|
||||
uni.previewImage({
|
||||
urls,
|
||||
})
|
||||
},
|
||||
actionSheet(obj, cb) {
|
||||
let sheetArray = [];
|
||||
for (let i = 0; i < obj.array.length; i++) {
|
||||
switch (obj.array[i]) {
|
||||
case 'sinaweibo':
|
||||
sheetArray[i] = '新浪微博';
|
||||
break;
|
||||
case 'qq':
|
||||
sheetArray[i] = 'QQ';
|
||||
break;
|
||||
case 'weixin':
|
||||
sheetArray[i] = '微信';
|
||||
break;
|
||||
case 'WXSceneSession':
|
||||
sheetArray[i] = '微信好友';
|
||||
break;
|
||||
case 'WXSenceTimeline':
|
||||
sheetArray[i] = '微信朋友圈';
|
||||
break;
|
||||
case 'WXSceneFavorite':
|
||||
sheetArray[i] = '微信收藏';
|
||||
break;
|
||||
case 0:
|
||||
sheetArray[i] = '图文链接';
|
||||
break;
|
||||
case 1:
|
||||
sheetArray[i] = '纯文字';
|
||||
break;
|
||||
case 2:
|
||||
sheetArray[i] = '纯图片';
|
||||
break;
|
||||
case 3:
|
||||
sheetArray[i] = '音乐';
|
||||
break;
|
||||
case 4:
|
||||
sheetArray[i] = '视频';
|
||||
break;
|
||||
case 5:
|
||||
sheetArray[i] = '小程序';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.showActionSheet(sheetArray, cb);
|
||||
},
|
||||
showActionSheet(sheetArray, cb) {
|
||||
uni.showActionSheet({
|
||||
itemList: sheetArray,
|
||||
success: (e) => {
|
||||
if (cb && typeof(cb) == 'function') cb(e.tapIndex);
|
||||
}
|
||||
})
|
||||
},
|
||||
getProvider(type, cb, sheet) {
|
||||
let _this = this;
|
||||
uni.getProvider({
|
||||
service: type,
|
||||
success: function(res) {
|
||||
if (sheet) {
|
||||
let obj = {};
|
||||
obj.array = res.provider;
|
||||
_this.actionSheet(obj, function(index) {
|
||||
if (cb && typeof(cb) == "function") cb(res.provider[index]);
|
||||
});
|
||||
} else {
|
||||
if (type == 'payment') {
|
||||
let providers = res.provider;
|
||||
let payTypeArray = [];
|
||||
for (let i = 0; i < providers.length; i++) {
|
||||
if (providers[i] == 'wxpay') {
|
||||
payTypeArray[i] = {
|
||||
name: '微信支付',
|
||||
value: providers[i],
|
||||
img: '/static/image/wei.png'
|
||||
};
|
||||
} else if (providers[i] == 'alipay') {
|
||||
payTypeArray[i] = {
|
||||
name: "支付宝支付",
|
||||
value: providers[i],
|
||||
img: '/static/image/ali.png'
|
||||
};
|
||||
}
|
||||
}
|
||||
if (cb && typeof(cb) == "function") cb(payTypeArray);
|
||||
} else {
|
||||
if (cb && typeof(cb) == "function") cb(res);
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
},
|
||||
// #ifdef APP-PLUS
|
||||
getShare(providerName, WXScene, shareType, title, summary, href, imageUrl, miniProgramObj, mediaUrl, scb,
|
||||
fcb) { //miniProgram: {path: '', type: 0, webUrl: ''}
|
||||
let _this = this;
|
||||
if (typeof(shareType) == 'number' && !isNaN(shareType) && shareType >= 0) {
|
||||
_this.readyShare(providerName, WXScene, shareType, title, summary, href, imageUrl, miniProgramObj,
|
||||
mediaUrl, scb,
|
||||
fcb);
|
||||
} else {
|
||||
_this.actionSheet(_this.shareTypeListSheetArray, function(index) {
|
||||
_this.readyShare(providerName, WXScene, _this.shareTypeListSheetArray.array[index], title,
|
||||
summary, href,
|
||||
imageUrl, miniProgramObj, mediaUrl, scb, fcb);
|
||||
});
|
||||
}
|
||||
},
|
||||
readyShare(providerName, WXScene, shareType, title, summary, href, imageUrl, miniProgramObj, mediaUrl, scb,
|
||||
fcb) {
|
||||
let _this = this;
|
||||
let shareObjData = {};
|
||||
switch (shareType) {
|
||||
case 0:
|
||||
shareObjData = {
|
||||
href: href,
|
||||
summary: summary,
|
||||
imageUrl: imageUrl
|
||||
};
|
||||
break;
|
||||
case 1:
|
||||
shareObjData = {
|
||||
summary: summary,
|
||||
href: href
|
||||
};
|
||||
break;
|
||||
case 2:
|
||||
shareObjData = {
|
||||
imageUrl: imageUrl
|
||||
};
|
||||
break;
|
||||
case 3:
|
||||
if (mediaUrl) {
|
||||
_this.showToast('暂不支持此分享类型');
|
||||
return;
|
||||
};
|
||||
shareObjData = {
|
||||
mediaUrl: mediaUrl
|
||||
};
|
||||
break;
|
||||
case 4:
|
||||
if (mediaUrl) {
|
||||
_this.showToast('暂不支持此分享类型');
|
||||
return;
|
||||
};
|
||||
shareObjData = {
|
||||
mediaUrl: mediaUrl
|
||||
};
|
||||
break;
|
||||
case 5:
|
||||
shareObjData = {
|
||||
miniProgram: {
|
||||
...miniProgramObj,
|
||||
id: miniProgramId,
|
||||
type: miniProgramShareType
|
||||
},
|
||||
imageUrl: imageUrl
|
||||
};
|
||||
providerName = 'weixin';
|
||||
break;
|
||||
default:
|
||||
_this.showToast('分享参数-shareType错误');
|
||||
return;
|
||||
break;
|
||||
}
|
||||
shareObjData.title = title;
|
||||
_this.share(providerName, WXScene, shareType, shareObjData, function(res) {
|
||||
if (scb && typeof(scb) == 'function') scb(res);
|
||||
}, function(err) {
|
||||
if (fcb && typeof(fcb) == 'function') fcb(err);
|
||||
});
|
||||
},
|
||||
share(providerName, WXScene, shareType, data, scb, fcb) {
|
||||
let _this = this;
|
||||
let shareObj = {
|
||||
provider: '',
|
||||
success: Function,
|
||||
fail: Function
|
||||
};
|
||||
if (providerName && providerName != '') {
|
||||
shareObj.provider = providerName;
|
||||
if (providerName == 'weixin') {
|
||||
_this.readyShareWXScene(WXScene, function(Scene) {
|
||||
shareObj.scene = Scene;
|
||||
_this.doingShare(shareObj, shareType, data, scb, fcb);
|
||||
});
|
||||
} else {
|
||||
_this.doingShare(shareObj, shareType, data, scb, fcb);
|
||||
}
|
||||
} else {
|
||||
_this.getProvider('share', function(name) {
|
||||
shareObj.provider = name;
|
||||
if (name == 'weixin') {
|
||||
_this.readyShareWXScene(WXScene, function(Scene) {
|
||||
shareObj.scene = Scene;
|
||||
_this.doingShare(shareObj, shareType, data, scb, fcb);
|
||||
});
|
||||
} else {
|
||||
_this.doingShare(shareObj, shareType, data, scb, fcb);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
},
|
||||
readyShareWXScene(WXScene, cb) {
|
||||
let _this = this;
|
||||
let WXScenetypelist = {
|
||||
array: ['WXSceneSession', 'WXSenceTimeline', 'WXSceneFavorite']
|
||||
};
|
||||
if (WXScene && WXScene != "") {
|
||||
if (cb && typeof(cb) == 'function') cb(WXScene);
|
||||
} else {
|
||||
_this.actionSheet(WXScenetypelist, function(index) {
|
||||
if (cb && typeof(cb) == 'function') cb(WXScenetypelist.array[index]);
|
||||
});
|
||||
}
|
||||
},
|
||||
doingShare(shareObj, shareType, data, scb, fcb) {
|
||||
shareObj.type = shareType;
|
||||
if (data && data.title) shareObj.title = data.title;
|
||||
switch (shareType) {
|
||||
case 0: //图文链接
|
||||
shareObj.href = data.href;
|
||||
shareObj.summary = data.summary;
|
||||
shareObj.imageUrl = data.imageUrl;
|
||||
break;
|
||||
case 1: //纯文字
|
||||
shareObj.summary = data.summary;
|
||||
shareObj.href = data.href;
|
||||
break;
|
||||
case 2: //纯图片
|
||||
shareObj.imageUrl = data.imageUrl;
|
||||
break;
|
||||
case 3: //音乐
|
||||
if (!data.mediaUrl) {
|
||||
_this.showToast('暂不支持此分享类型');
|
||||
return;
|
||||
};
|
||||
shareObj.mediaUrl = data.mediaUrl;
|
||||
break;
|
||||
case 4: //视频
|
||||
if (!data.mediaUrl) {
|
||||
_this.showToast('暂不支持此分享类型');
|
||||
return;
|
||||
};
|
||||
shareObj.mediaUrl = data.mediaUrl;
|
||||
break;
|
||||
case 5: //小程序
|
||||
if (miniProgramId == '') {
|
||||
uni.showToast('未设置小程序ID, 请使用其他方式分享');
|
||||
return;
|
||||
}
|
||||
let mp = {
|
||||
id: miniProgramId
|
||||
};
|
||||
mp.path = data.miniProgram.path;
|
||||
mp.type = data.miniProgram.type;
|
||||
if (data.miniProgram.webUrl) mp.webUrl = data.miniProgram.webUrl;
|
||||
shareObj.miniProgram = mp;
|
||||
shareObj.imageUrl = data.imageUrl;
|
||||
break;
|
||||
default:
|
||||
appJS.showToast('分享参数-shareType错误');
|
||||
break;
|
||||
}
|
||||
shareObj.success = function(res) {
|
||||
if (scb && typeof(scb) == 'function') scb(res);
|
||||
}
|
||||
shareObj.fail = function(err) {
|
||||
if (fcb && typeof(fcb) == 'function') fcb(err);
|
||||
}
|
||||
log(JSON.stringify(shareObj));
|
||||
uni.share(shareObj);
|
||||
},
|
||||
// #endif
|
||||
}
|
||||
|
||||
function checkMPUrl(url) {
|
||||
// #ifdef MP
|
||||
if (
|
||||
url.substring(0, 4) === 'http' &&
|
||||
url.substring(0, 5) !== 'https' &&
|
||||
url.substring(0, 12) !== 'http://store' &&
|
||||
url.substring(0, 10) !== 'http://tmp' &&
|
||||
url.substring(0, 10) !== 'http://usr'
|
||||
) {
|
||||
url = 'https' + url.substring(4, url.length);
|
||||
}
|
||||
// #endif
|
||||
return url;
|
||||
}
|
||||
|
||||
module.exports = _app;
|
||||
@@ -0,0 +1,147 @@
|
||||
function getLocalFilePath(path) {
|
||||
if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf('_downloads') === 0) {
|
||||
return path
|
||||
}
|
||||
if (path.indexOf('file://') === 0) {
|
||||
return path
|
||||
}
|
||||
if (path.indexOf('/storage/emulated/0/') === 0) {
|
||||
return path
|
||||
}
|
||||
if (path.indexOf('/') === 0) {
|
||||
var localFilePath = plus.io.convertAbsoluteFileSystem(path)
|
||||
if (localFilePath !== path) {
|
||||
return localFilePath
|
||||
} else {
|
||||
path = path.substr(1)
|
||||
}
|
||||
}
|
||||
return '_www/' + path
|
||||
}
|
||||
|
||||
export function pathToBase64(path) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (typeof window === 'object' && 'document' in window) {
|
||||
if (typeof FileReader === 'function') {
|
||||
var xhr = new XMLHttpRequest()
|
||||
xhr.open('GET', path, true)
|
||||
xhr.responseType = 'blob'
|
||||
xhr.onload = function() {
|
||||
if (this.status === 200) {
|
||||
let fileReader = new FileReader()
|
||||
fileReader.onload = function(e) {
|
||||
resolve(e.target.result)
|
||||
}
|
||||
fileReader.onerror = reject
|
||||
fileReader.readAsDataURL(this.response)
|
||||
}
|
||||
}
|
||||
xhr.onerror = reject
|
||||
xhr.send()
|
||||
return
|
||||
}
|
||||
var canvas = document.createElement('canvas')
|
||||
var c2x = canvas.getContext('2d')
|
||||
var img = new Image
|
||||
img.onload = function() {
|
||||
canvas.width = img.width
|
||||
canvas.height = img.height
|
||||
c2x.drawImage(img, 0, 0)
|
||||
resolve(canvas.toDataURL())
|
||||
canvas.height = canvas.width = 0
|
||||
}
|
||||
img.onerror = reject
|
||||
img.src = path
|
||||
return
|
||||
}
|
||||
if (typeof plus === 'object') {
|
||||
plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
|
||||
entry.file(function(file) {
|
||||
var fileReader = new plus.io.FileReader()
|
||||
fileReader.onload = function(data) {
|
||||
resolve(data.target.result)
|
||||
}
|
||||
fileReader.onerror = function(error) {
|
||||
reject(error)
|
||||
}
|
||||
fileReader.readAsDataURL(file)
|
||||
}, function(error) {
|
||||
reject(error)
|
||||
})
|
||||
}, function(error) {
|
||||
reject(error)
|
||||
})
|
||||
return
|
||||
}
|
||||
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
|
||||
wx.getFileSystemManager().readFile({
|
||||
filePath: path,
|
||||
encoding: 'base64',
|
||||
success: function(res) {
|
||||
resolve('data:image/png;base64,' + res.data)
|
||||
},
|
||||
fail: function(error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
reject(new Error('not support'))
|
||||
})
|
||||
}
|
||||
|
||||
export function base64ToPath(base64) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (typeof window === 'object' && 'document' in window) {
|
||||
base64 = base64.split(',')
|
||||
var type = base64[0].match(/:(.*?);/)[1]
|
||||
var str = atob(base64[1])
|
||||
var n = str.length
|
||||
var array = new Uint8Array(n)
|
||||
while (n--) {
|
||||
array[n] = str.charCodeAt(n)
|
||||
}
|
||||
return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
|
||||
}
|
||||
var extName = base64.match(/data\:\S+\/(\S+);/)
|
||||
if (extName) {
|
||||
extName = extName[1]
|
||||
} else {
|
||||
reject(new Error('base64 error'))
|
||||
}
|
||||
var fileName = Date.now() + '.' + extName
|
||||
if (typeof plus === 'object') {
|
||||
var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
|
||||
bitmap.loadBase64Data(base64, function() {
|
||||
var filePath = '_doc/uniapp_temp/' + fileName
|
||||
bitmap.save(filePath, {}, function() {
|
||||
bitmap.clear()
|
||||
resolve(filePath)
|
||||
}, function(error) {
|
||||
bitmap.clear()
|
||||
reject(error)
|
||||
})
|
||||
}, function(error) {
|
||||
bitmap.clear()
|
||||
reject(error)
|
||||
})
|
||||
return
|
||||
}
|
||||
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
|
||||
var filePath = wx.env.USER_DATA_PATH + '/' + fileName
|
||||
wx.getFileSystemManager().writeFile({
|
||||
filePath: filePath,
|
||||
data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
|
||||
encoding: 'base64',
|
||||
success: function() {
|
||||
resolve(filePath)
|
||||
},
|
||||
fail: function(error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
reject(new Error('not support'))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
import _app from '@/shopro/poster/QS-SharePoster/app.js';
|
||||
|
||||
export default {
|
||||
async checkImgHttp(imgPath, type) {
|
||||
let res = await this.getImageInfo_PromiseFc(imgPath)
|
||||
let newPath = '';
|
||||
if (!res) {
|
||||
uni.hideLoading()
|
||||
switch (type) {
|
||||
case 'avatar':
|
||||
console.log('%cerr:海报头像图片错误: ' + imgPath, 'color:green;background:yellow');
|
||||
break;
|
||||
case 'bgImage':
|
||||
console.log('%cerr:海报背景图片错误: ' + imgPath, 'color:green;background:yellow');
|
||||
break;
|
||||
case 'wxCode':
|
||||
console.log('%cerr:海报微信二维码图片错误: ' + imgPath, 'color:green;background:yellow');
|
||||
break;
|
||||
case 'goodsImage':
|
||||
console.log('%cerr:海报商品图片错误: ' + imgPath, 'color:green;background:yellow');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
let pathArr = imgPath.split('://');
|
||||
// #ifdef H5
|
||||
let ishttps = 'https:' == window.location.protocol ? true : false;
|
||||
ishttps ? (pathArr[0] = 'https') : (pathArr[0] = 'http');
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
pathArr[0] = 'https'
|
||||
// #endif
|
||||
newPath = pathArr.join('://');
|
||||
}
|
||||
|
||||
return newPath;
|
||||
},
|
||||
// 检测图片是否可用
|
||||
getImageInfo_PromiseFc(imgPath) {
|
||||
return new Promise((rs, rj) => {
|
||||
imgPath = this.checkMPUrl(imgPath);
|
||||
uni.getImageInfo({
|
||||
src: imgPath,
|
||||
success: res => {
|
||||
rs(1);
|
||||
},
|
||||
fail: err => {
|
||||
console.log(err)
|
||||
rs(0);
|
||||
}
|
||||
})
|
||||
});
|
||||
},
|
||||
// 微信头像
|
||||
checkMPUrl(url) {
|
||||
// #ifdef MP
|
||||
if (
|
||||
url.substring(0, 4) === 'http' &&
|
||||
url.substring(0, 5) !== 'https' &&
|
||||
url.substring(0, 12) !== 'http://store' &&
|
||||
url.substring(0, 10) !== 'http://tmp' &&
|
||||
url.substring(0, 10) !== 'http://usr'
|
||||
) {
|
||||
url = 'https' + url.substring(4, url.length);
|
||||
}
|
||||
// #endif
|
||||
return url;
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 相对比例
|
||||
* @param {Object} bgObj - 背景图片数据对象
|
||||
* @param {Number} num - 需要计算的绘制模块数值
|
||||
*/
|
||||
getScale(bgObj, num = 0) {
|
||||
return Number(bgObj.width * (num / bgObj.width).toFixed(2))
|
||||
},
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 绘制序列
|
||||
* @param {Object} bgObj - 背景图片数据对象
|
||||
* @param {Array} drawArr - 绘制元素数组对象
|
||||
*/
|
||||
initDrawArray(bgObj, drawArr) {
|
||||
const that = this;
|
||||
let arr = []
|
||||
drawArr.forEach(item => {
|
||||
switch (item.type) {
|
||||
case 'text':
|
||||
arr.push({
|
||||
type: 'text',
|
||||
text: item.text, //文本
|
||||
size: that.getScale(bgObj, item.size) || 50, //大小
|
||||
color: item.color || 'black', //颜色
|
||||
alpha: item.alpha || 1, //透明度
|
||||
textAlign: item.textAlign || 'left', //文字x对齐方式: 'left'、'middle'、'right'
|
||||
textBaseline: item.textBaseline ||
|
||||
'middle', //文字y对齐方式: 'top'、'bottom'、'middle'、'normal'
|
||||
dx: that.getScale(bgObj, item.dx) || 0, //文字x轴位置
|
||||
dy: that.getScale(bgObj, item.dy + item.size) || 0, //文字y轴位置
|
||||
[item.lineFeed ? 'lineFeed' : '']: { //设置换行
|
||||
maxWidth: item.lineFeed?.maxWidth && that.getScale(bgObj, item.lineFeed
|
||||
.maxWidth) || bgObj
|
||||
.width, //最大宽度
|
||||
lineHeight: item.lineFeed?.lineHeight && that.getScale(bgObj, item
|
||||
.lineFeed.lineHeight) || 50, //行高
|
||||
lineNum: item.lineFeed?.lineNum || -1, // 最多行数,小于0为无限
|
||||
dx: item.lineFeed?.dx || -1, //非第一行文字位置,小于0为默认位置
|
||||
},
|
||||
infoCallBack: (textLength) => { //回调函数,返回文本信息
|
||||
if (item.isBgCenter) {
|
||||
return {
|
||||
dx: (bgObj.width - textLength) / 2,
|
||||
}
|
||||
}
|
||||
return {}
|
||||
},
|
||||
[item.lineThrough ? 'lineThrough' : '']: { // 设置删除线,默认为字体样式
|
||||
width: item.lineThrough?.width || item.size / 10,
|
||||
style: item.lineThrough?.style || item.color,
|
||||
alpha: item.lineThrough?.alpha || 1,
|
||||
cap: item.lineThrough?.cap || 'butt',
|
||||
},
|
||||
fontStyle: item.fontStyle || 'normal', // 设置字体,跟随系统默认
|
||||
fontVariant: item.fontVariant || 'normal',
|
||||
fontWeight: item.fontWeight || 'normal',
|
||||
fontFamily: item.fontFamily || 'sans-serif',
|
||||
})
|
||||
break;
|
||||
case 'qrcode':
|
||||
arr.push({
|
||||
type: 'qrcode',
|
||||
text: String(item.text || '') || '', // 生成内容
|
||||
size: Number(that.getScale(bgObj, item.size) || 0) || 200, // 二维码大小
|
||||
background: String(item.background || '') || '#ffffff', // 背景色
|
||||
foreground: String(item.foreground || '') || '#000000', // 前景色
|
||||
correctLevel: Number(item.correctLevel || 0) || 3, // 容错级别
|
||||
image: String(item.image || '') || '', // 二维码图标
|
||||
imageSize: Number(that.getScale(bgObj, item.imageSize) || 0) || 40, // 二维码图标大小
|
||||
dx: item.isBgCenter ? (bgObj.width - item.size) / 2 : Number(that.getScale(
|
||||
bgObj, item.dx) || 0) || 0,
|
||||
dy: Number(that.getScale(bgObj, item.dy) || 0) || 0, // y轴距离
|
||||
|
||||
})
|
||||
break;
|
||||
case 'image':
|
||||
arr.push({
|
||||
type: 'image',
|
||||
url: this.checkImgHttp(item.url, item.name), // 网络图片路径
|
||||
dWidth: Number(that.getScale(bgObj, item.dWidth) || 0) || 100, //绘制图像的宽度,允许缩放
|
||||
dHeight: Number(that.getScale(bgObj, item.dHeight) || 0) || 100, //绘制图像的高度,允许缩放
|
||||
sWidth: Number(that.getScale(bgObj, item.sWidth) || 0) || 100, //绘制图像的宽度,允许缩放
|
||||
sHeight: Number(that.getScale(bgObj, item.sHeight) || 0) || 100, //绘制图像的高度,允许缩放
|
||||
dx: Number(that.getScale(bgObj, item.dx) || 0) || 0, // x轴距离
|
||||
dy: Number(that.getScale(bgObj, item.dy) || 0) || 0, // y轴距离
|
||||
sx: Number(that.getScale(bgObj, item.sx) || 0) || 0,
|
||||
sy: Number(that.getScale(bgObj, item.sy) || 0) || 0,
|
||||
[item.circleSet ? 'circleSet' : '']: { //圆形设置
|
||||
r: Number(item.circleSet?.r && that.getScale(bgObj, item.circleSet
|
||||
.r) || 0),
|
||||
x: Number(item.circleSet?.x && that.getScale(bgObj, item.circleSet
|
||||
.x) || 0),
|
||||
y: Number(item.circleSet?.y && that.getScale(bgObj, item.circleSet
|
||||
.y) || 0),
|
||||
},
|
||||
[item.roundRectSet ? 'roundRectSet' : '']: { //圆角设置
|
||||
r: Number(item.roundRectSet?.r && that.getScale(bgObj, item
|
||||
.roundRectSet.r) || 0),
|
||||
},
|
||||
infoCallBack: (imgInfo) => { //回调函数,返回图片信息
|
||||
if (item.isBgCenter) {
|
||||
return {
|
||||
dx: (bgObj.width - item.dWidth) / 2,
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
break;
|
||||
default:
|
||||
_app.log('暂无次类型');
|
||||
break;
|
||||
}
|
||||
})
|
||||
return arr
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户