123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import {
- server_host
- } from "@/config/system"
- import {
- getCache
- } from "./cache";
- const auth_white_list = [
- '/auth/b/doLogin',
- '/auth/c/orgInfo/depotLogin',
- '/auth/c/ipad/doLogin',
- ]
- const getData = ({
- method,
- url,
- data,
- config
- }) => {
- return new Promise((resolve, reject) => {
- const host = config?.server_host || server_host
- uni.request({
- url: `${host}${url}`,
- data,
- header: config?.header || {},
- method: method,
- dataType: config?.dataType || 'json',
- success(resp) {
- uni.hideLoading()
- if (config.directResp) {
- resolve(resp.data)
- } else {
- const code = resp.data.code
- if (code == '200') {
- resolve(resp.data)
- } else if (code == '404') {
- reject('找不到服务', code)
- } else if (code == '401') {
- uni.redirectTo({
- url: '/pages/login/login'
- })
- } else {
- reject(resp.data.msg || '系统错误', code)
- }
- }
- },
- fail(err) {
- uni.hideLoading()
- reject('无法连接服务器,请检查您的网络', '-1')
- },
- })
- })
- }
- const doRequest = ({
- method,
- url,
- data,
- config
- }) => {
- uni.showLoading({
- mask: true
- })
- if (auth_white_list.includes(url)) {
- return getData({
- method,
- url,
- data,
- config
- }).catch((err, code) => {
- uni.showToast({
- title: err.errMsg || err,
- icon: 'error',
- })
- if (code == '401') {
- uni.redirectTo({
- url: '/pages/login/login'
- })
- }
- return Promise.reject({
- err,
- code
- })
- })
- }
- return getCache('token').then(resp => {
- const token = resp.data
- if (config) {
- if (config.header) {
- config.header.token = token
- } else {
- config.header = {
- token
- }
- }
- } else {
- config = {
- header: {
- token
- }
- }
- }
- return getData({
- method,
- url,
- data,
- config
- })
- }).catch((err, code) => {
- uni.showToast({
- title: err.errMsg || err,
- icon: 'error',
- })
- })
- }
- const get = ({
- url,
- data,
- config
- }) => {
- const d = data || {}
- d._ = new Date().getTime()
- return doRequest({
- method: 'GET',
- url,
- data,
- config: config || {}
- })
- }
- const post = ({
- url,
- data,
- config
- }) => {
- return doRequest({
- method: 'POST',
- url,
- data,
- config: config || {}
- })
- }
- const request = {
- get,
- post
- }
- export default request
|