123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- const { api } = require('../config/env.js')
- const { DEBUG, NOT_LOGIN } = require('../config/index.js')
- const { isUndef, getStorageSync, getLoginUserInfo } = require('./util.js')
- const md5 = require('./md5.min.js')
- export const commonParams = () => {
- let systemInfo = getStorageSync('systemInfo')
- if (!systemInfo) {
- try {
- systemInfo = wx.getSystemInfoSync()
- if (systemInfo) {
- wx.setStorageSync('systemInfo', systemInfo)
- }
- } catch (error) {}
- }
- let deviceId = 'weapp'
- let systemVersion = 'weapp'
- if (systemInfo) {
- let {
- brand,
- model,
- system
- } = systemInfo
- if (brand || model) {
- deviceId = brand + model
- }
- if (system && 'string' === typeof system) {
- systemVersion = system
- }
- }
- const params = {
- cityId: '254', //城市ID默认为2
- language: 'cn', //语言参数
- program: 'liuxue', // 小程序名称
- deviceType: 'weapp', //接口调用来源参数
- deviceId: deviceId, //设备唯一标识码
- apiVersion: '1.0.0', //api版本号
- systemVersion: systemVersion //操作系统及版本号
- }
- const loginUserInfo = getLoginUserInfo()
- if (loginUserInfo && loginUserInfo.token) {
- params.token = loginUserInfo.token //用户登录凭证token
- }
- let citySelectObj = getStorageSync('citySelectObj')
- if (citySelectObj) {
- params.cityId = citySelectObj.cityId //用户选择的城市id
- }
- return params
- }
- export const paramsSign = (params, url) => {
- /*参数签名*/
- let key = 'd9ce9fe030776727e85442d033d1be84'
- if (url && url.indexOf('/common/') !== -1) {
- key = 'b15681f74461af9642ab5356fba7962b'
- }
- const tempParams = { ...params }
- const paramsString = Object.keys(tempParams).sort().map(key => {
- return `${key}=${tempParams[key]}`
- }).concat(`key=${key}`).join('&')
- return md5(paramsString).toLowerCase()
- }
- export const request = (params) => {
- return new Promise((resolve, reject) => {
- const temp = params
- // 过滤空值
- for (const key in temp.data) {
- if (isUndef(temp.data[key])) {
- delete temp.data[key]
- }
- }
- temp.data = { ...commonParams(), ...temp.data }
- temp.data.signToken = paramsSign(temp.data, temp.url)
- wx.request({
- url: api + temp.url,
- data: temp.data,
- header: {
- 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
- ...temp.header
- },
- method: temp.method || 'POST',
- dataType: temp.dataType || 'json',
- success: res => {
- const response = res.data
- DEBUG && console.log(`Request for ${temp.url} exception.--->${response.msg}`)
- if (response && response.code !== 0) {
- // 未登录
- if (NOT_LOGIN === response.code) {
- wx.clearStorage()
- wx.showToast({
- title: '您的用户信息已失效,请重新登录操作',
- icon: 'none',
- duration: 2000,
- success: () => {
- wx.navigateTo({
- url: '/pages/login/login'
- })
- }
- })
- } else {
- wx.showToast({
- title: response.msg || 'error',
- icon: 'none',
- duration: 2000
- })
- reject(response)
- }
- } else {
- resolve(response)
- }
- },
- fail: err => {
- DEBUG && console.log(`Request for ${temp.url} fail.--->`, err)
- reject(err)
- },
- complete: () => {
- wx.stopPullDownRefresh()
- }
- })
- })
- }
|