四零语境前端代码仓库
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.

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