index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //index.js
  2. const app = getApp()
  3. Page({
  4. data: {
  5. avatarUrl: './user-unlogin.png',
  6. userInfo: {},
  7. hasUserInfo: false,
  8. logged: false,
  9. takeSession: false,
  10. requestResult: '',
  11. canIUseGetUserProfile: false,
  12. canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') // 如需尝试获取用户信息可改为false
  13. },
  14. onLoad: function() {
  15. if (!wx.cloud) {
  16. wx.redirectTo({
  17. url: '../chooseLib/chooseLib',
  18. })
  19. return
  20. }
  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. this.setData({
  33. avatarUrl: res.userInfo.avatarUrl,
  34. userInfo: res.userInfo,
  35. hasUserInfo: true,
  36. })
  37. }
  38. })
  39. },
  40. onGetUserInfo: function(e) {
  41. if (!this.data.logged && e.detail.userInfo) {
  42. this.setData({
  43. logged: true,
  44. avatarUrl: e.detail.userInfo.avatarUrl,
  45. userInfo: e.detail.userInfo,
  46. hasUserInfo: true,
  47. })
  48. }
  49. },
  50. onGetOpenid: function() {
  51. // 调用云函数
  52. wx.cloud.callFunction({
  53. name: 'login',
  54. data: {},
  55. success: res => {
  56. console.log('[云函数] [login] user openid: ', res.result.openid)
  57. app.globalData.openid = res.result.openid
  58. wx.navigateTo({
  59. url: '../userConsole/userConsole',
  60. })
  61. },
  62. fail: err => {
  63. console.error('[云函数] [login] 调用失败', err)
  64. wx.navigateTo({
  65. url: '../deployFunctions/deployFunctions',
  66. })
  67. }
  68. })
  69. },
  70. // 上传图片
  71. doUpload: function () {
  72. // 选择图片
  73. wx.chooseImage({
  74. count: 1,
  75. sizeType: ['compressed'],
  76. sourceType: ['album', 'camera'],
  77. success: function (res) {
  78. wx.showLoading({
  79. title: '上传中',
  80. })
  81. const filePath = res.tempFilePaths[0]
  82. // 上传图片
  83. const cloudPath = `my-image${filePath.match(/\.[^.]+?$/)[0]}`
  84. wx.cloud.uploadFile({
  85. cloudPath,
  86. filePath,
  87. success: res => {
  88. console.log('[上传文件] 成功:', res)
  89. app.globalData.fileID = res.fileID
  90. app.globalData.cloudPath = cloudPath
  91. app.globalData.imagePath = filePath
  92. wx.navigateTo({
  93. url: '../storageConsole/storageConsole'
  94. })
  95. },
  96. fail: e => {
  97. console.error('[上传文件] 失败:', e)
  98. wx.showToast({
  99. icon: 'none',
  100. title: '上传失败',
  101. })
  102. },
  103. complete: () => {
  104. wx.hideLoading()
  105. }
  106. })
  107. },
  108. fail: e => {
  109. console.error(e)
  110. }
  111. })
  112. },
  113. })