authorization.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. my.setNavigationBar({
  17. title: app.globalData.title
  18. })
  19. this.setData({
  20. logoUrl: app.globalData.logoUrl
  21. })
  22. my.removeStorageSync('token');
  23. my.getAuthCode({
  24. success: function (res) {
  25. my.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(app.globalData.appid, 'appid')
  73. var data = {
  74. 'code': res.authCode,
  75. 'appid': app.globalData.appid,
  76. 'type': 1
  77. };
  78. app.request("/auth/weapp-login", data, "POST").then(res => {
  79. console.log(res, '登录')
  80. my.setStorageSync({ 'key': 'token', data: res.data.token });
  81. my.setStorageSync({ 'key': 'session_key', data: res.data.session_key });
  82. my.setStorageSync({ 'key': 'token_time', data: res.data.exp });
  83. my.setStorageSync({ 'key': 'user_ID', data: res.data.user.id });
  84. that.setData({
  85. token: res.data.token,
  86. session_key: res.data.session_key
  87. })
  88. app.request("/user/status", '', "GET").then(res => {
  89. console.log(res)
  90. if (res.status == 200) {
  91. that.setData({
  92. bind_mobile: res.data.is_bind_mobile
  93. })
  94. }
  95. })
  96. })
  97. }
  98. })
  99. },
  100. onGotUserInfo: function (e) {
  101. console.log(e)
  102. if (e.detail.errMsg == "getUserInfo:fail auth deny") {
  103. my.showToast({
  104. content: '授权失败请重试',
  105. icon: 'none'
  106. })
  107. } else {
  108. my.showLoading({
  109. content: '登陆中请稍等...',
  110. mask: true
  111. })
  112. if (e.detail.rawData) {
  113. var data = e.detail.userInfo
  114. my.setStorageSync({ 'key': 'userInfo', data: data })
  115. if (my.getStorageSync({ 'key': 'home' }).data.id != undefined) {
  116. data.area_id = my.getStorageSync({ 'key': 'home' }).data.id
  117. }
  118. console.log(data)
  119. app.request("/auth/weapp-userinfo-sync", data, "POST").then(res => {
  120. if (res.status == 200) {
  121. my.showToast({
  122. content: '授权成功',
  123. icon: 'none'
  124. })
  125. my.setStorageSync({ 'key': 'userInfo', data: res.data })
  126. // my.navigateBack()
  127. if (app.globalData.compatible) {
  128. console.log(app.globalData.compatible)
  129. my.redirectTo({
  130. url: '/pages/compatible/index/index',
  131. })
  132. } else {
  133. my.redirectTo({
  134. url: '/pages/index/index',
  135. })
  136. }
  137. } else {
  138. my.showToast({
  139. content: '登陆失败请重试',
  140. icon: 'none'
  141. })
  142. my.redirectTo({
  143. url: '/pages/index/index',
  144. })
  145. }
  146. })
  147. } else {
  148. my.showToast({
  149. content: '授权失败',
  150. icon: 'none'
  151. })
  152. }
  153. }
  154. },
  155. onAuthError: function () {
  156. my.showToast({
  157. content: '授权失败请重试',
  158. type: 'none'
  159. });
  160. },
  161. getUserPhoneNumber: function (e) {
  162. console.log(e)
  163. my.getPhoneNumber({
  164. success: (res) => {
  165. console.log(res, "KKKKKKKKKKK")
  166. let encryptedData = JSON.parse(res.response).response;
  167. //userObject.phoneInfo = res.response;
  168. console.log(encryptedData);
  169. //手机号解密请求
  170. // phoneAuth(res.response);
  171. // my.alert({
  172. // title: encryptedData,
  173. // });4nbLCy+uBSvO8waRPGv3/EsHNqpa68oN8bPAyJqGjCT6UAVIaMuwChCdRsJkrsKRT3lGHdqyaET7YL2IptOLp7pZYW6w4wl9BHDLmx6V9kRindBZMBTYKzp7ov99o/E6rG3fZZCXSNA59aiKMojMA+JIzMRuo3GRBOcq0YEml6I=
  174. if (encryptedData.length == 172) return;
  175. let data = {
  176. phone_detail: JSON.stringify(arr),
  177. type: '3',
  178. session_key: encryptedData,
  179. auth_id: this.data.auth_id
  180. }
  181. app.request('/auth/mobileLogin', data, 'POST', app.globalData.req).then(res => {
  182. console.log(res, "PPPPppp")
  183. my.setStorageSync({ key: 'userInfo', data: res.data.user })
  184. my.setStorageSync({ key: 'token', data: res.data.token });
  185. console.log(my.getStorageSync({ key: 'token' }))
  186. // my.setStorageSync('token', res.data.token)
  187. // my.setStorageSync('token_time', res.data.exp)
  188. // my.setStorageSync('userInfo', res.data.user)
  189. my.reLaunch({
  190. url: '/pages/index/index',
  191. })
  192. })
  193. },
  194. fail: (res) => {
  195. console.log(res, "error");
  196. }
  197. })
  198. },
  199. //手机号登陆
  200. phoneLogin: function () {
  201. my.navigateTo({
  202. url: '/pages/phonelogin/phonelogin',
  203. })
  204. },
  205. //暂不登录
  206. noSign: function () {
  207. my.reLaunch({
  208. url: '/pages/index/index',
  209. })
  210. },
  211. onReady: function () {
  212. },
  213. onShow: function () {
  214. my.hideBackHome()
  215. },
  216. onHide: function () {
  217. },
  218. onUnload: function () {
  219. },
  220. onPullDownRefresh: function () {
  221. },
  222. onReachBottom: function () {
  223. },
  224. onShareAppMessage: function () {
  225. }
  226. })