newRequest.js 4.6 KB

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