newRequest.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. let url = '' //保存链接地址
  47. //console.log(data);
  48. // 请求前数据处理
  49. requestData = service.request.success(data);
  50. // #ifdef H5
  51. if (process.env.NODE_ENV === 'development') {
  52. url = requestData.url;
  53. // console.log('开发环境')
  54. } else {
  55. url = service.baseURL + requestData.url;
  56. // console.log('生产环境')
  57. }
  58. // #endif
  59. // #ifndef H5
  60. url = service.baseURL + requestData.url;
  61. // #endif
  62. // 数据复制用于请求
  63. uni.request({
  64. url: url,
  65. method: requestData.method || 'GET',
  66. data: requestData.data,
  67. header: data.header || service.header,
  68. success: res => {
  69. resolve(service.response.success(res));
  70. },
  71. // 请求失败数据处理
  72. fail: (err) => {
  73. service.response.error(err)
  74. }
  75. });
  76. })
  77. },
  78. // 上传文件
  79. upFilse: async function(data) {
  80. return await new Promise(function(resolve, reject) {
  81. let requestData = ''; //保存处理完成后请求数据
  82. let url = '' //保存请求地址
  83. try {
  84. // 请求前数据处理
  85. requestData = service.request.success(data);
  86. } catch (e) {
  87. // 请求报错拦截
  88. service.request.error(e);
  89. }
  90. // #ifdef H5
  91. if (process.env.NODE_ENV === 'development') {
  92. url = requestData.url;
  93. console.log('开发环境')
  94. } else {
  95. url = service.baseURL + requestData.url;
  96. console.log('生产环境')
  97. }
  98. // #endif
  99. // #ifndef H5
  100. url = service.baseURL + requestData.url;
  101. // #endif
  102. let config = {
  103. // 上传图片的API
  104. upload_img_url: url
  105. }
  106. let Uploader = {
  107. // 选择图片
  108. choose(num) {
  109. return new Promise((resolve, reject) => {
  110. uni.chooseImage({
  111. count: num,
  112. success(res) {
  113. // console.log(res);
  114. // 缓存文件路径
  115. resolve(res.tempFilePaths)
  116. },
  117. fail(err) {
  118. console.log(err)
  119. reject(err)
  120. }
  121. })
  122. })
  123. },
  124. // 上传图片
  125. upload_one(path) {
  126. return new Promise((resolve, reject) => {
  127. uni.showLoading({
  128. title: '上传中'
  129. })
  130. uni.uploadFile({
  131. url: config.upload_img_url, //仅为示例,非真实的接口地址
  132. filePath: path,
  133. name: 'file',
  134. header: {
  135. "Token": '' + uni.getStorageSync(
  136. 'token')
  137. },
  138. success: (uploadFileRes) => {
  139. if ("string" === typeof uploadFileRes.data) {
  140. resolve(JSON.parse(uploadFileRes.data).data)
  141. } else {
  142. resolve(uploadFileRes.data.data)
  143. }
  144. },
  145. complete() {
  146. uni.hideLoading()
  147. }
  148. });
  149. })
  150. },
  151. // 上传多个图片
  152. upload(path_arr) {
  153. let num = path_arr.length;
  154. return new Promise(async (resolve, reject) => {
  155. let img_urls = []
  156. for (let i = 0; i < num; i++) {
  157. let img_url = await this.upload_one(path_arr[i]);
  158. img_urls.push(img_url)
  159. };
  160. resolve(img_urls)
  161. })
  162. },
  163. // 触发上传图片事件
  164. choose_and_upload(num) {
  165. return new Promise(async (resolve, reject) => {
  166. // 选择图片
  167. let path_arr = await this.choose(num);
  168. // 上传图片
  169. await this.upload(path_arr).then((e) => {
  170. resolve(e);
  171. }).catch((e) => {
  172. reject(e)
  173. });
  174. })
  175. }
  176. };
  177. Uploader.choose_and_upload(1).then((e) => {
  178. resolve(e);
  179. }).catch((e) => {
  180. console.log(e);
  181. })
  182. })
  183. },
  184. };
  185. export default service