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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import smCrypt from "../miniprogram_npm/sm-crypto/index.js"
  2. import {
  3. configObj
  4. } from "../config.js"
  5. const sm2 = smCrypt.sm2
  6. let privateKey = configObj.privateKey
  7. let publicKey = configObj.publicKey
  8. // 日期函数
  9. export function dateFtt(fmt, date) {
  10. //author: meizz
  11. var o = {
  12. "M+": date.getMonth() + 1, //月份
  13. "d+": date.getDate(), //日
  14. "h+": date.getHours(), //小时
  15. "m+": date.getMinutes(), //分
  16. "s+": date.getSeconds(), //秒
  17. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  18. "S+": date.getMilliseconds(), //毫秒
  19. }
  20. if (/(y+)/.test(fmt))
  21. fmt = fmt.replace(
  22. RegExp.$1,
  23. (date.getFullYear() + "").substr(4 - RegExp.$1.length)
  24. )
  25. for (var k in o)
  26. if (new RegExp("(" + k + ")").test(fmt))
  27. fmt = fmt.replace(
  28. RegExp.$1,
  29. RegExp.$1.length == 1 ?
  30. o[k] :
  31. ("00" + o[k]).substr(("" + o[k]).length)
  32. )
  33. return fmt
  34. }
  35. // 圈存存入当前时间
  36. export function formatDate(now) {
  37. var year = now.getFullYear()
  38. var month =
  39. now.getMonth() + 1 < 10 ?
  40. "0" + (now.getMonth() + 1) :
  41. now.getMonth() + 1
  42. var date = now.getDate() < 10 ? "0" + now.getDate() : now.getDate()
  43. var hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours()
  44. var minute =
  45. now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes()
  46. var second =
  47. now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds()
  48. return year + "" + month + "" + date + "" + hour + "" + minute + "" + second
  49. }
  50. // 脱敏函数
  51. export function desensitization(str, beginLen, endLen) {
  52. var len = str.length
  53. var firstStr = str.substr(0, beginLen)
  54. var lastStr = str.substr(endLen)
  55. var middleStr = str
  56. .substring(beginLen, len - Math.abs(endLen))
  57. .replace(/[\s\S]/gi, "*")
  58. let tempStr = firstStr + middleStr + lastStr
  59. return tempStr
  60. }
  61. // 缓存函数
  62. export function getStore(type) {
  63. try {
  64. return wx.getStorageSync(type)
  65. } catch (e) {}
  66. }
  67. // 缓存函数
  68. export function setStore(type, param) {
  69. if (param) {
  70. try {
  71. if (typeof param === "object") {
  72. wx.setStorageSync(type, JSON.stringify(param))
  73. } else {
  74. wx.setStorageSync(type, param)
  75. }
  76. } catch (e) {}
  77. }
  78. }
  79. // 清除缓存函数
  80. export function removeStore(key) {
  81. try {
  82. wx.removeStorageSync(key)
  83. } catch (e) {}
  84. }
  85. // 清除所有缓存
  86. export function clearAllStore() {
  87. let ary = [
  88. "wxOpenId",
  89. "token",
  90. "accessToken",
  91. "openId",
  92. "vehicleId",
  93. "accountId",
  94. "signChannelId",
  95. "sign",
  96. "handleType",
  97. "plateNum",
  98. "plateColor",
  99. "productObj",
  100. "redirectUrl",
  101. "etcProductId",
  102. "plateNumAndColor",
  103. "afterType",
  104. "orderType",
  105. "orderNo",
  106. "mobile",
  107. "afterOrderType",
  108. "orderStatus",
  109. "appOrderNo",
  110. "accountType",
  111. "carType",
  112. "wechatSignNo",
  113. "orderDetail",
  114. "selectAddress",
  115. "persionAccountId"
  116. ]
  117. for (let key in ary) {
  118. removeStore(ary[key])
  119. }
  120. }
  121. // 生成签名
  122. export function getSm2Sign(params) {
  123. let string = sortAscii(params)
  124. let sign = sm2.doSignature(string, privateKey, {
  125. hash: true,
  126. der: true
  127. })
  128. return sign
  129. }
  130. // 验签
  131. export function sm2Verify(params) {
  132. let string = sortAscii(params)
  133. let verifyResult = sm2.doVerifySignature(string, params.sign, publicKey, {
  134. hash: true,
  135. der: true,
  136. })
  137. return verifyResult
  138. }
  139. // Ascii排序
  140. function sortAscii(params) {
  141. let string = ""
  142. const ascll = []
  143. for (let key in params) {
  144. ascll.push({
  145. key,
  146. code: key.charCodeAt()
  147. })
  148. }
  149. const sortAscll = ascll.sort(function (a, b) {
  150. return a.code - b.code
  151. })
  152. for (const obj of sortAscll) {
  153. if (obj.key !== "sign" && obj.key !== "signType") {
  154. string = string ?
  155. string + "&" + (obj.key + "=" + params[obj.key]) :
  156. obj.key + "=" + params[obj.key]
  157. }
  158. }
  159. return string
  160. }
  161. // 校验日期格式
  162. export function checkDate(date) {
  163. if (date.length < 10) {
  164. return false
  165. }
  166. var result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/)
  167. if (result == null) return false
  168. var d = new Date(result[1], result[3] - 1, result[4])
  169. return (
  170. d.getFullYear() == result[1] &&
  171. d.getMonth() + 1 == result[3] &&
  172. d.getDate() == result[4]
  173. )
  174. }
  175. // 车牌校验
  176. export function isLicensePlate(str) {
  177. 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(
  178. str
  179. )
  180. }
  181. // 手机号码校验
  182. export function checkPhone(phone) {
  183. if (!/^1[3456789]\d{9}$/.test(phone) || !phone) {
  184. return false
  185. } else {
  186. return true
  187. }
  188. }