avatar.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view>
  3. <u-cell-group>
  4. <u-cell-item title="头像" @click="editAvatar">
  5. <image :src="avatarUrl" class="avatar"></image>
  6. </u-cell-item>
  7. <u-cell-item title="昵称" :value="nickname" @click="editNickName"></u-cell-item>
  8. </u-cell-group>
  9. <u-modal v-model="show" :show-cancel-button="true" title="修改昵称">
  10. <view class="slot-content">
  11. <u-input v-model="nickname" />
  12. </view>
  13. </u-modal>
  14. <view style="position: fixed;bottom: 50rpx;width: 100%;padding: 20rpx;">
  15. <button @click="submitForm" class="submit_btn">保存</button>
  16. <button @click="exit" class="exit_btn">退出登录</button>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import {mapGetters} from 'vuex';
  22. export default {
  23. data() {
  24. return {
  25. show: false,
  26. avatarUrl:'',
  27. nickname:'',
  28. url:''
  29. }
  30. },
  31. onLoad(option) {
  32. this.avatarUrl = this.$method.splitImgHost(this.userInfo.avatar)
  33. this.nickname = this.userInfo.nickname
  34. },
  35. methods: {
  36. imageInfos(){
  37. var self = this
  38. return new Promise((resolve, reject) => {
  39. uni.getImageInfo({
  40. src: self.avatarUrl,
  41. success: async res => {
  42. let canvasWidth = res.width; //图片原始长宽
  43. let canvasHeight = res.height;
  44. if (canvasWidth > 1000 || canvasHeight > 1000) {
  45. uni.compressImage({
  46. src: self.avatarUrl,
  47. quality: 75,
  48. width: '50%',
  49. height: '50%',
  50. success: async rest => {
  51. const waitUpload = await self.uploadFile(rest.tempFilePath, 0);
  52. resolve()
  53. }
  54. });
  55. } else {
  56. console.log('无需压缩');
  57. const waitUpload = await self.uploadFile(self.avatarUrl, 0);
  58. resolve()
  59. }
  60. }
  61. });
  62. });
  63. },
  64. uploadFile(options, int) {
  65. return new Promise((resolve, reject) => {
  66. var self = this;
  67. var data = {
  68. imageStatus: int
  69. };
  70. this.$api.aliyunpolicy(data).then(res => {
  71. var ossToken = res.data.data.resultContent;
  72. uni.uploadFile({
  73. url: ossToken.host,
  74. name: 'file',
  75. filePath: options,
  76. fileType: 'image',
  77. header: {
  78. AuthorizationToken: 'WX ' + uni.getStorageSync('token')
  79. },
  80. formData: {
  81. key: ossToken.dir,
  82. OSSAccessKeyId: ossToken.accessid,
  83. policy: ossToken.policy,
  84. Signature: ossToken.signature,
  85. callback: ossToken.callback,
  86. success_action_status: 200
  87. },
  88. success: result => {
  89. if (result.statusCode === 200) {
  90. uni.showToast({
  91. title: '成功',
  92. icon: 'none'
  93. });
  94. self.userInfo.avatar = ossToken.dir;
  95. resolve();
  96. } else {
  97. uni.showToast({
  98. title: '上传失败',
  99. icon: 'none'
  100. });
  101. return;
  102. }
  103. },
  104. fail: error => {
  105. uni.showToast({
  106. title: '上传接口报错',
  107. icon: 'none'
  108. });
  109. return;
  110. }
  111. });
  112. });
  113. });
  114. },
  115. updateInfo(){
  116. let self = this
  117. this.$api.appuserInfo(this.userInfo).then(res => {
  118. if (res.data.code === 200) {
  119. uni.showToast({
  120. title: '提交成功',
  121. icon: 'none'
  122. });
  123. self.$api.refreshUserInfo()
  124. }
  125. });
  126. },
  127. submitForm(){
  128. if(this.nickname==''){
  129. uni.showModal({
  130. title: "提示",
  131. content: '昵称不能为空',
  132. showCancel: false
  133. })
  134. return
  135. }
  136. this.userInfo.nickname=this.nickname
  137. this.submit()
  138. },
  139. async submit(){
  140. if(this.avatarUrl!=this.userInfo.avatar){
  141. const waitYS = await this.imageInfos();
  142. }
  143. this.updateInfo()
  144. },
  145. exit(){
  146. this.$method.exit()
  147. },
  148. editNickName(){
  149. this.show = true
  150. },
  151. editAvatar(){
  152. let that = this
  153. uni.chooseImage({
  154. count: 1, //默认9
  155. sizeType: [ 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  156. success: function (res) {
  157. that.avatarUrl = res.tempFilePaths[0]
  158. }
  159. });
  160. }
  161. },
  162. computed: {...mapGetters(['userInfo'])}
  163. }
  164. </script>
  165. <style scope>
  166. .exit_btn{
  167. font-size: 30rpx;
  168. margin-top: 30rpx;
  169. padding: 5rpx;
  170. }
  171. .submit_btn{
  172. background-color: #32467B !important;
  173. border-color: #32467B !important;
  174. color: #FFFFFF;
  175. font-size: 30rpx;
  176. padding: 5rpx;
  177. }
  178. .slot-content{
  179. margin: 30rpx;
  180. color: #606266;
  181. }
  182. .avatar{
  183. height: 70rpx;
  184. width: 70rpx;
  185. border-radius: 50%;
  186. }
  187. page {
  188. background: #FFFFFF;
  189. }
  190. </style>