app.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 展示本地存储能力
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. // 登录
  9. wx.login({
  10. success: res => {
  11. let self = this;
  12. wx.request({
  13. url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxfdaeb25e38c4c47e&secret=e5b5055cbc608d10c6de0d877c221270&js_code=${res.code}&grant_type=authorization_code`,
  14. data:{},
  15. success:function(res){
  16. self.globalData.userId.openid = res.data.openid;
  17. self.globalData.userId.unionid = res.data.unionid;
  18. }
  19. })
  20. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  21. }
  22. })
  23. // 获取用户信息
  24. wx.getSetting({
  25. success: res => {
  26. let self = this;
  27. if (res.authSetting['scope.userInfo']) {
  28. // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  29. // wx.getUserInfo({
  30. // success: res => {
  31. // // 可以将 res 发送给后台解码出 unionId
  32. // self.globalData.userInfo = res.userInfo
  33. // wx.request({
  34. // url: 'https://passport.lrlz.com/mobile/index.php?act=login&op=wxauthor&client_type=ios',
  35. // method: 'GET',
  36. // data: {
  37. // user_info: userInfo
  38. // },
  39. // success: function (res) {
  40. // wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  41. // if (res.statusCode == 200) {
  42. // wx.switchTab({
  43. // url: '/pages/index/index'
  44. // })
  45. // }
  46. // else {
  47. // }
  48. // }
  49. // })
  50. // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  51. // // 所以此处加入 callback 以防止这种情况
  52. // if (this.userInfoReadyCallback) {
  53. // this.userInfoReadyCallback(res)
  54. // }
  55. // }
  56. // })
  57. wx.authorize({
  58. scope: 'scope.userInfo',
  59. success: function (res) {
  60. wx.getUserInfo({
  61. success: res => {
  62. // 可以将 res 发送给后台解码出 unionId
  63. self.globalData.userInfo = res.userInfo;
  64. self.globalData.userInfo.nickname = res.userInfo.nickName;
  65. let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
  66. wx.request({
  67. url: 'https://passport.lrlz.com/mobile/index.php?act=login&op=wxauthor&client_type=ios',
  68. method: 'GET',
  69. data: {
  70. user_info: userInfo
  71. },
  72. success: function (res) {
  73. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  74. if (res.statusCode == 200) {
  75. wx.switchTab({
  76. url: '/pages/index/index'
  77. })
  78. }
  79. else {
  80. }
  81. }
  82. })
  83. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  84. // 所以此处加入 callback 以防止这种情况
  85. if (this.userInfoReadyCallback) {
  86. this.userInfoReadyCallback(res)
  87. }
  88. }
  89. })
  90. }
  91. });
  92. }
  93. else {
  94. wx.authorize({
  95. scope:'scope.userInfo',
  96. success:function(res){
  97. wx.getUserInfo({
  98. success: res => {
  99. // 可以将 res 发送给后台解码出 unionId
  100. self.globalData.userInfo = res.userInfo;
  101. self.globalData.userInfo.nickname = res.userInfo.nickName;
  102. let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
  103. wx.request({
  104. url: 'https://passport.lrlz.com/mobile/index.php?act=login&op=wxauthor&client_type=ios',
  105. method:'GET',
  106. data:{
  107. user_info: userInfo
  108. },
  109. success:function(res){
  110. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  111. if (res.statusCode == 200) {
  112. wx.switchTab({
  113. url: '/pages/index/index'
  114. })
  115. }
  116. else {
  117. }
  118. }
  119. })
  120. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  121. // 所以此处加入 callback 以防止这种情况
  122. if (this.userInfoReadyCallback) {
  123. this.userInfoReadyCallback(res)
  124. }
  125. }
  126. })
  127. }
  128. });
  129. }
  130. }
  131. })
  132. },
  133. globalData: {
  134. userId:{
  135. openid:'',
  136. unionid:''
  137. },
  138. userInfo: {}
  139. }
  140. })