http.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var config = require('../config/config.js')
  2. import {
  3. showToast
  4. } from '../utils/tips.js'
  5. //服务器地址
  6. const apiUrl = config.apiUrl;
  7. /**
  8. * 封装http 请求方法
  9. */
  10. const http = (params) => {
  11. // console.log(params)
  12. //返回promise 对象
  13. wx.showLoading({
  14. title: 'loading',
  15. mask: true
  16. });
  17. return new Promise((resolve, reject) => {
  18. wx.request({
  19. url: apiUrl + params.url, //服务器url+参数中携带的接口具体地址
  20. data: params.data, //请求参数
  21. header: Object.assign({
  22. "Content-Type": "application/json" //设置后端需要的常用的格式就好,特殊情况调用的时候单独设置
  23. }, params.header || {}),
  24. method: params.method || 'GET', //默认为GET,可以不写,如常用请求格式为POST,可以设置POST为默认请求方式
  25. dataType: params.dataType, //返回的数据格式,默认为JSON,特殊格式可以在调用的时候传入参数
  26. responseType: params.responseType, //响应的数据类型
  27. success: function(res) {
  28. console.log("11", res.data)
  29. wx.hideLoading()
  30. if (res.statusCode == 200) {
  31. var errorCode = res.data.errcode
  32. if (errorCode == 0) {
  33. return resolve(res.data)
  34. } else if (errorCode == 1020) {
  35. //未获取到用户手机信息
  36. wx.navigateTo({
  37. url: '/pages/auth/auth',
  38. })
  39. } else if (errorCode == 1014) {
  40. wx.setStorageSync('openId', res.data.data.openId);
  41. wx.setStorageSync('sessionKey', res.data.data.sessionKey);
  42. wx.setStorageSync('unionId', res.data.data.unionId);
  43. //前往绑定手机号
  44. wx.redirectTo({
  45. url: '/pages/bind-phone/index',
  46. })
  47. } else if (errorCode == 1005) {
  48. //未获取到微信登录信息
  49. wx.navigateTo({
  50. url: '/pages/login/login',
  51. })
  52. } else if (errorCode == 2008) {
  53. //账号不存在,或token无效
  54. } else if (errorCode == 1007) {
  55. //手机号已被绑定
  56. } else if (errorCode == 40005) {
  57. //用户信息和微信信息不匹配
  58. } else if (errorCode == 1015) {
  59. //用户尚未创建简历
  60. var targetUrl = wx.getStorageSync('resumeUrl')
  61. if (!targetUrl) {
  62. targetUrl = '/pages/user-info/user-info'
  63. }
  64. // wx.navigateTo({
  65. // url: targetUrl,
  66. // })
  67. } else if (errorCode == 40007) {
  68. //账号类型不符
  69. } else if (errorCode == 2006) {
  70. //后台接口异常
  71. }
  72. showToast(res.data.errmsg);
  73. reject(res.data);
  74. } else {
  75. //2. 操作不成功返回数据,以toast方式弹出响应信息,如后端未格式化非操作成功异常信息,则可以统一定义异常提示
  76. wx.showToast({
  77. icon: "none",
  78. title: "网络异常"
  79. })
  80. }
  81. },
  82. fail: function(e) {
  83. wx.hideLoading()
  84. console.log(e)
  85. wx.showToast({
  86. icon: "none",
  87. title: "网络异常"
  88. })
  89. }
  90. })
  91. })
  92. }
  93. module.exports = {
  94. http
  95. }