authentication.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. const app = getApp()
  2. var arr = [];
  3. Page({
  4. data: {
  5. //测试.
  6. versions: false,
  7. token: '',
  8. img: app.globalData.imgUrl,
  9. title: app.globalData.title,
  10. logoUrl: '',
  11. bind_mobile: 0,
  12. session_key:''
  13. },
  14. onLoad: function (options) {
  15. var that = this;
  16. wx.setNavigationBarTitle({
  17. title: app.globalData.title
  18. })
  19. this.setData({
  20. logoUrl: app.globalData.logoUrl
  21. })
  22. wx.removeStorageSync('token');
  23. wx.login({
  24. success: function (res) {
  25. wx.getSystemInfo({
  26. success(res) {
  27. console.log(arr)
  28. arr = [{
  29. label: '品牌',
  30. type: 'brand',
  31. value: res.brand
  32. },
  33. {
  34. label: '型号',
  35. type: 'model',
  36. value: res.model
  37. },
  38. {
  39. label: '系统平台',
  40. type: 'platform',
  41. value: res.platform
  42. },
  43. {
  44. label: '系统版本',
  45. type: 'system',
  46. value: res.system
  47. },
  48. {
  49. label: '微信版本',
  50. type: 'version',
  51. value: res.version
  52. },
  53. {
  54. label: '小程序版本',
  55. type: 'SDKVersion',
  56. value: res.SDKVersion
  57. },
  58. {
  59. label: '定位状态',
  60. type: 'locationEnabled',
  61. value: res.locationEnabled ? '开' : '关'
  62. },
  63. {
  64. label: '定位授权',
  65. type: 'locationAuthorized',
  66. value: res.locationAuthorized ? '已授权' : '未授权'
  67. }
  68. ]
  69. }
  70. })
  71. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  72. console.log(res.code)
  73. var data = {
  74. 'code': res.code,
  75. 'appid': app.globalData.appid,
  76. 'phone_detail': JSON.stringify(arr),
  77. 'invite_user_id': app.globalData.id,
  78. 'language':'zh'
  79. };
  80. app.request("/auth/weapp-login", data, "POST").then(res => {
  81. console.log(res)
  82. wx.setStorageSync('token', res.data.token);
  83. wx.setStorageSync('session_key', res.data.session_key);
  84. wx.setStorageSync('token_time', res.data.exp);
  85. wx.setStorageSync('user_ID', res.data.user.id);
  86. that.setData({
  87. token: res.data.token,
  88. session_key:res.data.session_key
  89. })
  90. app.request("/user/status", '', "GET").then(res => {
  91. console.log(res)
  92. if (res.statusCode == 200) {
  93. that.setData({
  94. bind_mobile: res.data.is_bind_mobile
  95. })
  96. }
  97. })
  98. })
  99. }
  100. })
  101. },
  102. onGotUserInfo: function (e) {
  103. console.log(e)
  104. if (e.detail.errMsg == "getUserInfo:fail auth deny") {
  105. wx.showToast({
  106. title: '授权失败请重试',
  107. icon: 'none'
  108. })
  109. } else {
  110. wx.showLoading({
  111. title: '登陆中请稍等...',
  112. mask: true
  113. })
  114. if (e.detail.rawData) {
  115. var data = e.detail.userInfo
  116. wx.setStorageSync('userInfo', data)
  117. if (wx.getStorageSync('home').id != undefined) {
  118. data.area_id = wx.getStorageSync('home').id
  119. }
  120. console.log(data)
  121. app.request("/auth/weapp-userinfo-sync", data, "POST").then(res => {
  122. if (res.statusCode == 200) {
  123. wx.showToast({
  124. title: '授权成功',
  125. icon: 'none'
  126. })
  127. wx.setStorageSync('userInfo', res.data)
  128. // wx.navigateBack()
  129. if (app.globalData.compatible) {
  130. console.log(app.globalData.compatible)
  131. wx.redirectTo({
  132. url: '/pages/compatible/index/index',
  133. })
  134. } else {
  135. wx.redirectTo({
  136. url: '/pages/index/index',
  137. })
  138. }
  139. } else {
  140. wx.showToast({
  141. title: '登陆失败请重试',
  142. icon: 'none'
  143. })
  144. wx.redirectTo({
  145. url: '/pages/index/index',
  146. })
  147. }
  148. })
  149. } else {
  150. wx.showToast({
  151. title: '授权失败',
  152. icon: 'none'
  153. })
  154. }
  155. }
  156. },
  157. getPhoneNumber: function (e) {
  158. var that = this;
  159. console.log(e)
  160. if (e.detail.errMsg == "getPhoneNumber:fail user deny" || e.detail.iv==undefined) {
  161. wx.showToast({
  162. title: '授权失败请重试',
  163. icon: 'none'
  164. })
  165. } else {
  166. let data = {
  167. phone_detail: JSON.stringify(arr),
  168. type: 1,
  169. session_key: that.data.session_key,
  170. iv: e.detail.iv,
  171. encryptedData: e.detail.encryptedData
  172. }
  173. app.request('/auth/mobileLogin', data, 'POST', app.globalData.req).then(res => {
  174. if (res.statusCode == 200) {
  175. console.log(res)
  176. wx.setStorageSync('userInfo', res.data.user)
  177. wx.setStorageSync('token', res.data.token);
  178. if (app.globalData.compatible) {
  179. wx.reLaunch({
  180. url: '/pages/compatible/index/index',
  181. })
  182. } else {
  183. wx.reLaunch({
  184. url: '/pages/index/index',
  185. })
  186. }
  187. }
  188. })
  189. }
  190. },
  191. agreement: function () {
  192. wx.navigateTo({
  193. url: '/pages/personal/privacy/privacy',
  194. })
  195. },
  196. mobile_login() {
  197. wx.navigateTo({
  198. url: '/pages/mobile_login/mobile_login',
  199. })
  200. },
  201. onReady: function () {
  202. },
  203. onShow: function () {
  204. wx.hideHomeButton()
  205. },
  206. onHide: function () {
  207. },
  208. onUnload: function () {
  209. },
  210. onPullDownRefresh: function () {
  211. },
  212. onReachBottom: function () {
  213. },
  214. onShareAppMessage: function () {
  215. }
  216. })