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.

323 lines
6.4 KiB

  1. <template>
  2. <view class="user-info-container container">
  3. <view class="user-avatar-section">
  4. <view class="user-info-title">
  5. 用户头像
  6. </view>
  7. <view class="avatar-container">
  8. <button class="choose-avatar-btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  9. <view class="avatar-wrapper">
  10. <image
  11. :src="avatarUrl"
  12. class="avatar-image"
  13. mode="aspectFill"
  14. ></image>
  15. </view>
  16. </button>
  17. </view>
  18. </view>
  19. <view class="user-info-section">
  20. <view class="user-info-title">
  21. 基本信息
  22. </view>
  23. <view class="user-info-form">
  24. <view class="user-info-item">
  25. <view class="user-info-label">昵称</view>
  26. <view class="user-info-input">
  27. <u-input v-model="nickname" placeholder="请输入昵称" :border="false" />
  28. </view>
  29. </view>
  30. <!-- <view class="user-info-item">
  31. <view class="user-info-label">会员等级</view>
  32. <view class="user-info-value">
  33. <text>{{userLevel}}</text>
  34. </view>
  35. </view> -->
  36. </view>
  37. </view>
  38. <view class="user-info-btns">
  39. <view class="user-info-btn" @click="save">
  40. <u-button color="#FFBF60" :loading="loading">
  41. <view style="color: #fff;">
  42. 保存
  43. </view>
  44. </u-button>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import { updateInfo } from '@/api/system/user'
  51. import {getPersonalInfo} from "@/api/system/personal.js"
  52. export default {
  53. data() {
  54. return {
  55. loading: false,
  56. fileList: [],
  57. avatarUrl: 'https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/avatar_1.png',
  58. nickname: '',
  59. userLevel: '',
  60. userInfoForm: {
  61. headImage: ''
  62. }
  63. }
  64. },
  65. onLoad() {
  66. this.getUserInfo()
  67. },
  68. methods: {
  69. // 获取用户信息
  70. getUserInfo() {
  71. getPersonalInfo().then(res => {
  72. if (res && (res.id || res.id === 0)) {
  73. this.nickname = res.nickname || ''
  74. this.userLevel = res.level || ''
  75. if (res.avatar) {
  76. this.avatarUrl = res.avatar
  77. this.userInfoForm.headImage = res.avatar
  78. this.fileList = [{url: res.avatar}]
  79. }
  80. }
  81. })
  82. },
  83. // 微信小程序头像选择
  84. async onChooseAvatar(e) {
  85. const { avatarUrl } = e.detail
  86. try {
  87. // 上传头像到服务器
  88. const result = await this.uploadFilePromise(avatarUrl)
  89. this.avatarUrl = result
  90. this.userInfoForm.headImage = result
  91. uni.showToast({
  92. title: '头像更新成功',
  93. icon: 'success'
  94. })
  95. } catch (error) {
  96. uni.showToast({
  97. title: '头像上传失败',
  98. icon: 'none'
  99. })
  100. }
  101. },
  102. uploadFilePromise(url) {
  103. return new Promise((resolve, reject) => {
  104. let a = uni.uploadFile({
  105. url: 'https://store-test.catmdogd.com/test-api/h5/oss/upload',
  106. filePath: url,
  107. name: 'file',
  108. formData: {
  109. user: 'test'
  110. },
  111. success: (res) => {
  112. setTimeout(() => {
  113. if(res && res.data){
  114. let resData = JSON.parse(res.data);
  115. resolve(resData.url);
  116. }
  117. reject("上传失败");
  118. }, 1000)
  119. }
  120. });
  121. })
  122. },
  123. // 保存用户信息
  124. save() {
  125. if (!this.nickname) {
  126. this.$modal.showToast('请输入昵称!')
  127. return
  128. }
  129. this.loading = true
  130. let params = {
  131. nickname: this.nickname,
  132. avatar: this.avatarUrl
  133. }
  134. updateInfo(params).then(res => {
  135. if (res && res.code == 200) {
  136. uni.showToast({
  137. title: '保存成功',
  138. duration: 3000,
  139. icon: "none"
  140. })
  141. setTimeout(() => {
  142. this.loading = false
  143. let len = getCurrentPages().length
  144. if (len >= 2) {
  145. uni.navigateBack()
  146. } else {
  147. uni.redirectTo({url: '/pages/personalCenter/index'})
  148. }
  149. }, 1000)
  150. } else {
  151. this.loading = false
  152. uni.showToast({
  153. title: '更新用户信息失败',
  154. duration: 3000,
  155. icon: "none"
  156. })
  157. }
  158. }).catch(() => {
  159. this.loading = false
  160. uni.showToast({
  161. title: '更新用户信息失败',
  162. duration: 3000,
  163. icon: "none"
  164. })
  165. })
  166. }
  167. }
  168. }
  169. </script>
  170. <style lang="scss">
  171. .user-info-container {
  172. position: relative;
  173. height: 100%;
  174. padding-bottom: 90px;
  175. .user-avatar-section {
  176. width: 100%;
  177. background-color: #fff;
  178. padding: 15px 20px;
  179. margin-bottom: 10px;
  180. }
  181. .user-info-section {
  182. width: 100%;
  183. background-color: #fff;
  184. padding: 15px 20px;
  185. }
  186. .user-info-title {
  187. font-size: 14px;
  188. color: #333;
  189. font-weight: bold;
  190. padding-bottom: 15px;
  191. }
  192. .user-info-form {
  193. width: 100%;
  194. }
  195. .user-info-item {
  196. display: flex;
  197. align-items: center;
  198. padding: 12px 0;
  199. border-bottom: 1px solid #efefef;
  200. &:last-child {
  201. border-bottom: none;
  202. }
  203. }
  204. .user-info-label {
  205. width: 80px;
  206. color: #333;
  207. font-size: 14px;
  208. }
  209. .user-info-input {
  210. flex: 1;
  211. }
  212. .user-info-value {
  213. flex: 1;
  214. color: #666;
  215. font-size: 14px;
  216. }
  217. .user-info-btns {
  218. background-color: #FFFFFF;
  219. padding: 10px 20px 40px;
  220. width: 100%;
  221. height: 90px;
  222. position: fixed;
  223. bottom: 0;
  224. z-index: 100;
  225. text-align: center;
  226. .user-info-btn {
  227. width: 80%;
  228. margin: 0 auto;
  229. }
  230. }
  231. }
  232. .user-avatar-circle-edit {
  233. width: 80px !important;
  234. height: 80px !important;
  235. border-radius: 50% !important;
  236. object-fit: cover !important;
  237. display: block !important;
  238. }
  239. // 头像选择相关样式
  240. .avatar-container {
  241. display: flex;
  242. justify-content: center;
  243. align-items: center;
  244. padding: 20px 0;
  245. }
  246. .choose-avatar-btn {
  247. background: none;
  248. border: none;
  249. padding: 0;
  250. margin: 0;
  251. position: relative;
  252. border-radius: 50%;
  253. overflow: visible;
  254. &::after {
  255. border: none;
  256. }
  257. }
  258. .avatar-wrapper {
  259. position: relative;
  260. width: 100px;
  261. height: 100px;
  262. border-radius: 50%;
  263. overflow: hidden;
  264. border: 3px solid #f0f0f0;
  265. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  266. transition: all 0.3s ease;
  267. &:active {
  268. transform: scale(0.95);
  269. box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
  270. }
  271. }
  272. .avatar-image {
  273. width: 100%;
  274. height: 100%;
  275. object-fit: cover;
  276. display: block;
  277. }
  278. .avatar-edit-icon {
  279. position: absolute;
  280. bottom: -5px;
  281. right: -5px;
  282. width: 30px;
  283. height: 30px;
  284. background: #FFBF60;
  285. border-radius: 50%;
  286. display: flex;
  287. align-items: center;
  288. justify-content: center;
  289. border: 2px solid #fff;
  290. box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
  291. }
  292. .edit-text {
  293. font-size: 10px;
  294. color: #fff;
  295. font-weight: bold;
  296. }
  297. </style>