http.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. export let api_url = null
  2. export function get(url, params, success = noop, fail = noop) {
  3. return http(url, 'GET', params, success, fail)
  4. }
  5. export function post(url, params, success = noop, fail = noop) {
  6. return http(url, 'POST', params, success, fail)
  7. }
  8. function noop() {}
  9. function http(url, method, params, success, fail) {
  10. // const type = wx.getStorageSync('env') || 'dev' // 测试环境
  11. const type = wx.getStorageSync('env') || 'ijolijoli' // 正式环境
  12. switch (type) {
  13. case 'dev':
  14. api_url = 'https://test-api-ads.tiantianqutao.com/'
  15. break
  16. default:
  17. api_url = 'https://test-api-ads.tiantianqutao.com/'
  18. break
  19. }
  20. return wx.request({
  21. url: `${api_url}${url}`,
  22. method: method,
  23. data: params,
  24. header: {
  25. token: wx.getStorageSync('token') || ''
  26. },
  27. success: ({ data, statusCode, header }) => {
  28. console.log(`==============${url}`, data)
  29. if (data.code == 200) {
  30. success(data, header)
  31. } else if (data.code == 201) {
  32. wx.showToast({
  33. title: data.msg,
  34. icon: 'none'
  35. })
  36. fail(data, header)
  37. } else if (data.code == 400) {
  38. console.log('登录失败')
  39. wx.showToast({
  40. title: data.msg,
  41. icon: 'none'
  42. })
  43. let pages = getCurrentPages()
  44. if (pages.length == 0) {
  45. wx.reLaunch({
  46. url: '/pages/login/login'
  47. })
  48. return
  49. }
  50. // if (pages[pages.length - 1].route == 'pages/deviceLogin/deviceLogin') {
  51. // wx.navigateTo({
  52. // url: '/pages/login/login?prePage=1'
  53. // })
  54. // } else {
  55. // wx.reLaunch({
  56. // url: '/pages/login/login'
  57. // })
  58. // }
  59. // wx.reLaunch({
  60. // url: '/pages/login/login',
  61. // })
  62. // login()
  63. }else{
  64. success(data, header)
  65. }
  66. },
  67. fail() {
  68. wx.showToast({
  69. title: '服务器异常 请稍后再试',
  70. icon: 'none'
  71. })
  72. }
  73. })
  74. }
  75. // 清理code
  76. export function clearWxLoginCode() {
  77. getApp().loginConfig = {
  78. code: '',
  79. codeTime: ''
  80. }
  81. }