diy-index.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. <script src="/resources/js/html2canvas.min.js"></script>
  2. <script type="text/javascript" src="/resources/js/hotzone.js"></script>
  3. <script>
  4. function getRandomId(list = []) {
  5. var initArr = [] // getInitArr(); // 拿到已有的id -- 一般不会冲突
  6. // 随机id不能和以前编辑冲突
  7. function generateId() {
  8. const s = [];
  9. const hexDigits = '0123456789abcdef';
  10. for (let i = 0; i < 36; i++) {
  11. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  12. }
  13. s[14] = '4';
  14. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  15. s[8] = s[13] = s[18] = s[23] = '-';
  16. const uuid = s.join('');
  17. if (initArr.some(str => (str === uuid))) { // 存在已有数据
  18. return generateId()
  19. }
  20. return uuid;
  21. }
  22. function getInitArr() {
  23. return list.map(item => {
  24. return item.id
  25. })
  26. }
  27. var id = generateId()
  28. return id
  29. }
  30. function deepClone(obj) {
  31. var o, i, j, k;
  32. if (typeof(obj) != "object" || obj === null) return obj;
  33. if (obj instanceof(Array)) {
  34. o = [];
  35. i = 0;
  36. j = obj.length;
  37. for (; i < j; i++) {
  38. if (typeof(obj[i]) == "object" && obj[i] != null) {
  39. o[i] = arguments.callee(obj[i]);
  40. } else {
  41. o[i] = obj[i];
  42. }
  43. }
  44. } else {
  45. o = {};
  46. for (i in obj) {
  47. if (typeof(obj[i]) == "object" && obj[i] != null) {
  48. o[i] = arguments.callee(obj[i]);
  49. } else {
  50. o[i] = obj[i];
  51. }
  52. }
  53. }
  54. return o;
  55. }
  56. function getObjValue(target = {}, keys = [], def) {
  57. let obj = deepClone(target)
  58. try {
  59. value = keys.reduce((prev, key) => {
  60. return prev[key]
  61. }, target)
  62. } catch (error) {
  63. value = def
  64. }
  65. return value || def
  66. }
  67. function deepEqual(x, y) {
  68. // 指向同一内存时
  69. if (x === y) {
  70. return true;
  71. } else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
  72. if (Object.keys(x).length !== Object.keys(y).length) {
  73. return false;
  74. }
  75. for (var prop in x) {
  76. if (y.hasOwnProperty(prop)) {
  77. if (!deepEqual(x[prop], y[prop])) return false;
  78. } else {
  79. return false;
  80. }
  81. }
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. }
  87. var basicMixins = {
  88. data() {
  89. return {
  90. defaultForm: {
  91. // default_color: "transparer",
  92. colorInfos: ['底部背景'],
  93. sizeInfos: ['上边距', '下边距', '左右边距'],
  94. radiusInfos: ['上圆角', '下圆角'],
  95. defColor: "transparent",
  96. tabItem: null
  97. },
  98. result: {},
  99. searchIpts: {},
  100. diy_img_prefix: '<?= Yii::getAlias('@attachurl') ?>',
  101. }
  102. },
  103. created() {
  104. this.init()
  105. },
  106. // 这里是基础的修改 - 需要其他自行添加对应方法-数据
  107. methods: {
  108. init() {
  109. // 设置默认值
  110. this.result = deepClone(this.activeItem)
  111. this.searchIpts = this.result.computedStyle.searchIpts || {}
  112. // 描边颜色默认值
  113. let arr = ['computedStyle', 'tabItem']
  114. this.defaultForm.tabItem = getObjValue(this.result, arr, this.defaultForm.tabItem)
  115. // 选择颜色样式
  116. let arr1 = ['computedStyle', 'searchBox']
  117. var colorArr = [{
  118. name: '底部背景', //名字
  119. color: getObjValue(this.result, arr1, "transparent"), //颜色
  120. showAlpha: false, //是否开启透明度
  121. }, ]
  122. this.defaultForm.colorInfos = colorArr
  123. // 边距
  124. let arr2 = ['computedStyle', 'searchIpts', 'marginBottom']
  125. let arr3 = ['computedStyle', 'searchIpts', 'marginLeft']
  126. let arr4 = ['computedStyle', 'searchIpts', 'marginTop']
  127. var marginArr = [{
  128. name: '上边距', //名字
  129. value: parseFloat(getObjValue(this.result, arr4, 0)), //值
  130. unit: "px", //单位
  131. disabled: false, //是否禁用
  132. maxValue: 50,
  133. }, {
  134. name: '下边距', //名字
  135. value: parseFloat(getObjValue(this.result, arr2, 0)), //值
  136. unit: "px", //单位
  137. disabled: false, //是否禁用
  138. maxValue: 50,
  139. }, {
  140. name: '左右边距', //名字
  141. value: parseFloat(getObjValue(this.result, arr3, 0)), //值
  142. unit: "px", //单位
  143. disabled: false, //是否禁用
  144. maxValue: 20,
  145. }]
  146. this.defaultForm.sizeInfos = marginArr
  147. // 圆角
  148. let arr5 = ['computedStyle', 'searchIpts', 'border-top-left-radius']
  149. let arr6 = ['computedStyle', 'searchIpts', 'border-bottom-right-radius']
  150. var radiusArr = [{
  151. name: '上圆角', //名字
  152. value: parseFloat(getObjValue(this.result, arr5, 0)), //值
  153. unit: "px", //单位
  154. disabled: false, //是否禁用
  155. maxValue: 20,
  156. }, {
  157. name: '下圆角', //名字
  158. value: parseFloat(getObjValue(this.result, arr6, 0)), //值
  159. unit: "px", //单位
  160. disabled: false, //是否禁用
  161. maxValue: 20,
  162. }]
  163. this.defaultForm.radiusInfos = radiusArr
  164. },
  165. // 基础逻辑
  166. colorSelect(e) { // 样式
  167. console.log(e)
  168. let tabItem = this.defaultForm.tabItem || {}
  169. this.defaultForm.tabItem = Object.assign(tabItem, e.item)
  170. console.log(this.defaultForm.tabItem);
  171. this.updataResult(this.defaultForm.tabItem, 'tabItem')
  172. },
  173. sliderChange(arr, type, showUnit = true) { // 边距
  174. let top = arr[0] && (showUnit ? (arr[0].value + arr[0].unit) : arr[0].value)
  175. let bottom = arr[1] && (showUnit ? (arr[1].value + arr[1].unit) : arr[1].value)
  176. let lr = arr[2] && (showUnit ? (arr[2].value + arr[2].unit) : arr[2].value)
  177. const style = {
  178. marginTop: top,
  179. marginBottom: bottom,
  180. marginLeft: lr,
  181. marginRight: lr
  182. }
  183. this.searchIpts = Object.assign(this.searchIpts, style)
  184. this.updataResult(this.searchIpts, 'searchIpts')
  185. },
  186. RadiusChange(arr) {
  187. let top = arr[0].value + arr[0].unit
  188. let bottom = arr[1].value + arr[1].unit
  189. const style = {
  190. 'border-top-left-radius': top,
  191. 'border-top-right-radius': top,
  192. 'border-bottom-right-radius': bottom,
  193. 'border-bottom-left-radius': bottom
  194. }
  195. this.searchIpts = Object.assign(this.searchIpts, style)
  196. this.updataResult(this.searchIpts, 'searchIpts')
  197. },
  198. updateColor(e) { // 选择颜色
  199. this.result.computedStyle.searchBox = e[0].color
  200. this.$emit("update", this.result)
  201. },
  202. updataResult(style, name) {
  203. var result = deepClone(style)
  204. this.result.computedStyle[name] = result
  205. this.$emit("update", this.result)
  206. },
  207. iptSelect(e) {
  208. e.currentTarget.select();
  209. },
  210. }
  211. }
  212. //16进制颜色转化为RGB颜色
  213. function colorRgb(str) {
  214. var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  215. var sColor = str.toLowerCase();
  216. if (sColor && reg.test(sColor)) {
  217. if (sColor.length === 4) {
  218. var sColorNew = "#";
  219. for (var i = 1; i < 4; i += 1) {
  220. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
  221. }
  222. sColor = sColorNew;
  223. }
  224. //处理六位的颜色值
  225. var sColorChange = [];
  226. for (var i = 1; i < 7; i += 2) {
  227. sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
  228. }
  229. return "rgb(" + sColorChange.join(",") + ")";
  230. } else {
  231. return sColor;
  232. }
  233. }
  234. //判断是否为亮色
  235. function getContrastYIQ(hexcolor) {
  236. var colorrgb = colorRgb(hexcolor);
  237. var colors = colorrgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  238. var red = colors[1];
  239. var green = colors[2];
  240. var blue = colors[3];
  241. var brightness;
  242. brightness = (red * 299) + (green * 587) + (blue * 114);
  243. brightness = brightness / 255000;
  244. if (brightness >= 0.5) {
  245. return "light";
  246. } else {
  247. return "dark";
  248. }
  249. }
  250. // 获取链接选择其他链接类型中文
  251. function getOtherUrlName(type) {
  252. let text = "";
  253. if (type == "call") {
  254. text = "拨打电话"
  255. } else if (type == "web") {
  256. text = "自定义链接"
  257. } else if (type == "applet") {
  258. text = "小程序"
  259. }
  260. return text;
  261. }
  262. // dom生成canvas
  263. // function domToCanvas(contain) {
  264. // var canvas2 = document.createElement("canvas");
  265. // var w = $(contain).outerWidth();
  266. // var h = $(contain).outerHeight();
  267. // //先放大2倍,后期缩小,处理模糊问题
  268. // canvas2.width = w * 2;
  269. // canvas2.height = h * 2;
  270. // canvas2.style.width = w + "px";
  271. // canvas2.style.height = h + "px";
  272. // var context = canvas2.getContext("2d");
  273. // // 进行缩放
  274. // context.scale(2, 2);
  275. // return new Promise((resolve, reject) => {
  276. // html2canvas(document.querySelector(contain), {
  277. // canvas: canvas2,
  278. // allowTaint: true, //允许污染
  279. // taintTest: true, //在渲染前测试图片
  280. // useCORS: true //使用跨域
  281. // }).then(function(canvas) {
  282. // resolve(canvas)
  283. // });
  284. // })
  285. // }
  286. function getNavData() {
  287. const result = [{
  288. "id": getRandomId(),
  289. "data": {
  290. backSrc: "",
  291. contentType: "1",
  292. isShowName: "1",
  293. logoSrc: "",
  294. mainPage: { // 主页面设置
  295. defColor: "#fff",
  296. name: "拼团",
  297. type: "纯色",
  298. imgUrl: "resources/img/decorate/group-goods/group_bg.png"
  299. },
  300. name: "拼团",
  301. positionChoose: "1"
  302. },
  303. "computedStyle": {
  304. "mode": 1,
  305. "style": 1,
  306. "titleSwitch": true,
  307. "titleMode": 1,
  308. "imageUrl": [],
  309. "position": 1,
  310. "searchSwitch": true,
  311. "searchBgColor": "",
  312. "statusBarColor": "#DB0505",
  313. "defaultForm": {
  314. "defColor": "transparer"
  315. },
  316. "titleDistance": {
  317. "fontSize": 16,
  318. "height": 30,
  319. "width": 57,
  320. "marginLeft": 20
  321. },
  322. "statusBar": "2",
  323. "searchMarPad": {
  324. "marginLeft": 20,
  325. "paddingLeft": 14
  326. }
  327. },
  328. "permission": [],
  329. "funcListItem": {
  330. "type": "nav",
  331. "title": "顶部导航",
  332. "is_top": 1
  333. },
  334. "isShow": true,
  335. "is_show_permission": false,
  336. "is_drag": false,
  337. "disabled": true,
  338. }, {
  339. "id": getRandomId(),
  340. "data": {
  341. buttonData: {
  342. typeIndex: 0,
  343. typeItem: null
  344. },
  345. buttonPointData: {
  346. open: true,
  347. pointIndex: 1,
  348. pointItem: {
  349. label: "resources/img/decorate/point-two.png",
  350. value: "two"
  351. },
  352. position: 1
  353. },
  354. carouselImgArr: [{
  355. hotZone: [{
  356. heightPer: 0.3,
  357. leftPer: 0,
  358. topPer: 0,
  359. url: "",
  360. urlName: "",
  361. urlType: "",
  362. widthPer: 0.3
  363. }],
  364. imgUrl: "resources/img/decorate/group-goods/banner.png",
  365. url: "",
  366. urlName: "",
  367. urlType: "",
  368. visible: false
  369. }],
  370. spaceInfo: [{
  371. disabled: false,
  372. hidden: true,
  373. maxValue: 20,
  374. name: "图片间距",
  375. unit: "px",
  376. value: 5
  377. },
  378. {
  379. disabled: false,
  380. hidden: false,
  381. maxValue: 50,
  382. name: "上边距",
  383. unit: "px",
  384. value: 0,
  385. },
  386. {
  387. disabled: false,
  388. hidden: false,
  389. maxValue: 50,
  390. name: "下边距",
  391. unit: "px",
  392. value: 0
  393. },
  394. {
  395. disabled: false,
  396. hidden: false,
  397. maxValue: 50,
  398. name: "左右边距",
  399. unit: "px",
  400. value: 12
  401. }
  402. ]
  403. },
  404. "computedStyle": {
  405. "carouselImgArr": [],
  406. "spaceStyle": {
  407. marginBottom: "0px",
  408. marginLeft: "12px",
  409. marginRight: "12px",
  410. marginTop: "0px"
  411. },
  412. "searchIpts": {
  413. 'border-bottom-left-radius': "10px",
  414. 'border-bottom-right-radius': "10px",
  415. 'border-top-left-radius': "10px",
  416. 'border-top-right-radius': "10px"
  417. }
  418. },
  419. "permission": [],
  420. "funcListItem": {
  421. is_top: 0,
  422. title: "轮播广告",
  423. type: "carousel-img"
  424. },
  425. "isShow": true,
  426. "is_show_permission": false,
  427. "is_drag": false,
  428. "disabled": true,
  429. }, {
  430. "id": getRandomId(),
  431. "data": {
  432. "buttonData": {
  433. "line_num": 1,
  434. "pattern": "fixed",
  435. "patternItem": null,
  436. "shape": "square",
  437. "single_line": 3,
  438. "style": 1,
  439. "type": 1,
  440. },
  441. "colorInfos": [{
  442. color: "",
  443. name: "底部背景",
  444. showAlpha: false,
  445. value: "transparent"
  446. },
  447. {
  448. color: "",
  449. name: "组件背景",
  450. showAlpha: false,
  451. value: "transparent"
  452. },
  453. {
  454. color: "#333333",
  455. name: "文字颜色",
  456. showAlpha: false,
  457. value: "#333333",
  458. }
  459. ],
  460. "iconList": [{
  461. img: 'resources/img/decorate/group-goods/icon1.png',
  462. icon: 'el-icon-setting',
  463. is_show_tag: false,
  464. tags: '热门',
  465. tagsBgColor: '#f83287',
  466. tagsTextColor: '#ffffff',
  467. btnText: '三人团',
  468. link_params: null,
  469. },
  470. {
  471. img: 'resources/img/decorate/group-goods/icon2.png',
  472. icon: 'el-icon-setting',
  473. is_show_tag: false,
  474. tags: '热门',
  475. tagsBgColor: '#f83287',
  476. tagsTextColor: '#ffffff',
  477. btnText: '五人团',
  478. link_params: null,
  479. },
  480. {
  481. img: 'resources/img/decorate/group-goods/icon3.png',
  482. icon: 'el-icon-setting',
  483. is_show_tag: false,
  484. tags: '热门',
  485. tagsBgColor: '#f83287',
  486. tagsTextColor: '#ffffff',
  487. btnText: '十人团',
  488. link_params: null,
  489. }
  490. ],
  491. "sizeInfos": [{
  492. disabled: false,
  493. hidden: false,
  494. maxValue: 50,
  495. name: "上边距",
  496. unit: "px",
  497. value: 8
  498. },
  499. {
  500. disabled: false,
  501. hidden: false,
  502. maxValue: 50,
  503. name: "下边距",
  504. unit: "px",
  505. value: 8
  506. },
  507. {
  508. disabled: false,
  509. hidden: false,
  510. maxValue: 50,
  511. name: "左右边距",
  512. unit: "px",
  513. value: 0
  514. }
  515. ]
  516. },
  517. "computedStyle": {
  518. "iptBg": "",
  519. "searchBox": "",
  520. "searchIpts": {
  521. "marginBottom": "8px",
  522. "marginLeft": "0px",
  523. "marginRight": "0px",
  524. "marginTop": "8px",
  525. },
  526. "textStyle": "#333333"
  527. },
  528. "permission": [],
  529. "funcListItem": {
  530. is_top: 0,
  531. title: "按钮组",
  532. type: "button-group"
  533. },
  534. "isShow": true,
  535. "is_show_permission": false,
  536. "is_drag": false,
  537. "disabled": true,
  538. }, {
  539. "id": getRandomId(),
  540. "data": {
  541. colorInfos: [],
  542. sizeInfos: [],
  543. title: "商品拼团",
  544. titleRadio: 1,
  545. paddingBottom: 0,
  546. paddingTop: 10
  547. },
  548. "computedStyle": {
  549. aroundMargin: "0px",
  550. bgColor: "transparent",
  551. btnBackground: "#db0505",
  552. btnTextColor: "#fff",
  553. marginBottom: "0px",
  554. marginTop: "0px",
  555. spacing: "5px"
  556. },
  557. "permission": [],
  558. "funcListItem": {
  559. is_top: 0,
  560. title: "拼团",
  561. type: "group-goods"
  562. },
  563. "isShow": true,
  564. "is_show_permission": false,
  565. "is_drag": false,
  566. "disabled": true,
  567. }]
  568. return result
  569. }
  570. function debounce(func, wait) {
  571. let timer;
  572. return function() {
  573. let context = this; // 注意 this 指向
  574. let args = arguments; // arguments中存着e
  575. if (timer) clearTimeout(timer);
  576. timer = setTimeout(() => {
  577. func.apply(this, args)
  578. }, wait)
  579. }
  580. }
  581. </script>
  582. <?php
  583. use common\helpers\ComponentHelper;
  584. use common\models\diy\DiyComponents;
  585. $diyPath = __DIR__ . '/components/common';
  586. $stylePath = __DIR__ . '/components/style-components';
  587. $components = DiyComponents::getDir($diyPath, $diyPath);
  588. $styleComponents = DiyComponents::getDir($stylePath, $stylePath);
  589. ComponentHelper::loadComponentView('components/diy-preview', __DIR__);
  590. ComponentHelper::loadComponentView('components/diy-style-preview', __DIR__);
  591. foreach ($components as $item) {
  592. ComponentHelper::loadComponentView("components/common/{$item}", __DIR__);
  593. }
  594. foreach ($styleComponents as $item) {
  595. ComponentHelper::loadComponentView("components/style-components/{$item}", __DIR__);
  596. }
  597. ?>
  598. <div id="app">
  599. <div style="padding-bottom: 10px;padding-right: 20px;background: #F4F6F8;" @click="save" flex="main:right">
  600. <el-button type="primary">保存</el-button>
  601. </div>
  602. <div class="diy-contain flex" v-loading="loading">
  603. <diy-preview :active-index="activeIndex" :list="previewList" @select-item="selectItme" @change-move="changeMove"></diy-preview>
  604. <transition name="fade">
  605. <diy-style-preview :active-item="activeItem" :active-index="activeIndex" :permission="permission" @update="changeDiyItem" v-if="showUpdate"></diy-style-preview>
  606. </transition>
  607. </div>
  608. <!-- 预览弹窗 -->
  609. <el-dialog title="预览弹窗" :modal-append-to-body="false" :visible.sync="dialogFormVisible">
  610. <div class="flex flex-x-center" style="height: 600px;overflow:auto">
  611. <img id="diy-test-preview" :src="previewSrc"></img>
  612. </div>
  613. <div slot="footer" class="dialog-footer">
  614. <el-button @click="dialogFormVisible = false">取 消</el-button>
  615. <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
  616. </div>
  617. </el-dialog>
  618. </div>
  619. <script>
  620. Vue.prototype.$capitals = []; // 用户信息对象
  621. Vue.prototype.$eventBus = new Vue();
  622. var PageShareData = {
  623. pageIndex: false // 点击页面设置标识 => nav/style
  624. }; // 全局共享数据 - 没有响应式
  625. new Vue({
  626. el: '#app',
  627. data() {
  628. return {
  629. capital: [],
  630. dialogFormVisible: false,
  631. /**
  632. * @descriptions 浮动()、普通()、置顶(isTop)、弹窗(alert)四种显示组件
  633. * @param {string} type // diy组件类型
  634. * @param {string} title // 二级标题
  635. * @param {string} img // diy组件图片
  636. * @param {number} count // 可添加diy组件数量
  637. * @param {boolean} is_top // diy组件是否置顶组件 - 待添加 - 分添加等级 1 2 3 数字靠前权限越高
  638. * @param {object} permission // 组件的不可操作权限 {"is_delete": false,"is_drag": false,"disabled": true, // 禁用操作}
  639. */
  640. pageComponentsList: [],
  641. /**
  642. * @param {boolean} is_show_permission // 显示权限
  643. * @param {boolean} is_delete // 是否可删除
  644. * @param {boolean} is_up // 是否可上移
  645. * @param {boolean} is_down // 是否可下移
  646. * @param {boolean} is_drag // 是否可拖拽
  647. * @param {boolean} is_copy // 是否可复制
  648. * @param {boolean} disabled // 全部禁用操作
  649. */
  650. previewList: [], // 预览数组对象
  651. activeIndex: -1, // 预览激活索引
  652. activeItem: null, // 预览激活对象
  653. type: "add", // add新增 - edit编辑
  654. test: ["a", "b", "c"],
  655. showUpdate: true,
  656. responentData: null, // 详情响应的总数据
  657. permission: [], // 权限数组
  658. isClickPage: false, // 是否点击分页设置
  659. loading: false, // 数据加载
  660. saveLoading: false, // 保存数据
  661. fields: {
  662. upload_type: 'images'
  663. },
  664. previewSrc: "",
  665. }
  666. },
  667. provide() {
  668. return {
  669. capitals: this.capital
  670. }
  671. },
  672. watch: {
  673. activeIndex() {
  674. this.showUpdate = false
  675. setTimeout(() => {
  676. this.showUpdate = true
  677. }, 0)
  678. },
  679. },
  680. created() {
  681. // console.log(this.dynamicImgSrc('/abc/ss'));
  682. // 添加不同的模板nav
  683. this.previewList.push(...getNavData())
  684. this.init()
  685. this.type = getQuery('id') ? "edit" : "add"
  686. const id = getQuery('id');
  687. this.detail(id)
  688. },
  689. methods: {
  690. savePreview() {
  691. this.activeIndex = -1
  692. this.activeItem = null
  693. this.$nextTick(() => {
  694. const contain = document.getElementById("diy-preview-contain");
  695. this.deepNode(contain);
  696. setTimeout(() => {
  697. this.dialogFormVisible = true
  698. html2canvas(contain, {
  699. useCORS: true
  700. }).then(canvas => {
  701. const src = canvas.toDataURL("image/png")
  702. this.upload(this.dataURLtoFile(src)).then(res => {
  703. if (res.code == 0) {
  704. this.previewSrc = res.data.url
  705. this.save(false)
  706. } else {
  707. this.$message({
  708. message: res.msg,
  709. type: 'warning'
  710. });
  711. }
  712. })
  713. })
  714. }, 0)
  715. })
  716. },
  717. dataURLtoFile(dataurl) { // 生成Blob
  718. var arr = dataurl.split(',');
  719. var mime = arr[0].match(/:(.*?);/)[1];
  720. var bstr = atob(arr[1]);
  721. var n = bstr.length;
  722. var u8arr = new Uint8Array(n);
  723. while (n--) {
  724. u8arr[n] = bstr.charCodeAt(n);
  725. }
  726. return new Blob([u8arr], {
  727. type: mime
  728. });
  729. },
  730. upload(file) {
  731. let formData = new FormData();
  732. const params = {
  733. upload_type: 'images'
  734. };
  735. params['r'] = 'common/file/upload';
  736. for (let i in this.fields) {
  737. formData.append(i, this.fields[i]);
  738. }
  739. formData.append('file', file, "image.png");
  740. return new Promise(successCallback => {
  741. this.$request({
  742. headers: {
  743. 'Content-Type': 'multipart/form-data'
  744. },
  745. params: params,
  746. method: 'post',
  747. data: formData,
  748. }).then(e => {
  749. successCallback(e.data)
  750. }).catch(e => {
  751. file._complete = true;
  752. });
  753. })
  754. },
  755. deepNode(contain) {
  756. if (contain.tagName === "IMG") {
  757. contain.src = contain.src + '?t=' + Date.now()
  758. contain.setAttribute('crossorigin', 'anonymous')
  759. }
  760. if (contain.children && contain.children.length) {
  761. [].forEach.call(contain.children, (item => {
  762. this.deepNode(item)
  763. }))
  764. }
  765. },
  766. pageSet() {
  767. PageShareData.pageIndex = true
  768. this.$eventBus.$emit("onPageClick")
  769. var index = 0;
  770. this.activeIndex = index
  771. this.activeItem = this.previewList[index]
  772. // this.changeDiyItem()
  773. },
  774. detail(id) {
  775. // 详情
  776. this.loading = true
  777. request({
  778. params: {
  779. r: 'group-buy/default/diy-index'
  780. },
  781. method: 'get',
  782. }).then(res => {
  783. const {
  784. data
  785. } = res.data
  786. if (res.data.code != 0) {
  787. this.$message({
  788. message: res.data.msg,
  789. type: 'error'
  790. });
  791. return false
  792. }
  793. const previewList = JSON.parse(data.content)
  794. this.previewList = previewList
  795. // this.responentData = data;
  796. // if (id) {
  797. // if (data.diypage.content) {
  798. // const previewList = JSON.parse(data.diypage.content);
  799. // console.log(previewList, '....................///previewList')
  800. // // 兼容以前数据
  801. // const memberInfo = this.addIsTop(previewList, "nav")
  802. // this.previewList = this.addIsTop(memberInfo, "member-info")
  803. // }
  804. // }
  805. // if (data.components && data.components.length) {
  806. // this.pageComponentsList = data.components;
  807. // Vue.prototype.$capitals = data.capital
  808. // }
  809. // this.permission = data.auth
  810. }).finally(res => {
  811. this.loading = false
  812. })
  813. },
  814. save: debounce(function(isLoading = true) {
  815. // 对接保存数据接口
  816. if (this.saveFlag) {
  817. console.log("重复提交");
  818. return
  819. }
  820. const title = this.previewList[0].data.name
  821. if (!title) {
  822. this.$message({
  823. message: '请输入标题',
  824. type: 'warning'
  825. });
  826. return
  827. }
  828. isLoading && (this.saveLoading = true)
  829. this.saveFlag = true // 防止重复提交限制
  830. request({
  831. params: {
  832. r: '/group-buy/default/diy-index'
  833. },
  834. method: 'post',
  835. data: {
  836. // id: getQuery('id') || null,
  837. // title,
  838. content: JSON.stringify(this.previewList),
  839. // cover: this.previewSrc
  840. }
  841. }).then(res => {
  842. if (res.data.code != 0) {
  843. this.$message({
  844. message: res.data.msg,
  845. type: 'error'
  846. });
  847. } else {
  848. this.$message({
  849. message: res.data.msg,
  850. type: 'success'
  851. });
  852. }
  853. }).finally(res => {
  854. this.saveFlag = false
  855. this.saveLoading = false
  856. })
  857. },
  858. 400),
  859. init() {
  860. this.$eventBus.$on("diy-preview-copy", (item = {}) => {
  861. this.copy(item)
  862. })
  863. this.$eventBus.$on("diy-preview-operateFn", (item = {}) => {
  864. this.operateFn(item)
  865. })
  866. this.$eventBus.$on("diy-back-set", () => {
  867. this.updateView(null, -1)
  868. })
  869. },
  870. validateCount(e) {
  871. // 校验组件能添加几个
  872. var msg = "";
  873. var list = this.previewList.filter(item => {
  874. return item.funcListItem.type === e.type
  875. });
  876. if (e && e.count && list.length >= e.count) {
  877. msg = `该组件只能添加${e.count}个哦`
  878. }
  879. if (msg) {
  880. this.$message({
  881. message: msg,
  882. type: 'warning'
  883. });
  884. }
  885. return msg
  886. },
  887. setInitObj(data, e) {
  888. if (!e.permission) {
  889. return false
  890. }
  891. const target = typeof e.permission === "object" ? e.permission : JSON.parse(e.permission)
  892. for (const key in target) {
  893. data[key] = e.permission[key]
  894. }
  895. },
  896. onClick(e) {
  897. const flag = this.validateCount(e) // 校验组件可添加个数
  898. if (flag) return // 不通过
  899. var initObj = {
  900. id: getRandomId(this.previewList),
  901. data: {}, // 对应数据
  902. computedStyle: {}, // 对应组件样式-尺寸
  903. permission: [], // 展示权限列表 ==> 对应用户等级or其他
  904. funcListItem: e,
  905. isShow: true, // 前端是否显示
  906. is_show_permission: true, // 是否显示权限
  907. } // 创建的初始数据
  908. this.setInitObj(initObj, e) // 设置可操作方法
  909. if (e.is_top) { // 置顶组件操作 - 不操作以前===>
  910. const index = this.previewList.findIndex(item => {
  911. const e1 = item.funcListItem || {}
  912. return e1.is_top === undefined || (e1.is_top >= e.is_top)
  913. })
  914. this.activeItem = initObj
  915. if (index < 0) {
  916. this.previewList.push(initObj)
  917. this.activeIndex = Math.max(0, this.previewList.length - 1)
  918. } else {
  919. this.previewList.splice(index, 0, initObj)
  920. this.activeIndex = index
  921. }
  922. return false
  923. }
  924. // if(this.isAlearComponents()){
  925. // return false
  926. // }
  927. // 不置顶组件添加
  928. this.activeIndex = Math.max(this.getIsTopArrLength() - 1, this.activeIndex) // 限制普通组件到istop前面
  929. var length = this.previewList.length;
  930. var isLast = length - 1 === this.activeIndex;
  931. this.previewList = this.addIndexItem(this.previewList, initObj, this.activeIndex)
  932. if (length && (isLast || this.activeIndex == -1)) {
  933. // 有值是否最后一项
  934. this.activeIndex = length - 1
  935. }
  936. this.activeItem = initObj
  937. this.activeIndex += 1
  938. },
  939. getIsTopArrLength() {
  940. return this.previewList.filter(item => {
  941. const e1 = item.funcListItem || {}
  942. return e1.is_top
  943. }).length
  944. },
  945. changeDiyItem(item, index, params = {}) {
  946. var {
  947. type
  948. } = params
  949. var list = deepClone(this.previewList) // 克隆一下
  950. if (type !== "isShow") {
  951. // 显示问题 -- 样式组件不让修改
  952. item.isShow = list[this.activeIndex].isShow;
  953. }
  954. // 直接全部覆盖
  955. list[this.activeIndex] = deepClone(item)
  956. this.activeItem = deepClone(item)
  957. this.previewList = list
  958. },
  959. selectItme(item, index) {
  960. if (this.activeIndex == index) {
  961. this.activeIndex = -1
  962. this.activeItem = null
  963. } else {
  964. this.activeIndex = index
  965. this.activeItem = item
  966. }
  967. this.$forceUpdate()
  968. },
  969. pushItem(list = [], target = {}) {
  970. var result = deepClone(list)
  971. result.push(target)
  972. return result
  973. },
  974. /**
  975. * 两种形式,一种push,追加index
  976. */
  977. addIndexItem(list = [], target = {}, index) { // 往指定的index添加item
  978. var result = deepClone(list)
  979. if (this.activeIndex < 0) {
  980. result.push(target)
  981. } else {
  982. var handleArr = [deepClone(result[index]), target]
  983. result.splice(index, 1, ...handleArr)
  984. }
  985. return result
  986. },
  987. // 操作相关
  988. copy() {
  989. if (!this.activeItem || this.activeItem.disabled || this.activeItem.is_copy === false) {
  990. return
  991. }
  992. var item = deepClone(this.activeItem)
  993. item.id = getRandomId()
  994. this.previewList = this.addIndexItem(this.previewList, item, this.activeIndex)
  995. },
  996. /**
  997. * @params {string} obj.type up - down - show
  998. */
  999. operateFn(obj) {
  1000. const methods = {
  1001. show: 'showItem',
  1002. up: 'upItem',
  1003. down: 'downItem',
  1004. delete: 'deleteItem'
  1005. }
  1006. this[methods[obj.type]](obj.index)
  1007. },
  1008. showItem(index) {
  1009. var item = deepClone(this.activeItem);
  1010. item.isShow = !item.isShow
  1011. this.changeDiyItem(item, index, {
  1012. type: "isShow"
  1013. })
  1014. },
  1015. updateView(item, index) {
  1016. this.activeItem = item
  1017. this.activeIndex = index
  1018. },
  1019. deleteItem(index) {
  1020. if (this.activeItem.disabled || this.activeItem.is_delete === false) {
  1021. return false
  1022. }
  1023. this.previewList.splice(index, 1);
  1024. // 删除后失去焦点
  1025. this.activeIndex = -1;
  1026. this.activeItem = null;
  1027. },
  1028. upItem(index) {
  1029. const topArr = this.previewList.filter(item => {
  1030. const e1 = item.funcListItem || {}
  1031. return e1.is_top
  1032. }) // 操作上限
  1033. const topIndex = Math.max(0, topArr.length)
  1034. if (index <= 0 || this.activeItem.disabled || this.activeItem.is_up === false) {
  1035. return false
  1036. } else if (index <= topIndex) {
  1037. alert("不可以再操作了噢");
  1038. return false
  1039. }
  1040. var previewList = deepClone(this.previewList);
  1041. this.previewList = this.swapItems(previewList, index, index - 1);
  1042. this.updateView(this.previewList[index - 1], index - 1);
  1043. },
  1044. downItem(index) {
  1045. if (this.activeItem.disabled || this.activeItem.is_down === false) {
  1046. return false
  1047. } else if (index === this.previewList.length - 1) {
  1048. alert("不可以再操作了噢");
  1049. return false
  1050. }
  1051. var previewList = deepClone(this.previewList);
  1052. this.previewList = this.swapItems(previewList, index, index + 1);
  1053. this.updateView(this.previewList[index + 1], index + 1);
  1054. },
  1055. swapItems(arr, index1, index2) {
  1056. var item = deepClone(arr[index1]);
  1057. arr[index1] = arr[index2]
  1058. arr[index2] = item
  1059. return arr
  1060. },
  1061. changeMove(e) {
  1062. const {
  1063. newIndex,
  1064. list
  1065. } = e
  1066. // 移动拖拽改变索引
  1067. this.previewList = deepClone(list);
  1068. this.updateView(this.previewList[newIndex], newIndex);
  1069. },
  1070. // 更新主页面
  1071. updateMainPage(item) {
  1072. this.mainPage = deepClone(item);
  1073. },
  1074. // 处理兼容老数据
  1075. addIsTop(list, type) {
  1076. const obj = {
  1077. ['nav']: 1,
  1078. ['member-info']: 2
  1079. }
  1080. const top = obj[type]
  1081. list.forEach(item => {
  1082. if (item.funcListItem.type === type && !item.funcListItem.is_top) {
  1083. item.funcListItem.is_top = top
  1084. }
  1085. });
  1086. return list
  1087. }
  1088. }
  1089. })
  1090. // 拦截页面卸载
  1091. window.onbeforeunload = function(e) {
  1092. var e = window.event || e;
  1093. e.returnValue = ("确定离开当前页面吗?");
  1094. }
  1095. </script>
  1096. <style>
  1097. #app {
  1098. display: flex;
  1099. flex-direction: column;
  1100. min-width: 1250px;
  1101. }
  1102. #diy-test-preview {
  1103. margin: auto;
  1104. }
  1105. .diy-header-backs {
  1106. cursor: pointer;
  1107. }
  1108. .fixed {
  1109. position: fixed;
  1110. left: 0;
  1111. top: 0;
  1112. width: 100%;
  1113. height: 100%;
  1114. z-index: 999;
  1115. }
  1116. .diy-header {
  1117. height: 55px;
  1118. background: #313131;
  1119. opacity: 1;
  1120. border-radius: 0px;
  1121. justify-content: space-between;
  1122. font-size: 13px;
  1123. font-family: PingFang SC;
  1124. /*font-weight: bold;*/
  1125. color: #FFFFFF;
  1126. padding: 0 30px;
  1127. }
  1128. .diy-contain {
  1129. flex: 1;
  1130. overflow: auto;
  1131. background: #F4F6F8;
  1132. justify-content: space-between;
  1133. }
  1134. .diy-func-list {
  1135. box-sizing: border-box;
  1136. width: 300px;
  1137. background: #FFFFFF;
  1138. border-radius: 0px;
  1139. }
  1140. .diy-preview {
  1141. flex: 1;
  1142. background: #F4F6F8;
  1143. justify-content: space-around;
  1144. }
  1145. .fill-box {
  1146. position: relative;
  1147. width: 467px;
  1148. height: 100%;
  1149. }
  1150. .flex {
  1151. display: flex;
  1152. }
  1153. .flex-x-center {
  1154. justify-content: center;
  1155. }
  1156. .flex-y-center {
  1157. align-items: center;
  1158. }
  1159. /* */
  1160. .fade-enter-active,
  1161. .fade-leave-active {
  1162. transition: opacity 1s;
  1163. }
  1164. .fade-enter,
  1165. .fade-leave-to {
  1166. opacity: 0;
  1167. }
  1168. .diy-text-font {
  1169. font-size: 13px;
  1170. font-weight: 700;
  1171. color: #999999;
  1172. }
  1173. .fill-border-top {
  1174. border-top: 10px solid #F4F6F8;
  1175. }
  1176. .fill-border-bottom {
  1177. border-bottom: 10px solid #F4F6F8;
  1178. }
  1179. /* 导航栏 */
  1180. .custom-page {
  1181. width: 181px;
  1182. height: 60px;
  1183. line-height: 60px;
  1184. background: var(--diy-theme);
  1185. font-weight: 700;
  1186. border-radius: 0px;
  1187. text-align: center;
  1188. }
  1189. .save {
  1190. font-size: 24px;
  1191. vertical-align: middle;
  1192. }
  1193. .save+span {
  1194. vertical-align: middle;
  1195. }
  1196. .sava_btn .el-button--primary {
  1197. background: var(--diy-theme);
  1198. border-color: var(--diy-theme);
  1199. padding: 8px 14px;
  1200. }
  1201. .diy_nav_right>div {
  1202. padding: 8px 14px;
  1203. }
  1204. .diy_nav_right .sava_btn {
  1205. padding: 0;
  1206. }
  1207. .page_set>i {
  1208. font-size: 24px;
  1209. vertical-align: middle;
  1210. }
  1211. .page_set>span {
  1212. vertical-align: middle;
  1213. }
  1214. .xiaoshou {
  1215. cursor: pointer;
  1216. }
  1217. :root {
  1218. /* 主题色 */
  1219. --diy-theme: #F10009;
  1220. /* 主要字体颜色 */
  1221. --diy-color: #F10009;
  1222. }
  1223. /* 修改ele默认样式 */
  1224. /* slider组件 */
  1225. .el-slider__bar {
  1226. background-color: var(--diy-theme);
  1227. }
  1228. .el-slider__button {
  1229. border-color: var(--diy-theme);
  1230. }
  1231. /* input组件 */
  1232. .el-input .el-input__inner:focus {
  1233. border: 1px solid var(--diy-theme);
  1234. }
  1235. /* textarea */
  1236. .el-textarea .el-textarea__inner:focus {
  1237. border: 1px solid var(--diy-theme);
  1238. }
  1239. /* radio单选组件 */
  1240. .el-radio__input.is-checked+.el-radio__label {
  1241. color: var(--diy-color);
  1242. }
  1243. .el-radio__input.is-checked .el-radio__inner {
  1244. border-color: var(--diy-theme);
  1245. background: var(--diy-theme);
  1246. }
  1247. .el-radio-button__orig-radio:checked+.el-radio-button__inner {
  1248. border-color: var(--diy-theme);
  1249. box-shadow: -1px 0 0 0 var(--diy-theme);
  1250. }
  1251. /* 多选 */
  1252. .el-checkbox__input.is-checked+.el-checkbox__label {
  1253. color: var(--diy-color);
  1254. }
  1255. .el-checkbox__input.is-checked .el-checkbox__inner,
  1256. .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  1257. border-color: var(--diy-theme);
  1258. background: var(--diy-theme);
  1259. }
  1260. </style>