app.js 4.9 KB

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