userInfo.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view class="app">
  3. <view class="cell">
  4. <text class="tit fill">头像</text>
  5. <view class="avatar-wrap" @click="chooseImage">
  6. <image class="avatar" :src="tempAvatar || userInfo.avatar || '/static/icon/default-avatar.png'" mode="aspectFill"></image>
  7. <!-- 进度遮盖 -->
  8. <view class="progress center"
  9. :class="{
  10. 'no-transtion': uploadProgress === 0,
  11. show: uploadProgress != 100
  12. }"
  13. :style="{
  14. width: uploadProgress + '%',
  15. height: uploadProgress + '%',
  16. }"></view>
  17. </view>
  18. </view>
  19. <view class="cell b-b">
  20. <text class="tit fill">昵称</text>
  21. <input class="input" v-model="userInfo.nickname" type="text" maxlength="8" placeholder="请输入昵称" placeholder-class="placeholder">
  22. </view>
  23. <mix-button ref="confirmBtn" text="保存资料" marginTop="80rpx" @onConfirm="confirm"></mix-button>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. uploadProgress: 100, //头像上传进度
  31. tempAvatar: '',
  32. userInfo: {},
  33. }
  34. },
  35. computed: {
  36. curUserInfo(){
  37. return this.$store.state.userInfo
  38. }
  39. },
  40. watch: {
  41. curUserInfo(curUserInfo){
  42. const {avatar, nickname, gender} = curUserInfo;
  43. this.userInfo = {avatar, nickname, gender,};
  44. }
  45. },
  46. onLoad() {
  47. const {avatar, nickname, gender, anonymous} = this.curUserInfo;
  48. this.userInfo = {avatar, nickname, gender};
  49. },
  50. methods: {
  51. // 提交修改
  52. async confirm() {
  53. // 校验信息是否变化
  54. const {uploadProgress, userInfo, curUserInfo} = this;
  55. let isUpdate = false;
  56. for (let key in userInfo) {
  57. if(userInfo[key] !== curUserInfo[key]){
  58. isUpdate = true;
  59. break;
  60. }
  61. }
  62. if (isUpdate === false) {
  63. this.$util.msg('信息未修改');
  64. this.$refs.confirmBtn.stop();
  65. return;
  66. }
  67. if (!userInfo.avatar) {
  68. this.$util.msg('请上传头像');
  69. this.$refs.confirmBtn.stop();
  70. return;
  71. }
  72. if (uploadProgress !== 100) {
  73. this.$util.msg('请等待头像上传完毕');
  74. this.$refs.confirmBtn.stop();
  75. return;
  76. }
  77. if (!userInfo.nickname) {
  78. this.$util.msg('请输入您的昵称');
  79. this.$refs.confirmBtn.stop();
  80. return;
  81. }
  82. const res = await this.$request('user', 'update', userInfo);
  83. this.$refs.confirmBtn.stop();
  84. this.$util.msg(res.msg);
  85. if(res.status === 1){
  86. this.$store.dispatch('getUserInfo'); //刷新用户信息
  87. setTimeout(()=>{
  88. uni.navigateBack();
  89. }, 1000)
  90. }
  91. },
  92. // 选择头像
  93. chooseImage(){
  94. uni.chooseImage({
  95. count: 1,
  96. success: res=> {
  97. uni.navigateTo({
  98. url: `./cutImage/cut?src=${res.tempFilePaths[0]}`
  99. });
  100. }
  101. });
  102. },
  103. // 裁剪回调
  104. async setAvatar(filePath){
  105. this.tempAvatar = filePath;
  106. this.uploadProgress = 0;
  107. const result = await uniCloud.uploadFile({
  108. filePath: filePath,
  109. cloudPath: + new Date() + ('000000' + Math.floor(Math.random() * 999999)).slice(-6) + '.jpg',
  110. onUploadProgress: e=> {
  111. this.uploadProgress = Math.round(
  112. (e.loaded * 100) / e.total
  113. );
  114. }
  115. });
  116. if(!result.fileID){
  117. this.$util.msg('头像上传失败');
  118. return;
  119. }
  120. if(typeof uniCloud.getTempFileURL === 'undefined'){
  121. this.userInfo.avatar = result.fileID;
  122. }else{
  123. const tempFiles = await uniCloud.getTempFileURL({
  124. fileList: [result.fileID]
  125. })
  126. const tempFile = tempFiles.fileList[0];
  127. if(tempFile.download_url || tempFile.fileID){
  128. this.userInfo.avatar = tempFile.download_url || tempFile.fileID;
  129. }else{
  130. this.$util.msg('头像上传失败');
  131. }
  132. }
  133. }
  134. }
  135. }
  136. </script>
  137. <style scoped lang="scss">
  138. .app{
  139. padding-top: 16rpx;
  140. }
  141. .cell{
  142. display: flex;
  143. align-items: center;
  144. min-height: 110rpx;
  145. padding: 0 40rpx;
  146. &:first-child{
  147. margin-bottom: 10rpx;
  148. }
  149. &:after{
  150. left: 40rpx;
  151. right: 40rpx;
  152. border-color: #d8d8d8;
  153. }
  154. .tit{
  155. font-size: 30rpx;
  156. color: #333;
  157. }
  158. .avatar-wrap{
  159. width: 120rpx;
  160. height: 120rpx;
  161. position: relative;
  162. border-radius: 100rpx;
  163. overflow: hidden;
  164. .avatar{
  165. width: 100%;
  166. height: 100%;
  167. border-radius: 100rpx;
  168. }
  169. .progress{
  170. position: absolute;
  171. left: 50%;
  172. top: 50%;
  173. transform: translate(-50%, -50%);
  174. width: 100rpx;
  175. height: 100rpx;
  176. box-shadow: rgba(0,0,0,.6) 0px 0px 0px 2005px;
  177. border-radius: 100rpx;
  178. transition: .5s;
  179. opacity: 0;
  180. &.no-transtion{
  181. transition: 0s;
  182. }
  183. &.show{
  184. opacity: 1;
  185. }
  186. }
  187. }
  188. .input{
  189. flex: 1;
  190. text-align: right;
  191. font-size: 28rpx;
  192. color: #333;
  193. }
  194. switch{
  195. margin: 0;
  196. transform: scale(0.8) translateX(10rpx);
  197. transform-origin: center right;
  198. }
  199. .tip{
  200. margin-left: 20rpx;
  201. font-size: 28rpx;
  202. color: #999;
  203. }
  204. .checkbox{
  205. padding: 12rpx 0 12rpx 40rpx;
  206. font-size: 28rpx;
  207. color: #333;
  208. .mix-icon{
  209. margin-right: 12rpx;
  210. font-size: 36rpx;
  211. color: #ccc;
  212. }
  213. .icon-xuanzhong{
  214. color: $base-color;
  215. }
  216. }
  217. }
  218. </style>