requestAdmin.js 4.2 KB

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