| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /* eslint-disable no-console */
- // 全局请求封装
- const http = {
- request: function (params) {
- // 请求参数处理
- if (Object.prototype.toString.call(params.data) === '[object Array]') {
- params.data = JSON.stringify(params.data)
- } else if (Object.prototype.toString.call(params.data) === '[object Number]') {
- params.data = params.data + ''
- }
- // 发起请求
- return new Promise((resolve, reject) => {
- uni.request({
- dataType: 'json',
- responseType: params.responseType === undefined ? 'text' : params.responseType,
- header: {
- Authorization: uni.getStorageSync('mall4jStationToken'),
- locale: uni.getStorageSync('bbcLang') || 'zh_CN',
- timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
- },
- url: (params.domain ? params.domain : import.meta.env.VITE_APP_BASE_API) + params.url,
- data: params.data,
- method: params.method === undefined ? 'POST' : params.method,
- success: (res) => {
- const responseData = res.data
- if (responseData.code === 'A00001') {
- if (!params.hasCatch) {
- uni.showToast({
- title: responseData.msg || responseData.data || 'Error',
- icon: 'none'
- })
- }
- }
- // 00000 请求成功
- // A00002 用于直接显示提示系统的成功,内容由输入决定
- if (responseData.code === '00000' || responseData.code === 'A00002') {
- resolve(responseData)
- }
- // A00004 未授权
- if (responseData.code === 'A00004') {
- uni.removeStorageSync('mall4jStationLoginResult')
- uni.removeStorageSync('mall4jStationToken')
- uni.hideLoading()
- if (!params.dontTrunLogin) {
- if (uni.getStorageSync('mall4jHadLogin')) {
- uni.showModal({
- title: '提示',
- content: '登录已过期,请重新登录!',
- success: res => {
- if (res.confirm) {
- // 跳转登录页面
- uni.redirectTo({
- url: '/pages/account-login/account-login'
- })
- } else {
- uni.navigateTo({
- url: '/pages/index/index'
- })
- }
- }
- })
- } else {
- // 跳转登录页面
- uni.navigateTo({
- url: '/pages/account-login/account-login'
- })
- }
- }
- }
- // A00005 服务器出了点小差
- if (responseData.code === 'A00005') {
- this.onRequestFail(params, responseData)
- uni.showToast({
- title: '服务器出了点小差~',
- icon: 'none'
- })
- }
- if (responseData.code !== '00000') {
- reject(responseData)
- }
- },
- fail: (err) => {
- uni.showToast({
- title: '请求失败'
- })
- reject(err)
- }
- })
- })
- },
- onRequestFail: (params, responseData) => {
- console.error('============== 请求异常 ==============')
- console.log('接口地址: ', params.url)
- console.log('异常信息: ', responseData)
- console.error('============== 请求异常 end ==========')
- },
- /**
- * 上传文件统一接口
- */
- upload: (params) => {
- uni.uploadFile({
- url: import.meta.env.VITE_APP_BASE_API + params.url,
- filePath: params.filePath,
- name: params.name,
- header: {
- Authorization: params.login ? undefined : uni.getStorageSync('mall4jStationToken')
- },
- dataType: 'json',
- responseType: params.responseType === undefined ? 'json' : params.responseType,
- success: (res) => {
- const responseData = JSON.parse(res.data)
- if (responseData.code === '00000') {
- if (params.callBack) {
- params.callBack(responseData.data)
- }
- } else {
- uni.showToast({
- title: '服务器出了点小差~',
- icon: 'none'
- })
- }
- },
- fail: function () {
- uni.hideLoading()
- }
- })
- }
- }
- export default http
|