app.js 5.0 KB

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