123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- import store from '@/store'
- import request from './request'
- import config from '@/config'
- import { isWechat } from '../app';
- const baseURL = config.apiUrl;
- const merchantNo = config.merchantNo;
- const $http = new request({
-
- baseUrl: baseURL,
-
- fileUrl: baseURL,
-
- defaultUploadUrl: 'clientApi/file/upload',
-
- header: {
- 'content-type': 'application/json;charset=utf-8'
- },
-
- timeout: 300000,
-
- config: {
-
- isPrompt: true,
-
- load: true,
-
- isFactory: true
- }
- })
- let requestNum = 0
- $http.requestStart = options => {
- if (options.load) {
- if (requestNum <= 0) {
-
- uni.showLoading({
- title: '加载中',
- mask: true
- })
- }
- requestNum += 1
- }
-
- if (options.method == "FILE" && options.maxSize) {
-
- const maxSize = options.maxSize
- for (let item of options.files) {
- if (item.size > maxSize) {
- setTimeout(() => {
- uni.showToast({
- title: "图片过大,请重新上传",
- icon: "none"
- })
- }, 10)
- return false
- }
- }
- }
-
- options.header['platform'] = store.getters.platform ? String(store.getters.platform) : ''
-
-
- options.header['Access-Token'] = store.getters.token ? String(store.getters.token) : ''
-
- options.header['merchantNo'] = uni.getStorageSync("merchantNo") ? uni.getStorageSync("merchantNo") : merchantNo;
-
- options.header['storeId'] = uni.getStorageSync("storeId") ? uni.getStorageSync("storeId") : 0;
- options.header['orderId'] = uni.getStorageSync("orderId") ? uni.getStorageSync("orderId") : 0;
- options.header['tableId'] = uni.getStorageSync("tableId") ? uni.getStorageSync("tableId") : 0;
- options.header['latitude'] = uni.getStorageSync("latitude") ? uni.getStorageSync("latitude") : '';
- options.header['longitude'] = uni.getStorageSync("longitude") ? uni.getStorageSync("longitude") : '';
- options.header['isWechat'] = isWechat() ? 'Y' : 'N';
-
- return options
- }
- $http.requestEnd = options => {
-
- if (options.load) {
- requestNum = requestNum - 1
- if (requestNum <= 0) {
- uni.hideLoading()
- }
- }
- }
- let loginPopupNum = 0
- $http.dataFactory = async res => {
-
-
-
-
-
-
-
- if (!res.response.statusCode || res.response.statusCode != 200) {
-
- return Promise.reject({
- statusCode: res.response.statusCode,
-
- errMsg: 'http状态码错误'
- })
- }
- let httpData = res.response.data
- if (typeof httpData == "string") {
- try {
- httpData = JSON.parse(httpData)
- } catch {
- httpData = false
- }
- }
- if (httpData === false || typeof httpData !== 'object') {
-
- return Promise.reject({
- statusCode: res.response.statusCode,
- errMsg: "返回数据验证不通过"
- })
- }
-
-
-
-
- if (httpData.code == 1001) {
-
- store.dispatch('Logout')
-
- if (loginPopupNum <= 0) {
- loginPopupNum++
- uni.showModal({
- title: '温馨提示',
- content: '此时此刻需要您登录喔~',
- confirmText: "去登录",
- cancelText: "再逛会",
- success: res => {
- loginPopupNum--
- if (res.confirm) {
- uni.navigateTo({
- url: "/pages/login/index"
- })
- }
- }
- })
- }
-
- return Promise.reject({
- statusCode: 0,
- errMsg: httpData.message,
- result: httpData
- })
- }
-
- else if (httpData.status == 5000) {
- if (res.isPrompt) {
- setTimeout(() => {
- uni.showToast({
- title: httpData.message,
- icon: "none",
- duration: 2500
- }, 10)
- })
- }
-
- return Promise.reject({
- statusCode: 0,
- errMsg: httpData.message,
- result: httpData
- })
- } else {
-
- return Promise.resolve(httpData)
- }
-
- }
- $http.requestError = e => {
- if (e.statusCode === 0) {
- throw e
- } else {
- setTimeout(() => {
- uni.showToast({
- title: `网络请求出错:${e.errMsg}`,
- icon: "none",
- duration: 2500
- })
- })
- }
- }
- export default $http
|