您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

request.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * version 1.1.7
  3. */
  4. import {
  5. cryptUtil,
  6. encrypt
  7. } from "./aesutils.js"
  8. import {
  9. dateFtt,
  10. getSm2Sign,
  11. getStore,
  12. sm2Verify,
  13. setStore
  14. } from "./index.js"
  15. import RequestManager from "./requestManager.js"
  16. import {
  17. configObj
  18. } from "../config.js"
  19. import Api from "../api/index.js"
  20. // 请求锁
  21. const manager = new RequestManager()
  22. const requestParams = {
  23. encryptType: "SM4",
  24. version: "1.0",
  25. signType: "SM2",
  26. }
  27. class Request {
  28. constructor(config = {}) {
  29. this.config = {}
  30. }
  31. post(url, config = {}) {
  32. let requestId = manager.generateId(config.filename, url, config.data)
  33. let params = getParams(config, requestId)
  34. return this._request("post", url, params, requestId)
  35. }
  36. setConfig(config = {}) {
  37. this.config = config
  38. }
  39. getConfig() {
  40. return this.config
  41. }
  42. _request(method, url, params, requestId) {
  43. return new Promise((resolve, reject) => {
  44. if (!requestId) {
  45. console.log("pss重复请求 " + params.filename)
  46. reject()
  47. return
  48. }
  49. wx.request({
  50. url: configObj.reqUrl,
  51. method,
  52. header: {
  53. token: getStore("token"),
  54. ...this.config.header,
  55. },
  56. data: params ? params : {},
  57. complete(response) {
  58. manager.deleteById(requestId)
  59. console.log("接口返回response", response)
  60. if (response.statusCode === 200) {
  61. getResponse(response, resolve, reject)
  62. } else {
  63. wx.showToast({
  64. title: "网络异常,请确认网络是否正常。",
  65. duration: 2000,
  66. icon: "none",
  67. })
  68. reject()
  69. }
  70. },
  71. })
  72. })
  73. }
  74. }
  75. let request = new Request()
  76. // 处理请求参数
  77. function getParams(config) {
  78. let params = config.data || {}
  79. var nowDate = dateFtt("yyyyMMddhhmmssSSS", new Date())
  80. let timestamp = dateFtt("yyyy-MM-dd hh:mm:ss:SSS", new Date())
  81. if (getStore("accessToken")) {
  82. params.accessToken = getStore("accessToken")
  83. }
  84. if (getStore("openId")) {
  85. params.openId = getStore("openId")
  86. }
  87. let reqParams = {
  88. ...requestParams,
  89. filename: config.filename + "_" + nowDate + ".json",
  90. timestamp,
  91. bizContent: cryptUtil.encrypt(JSON.stringify(params)),
  92. }
  93. console.log("请求参数" + reqParams.filename, params)
  94. if (
  95. getStore("accessToken") &&
  96. !config.filename.includes("APPLET_LOGINCHECK")
  97. ) {
  98. reqParams.token = getStore("accessToken")
  99. }
  100. reqParams.sign = getSm2Sign(reqParams)
  101. config.data = reqParams
  102. return config.data
  103. }
  104. // 处理回参
  105. function getResponse(response, resolve, reject) {
  106. const res = response.data
  107. if (res.statusCode !== 0) {
  108. if (
  109. res.errorMsg &&
  110. res.statusCode !== 4000 &&
  111. res.statusCode !== -10000
  112. ) {
  113. wx.showModal({
  114. title: "提示",
  115. content: res.errorMsg,
  116. showCancel: false,
  117. })
  118. console.log("errorMsg", res.errorMsg)
  119. }
  120. // token过期
  121. if (res.statusCode === 4000) {
  122. repeatLogin()
  123. }
  124. // accessToken过期
  125. if (res.statusCode === -10000) {
  126. if (getStore("handleType") === "continuation") {
  127. wx.reLaunch({
  128. url: "plugin://issuer-plugin/login?repeatLogin=1&plateNum=" +
  129. getStore("plateNumAndColor") +
  130. "&handleType=" +
  131. getStore("handleType") +
  132. "&etcProductId=" +
  133. getStore("etcProductId") +
  134. "&mobile=" +
  135. getStore("wxOpenId") +
  136. "&redirectUrl=" +
  137. getStore("redirectUrl"),
  138. })
  139. } else if (getStore("handleType") === "aftersale") {
  140. wx.reLaunch({
  141. url: "plugin://issuer-plugin/login?repeatLogin=1&plateNum=" +
  142. getStore("plateNumAndColor") +
  143. "&handleType=" +
  144. getStore("handleType") +
  145. "&orderNo=" +
  146. getStore("appOrderNo") +
  147. "&afterType=" +
  148. getStore("afterType") +
  149. "&mobile=" +
  150. getStore("wxOpenId") +
  151. "&redirectUrl=" +
  152. getStore("redirectUrl"),
  153. })
  154. } else if (getStore("wechatSignNo")) {
  155. wx.reLaunch({
  156. url: "plugin://issuer-plugin/login?repeatLogin=1&redirectUrl=" +
  157. getStore("redirectUrl") +
  158. "&mobile=" +
  159. getStore("wxOpenId") +
  160. "&wechatSignNo=" +
  161. getStore("wechatSignNo"),
  162. })
  163. } else {
  164. wx.reLaunch({
  165. url: "plugin://issuer-plugin/login?repeatLogin=1&redirectUrl=" +
  166. getStore("redirectUrl") +
  167. "&accountType=" +
  168. getStore("accountType") +
  169. "&etcProductId=" +
  170. getStore("etcProductId") +
  171. "&mobile=" +
  172. getStore("wxOpenId") +
  173. "&carType=" +
  174. getStore("carType"),
  175. })
  176. }
  177. }
  178. reject(res)
  179. } else if (!sm2Verify(res)) {
  180. wx.showToast({
  181. title: "验签异常",
  182. icon: "error",
  183. duration: 2000,
  184. })
  185. reject(res)
  186. } else {
  187. let decrypting = res.bizContent || ""
  188. if (res.bizContent) {
  189. decrypting = JSON.parse(cryptUtil.decrypt(res.bizContent))
  190. }
  191. console.log("返回数据", decrypting)
  192. resolve(decrypting)
  193. }
  194. }
  195. // 公共请求
  196. export function requestFnc(url, data, cb, errorFunc, catchFunc) {
  197. request
  198. .post(url, data)
  199. .then(
  200. (res) => {
  201. cb(res)
  202. },
  203. (err) => {
  204. if (errorFunc) {
  205. errorFunc(err)
  206. }
  207. }
  208. )
  209. .catch((err) => {
  210. if (catchFunc) {
  211. catchFunc(err)
  212. }
  213. })
  214. }
  215. // 业务员过期自动重登
  216. function repeatLogin() {
  217. let params = {
  218. filename: Api.checkLogin.filename,
  219. data: {
  220. wxOpenId: getStore("wxOpenId"),
  221. sign: encrypt(getStore("wxOpenId"), configObj.aesKey),
  222. plateNumAndColor: "",
  223. priOrderNo: "",
  224. },
  225. }
  226. requestFnc(Api.checkLogin.url, params, (res) => {
  227. setStore("token", res.token)
  228. if (getStore("handleType") === "continuation") {
  229. wx.reLaunch({
  230. url: "plugin://issuer-plugin/login?plateNum=" +
  231. getStore("plateNumAndColor") +
  232. "&handleType=" +
  233. getStore("handleType") +
  234. "&etcProductId=" +
  235. getStore("etcProductId") +
  236. "&mobile=" +
  237. getStore("wxOpenId") +
  238. "&redirectUrl=" +
  239. getStore("redirectUrl"),
  240. })
  241. } else if (getStore("handleType") === "aftersale") {
  242. wx.reLaunch({
  243. url: "plugin://issuer-plugin/login?plateNum=" +
  244. getStore("plateNumAndColor") +
  245. "&handleType=" +
  246. getStore("handleType") +
  247. "&orderNo=" +
  248. getStore("appOrderNo") +
  249. "&afterType=" +
  250. getStore("afterType") +
  251. "&mobile=" +
  252. getStore("wxOpenId") +
  253. "&redirectUrl=" +
  254. getStore("redirectUrl"),
  255. })
  256. } else if (getStore("wechatSignNo")) {
  257. wx.reLaunch({
  258. url: "plugin://issuer-plugin/login?redirectUrl=" +
  259. getStore("redirectUrl") +
  260. "&mobile=" +
  261. getStore("wxOpenId") +
  262. "&wechatSignNo=" +
  263. getStore("wechatSignNo"),
  264. })
  265. } else {
  266. wx.reLaunch({
  267. url: "plugin://issuer-plugin/login?redirectUrl=" +
  268. getStore("redirectUrl") +
  269. "&accountType=" +
  270. getStore("accountType") +
  271. "&etcProductId=" +
  272. getStore("etcProductId") +
  273. "&mobile=" +
  274. getStore("wxOpenId") +
  275. "&carType=" +
  276. getStore("carType"),
  277. })
  278. }
  279. })
  280. }