123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import {randomString, sha1} from "./util"
- class request {
- // static BASE_URL = 'http://127.0.0.1:8014/open/'
- static BASE_URL = 'https://kd.llzlovesh.top/open/'
- static COUPON_URL = this.BASE_URL
- static HEAD = {
- "Content-Type": "application/json",
- "defaultToken": 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTY5MDg3NjAsImV4cCI6MjQ4MDgyMjM2MCwidXNlcklkIjoxMjM0NTY3ODksInZpc2l0b3IiOiJ2aXNpdG9yNjA2MDExZDg4NTQwYyIsImlzQWRtaW4iOjAsImFkbWluSWQiOjB9.7XrRxA3WYMBlqJWIXEw-71AxOxQvCc4VVVN6rlqn3uc',
- "token":'',
- }
- static constructor() {
- }
- /**
- * 设置统一的异常处理
- */
- static setErrorHandler(handler) {
- this._errorHandler = handler
- }
- /**
- * GET类型的网络请求
- */
- static getRequest(url, data, header = this.HEAD ) {
- const token = wx.getStorageSync('token')
- if (token) {
- header.token = token
- }else {
- header.token = header.defaultToken
- }
- return this.requestAll(url, data, header, 'GET')
- }
- /**
- * DELETE类型的网络请求
- */
- static deleteRequest(url, data, header = this.HEAD ) {
- const token = wx.getStorageSync('token')
- if (token) {
- header.token = token
- }else {
- header.token = header.defaultToken
- }
- return this.requestAll(url, data, header, 'DELETE')
- }
- /**
- * PUT类型的网络请求
- */
- static putRequest(url, data, header = this.HEAD ) {
- const token = wx.getStorageSync('token')
- if (token) {
- header.token = token
- }else {
- header.token = header.defaultToken
- }
- return this.requestAll(url, data, header, 'PUT')
- }
- /**
- * POST类型的网络请求
- */
- static postRequest(url, data, header = this.HEAD) {
- const token = wx.getStorageSync('token')
- if (token) {
- header.token = token
- }else {
- header.token = header.defaultToken
- }
- return this.requestAll(url, data, header, 'POST')
- }
- static upload(url, name, path, ortherData, header = this.HEAD){
- const token = wx.getStorageSync('token')
- if (token) {
- header.token = token
- }else {
- header.token = header.defaultToken
- }
- return new Promise((resolve, reject) => {
- wx.uploadFile({
- url: url,
- header: header,
- filePath: path,
- name: name,
- formData: ortherData,
- success: (res => {
- res.data = JSON.parse(res.data);
- if (res.data.code === 1 || res.data.code == 200) {
- //200: 服务端业务处理正常结束
- resolve(res.data)
- } else {
- if (res.data.code === 0){
- wx.showToast({
- title: res.data.message,
- })
- }
- if (res.data.code === 901) {
- _self.HEAD.token = ''
- }
- //其它错误,提示用户错误信息
- if (this._errorHandler != null) {
- //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
- this._errorHandler(res)
- }
- reject(res)
- }
- }),
- fail: (res => {
- if (this._errorHandler != null) {
- this._errorHandler(res)
- }
- wx.showToast({
- title: '网络异常请,稍后再试~',
- })
- reject(res)
- })
- })
- })
- }
- /**
- * 网络请求
- */
- static requestAll(url, data, header, method) {
- const _self = this
- const timestamp = parseInt(new Date().getTime()/1000)
- const nonce = randomString(16)
- const msg = ''
- const token = 'U6Watb875eCiX4Lq'
- const strArr = timestamp + nonce + msg + token
- const encodeStr = sha1(strArr)
- header['NONCE'] = nonce
- header['msg'] = msg
- header['TIMESTAMP'] = timestamp
- header['SIGN'] = encodeStr
- header['VERSION'] = '1.8.0'
- data['errMsg'] = data['errMsg'] ? 'modal' :'toast'
- wx.showLoading()
- return new Promise((resolve, reject) => {
- wx.request({
- url: url,
- data: data,
- header: header,
- method: method,
- success: (res => {
- wx.hideLoading()
- if (res.data.code === 1 || res.data.code == 200) {
- //200: 服务端业务处理正常结束
- resolve(res.data)
- } else {
- if (res.data.code === 401){
- if (data['errMsg'] == 'modal') {
- wx.showModal({
- title: '错误',
- content: res.data.msg,
- })
- }else {
- wx.showToast({
- title: res.data.msg,
- })
- }
-
- }
- if (res.data.code === 901) {
- _self.HEAD.token = ''
- // wx.setStorageSync('token', '')
- }
- //其它错误,提示用户错误信息
- if (this._errorHandler != null) {
- //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
- this._errorHandler(res)
- }
- reject(res)
- }
- }),
- fail: (res => {
- if (this._errorHandler != null) {
- this._errorHandler(res)
- }
- wx.showToast({
- title: '网络异常请,稍后再试~',
- })
- reject(res)
- })
- })
- })
- }
- }
- export default request
|