123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import {
- server_host
- } from "@/config/system"
- import {
- getCache
- } from "./cache";
- const auth_whit_list = [
- '/auth/c/userCollect/appCarLogin',
- '/auth/c/userCollect/appCarEnroll',
- ]
- const getData = ({
- method,
- url,
- data,
- config
- }) => {
- return new Promise((resolve, reject) => {
- console.log('---', config);
- const host = config?.server_host || server_host
- console.log(`请求地址 - ${method} - ${host}${url}`);
- uni.request({
- url: `${host}${url}`,
- data,
- header: config?.header || {},
- method: method,
- dataType: config?.dataType || 'json',
- success(resp) {
- uni.hideLoading()
- const code = resp.data.code
- if (code == '200') {
- console.log('1', resp.data);
- resolve(resp.data)
- } else if (code == '404') {
- reject('找不到服务', code)
- } else if (code == '401') {
- reject('授权错误', code)
- } else {
- reject(resp.data.msg, code)
- }
- },
- fail(err) {
- uni.hideLoading()
- reject('无法连接服务器,请检查您的网络', '-1')
- },
- })
- })
- }
- const doRequest = ({
- method,
- url,
- data,
- config
- }) => {
- uni.showLoading({
- mask: true
- })
- if (auth_whit_list.includes(url)) {
- console.log('白名单 不检查 token');
- return getData({
- method,
- url,
- data,
- config
- }).catch((err, code) => {
- console.log('err', err, code);
- uni.showToast({
- title: err.errMsg || err,
- icon: 'error',
- })
- if (code == '401') {
- uni.redirectTo({
- url: '/pages/login/login'
- })
- }
- })
- }
- return getCache('token').then(resp => {
- console.log('doRequest - getCache', resp, config);
- const token = resp.data
- if (config) {
- if (config.header) {
- config.header.token = token
- } else {
- config.header = {
- token
- }
- }
- } else {
- config = {
- header: {
- token
- }
- }
- }
- console.log('122');
- return getData({
- method,
- url,
- data,
- config
- })
- }).catch((err, code) => {
- console.log('err', err, code);
- uni.showToast({
- title: err.errMsg || err,
- icon: 'error',
- })
- if (code == '401') {
- uni.redirectTo({
- url: '/pages/login/login'
- })
- }
- })
- }
- const get = ({
- url,
- data,
- config
- }) => {
- return doRequest({
- method: 'GET',
- url,
- data,
- config
- })
- }
- const post = ({
- url,
- data,
- config
- }) => {
- return doRequest({
- method: 'POST',
- url,
- data,
- config
- })
- }
- const request = {
- get,
- post
- }
- export default request
|