You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

811 lines
20 KiB

4 years ago
import md5 from "./md5.min";
const HOSTARR = {
'development': 'https://xinwuqu.langye.net/',
'production': 'https://xinwuqu.langye.net/'
};
const HOST = HOSTARR[process.env
.NODE_ENV]; //"https://tiantianxinye.365care.langye.net/";//HOSTARR[process.env.NODE_ENV];//
const formatTime = date => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return [year, month, day].map(formatNumber).join('-');
3 years ago
};
4 years ago
// 其他更多是格式化有如下:
// yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
3 years ago
function timeFormat(dateTime = null, fmt = 'yyyy-mm-dd') {
try {
// 如果为null,则格式化当前时间
if (!dateTime) dateTime = Number(new Date());
// 如果dateTime长度为10或者13则为秒和毫秒的时间戳如果超过13位则为其他的时间格式
if (dateTime.toString().length == 10) dateTime *= 1000;
if (dateTime.toString().indexOf("-") > -1) dateTime = dateTime.replace(/-/g, '/')
let date = new Date(dateTime);
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;
} catch (e) {
console.log(e)
4 years ago
}
}
const stateArr = {
"save": "待处理",
"ongoing": "处理中",
"finished": "已处理"
}
const formatTimeAll = date => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(
':');
};
const formatNumber = n => {
n = n.toString();
return n[1] ? n : '0' + n;
}; // 快捷显示alert
const alert = msg => {
uni.showModal({
title: '',
content: msg,
showCancel: false
});
}; // 订单状态
const orderStatusDic = {
'0': '待确认',
'10': '待派单',
'20': '进行中',
'100': '已完成'
}; // 审核状态
const auditStatusDic = {
'0': '待审核',
'1': '已审核',
'-1': '已拒绝'
}; // 请求封装
const request = options => {
3 years ago
if (!options.customLoading) {
//if(false){
4 years ago
uni.showLoading({
title: '正在加载'
});
} else {
// 当前页面请求数量+1
if (options.bindThis) {
//options.bindThis.loadingCount=options.bindThis.data.loadingCount + 1;
}
}
options.url = HOST + options.api;
options.header = {
...options.header,
//'Accept': 'application/json',
//'Connection': 'keep-alive'
//'content-type': 'application/json'
}; // 如果已登录,请求中拼openId
var access_token = uni.getStorageSync('userInfo').token;
if (isLogin()) {
options.data = {
...options.data,
'token': access_token
};
} // 如果是POST方法
if (options.method == 'POST' && !isNull(access_token)) {
// 拼时间戳
options.data.ts = new Date().getTime();
}
3 years ago
4 years ago
uni.request({
...options,
3 years ago
success: function(res) {
// if(!options.hideload){
// console.log("options.hideload")
// uni.hideLoading();
// }
3 years ago
uni.hideLoading();
4 years ago
if (res.statusCode != 200) {
if (options.utilFail != undefined) {
if (res.statusCode == 401) {
uni.clearStorageSync();
uni.navigateTo({
url: '/pages/login/login'
});
} else {
options.utilFail('TODO: 特殊处理非200错误(' + res.statusCode + ')');
}
}
} else {
if (!isNull(res.data.errorcode) || res.data.errorcode == 0) {
if (options.utilFail != undefined) {
options.utilFail(res.data.errormsg || '接口发生未知错误');
} else {
options.utilFail(res.data.errormsg);
}
} else {
if (options.utilSuccess != undefined) {
options.utilSuccess(res.data);
}
}
}
},
3 years ago
fail: function(res) {
uni.hideLoading();
options.utilFail(res);
},
4 years ago
complete: function(res) {
if (!options.customLoading) {
uni.hideNavigationBarLoading();
3 years ago
4 years ago
} else {
// 当前页面请求数量-1
if (options.bindThis) {
//options.bindThis.setData({
3 years ago
//loadingCount: options.bindThis.data.loadingCount - 1
4 years ago
//});
}
}
}
});
}; // 提交formID封装
const uploadFormID = formID => {
var timestamp = Math.floor(new Date().getTime() / 1000);
request({
api: 'WeChat/SaveFormID',
method: 'POST',
data: {
FormIDs: formID
},
utilSuccess: function(res) {
console.log('上传formID成功');
},
utilFail: function(err) {
console.log('上传formID失败');
}
});
}; // 支付
const payOrder = (orderID, orderType, success, fail) => {
uni.request({
url: HOST + 'WxPay/ToPay?openId=' + uni.getStorageSync('userInfo').WeChatOpenID + '&orderId=' +
orderID + '&OrderType=' + orderType,
method: 'GET',
header: {
'content-type': 'application/json'
},
success: function(res) {
// 内容为空, 代表失败
if (res.data == '') {
fail('当前网络状况不好');
return;
} // 去掉转义字符
var pureStr = res.data.replace(/\\/g, '');
res.data = JSON.parse(pureStr);
var index = res.data.package.indexOf('=') + 1;
var prepayId = res.data.package.substring(index); // wxpay
uni.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.package,
signType: res.data.signType,
paySign: res.data.paySign,
success: function(res) {
success(res); // 发送成功通知
request({
api: 'WxPay/PayTemplateMsg',
data: {
prepay_id: prepayId,
orderId: orderID,
isSuccess: 1,
failMsg: '',
xType: orderType
}
});
},
fail: function(res) {
fail(res);
var errMsg = res == 'requestPayment:fail cancel' ? '用户取消支付' :
res; // 发送失败通知
request({
api: 'WxPay/PayTemplateMsg',
data: {
prepay_id: prepayId,
orderId: orderID,
isSuccess: 0,
failMsg: errMsg,
xType: orderType
}
});
}
});
},
fail: function(err) {
fail('当前网络状况不好');
}
});
}; // 判断是否已登录同时有open-id和PhoneNum, 则认为已登录)
const getRegions = () => {
request({
3 years ago
api: 'api/get-regions',
customLoading: true,
4 years ago
utilSuccess: function(res) {
var r = res[0];
r.latitude = 31.53908730560652; //res.latitude;31.536744,120.40841
r.longitude = 120.41435442003421; // res.longitude;
3 years ago
uni.setStorageSync("region", r);
4 years ago
},
utilFail: function(res) {
console.log(res)
}
});
}
const scanPay = (orderID, amt, scan, type, success, fail) => {
var access_token = uni.getStorageSync('userInfo').access_token;
console.log("1.支付进入")
var data = {
token: access_token,
order_id: orderID,
money: amt,
auth_code: scan,
type: type
};
console.log("2.参数")
console.log(data)
uni.showLoading({
title: '正在加载',
});
uni.request({
url: HOST + 'manager/scan-pay/' + orderID,
method: 'POST',
data: {
token: access_token,
order_id: orderID,
money: amt,
auth_code: scan,
type: type
},
header: {
'content-type': 'application/json'
},
success: function(res) {
uni.hideLoading();
if (isNull(res.data.errorcode)) {
success(res);
console.log("3.支付成功")
console.log(res)
} else {
alert("支付失败:" + res.data.errormsg);
console.log("4.支付失败")
console.log(res)
}
},
fail: function(err) {
fail('当前网络状况不好');
console.log("4.支付失败")
console.log(res)
}
});
}; // 判断是否已登录同时有open-id和PhoneNum, 则认为已登录)
const isLogin = () => {
var userInfo = uni.getStorageSync('userInfo');
return !isNull(userInfo.token);
}; // 判断是否为空
const getfile = (id, success, fail) => {
var access_token = uni.getStorageSync('userInfo').access_token;
var url = HOST + 'manager/get-attachment/' + id + '?token=' + access_token;
success(url);
// uni.request({
// url: HOST + 'manager/get-attachment/' + id + '?token=' + access_token,
// method: 'GET',
// header: {
// 'content-type': 'application/json'
// },
// success: function(res) {
// success(res); // 发送成功通知
// },
// fail: function(res) {
// fail(res);
// }
// });
};
3 years ago
4 years ago
const openid_info_key = "openid_info"
3 years ago
const user_info_key = 'user_info_key'
const solgn = "无锡新吴欢迎您";
const shareInfo = (type) => {
return {
title: solgn
}
}
const getOpenidInfo = (cb, refresh) => {
cb = cb || function() {}
refresh = refresh || false
if (!refresh) {
let user_info = uni.getStorageSync(user_info_key)
if (user_info && user_info.openid && user_info.session_key) {
cb(user_info)
return
4 years ago
}
3 years ago
}
uni.login({
provider: 'weixin',
success: (res) => {
uni.request({
url: HOST + 'api/member/login-by-code',
method: 'POST',
data: {
code: res.code
},
success: result => {
const user_info1 = result.data.data.user_info
user_info1.openid = user_info1.wechat_openid
2 years ago
uni.setStorageSync(user_info_key, user_info1)
3 years ago
cb(user_info1)
}
});
}
});
}
const getUserProfile = (cb) => {
cb = cb || function() {}
wx.getUserProfile({
desc: '用于完善会员资料',
success: (res) => {
uni.setStorageSync('user_profile', res.userInfo)
cb(res.userInfo)
}
})
}
const getUserInfoCache = () => {
return uni.getStorageSync(user_info_key)
}
4 years ago
//获取订单详情
const getOrderInfo = (id, success, fail) => {
var access_token = uni.getStorageSync('userInfo').access_token;
var url = HOST + 'manager/get-order/' + id + '?token=' + access_token;
uni.request({
url: url,
method: 'GET',
header: {
'content-type': 'application/json'
},
success: function(res) {
res.data.factorjson = JSON.parse(res.data.factors);
success(res); // 发送成功通知
},
fail: function(res) {
fail(res);
}
});
};
//获取订单详情
const getOrderItemInfo = (id, success, fail) => {
var access_token = uni.getStorageSync('userInfo').access_token;
var url = HOST + 'manager/get-order-item/' + id + '?token=' + access_token;
uni.request({
url: url,
method: 'GET',
header: {
'content-type': 'application/json'
},
success: function(res) {
res.data.factorjson = JSON.parse(res.data.factors);
success(res.data); // 发送成功通知
},
fail: function(res) {
fail(res);
}
});
};
const isNull = p => {
return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null';
}; // 正则
const phoneRegex = /^1\d{10}$/;
const idCardRegex = /^\d{17}([0-9]|X)$/; // 去掉特殊字符
const filterSpecialChars = str => {
return str.replace(/[^\u4e00-\u9fa5a-zA-Z0-9\w]/g, '');
}; // 生成随机字符串
const randomStr = length => {
var ret = '';
var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z'
];
for (var i = 0; i < length; i++) {
var randomIndex = Math.round(Math.random() * (arr.length - 1));
ret += arr[randomIndex];
}
return ret;
}; // proType对应的中文描述
const proTypeDic = {}; // 排序字段
const orderByArr = [{
id: '1',
name: '近期销量最高'
}, // { id: '2', name: '产品评价最高' },
{
id: '3',
name: '价格由高到低'
}, {
id: '4',
name: '价格由低到高'
}
]; // 日期天数
const travelDaysArr = [{
id: '1',
name: '2日',
minDay: 2,
maxDay: 2
}, {
id: '2',
name: '3日',
minDay: 3,
maxDay: 3
}, {
id: '3',
name: '4日',
minDay: 4,
maxDay: 4
}, {
id: '4',
name: '5日',
minDay: 5,
maxDay: 5
}, {
id: '5',
name: '6日',
minDay: 6,
maxDay: 6
}, {
id: '6',
name: '7日',
minDay: 7,
maxDay: 7
}, {
id: '7',
name: '8日',
minDay: 8,
maxDay: 8
}, {
id: '8',
name: '9日',
minDay: 9,
maxDay: 9
}, {
id: '9',
name: '10日',
minDay: 10,
maxDay: 10
}, {
id: '10',
name: '10日以上',
minDay: 11,
maxDay: 0
}]; // 中文星期
const weekdayArr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; // 是否合法的身份证
const isValidCardID = cardID => {
// Step1: 先判断格式
if (!idCardRegex.test(cardID)) return false; // Step2: 校验最后一位
var code = cardID.split(''); //加权因子
var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //校验位
var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
var sum = 0;
var ai = 0;
var wi = 0;
for (var i = 0; i < 17; i++) {
ai = code[i];
wi = factor[i];
sum += ai * wi;
}
var last = parity[sum % 11];
return parity[sum % 11] == code[17];
};
/**
* UTF16和UTF8转换对照表
* U+00000000 U+0000007F 0xxxxxxx
* U+00000080 U+000007FF 110xxxxx 10xxxxxx
* U+00000800 U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
* U+00010000 U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
* U+00200000 U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
* U+04000000 U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
var Base64 = {
// 转码表
table: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U',
'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '+',
'/'
],
UTF16ToUTF8: function(str) {
var res = [],
len = str.length;
for (var i = 0; i < len; i++) {
var code = str.charCodeAt(i);
if (code > 0x0000 && code <= 0x007F) {
// 单字节这里并不考虑0x0000因为它是空字节
// U+00000000 U+0000007F 0xxxxxxx
res.push(str.charAt(i));
} else if (code >= 0x0080 && code <= 0x07FF) {
// 双字节
// U+00000080 U+000007FF 110xxxxx 10xxxxxx
// 110xxxxx
var byte1 = 0xC0 | code >> 6 & 0x1F; // 10xxxxxx
var byte2 = 0x80 | code & 0x3F;
res.push(String.fromCharCode(byte1), String.fromCharCode(byte2));
} else if (code >= 0x0800 && code <= 0xFFFF) {
// 三字节
// U+00000800 U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
// 1110xxxx
var byte1 = 0xE0 | code >> 12 & 0x0F; // 10xxxxxx
var byte2 = 0x80 | code >> 6 & 0x3F; // 10xxxxxx
var byte3 = 0x80 | code & 0x3F;
res.push(String.fromCharCode(byte1), String.fromCharCode(byte2), String.fromCharCode(
byte3));
} else if (code >= 0x00010000 && code <= 0x001FFFFF) { // 四字节
// U+00010000 U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
} else if (code >= 0x00200000 && code <= 0x03FFFFFF) { // 五字节
// U+00200000 U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
} else
/** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/
{ // 六字节
// U+04000000 U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
}
}
return res.join('');
},
UTF8ToUTF16: function(str) {
var res = [],
len = str.length;
var i = 0;
for (var i = 0; i < len; i++) {
var code = str.charCodeAt(i); // 对第一个字节进行判断
if ((code >> 7 & 0xFF) == 0x0) {
// 单字节
// 0xxxxxxx
res.push(str.charAt(i));
} else if ((code >> 5 & 0xFF) == 0x6) {
// 双字节
// 110xxxxx 10xxxxxx
var code2 = str.charCodeAt(++i);
var byte1 = (code & 0x1F) << 6;
var byte2 = code2 & 0x3F;
var utf16 = byte1 | byte2;
res.push(String.fromCharCode(utf16));
} else if ((code >> 4 & 0xFF) == 0xE) {
// 三字节
// 1110xxxx 10xxxxxx 10xxxxxx
var code2 = str.charCodeAt(++i);
var code3 = str.charCodeAt(++i);
var byte1 = code << 4 | code2 >> 2 & 0x0F;
var byte2 = (code2 & 0x03) << 6 | code3 & 0x3F;
utf16 = (byte1 & 0x00FF) << 8 | byte2;
res.push(String.fromCharCode(utf16));
} else if ((code >> 3 & 0xFF) == 0x1E) { // 四字节
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
} else if ((code >> 2 & 0xFF) == 0x3E) { // 五字节
// 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
} else
/** if (((code >> 1) & 0xFF) == 0x7E)*/
{ // 六字节
// 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
}
}
return res.join('');
},
encode: function(str) {
if (!str) {
return '';
}
var utf8 = this.UTF16ToUTF8(str); // 转成UTF8
var i = 0; // 遍历索引
var len = utf8.length;
var res = [];
while (i < len) {
var c1 = utf8.charCodeAt(i++) & 0xFF;
res.push(this.table[c1 >> 2]); // 需要补2个=
if (i == len) {
res.push(this.table[(c1 & 0x3) << 4]);
res.push('==');
break;
}
var c2 = utf8.charCodeAt(i++); // 需要补1个=
if (i == len) {
res.push(this.table[(c1 & 0x3) << 4 | c2 >> 4 & 0x0F]);
res.push(this.table[(c2 & 0x0F) << 2]);
res.push('=');
break;
}
var c3 = utf8.charCodeAt(i++);
res.push(this.table[(c1 & 0x3) << 4 | c2 >> 4 & 0x0F]);
res.push(this.table[(c2 & 0x0F) << 2 | (c3 & 0xC0) >> 6]);
res.push(this.table[c3 & 0x3F]);
}
return res.join('');
},
decode: function(str) {
if (!str) {
return '';
}
var len = str.length;
var i = 0;
var res = [];
while (i < len) {
var code1 = this.table.indexOf(str.charAt(i++));
var code2 = this.table.indexOf(str.charAt(i++));
var code3 = this.table.indexOf(str.charAt(i++));
var code4 = this.table.indexOf(str.charAt(i++));
var c1 = code1 << 2 | code2 >> 4;
var c2 = (code2 & 0xF) << 4 | code3 >> 2;
var c3 = (code3 & 0x3) << 6 | code4;
res.push(String.fromCharCode(c1));
if (code3 != 64) {
res.push(String.fromCharCode(c2));
}
if (code4 != 64) {
res.push(String.fromCharCode(c3));
}
}
return this.UTF8ToUTF16(res.join(''));
3 years ago
},
4 years ago
};
module.exports = {
HOST: HOST,
formatTime: formatTime,
formatTimeAll: formatTimeAll,
formatNumber: formatNumber,
alert: alert,
getfile: getfile,
request: request,
uploadFormID: uploadFormID,
payOrder: payOrder,
scanPay: scanPay,
isLogin: isLogin,
isNull: isNull,
phoneRegex: phoneRegex,
idCardRegex: idCardRegex,
filterSpecialChars: filterSpecialChars,
randomStr: randomStr,
proTypeDic: proTypeDic,
orderByArr: orderByArr,
travelDaysArr: travelDaysArr,
weekdayArr: weekdayArr,
Base64: Base64,
orderStatusDic: orderStatusDic,
auditStatusDic: auditStatusDic,
getOrderInfo: getOrderInfo,
getOrderItemInfo: getOrderItemInfo,
stateArr: stateArr,
3 years ago
getRegions: getRegions,
getOpenidInfo: getOpenidInfo,
getUserProfile: getUserProfile,
getUserInfoCache: getUserInfoCache,
timeFormat: timeFormat,
shareInfo: shareInfo
4 years ago
};