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.

257 lines
6.2 KiB

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