index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // index.ts
  2. // 获取应用实例
  3. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  4. Component({
  5. data: {
  6. motto: 'Hello World',
  7. userInfo: {
  8. avatarUrl: defaultAvatarUrl,
  9. nickName: '',
  10. },
  11. hasUserInfo: false,
  12. canIUseGetUserProfile: wx.canIUse('getUserProfile'),
  13. canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  14. },
  15. methods: {
  16. // 事件处理函数
  17. bindViewTap() {
  18. wx.navigateTo({
  19. url: '../logs/logs',
  20. })
  21. },
  22. onChooseAvatar(e: any) {
  23. const { avatarUrl } = e.detail
  24. const { nickName } = this.data.userInfo
  25. this.setData({
  26. "userInfo.avatarUrl": avatarUrl,
  27. hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
  28. })
  29. },
  30. onInputChange(e: any) {
  31. const nickName = e.detail.value
  32. const { avatarUrl } = this.data.userInfo
  33. this.setData({
  34. "userInfo.nickName": nickName,
  35. hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
  36. })
  37. },
  38. getUserProfile() {
  39. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  40. wx.getUserProfile({
  41. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  42. success: (res) => {
  43. this.setData({
  44. userInfo: res.userInfo,
  45. hasUserInfo: true
  46. })
  47. }
  48. })
  49. },
  50. },
  51. })