wx.uvue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <cl-popup
  3. v-model="editVisible"
  4. direction="center"
  5. :title="t('提示')"
  6. size="80%"
  7. @close="onEditClose"
  8. >
  9. <div class="p-4 pt-0">
  10. <cl-text color="info" :pt="{ className: '!text-sm' }">
  11. {{ t("为提供更好的服务,我们邀请您填写昵称、头像等公开信息") }}
  12. </cl-text>
  13. <view
  14. class="flex flex-row justify-between items-center bg-surface-100 rounded-xl p-2 px-3 mt-3 h-[95rpx]"
  15. >
  16. <cl-text>{{ t("头像") }}</cl-text>
  17. <view class="relative">
  18. <cl-avatar :size="60" :src="editForm.avatarUrl"></cl-avatar>
  19. <button
  20. class="absolute top-0 right-0 h-10 w-10 z-10 opacity-0 p-0 m-0"
  21. open-type="chooseAvatar"
  22. @chooseavatar="onEditChooseAvatar"
  23. ></button>
  24. </view>
  25. </view>
  26. <view
  27. class="flex flex-row justify-between items-center bg-surface-100 rounded-xl p-2 px-3 mt-3 h-[95rpx]"
  28. >
  29. <cl-text>{{ t("昵称") }}</cl-text>
  30. <cl-input
  31. v-model="editForm.nickName"
  32. type="nickname"
  33. :border="false"
  34. :placeholder="t('点击输入昵称')"
  35. :maxlength="16"
  36. :pt="{
  37. className: '!bg-transparent !px-0 flex-1',
  38. inner: {
  39. className: 'text-right'
  40. }
  41. }"
  42. ></cl-input>
  43. </view>
  44. <view class="flex flex-row mt-4">
  45. <cl-button
  46. size="large"
  47. text
  48. border
  49. type="light"
  50. :pt="{
  51. className: 'flex-1 !rounded-xl h-[80rpx]'
  52. }"
  53. @tap="editClose"
  54. >{{ t("取消") }}</cl-button
  55. >
  56. <cl-button
  57. size="large"
  58. :pt="{
  59. className: 'flex-1 !rounded-xl h-[80rpx]'
  60. }"
  61. :loading="editLoading"
  62. @tap="editSave"
  63. >{{ t("确认") }}</cl-button
  64. >
  65. </view>
  66. </div>
  67. </cl-popup>
  68. </template>
  69. <script setup lang="ts">
  70. import { service, upload, useStore, type Response } from "@/cool";
  71. import { t } from "@/locale";
  72. import { useUi } from "@/uni_modules/cool-ui";
  73. import { reactive, ref } from "vue";
  74. const emit = defineEmits(["success"]);
  75. const { user } = useStore();
  76. const ui = useUi();
  77. // 是否显示编辑
  78. const editVisible = ref(false);
  79. // 是否保存中
  80. const editLoading = ref(false);
  81. // 编辑表单
  82. type EditForm = {
  83. avatarUrl: string;
  84. nickName: string;
  85. };
  86. const editForm = reactive<EditForm>({
  87. avatarUrl: "",
  88. nickName: ""
  89. });
  90. // 编辑打开
  91. function editOpen() {
  92. editVisible.value = true;
  93. }
  94. // 编辑关闭
  95. function editClose() {
  96. editVisible.value = false;
  97. }
  98. // 编辑保存
  99. async function editSave() {
  100. // 校验头像是否已上传
  101. if (editForm.avatarUrl == "") {
  102. ui.showToast({
  103. message: t("请上传头像")
  104. });
  105. return;
  106. }
  107. // 校验昵称是否已填写
  108. if (editForm.nickName == "") {
  109. ui.showToast({
  110. message: t("请输入昵称")
  111. });
  112. return;
  113. }
  114. // 设置保存状态为加载中
  115. editLoading.value = true;
  116. // 上传头像并更新用户信息
  117. await upload(editForm.avatarUrl)
  118. .then((url) => {
  119. // 上传成功后,更新用户昵称和头像
  120. user.update({
  121. nickName: editForm.nickName,
  122. avatarUrl: url
  123. });
  124. // 关闭弹窗
  125. editClose();
  126. })
  127. .catch((err) => {
  128. // 上传失败,提示错误信息
  129. ui.showToast({
  130. message: (err as Response).message!
  131. });
  132. });
  133. // 恢复保存状态
  134. editLoading.value = false;
  135. }
  136. // 编辑选择头像
  137. function onEditChooseAvatar(e: UniEvent) {
  138. // #ifdef MP-WEIXIN
  139. editForm.avatarUrl = e.detail.avatarUrl;
  140. // #endif
  141. }
  142. // 编辑关闭
  143. function onEditClose() {
  144. editVisible.value = false;
  145. }
  146. // 微信小程序登录
  147. async function miniLogin() {
  148. await service.user.login
  149. .mini({})
  150. .then((res) => {
  151. emit("success", res);
  152. })
  153. .catch((err) => {
  154. ui.showToast({
  155. message: (err as Response).message!
  156. });
  157. });
  158. }
  159. // 微信APP登录
  160. function appLogin() {}
  161. // 微信登录
  162. async function login() {
  163. ui.showToast({
  164. message: t("开发中,敬请期待")
  165. });
  166. return;
  167. // #ifdef MP-WEIXIN
  168. miniLogin();
  169. // #endif
  170. // #ifdef APP
  171. appLogin();
  172. // #endif
  173. emit("success");
  174. }
  175. defineExpose({
  176. login,
  177. editOpen,
  178. editClose
  179. });
  180. </script>