index.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. },
  16. onLoad() {
  17. // @ts-ignore
  18. if (wx.getUserProfile) {
  19. this.setData({
  20. canIUseGetUserProfile: true
  21. })
  22. }
  23. },
  24. onShow() {
  25. this.getTabBar().init();
  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. })