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

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