requestAdmin.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import store from '@/store'
  2. import config from '@/config'
  3. import { getToken } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. import { toast, showConfirm, tansParams } from '@/utils/common'
  6. import {getAdminToken} from "./auth";
  7. let timeout = 10000
  8. const baseUrl = config.baseUrl
  9. const request = config => {
  10. console.log(44444)
  11. // 是否需要设置 token
  12. const isToken = (config.headers || {}).isToken === false
  13. config.header = config.header || {}
  14. if (getAdminToken() && !isToken) {
  15. config.header['sessionId'] = getAdminToken()
  16. }
  17. // get请求映射params参数
  18. if (config.params) {
  19. let url = config.url + '?' + tansParams(config.params)
  20. url = url.slice(0, -1)
  21. config.url = url
  22. }
  23. return new Promise(resolve => {
  24. uni.request({
  25. method: config.method || 'get',
  26. timeout: config.timeout || timeout,
  27. url: config.baseUrl || baseUrl + config.url,
  28. data: config.data,
  29. header: config.header,
  30. dataType: 'json'
  31. }).then(response => {
  32. const error = response.statusCode !== 200
  33. const res = response
  34. console.log(response)
  35. if (error) {
  36. toast('后端接口连接异常')
  37. resolve({
  38. success: false,
  39. code: response.statusCode,
  40. msg: '后端接口连接异常',
  41. data: null
  42. })
  43. return
  44. }
  45. const code = res.data.code !== undefined ? res.data.code : 200
  46. const msg = res.data.msg || errorCode[code] || errorCode.default
  47. // 构造统一返回结构
  48. const result = {
  49. success: code === 0,
  50. code,
  51. msg,
  52. data: res.data.data || null,
  53. raw: res.data
  54. }
  55. if (code === 401 || code === -10) {
  56. if (getAdminToken()) {
  57. store.dispatch('LogOut').then(() => {
  58. uni.reLaunch({ url: '/pages/cjx/hexiao/login' })
  59. })
  60. }
  61. result.success = false
  62. resolve(result)
  63. return
  64. }
  65. if (code === 500) {
  66. result.success = false
  67. resolve(result)
  68. return
  69. }
  70. if (code !== 0) {
  71. result.success = false
  72. resolve(result)
  73. return
  74. }
  75. resolve(result)
  76. }).catch(error => {
  77. let { message } = error
  78. if (message === 'Network Error') {
  79. message = '后端接口连接异常'
  80. } else if (message.includes('timeout')) {
  81. message = '系统接口请求超时'
  82. } else if (message.includes('Request failed with status code')) {
  83. message = '系统接口' + message.substr(message.length - 3) + '异常'
  84. }
  85. toast(message)
  86. resolve({
  87. success: false,
  88. code: -999,
  89. msg: message,
  90. data: null
  91. })
  92. })
  93. })
  94. }
  95. export function uploadImageToServer(tempFilePath){
  96. return new Promise((resolve, reject) => {
  97. // 获取已选择的图片临时路径
  98. // const tempFilePath = this.formData.photos[0].url;
  99. config.header = config.header || {}
  100. if (getAdminToken()) {
  101. config.header['sessionId'] = getAdminToken()
  102. }else{
  103. }
  104. // 使用 uni.uploadFile 上传
  105. uni.uploadFile({
  106. // 【重要】这里是您后端接收上传文件的接口地址
  107. url: config.baseUrl +'/ywy/uploadStoreImage',
  108. filePath: tempFilePath,
  109. name: 'file', // 和后端约定的文件名参数,'file' 是常用名
  110. // 如果您的接口需要token等header信息
  111. header: config.header,
  112. success: (res) => {
  113. console.log('上传接口返回结果:', res);
  114. // 通常后端会返回一个JSON字符串,需要解析
  115. const resData = JSON.parse(res.data);
  116. // 根据您和后端约定的返回格式进行判断
  117. // 假设成功时 { "code": 0, "data": { "url": "https://oss..." } }
  118. if (resData.code === 0) {
  119. // 上传成功,resolve返回最终的图片URL
  120. resolve(resData.data);
  121. } else {
  122. // 上传失败,reject返回错误信息
  123. reject(new Error(resData.msg || '上传失败'));
  124. }
  125. },
  126. fail: (err) => {
  127. reject(err);
  128. }
  129. });
  130. });
  131. }
  132. export default request