crop.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Created by 熊竹 on 2017/3/15.
  3. */
  4. Vue.component('crop-upload', {
  5. template: '<div >' +
  6. '<input type="file" style="display: none;" ref="input" @change="fileChange"/>' +
  7. '<div @click="chooseFile" class="crop-upload-box" :style="{height:height,width:width}">' +
  8. '<img v-if="src" :src="src" :style="{height:height,width:width,lineHeight:height}"/>' +
  9. '<i v-else :style="{height:height,width:width,lineHeight:height}" class="icon el-icon-plus"></i>' +
  10. '</div>' +
  11. '<div v-show="showCropper" class="crop-wrap" ref="wrap">' +
  12. '<img id="cropper" :src="raw" ref="cropper" :style="cropperStyle"/>' +
  13. '<div style="margin-top: 20px">' +
  14. '<el-button @click="cancel">取消</el-button>' +
  15. '<el-button type="primary" @click="confirm">确认</el-button>' +
  16. '</div>' +
  17. '</div>' +
  18. '</div>',
  19. props: {
  20. src: {
  21. type: String,
  22. default: ''
  23. },
  24. ratioX: {
  25. type: Number,
  26. default: 1
  27. },
  28. ratioY: {
  29. type: Number,
  30. default: 1
  31. }
  32. },
  33. data: function () {
  34. return {
  35. showCropper: false,
  36. raw: '',
  37. croperHeight: 0,
  38. croperWidth: 0
  39. }
  40. },
  41. computed: {
  42. height: function () {
  43. return 180 + 'px';
  44. },
  45. width: function () {
  46. return 180 * this.ratioX / this.ratioY + 'px';
  47. },
  48. cropperStyle: function () {
  49. return {
  50. height: this.croperHeight,
  51. width: this.croperWidth,
  52. maxWidth: '100px'
  53. }
  54. }
  55. },
  56. methods: {
  57. fileChange: function (res) {
  58. var reader = new FileReader();
  59. reader.onload = function () {
  60. var image = new Image();
  61. image.onload = function () {
  62. this.croperWidth = image.width;
  63. this.croperHeight = image.height;
  64. this.raw = image.src;
  65. this.showCropper = true;
  66. $('#cropper').Jcrop({
  67. aspectRatio: 1,
  68. allowSelect: false,
  69. bgOpacity: 0.3,
  70. setSelect: [0, 0, 100, 100],
  71. onChange: function (res) {
  72. console.log(res)
  73. }.bind(this)
  74. });
  75. }.bind(this);
  76. image.src = reader.result;
  77. }.bind(this);
  78. reader.readAsDataURL(this.$refs.input.files[0]);
  79. },
  80. chooseFile: function () {
  81. this.$refs.input.click();
  82. },
  83. upload: function () {
  84. var formData = new FormData();
  85. // 建立一个upload表单项,值为上传的文件
  86. formData.append('file', this.$refs.input.files[0]);
  87. var xhr = new XMLHttpRequest();
  88. xhr.open('POST', '../assets/uploadFile');
  89. // 定义上传完成后的回调函数
  90. xhr.onload = function () {
  91. if (xhr.status === 200) {
  92. var res = JSON.parse(xhr.response)
  93. if (res.success) {
  94. this.$emit('uploaded', res.data[0])
  95. }
  96. }
  97. }.bind(this);
  98. xhr.send(formData);
  99. },
  100. cancel: function () {
  101. this.showCropper = false;
  102. },
  103. confirm: function () {
  104. }
  105. }
  106. });