123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import store from '../store'
- import { env } from '../static/config'
- export function requestApi (reqUrl, type = 'POST', data = {}, auth = '', multipart = false, arraybuffer = false) {
- if (!reqUrl) { return }
- let headers = {}
- if (auth === 'member') {
- headers['X-DS-KEY'] = store.state.member.token
- }
- type = type.toUpperCase()
- reqUrl = env.API_HOST + reqUrl
-
- return new Promise((resolve, reject) => {
- function returnData(res) {
- console.log('res==>>',res)
- if(arraybuffer){
- resolve(res.data)
- }else{
- if (res.data.code === 10000) {
- resolve(res.data)
- } else if (res.data.code === 11001) {
- var pages = getCurrentPages()
- var page = pages[pages.length - 2]
- if (page && (page.route.search('pages/member') != -1)) {
-
- }else{
- uni.navigateTo({
- url: '/pages/home/memberlogin/Login?clear=1'
- }) // token过期,需要删除
- }
- } else {
- reject(res.data)
- }
- }
-
- }
- if (multipart) {
- var filePath = data.filePath
- var name = data.name
- delete data.filePath
- uni.uploadFile({
- url: reqUrl,
- filePath: filePath,
- name: name,
- formData: data,
- header: headers,
- success: (res) => {
- res.data = JSON.parse(res.data)
- returnData(res)
- },
- fail: (error) => {
- reject(error)
- }
- })
- } else {
- uni.request({
- url: reqUrl,
- data: data,
- header: headers,
- method: type,
- withCredentials: true,
- responseType:arraybuffer?'arraybuffer':'text',
- success: (res) => {
- returnData(res)
- },
- fail: (error) => {
- reject(error)
- },
- })
- }
-
- })
- }
- export function requestRaw(reqUrl, type = 'POST', data = {}) {
- type = type.toUpperCase()
-
- return new Promise((resolve, reject) => {
- uni.request({
- url: reqUrl,
- data: data,
- method: type,
- withCredentials: true,
- success: (res) => {
- resolve(res.data)
- },
- fail: (error) => {
- reject(error)
- }
- })
-
- })
- }
|