index.ts 1.7 KB

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