login.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {
  2. imgServerUrl
  3. } from '../../config/config.js'
  4. import {
  5. getUserInfo
  6. } from '../../utils/wx.js'
  7. import {
  8. updataStorageData
  9. } from '../../utils/storage.js'
  10. import {
  11. saveLogin
  12. } from '../../services/wx.js'
  13. var app = getApp()
  14. Page({
  15. data: {
  16. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  17. imgServerUrl: imgServerUrl
  18. },
  19. onLoad: function() {
  20. },
  21. bindGetUserInfo: function(e) {
  22. if (e.detail.userInfo) {
  23. //用户按了允许授权按钮
  24. getUserInfo().then(res => {
  25. if (!updataStorageData('city')) {
  26. updataStorageData('city', res.userInfo.city)
  27. }
  28. saveLogin({
  29. encryptedData: encodeURIComponent(res.encryptedData),
  30. iv: encodeURIComponent(res.iv),
  31. openid: app.globalData.openId,
  32. session_key: app.globalData.sessionKey
  33. }).then(data => {
  34. console.log(data)
  35. app.globalData.userId = data.data.hpUser.id ? data.data.hpUser.id : '';
  36. app.globalData.userToken = data.data.hpUser.userToken ? data.data.hpUser.userToken : '';
  37. app.globalData.userInfo = res.userInfo;
  38. updataStorageData('shareToken', data.data.hpUser.userToken || '') //用户识别码
  39. wx.setStorageSync("hpuser", data.data.hpUser);
  40. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  41. // 所以此处加入 callback 以防止这种情况
  42. if (app.userInfoReadyCallback) {
  43. app.userInfoReadyCallback(res)
  44. }
  45. }).catch(data => {
  46. console.log(data)
  47. })
  48. wx.navigateBack()
  49. }).catch(data => {
  50. console.log(data)
  51. })
  52. } else {
  53. //用户按了拒绝按钮
  54. wx.showModal({
  55. title: '提示',
  56. content: '您点击了拒绝授权,将无法正常使用该小程序,请授权之后再使用。',
  57. showCancel: false,
  58. confirmText: '返回授权',
  59. success: function(res) {
  60. if (res.confirm) {
  61. console.log('用户点击了“返回授权”')
  62. }
  63. }
  64. })
  65. }
  66. }
  67. })