123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- /**
- * version 1.1.7
- */
- import {
- cryptUtil,
- encrypt
- } from "./aesutils.js"
- import {
- dateFtt,
- getSm2Sign,
- getStore,
- sm2Verify,
- setStore
- } from "./index.js"
- import RequestManager from "./requestManager.js"
- import {
- configObj
- } from "../config.js"
- import Api from "../api/index.js"
-
- // 请求锁
- const manager = new RequestManager()
-
- const requestParams = {
- encryptType: "SM4",
- version: "1.0",
- signType: "SM2",
- }
-
- class Request {
- constructor(config = {}) {
- this.config = {}
- }
- post(url, config = {}) {
- let requestId = manager.generateId(config.filename, url, config.data)
- let params = getParams(config, requestId)
- return this._request("post", url, params, requestId)
- }
- setConfig(config = {}) {
- this.config = config
- }
- getConfig() {
- return this.config
- }
- _request(method, url, params, requestId) {
- return new Promise((resolve, reject) => {
- if (!requestId) {
- console.log("pss重复请求 " + params.filename)
- reject()
- return
- }
- wx.request({
- url: configObj.reqUrl,
- method,
- header: {
- token: getStore("token"),
- ...this.config.header,
- },
- data: params ? params : {},
- complete(response) {
- manager.deleteById(requestId)
- console.log("接口返回response", response)
- if (response.statusCode === 200) {
- getResponse(response, resolve, reject)
- } else {
- wx.showToast({
- title: "网络异常,请确认网络是否正常。",
- duration: 2000,
- icon: "none",
- })
- reject()
- }
- },
- })
- })
- }
- }
- let request = new Request()
-
- // 处理请求参数
- function getParams(config) {
- let params = config.data || {}
- var nowDate = dateFtt("yyyyMMddhhmmssSSS", new Date())
- let timestamp = dateFtt("yyyy-MM-dd hh:mm:ss:SSS", new Date())
- if (getStore("accessToken")) {
- params.accessToken = getStore("accessToken")
- }
- if (getStore("openId")) {
- params.openId = getStore("openId")
- }
- let reqParams = {
- ...requestParams,
- filename: config.filename + "_" + nowDate + ".json",
- timestamp,
- bizContent: cryptUtil.encrypt(JSON.stringify(params)),
- }
- console.log("请求参数" + reqParams.filename, params)
- if (
- getStore("accessToken") &&
- !config.filename.includes("APPLET_LOGINCHECK")
- ) {
- reqParams.token = getStore("accessToken")
- }
- reqParams.sign = getSm2Sign(reqParams)
- config.data = reqParams
- return config.data
- }
-
- // 处理回参
- function getResponse(response, resolve, reject) {
- const res = response.data
- if (res.statusCode !== 0) {
- if (
- res.errorMsg &&
- res.statusCode !== 4000 &&
- res.statusCode !== -10000
- ) {
- wx.showModal({
- title: "提示",
- content: res.errorMsg,
- showCancel: false,
- })
- console.log("errorMsg", res.errorMsg)
- }
- // token过期
- if (res.statusCode === 4000) {
- repeatLogin()
- }
- // accessToken过期
- if (res.statusCode === -10000) {
- if (getStore("handleType") === "continuation") {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?repeatLogin=1&plateNum=" +
- getStore("plateNumAndColor") +
- "&handleType=" +
- getStore("handleType") +
- "&etcProductId=" +
- getStore("etcProductId") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&redirectUrl=" +
- getStore("redirectUrl"),
- })
- } else if (getStore("handleType") === "aftersale") {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?repeatLogin=1&plateNum=" +
- getStore("plateNumAndColor") +
- "&handleType=" +
- getStore("handleType") +
- "&orderNo=" +
- getStore("appOrderNo") +
- "&afterType=" +
- getStore("afterType") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&redirectUrl=" +
- getStore("redirectUrl"),
- })
- } else if (getStore("wechatSignNo")) {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?repeatLogin=1&redirectUrl=" +
- getStore("redirectUrl") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&wechatSignNo=" +
- getStore("wechatSignNo"),
- })
- } else {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?repeatLogin=1&redirectUrl=" +
- getStore("redirectUrl") +
- "&accountType=" +
- getStore("accountType") +
- "&etcProductId=" +
- getStore("etcProductId") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&carType=" +
- getStore("carType"),
- })
- }
- }
- reject(res)
- } else if (!sm2Verify(res)) {
- wx.showToast({
- title: "验签异常",
- icon: "error",
- duration: 2000,
- })
- reject(res)
- } else {
- let decrypting = res.bizContent || ""
- if (res.bizContent) {
- decrypting = JSON.parse(cryptUtil.decrypt(res.bizContent))
- }
- console.log("返回数据", decrypting)
- resolve(decrypting)
- }
- }
-
- // 公共请求
- export function requestFnc(url, data, cb, errorFunc, catchFunc) {
- request
- .post(url, data)
- .then(
- (res) => {
- cb(res)
- },
- (err) => {
- if (errorFunc) {
- errorFunc(err)
- }
- }
- )
- .catch((err) => {
- if (catchFunc) {
- catchFunc(err)
- }
- })
- }
-
- // 业务员过期自动重登
- function repeatLogin() {
- let params = {
- filename: Api.checkLogin.filename,
- data: {
- wxOpenId: getStore("wxOpenId"),
- sign: encrypt(getStore("wxOpenId"), configObj.aesKey),
- plateNumAndColor: "",
- priOrderNo: "",
- },
- }
- requestFnc(Api.checkLogin.url, params, (res) => {
- setStore("token", res.token)
- if (getStore("handleType") === "continuation") {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?plateNum=" +
- getStore("plateNumAndColor") +
- "&handleType=" +
- getStore("handleType") +
- "&etcProductId=" +
- getStore("etcProductId") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&redirectUrl=" +
- getStore("redirectUrl"),
- })
- } else if (getStore("handleType") === "aftersale") {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?plateNum=" +
- getStore("plateNumAndColor") +
- "&handleType=" +
- getStore("handleType") +
- "&orderNo=" +
- getStore("appOrderNo") +
- "&afterType=" +
- getStore("afterType") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&redirectUrl=" +
- getStore("redirectUrl"),
- })
- } else if (getStore("wechatSignNo")) {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?redirectUrl=" +
- getStore("redirectUrl") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&wechatSignNo=" +
- getStore("wechatSignNo"),
- })
- } else {
- wx.reLaunch({
- url: "plugin://issuer-plugin/login?redirectUrl=" +
- getStore("redirectUrl") +
- "&accountType=" +
- getStore("accountType") +
- "&etcProductId=" +
- getStore("etcProductId") +
- "&mobile=" +
- getStore("wxOpenId") +
- "&carType=" +
- getStore("carType"),
- })
- }
- })
- }
|