avatar.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. console.log(tempFilePaths)
  81. var self = this;
  82. uni.showLoading({
  83. title: '上传中',
  84. mask: true
  85. });
  86. uni.uploadFile({
  87. url: config.base_url+'/base/file/upload', //仅为示例,非真实的接口地址
  88. header:{'Authorization':uni.getStorageSync('token')},
  89. filePath: tempFilePaths,
  90. name: 'file',
  91. formData: {
  92. 'user': 'test'
  93. },
  94. success: (uploadFileRes) => {
  95. let res = JSON.parse(uploadFileRes.data);
  96. if(res.code == 200){
  97. self.appAvatar(res.data.url);
  98. }else{
  99. uni.showToast({
  100. title: res.msg,
  101. icon:"none",
  102. mask:true,
  103. duration: 2000
  104. });
  105. }
  106. },
  107. fail: err => {},
  108. complete: () => {
  109. uni.hideLoading()
  110. }
  111. });
  112. },
  113. //修改头像
  114. async appAvatar(url){
  115. let obj = {
  116. imgUrl:url
  117. }
  118. const {data} = await appAvatar(obj);
  119. if(data.code == 200){
  120. uni.showToast({
  121. title:'上传成功',
  122. icon:"none",
  123. mask:true,
  124. duration: 2000
  125. });
  126. setTimeout(function(){
  127. uni.navigateBack();
  128. },2000);
  129. }
  130. },
  131. }
  132. }
  133. </script>
  134. <style lang="stylus" scoped>
  135. .avatar-page{
  136. height:100%;
  137. width:100%
  138. .left-button{
  139. background #E0E0E0;
  140. color:#333;
  141. text-align center;
  142. font-size:26rpx;
  143. width:140rpx;
  144. height:60rpx;
  145. line-height:60rpx;
  146. position fixed;
  147. left:40rpx;
  148. bottom:40rpx;
  149. z-index:100;
  150. border-radius:10rpx;
  151. }
  152. .right-button{
  153. background #007AFF;
  154. color:#fff;
  155. text-align center;
  156. font-size:26rpx;
  157. width:140rpx;
  158. height:60rpx;
  159. line-height:60rpx;
  160. position fixed;
  161. right:40rpx;
  162. bottom:40rpx;
  163. z-index:100;
  164. border-radius:10rpx;
  165. }
  166. }
  167. </style>