585 lines
13 KiB
JavaScript
585 lines
13 KiB
JavaScript
import dayjs from 'dayjs';
|
|
var api = require('../config/api.js');
|
|
var util = require('../utils/util.js');
|
|
|
|
const formatTime = (date, template) => dayjs(date).format(template);
|
|
|
|
const formatNumber = n => {
|
|
n = n.toString()
|
|
return n[1] ? n : '0' + n
|
|
}
|
|
|
|
function formatTimeNum(number, format) {
|
|
|
|
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
|
|
var returnArr = [];
|
|
|
|
var date = new Date(number * 1000);
|
|
returnArr.push(date.getFullYear());
|
|
returnArr.push(formatNumber(date.getMonth() + 1));
|
|
returnArr.push(formatNumber(date.getDate()));
|
|
|
|
returnArr.push(formatNumber(date.getHours()));
|
|
returnArr.push(formatNumber(date.getMinutes()));
|
|
returnArr.push(formatNumber(date.getSeconds()));
|
|
|
|
for (var i in returnArr) {
|
|
format = format.replace(formateArr[i], returnArr[i]);
|
|
}
|
|
return format;
|
|
}
|
|
|
|
function testMobile(num) {
|
|
console.log
|
|
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(16[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
|
|
if (num.length == 0) {
|
|
wx.showToast({
|
|
title: '手机号为空',
|
|
image: '/images/icon/icon_error.png',
|
|
})
|
|
return false;
|
|
} else if (num.length < 11) {
|
|
wx.showToast({
|
|
title: '手机号长度有误!',
|
|
image: '/images/icon/icon_error.png',
|
|
})
|
|
return false;
|
|
} else if (!myreg.test(num)) {
|
|
wx.showToast({
|
|
title: '手机号有误!',
|
|
image: '/images/icon/icon_error.png',
|
|
})
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 封封微信的的request
|
|
*/
|
|
function request(url, data = {}, method = "GET") {
|
|
return new Promise(function (resolve, reject) {
|
|
wx.request({
|
|
url: url,
|
|
data: data,
|
|
method: method,
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
// 'XDEBUG_TRIGGER': 'true',
|
|
'token': wx.getStorageSync('token')
|
|
},
|
|
success: function (res) {
|
|
if (res.statusCode == 200) {
|
|
|
|
if (res.data.errno == 401) {
|
|
//需要登录后才可以操作
|
|
|
|
// let code = null;
|
|
// return login().then((res) => {
|
|
// code = res.code;
|
|
// return getUserInfo();
|
|
// }).then((userInfo) => {
|
|
// //登录远程服务器
|
|
// request(api.AuthLoginByWeixin, {
|
|
// code: code,
|
|
// userInfo: userInfo
|
|
// }, 'POST').then(res => {
|
|
// if (res.errno === 0) {
|
|
// //存储用户信息
|
|
// wx.setStorageSync('userInfo', res.data.userInfo);
|
|
// wx.setStorageSync('token', res.data.token);
|
|
// resolve(res);
|
|
// } else {
|
|
// reject(res);
|
|
// }
|
|
// }).catch((err) => {
|
|
// reject(err);
|
|
// });
|
|
// }).catch((err) => {
|
|
// reject(err);
|
|
// })
|
|
} else {
|
|
resolve(res.data);
|
|
}
|
|
} else {
|
|
reject(res.errMsg);
|
|
}
|
|
|
|
},
|
|
fail: function (err) {
|
|
reject(err)
|
|
}
|
|
})
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 检查微信会话是否过期
|
|
*/
|
|
function checkSession() {
|
|
return new Promise(function (resolve, reject) {
|
|
wx.checkSession({
|
|
success: function () {
|
|
resolve(true);
|
|
},
|
|
fail: function () {
|
|
reject(false);
|
|
}
|
|
})
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 调用微信登录
|
|
*/
|
|
function login() {
|
|
return new Promise(function (resolve, reject) {
|
|
wx.login({
|
|
success: function (res) {
|
|
if (res.code) {
|
|
//登录远程服务器
|
|
resolve(res);
|
|
} else {
|
|
reject(res);
|
|
}
|
|
},
|
|
fail: function (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function getUserInfo() {
|
|
return new Promise(function (resolve, reject) {
|
|
wx.getUserInfo({
|
|
withCredentials: true,
|
|
success: function (res) {
|
|
resolve(res);
|
|
},
|
|
fail: function (err) {
|
|
reject(err);
|
|
}
|
|
})
|
|
});
|
|
}
|
|
|
|
function redirect(url) {
|
|
|
|
//判断页面是否需要登录
|
|
if (false) {
|
|
wx.redirectTo({
|
|
url: '/pages/auth/login/login'
|
|
});
|
|
return false;
|
|
} else {
|
|
wx.redirectTo({
|
|
url: url
|
|
});
|
|
}
|
|
}
|
|
|
|
function showErrorToast(msg) {
|
|
wx.showToast({
|
|
title: msg,
|
|
icon: 'none',
|
|
})
|
|
}
|
|
|
|
function showSuccessToast(msg) {
|
|
wx.showToast({
|
|
title: msg,
|
|
icon: 'success',
|
|
})
|
|
}
|
|
|
|
function sentRes(url, data, method, fn) {
|
|
data = data || null;
|
|
if (data == null) {
|
|
var content = require('querystring').stringify(data);
|
|
} else {
|
|
var content = JSON.stringify(data); //json format
|
|
}
|
|
|
|
var parse_u = require('url').parse(url, true);
|
|
var isHttp = parse_u.protocol == 'http:';
|
|
var options = {
|
|
host: parse_u.hostname,
|
|
port: parse_u.port || (isHttp ? 80 : 443),
|
|
path: parse_u.path,
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Content-Length': Buffer.byteLength(content, "utf8"),
|
|
'Trackingmore-Api-Key': '1b70c67e-d191-4301-9c05-a50436a2526d'
|
|
}
|
|
};
|
|
var req = require(isHttp ? 'http' : 'https').request(options, function (res) {
|
|
var _data = '';
|
|
res.on('data', function (chunk) {
|
|
_data += chunk;
|
|
});
|
|
res.on('end', function () {
|
|
fn != undefined && fn(_data);
|
|
});
|
|
});
|
|
req.write(content);
|
|
req.end();
|
|
}
|
|
|
|
function loginNow() {
|
|
let userInfo = wx.getStorageSync('userInfo');
|
|
if (userInfo == '') {
|
|
wx.login({
|
|
success: (res) => {
|
|
request(api.AuthLoginByWeixin, {
|
|
code: res.code
|
|
}, 'POST').then(function (res) {
|
|
if (res.code === 1) {
|
|
let userInfo = res.data.userInfo;
|
|
wx.setStorageSync('token', res.data.token);
|
|
wx.setStorageSync('userInfo', userInfo);
|
|
}
|
|
});
|
|
},
|
|
});
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function getTextLength(str, full) {
|
|
let len = 0;
|
|
for (let i = 0; i < str.length; i++) {
|
|
let c = str.charCodeAt(i);
|
|
//单字节加1
|
|
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
|
|
len++;
|
|
} else {
|
|
len += (full ? 2 : 1);
|
|
}
|
|
}
|
|
return len;
|
|
}
|
|
|
|
/**
|
|
* rgba(255, 255, 255, 1) => #ffffff
|
|
* @param {String} color
|
|
*/
|
|
function transferColor(color = '') {
|
|
let res = '#';
|
|
color = color.replace(/^rgba?\(/, '').replace(/\)$/, '');
|
|
color = color.split(', ');
|
|
|
|
color.length > 3 ? color.length = 3 : '';
|
|
for (let item of color) {
|
|
item = parseInt(item || 0);
|
|
if (item < 10) {
|
|
res += ('0' + item)
|
|
} else {
|
|
res += (item.toString(16))
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
function transferBorder(border = '') {
|
|
let res = border.match(/(\w+)px\s(\w+)\s(.*)/);
|
|
let obj = {};
|
|
|
|
if (res) {
|
|
obj = {
|
|
width: +res[1],
|
|
style: res[2],
|
|
color: res[3]
|
|
}
|
|
}
|
|
|
|
return res ? obj : null;
|
|
}
|
|
|
|
|
|
/**
|
|
* 内边距,依次为上右下左
|
|
* @param {*} padding
|
|
*/
|
|
function transferPadding(padding = '0 0 0 0') {
|
|
padding = padding.split(' ');
|
|
for (let i = 0, len = padding.length; i < len; i++) {
|
|
padding[i] = +padding[i].replace('px', '');
|
|
}
|
|
|
|
return padding;
|
|
}
|
|
/**
|
|
* type1: 0, 25, 17, rgba(0, 0, 0, 0.3)
|
|
* type2: rgba(0, 0, 0, 0.3) 0px 25px 17px 0px => (0, 25, 17, rgba(0, 0, 0, 0.3))
|
|
* @param {*} shadow
|
|
*/
|
|
function transferBoxShadow(shadow = '', type) {
|
|
if (!shadow || shadow === 'none') return;
|
|
let color;
|
|
let split;
|
|
|
|
split = shadow.match(/(\w+)\s(\w+)\s(\w+)\s(rgb.*)/);
|
|
|
|
if (split) {
|
|
split.shift();
|
|
shadow = split;
|
|
color = split[3] || '#ffffff';
|
|
} else {
|
|
split = shadow.split(') ');
|
|
color = split[0] + ')'
|
|
shadow = split[1].split('px ');
|
|
}
|
|
|
|
return {
|
|
offsetX: +shadow[0] || 0,
|
|
offsetY: +shadow[1] || 0,
|
|
blur: +shadow[2] || 0,
|
|
color
|
|
}
|
|
}
|
|
|
|
function getUid(prefix) {
|
|
prefix = prefix || '';
|
|
|
|
return (
|
|
prefix +
|
|
'xxyxxyxx'.replace(/[xy]/g, c => {
|
|
let r = (Math.random() * 16) | 0;
|
|
let v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
return v.toString(16);
|
|
})
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 格式化价格数额为字符串
|
|
* 可对小数部分进行填充,默认不填充
|
|
* @param price 价格数额,以分为单位!
|
|
* @param fill 是否填充小数部分 0-不填充 1-填充第一位小数 2-填充两位小数
|
|
*/
|
|
function priceFormat(price, fill = 0) {
|
|
if (isNaN(price) || price === null || price === Infinity) {
|
|
return price;
|
|
}
|
|
|
|
let priceFormatValue = Math.round(parseFloat(`${price}`) * 10 ** 8) / 10 ** 8; // 恢复精度丢失
|
|
priceFormatValue = `${Math.ceil(priceFormatValue) / 100}`; // 向上取整,单位转换为元,转换为字符串
|
|
if (fill > 0) {
|
|
// 补充小数位数
|
|
if (priceFormatValue.indexOf('.') === -1) {
|
|
priceFormatValue = `${priceFormatValue}.`;
|
|
}
|
|
const n = fill - priceFormatValue.split('.')[1]?.length;
|
|
for (let i = 0; i < n; i++) {
|
|
priceFormatValue = `${priceFormatValue}0`;
|
|
}
|
|
}
|
|
return priceFormatValue;
|
|
}
|
|
|
|
/**
|
|
* 获取cdn裁剪后链接
|
|
*
|
|
* @param {string} url 基础链接
|
|
* @param {number} width 宽度,单位px
|
|
* @param {number} [height] 可选,高度,不填时与width同值
|
|
*/
|
|
const cosThumb = (url, width, height = width) => {
|
|
if (url.indexOf('?') > -1) {
|
|
return url;
|
|
}
|
|
|
|
if (url.indexOf('http://') === 0) {
|
|
url = url.replace('http://', 'https://');
|
|
}
|
|
|
|
return `${url}?imageMogr2/thumbnail/${~~width}x${~~height}`;
|
|
};
|
|
|
|
const get = (source, paths, defaultValue) => {
|
|
if (typeof paths === 'string') {
|
|
paths = paths
|
|
.replace(/\[/g, '.')
|
|
.replace(/\]/g, '')
|
|
.split('.')
|
|
.filter(Boolean);
|
|
}
|
|
const {
|
|
length
|
|
} = paths;
|
|
let index = 0;
|
|
while (source != null && index < length) {
|
|
source = source[paths[index++]];
|
|
}
|
|
return source === undefined || index === 0 ? defaultValue : source;
|
|
};
|
|
let systemWidth = 0;
|
|
/** 获取系统宽度,为了减少启动消耗所以在函数里边做初始化 */
|
|
export const loadSystemWidth = () => {
|
|
if (systemWidth) {
|
|
return systemWidth;
|
|
}
|
|
|
|
try {
|
|
({
|
|
screenWidth: systemWidth,
|
|
pixelRatio
|
|
} = wx.getSystemInfoSync());
|
|
} catch (e) {
|
|
systemWidth = 0;
|
|
}
|
|
return systemWidth;
|
|
};
|
|
|
|
/**
|
|
* 转换rpx为px
|
|
*
|
|
* @description
|
|
* 什么时候用?
|
|
* - 布局(width: 172rpx)已经写好, 某些组件只接受px作为style或者prop指定
|
|
*
|
|
*/
|
|
const rpx2px = (rpx, round = false) => {
|
|
loadSystemWidth();
|
|
|
|
// px / systemWidth = rpx / 750
|
|
const result = (rpx * systemWidth) / 750;
|
|
|
|
if (round) {
|
|
return Math.floor(result);
|
|
}
|
|
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* 手机号码*加密函数
|
|
* @param {string} phone 电话号
|
|
* @returns
|
|
*/
|
|
const phoneEncryption = (phone) => {
|
|
return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
|
|
};
|
|
|
|
// 内置手机号正则字符串
|
|
const innerPhoneReg =
|
|
'^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$';
|
|
|
|
/**
|
|
* 手机号正则校验
|
|
* @param phone 手机号
|
|
* @param phoneReg 正则字符串
|
|
* @returns true - 校验通过 false - 校验失败
|
|
*/
|
|
const phoneRegCheck = (phone) => {
|
|
const phoneRegExp = new RegExp(innerPhoneReg);
|
|
return phoneRegExp.test(phone);
|
|
};
|
|
|
|
const obj2Params = (obj = {}, encode = false) => {
|
|
const result = [];
|
|
Object.keys(obj).forEach((key) =>
|
|
result.push(`${key}=${encode ? encodeURIComponent(obj[key]) : obj[key]}`),
|
|
);
|
|
|
|
return result.join('&');
|
|
};
|
|
|
|
/**
|
|
* 标记某条需求需要在列表页刷新状态
|
|
* 用 globalData 传递,不依赖页面栈深度(backAndRefresh 的 delta 在多层跳转后容易算错上一页)
|
|
* @param {number|string} demandId 需求ID
|
|
*/
|
|
function markDemandRefresh(demandId) {
|
|
const app = getApp();
|
|
if (app && app.globalData) {
|
|
app.globalData.refreshDemandId = Number(demandId) || 0;
|
|
}
|
|
}
|
|
|
|
function backAndRefresh(delta = 1, freshField = 'freshPage') {
|
|
const pages = getCurrentPages(); // 获取页面栈
|
|
const prevPage = pages[pages.length - (delta + 1)]; // 上一页实例
|
|
if (prevPage) {
|
|
prevPage.setData({ [freshField]: true });
|
|
}
|
|
wx.navigateBack({ delta }); // 返回上一页
|
|
}
|
|
|
|
function checkSubscriptionsSetting() {
|
|
wx.getSetting({
|
|
withSubscriptions: true,
|
|
success(res) {
|
|
console.log('getSetting:', res);
|
|
const itemSettings = res.subscriptionsSetting.itemSettings || {};
|
|
let hasReject = false;
|
|
if (res.subscriptionsSetting.mainSwitch === false) {
|
|
hasReject = true;
|
|
}
|
|
for (const id in itemSettings) {
|
|
if (itemSettings[id] === 'reject') {
|
|
hasReject = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (hasReject) {
|
|
wx.showToast({
|
|
title: '请授权订阅消息,用于接收重要消息',
|
|
icon: 'none',
|
|
duration: 3000
|
|
});
|
|
wx.openSetting({
|
|
withSubscriptions: true,
|
|
success(openRes) {
|
|
console.log('openSetting:', openRes);
|
|
},
|
|
fail(err) {
|
|
console.error('打开设置页失败:', err);
|
|
}
|
|
});
|
|
} else {
|
|
console.log('所有消息均未被拒绝');
|
|
}
|
|
},
|
|
fail(err) {
|
|
console.error('获取设置失败:', err);
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
formatTime,
|
|
priceFormat,
|
|
cosThumb,
|
|
get,
|
|
rpx2px,
|
|
phoneEncryption,
|
|
phoneRegCheck,
|
|
formatTimeNum: formatTimeNum,
|
|
request,
|
|
redirect,
|
|
showErrorToast,
|
|
showSuccessToast,
|
|
checkSession,
|
|
login,
|
|
getUserInfo,
|
|
testMobile,
|
|
sentRes,
|
|
loginNow,
|
|
getTextLength,
|
|
transferBorder,
|
|
transferColor,
|
|
transferPadding,
|
|
transferBoxShadow,
|
|
obj2Params,
|
|
backAndRefresh,
|
|
markDemandRefresh,
|
|
checkSubscriptionsSetting,
|
|
getUid
|
|
}; |