requestAdmin.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 {clearAdminToken, 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. clearAdminToken();
  58. store.dispatch('LogOut').then(() => {
  59. uni.reLaunch({ url: '/pages/cjx/hexiao/login' })
  60. })
  61. }
  62. result.success = false
  63. resolve(result)
  64. return
  65. }
  66. if (code === 500) {
  67. result.success = false
  68. resolve(result)
  69. return
  70. }
  71. if (code !== 0) {
  72. result.success = false
  73. resolve(result)
  74. return
  75. }
  76. resolve(result)
  77. }).catch(error => {
  78. let { message } = error
  79. if (message === 'Network Error') {
  80. message = '后端接口连接异常'
  81. } else if (message.includes('timeout')) {
  82. message = '系统接口请求超时'
  83. } else if (message.includes('Request failed with status code')) {
  84. message = '系统接口' + message.substr(message.length - 3) + '异常'
  85. }
  86. toast(message)
  87. resolve({
  88. success: false,
  89. code: -999,
  90. msg: message,
  91. data: null
  92. })
  93. })
  94. })
  95. }
  96. export function uploadImageToServer(tempFilePath){
  97. return new Promise((resolve, reject) => {
  98. // 获取已选择的图片临时路径
  99. // const tempFilePath = this.formData.photos[0].url;
  100. config.header = config.header || {}
  101. if (getAdminToken()) {
  102. config.header['sessionId'] = getAdminToken()
  103. }else{
  104. }
  105. // 使用 uni.uploadFile 上传
  106. uni.uploadFile({
  107. // 【重要】这里是您后端接收上传文件的接口地址
  108. url: config.baseUrl +'/ywy/uploadStoreImage',
  109. filePath: tempFilePath,
  110. name: 'file', // 和后端约定的文件名参数,'file' 是常用名
  111. // 如果您的接口需要token等header信息
  112. header: config.header,
  113. success: (res) => {
  114. console.log('上传接口返回结果:', res);
  115. // 通常后端会返回一个JSON字符串,需要解析
  116. const resData = JSON.parse(res.data);
  117. // 根据您和后端约定的返回格式进行判断
  118. // 假设成功时 { "code": 0, "data": { "url": "https://oss..." } }
  119. if (resData.code === 0) {
  120. // 上传成功,resolve返回最终的图片URL
  121. resolve(resData.data);
  122. } else {
  123. // 上传失败,reject返回错误信息
  124. reject(new Error(resData.msg || '上传失败'));
  125. }
  126. },
  127. fail: (err) => {
  128. reject(err);
  129. }
  130. });
  131. });
  132. }
  133. export default request