index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="profile-page">
  3. <nav-bar title="个人信息" bgColor="#fff" color="#000"></nav-bar>
  4. <view class="form-container" v-if="!loading">
  5. <view class="avatar-section" @click="handleChooseAvatar">
  6. <image :src="formData.avatar || '/static/images/profile.png'" class="avatar-img" mode="aspectFill"></image>
  7. <view class="avatar-tip">点击更换头像</view>
  8. </view>
  9. <view class="form-group">
  10. <view class="form-item">
  11. <text class="form-label require">用户昵称</text>
  12. <input class="form-input" v-model="formData.nickName" placeholder="请输入昵称" placeholder-class="input-placeholder" />
  13. </view>
  14. <view class="line"></view>
  15. <view class="form-item">
  16. <text class="form-label require">手机号码</text>
  17. <input class="form-input" type="number" v-model="formData.phonenumber" placeholder="请输入手机号码" placeholder-class="input-placeholder" />
  18. </view>
  19. <view class="line"></view>
  20. <view class="form-item">
  21. <text class="form-label">邮箱</text>
  22. <input class="form-input" v-model="formData.email" placeholder="请输入邮箱" placeholder-class="input-placeholder" />
  23. </view>
  24. <view class="line"></view>
  25. <view class="form-item">
  26. <text class="form-label">性别</text>
  27. <picker :range="sexOptions" range-key="label" @change="onSexChange">
  28. <view class="picker-value" :class="{'placeholder': formData.sex === undefined || formData.sex === ''}">
  29. {{ getSexLabel }}
  30. </view>
  31. </picker>
  32. </view>
  33. </view>
  34. <view class="btn-group">
  35. <button class="submit-btn" @click="submit">保存修改</button>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script setup>
  41. // @Author: Antigravity
  42. import { ref, reactive, computed } from 'vue'
  43. import { onLoad } from '@dcloudio/uni-app'
  44. import navBar from '@/components/nav-bar/index.vue'
  45. import { getInfo, updateUserProfile, uploadAvatar } from '@/api/system/user'
  46. const loading = ref(true)
  47. const formData = reactive({
  48. avatar: '',
  49. nickName: '',
  50. phonenumber: '',
  51. email: '',
  52. sex: ''
  53. })
  54. const sexOptions = [
  55. { label: '男', value: '0' },
  56. { label: '女', value: '1' },
  57. { label: '未知', value: '2' }
  58. ]
  59. onLoad(async () => {
  60. try {
  61. const res = await getInfo()
  62. if (res && res.user) {
  63. formData.avatar = res.user.avatarUrl || ''
  64. formData.nickName = res.user.nickName || ''
  65. formData.phonenumber = res.user.phonenumber || ''
  66. formData.email = res.user.email || ''
  67. formData.sex = res.user.sex || '2'
  68. }
  69. } catch (e) {
  70. console.error('获取个人信息失败', e)
  71. } finally {
  72. loading.value = false
  73. }
  74. })
  75. const getSexLabel = computed(() => {
  76. const opt = sexOptions.find(o => o.value === String(formData.sex))
  77. return opt ? opt.label : '请选择'
  78. })
  79. const onSexChange = (e) => {
  80. formData.sex = sexOptions[e.detail.value].value
  81. }
  82. // 选择并上传头像
  83. const handleChooseAvatar = () => {
  84. uni.chooseImage({
  85. count: 1,
  86. sizeType: ['compressed'],
  87. sourceType: ['album', 'camera'],
  88. success: async (res) => {
  89. const tempFilePaths = res.tempFilePaths;
  90. try {
  91. uni.showLoading({ title: '上传中...' });
  92. const uploadRes = await uploadAvatar(tempFilePaths[0]);
  93. formData.avatar = uploadRes.imgUrl;
  94. uni.hideLoading();
  95. uni.showToast({ title: '头像上传成功', icon: 'success' });
  96. } catch (e) {
  97. uni.hideLoading();
  98. console.error('上传头像失败', e);
  99. }
  100. }
  101. });
  102. }
  103. const validate = () => {
  104. if (!formData.nickName || !formData.nickName.trim()) {
  105. uni.showToast({ title: '请输入昵称', icon: 'none' })
  106. return false
  107. }
  108. if (!formData.phonenumber || !formData.phonenumber.trim()) {
  109. uni.showToast({ title: '请输入手机号码', icon: 'none' })
  110. return false
  111. }
  112. if (!/^1[3-9]\d{9}$/.test(formData.phonenumber)) {
  113. uni.showToast({ title: '请输入正确的手机号码', icon: 'none' })
  114. return false
  115. }
  116. if (formData.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) {
  117. uni.showToast({ title: '请输入正确的邮箱格式', icon: 'none' })
  118. return false
  119. }
  120. return true
  121. }
  122. const submit = async () => {
  123. if (!validate()) return
  124. try {
  125. uni.showLoading({ title: '保存中...' })
  126. await updateUserProfile(formData)
  127. uni.hideLoading()
  128. uni.showToast({ title: '个人信息修改成功', icon: 'success' })
  129. setTimeout(() => {
  130. uni.navigateBack()
  131. }, 1000)
  132. } catch (err) {
  133. uni.hideLoading()
  134. console.log('保存失败', err)
  135. }
  136. }
  137. </script>
  138. <style lang="scss" scoped>
  139. .profile-page {
  140. min-height: 100vh;
  141. background-color: #f7f8fa;
  142. }
  143. .form-container {
  144. padding-top: 0;
  145. }
  146. .avatar-section {
  147. display: flex;
  148. flex-direction: column;
  149. align-items: center;
  150. padding: 60rpx 0;
  151. background-color: #fff;
  152. margin-bottom: 20rpx;
  153. border-bottom: 2rpx solid #f0f0f0;
  154. .avatar-img {
  155. width: 160rpx;
  156. height: 160rpx;
  157. border-radius: 80rpx;
  158. background-color: #f7f8fa;
  159. }
  160. .avatar-tip {
  161. margin-top: 20rpx;
  162. font-size: 24rpx;
  163. color: #999;
  164. }
  165. }
  166. .form-group {
  167. background: #fff;
  168. margin: 0 24rpx;
  169. border-radius: 20rpx;
  170. padding: 0 32rpx;
  171. }
  172. .form-item {
  173. display: flex;
  174. align-items: center;
  175. padding: 24rpx 0;
  176. }
  177. .form-label {
  178. width: 200rpx;
  179. font-size: 28rpx;
  180. color: #333;
  181. flex-shrink: 0;
  182. &::before {
  183. content: '*';
  184. color: transparent;
  185. margin-right: 4rpx;
  186. }
  187. &.require::before {
  188. color: #f56c6c;
  189. }
  190. }
  191. .form-input {
  192. flex: 1;
  193. height: 48rpx;
  194. line-height: 48rpx;
  195. font-size: 28rpx;
  196. color: #333;
  197. text-align: right;
  198. }
  199. .input-placeholder {
  200. color: #c0c4cc;
  201. font-size: 28rpx;
  202. }
  203. .line {
  204. height: 2rpx;
  205. background: #eee;
  206. margin: 0;
  207. }
  208. .btn-group {
  209. margin: 60rpx 32rpx 32rpx;
  210. }
  211. .submit-btn {
  212. height: 88rpx;
  213. line-height: 88rpx;
  214. background: linear-gradient(90deg, #ffd53f, #ff9500);
  215. color: #fff;
  216. border: none;
  217. border-radius: 44rpx;
  218. font-size: 30rpx;
  219. font-weight: bold;
  220. &::after { border: none; }
  221. }
  222. .picker-value {
  223. flex: 1;
  224. font-size: 28rpx;
  225. color: #333;
  226. text-align: right;
  227. height: 48rpx;
  228. line-height: 48rpx;
  229. padding-right: 20rpx;
  230. }
  231. .picker-value.placeholder {
  232. color: #c0c4cc;
  233. }
  234. </style>