network.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import store from '../store'
  2. import { env } from '../static/config'
  3. export function requestApi (reqUrl, type = 'POST', data = {}, auth = '', multipart = false, arraybuffer = false) {
  4. if (!reqUrl) { return }
  5. let headers = {}
  6. if (auth === 'seller') {
  7. headers['X-DS-KEY'] = store.state.seller.token
  8. }
  9. type = type.toUpperCase()
  10. reqUrl = env.API_HOST + reqUrl
  11. return new Promise((resolve, reject) => {
  12. function returnData(res) {
  13. if(arraybuffer){
  14. resolve(res.data)
  15. }else{
  16. if (res.data.code === 10000) {
  17. resolve(res.data)
  18. } else if (res.data.code === 13001) {
  19. uni.navigateTo({
  20. url: '/pages/home/sellerlogin/Login?clear=1'
  21. }) // token过期,需要删除
  22. } else {
  23. reject(res.data)
  24. }
  25. }
  26. }
  27. if (multipart) {
  28. var filePath = data.filePath
  29. var name = data.name
  30. delete data.filePath
  31. uni.uploadFile({
  32. url: reqUrl,
  33. filePath: filePath,
  34. name: name,
  35. formData: data,
  36. header: headers,
  37. success: (res) => {
  38. res.data = JSON.parse(res.data)
  39. returnData(res)
  40. },
  41. fail: (error) => {
  42. reject(error)
  43. }
  44. })
  45. } else {
  46. uni.request({
  47. url: reqUrl,
  48. data: data,
  49. header: headers,
  50. method: type,
  51. withCredentials: true,
  52. responseType:arraybuffer?'arraybuffer':'text',
  53. success: (res) => {
  54. returnData(res)
  55. },
  56. fail: (error) => {
  57. reject(error)
  58. },
  59. })
  60. }
  61. })
  62. }
  63. export function requestRaw(reqUrl, type = 'POST', data = {}) {
  64. type = type.toUpperCase()
  65. return new Promise((resolve, reject) => {
  66. uni.request({
  67. url: reqUrl,
  68. data: data,
  69. method: type,
  70. withCredentials: true,
  71. success: (res) => {
  72. resolve(res.data)
  73. },
  74. fail: (error) => {
  75. reject(error)
  76. }
  77. })
  78. })
  79. }