avatar.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. console.log(`当前画布大小:${info.width}*${info.height}`)
  58. },
  59. cropped(imagePath, imageInfo) {
  60. console.log("imagePath",imagePath)
  61. this.srcData = imagePath;
  62. },
  63. /************************************************************************************************/
  64. upImg(){
  65. let self = this;
  66. uni.showModal({
  67. content: '确认提交吗?',
  68. cancelColor:"#999",
  69. confirmColor:"#0183FA",
  70. success: function (res) {
  71. if (res.confirm) {
  72. self.uploadImg(self.srcData);
  73. console.log('用户点击确定');
  74. } else if (res.cancel) {
  75. console.log('用户点击取消');
  76. }
  77. }
  78. });
  79. },
  80. backButton(){
  81. uni.navigateBack();
  82. },
  83. async uploadImg(tempFilePaths){
  84. var self = this;
  85. uni.showLoading({
  86. title: '上传中',
  87. mask: true
  88. });
  89. uni.uploadFile({
  90. url: config.base_url+'/file/upload', //仅为示例,非真实的接口地址
  91. header:{'Authorization':uni.getStorageSync('token')},
  92. filePath: tempFilePaths,
  93. name: 'file',
  94. formData: {
  95. 'user': 'test'
  96. },
  97. success: (uploadFileRes) => {
  98. let res = JSON.parse(uploadFileRes.data);
  99. if(res.code == 200){
  100. self.appAvatar(res.data.url);
  101. }else{
  102. uni.showToast({
  103. title: res.msg,
  104. icon:"none",
  105. mask:true,
  106. duration: 2000
  107. });
  108. }
  109. },
  110. fail: err => {},
  111. complete: () => {
  112. uni.hideLoading()
  113. }
  114. });
  115. },
  116. //修改头像
  117. async appAvatar(url){
  118. let obj = {
  119. imgUrl:url
  120. }
  121. const {data} = await appAvatar(obj);
  122. if(data.code == 200){
  123. uni.showToast({
  124. title:'上传成功',
  125. icon:"none",
  126. mask:true,
  127. duration: 2000
  128. });
  129. setTimeout(function(){
  130. uni.navigateBack();
  131. },2000);
  132. }
  133. },
  134. }
  135. }
  136. </script>
  137. <style lang="stylus" scoped>
  138. .avatar-page{
  139. height:100%;
  140. width:100%
  141. .left-button{
  142. background #E0E0E0;
  143. color:#333;
  144. text-align center;
  145. font-size:26rpx;
  146. width:140rpx;
  147. height:60rpx;
  148. line-height:60rpx;
  149. position fixed;
  150. left:40rpx;
  151. bottom:40rpx;
  152. z-index:100;
  153. border-radius:10rpx;
  154. }
  155. .right-button{
  156. background #007AFF;
  157. color:#fff;
  158. text-align center;
  159. font-size:26rpx;
  160. width:140rpx;
  161. height:60rpx;
  162. line-height:60rpx;
  163. position fixed;
  164. right:40rpx;
  165. bottom:40rpx;
  166. z-index:100;
  167. border-radius:10rpx;
  168. }
  169. }
  170. </style>