You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

578 lines
15 KiB

5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
  1. <template>
  2. <el-row>
  3. <el-col :span=20>
  4. <div id="boxDiv" :class="boxSize">
  5. <vue-cropper
  6. ref="cropper"
  7. :img="option.img"
  8. :outputSize="option.size"
  9. :outputType="option.outputType"
  10. :info="true"
  11. :full="option.full"
  12. :canMove="option.canMove"
  13. :canMoveBox="option.canMoveBox"
  14. :fixedBox="option.fixedBox"
  15. :original="option.original"
  16. :autoCrop="option.autoCrop"
  17. :autoCropWidth="option.autoCropWidth"
  18. :autoCropHeight="option.autoCropHeight"
  19. @img-load="imgLoad"
  20. ></vue-cropper>
  21. </div>
  22. </el-col>
  23. <el-col :span=4>
  24. <el-row>
  25. <el-col :span=24>
  26. <label>等比缩放裁剪边框 </label>
  27. <br>
  28. <el-button icon="el-icon-circle-plus-outline" circle @click="plusCjbk"></el-button><el-button icon="el-icon-remove-outline" circle @click="reduceCjbk"></el-button>
  29. <br>
  30. <el-button @click="recoveryCjbk">恢复{{defaultImgWidth +' X '+ defaultImgHeight}}</el-button>
  31. </el-col>
  32. <el-col :span=24>
  33. <br>
  34. <label>调整画布大小</label>
  35. <br>
  36. <el-button @click="changeCanvasSize('s1')">800 X 600</el-button>
  37. <br>
  38. <el-button @click="changeCanvasSize('s2')">800 X 700</el-button>
  39. <br>
  40. <el-button @click="changeCanvasSize('s3')">800 X 800</el-button>
  41. </el-col>
  42. </el-row>
  43. </el-col>
  44. <div class="test-button">
  45. <!-- <el-button class="bu_bg" type="primary" @click="startCrop" v-if="!crap">开始剪切</el-button>
  46. <el-button class="bu_bg" type="primary" @click="stopCrop" v-else>停止剪切</el-button> -->
  47. <el-tooltip class="item" effect="dark" content="清除裁剪后,可以移动图片" placement="top-start">
  48. <el-button class="bu_bg" v-if="crap==true" type="primary" @click="clearCrop">清除裁剪</el-button>
  49. </el-tooltip>
  50. <el-tooltip class="item" effect="dark" content="为了良好的客户体验,app图片建议控制在50Kb左右,宽400,高300上限,如果是兼容pc的图片,建议宽800,高600为上线" placement="top-start">
  51. <el-button class="bu_bg" v-if="crap==false" type="primary" @click="startCrop">开始裁剪</el-button>
  52. </el-tooltip>
  53. <el-button class="bu_bg" type="primary" @click="rotateLeft">左旋</el-button>
  54. <el-button class="bu_bg" type="primary" @click="rotateRight">右旋</el-button>
  55. <!-- <el-button class="bu_bg" type="primary" @click="finish('base64')">浏览(base64)</el-button>
  56. <el-button class="bu_bg" type="primary" @click="finish('blob')">浏览(blob)</el-button> -->
  57. <el-button class="bu_bg" type="primary" @click="upload('base64')">完成并上传图片</el-button>
  58. <el-tag> 有时候由于网络原因加载图片会需要3-5秒钟,请耐心等候 </el-tag>
  59. </div>
  60. </el-row>
  61. </template>
  62. <script>
  63. //import vueCropper from 'vue-cropper'
  64. import config from '@/common/config';//全局公共库import
  65. import { uploadBase64 } from '@/api/mdp/arc/image';
  66. import screenfull from 'screenfull'
  67. export default {
  68. props:['branchId','image','categoryId','remark','imgWidth','imgHeight','deptid','visible','storedb','fullscreen'],
  69. watch:{
  70. 'imgWidth':function(val){
  71. this.option.autoCropWidth = val;
  72. },
  73. 'imgHeight':function(val){
  74. this.option.autoCropHeight = val;
  75. },
  76. 'visible':function(val){
  77. if(val==true){
  78. }
  79. },
  80. 'image':function(image) {
  81. this.selectImg = image;
  82. this.option.img=this.converUrl(image.url);
  83. this.option.autoCropWidth=this.defaultImgWidth
  84. this.option.autoCropHeight=this.defaultImgHeight
  85. //this.imgLoading=true
  86. //this.$refs.cropper.reload()
  87. }
  88. },
  89. data: function () {
  90. return {
  91. model: false,
  92. modelSrc: '',
  93. crap: false,
  94. previews: {},
  95. lists: [
  96. {
  97. /* img: 'http://ofyaji162.bkt.clouddn.com/touxiang.jpg' */
  98. },
  99. {
  100. /* img: 'https://o90cnn3g2.qnssl.com/0C3ABE8D05322EAC3120DDB11F9D1F72.png' */
  101. }
  102. ],
  103. boxSize:"s1",
  104. option: {
  105. img: '',
  106. size: 1,
  107. full: false,
  108. outputType: 'png',
  109. canMove: true,
  110. fixedBox: false,
  111. original: false,
  112. canMoveBox: true,
  113. autoCrop: true,
  114. autoCropWidth: this.imgWidth,
  115. autoCropHeight: this.imgHeight,
  116. },
  117. downImg: '#',
  118. selectImg:{categoryId:''},
  119. imgLoading: false
  120. }
  121. },
  122. methods: {
  123. converUrl(url){
  124. if(url!='' && url!=null && !url.indexOf('http')==0 && !url.indexOf('www')==0 && !url.indexOf('data:image/')==0){
  125. return config.getArcImagePath()+"/"+url;
  126. }
  127. return url;
  128. },
  129. changeImg () {
  130. this.option.img = this.lists[~~(Math.random() * this.lists.length)].img
  131. },
  132. startCrop (reload) {
  133. // start
  134. this.crap = true
  135. if(this.imgWidth== undefined ){
  136. this.option.autoCropWidth=600
  137. }else{
  138. this.option.autoCropWidth=this.imgWidth
  139. }
  140. if(this.imgHeight== undefined ){
  141. this.option.autoCropHeight=600
  142. }else{
  143. this.option.autoCropHeight=this.imgHeight
  144. }
  145. this.$refs.cropper.startCrop()
  146. if(reload!=false){
  147. this.$refs.cropper.reload()
  148. }
  149. },
  150. stopCrop () {
  151. // stop
  152. this.crap = false
  153. this.$refs.cropper.stopCrop()
  154. },
  155. clearCrop () {
  156. // clear
  157. this.$refs.cropper.clearCrop()
  158. this.stopCrop ()
  159. },
  160. refreshCrop () {
  161. // clear
  162. this.$refs.cropper.refresh()
  163. },
  164. changeScale (num) {
  165. num = num || 1
  166. this.$refs.cropper.changeScale(num)
  167. },
  168. rotateLeft () {
  169. this.$refs.cropper.rotateLeft()
  170. },
  171. rotateRight () {
  172. this.$refs.cropper.rotateRight()
  173. },
  174. finish (type) {
  175. // 输出
  176. // var test = window.open('about:blank')
  177. // test.document.body.innerHTML = '图片生成中..'
  178. if (type === 'blob') {
  179. this.$refs.cropper.getCropBlob((data) => {
  180. var img = window.URL.createObjectURL(data)
  181. this.model = true
  182. this.modelSrc = img
  183. })
  184. } else {
  185. this.$refs.cropper.getCropData((data) => {
  186. this.model = true
  187. this.modelSrc = data
  188. })
  189. }
  190. },
  191. //调整画布大小
  192. changeCanvasSize(val){
  193. this.boxSize=val
  194. },
  195. // 实时预览函数
  196. realTime (data) {
  197. this.previews = data
  198. },
  199. plusCjbk(){
  200. if(this.crap==false){
  201. this.$message({ message: "请先开始裁剪", type: "warning" });
  202. return;
  203. }
  204. if(this.option.autoCropWidth+this.defaultImgHeight*0.1>800){
  205. this.$message({ message: "不能再放大", type: "warning" });
  206. return;
  207. }
  208. if(this.option.autoCropHeight+this.defaultImgHeight*0.1>800){
  209. this.$message({ message: "不能再放大", type: "warning" });
  210. return;
  211. }
  212. this.option.autoCropWidth=this.option.autoCropWidth+this.defaultImgWidth*0.1
  213. this.option.autoCropHeight=this.option.autoCropHeight+this.defaultImgHeight*0.1
  214. this.$refs.cropper.reload()
  215. },
  216. reduceCjbk(){
  217. if(this.crap==false){
  218. this.$message({ message: "请先开始裁剪", type: "warning" });
  219. return;
  220. }
  221. if(this.option.autoCropWidth-this.defaultImgWidth*0.1<=0){
  222. this.$message({ message: "不能再缩小", type: "warning" });
  223. return;
  224. }
  225. if(this.option.autoCropHeight-this.defaultImgHeight*0.1<=0){
  226. this.$message({ message: "不能再缩小", type: "warning" });
  227. return;
  228. }
  229. this.option.autoCropWidth=this.option.autoCropWidth-this.defaultImgWidth*0.1
  230. this.option.autoCropHeight=this.option.autoCropHeight-this.defaultImgHeight*0.1
  231. this.$refs.cropper.reload()
  232. },
  233. recoveryCjbk(){
  234. if(this.crap==false){
  235. this.$message({ message: "请先开始裁剪", type: "warning" });
  236. return;
  237. }
  238. this.option.autoCropWidth=this.defaultImgWidth
  239. this.option.autoCropHeight=this.defaultImgHeight
  240. this.$refs.cropper.reload()
  241. },
  242. finish2 (type) {
  243. this.$refs.cropper2.getCropData((data) => {
  244. this.model = true
  245. this.modelSrc = data
  246. })
  247. },
  248. finish3 (type) {
  249. this.$refs.cropper3.getCropData((data) => {
  250. this.model = true
  251. this.modelSrc = data
  252. })
  253. },
  254. upload (type) {
  255. // event.preventDefault()
  256. /* var aLink = document.createElement('a')
  257. aLink.download = 'demo' */
  258. // 输出
  259. var params={branchId:this.branchId ,categoryId: this.categoryId, fileData: '', remark: this.remark,name:'',deptid:this.deptid}
  260. if(this.storedb){
  261. params.storedb=this.storedb
  262. }else{
  263. if(this.selectImg.storedb){
  264. params.storedb=this.selectImg.storedb
  265. }
  266. }
  267. //var parm= this.$options.computed.uploadParams();
  268. if(params.categoryId==''||params.categoryId==undefined){
  269. this.$message({ message: "分类不能为空", type: "warning" });
  270. return;
  271. }
  272. if (type === 'blob') {
  273. this.$refs.cropper.getCropBlob((data) => {
  274. /* this.downImg = window.URL.createObjectURL(data)
  275. aLink.href = window.URL.createObjectURL(data)
  276. aLink.click() */
  277. alert("请上传64base");
  278. })
  279. } else {
  280. this.$refs.cropper.getCropData((data) => {
  281. params.fileData=data;
  282. var baseStr=data.substring(data.indexOf('base64,')+'base64,'.length);
  283. var eqTagIndex=baseStr.indexOf("=");
  284. baseStr=eqTagIndex!=-1?baseStr.substring(0,eqTagIndex):baseStr;
  285. var strLen=baseStr.length;
  286. var fileSize=parseInt((strLen-(strLen/8)*2)/1024);
  287. if(fileSize>110 && fileSize<1100){
  288. this.$confirm('图片大小为'+fileSize+'kb,超出建议值, 是否继续?', '提示', {
  289. confirmButtonText: '确定',
  290. cancelButtonText: '取消',
  291. type: 'warning'
  292. }).then(() => {
  293. params.name=this.selectImg.name+this.selectImg.fileSuffix;
  294. uploadBase64(params).then(res=>{
  295. if(res.data.tips.isOk){
  296. //返回触发父组件的方法
  297. //this.clearCrop ();
  298. var image = res.data.data;
  299. //this.refreshCrop ();
  300. this.$emit('upload-success',image);
  301. }else{
  302. this.$message({ message: "上传错误", type: "warning" });
  303. }
  304. }).catch(()=>{
  305. this.$message.warning("上传失败");
  306. });
  307. }).catch(() => {
  308. this.$message({
  309. type: 'info',
  310. message: '已取消'
  311. });
  312. });
  313. }else{
  314. if(fileSize<=110){
  315. params.name=this.selectImg.name+this.selectImg.fileSuffix;
  316. uploadBase64(params).then(res=>{
  317. if(res.data.tips.isOk){
  318. //返回触发父组件的方法
  319. //this.clearCrop ();
  320. var image = res.data.data;
  321. //this.refreshCrop ();
  322. this.$emit('upload-success',image);
  323. }else{
  324. this.$message({ message: "上传错误", type: "warning" });
  325. }
  326. }).catch(()=>{
  327. this.$message.warning("上传失败");
  328. });
  329. }else{
  330. this.$message({ message: "图片太大,不允许上传,请重新裁剪后上传", type: "warning" });
  331. }
  332. }
  333. //this.$emit('getInfo',this.params);
  334. /* this.downImg = data
  335. aLink.href = data
  336. aLink.click() */
  337. });
  338. }
  339. },
  340. imgLoad(info){
  341. this.imgLoading=false
  342. if(info=='success'){
  343. this.$message({ message: "加载图片成功", type: "success" });
  344. }else{
  345. this.$message({ message: "加载图片失败", type: "error" });
  346. }
  347. },
  348. uploadImg (e, num) {
  349. //上传图片
  350. // this.option.img
  351. var file = e.target.files[0]
  352. if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
  353. alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  354. return false
  355. }
  356. var reader = new FileReader()
  357. reader.onload = (e) => {
  358. let data
  359. if (typeof e.target.result === 'object') {
  360. // 把Array Buffer转化为blob 如果是base64不需要
  361. data = window.URL.createObjectURL(new Blob([e.target.result]))
  362. } else {
  363. data = e.target.result
  364. }
  365. if (num === 1) {
  366. this.option.img = data
  367. } else if (num === 2) {
  368. this.example2.img = data
  369. }
  370. }
  371. // 转化为base64
  372. // reader.readAsDataURL(file)
  373. // 转化为blob
  374. reader.readAsArrayBuffer(file)
  375. },
  376. fuselect(){
  377. this.$emit('confirmSelect');
  378. },
  379. imgLoad (msg) {
  380. console.log(msg)
  381. },
  382. handleSuccess(res,file) {
  383. //var jsonData = JSON.stringify(res, null, 4);
  384. //alert(jsonData);
  385. /* var tips= res.tips;
  386. if(tips.isOk){
  387. this.$message({ message: '上传成功', type: 'info' });
  388. }else{
  389. if(tips.msg=='该图片不支持'){
  390. this.$message({ message: '该图片不支持', type: 'info' });
  391. }else{
  392. this.$message({ message: '未知异常', type: 'info' });
  393. }
  394. } */
  395. }
  396. },
  397. components: {
  398. //'vue-cropper':vueCropper,
  399. },
  400. mounted () {
  401. //this.changeImg()
  402. // hljs.configure({useBR: true})
  403. /* var list = [].slice.call(document.querySelectorAll('pre code'))
  404. list.forEach((val, index) => {
  405. hljs.highlightBlock(val)
  406. }) */
  407. this.selectImg = this.image;
  408. this.option.img=this.converUrl(this.image.url);
  409. this.startCrop(false);
  410. this.imgLoading=true
  411. },
  412. computed:{
  413. defaultImgWidth: function(){
  414. let defaultImgWidth=600;
  415. if(this.imgWidth== undefined ){
  416. defaultImgWidth=600
  417. }else{
  418. defaultImgWidth=this.imgWidth
  419. }
  420. return defaultImgWidth;
  421. },
  422. defaultImgHeight: function(){
  423. let defaultImgHeight=600;
  424. if(this.imgHeight== undefined ){
  425. defaultImgHeight=600
  426. }else{
  427. defaultImgHeight=this.imgHeight
  428. }
  429. return defaultImgHeight;
  430. }
  431. }
  432. }
  433. </script>
  434. <style>
  435. * {
  436. margin: 0;
  437. padding: 0;
  438. }
  439. .content {
  440. margin: auto;
  441. max-width: 1200px;
  442. margin-bottom: 100px;
  443. }
  444. .test-button {
  445. padding-top: 30px;
  446. clear: both;
  447. display: flex;
  448. flex-wrap: wrap;
  449. }
  450. .btn {
  451. display: inline-block;
  452. line-height: 1;
  453. white-space: nowrap;
  454. cursor: pointer;
  455. background: #fff;
  456. border: 1px solid #c0ccda;
  457. color: #1f2d3d;
  458. text-align: center;
  459. box-sizing: border-box;
  460. outline: none;
  461. margin:20px 10px 0px 0px;
  462. padding: 9px 15px;
  463. font-size: 14px;
  464. border-radius: 4px;
  465. color: #fff;
  466. background-color: #50bfff;
  467. border-color: #50bfff;
  468. transition: all .2s ease;
  469. text-decoration: none;
  470. user-select: none;
  471. }
  472. .des {
  473. line-height: 30px;
  474. }
  475. .show-info {
  476. margin-bottom: 50px;
  477. text-align: left;
  478. }
  479. .show-info h2 {
  480. line-height: 50px;
  481. }
  482. /*.title, .title:hover, .title-focus, .title:visited {
  483. color: black;
  484. }*/
  485. .s1 {
  486. height: 600px;
  487. width:800px;
  488. float: left;
  489. }
  490. .s2 {
  491. height: 700px;
  492. width:800px;
  493. float: left;
  494. }
  495. .s3 {
  496. height: 800px;
  497. width:800px;
  498. float: left;
  499. }
  500. .app {
  501. height: 300px;
  502. width:400px;
  503. float: left;
  504. }
  505. .show-preview{
  506. float: left;
  507. }
  508. .model {
  509. position: fixed;
  510. z-index: 10;
  511. width: 100vw;
  512. height: 100vh;
  513. overflow: auto;
  514. top: 0;
  515. left: 0;
  516. background: rgba(0, 0, 0, 0.8);
  517. }
  518. .model-show {
  519. display: flex;
  520. justify-content: center;
  521. align-items: center;
  522. width: 100vw;
  523. height: 100vh;
  524. }
  525. .model img {
  526. display: block;
  527. margin: auto;
  528. max-width: 80%;
  529. user-select: none;
  530. background-position: 0px 0px, 10px 10px;
  531. background-size: 20px 20px;
  532. background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);
  533. }
  534. .c-item {
  535. display: block;
  536. padding: 10px 0;
  537. user-select: none;
  538. }
  539. .bu_bg{
  540. color: #ffffff;
  541. background-color: #50c0fe;
  542. border-color: #50c0fe;
  543. height: 32px;
  544. margin-left: 10px;
  545. }
  546. </style>