newRequest.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import store from '../store'
  2. let service = {
  3. baseURL: store.state.baseURL, // 请求头
  4. header: {
  5. 'Content-Type': "application/x-www-form-urlencoded"
  6. },
  7. // 请求拦截器
  8. interceptors: {
  9. // 安装请求前拦截
  10. request: function(success, iserror) {
  11. service.request.success = success;
  12. service.request.error = iserror;
  13. },
  14. // 安装请求完成后拦截
  15. response: function(success, iserror) {
  16. service.response.success = success;
  17. service.response.error = iserror;
  18. }
  19. },
  20. // 请求前拦截
  21. request: {
  22. // 回调成功执行方法
  23. success: function(e) {
  24. return e
  25. },
  26. // 回调失败
  27. error: function(e) {
  28. return e
  29. }
  30. },
  31. // 请求完成后拦截
  32. response: {
  33. // 回调成功执行方法
  34. success: function(e) {
  35. return e
  36. },
  37. // 回调失败
  38. error: function(e) {
  39. return e
  40. }
  41. },
  42. // 开始请求
  43. open: async function(data) {
  44. return await new Promise(function(resolve, reject) {
  45. let requestData = ''; //保存处理完成后请求数据
  46. //console.log(data);
  47. // 请求前数据处理
  48. requestData = service.request.success(data);
  49. // 数据复制用于请求
  50. uni.request({
  51. url: service.baseURL + requestData.url,
  52. method: requestData.method || 'GET',
  53. data: requestData.data,
  54. header: data.header || service.header,
  55. success: res => {
  56. if (res.statusCode == 200) {
  57. try {
  58. resolve(service.response.success(res));
  59. } catch (e) {
  60. service.response.error(e)
  61. }
  62. }
  63. },
  64. // 请求失败数据处理
  65. fail: (err) => {
  66. service.response.error(err)
  67. }
  68. });
  69. })
  70. },
  71. // 上传文件
  72. upFilse: async function(data) {
  73. return await new Promise(function(resolve, reject) {
  74. let requestData = ''; //保存处理完成后请求数据
  75. try {
  76. // 请求前数据处理
  77. requestData = service.request.success(data);
  78. } catch (e) {
  79. // 请求报错拦截
  80. service.request.error(e);
  81. }
  82. let config = {
  83. // 上传图片的API
  84. upload_img_url: service.baseURL + requestData.url
  85. }
  86. let Uploader = {
  87. // 选择图片
  88. choose(num) {
  89. return new Promise((resolve, reject) => {
  90. uni.chooseImage({
  91. count: num,
  92. sourceType:['album'],
  93. sizeType:['compressed'],
  94. success(res) {
  95. // console.log(res);
  96. // 缓存文件路径
  97. resolve(res.tempFilePaths)
  98. },
  99. fail(err) {
  100. console.log(err)
  101. reject(err)
  102. }
  103. })
  104. })
  105. },
  106. // 上传图片
  107. upload_one(path) {
  108. return new Promise((resolve, reject) => {
  109. uni.showLoading({
  110. title: '上传中'
  111. })
  112. uni.uploadFile({
  113. url: config.upload_img_url, //仅为示例,非真实的接口地址
  114. filePath: path,
  115. name: 'file',
  116. header: {
  117. "Authori-zation": 'Bearer ' + uni.getStorageSync('token')
  118. },
  119. success: (uploadFileRes) => {
  120. if ("string" === typeof uploadFileRes.data) {
  121. resolve(JSON.parse(uploadFileRes.data).data)
  122. } else {
  123. resolve(uploadFileRes.data.data)
  124. }
  125. },
  126. complete() {
  127. uni.hideLoading()
  128. }
  129. });
  130. })
  131. },
  132. // 上传多个图片
  133. upload(path_arr) {
  134. let num = path_arr.length;
  135. return new Promise(async (resolve, reject) => {
  136. let img_urls = []
  137. for (let i = 0; i < num; i++) {
  138. let img_url = await this.upload_one(path_arr[i]);
  139. img_urls.push(img_url)
  140. };
  141. resolve(img_urls)
  142. })
  143. },
  144. // 触发上传图片事件
  145. choose_and_upload(num) {
  146. return new Promise(async (resolve, reject) => {
  147. // 选择图片
  148. let path_arr = await this.choose(num);
  149. // 上传图片
  150. await this.upload(path_arr).then((e) => {
  151. resolve(e);
  152. }).catch((e) => {
  153. reject(e)
  154. });
  155. })
  156. }
  157. };
  158. Uploader.choose_and_upload(1).then((e) => {
  159. resolve(e);
  160. }).catch((e) => {
  161. console.log(e);
  162. })
  163. })
  164. },
  165. };
  166. export default service