index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. uni.showToast({ title: typeof e === 'string' ? e : '获取个人信息失败', icon: 'none' })
  72. } finally {
  73. loading.value = false
  74. }
  75. })
  76. const getSexLabel = computed(() => {
  77. const opt = sexOptions.find(o => o.value === String(formData.sex))
  78. return opt ? opt.label : '请选择'
  79. })
  80. const onSexChange = (e) => {
  81. formData.sex = sexOptions[e.detail.value].value
  82. }
  83. // 选择并上传头像
  84. const handleChooseAvatar = () => {
  85. uni.chooseImage({
  86. count: 1,
  87. sizeType: ['compressed'],
  88. sourceType: ['album', 'camera'],
  89. success: async (res) => {
  90. const tempFilePaths = res.tempFilePaths;
  91. try {
  92. uni.showLoading({ title: '上传中...' });
  93. const uploadRes = await uploadAvatar(tempFilePaths[0]);
  94. formData.avatar = uploadRes.imgUrl;
  95. uni.hideLoading();
  96. uni.showToast({ title: '头像上传成功', icon: 'success' });
  97. } catch (e) {
  98. uni.hideLoading();
  99. console.error('上传头像失败', e);
  100. uni.showToast({ title: typeof e === 'string' ? e : '上传头像失败', icon: 'none' });
  101. }
  102. }
  103. });
  104. }
  105. const validate = () => {
  106. if (!formData.nickName || !formData.nickName.trim()) {
  107. uni.showToast({ title: '请输入昵称', icon: 'none' })
  108. return false
  109. }
  110. if (!formData.phonenumber || !formData.phonenumber.trim()) {
  111. uni.showToast({ title: '请输入手机号码', icon: 'none' })
  112. return false
  113. }
  114. if (!/^1[3-9]\d{9}$/.test(formData.phonenumber)) {
  115. uni.showToast({ title: '请输入正确的手机号码', icon: 'none' })
  116. return false
  117. }
  118. if (formData.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) {
  119. uni.showToast({ title: '请输入正确的邮箱格式', icon: 'none' })
  120. return false
  121. }
  122. return true
  123. }
  124. const submit = async () => {
  125. if (!validate()) return
  126. try {
  127. uni.showLoading({ title: '保存中...' })
  128. await updateUserProfile(formData)
  129. uni.hideLoading()
  130. uni.showToast({ title: '个人信息修改成功', icon: 'success' })
  131. setTimeout(() => {
  132. uni.navigateBack()
  133. }, 1000)
  134. } catch (err) {
  135. uni.hideLoading()
  136. console.log('保存失败', err)
  137. uni.showToast({ title: typeof err === 'string' ? err : '保存失败', icon: 'none' })
  138. }
  139. }
  140. </script>
  141. <style lang="scss" scoped>
  142. .profile-page {
  143. min-height: 100vh;
  144. background-color: #f7f8fa;
  145. }
  146. .form-container {
  147. padding-top: 0;
  148. }
  149. .avatar-section {
  150. display: flex;
  151. flex-direction: column;
  152. align-items: center;
  153. padding: 60rpx 0;
  154. background-color: #fff;
  155. margin-bottom: 20rpx;
  156. border-bottom: 2rpx solid #f0f0f0;
  157. .avatar-img {
  158. width: 160rpx;
  159. height: 160rpx;
  160. border-radius: 80rpx;
  161. background-color: #f7f8fa;
  162. }
  163. .avatar-tip {
  164. margin-top: 20rpx;
  165. font-size: 24rpx;
  166. color: #999;
  167. }
  168. }
  169. .form-group {
  170. background: #fff;
  171. margin: 0 24rpx;
  172. border-radius: 20rpx;
  173. padding: 0 32rpx;
  174. }
  175. .form-item {
  176. display: flex;
  177. align-items: center;
  178. padding: 24rpx 0;
  179. }
  180. .form-label {
  181. width: 200rpx;
  182. font-size: 28rpx;
  183. color: #333;
  184. flex-shrink: 0;
  185. &::before {
  186. content: '*';
  187. color: transparent;
  188. margin-right: 4rpx;
  189. }
  190. &.require::before {
  191. color: #f56c6c;
  192. }
  193. }
  194. .form-input {
  195. flex: 1;
  196. height: 48rpx;
  197. line-height: 48rpx;
  198. font-size: 28rpx;
  199. color: #333;
  200. text-align: right;
  201. }
  202. .input-placeholder {
  203. color: #c0c4cc;
  204. font-size: 28rpx;
  205. }
  206. .line {
  207. height: 2rpx;
  208. background: #eee;
  209. margin: 0;
  210. }
  211. .btn-group {
  212. margin: 60rpx 32rpx 32rpx;
  213. }
  214. .submit-btn {
  215. height: 88rpx;
  216. line-height: 88rpx;
  217. background: linear-gradient(90deg, #ffd53f, #ff9500);
  218. color: #fff;
  219. border: none;
  220. border-radius: 44rpx;
  221. font-size: 30rpx;
  222. font-weight: bold;
  223. &::after { border: none; }
  224. }
  225. .picker-value {
  226. flex: 1;
  227. font-size: 28rpx;
  228. color: #333;
  229. text-align: right;
  230. height: 48rpx;
  231. line-height: 48rpx;
  232. padding-right: 20rpx;
  233. }
  234. .picker-value.placeholder {
  235. color: #c0c4cc;
  236. }
  237. </style>