userInfo.vue 5.7 KB

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