猫妈狗爸伴宠师小程序前端代码
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.

260 lines
5.6 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 style="display: flex;justify-content: center;">
  8. <u-upload
  9. accept="image"
  10. :capture="['album','camera']"
  11. :fileList="fileList"
  12. @afterRead="afterRead"
  13. @delete="deletePic"
  14. :max-count="1"
  15. name="avatar"
  16. width="80"
  17. height="80"
  18. :custom-style="{flex:0}"
  19. >
  20. <image :src="avatarUrl" style="width: 80px;height: 80px;border-radius: 50%;"></image>
  21. </u-upload>
  22. </view>
  23. </view>
  24. <view class="user-info-section">
  25. <view class="user-info-title">
  26. 基本信息
  27. </view>
  28. <view class="user-info-form">
  29. <view class="user-info-item">
  30. <view class="user-info-label">昵称</view>
  31. <view class="user-info-input">
  32. <u-input v-model="nickname" placeholder="请输入昵称" :border="false" />
  33. </view>
  34. </view>
  35. <!-- <view class="user-info-item">
  36. <view class="user-info-label">会员等级</view>
  37. <view class="user-info-value">
  38. <text>{{userLevel}}</text>
  39. </view>
  40. </view> -->
  41. </view>
  42. </view>
  43. <view class="user-info-btns">
  44. <view class="user-info-btn" @click="save">
  45. <u-button color="#FFBF60" :loading="loading">
  46. <view style="color: #fff;">
  47. 保存
  48. </view>
  49. </u-button>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import {updateUserProfile, uploadAvatar} from '@/api/system/user'
  56. import {getPersonalInfo} from "@/api/system/personal.js"
  57. export default {
  58. data() {
  59. return {
  60. loading: false,
  61. fileList: [],
  62. avatarUrl: 'https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/avatar_1.png',
  63. nickname: '',
  64. userLevel: ''
  65. }
  66. },
  67. onLoad() {
  68. this.getUserInfo()
  69. },
  70. methods: {
  71. // 获取用户信息
  72. getUserInfo() {
  73. getPersonalInfo().then(res => {
  74. if (res && (res.id || res.id === 0)) {
  75. this.nickname = res.nickname || ''
  76. this.userLevel = res.level || ''
  77. if (res.avatar) {
  78. this.avatarUrl = res.avatar
  79. this.fileList = [{url: res.avatar}]
  80. }
  81. }
  82. })
  83. },
  84. // 删除图片
  85. deletePic(event) {
  86. this.fileList.splice(event.index, 1)
  87. this.avatarUrl = 'https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/index/avatar_1.png'
  88. },
  89. // 新增图片
  90. async afterRead(event) {
  91. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  92. let lists = [].concat(event.file)
  93. let fileListLen = this.fileList.length
  94. lists.map((item) => {
  95. this.fileList.push({
  96. ...item,
  97. status: 'uploading',
  98. message: '上传中'
  99. })
  100. })
  101. for (let i = 0; i < lists.length; i++) {
  102. const result = await this.uploadFilePromise(lists[i].url)
  103. let item = this.fileList[fileListLen]
  104. this.fileList.splice(fileListLen, 1, Object.assign(item, {
  105. status: 'success',
  106. message: '',
  107. url: result
  108. }))
  109. this.avatarUrl = result
  110. fileListLen++
  111. }
  112. },
  113. uploadFilePromise(url) {
  114. return new Promise((resolve, reject) => {
  115. let a = uni.uploadFile({
  116. url: 'https://store-test.catmdogd.com/test-api/h5/oss/upload',
  117. filePath: url,
  118. name: 'file',
  119. formData: {
  120. user: 'test'
  121. },
  122. success: (res) => {
  123. setTimeout(() => {
  124. if(res && res.data){
  125. let resData = JSON.parse(res.data);
  126. resolve(resData.url);
  127. }
  128. reject("上传失败");
  129. }, 1000)
  130. }
  131. });
  132. })
  133. },
  134. // 保存用户信息
  135. save() {
  136. if (!this.nickname) {
  137. this.$modal.showToast('请输入昵称!')
  138. return
  139. }
  140. this.loading = true
  141. let params = {
  142. nickname: this.nickname,
  143. avatar: this.avatarUrl
  144. }
  145. updateUserProfile(params).then(res => {
  146. if (res && res.code == 200) {
  147. uni.showToast({
  148. title: '保存成功',
  149. duration: 3000,
  150. icon: "none"
  151. })
  152. setTimeout(() => {
  153. this.loading = false
  154. let len = getCurrentPages().length
  155. if (len >= 2) {
  156. uni.navigateBack()
  157. } else {
  158. uni.redirectTo({url: '/pages/personalCenter/index'})
  159. }
  160. }, 1000)
  161. } else {
  162. this.loading = false
  163. uni.showToast({
  164. title: '更新用户信息失败',
  165. duration: 3000,
  166. icon: "none"
  167. })
  168. }
  169. }).catch(() => {
  170. this.loading = false
  171. uni.showToast({
  172. title: '更新用户信息失败',
  173. duration: 3000,
  174. icon: "none"
  175. })
  176. })
  177. }
  178. }
  179. }
  180. </script>
  181. <style lang="scss">
  182. .user-info-container {
  183. position: relative;
  184. height: 100%;
  185. padding-bottom: 90px;
  186. .user-avatar-section {
  187. width: 100%;
  188. background-color: #fff;
  189. padding: 15px 20px;
  190. margin-bottom: 10px;
  191. }
  192. .user-info-section {
  193. width: 100%;
  194. background-color: #fff;
  195. padding: 15px 20px;
  196. }
  197. .user-info-title {
  198. font-size: 14px;
  199. color: #333;
  200. font-weight: bold;
  201. padding-bottom: 15px;
  202. }
  203. .user-info-form {
  204. width: 100%;
  205. }
  206. .user-info-item {
  207. display: flex;
  208. align-items: center;
  209. padding: 12px 0;
  210. border-bottom: 1px solid #efefef;
  211. &:last-child {
  212. border-bottom: none;
  213. }
  214. }
  215. .user-info-label {
  216. width: 80px;
  217. color: #333;
  218. font-size: 14px;
  219. }
  220. .user-info-input {
  221. flex: 1;
  222. }
  223. .user-info-value {
  224. flex: 1;
  225. color: #666;
  226. font-size: 14px;
  227. }
  228. .user-info-btns {
  229. background-color: #FFFFFF;
  230. padding: 10px 20px 40px;
  231. width: 100%;
  232. height: 90px;
  233. position: fixed;
  234. bottom: 0;
  235. z-index: 100;
  236. text-align: center;
  237. .user-info-btn {
  238. width: 80%;
  239. margin: 0 auto;
  240. }
  241. }
  242. }
  243. </style>