my.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // pages/my/my.ts
  2. import { Login } from '../../api/login'
  3. import { getUserInfo } from '../../api/user'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. avatar: 'https://4dkk.4dage.com/miniapp-source/daikan/avatar_default.png',
  10. nickname: 'HI,游客',
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad() {
  16. },
  17. /**
  18. * 生命周期函数--监听页面初次渲染完成
  19. */
  20. onReady() {
  21. const app = getApp<IAppOption>();
  22. app.watch('userInfo', this.updateUserInfo)
  23. app.watch('isLogin', this.updateLoginStatus)
  24. },
  25. updateUserInfo(data?: any) {
  26. const app = getApp<IAppOption>();
  27. const updateUserInfo = app.globalData.userInfo
  28. const updateData = Object.assign({}, updateUserInfo, data)
  29. this.setData({
  30. userInfo: updateData
  31. })
  32. },
  33. async updateLoginStatus(data: boolean) {
  34. console.log('my页面-isLogin', data)
  35. this.setData({
  36. isLogin: data
  37. })
  38. if (data) {
  39. await getUserInfo();
  40. }
  41. },
  42. /**
  43. * 生命周期函数--监听页面显示
  44. */
  45. async onShow() {
  46. this.getTabBar().init();
  47. const app = getApp<IAppOption>();
  48. const isLogin = wx.getStorageSync('isLogin')
  49. app.setLogin(isLogin)
  50. this.updateUserInfo();
  51. },
  52. login() {
  53. const app = getApp<IAppOption>();
  54. wx.showLoading({
  55. title: "登录中..."
  56. })
  57. wx.login({
  58. success: async function (res) {
  59. if (res.code) {
  60. const code = res.code
  61. const result = await Login(code)
  62. if (result.token) {
  63. app.setLogin(true)
  64. wx.hideLoading();
  65. wx.setStorageSync('token', result.token)
  66. wx.setStorageSync('isLogin', true)
  67. wx.setStorageSync('wxUserId', result.wxUserId)
  68. }
  69. console.log('result', result)
  70. }
  71. }
  72. });
  73. },
  74. /**
  75. * 生命周期函数--监听页面隐藏
  76. */
  77. onHide() {
  78. },
  79. /**
  80. * 生命周期函数--监听页面卸载
  81. */
  82. onUnload() {
  83. },
  84. /**
  85. * 页面相关事件处理函数--监听用户下拉动作
  86. */
  87. onPullDownRefresh() {
  88. },
  89. /**
  90. * 页面上拉触底事件的处理函数
  91. */
  92. onReachBottom() {
  93. },
  94. /**
  95. * 用户点击右上角分享
  96. */
  97. onShareAppMessage() {
  98. },
  99. handleToPersonalEdit() {
  100. wx.navigateTo({
  101. url: "/pages/personal/personal"
  102. })
  103. }
  104. })