avatar.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!-- 修改头像 -->
  2. <template>
  3. <view class="avatar-page">
  4. <view style="height:100%;width:100%;" v-if="src&&src!='null'">
  5. <image-cropper
  6. id="image-cropper"
  7. :zoom="1"
  8. :angle="0"
  9. :src="src"
  10. canvasBackground="red"
  11. @cropped="cropped"
  12. @afterDraw="afterDraw"
  13. @beforeDraw="beforeDraw"/>
  14. </view>
  15. <view class="left-button" @click="backButton">取消</view>
  16. <view class="right-button" @click="upImg">确定</view>
  17. </view>
  18. </template>
  19. <script>
  20. import { config } from '@/api/request/config.js'
  21. import { appAvatar } from '@/api/index.js'
  22. import ImageCropper from '@/component/cropper.vue'
  23. export default {
  24. components: {
  25. ImageCropper
  26. },
  27. data() {
  28. return {
  29. src:"",
  30. srcData:"",
  31. }
  32. },
  33. onLoad(option){
  34. this.src = option.src;
  35. },
  36. onShow(){
  37. },
  38. methods:{
  39. /************************************************修改头像************************************************/
  40. selectImg() {
  41. uni.chooseImage({
  42. count: 1,
  43. sizeType: ['original'],
  44. sourceType: ['album', 'camera'],
  45. success: res => {
  46. var tempFilePaths = res.tempFilePaths
  47. this.src = tempFilePaths[0]
  48. }
  49. })
  50. },
  51. beforeDraw(context, transform) {
  52. context.setFillStyle('yellow')
  53. transform.zoom = 2
  54. },
  55. afterDraw(ctx, info) {
  56. ctx.fillText('', 20, 20)
  57. },
  58. cropped(imagePath, imageInfo) {
  59. this.srcData = imagePath;
  60. },
  61. /************************************************************************************************/
  62. upImg(){
  63. let self = this;
  64. uni.showModal({
  65. content: '确认提交吗?',
  66. cancelColor:"#999",
  67. confirmColor:"#0183FA",
  68. success: function (res) {
  69. if (res.confirm) {
  70. self.uploadImg(self.srcData);
  71. } else if (res.cancel) {
  72. }
  73. }
  74. });
  75. },
  76. backButton(){
  77. uni.navigateBack();
  78. },
  79. async uploadImg(tempFilePaths){
  80. var self = this;
  81. uni.showLoading({
  82. title: '上传中',
  83. mask: true
  84. });
  85. uni.uploadFile({
  86. url: config.base_url+'/base/file/upload', //仅为示例,非真实的接口地址
  87. header:{'Authorization':uni.getStorageSync('token')},
  88. filePath: tempFilePaths,
  89. name: 'file',
  90. formData: {
  91. 'user': 'test'
  92. },
  93. success: (uploadFileRes) => {
  94. let res = JSON.parse(uploadFileRes.data);
  95. if(res.code == 200){
  96. self.appAvatar(res.data.url);
  97. }else{
  98. uni.showToast({
  99. title: res.msg,
  100. icon:"none",
  101. mask:true,
  102. duration: 2000
  103. });
  104. }
  105. },
  106. fail: err => {},
  107. complete: () => {
  108. uni.hideLoading()
  109. }
  110. });
  111. },
  112. //修改头像
  113. async appAvatar(url){
  114. let obj = {
  115. imgUrl:url
  116. }
  117. const {data} = await appAvatar(obj);
  118. if(data.code == 200){
  119. uni.showToast({
  120. title:'上传成功',
  121. icon:"none",
  122. mask:true,
  123. duration: 2000
  124. });
  125. setTimeout(function(){
  126. uni.navigateBack();
  127. },2000);
  128. }
  129. },
  130. }
  131. }
  132. </script>
  133. <style lang="stylus" scoped>
  134. .avatar-page{
  135. height:100%;
  136. width:100%
  137. .left-button{
  138. background #E0E0E0;
  139. color:#333;
  140. text-align center;
  141. font-size:26rpx;
  142. width:140rpx;
  143. height:60rpx;
  144. line-height:60rpx;
  145. position fixed;
  146. left:40rpx;
  147. bottom:40rpx;
  148. z-index:100;
  149. border-radius:10rpx;
  150. }
  151. .right-button{
  152. background #007AFF;
  153. color:#fff;
  154. text-align center;
  155. font-size:26rpx;
  156. width:140rpx;
  157. height:60rpx;
  158. line-height:60rpx;
  159. position fixed;
  160. right:40rpx;
  161. bottom:40rpx;
  162. z-index:100;
  163. border-radius:10rpx;
  164. }
  165. }
  166. </style>