app.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. self.globalData.userId.openid = res.data.datas.openid;
  21. self.globalData.userId.unionid = res.data.datas.unionid;
  22. // self.globalData.userId.openid = res.data.openid;
  23. // self.globalData.userId.unionid = res.data.unionid;
  24. // 获取用户信息
  25. wx.getSetting({
  26. success: res => {
  27. if (res.authSetting['scope.userInfo']) {
  28. wx.getUserInfo({
  29. success: res => {
  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: self.host,
  35. method: 'GET',
  36. data: {
  37. user_info: userInfo,
  38. act: "login",
  39. op: "wxauthen",
  40. // op: "wxauthor",
  41. client_type: "ios"
  42. },
  43. success: function (res) {
  44. console.log(res.data.datas.HPHPSESSID);
  45. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  46. if (res.statusCode == 200) {
  47. let current_url = "/" + getCurrentPages()[0].route;
  48. wx.reLaunch({
  49. url: current_url
  50. });
  51. }
  52. else {
  53. }
  54. }
  55. })
  56. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  57. // 所以此处加入 callback 以防止这种情况
  58. if (this.userInfoReadyCallback) {
  59. this.userInfoReadyCallback(res)
  60. }
  61. }
  62. })
  63. }
  64. else {
  65. wx.authorize({
  66. scope: 'scope.userInfo',
  67. success: function (res) {
  68. wx.getUserInfo({
  69. success: res => {
  70. // 可以将 res 发送给后台解码出 unionId
  71. self.globalData.userInfo = res.userInfo;
  72. self.globalData.userInfo.nickname = res.userInfo.nickName;
  73. let userInfo = Object.assign({}, self.globalData.userId, self.globalData.userInfo);
  74. // console.log(userInfo);
  75. wx.request({
  76. url: self.host,
  77. method: 'GET',
  78. data: {
  79. user_info: userInfo,
  80. act: "login",
  81. op: "wxauthen",
  82. // op: "wxauthor",
  83. client_type: "ios"
  84. },
  85. success: function (res) {
  86. // console.log(res.data.datas.HPHPSESSID)
  87. wx.setStorageSync('session_id', res.data.datas.HPHPSESSID);
  88. if (res.statusCode == 200) {
  89. let current_url = "/" + getCurrentPages()[0].route;
  90. wx.reLaunch({
  91. url: current_url
  92. });
  93. }
  94. else {
  95. }
  96. }
  97. })
  98. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  99. // 所以此处加入 callback 以防止这种情况
  100. if (this.userInfoReadyCallback) {
  101. this.userInfoReadyCallback(res)
  102. }
  103. }
  104. })
  105. }
  106. });
  107. }
  108. }
  109. })
  110. }
  111. })
  112. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  113. }
  114. })
  115. },
  116. globalData: {
  117. userId:{
  118. openid:'',
  119. unionid:''
  120. },
  121. userInfo: null,
  122. defaultAddress: null,
  123. fcodeErr: '',
  124. backLogin:false,
  125. arrayTree: []
  126. },
  127. checkDefaultAddress(address) {
  128. this.globalData.defaultAddress = address
  129. },
  130. clearAddress() {
  131. this.globalData.defaultAddress = null
  132. },
  133. setArrayTree(tree) {
  134. this.globalData.arrayTree = tree
  135. }
  136. })