index.ts 1.4 KB

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