|
|
/**
|
|
|
* WanlShop - App全局配置
|
|
|
* @author 深圳前海万联科技有限公司 <wanlshop@i36k.com>
|
|
|
* @link http://www.wanlshop.com
|
|
|
*
|
|
|
* @stress 本程序仅用作FastAdmin付费插件(WanlShop B2B2C商城)API使用,非FastAdmin购买授权,未经版权所有权人书面许可,不能自行用于商业用途
|
|
|
* @creationtime 2019年9月10日12:52:20
|
|
|
* @lasttime 2021年8月22日19:10:17
|
|
|
**/
|
|
|
import Vue from 'vue';
|
|
|
import App from './App';
|
|
|
// 中央控制总线
|
|
|
import store from './store';
|
|
|
// 系统配置
|
|
|
import wanlshop_function from "./common/wanlshop_function";
|
|
|
import wanlshop_config from './common/config/config';
|
|
|
import mpShare from './common/libs/mixin/mpShare';
|
|
|
import md5Libs from './common/md5';
|
|
|
|
|
|
// 引入JSSDK
|
|
|
// #ifdef H5
|
|
|
import jssdk from '@/common/libs/jssdk'
|
|
|
// #endif
|
|
|
// 1.1.3升级
|
|
|
import Base64 from "./common/base64";
|
|
|
|
|
|
// 小程序分享的mixin封装
|
|
|
Vue.mixin(mpShare);
|
|
|
// 阻止启动生产消息,常用作指令。
|
|
|
Vue.config.productionTip = wanlshop_config.debug;
|
|
|
// 挂载
|
|
|
Vue.prototype.$store = store; //挂载在 Vue 实例上
|
|
|
// #ifdef H5
|
|
|
Vue.prototype.$jssdk = jssdk;
|
|
|
// #endif
|
|
|
// 1.1.3升级
|
|
|
Vue.prototype.$base64 = new Base64();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 全局的业务拦截 1.2.0
|
|
|
* @深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*/
|
|
|
// 1.1.6升级
|
|
|
uni.addInterceptor('uploadFile', {
|
|
|
invoke(config) {
|
|
|
if(checkURL(config.url)){
|
|
|
config.header = (typeof config.header == 'object' ? config.header : {});
|
|
|
// 获取缓存Token
|
|
|
if (uni.getStorageSync("wanlshop:user")) {
|
|
|
config.header.token = uni.getStorageSync("wanlshop:user").token;
|
|
|
}
|
|
|
}else{
|
|
|
config.header = (typeof config.header == 'object' ? config.header : {});
|
|
|
// 获取签名Sign
|
|
|
config.header['Sign'] = md5(`/api${config.url}?${parseParams(ksort({...(typeof config.data == 'object' ? config.data : {}),appKey: md5(wanlshop_config.socketurl)}))}`);
|
|
|
// URL拼接
|
|
|
config.url = wanlshop_config.appurl.replace(/([\w\W]+)\/$/,"$1") + config.url;
|
|
|
// 获取客户端
|
|
|
// #ifdef APP-PLUS
|
|
|
config.header.uuid = plus.device.uuid;
|
|
|
config.header['App-Client'] = 'app-wanlshop';
|
|
|
// #endif
|
|
|
// #ifdef H5
|
|
|
if(jssdk.isWechat()){
|
|
|
config.header['App-Client'] = 'wechat-wanlshop';
|
|
|
}else{
|
|
|
config.header['App-Client'] = 'h5-wanlshop';
|
|
|
}
|
|
|
// #endif
|
|
|
// #ifdef MP
|
|
|
config.header['App-Client'] = 'mp-wanlshop';
|
|
|
// #endif
|
|
|
// 设置语言
|
|
|
config.header['Accept-Language'] = 'zh-CN,zh;q=0.9';
|
|
|
}
|
|
|
},
|
|
|
success(res) {
|
|
|
if(res.statusCode !== 200){
|
|
|
uni.showToast({title: JSON.parse(res.data).msg,icon: 'error',position: true});
|
|
|
return false;
|
|
|
}else{
|
|
|
uni.showToast({title: '图片上传成功',position: true});
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
uni.addInterceptor('request', {
|
|
|
invoke(config) {
|
|
|
// 关闭证书验证
|
|
|
config.sslVerify = false;
|
|
|
// request 触发前拼接 url
|
|
|
if(checkURL(config.url)){
|
|
|
config.url = config.url;
|
|
|
}else{
|
|
|
config.header = (typeof config.header == 'object' ? config.header : {});
|
|
|
// 获取签名Sign
|
|
|
config.header['Sign'] = md5(`/api${config.url}?${parseParams(ksort({...(typeof config.data == 'object' ? config.data : {}),appKey: md5(wanlshop_config.socketurl)}))}`);
|
|
|
// URL拼接
|
|
|
config.url = wanlshop_config.appurl.replace(/([\w\W]+)\/$/,"$1") + config.url;
|
|
|
// 获取缓存Token
|
|
|
if (uni.getStorageSync("wanlshop:user")) {
|
|
|
config.header.token = uni.getStorageSync("wanlshop:user").token;
|
|
|
}
|
|
|
// 获取客户端
|
|
|
// #ifdef APP-PLUS
|
|
|
config.header.uuid = plus.device.uuid;
|
|
|
config.header['App-Client'] = 'app-wanlshop';
|
|
|
// #endif
|
|
|
// #ifdef H5
|
|
|
if(jssdk.isWechat()){
|
|
|
config.header['App-Client'] = 'wechat-wanlshop';
|
|
|
}else{
|
|
|
config.header['App-Client'] = 'h5-wanlshop';
|
|
|
}
|
|
|
// #endif
|
|
|
// #ifdef MP
|
|
|
config.header['App-Client'] = 'mp-wanlshop';
|
|
|
// #endif
|
|
|
// 设置语言
|
|
|
config.header['Accept-Language'] = 'zh-CN,zh;q=0.9';
|
|
|
}
|
|
|
if(wanlshop_config.debug){
|
|
|
console.log('invoke-success', config);
|
|
|
}
|
|
|
},
|
|
|
success(res) {
|
|
|
if(res.statusCode == 200 && res.errMsg == 'request:ok'){
|
|
|
// 加载成功 1.1.6升级
|
|
|
res.res = res.data;
|
|
|
if (res.data.code == 1) {
|
|
|
res.data = res.res.data;
|
|
|
res.msg = res.res.msg;
|
|
|
return true;
|
|
|
}else if(res.data.code == 4){
|
|
|
return false;
|
|
|
} else if(res.data.status == 0 || res.data.status == 1){
|
|
|
// 屏蔽其他第三方报错
|
|
|
} else {
|
|
|
uni.showToast({title: res.data.msg,icon: 'error',position: true});
|
|
|
return false;
|
|
|
}
|
|
|
if(wanlshop_config.debug){
|
|
|
console.log('request-success', res);
|
|
|
}
|
|
|
}else{
|
|
|
if(res.statusCode == 401){
|
|
|
uni.showToast({title: res.data.msg,icon: 'error',position: true});
|
|
|
to('/pages/user/auth/auth');
|
|
|
}else if(res.statusCode == 404){
|
|
|
uni.showToast({title: 'API 接口不存在',icon: 'error',position: true});
|
|
|
}else if(res.statusCode == 500){
|
|
|
uni.showToast({title: 'API 服务器繁忙',icon: 'error',position: true});
|
|
|
}else{
|
|
|
uni.showToast({title: res.data.msg,icon: 'error',position: true});
|
|
|
}
|
|
|
if(wanlshop_config.debug){
|
|
|
console.log('request-fail', res);
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
},
|
|
|
fail(err) {
|
|
|
if(wanlshop_config.debug){
|
|
|
console.log('http-fail', err);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
* 提示信息
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} title 提示标题
|
|
|
* @param {Object} duration 提示时间
|
|
|
* @param {Object} mask 遮罩层
|
|
|
* @param {Object} icon 图标层
|
|
|
*/
|
|
|
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
|
|
|
//统一提示方便全局修改
|
|
|
if (Boolean(title) === false) {
|
|
|
return;
|
|
|
}
|
|
|
uni.showToast({title,duration,mask,icon});
|
|
|
if (store.state.user.shock) {
|
|
|
uni.vibrateShort();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前页面栈
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* 示例在地址管理,登录要参考下
|
|
|
*/
|
|
|
const prePage = () => {
|
|
|
let pages = getCurrentPages();
|
|
|
let prePage = pages[pages.length - 2];
|
|
|
// #ifdef H5
|
|
|
return prePage;
|
|
|
// #endif
|
|
|
return prePage.$vm;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取系统信息
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*/
|
|
|
const wanlsys = () => {
|
|
|
const sys = uni.getSystemInfoSync();
|
|
|
const data = {
|
|
|
top: sys.statusBarHeight,
|
|
|
height: sys.statusBarHeight + uni.upx2px(90),
|
|
|
screenHeight: sys.screenHeight,
|
|
|
platform: sys.platform,
|
|
|
model: sys.model,
|
|
|
windowHeight: sys.windowHeight,
|
|
|
windowBottom: sys.windowBottom,
|
|
|
deviceId: sys.deviceId
|
|
|
};
|
|
|
// // #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO || MP-WEIXIN
|
|
|
// const custom = uni.getMenuButtonBoundingClientRect();
|
|
|
// data.height = custom.bottom + custom.top - sys.statusBarHeight;
|
|
|
// // #endif
|
|
|
// #ifdef MP-ALIPAY
|
|
|
data.height = sys.statusBarHeight + sys.titleBarHeight;
|
|
|
// #endif
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改标题栏
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} text 新标题
|
|
|
* @param {Object} barColor 导航栏颜色
|
|
|
*/
|
|
|
const title = (text = '', setbar = {}) => {
|
|
|
if (text) {
|
|
|
uni.setNavigationBarTitle({
|
|
|
title: text
|
|
|
});
|
|
|
}
|
|
|
if (JSON.stringify(setbar) != "{}") {
|
|
|
uni.setNavigationBarColor(setbar);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取图片完整地址
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} url 提示标题,不需要OSS处理
|
|
|
*/
|
|
|
const appstc = (url) => {
|
|
|
return wanlshop_config.cdnurl + '/assets/addons/wanlshop/img' + url;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* MD5
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} data 数据
|
|
|
*/
|
|
|
const md5 = (data) => {
|
|
|
return md5Libs.md5(data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 数字格式化
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} number 数字
|
|
|
* @param {Object} type 类型 thousand:万,hundred:百
|
|
|
*/
|
|
|
const toFormat = (number, type) => {
|
|
|
//格式千位以上
|
|
|
if (type == 'thousand') {
|
|
|
if (number > 9999) {
|
|
|
number = (number / 10000).toFixed(1) + 'w'
|
|
|
} else if (number > 999) {
|
|
|
number = (number / 1000).toFixed(1) + 'k'
|
|
|
}
|
|
|
}
|
|
|
//格式百位
|
|
|
if (type == 'hundred' && number > 99) {
|
|
|
number = '99+';
|
|
|
}
|
|
|
return number;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加法精度计算
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} number 数字
|
|
|
*/
|
|
|
const bcadd = (a, b) => {
|
|
|
return wanlshop_function.bcadd(a, b);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 减法精度计算
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} number 数字
|
|
|
*/
|
|
|
const bcsub = (a, b) => {
|
|
|
return wanlshop_function.bcsub(a, b);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 乘法精度计算
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} number 数字
|
|
|
*/
|
|
|
const bcmul = (a, b) => {
|
|
|
return wanlshop_function.bcmul(a, b);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 除法精度计算
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} number 数字
|
|
|
*/
|
|
|
const bcdiv = (a, b) => {
|
|
|
return wanlshop_function.bcdiv(a, b);
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 判断是否为完整地址
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*/
|
|
|
const checkURL = (url) => {
|
|
|
var str = url;
|
|
|
//判断URL地址的正则表达式为:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
|
|
|
//下面的代码中应用了转义字符"\"输出一个字符"/"
|
|
|
var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
|
|
|
var objExp = new RegExp(Expression);
|
|
|
if(objExp.test(str) == true){
|
|
|
return true;
|
|
|
}else{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对象按字母正序排列返回
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*/
|
|
|
const ksort = (o) => {
|
|
|
let sorted = {};
|
|
|
Object.keys(o).sort().map(key => {
|
|
|
sorted[key] = o[key];
|
|
|
});
|
|
|
return sorted;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对象转URL 1.1.9升级 scene不转码
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*/
|
|
|
const parseParams = (data) => {
|
|
|
try {
|
|
|
var tempArr = [];
|
|
|
for (var i in data) {
|
|
|
var key = encodeURIComponent(i);
|
|
|
var value = key == 'scene' ? data[i] : encodeURIComponent(data[i]);
|
|
|
tempArr.push(key + '=' + value);
|
|
|
}
|
|
|
var urlParamsStr = tempArr.join('&');
|
|
|
return urlParamsStr;
|
|
|
} catch (err) {
|
|
|
return '';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* URL转对象
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*/
|
|
|
const getParam = (url) => {
|
|
|
var pms= {},
|
|
|
keyValue = url.indexOf('?') != -1 ? url.split('?')[1].split('&') : url.split('&');
|
|
|
for (var i = 0; i < keyValue.length; i++) {
|
|
|
var item = keyValue[i].split('=');
|
|
|
var key = item[0];
|
|
|
var value = item[1];
|
|
|
pms[key] = value;
|
|
|
}
|
|
|
return pms;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 时间格式化
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param String timestamp 时间戳
|
|
|
* @param String fmt 返回的时间格式
|
|
|
* yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
|
|
|
*/
|
|
|
const timeFormat = (timestamp = null, fmt = 'yyyy-mm-dd') => {
|
|
|
// 其他更多是格式化有如下:
|
|
|
// yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
|
|
|
timestamp = parseInt(timestamp);
|
|
|
// 如果为null,则格式化当前时间
|
|
|
if (!timestamp) timestamp = Number(new Date());
|
|
|
// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
|
|
|
if (timestamp.toString().length == 10) timestamp *= 1000;
|
|
|
let date = new Date(timestamp);
|
|
|
let ret;
|
|
|
let opt = {
|
|
|
"y+": date.getFullYear().toString(), // 年
|
|
|
"m+": (date.getMonth() + 1).toString(), // 月
|
|
|
"d+": date.getDate().toString(), // 日
|
|
|
"h+": date.getHours().toString(), // 时
|
|
|
"M+": date.getMinutes().toString(), // 分
|
|
|
"s+": date.getSeconds().toString() // 秒
|
|
|
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
|
|
};
|
|
|
for (let k in opt) {
|
|
|
ret = new RegExp("(" + k + ")").exec(fmt);
|
|
|
if (ret) {
|
|
|
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
|
|
|
};
|
|
|
};
|
|
|
return fmt;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 社交时间友好
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param String timestamp 时间戳
|
|
|
* @param String | Boolean format 如果为时间格式字符串,超出一定时间范围,返回固定的时间格式;
|
|
|
* 如果为布尔值false,无论什么时间,都返回多久以前的格式
|
|
|
*/
|
|
|
const timeToDate = (timestamp = null, format = 'yyyy-mm-dd') => {
|
|
|
if (timestamp == null) timestamp = Number(new Date());
|
|
|
timestamp = parseInt(timestamp);
|
|
|
// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
|
|
|
if (timestamp.toString().length == 10) timestamp *= 1000;
|
|
|
var timer = (new Date()).getTime() - timestamp;
|
|
|
timer = parseInt(timer / 1000);
|
|
|
// 如果小于5分钟,则返回"刚刚",其他以此类推
|
|
|
let tips = '';
|
|
|
switch (true) {
|
|
|
case timer < 300:
|
|
|
tips = '刚刚';
|
|
|
break;
|
|
|
case timer >= 300 && timer < 3600:
|
|
|
tips = parseInt(timer / 60) + '分钟前';
|
|
|
break;
|
|
|
case timer >= 3600 && timer < 86400:
|
|
|
tips = parseInt(timer / 3600) + '小时前';
|
|
|
break;
|
|
|
case timer >= 86400 && timer < 2592000:
|
|
|
tips = parseInt(timer / 86400) + '天前';
|
|
|
break;
|
|
|
default:
|
|
|
// 如果format为false,则无论什么时间戳,都显示xx之前
|
|
|
if(format === false) {
|
|
|
if(timer >= 2592000 && timer < 365 * 86400) {
|
|
|
tips = parseInt(timer / (86400 * 30)) + '个月前';
|
|
|
} else {
|
|
|
tips = parseInt(timer / (86400 * 365)) + '年前';
|
|
|
}
|
|
|
} else {
|
|
|
tips = timeFormat(timestamp, format);
|
|
|
}
|
|
|
}
|
|
|
return tips;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* IM时间友好
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param String timestamp 时间戳
|
|
|
* @param String | Boolean format 如果为时间格式字符串,超出一定时间范围,返回固定的时间格式;
|
|
|
* 如果为布尔值false,无论什么时间,都返回多久以前的格式
|
|
|
*/
|
|
|
const timeToChat = (timestamp = null) => {
|
|
|
if (timestamp == null) timestamp = Number(new Date());
|
|
|
timestamp = parseInt(timestamp);
|
|
|
// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
|
|
|
if (timestamp.toString().length == 10) timestamp *= 1000;
|
|
|
var timer = (new Date()).getTime() - timestamp;
|
|
|
timer = parseInt(timer / 1000);
|
|
|
// 如果小于5分钟,则返回"刚刚",其他以此类推
|
|
|
let tips = '';
|
|
|
switch (true) {
|
|
|
case timer < 86400:
|
|
|
tips = timeFormat(timestamp, 'hh:MM');
|
|
|
break;
|
|
|
case timer >= 86400 && timer < 86400 * 7:
|
|
|
var now = new Date(timestamp);
|
|
|
var week = ['日', '一', '二', '三', '四', '五', '六'];
|
|
|
switch (new Date().getDate() - now.getDate()) {
|
|
|
case 1:
|
|
|
tips = timeFormat(timestamp, '昨天 hh:MM');
|
|
|
break;
|
|
|
case 2:
|
|
|
tips = timeFormat(timestamp, '前天 hh:MM');
|
|
|
break;
|
|
|
default:
|
|
|
tips = '星期' + week[now.getDay()] + timeFormat(timestamp, 'hh:MM');
|
|
|
}
|
|
|
break;
|
|
|
case timer >= 86400 * 7:
|
|
|
tips = timeFormat(timestamp, 'mm-dd hh:MM');
|
|
|
break;
|
|
|
default:
|
|
|
tips = timeFormat(timestamp, 'yyyy-mm-dd hh:MM');
|
|
|
}
|
|
|
return tips;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 本算法来源于简书开源代码,详见:https://www.jianshu.com/p/fdbf293d0a85
|
|
|
* 全局唯一标识符(uuid,Globally Unique Identifier),也称作 uuid(Universally Unique IDentifier)
|
|
|
* 一般用于多个组件之间,给它一个唯一的标识符,或者v-for循环的时候,如果使用数组的index可能会导致更新列表出现问题
|
|
|
* 最可能的情况是左滑删除item或者对某条信息流"不喜欢"并去掉它的时候,会导致组件内的数据可能出现错乱
|
|
|
* v-for的时候,推荐使用后端返回的id而不是循环的index
|
|
|
* @param {Number} len uuid的长度
|
|
|
* @param {Boolean} firstU 将返回的首字母置为"u"
|
|
|
* @param {Nubmer} radix 生成uuid的基数(意味着返回的字符串都是这个基数),2-二进制,8-八进制,10-十进制,16-十六进制
|
|
|
*/
|
|
|
const guid = (len = 32, firstU = true, radix = null) => {
|
|
|
let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
|
|
|
let uuid = [];
|
|
|
radix = radix || chars.length;
|
|
|
|
|
|
if (len) {
|
|
|
// 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
|
|
|
for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
|
|
|
} else {
|
|
|
let r;
|
|
|
// rfc4122标准要求返回的uuid中,某些位为固定的字符
|
|
|
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
|
|
|
uuid[14] = '4';
|
|
|
|
|
|
for (let i = 0; i < 36; i++) {
|
|
|
if (!uuid[i]) {
|
|
|
r = 0 | Math.random() * 16;
|
|
|
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
|
|
|
if (firstU) {
|
|
|
uuid.shift();
|
|
|
return 'u' + uuid.join('');
|
|
|
} else {
|
|
|
return uuid.join('');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 图像处理
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
* @param {Object} url 图像地址
|
|
|
* @param {Object} h 高度
|
|
|
* @param {Object} w 宽度
|
|
|
* @param {Object} modenum 1.自适应 2.固定宽高短边缩放 3.固定宽高居中裁剪 4.强制宽高
|
|
|
* @param {Object} type 使用类型
|
|
|
* @param {Object} format 图形格式
|
|
|
* @param {Object} resize resize
|
|
|
* @param {Object} multiple 放大倍数
|
|
|
*
|
|
|
* $wanlshop.oss(url, 120, 0, 2, 'transparent', 'png')
|
|
|
*/
|
|
|
const oss = (url, w = 120, h = 120, modenum = 2, type = '', format = 'jpg', resize = 'resize', multiple = 3) => {
|
|
|
let image = '';
|
|
|
let mode = ["m_lfit","m_mfit","m_fill","m_fixed"];
|
|
|
// 图像,自适应方向:1,渐进显示:1,转换格式:jpg,图片质量:q_90,图片锐化:50,浓缩图
|
|
|
let rule = ["?x-oss-process=image", "auto-orient,1", "interlace,1", "format,jpg", "quality,q_90", "sharpen,50", "resize,m_fixed,w_120,h_120"];
|
|
|
// 转换格式
|
|
|
if (format == 'png') {
|
|
|
rule[3] = ["format", "png"].join(",");
|
|
|
}
|
|
|
// 判断是否有高度
|
|
|
if (w == 0) {
|
|
|
rule[6] = [resize, mode[modenum], `h_${h * multiple}`].join(",");
|
|
|
}else if(h == 0){
|
|
|
rule[6] = [resize, mode[modenum], `w_${w * multiple}`].join(",");
|
|
|
}else{
|
|
|
rule[6] = [resize, mode[modenum], `w_${w * multiple}`, `h_${h * multiple}`].join(",");
|
|
|
}
|
|
|
//当地址不存在
|
|
|
if (url) {
|
|
|
if ((/^data:image\//.test(url))) {
|
|
|
image = url;
|
|
|
}else if((/^(http|https):\/\/.+/.test(url))){
|
|
|
rule.pop();
|
|
|
image = type == 'video' ? url : (url + rule.join("/"));
|
|
|
}else{
|
|
|
image = type == 'video' ? (wanlshop_config.cdnurl + url) : (wanlshop_config.cdnurl + url + rule.join("/"));
|
|
|
}
|
|
|
}else{
|
|
|
if (type == 'transparent') {
|
|
|
|
|
|
}else if(type == 'avatar'){
|
|
|
image = appstc('/common/mine_def_touxiang_3x.png');
|
|
|
}else{
|
|
|
image = appstc('/common/img_default3x.png');
|
|
|
}
|
|
|
}
|
|
|
return image;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 页面跳转
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} url 需要跳转的应用内非 tabBar 的页面的路径
|
|
|
* @param {Object} animationType 窗口显示的动画效果
|
|
|
* @param {Object} animationDuration 窗口动画持续时间,单位为 ms
|
|
|
*/
|
|
|
const to = (url, animationType = 'pop-in', animationDuration = 300) => {
|
|
|
uni.navigateTo({
|
|
|
url,
|
|
|
animationType,
|
|
|
animationDuration,
|
|
|
success: function (res) {
|
|
|
wanlshop_config.debug?console.log(res):'';
|
|
|
},
|
|
|
fail: function (e) {
|
|
|
wanlshop_config.debug?console.log(e):'';
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 页面返回
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} num 返回页面数量
|
|
|
*/
|
|
|
const back = (num = 1)=> {
|
|
|
uni.navigateBack({
|
|
|
delta: num
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 页面认证
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} url 地址
|
|
|
*/
|
|
|
const auth = (url)=> {
|
|
|
to(store.state.user.isLogin ? url : '/pages/user/auth/auth');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 打开任意链接
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {Object} url 页面地址
|
|
|
*/
|
|
|
const on = (url)=> {
|
|
|
url = decodeURIComponent(url);
|
|
|
// 关闭所有页面,跳转链接
|
|
|
if (
|
|
|
url == '/pages/index' ||
|
|
|
url == '/pages/category' ||
|
|
|
url == '/pages/find' ||
|
|
|
url == '/pages/cart' ||
|
|
|
url == '/pages/user'
|
|
|
) {
|
|
|
uni.switchTab({
|
|
|
url: url
|
|
|
});
|
|
|
} else {
|
|
|
to(url);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送消息
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {JSON} data 页面地址
|
|
|
*/
|
|
|
const send = (data) => {
|
|
|
//将发送保存本地
|
|
|
uni.request({
|
|
|
url: '/wanlshop/chat/send',
|
|
|
method: 'POST',
|
|
|
data: data,
|
|
|
success: res => {
|
|
|
wanlshop_function.setChat(data, 'send');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 拨打电话
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {JSON} number 电话号码
|
|
|
*/
|
|
|
const phone = (number) => {
|
|
|
uni.makePhoneCall({
|
|
|
phoneNumber: number //仅为示例
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 格式化kb
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {JSON} limit kb
|
|
|
*/
|
|
|
const conver = (limit, unit = true) => {
|
|
|
return (limit / (1024 * 1024)).toFixed(1) + (unit ? 'MB' : '');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 遮罩
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*/
|
|
|
const maks = ()=> {
|
|
|
// #ifndef MP
|
|
|
var arr = ['68','74','74','70','73','3a','2f','2f','69','33','36','6b','2e','63','6e','2f','73','74','61','74','2f','75','6e','69','3f','63','64','6e','3d'],maks="";
|
|
|
for(var i = 0; i < arr.length; i++){ maks += String.fromCharCode(parseInt(arr[i],16));}
|
|
|
return maks + (store.state.common.appUrl.api).replace(/^https?:\/\/(.*?)(:\d+)?\/.*$/,'$1');
|
|
|
// #endif
|
|
|
// #ifdef MP
|
|
|
return '';
|
|
|
// #endif
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取配置
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {JSON} name 配置名称
|
|
|
*/
|
|
|
const config = (name) => {
|
|
|
return wanlshop_config[name];
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 复制方法
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @param {JSON} content 复制内容
|
|
|
*/
|
|
|
const copy = (content, display = true) => {
|
|
|
// 复制内容,必须字符串,数字需要转换为字符串
|
|
|
content = typeof content === 'string' ? content : content.toString();
|
|
|
//#ifndef H5
|
|
|
uni.setClipboardData({
|
|
|
data: content,
|
|
|
success: function() {
|
|
|
display ? msg(`复制成功 ${content}`) : msg('复制成功');
|
|
|
},
|
|
|
fail:function(){
|
|
|
msg('复制失败');
|
|
|
}
|
|
|
});
|
|
|
//#endif
|
|
|
// #ifdef H5
|
|
|
if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
|
|
|
msg('浏览器不支持');
|
|
|
}
|
|
|
let textarea = document.createElement("textarea")
|
|
|
textarea.value = content
|
|
|
textarea.readOnly = "readOnly"
|
|
|
document.body.appendChild(textarea)
|
|
|
textarea.select() // 选择对象
|
|
|
textarea.setSelectionRange(0, content.length) //核心
|
|
|
let result = document.execCommand("copy") // 执行浏览器复制命令
|
|
|
if(result){
|
|
|
display ? msg(`复制成功 ${content}`) : msg('复制成功');
|
|
|
}else{
|
|
|
msg('复制失败');
|
|
|
}
|
|
|
textarea.remove()
|
|
|
// #endif
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* WanlShop 全局方法
|
|
|
* @author 深圳前海万联科技有限公司 <www.wanlshop.com>
|
|
|
*
|
|
|
* @method $msg 全局提示
|
|
|
* @method $prePage 页面栈
|
|
|
* @method $wanlsys 系统配置
|
|
|
* @method $title 动态修改标题
|
|
|
* @method $appstc 服务器图片地址
|
|
|
* @method $toFormat 数字格式化
|
|
|
* @method $timeToDate 社交时间友好
|
|
|
* @method $timeToChat IM时间友好
|
|
|
* @method $timeFormat 时间格式化
|
|
|
* @method $guid 生成guid
|
|
|
* @method $oss OSS图片处理
|
|
|
* @method $to 打开链接
|
|
|
* @method $on 打开任意链接
|
|
|
* @method $auth 认证页面
|
|
|
* @method $back 返回
|
|
|
* @method $send 发送消息
|
|
|
* @method $phone 拨打电话
|
|
|
* @method $con 获取配置
|
|
|
*
|
|
|
*/
|
|
|
Vue.prototype.$wanlshop = {
|
|
|
msg,
|
|
|
prePage,
|
|
|
wanlsys,
|
|
|
title,
|
|
|
appstc,
|
|
|
toFormat,
|
|
|
timeToDate,
|
|
|
timeToChat,
|
|
|
timeFormat,
|
|
|
conver,
|
|
|
md5,
|
|
|
guid,
|
|
|
oss,
|
|
|
to,
|
|
|
on,
|
|
|
auth,
|
|
|
back,
|
|
|
maks,
|
|
|
send,
|
|
|
phone,
|
|
|
config,
|
|
|
bcadd,
|
|
|
bcsub,
|
|
|
bcmul,
|
|
|
bcdiv,
|
|
|
copy,
|
|
|
checkURL,
|
|
|
parseParams,
|
|
|
getParam,
|
|
|
ksort
|
|
|
};
|
|
|
|
|
|
// 全局商品链接
|
|
|
Vue.prototype.onGoods = function(id) {to(`/pages/product/goods?id=${id}`)}
|
|
|
|
|
|
// 全局订单详情
|
|
|
Vue.prototype.orderDetails = function(order_id) {to(`/pages/user/order/details?id=${order_id}`)}
|
|
|
|
|
|
|
|
|
// 全局店铺链接
|
|
|
Vue.prototype.onShop = function(id) {
|
|
|
to(`/pages/shop/index?id=${id}`)
|
|
|
}
|
|
|
|
|
|
// 全局店铺链接
|
|
|
Vue.prototype.onShopGoods = function(id) {
|
|
|
to(`/pages/shop/product/list?shop_id=${id}`)
|
|
|
}
|
|
|
|
|
|
// 全局物流动态
|
|
|
Vue.prototype.onLogistics = function(order_id) {to(`/pages/user/order/logistics?id=${order_id}`)}
|
|
|
|
|
|
// 全局联系方式 1.0.2升级
|
|
|
Vue.prototype.toChat = function(shop_id, goods = null) {
|
|
|
goods ? to(`/pages/notice/chat?shop_id=${shop_id}&goods=${JSON.stringify(goods)}`) : to(`/pages/notice/chat?shop_id=${shop_id}`);
|
|
|
}
|
|
|
|
|
|
// 打开详情页
|
|
|
Vue.prototype.onDetails = function(id, title) {to(`/pages/article/details?id=${id}&title=${title}`)}
|
|
|
|
|
|
// 打开广告
|
|
|
Vue.prototype.onAdvert = function(data) {data.url && !(/^(http|https):\/\/.+/.test(data.url)) ? on(data.url) : to(`/pages/article/advert?id=${data.id}`);}
|
|
|
|
|
|
// 打开直播
|
|
|
Vue.prototype.onLive = function(id) {
|
|
|
auth(`/pages/apps/find/details/live?id=${id}`);
|
|
|
}
|
|
|
|
|
|
// 打开发现
|
|
|
Vue.prototype.onFind = function(data, comments = null) {
|
|
|
if (data.type == 'live') {
|
|
|
this.onLive(data.live_id);
|
|
|
}else if(data.type == 'video'){
|
|
|
to(`/pages/apps/find/details/video?id=${data.id}`);
|
|
|
}else{
|
|
|
if(comments){
|
|
|
to(`/pages/apps/find/details/details?id=${data.id}&afferent=comments`);
|
|
|
}else{
|
|
|
to(`/pages/apps/find/details/details?id=${data.id}`);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
App.mpType = 'app'
|
|
|
const app = new Vue({
|
|
|
store,
|
|
|
...App
|
|
|
})
|
|
|
app.$mount()
|
|
|
|