app.js 4.2 KB

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