app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 登录
  5. wx.login({
  6. success: res => {
  7. let self = this;
  8. wx.request({
  9. url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxfdaeb25e38c4c47e&secret=e5b5055cbc608d10c6de0d877c221270&js_code=${res.code}&grant_type=authorization_code`,
  10. data:{},
  11. success:function(res){
  12. self.globalData.userId.openid = res.data.openid;
  13. self.globalData.userId.unionid = res.data.unionid;
  14. }
  15. })
  16. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  17. }
  18. })
  19. // 获取用户信息
  20. wx.getSetting({
  21. success: res => {
  22. let self = this;
  23. if (res.authSetting['scope.userInfo']) {
  24. let local_session_id = wx.getStorageSync('session_id');
  25. if (local_session_id == '') {
  26. wx.getUserInfo({
  27. success: res => {
  28. // 可以将 res 发送给后台解码出 unionId
  29. self.globalData.userInfo = res.userInfo;
  30. self.globalData.userInfo.nickname = res.userInfo.nickName;
  31. let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
  32. wx.request({
  33. url: 'https://passport.lrlz.com/mobile/index.php?act=login&op=wxauthor&client_type=ios',
  34. method: 'GET',
  35. data: {
  36. user_info: userInfo
  37. },
  38. success: function (res) {
  39. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  40. if (res.statusCode == 200) {
  41. let current_url = "/" + getCurrentPages()[0].route;
  42. wx.reLaunch({
  43. url: current_url
  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. }
  58. }
  59. else {
  60. wx.authorize({
  61. scope:'scope.userInfo',
  62. success:function(res){
  63. wx.getUserInfo({
  64. success: res => {
  65. // 可以将 res 发送给后台解码出 unionId
  66. self.globalData.userInfo = res.userInfo;
  67. self.globalData.userInfo.nickname = res.userInfo.nickName;
  68. let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
  69. wx.request({
  70. url: 'https://passport.lrlz.com/mobile/index.php?act=login&op=wxauthor&client_type=ios',
  71. method:'GET',
  72. data:{
  73. user_info: userInfo
  74. },
  75. success:function(res){
  76. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  77. if (res.statusCode == 200) {
  78. let current_url = "/" + getCurrentPages()[0].route;
  79. wx.reLaunch({
  80. url: current_url
  81. });
  82. }
  83. else {
  84. }
  85. }
  86. })
  87. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  88. // 所以此处加入 callback 以防止这种情况
  89. if (this.userInfoReadyCallback) {
  90. this.userInfoReadyCallback(res)
  91. }
  92. }
  93. })
  94. }
  95. });
  96. }
  97. }
  98. })
  99. },
  100. globalData: {
  101. userId:{
  102. openid:'',
  103. unionid:''
  104. },
  105. userInfo: {}
  106. }
  107. })