app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const {
  2. $Message
  3. } = require('/component/iView/base/index');
  4. const mtjwxsdk = require('./utils/mtj-wx-sdk.js');
  5. App({
  6. globalData: {
  7. baseAPI: "http://localhost:8000",
  8. pageSize: 20
  9. },
  10. onLaunch: function() {
  11. let _this = this
  12. let token = wx.getStorageSync('token')
  13. if (null == token || token == '') {
  14. wx.login({
  15. success(wxres) {
  16. if (wxres.code) {
  17. _this.formPost('/api/wx/student/auth/checkBind', {
  18. "code": wxres.code
  19. }).then(res => {
  20. if (res.code == 1) {
  21. wx.setStorageSync('token', res.response)
  22. wx.reLaunch({
  23. url: '/pages/index/index',
  24. });
  25. } else if (res.code == 2) {
  26. wx.reLaunch({
  27. url: '/pages/user/bind/index',
  28. });
  29. } else {
  30. _this.message(res.message, 'error')
  31. }
  32. }).catch(e => {
  33. _this.message(e, 'error')
  34. })
  35. } else {
  36. _this.message(res.errMsg, 'error')
  37. }
  38. }
  39. })
  40. }
  41. },
  42. message: function(content, type) {
  43. $Message({
  44. content: content,
  45. type: type
  46. });
  47. },
  48. formPost: function(url, data) {
  49. let _this = this
  50. return new Promise(function(resolve, reject) {
  51. wx.showNavigationBarLoading();
  52. wx.request({
  53. url: _this.globalData.baseAPI + url,
  54. header: {
  55. 'content-type': 'application/x-www-form-urlencoded',
  56. 'token': wx.getStorageSync('token')
  57. },
  58. method: 'POST',
  59. data,
  60. success(res) {
  61. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  62. reject('网络出错')
  63. return false;
  64. }
  65. if (res.data.code === 400) {
  66. let token = res.data.response
  67. wx.setStorageSync('token', token)
  68. wx.request({
  69. url: _this.globalData.baseAPI + url,
  70. header: {
  71. 'content-type': 'application/x-www-form-urlencoded',
  72. 'token': wx.getStorageSync('token')
  73. },
  74. method: 'POST',
  75. data,
  76. success(result) {
  77. resolve(result.data);
  78. return true;
  79. }
  80. })
  81. } else if (res.data.code === 401) {
  82. wx.reLaunch({
  83. url: '/pages/user/bind/index',
  84. });
  85. return false;
  86. } else if (res.data.code === 500) {
  87. reject(res.data.message)
  88. return false;
  89. } else if (res.data.code === 501) {
  90. reject(res.data.message)
  91. return false;
  92. } else {
  93. resolve(res.data);
  94. return true;
  95. }
  96. },
  97. fail(res) {
  98. reject(res.errMsg)
  99. return false;
  100. },
  101. complete(res) {
  102. wx.hideNavigationBarLoading();
  103. }
  104. })
  105. })
  106. }
  107. })