http.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var config = require('../config/config.js')
  2. import {
  3. showToast
  4. } from '../utils/tips.js'
  5. //model状态
  6. let model = true;
  7. let loading = true;
  8. //服务器地址
  9. const apiUrl = config.apiUrl;
  10. /**
  11. * 处理请求数据
  12. */
  13. const getData = (data) => {
  14. const _data = Object.assign(data, {
  15. user_token: wx.getStorageSync('user_token'),
  16. user_id: wx.getStorageSync('user_id'),
  17. member_id: wx.getStorageSync('member_id'),
  18. })
  19. for (let key in _data) {
  20. if (!_data[key] && _data[key] !== 0) {
  21. delete _data[key]
  22. }
  23. }
  24. console.log('data=>', _data)
  25. return _data;
  26. }
  27. // const getUrl = function(url) {
  28. // if (url.indexOf('https://') || url.indexOf('https://')) {
  29. // }
  30. // }
  31. /**
  32. * 封装http 请求方法
  33. */
  34. const http = (params) => {
  35. // console.log(params)
  36. //返回promise 对象
  37. return new Promise((resolve, reject) => {
  38. // wx.showToast({
  39. // icon: 'loading',
  40. // title: '加载中...',
  41. // mask: true,
  42. // duration: 10000,
  43. // success: res => {
  44. // console.log(new Date())
  45. // loading = false
  46. // }
  47. // })
  48. if (loading) {
  49. }
  50. wx.showLoading({
  51. title: 'loading',
  52. mask: true,
  53. success: res => {
  54. loading = false
  55. }
  56. });
  57. console.log(params.url)
  58. wx.request({
  59. url: apiUrl + params.url, //服务器url+参数中携带的接口具体地址
  60. data: getData(params.data), //请求参数
  61. header: Object.assign({
  62. "Content-Type": "application/json" //设置后端需要的常用的格式就好,特殊情况调用的时候单独设置
  63. }, params.header || {}),
  64. method: params.method || 'GET', //默认为GET,可以不写,如常用请求格式为POST,可以设置POST为默认请求方式
  65. dataType: params.dataType, //返回的数据格式,默认为JSON,特殊格式可以在调用的时候传入参数
  66. responseType: params.responseType, //响应的数据类型
  67. success: function(res) {
  68. console.log("res=>", res.data)
  69. wx.hideLoading({
  70. success: res => {
  71. loading = true
  72. }
  73. });
  74. if (res.statusCode == 200) {
  75. var errorCode = res.data.errcode
  76. if (errorCode == 0) {
  77. // wx.hideToast()
  78. // wx.showToast({
  79. // icon: 'loading',
  80. // title: '加载中...',
  81. // duration: 0
  82. // })
  83. return resolve(res.data)
  84. } else if (errorCode == 1014) {
  85. wx.setStorageSync('openId', res.data.data.openId);
  86. wx.setStorageSync('sessionKey', res.data.data.sessionKey);
  87. wx.setStorageSync('unionId', res.data.data.unionId);
  88. //前往绑定手机号
  89. wx.redirectTo({
  90. url: '/pages/bind-phone/index',
  91. })
  92. } else if (errorCode == 1005) {
  93. //未获取到微信登录信息
  94. wx.navigateTo({
  95. url: '/pages/login/login',
  96. })
  97. } else if (errorCode == 2009) {
  98. //用户信息重复,请选择用户信息
  99. wx.setStorageSync('userList', res.data.data.userList);
  100. wx.setStorageSync('currentUser', res.data.data.currentUser);
  101. wx.redirectTo({
  102. url: '/pages/select-info/index',
  103. })
  104. } else if (errorCode == 2008) {
  105. //账号不存在,或token无效
  106. if (model && params.data.task_id !== 10) {
  107. wx.reLaunch({
  108. url: '/pages/login/login',
  109. success: res => {
  110. wx.showModal({
  111. title: '提示',
  112. content: '登录失效请重新登录',
  113. showCancel: false,
  114. success: res => {
  115. if (res.confirm) {
  116. model = true;
  117. }
  118. }
  119. })
  120. }
  121. })
  122. model = false
  123. }
  124. return
  125. } else if (errorCode == 1007) {
  126. //手机号已被绑定
  127. } else if (errorCode == 40005) {
  128. //用户信息和微信信息不匹配
  129. } else if (errorCode == 1015) {
  130. //用户尚未创建简历
  131. return reject(res.data);
  132. } else if (errorCode == 40007) {
  133. //账号类型不符
  134. } else if (errorCode == 2006) {
  135. //后台接口异常
  136. }
  137. params.data.task_id !== 10 && showToast(res.data.errmsg);
  138. reject(res.data);
  139. } else {
  140. //2. 操作不成功返回数据,以toast方式弹出响应信息,如后端未格式化非操作成功异常信息,则可以统一定义异常提示
  141. wx.showToast({
  142. icon: "none",
  143. title: "网络异常",
  144. success: () => {
  145. reject()
  146. }
  147. })
  148. }
  149. },
  150. fail: function(e) {
  151. console.log('error=>', e)
  152. wx.showToast({
  153. icon: "none",
  154. title: "网络异常",
  155. success: () => {
  156. loading = true
  157. reject()
  158. }
  159. })
  160. },
  161. complete: () => {
  162. // wx.hideLoading({
  163. // success: res => {
  164. // loading = true
  165. // }
  166. // })
  167. // wx.showToast({
  168. // icon: 'loading',
  169. // title: 'loading...',
  170. // duration: 0
  171. // })
  172. // wx.hideToast({
  173. // success: res => {
  174. // console.log(new Date())
  175. // console.log('complete close')
  176. // }
  177. // })
  178. wx.stopPullDownRefresh();
  179. }
  180. })
  181. })
  182. };
  183. // function _request(options) {
  184. // wx.request({
  185. // url: options.url,
  186. // data: options.data,
  187. // header: {
  188. // "Content-Type": "application/json",
  189. // ...options.header
  190. // },
  191. // method: options.method,
  192. // dataType: options.dataType || 'json',
  193. // responseType: options.responseType || 'text',
  194. // success: function(res) {
  195. // options.success && options.success(res)
  196. // },
  197. // fail: function(res) {
  198. // options.fail && options.fail(res)
  199. // },
  200. // complete: function(res) {
  201. // options.complete && options.complete(res)
  202. // },
  203. // })
  204. // }
  205. module.exports = {
  206. http
  207. }