app.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. else {
  59. wx.getUserInfo({
  60. success: res => {
  61. self.globalData.userInfo = res.userInfo;
  62. }
  63. })
  64. }
  65. }
  66. else {
  67. wx.authorize({
  68. scope:'scope.userInfo',
  69. success:function(res){
  70. wx.getUserInfo({
  71. success: res => {
  72. // 可以将 res 发送给后台解码出 unionId
  73. self.globalData.userInfo = res.userInfo;
  74. self.globalData.userInfo.nickname = res.userInfo.nickName;
  75. let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
  76. wx.request({
  77. url: 'https://passport.lrlz.com/mobile/index.php?act=login&op=wxauthor&client_type=ios',
  78. method:'GET',
  79. data:{
  80. user_info: userInfo
  81. },
  82. success:function(res){
  83. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  84. if (res.statusCode == 200) {
  85. let current_url = "/" + getCurrentPages()[0].route;
  86. wx.reLaunch({
  87. url: current_url
  88. });
  89. }
  90. else {
  91. }
  92. }
  93. })
  94. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  95. // 所以此处加入 callback 以防止这种情况
  96. if (this.userInfoReadyCallback) {
  97. this.userInfoReadyCallback(res)
  98. }
  99. }
  100. })
  101. }
  102. });
  103. }
  104. }
  105. })
  106. },
  107. globalData: {
  108. userId:{
  109. openid:'',
  110. unionid:''
  111. },
  112. userInfo: {},
  113. defaultAddress: null
  114. },
  115. checkDefaultAddress(address) {
  116. this.globalData.defaultAddress = address
  117. }
  118. })