authorization.js 6.7 KB

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