app.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. App({
  2. onLaunch: function () {
  3. // 展示本地存储能力
  4. wx.login({
  5. success: function (res) {
  6. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  7. console.log(res.code)
  8. var data = {
  9. 'code': res.code
  10. };
  11. wx.request({
  12. url: 'https://yutang.web.ximengnaikang.com/api/login',
  13. method: 'POST',
  14. data:data,
  15. header: {
  16. 'content-type': 'application/x-www-form-urlencoded'
  17. },
  18. success: function (res) {
  19. console.log(res)
  20. wx.setStorageSync('token', res.data.data.authorize)
  21. wx.setStorageSync('userInfo', res.data.data.user)
  22. wx.setStorageSync('session_key', res.data.data.session_key)
  23. }
  24. })
  25. }
  26. })
  27. },
  28. globalData: {
  29. userInfo: null
  30. },
  31. request(api, params, method, req1) {
  32. var that = this;
  33. // console.log(req1)
  34. if (req1 == false) return;
  35. if (req1) {
  36. that.globalData.req = false;
  37. }
  38. if (wx.getStorageSync('token') != '' || wx.getStorageSync('token') != undefined) {
  39. if (req1 == true || req1 == undefined) {
  40. return new Promise((resolve, reject) => {
  41. wx.request({
  42. url: this.globalData.url + api,
  43. data: params,
  44. header: {
  45. 'content-type': 'application/x-www-form-urlencoded',
  46. 'Authorization': wx.getStorageSync('token'),
  47. 'merchant-id': this.globalData.merchant_id
  48. },
  49. method: method,
  50. success: (res) => {
  51. resolve(res)
  52. if (res.statusCode == 450) {
  53. return;
  54. }
  55. if (res.data.statusCode != 200) {
  56. if (res.message != undefined) {
  57. wx.showToast({
  58. title: res.message,
  59. icon: 'none',
  60. duration: 3000,
  61. mask: true
  62. })
  63. } else if (res.data.message != undefined) {
  64. wx.showToast({
  65. title: res.data.message,
  66. icon: 'none',
  67. duration: 3000,
  68. mask: true
  69. })
  70. }
  71. } else if (res.statusCode != 200) {
  72. if (res.message != undefined) {
  73. wx.showToast({
  74. title: res.message,
  75. icon: 'none',
  76. duration: 3000,
  77. mask: true
  78. })
  79. } else if (res.data.message != undefined) {
  80. wx.showToast({
  81. title: res.data.message,
  82. icon: 'none',
  83. duration: 3000,
  84. mask: true
  85. })
  86. }
  87. }
  88. },
  89. fail: (err) => {
  90. wx.showToast({
  91. title: err,
  92. icon: 'none',
  93. mask: true
  94. });
  95. reject("请求失败")
  96. },
  97. complete: () => {
  98. that.globalData.req = true
  99. }
  100. })
  101. })
  102. }
  103. }
  104. },
  105. })