| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import store from '@/store'
- import config from '@/config'
- import { getToken } from '@/utils/auth'
- import errorCode from '@/utils/errorCode'
- import { toast, showConfirm, tansParams } from '@/utils/common'
- import {clearAdminToken, getAdminToken} from "./auth";
- let timeout = 10000
- const baseUrl = config.baseUrl
- const request = config => {
- console.log(44444)
- // 是否需要设置 token
- const isToken = (config.headers || {}).isToken === false
- config.header = config.header || {}
- if (getAdminToken() && !isToken) {
- config.header['sessionId'] = getAdminToken()
- }
- // get请求映射params参数
- if (config.params) {
- let url = config.url + '?' + tansParams(config.params)
- url = url.slice(0, -1)
- config.url = url
- }
- return new Promise(resolve => {
- uni.request({
- method: config.method || 'get',
- timeout: config.timeout || timeout,
- url: config.baseUrl || baseUrl + config.url,
- data: config.data,
- header: config.header,
- dataType: 'json'
- }).then(response => {
- const error = response.statusCode !== 200
- const res = response
- console.log(response)
- if (error) {
- toast('后端接口连接异常')
- resolve({
- success: false,
- code: response.statusCode,
- msg: '后端接口连接异常',
- data: null
- })
- return
- }
- const code = res.data.code !== undefined ? res.data.code : 200
- const msg = res.data.msg || errorCode[code] || errorCode.default
- // 构造统一返回结构
- const result = {
- success: code === 0,
- code,
- msg,
- data: res.data.data || null,
- raw: res.data
- }
- if (code === 401 || code === -10) {
- if (getAdminToken()) {
- clearAdminToken();
- store.dispatch('LogOut').then(() => {
- uni.reLaunch({ url: '/pages/cjx/hexiao/login' })
- })
- }
- result.success = false
- resolve(result)
- return
- }
- if (code === 500) {
- result.success = false
- resolve(result)
- return
- }
- if (code !== 0) {
- result.success = false
- resolve(result)
- return
- }
- resolve(result)
- }).catch(error => {
- let { message } = error
- if (message === 'Network Error') {
- message = '后端接口连接异常'
- } else if (message.includes('timeout')) {
- message = '系统接口请求超时'
- } else if (message.includes('Request failed with status code')) {
- message = '系统接口' + message.substr(message.length - 3) + '异常'
- }
- toast(message)
- resolve({
- success: false,
- code: -999,
- msg: message,
- data: null
- })
- })
- })
- }
- export function uploadImageToServer(tempFilePath){
- return new Promise((resolve, reject) => {
- // 获取已选择的图片临时路径
- // const tempFilePath = this.formData.photos[0].url;
- config.header = config.header || {}
- if (getAdminToken()) {
- config.header['sessionId'] = getAdminToken()
- }else{
- }
- // 使用 uni.uploadFile 上传
- uni.uploadFile({
- // 【重要】这里是您后端接收上传文件的接口地址
- url: config.baseUrl +'/ywy/uploadStoreImage',
- filePath: tempFilePath,
- name: 'file', // 和后端约定的文件名参数,'file' 是常用名
- // 如果您的接口需要token等header信息
- header: config.header,
- success: (res) => {
- console.log('上传接口返回结果:', res);
- // 通常后端会返回一个JSON字符串,需要解析
- const resData = JSON.parse(res.data);
- // 根据您和后端约定的返回格式进行判断
- // 假设成功时 { "code": 0, "data": { "url": "https://oss..." } }
- if (resData.code === 0) {
- // 上传成功,resolve返回最终的图片URL
- resolve(resData.data);
- } else {
- // 上传失败,reject返回错误信息
- reject(new Error(resData.msg || '上传失败'));
- }
- },
- fail: (err) => {
- reject(err);
- }
- });
- });
- }
- export default request
|