import smCrypt from "../miniprogram_npm/sm-crypto/index.js" import { configObj } from "../config.js" const sm2 = smCrypt.sm2 let privateKey = configObj.privateKey let publicKey = configObj.publicKey // 日期函数 export function dateFtt(fmt, date) { //author: meizz var o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "h+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S+": date.getMilliseconds(), //毫秒 } if (/(y+)/.test(fmt)) fmt = fmt.replace( RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length) ) for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace( RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length) ) return fmt } // 圈存存入当前时间 export function formatDate(now) { var year = now.getFullYear() var month = now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1 var date = now.getDate() < 10 ? "0" + now.getDate() : now.getDate() var hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours() var minute = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes() var second = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds() return year + "" + month + "" + date + "" + hour + "" + minute + "" + second } // 脱敏函数 export function desensitization(str, beginLen, endLen) { var len = str.length var firstStr = str.substr(0, beginLen) var lastStr = str.substr(endLen) var middleStr = str .substring(beginLen, len - Math.abs(endLen)) .replace(/[\s\S]/gi, "*") let tempStr = firstStr + middleStr + lastStr return tempStr } // 缓存函数 export function getStore(type) { try { return wx.getStorageSync(type) } catch (e) {} } // 缓存函数 export function setStore(type, param) { if (param) { try { if (typeof param === "object") { wx.setStorageSync(type, JSON.stringify(param)) } else { wx.setStorageSync(type, param) } } catch (e) {} } } // 清除缓存函数 export function removeStore(key) { try { wx.removeStorageSync(key) } catch (e) {} } // 清除所有缓存 export function clearAllStore() { let ary = [ "wxOpenId", "token", "accessToken", "openId", "vehicleId", "accountId", "signChannelId", "sign", "handleType", "plateNum", "plateColor", "productObj", "redirectUrl", "etcProductId", "plateNumAndColor", "afterType", "orderType", "orderNo", "mobile", "afterOrderType", "orderStatus", "appOrderNo", "accountType", "carType", "wechatSignNo", "orderDetail", "selectAddress", "persionAccountId" ] for (let key in ary) { removeStore(ary[key]) } } // 生成签名 export function getSm2Sign(params) { let string = sortAscii(params) let sign = sm2.doSignature(string, privateKey, { hash: true, der: true }) return sign } // 验签 export function sm2Verify(params) { let string = sortAscii(params) let verifyResult = sm2.doVerifySignature(string, params.sign, publicKey, { hash: true, der: true, }) return verifyResult } // Ascii排序 function sortAscii(params) { let string = "" const ascll = [] for (let key in params) { ascll.push({ key, code: key.charCodeAt() }) } const sortAscll = ascll.sort(function (a, b) { return a.code - b.code }) for (const obj of sortAscll) { if (obj.key !== "sign" && obj.key !== "signType") { string = string ? string + "&" + (obj.key + "=" + params[obj.key]) : obj.key + "=" + params[obj.key] } } return string } // 校验日期格式 export function checkDate(date) { if (date.length < 10) { return false } var result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/) if (result == null) return false var d = new Date(result[1], result[3] - 1, result[4]) return ( d.getFullYear() == result[1] && d.getMonth() + 1 == result[3] && d.getDate() == result[4] ) } // 车牌校验 export function isLicensePlate(str) { return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([A-Z]([A-HJ-NP-Z0-9])[A-HJ-NP-Z0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领试][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领试]))$/.test( str ) } // 手机号码校验 export function checkPhone(phone) { if (!/^1[3456789]\d{9}$/.test(phone) || !phone) { return false } else { return true } }