UserInfoPopup.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const utils_api = require("../utils/api.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. visible: false,
  8. nickname: "",
  9. avatarUrl: "",
  10. tempAvatarPath: "",
  11. // 临时头像路径
  12. userData: null
  13. // 后端返回的用户数据
  14. };
  15. },
  16. methods: {
  17. /**
  18. * 打开弹窗
  19. */
  20. open(userData) {
  21. this.visible = true;
  22. this.userData = userData;
  23. this.nickname = "";
  24. this.avatarUrl = "";
  25. this.tempAvatarPath = "";
  26. },
  27. /**
  28. * 关闭弹窗
  29. */
  30. close() {
  31. this.visible = false;
  32. },
  33. /**
  34. * 选择头像
  35. */
  36. handleChooseAvatar(e) {
  37. console.log("[用户信息] 选择头像:", e);
  38. const { avatarUrl } = e.detail;
  39. this.tempAvatarPath = avatarUrl;
  40. this.avatarUrl = avatarUrl;
  41. },
  42. /**
  43. * 点击遮罩层
  44. */
  45. handleMaskClick() {
  46. },
  47. /**
  48. * 取消
  49. */
  50. handleCancel() {
  51. common_vendor.index.showModal({
  52. title: "提示",
  53. content: "取消后将无法完成登录,确定要取消吗?",
  54. success: (res) => {
  55. if (res.confirm) {
  56. this.close();
  57. }
  58. }
  59. });
  60. },
  61. /**
  62. * 确定提交
  63. */
  64. async handleConfirm() {
  65. if (!this.nickname || this.nickname.trim() === "") {
  66. common_vendor.index.showToast({
  67. title: "请输入昵称",
  68. icon: "none"
  69. });
  70. return;
  71. }
  72. if (!this.avatarUrl) {
  73. common_vendor.index.showToast({
  74. title: "请选择头像",
  75. icon: "none"
  76. });
  77. return;
  78. }
  79. try {
  80. common_vendor.index.showLoading({ title: "上传中..." });
  81. let uploadedAvatarUrl = this.avatarUrl;
  82. if (this.tempAvatarPath) {
  83. uploadedAvatarUrl = await this.uploadAvatar(this.tempAvatarPath);
  84. }
  85. common_vendor.index.hideLoading();
  86. this.$emit("confirm", {
  87. nickname: this.nickname.trim(),
  88. avatarUrl: uploadedAvatarUrl
  89. });
  90. this.close();
  91. } catch (error) {
  92. common_vendor.index.hideLoading();
  93. console.error("[用户信息] 上传头像失败:", error);
  94. common_vendor.index.showToast({
  95. title: "头像上传失败,请重试",
  96. icon: "none"
  97. });
  98. }
  99. },
  100. /**
  101. * 上传头像到OSS
  102. */
  103. uploadAvatar(filePath) {
  104. return new Promise((resolve, reject) => {
  105. common_vendor.index.uploadFile({
  106. url: utils_api.uploadFile.url,
  107. // 从api.js导入
  108. filePath,
  109. name: "file",
  110. header: {
  111. "Authorization": `Bearer ${common_vendor.index.getStorageSync("user_token") || ""}`
  112. },
  113. success: (res) => {
  114. const data = JSON.parse(res.data);
  115. if (data.code === 200 && data.data && data.data.url) {
  116. resolve(data.data.url);
  117. } else {
  118. reject(new Error(data.message || "上传失败"));
  119. }
  120. },
  121. fail: (err) => {
  122. reject(err);
  123. }
  124. });
  125. });
  126. }
  127. }
  128. };
  129. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  130. return common_vendor.e({
  131. a: $data.visible
  132. }, $data.visible ? {
  133. b: $data.avatarUrl || "/static/images/default-avatar.png",
  134. c: common_vendor.o((...args) => $options.handleChooseAvatar && $options.handleChooseAvatar(...args)),
  135. d: $data.nickname,
  136. e: common_vendor.o(($event) => $data.nickname = $event.detail.value),
  137. f: common_vendor.o((...args) => $options.handleCancel && $options.handleCancel(...args)),
  138. g: common_vendor.o((...args) => $options.handleConfirm && $options.handleConfirm(...args)),
  139. h: common_vendor.o(() => {
  140. }),
  141. i: common_vendor.o((...args) => $options.handleMaskClick && $options.handleMaskClick(...args))
  142. } : {});
  143. }
  144. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-a5b292f7"], ["__file", "D:/program/gupiao-wx/src/components/UserInfoPopup.vue"]]);
  145. wx.createComponent(Component);