index.js 1.5 KB

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