avatar.vue 4.9 KB

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