app.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //app.js
  2. const api = require('./config.js');
  3. App({
  4. host: api.host,
  5. onLaunch: function () {
  6. let self = this;
  7. // 登录
  8. wx.login({
  9. success: res => {
  10. wx.request({
  11. url: self.host,
  12. data: {
  13. act: "login",
  14. op: "ministart",
  15. code: res.code,
  16. client_type: "mini"
  17. },
  18. success: function (res) {
  19. self.globalData.userId.openid = res.data.datas.openid;
  20. self.globalData.userId.unionid = res.data.datas.unionid;
  21. // 获取用户信息
  22. wx.getSetting({
  23. success: res => {
  24. if (res.authSetting['scope.userInfo']) {
  25. wx.getUserInfo({
  26. success: res => {
  27. self.globalData.userInfo = res.userInfo;
  28. self.globalData.userInfo.nickname = res.userInfo.nickName;
  29. let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
  30. wx.request({
  31. url: self.host,
  32. method: 'GET',
  33. data: {
  34. user_info: userInfo,
  35. act: "login",
  36. op: "wxauthen",
  37. client_type: "mini"
  38. },
  39. success: function (res) {
  40. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  41. if (res.statusCode == 200) {
  42. let len = getCurrentPages().length
  43. let currentPage = getCurrentPages()[len - 1]
  44. let params = ''
  45. if (currentPage.options) {
  46. for (let k in currentPage.options) {
  47. params = params + k + '=' + currentPage.options[k] + '&'
  48. }
  49. params = params.slice(0, params.length - 1)
  50. }
  51. let current_url = "/" + currentPage['route']
  52. if (params) {
  53. current_url = current_url + '?' + params
  54. }
  55. console.log('current_url:', current_url);
  56. wx.reLaunch({
  57. url: current_url
  58. });
  59. }
  60. else {
  61. }
  62. }
  63. })
  64. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  65. // 所以此处加入 callback 以防止这种情况
  66. if (this.userInfoReadyCallback) {
  67. this.userInfoReadyCallback(res)
  68. }
  69. }
  70. })
  71. }
  72. else {
  73. let len = getCurrentPages().length
  74. let currentPage = getCurrentPages()[len - 1].route
  75. console.log('currentPage:', getCurrentPages()[len - 1]);
  76. if(currentPage.indexOf('shareBonus/shareBonus') != -1) {
  77. wx.navigateTo({
  78. url: `/pages/login/login`
  79. });
  80. return;
  81. }
  82. return;
  83. }
  84. }
  85. })
  86. }
  87. })
  88. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  89. }
  90. })
  91. },
  92. globalData: {
  93. userId: {
  94. openid: '',
  95. unionid: ''
  96. },
  97. userInfo: null,
  98. defaultAddress: null,
  99. fcodeErr: '',
  100. backLogin: false,
  101. arrayTree: [],
  102. fromSource: ''
  103. },
  104. setFromSource(fromSource) {
  105. this.globalData.fromSource = fromSource
  106. },
  107. checkDefaultAddress(address) {
  108. this.globalData.defaultAddress = address
  109. },
  110. clearAddress() {
  111. this.globalData.defaultAddress = null
  112. },
  113. setArrayTree(tree) {
  114. this.globalData.arrayTree = tree
  115. }
  116. })