avatar.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 { systemMineUserEdit } from '@/pages_basics/api/index.js'
  22. import ImageCropper from '@/pages_basics/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+'/system/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.systemMineUserEdit(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 systemMineUserEdit(url){
  115. let obj = {
  116. userId:uni.getStorageSync('userId'),
  117. avatar:url,
  118. }
  119. const {data} = await systemMineUserEdit(obj);
  120. if(data.code == 200){
  121. uni.showToast({
  122. title:'上传成功',
  123. icon:"none",
  124. mask:true,
  125. duration: 2000
  126. });
  127. setTimeout(function(){
  128. uni.navigateBack();
  129. },2000);
  130. }
  131. },
  132. }
  133. }
  134. </script>
  135. <style lang="stylus" scoped>
  136. .avatar-page{
  137. height:100%;
  138. width:100%
  139. .left-button{
  140. background #E0E0E0;
  141. color:#333;
  142. text-align center;
  143. font-size:26rpx;
  144. width:140rpx;
  145. height:60rpx;
  146. line-height:60rpx;
  147. position fixed;
  148. left:40rpx;
  149. bottom:40rpx;
  150. z-index:100;
  151. border-radius:10rpx;
  152. }
  153. .right-button{
  154. background #007AFF;
  155. color:#fff;
  156. text-align center;
  157. font-size:26rpx;
  158. width:140rpx;
  159. height:60rpx;
  160. line-height:60rpx;
  161. position fixed;
  162. right:40rpx;
  163. bottom:40rpx;
  164. z-index:100;
  165. border-radius:10rpx;
  166. }
  167. }
  168. </style>