http.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import EventRequest from './EventRequest'
  2. import { getSign } from './util'
  3. const versionInfo = wx.getAccountInfoSync()
  4. const { envVersion = __wxConfig } = versionInfo.miniProgram
  5. let baseUrl = 'http://localhost:21028'
  6. // let baseUrl = 'https://api.huanart.com'
  7. let fileUrl = ''
  8. switch (envVersion) {
  9. case 'trial':
  10. case 'release':
  11. baseUrl = 'https://api.huanart.com'
  12. break
  13. default:
  14. break
  15. }
  16. fileUrl = `${baseUrl}/api/FileAuth/Transport`
  17. const http = new EventRequest({
  18. baseUrl,
  19. timeout: 60000,
  20. })
  21. http.setResponseInterceptor((_, rsp) => {
  22. return new Promise<any>((resolve, reject) => {
  23. if (rsp.statusCode === 200) {
  24. resolve(rsp.data)
  25. return
  26. } else {
  27. if (rsp.data && rsp.data.error && rsp.data.error.message) {
  28. wx.showModal({
  29. title: '请求出错',
  30. content: rsp.data.error.message,
  31. success() {},
  32. })
  33. reject(rsp.data.error)
  34. return
  35. }
  36. if (rsp.errMsg && rsp.errMsg.length > 0) {
  37. reject({ message: '服务器链接不成功' })
  38. return
  39. }
  40. if (rsp.timeout && rsp.timeout.length > 0) {
  41. reject({ message: rsp.timeout })
  42. return
  43. }
  44. }
  45. reject({ message: '与服务器通讯错误' })
  46. })
  47. })
  48. export default http
  49. export const uploadProps = (query: string = '') => {
  50. const token = getSign()
  51. const language = 'zh-Hans'
  52. if (token != null) {
  53. return {
  54. url: fileUrl + (query && query.length > 0 ? '?' + query : ''),
  55. header: {
  56. Authorization: `Bearer ${token}`,
  57. '.AspNetCore.Culture': `c=${language}|uic=${language}`,
  58. Accept: 'application/json',
  59. } as Record<string, any>,
  60. }
  61. }
  62. return undefined
  63. }
  64. export const weburl = baseUrl