四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
5.7 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="profile-container">
  3. <!-- 个人信息表单 -->
  4. <view class="form-container">
  5. <view class="form-title">个人信息</view>
  6. <!-- 昵称 -->
  7. <view class="form-item">
  8. <view class="label">
  9. <text class="required">*</text>
  10. <text>昵称</text>
  11. </view>
  12. <uv-input
  13. v-model="userInfo.nickName"
  14. placeholder="请输入昵称"
  15. :customStyle="inputStyle"
  16. border="bottom"
  17. ></uv-input>
  18. </view>
  19. <!-- 电话 -->
  20. <view class="form-item">
  21. <view class="label">
  22. <text class="required">*</text>
  23. <text>电话</text>
  24. </view>
  25. <uv-input
  26. v-model="userInfo.phone"
  27. placeholder="请输入手机号"
  28. :customStyle="inputStyle"
  29. border="bottom"
  30. type="number"
  31. ></uv-input>
  32. </view>
  33. <!-- 头像 -->
  34. <view class="form-item">
  35. <view class="label">
  36. <text class="required">*</text>
  37. <text>头像</text>
  38. </view>
  39. <view class="avatar-container" @click="uploadAvatar">
  40. <image
  41. :src="userInfo.headImage || '/static/默认头像.png'"
  42. class="avatar-image"
  43. mode="aspectFill"
  44. ></image>
  45. <!-- 遮罩层 -->
  46. <view class="avatar-mask" :class="{ 'fade-out': showMask }" v-if="showMask">
  47. <text>点击上传头像</text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. <!-- 固定底部保存按钮 -->
  53. <view class="save-button-container">
  54. <uv-button
  55. @click="saveProfile"
  56. :customStyle="saveButtonStyle"
  57. shape="circle"
  58. >
  59. 保存
  60. </uv-button>
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. export default {
  66. data() {
  67. return {
  68. userInfo: {
  69. headImage: '',
  70. nickName: '',
  71. phone: ''
  72. },
  73. showMask: true,
  74. saveButtonStyle: {
  75. backgroundColor: '#06DADC',
  76. borderRadius: '41rpx',
  77. height: '94rpx',
  78. width: '594rpx',
  79. border: 'none',
  80. color: '#fff',
  81. fontSize: '32rpx',
  82. fontWeight: '500'
  83. },
  84. inputStyle: {
  85. backgroundColor: '#fff',
  86. borderRadius: '12rpx',
  87. padding: '0 -20rpx',
  88. fontSize: '28rpx'
  89. }
  90. }
  91. },
  92. methods: {
  93. // 上传头像
  94. async uploadAvatar() {
  95. try {
  96. const result = await this.$utils.chooseAndUpload()
  97. if (result && result.success) {
  98. console.log(result);
  99. this.userInfo.headImage = result.url
  100. }
  101. } catch (error) {
  102. console.error('头像上传失败:', error)
  103. uni.showToast({
  104. title: '头像上传失败',
  105. icon: 'error'
  106. })
  107. }
  108. },
  109. // 保存资料
  110. async saveProfile() {
  111. if (!this.userInfo.nickName.trim()) {
  112. uni.showToast({
  113. title: '请输入昵称',
  114. icon: 'none'
  115. })
  116. return
  117. }
  118. if (!this.userInfo.phone.trim()) {
  119. uni.showToast({
  120. title: '请输入手机号',
  121. icon: 'none'
  122. })
  123. return
  124. }
  125. // 简单的手机号验证
  126. const phoneReg = /^1[3-9]\d{9}$/
  127. if (!phoneReg.test(this.userInfo.phone)) {
  128. uni.showToast({
  129. title: '请输入正确的手机号',
  130. icon: 'none'
  131. })
  132. return
  133. }
  134. // TODO: 调用API保存用户信息
  135. const res = await this.$api.user.updateUser({
  136. headImage: this.userInfo.headImage,
  137. nickName: this.userInfo.nickName,
  138. phone: this.userInfo.phone
  139. })
  140. if (res.code === 200) {
  141. uni.showToast({
  142. title: '保存成功',
  143. icon: 'success'
  144. })
  145. // 延迟返回上一页
  146. setTimeout(() => {
  147. uni.navigateBack()
  148. }, 1500)
  149. }
  150. },
  151. // 获取个人信息
  152. async getProfile() {
  153. const res = await this.$api.user.queryUser()
  154. if (res.code === 200) {
  155. this.userInfo = res.result
  156. }
  157. }
  158. },
  159. onLoad() {
  160. // this.getProfile()
  161. // 3秒后隐藏遮罩层
  162. setTimeout(() => {
  163. this.showMask = false
  164. }, 3000)
  165. }
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. .profile-container {
  170. min-height: 100vh;
  171. background-color: #f5f5f5;
  172. padding: 40rpx 32rpx 200rpx;
  173. .form-container {
  174. background: #fff;
  175. border-radius: 20rpx;
  176. padding: 40rpx 32rpx;
  177. .form-title {
  178. font-size: 36rpx;
  179. font-weight: 600;
  180. color: #333;
  181. margin-bottom: 60rpx;
  182. }
  183. .form-item {
  184. margin-bottom: 60rpx;
  185. &:last-child {
  186. margin-bottom: 0;
  187. }
  188. .label {
  189. display: flex;
  190. align-items: center;
  191. margin-bottom: 20rpx;
  192. font-size: 28rpx;
  193. color: #333;
  194. .required {
  195. color: #ff4757;
  196. margin-right: 8rpx;
  197. }
  198. }
  199. }
  200. .avatar-container {
  201. position: relative;
  202. width: 200rpx;
  203. height: 200rpx;
  204. border-radius: 16rpx;
  205. overflow: hidden;
  206. .avatar-image {
  207. width: 100%;
  208. height: 100%;
  209. }
  210. .avatar-mask {
  211. position: absolute;
  212. top: 0;
  213. left: 0;
  214. right: 0;
  215. bottom: 0;
  216. background: rgba(0, 0, 0, 0.6);
  217. display: flex;
  218. align-items: center;
  219. justify-content: center;
  220. color: #fff;
  221. font-size: 24rpx;
  222. transition: opacity 1s ease-out;
  223. &.fade-out {
  224. opacity: 0;
  225. }
  226. }
  227. }
  228. }
  229. .save-button-container {
  230. position: fixed;
  231. bottom: 60rpx;
  232. left: 50%;
  233. transform: translateX(-50%);
  234. z-index: 999;
  235. }
  236. }
  237. </style>