index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // index.ts
  2. // 获取应用实例
  3. const app = getApp<IAppOption>()
  4. Page({
  5. data: {
  6. motto: 'Hello World',
  7. userInfo: {},
  8. hasUserInfo: false,
  9. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  10. canIUseGetUserProfile: false,
  11. canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  12. },
  13. // 事件处理函数
  14. bindViewTap() {
  15. wx.navigateTo({
  16. url: '../logs/logs',
  17. })
  18. },
  19. onLoad() {
  20. // @ts-ignore
  21. if (wx.getUserProfile) {
  22. this.setData({
  23. canIUseGetUserProfile: true
  24. })
  25. }
  26. },
  27. getUserProfile() {
  28. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  29. wx.getUserProfile({
  30. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  31. success: (res) => {
  32. console.log(res)
  33. this.setData({
  34. userInfo: res.userInfo,
  35. hasUserInfo: true
  36. })
  37. }
  38. })
  39. },
  40. getUserInfo(e: any) {
  41. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
  42. console.log(e)
  43. this.setData({
  44. userInfo: e.detail.userInfo,
  45. hasUserInfo: true
  46. })
  47. }
  48. })