app.js 5.5 KB

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