authentication.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const app = getApp()
  2. var http = require("../../utils/config.js");
  3. Page({
  4. data: {
  5. //测试.
  6. versions: false,
  7. token:''
  8. },
  9. onLoad: function (options) {
  10. var that = this;
  11. wx.removeStorageSync('token');
  12. wx.login({
  13. success: function (res) {
  14. var arr = [];
  15. wx.getSystemInfo({
  16. success(res) {
  17. console.log(arr)
  18. arr = [{
  19. label: '品牌',
  20. type: 'brand',
  21. value: res.brand
  22. },
  23. {
  24. label: '型号',
  25. type: 'model',
  26. value: res.model
  27. },
  28. {
  29. label: '系统平台',
  30. type: 'platform',
  31. value: res.platform
  32. },
  33. {
  34. label: '系统版本',
  35. type: 'system',
  36. value: res.system
  37. },
  38. {
  39. label: '微信版本',
  40. type: 'version',
  41. value: res.version
  42. },
  43. {
  44. label: '小程序版本',
  45. type: 'SDKVersion',
  46. value: res.SDKVersion
  47. },
  48. {
  49. label: '定位状态',
  50. type: 'locationEnabled',
  51. value: res.locationEnabled ? '开' : '关'
  52. },
  53. {
  54. label: '定位授权',
  55. type: 'locationAuthorized',
  56. value: res.locationAuthorized ? '已授权' : '未授权'
  57. }
  58. ]
  59. }
  60. })
  61. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  62. wx.request({
  63. url: http.url + '/auth/weapp-login',
  64. data: {
  65. 'code': res.code,
  66. 'appid': 'wx573623a9b7a7ed34',
  67. 'phone_detail': JSON.stringify(arr),
  68. 'invite_user_id':app.globalData.id
  69. },
  70. header: {
  71. 'content-type': 'application/x-www-form-urlencoded',
  72. },
  73. method: 'POST',
  74. success: function (res) {
  75. console.log(res)
  76. wx.setStorageSync('token', res.data.token);
  77. wx.setStorageSync('session_key', res.data.session_key);
  78. wx.setStorageSync('token_time', res.data.exp);
  79. wx.setStorageSync('user_ID', res.data.user.id);
  80. that.setData({token:res.data.token})
  81. }
  82. })
  83. }
  84. })
  85. },
  86. onGotUserInfo: function (e) {
  87. console.log(e)
  88. wx.showLoading({
  89. title: '登陆中请稍等...',
  90. mask: true
  91. })
  92. var that = this;
  93. if (e.detail.rawData) {
  94. var data = e.detail.userInfo
  95. wx.setStorageSync('userInfo', data)
  96. if (wx.getStorageSync('home').id != undefined) {
  97. data.area_id = wx.getStorageSync('home').id
  98. }
  99. console.log(data)
  100. wx.request({
  101. url: http.url + '/auth/weapp-userinfo-sync',
  102. data: data,
  103. header: {
  104. 'content-type': 'application/x-www-form-urlencoded',
  105. 'Authorization':this.data.token
  106. },
  107. method: 'POST',
  108. success: function (res) {
  109. if (res.statusCode == 200) {
  110. wx.showToast({
  111. title: '授权成功',
  112. icon: 'none'
  113. })
  114. wx.setStorageSync('userInfo', res.data)
  115. // wx.navigateBack()
  116. if(app.globalData.compatible){
  117. console.log(app.globalData.compatible)
  118. wx.redirectTo({
  119. url: '/pages/compatible/index/index',
  120. })
  121. }else{
  122. wx.redirectTo({
  123. url: '/pages/index/index',
  124. })
  125. }
  126. } else {
  127. wx.showToast({
  128. title: '登陆失败请重试',
  129. icon: 'none'
  130. })
  131. }
  132. }
  133. })
  134. } else {
  135. wx.showToast({
  136. title: '授权失败',
  137. icon: 'none'
  138. })
  139. }
  140. },
  141. agreement: function () {
  142. wx.navigateTo({
  143. url: '/pages/personal/privacy/privacy',
  144. })
  145. },
  146. mobile_login(){
  147. wx.navigateTo({
  148. url: '/pages/mobile_login/mobile_login',
  149. })
  150. },
  151. //用户隐私协议
  152. handleAgreePrivacyAuthorization(){},
  153. onReady: function () {
  154. },
  155. onShow: function () {
  156. wx.hideHomeButton()
  157. },
  158. onHide: function () {
  159. },
  160. onUnload: function () {
  161. },
  162. onPullDownRefresh: function () {
  163. },
  164. onReachBottom: function () {
  165. },
  166. onShareAppMessage: function () {
  167. }
  168. })