app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const { $Message} = require('/component/iView/base/index');
  2. const md5 = require('./utils/md5.js');
  3. const secret= "117c0743819088468e590246464cc75e"
  4. App({
  5. globalData: {
  6. // baseAPI: "http://dev.ndjsxh.cn:8888/weixin/",
  7. baseAPI:"http://localhost:8000/weixin/",
  8. pageSize: 10,
  9. userInfo: {},
  10. },
  11. onLaunch: function() {
  12. },
  13. doLogin: function(cb){
  14. let _this = this
  15. wx.login({
  16. success(wxres) {
  17. if (wxres.code) {
  18. _this.formPost('Auth.wxLogin', {
  19. "code": wxres.code
  20. }).then(res => {
  21. if (res.code == 200) {
  22. _this.setUserInfo( res.data )
  23. cb && cb( res.data );
  24. } else if (res.code == 401) {
  25. wx.reLaunch({
  26. url: '/pages/user/register/index',
  27. });
  28. } else {
  29. _this.message(res.message, 'error')
  30. }
  31. }).catch(e => {
  32. _this.message(e, 'error')
  33. })
  34. } else {
  35. _this.message(res.errMsg, 'error')
  36. }
  37. }
  38. })
  39. },
  40. message: function(content, type) {
  41. $Message({
  42. content: content,
  43. type: type
  44. });
  45. },
  46. reLogin( cb ){
  47. this.globalData.userInfo = {};
  48. this.checkLogin( cb )
  49. },
  50. checkLogin: function(cb){
  51. let userInfo = this.globalData.userInfo;
  52. if (!userInfo || !userInfo.token) {
  53. this.doLogin( cb )
  54. }else{
  55. cb&&cb( userInfo )
  56. }
  57. },
  58. formPost: function(action, data) {
  59. let _this = this
  60. const token = wx.getStorageSync('token')
  61. const user_id = wx.getStorageSync('userId')||""
  62. let timestamp = Date.now()
  63. action = action.toLowerCase()
  64. let signstr=`weixin_${token}_${action}_${timestamp}_${secret}`
  65. let signsure = md5.md5( signstr ).toLowerCase()
  66. let headers = {
  67. 'Content-Type': 'application/json',
  68. 'x-signsure': signsure,
  69. 'x-timestamp': timestamp,
  70. 'x-userId': user_id
  71. }
  72. return new Promise(function(resolve, reject) {
  73. wx.showNavigationBarLoading();
  74. wx.request({
  75. url: _this.globalData.baseAPI + action,
  76. header: headers,
  77. method: 'POST',
  78. data,
  79. success(res) {
  80. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  81. reject('网络出错')
  82. return false;
  83. }
  84. if( res.data.code == 200){
  85. resolve(res.data);
  86. return true;
  87. }
  88. if (res.data.code === 401) {
  89. wx.reLaunch({
  90. url: '/pages/user/bind/index',
  91. });
  92. return false;
  93. }
  94. reject(res.data.msg)
  95. return false
  96. },
  97. fail(res) {
  98. reject(res.errMsg)
  99. return false;
  100. },
  101. complete(res) {
  102. wx.hideNavigationBarLoading();
  103. }
  104. })
  105. })
  106. },
  107. setUserInfo:function(userInfo){
  108. this.globalData.userInfo = userInfo;
  109. wx.setStorageSync('token', userInfo.token )
  110. wx.setStorageSync('userId', userInfo.userId)
  111. }
  112. })