edit.uvue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <cl-page>
  3. <view class="p-3">
  4. <view class="flex flex-col justify-center items-center py-10 mb-3">
  5. <view class="relative overflow-visible">
  6. <!-- #ifdef MP-WEIXIN -->
  7. <button
  8. class="absolute top-0 left-0 w-full h-full opacity-0 z-10"
  9. open-type="chooseAvatar"
  10. @chooseavatar="uploadAvatar"
  11. ></button>
  12. <!-- #endif -->
  13. <cl-avatar
  14. :src="user.info.avatarUrl"
  15. :size="150"
  16. :pt="{ className: '!rounded-3xl', icon: { size: 60 } }"
  17. @tap="uploadAvatar"
  18. >
  19. </cl-avatar>
  20. <view
  21. class="flex flex-col justify-center items-center absolute bottom-0 right-[-6rpx] bg-black rounded-full p-1 border border-solid border-white"
  22. >
  23. <cl-icon name="edit-line" color="white" :size="24"></cl-icon>
  24. </view>
  25. </view>
  26. </view>
  27. <cl-list :pt="{ className: 'mb-3' }">
  28. <cl-list-item
  29. :label="t('我的昵称')"
  30. hoverable
  31. arrow
  32. justify="start"
  33. @tap="router.to('/pages/user/edit-name')"
  34. >
  35. <cl-text>{{ user.info.nickName }}</cl-text>
  36. </cl-list-item>
  37. <cl-list-item label="手机号" hoverable justify="start">
  38. <cl-text>{{ user.info.phone }}</cl-text>
  39. </cl-list-item>
  40. </cl-list>
  41. <cl-list :pt="{ className: 'mb-3' }">
  42. <cl-list-item
  43. :label="t('简介')"
  44. hoverable
  45. arrow
  46. justify="start"
  47. @tap="router.to('/pages/user/edit-description')"
  48. >
  49. <cl-text color="info" v-if="user.info.description == null">{{
  50. t("介绍一下自己")
  51. }}</cl-text>
  52. <cl-text ellipsis v-else>{{ user.info.description }}</cl-text>
  53. </cl-list-item>
  54. </cl-list>
  55. <cl-list :pt="{ className: 'mb-3' }">
  56. <cl-list-item
  57. :label="t('性别')"
  58. hoverable
  59. arrow
  60. justify="start"
  61. @tap="open('gender')"
  62. >
  63. <cl-text>{{ genderText }}</cl-text>
  64. <cl-text color="info" v-if="genderText == ''">{{ t("编辑性别") }}</cl-text>
  65. </cl-list-item>
  66. <cl-list-item
  67. :label="t('生日')"
  68. hoverable
  69. arrow
  70. justify="start"
  71. @tap="open('birthday')"
  72. >
  73. <cl-text>{{ user.info.birthday }}</cl-text>
  74. <cl-text color="info" v-if="user.info['birthday'] == null">{{
  75. t("选择生日")
  76. }}</cl-text>
  77. </cl-list-item>
  78. <cl-list-item
  79. :label="t('地区')"
  80. hoverable
  81. arrow
  82. justify="start"
  83. @tap="open('region')"
  84. >
  85. <cl-text>{{ regionText }}</cl-text>
  86. <cl-text color="info" v-if="regionText == ''">{{
  87. t("选择所在的地区")
  88. }}</cl-text>
  89. </cl-list-item>
  90. </cl-list>
  91. <cl-select
  92. :title="t('选择性别')"
  93. :model-value="user.info.gender"
  94. :ref="refs.set('gender')"
  95. :options="genderOptions"
  96. :show-trigger="false"
  97. @change="onGenderChange"
  98. ></cl-select>
  99. <cl-select-date
  100. :title="t('选择生日')"
  101. :model-value="user.info.birthday"
  102. :ref="refs.set('birthday')"
  103. type="date"
  104. :end="today"
  105. :show-trigger="false"
  106. @change="onBirthdayChange"
  107. ></cl-select-date>
  108. <cl-cascader
  109. :title="t('选择所在的地区')"
  110. :ref="refs.set('region')"
  111. :options="regionOptions"
  112. :show-trigger="false"
  113. @change="onRegionChange"
  114. ></cl-cascader>
  115. </view>
  116. </cl-page>
  117. </template>
  118. <script setup lang="ts">
  119. import { dayUts, router, upload, useRefs, useStore, type Response } from "@/cool";
  120. import { t } from "@/locale";
  121. import { useCascader, useUi, type ClSelectOption } from "@/uni_modules/cool-ui";
  122. import { computed, ref } from "vue";
  123. import pca from "@/data/pca.json";
  124. const { user } = useStore();
  125. const ui = useUi();
  126. const refs = useRefs();
  127. // 今天
  128. const today = dayUts().format("YYYY-MM-DD");
  129. // 性别选项
  130. const genderOptions = ref<ClSelectOption[]>([
  131. {
  132. label: t("保密"),
  133. value: 0
  134. },
  135. {
  136. label: t("男"),
  137. value: 1
  138. },
  139. {
  140. label: t("女"),
  141. value: 2
  142. }
  143. ]);
  144. // 性别文本
  145. const genderText = computed(() => {
  146. return [t("保密"), t("男"), t("女")][user.info.gender!];
  147. });
  148. // 性别改变
  149. function onGenderChange(val: number) {
  150. user.update({
  151. gender: val
  152. });
  153. ui.showToast({
  154. message: t("性别设置成功")
  155. });
  156. }
  157. // 生日改变
  158. function onBirthdayChange(val: string) {
  159. user.update({
  160. birthday: val
  161. });
  162. ui.showToast({
  163. message: t("生日设置成功")
  164. });
  165. }
  166. // 地区选项
  167. const regionOptions = useCascader(pca);
  168. // 地区文本
  169. const regionText = computed(() => {
  170. return [user.info.province, user.info.city, user.info.district]
  171. .filter((e) => e != null)
  172. .join(" - ");
  173. });
  174. // 地区改变
  175. function onRegionChange(arr: string[]) {
  176. user.update({
  177. province: arr[0],
  178. city: arr[1],
  179. district: arr[2]
  180. });
  181. ui.showToast({
  182. message: t("地区设置成功")
  183. });
  184. }
  185. // 打开弹窗
  186. function open(name: string) {
  187. refs.open(name);
  188. }
  189. // 上传头像
  190. function uploadAvatar(e: UniEvent) {
  191. function next(path: string) {
  192. upload(path)
  193. .then((url) => {
  194. ui.showToast({
  195. message: t("头像上传成功")
  196. });
  197. user.update({
  198. avatarUrl: url
  199. });
  200. })
  201. .catch((err) => {
  202. ui.showToast({
  203. message: (err as Response).message!
  204. });
  205. });
  206. }
  207. // #ifdef MP-WEIXIN
  208. next(e.detail.avatarUrl);
  209. // #endif
  210. // #ifndef MP-WEIXIN
  211. uni.chooseImage({
  212. count: 1,
  213. success(res) {
  214. next(res.tempFiles[0].path);
  215. }
  216. });
  217. // #endif
  218. }
  219. </script>