123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import Vue from 'vue'
- import md5 from 'js-md5'
- import Storage from '@/utils/storage'
- import * as Foundation from '@/ui-utils/Foundation'
- import store from '@/store'
- import checkToken from '@/utils/checkToken'
- export const API = {
- // base: 'https://api.base.wdklian.com',
- // buyer: 'https://api.buyer.wdklian.com',
- base: 'http://192.168.1.199:7000',
- buyer: 'http://192.168.1.199:7002'
- }
- const config = {
- timeout: 15000,
- baseURL: API.buyer,
- headers: {
- 'Content-Type':'application/x-www-form-urlencoded'
- }
- }
- export const Method = {
- GET: 'GET',
- POST: 'POST',
- PUT: 'PUT',
- DELETE: 'DELETE'
- }
- function service(options) {
- let url = config.baseURL + '/' + options.url
- if (options.url.includes("http")){
- url = options.url
- }
- let headers = config.headers
- const uuid = Storage.getItem('uuid')
- headers['uuid'] = uuid
- if (options.config.isJson){
- headers['Content-Type']='application/json'
- } else {
- headers['Content-Type']='application/x-www-form-urlencoded'
- }
- if (options.config.loading){
- uni.showLoading()
- }
- if (options.config.needToken){
- let accessToken = Storage.getItem('access_token')
- if (!accessToken) {
- uni.reLaunch({
- url: '/pages/login/index'
- })
- return
- }
- let uid = Storage.getItem('uid')
- let nonce = Foundation.randomString(6)
- let timestamp = parseInt(new Date().getTime() / 1000)
- let sign = md5(uid + nonce + timestamp + accessToken)
- if (url.includes('?')) {
- url = `${url}&uid=${uid}&nonce=${nonce}×tamp=${timestamp}&sign=${sign}`
- } else {
- url = `${url}?uid=${uid}&nonce=${nonce}×tamp=${timestamp}&sign=${sign}`
- }
- }
- return new Promise((resolve,reject)=>{
- // console.log(options, config);
- uni.request({
- url: url,
- data: options.params,
- header: headers,
- timeout: config.timeout,
- method: options.config.method,
- success(res) {
- uni.hideLoading()
- if (res.statusCode == 200){
- resolve(res.data)
- } else if (res.statusCode == 403){
- if (!Storage.getItem('refresh_token')) {
- console.log('444444444444444')
- uni.reLaunch({
- url: '/pages/login/index'
- })
- resolve()
- return
- }
- store.dispatch('removeUserAction')
- store.dispatch('removeAccessTokenAction')
- store.dispatch('removeRefreshTokenAction')
- Storage.removeItem('login_result')
- Storage.removeItem('refresh_token')
- Storage.removeItem('access_token_timeout')
- Storage.removeItem('refresh_token_timeout')
- Vue.prototype.$checkHasLogin()
- } else{
- if (url.indexOf("/wx_mobile") === -1) {
- uni.showToast({
- title: res.data.message,
- icon: 'none'
- })
- }
- reject(res)
- }
- },
- fail(err){
- uni.hideLoading()
- console.log('错误消息====', err)
- if (err.data) {
- uni.showToast({
- title: err.data.message,
- icon: 'none'
- })
- }
- reject(err)
- }
- });
- })
- }
- export default function request(options){
- if(/passport\/mini-program\/*|passport\/login-binder\/wap\/*|passport\/login\/*/.test(options.url)){
- return service(options)
- }else{
- return checkToken(options).then(()=>{
- return service(options)
- })
- }
- }
|