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

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