avatar.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. var self = this;
  66. return new Promise((resolve, reject) => {
  67. var data = {
  68. imageStatus: int
  69. };
  70. self.$api.aliyunpolicy(data).then(res => {
  71. console.log(res.data,6)
  72. if(res.data.code!=200){
  73. self.$method.showToast('签名错误'+JSON.stringify(res.data))
  74. return
  75. }
  76. var ossToken = res.data.data.resultContent;
  77. if(ossToken.host==null||ossToken.host==undefined){
  78. self.$method.showToast('上传路径报错'+JSON.stringify(res.data))
  79. return
  80. }
  81. uni.uploadFile({
  82. url: ossToken.host,
  83. name: 'file',
  84. filePath: options,
  85. fileType: 'image',
  86. header: {
  87. AuthorizationToken: 'WX ' + uni.getStorageSync('token')
  88. },
  89. formData: {
  90. key: ossToken.dir,
  91. OSSAccessKeyId: ossToken.accessid,
  92. policy: ossToken.policy,
  93. Signature: ossToken.signature,
  94. callback: ossToken.callback,
  95. success_action_status: 200
  96. },
  97. success: result => {
  98. if (result.statusCode === 200) {
  99. self.userInfo.avatar = ossToken.dir;
  100. resolve();
  101. } else {
  102. uni.showToast({
  103. title: '上传失败',
  104. icon: 'none'
  105. });
  106. return;
  107. }
  108. },
  109. fail: error => {
  110. uni.showToast({
  111. title: '上传接口报错'+error,
  112. icon: 'none'
  113. });
  114. return;
  115. }
  116. });
  117. });
  118. });
  119. },
  120. updateInfo(){
  121. let self = this
  122. this.$api.appuserInfo(this.userInfo).then(res => {
  123. if (res.data.code === 200) {
  124. self.$method.showToast('提交成功')
  125. self.$api.refreshUserInfo()
  126. }
  127. });
  128. },
  129. submitForm(){
  130. if(this.nickname==''){
  131. uni.showModal({
  132. title: "提示",
  133. content: '昵称不能为空',
  134. showCancel: false
  135. })
  136. return
  137. }
  138. this.userInfo.nickname=this.nickname
  139. this.submit()
  140. },
  141. async submit(){
  142. if(this.avatarUrl!=this.$method.splitImgHost(this.userInfo.avatar)){
  143. const waitYS = await this.imageInfos();
  144. }
  145. this.updateInfo()
  146. },
  147. exit(){
  148. this.$method.exit()
  149. },
  150. editNickName(){
  151. this.show = true
  152. },
  153. editAvatar(){
  154. let that = this
  155. uni.chooseImage({
  156. count: 1, //默认9
  157. sizeType: [ 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  158. success: function (res) {
  159. that.avatarUrl = res.tempFilePaths[0]
  160. }
  161. });
  162. }
  163. },
  164. computed: {...mapGetters(['userInfo'])}
  165. }
  166. </script>
  167. <style scope>
  168. .exit_btn{
  169. font-size: 30rpx;
  170. margin-top: 30rpx;
  171. padding: 5rpx;
  172. }
  173. .submit_btn{
  174. background-color: #32467B !important;
  175. border-color: #32467B !important;
  176. color: #FFFFFF;
  177. font-size: 30rpx;
  178. padding: 5rpx;
  179. }
  180. .slot-content{
  181. margin: 30rpx;
  182. color: #606266;
  183. }
  184. .avatar{
  185. height: 70rpx;
  186. width: 70rpx;
  187. border-radius: 50%;
  188. }
  189. page {
  190. background: #FFFFFF;
  191. }
  192. </style>