index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //index.js
  2. const app = getApp()
  3. Page({
  4. data: {
  5. avatarUrl: './user-unlogin.png',
  6. userInfo: {},
  7. logged: false,
  8. takeSession: false,
  9. requestResult: ''
  10. },
  11. onLoad: function() {
  12. if (!wx.cloud) {
  13. wx.redirectTo({
  14. url: '../chooseLib/chooseLib',
  15. })
  16. return
  17. }
  18. // 获取用户信息
  19. wx.getSetting({
  20. success: res => {
  21. if (res.authSetting['scope.userInfo']) {
  22. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  23. wx.getUserInfo({
  24. success: res => {
  25. this.setData({
  26. avatarUrl: res.userInfo.avatarUrl,
  27. userInfo: res.userInfo
  28. })
  29. }
  30. })
  31. }
  32. }
  33. })
  34. },
  35. onGetUserInfo: function(e) {
  36. if (!this.logged && e.detail.userInfo) {
  37. this.setData({
  38. logged: true,
  39. avatarUrl: e.detail.userInfo.avatarUrl,
  40. userInfo: e.detail.userInfo
  41. })
  42. }
  43. },
  44. onGetOpenid: function() {
  45. // 调用云函数
  46. wx.cloud.callFunction({
  47. name: 'login',
  48. data: {},
  49. success: res => {
  50. console.log('[云函数] [login] user openid: ', res.result.openid)
  51. app.globalData.openid = res.result.openid
  52. wx.navigateTo({
  53. url: '../userConsole/userConsole',
  54. })
  55. },
  56. fail: err => {
  57. console.error('[云函数] [login] 调用失败', err)
  58. wx.navigateTo({
  59. url: '../deployFunctions/deployFunctions',
  60. })
  61. }
  62. })
  63. },
  64. // 上传图片
  65. doUpload: function () {
  66. // 选择图片
  67. wx.chooseImage({
  68. count: 1,
  69. sizeType: ['compressed'],
  70. sourceType: ['album', 'camera'],
  71. success: function (res) {
  72. wx.showLoading({
  73. title: '上传中',
  74. })
  75. const filePath = res.tempFilePaths[0]
  76. // 上传图片
  77. const cloudPath = 'my-image' + filePath.match(/\.[^.]+?$/)[0]
  78. wx.cloud.uploadFile({
  79. cloudPath,
  80. filePath,
  81. success: res => {
  82. console.log('[上传文件] 成功:', res)
  83. app.globalData.fileID = res.fileID
  84. app.globalData.cloudPath = cloudPath
  85. app.globalData.imagePath = filePath
  86. wx.navigateTo({
  87. url: '../storageConsole/storageConsole'
  88. })
  89. },
  90. fail: e => {
  91. console.error('[上传文件] 失败:', e)
  92. wx.showToast({
  93. icon: 'none',
  94. title: '上传失败',
  95. })
  96. },
  97. complete: () => {
  98. wx.hideLoading()
  99. }
  100. })
  101. },
  102. fail: e => {
  103. console.error(e)
  104. }
  105. })
  106. },
  107. })