diy-index.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. priceColor: '#db0505'
  557. },
  558. "permission": [],
  559. "funcListItem": {
  560. is_top: 0,
  561. title: "砍价",
  562. type: "bargain-goods"
  563. },
  564. "isShow": true,
  565. "is_show_permission": false,
  566. "is_drag": false,
  567. "disabled": true,
  568. }]
  569. return result
  570. }
  571. function debounce(func, wait) {
  572. let timer;
  573. return function() {
  574. let context = this; // 注意 this 指向
  575. let args = arguments; // arguments中存着e
  576. if (timer) clearTimeout(timer);
  577. timer = setTimeout(() => {
  578. func.apply(this, args)
  579. }, wait)
  580. }
  581. }
  582. </script>
  583. <?php
  584. use common\helpers\ComponentHelper;
  585. use common\models\diy\DiyComponents;
  586. $diyPath = __DIR__ . '/components/common';
  587. $stylePath = __DIR__ . '/components/style-components';
  588. $components = DiyComponents::getDir($diyPath, $diyPath);
  589. $styleComponents = DiyComponents::getDir($stylePath, $stylePath);
  590. ComponentHelper::loadComponentView('components/diy-preview', __DIR__);
  591. ComponentHelper::loadComponentView('components/diy-style-preview', __DIR__);
  592. foreach ($components as $item) {
  593. ComponentHelper::loadComponentView("components/common/{$item}", __DIR__);
  594. }
  595. foreach ($styleComponents as $item) {
  596. ComponentHelper::loadComponentView("components/style-components/{$item}", __DIR__);
  597. }
  598. ?>
  599. <div id="app">
  600. <div style="padding-bottom: 10px;padding-right: 20px;background: #F4F6F8;" @click="save" flex="main:right">
  601. <el-button type="primary">保存</el-button>
  602. </div>
  603. <div class="diy-contain flex" v-loading="loading">
  604. <diy-preview :active-index="activeIndex" :list="previewList" @select-item="selectItme" @change-move="changeMove"></diy-preview>
  605. <transition name="fade">
  606. <diy-style-preview :active-item="activeItem" :active-index="activeIndex" :permission="permission" @update="changeDiyItem" v-if="showUpdate"></diy-style-preview>
  607. </transition>
  608. </div>
  609. <!-- 预览弹窗 -->
  610. <el-dialog title="预览弹窗" :modal-append-to-body="false" :visible.sync="dialogFormVisible">
  611. <div class="flex flex-x-center" style="height: 600px;overflow:auto">
  612. <img id="diy-test-preview" :src="previewSrc"></img>
  613. </div>
  614. <div slot="footer" class="dialog-footer">
  615. <el-button @click="dialogFormVisible = false">取 消</el-button>
  616. <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
  617. </div>
  618. </el-dialog>
  619. </div>
  620. <script>
  621. Vue.prototype.$capitals = []; // 用户信息对象
  622. Vue.prototype.$eventBus = new Vue();
  623. var PageShareData = {
  624. pageIndex: false // 点击页面设置标识 => nav/style
  625. }; // 全局共享数据 - 没有响应式
  626. new Vue({
  627. el: '#app',
  628. data() {
  629. return {
  630. capital: [],
  631. dialogFormVisible: false,
  632. /**
  633. * @descriptions 浮动()、普通()、置顶(isTop)、弹窗(alert)四种显示组件
  634. * @param {string} type // diy组件类型
  635. * @param {string} title // 二级标题
  636. * @param {string} img // diy组件图片
  637. * @param {number} count // 可添加diy组件数量
  638. * @param {boolean} is_top // diy组件是否置顶组件 - 待添加 - 分添加等级 1 2 3 数字靠前权限越高
  639. * @param {object} permission // 组件的不可操作权限 {"is_delete": false,"is_drag": false,"disabled": true, // 禁用操作}
  640. */
  641. pageComponentsList: [],
  642. /**
  643. * @param {boolean} is_show_permission // 显示权限
  644. * @param {boolean} is_delete // 是否可删除
  645. * @param {boolean} is_up // 是否可上移
  646. * @param {boolean} is_down // 是否可下移
  647. * @param {boolean} is_drag // 是否可拖拽
  648. * @param {boolean} is_copy // 是否可复制
  649. * @param {boolean} disabled // 全部禁用操作
  650. */
  651. previewList: [], // 预览数组对象
  652. activeIndex: -1, // 预览激活索引
  653. activeItem: null, // 预览激活对象
  654. type: "add", // add新增 - edit编辑
  655. test: ["a", "b", "c"],
  656. showUpdate: true,
  657. responentData: null, // 详情响应的总数据
  658. permission: [], // 权限数组
  659. isClickPage: false, // 是否点击分页设置
  660. loading: false, // 数据加载
  661. saveLoading: false, // 保存数据
  662. fields: {
  663. upload_type: 'images'
  664. },
  665. previewSrc: "",
  666. }
  667. },
  668. provide() {
  669. return {
  670. capitals: this.capital
  671. }
  672. },
  673. watch: {
  674. activeIndex() {
  675. this.showUpdate = false
  676. setTimeout(() => {
  677. this.showUpdate = true
  678. }, 0)
  679. },
  680. },
  681. created() {
  682. // console.log(this.dynamicImgSrc('/abc/ss'));
  683. // 添加不同的模板nav
  684. this.previewList.push(...getNavData())
  685. this.init()
  686. this.type = getQuery('id') ? "edit" : "add"
  687. const id = getQuery('id');
  688. this.detail(id)
  689. },
  690. methods: {
  691. savePreview() {
  692. this.activeIndex = -1
  693. this.activeItem = null
  694. this.$nextTick(() => {
  695. const contain = document.getElementById("diy-preview-contain");
  696. this.deepNode(contain);
  697. setTimeout(() => {
  698. this.dialogFormVisible = true
  699. html2canvas(contain, {
  700. useCORS: true
  701. }).then(canvas => {
  702. const src = canvas.toDataURL("image/png")
  703. this.upload(this.dataURLtoFile(src)).then(res => {
  704. if (res.code == 0) {
  705. this.previewSrc = res.data.url
  706. this.save(false)
  707. } else {
  708. this.$message({
  709. message: res.msg,
  710. type: 'warning'
  711. });
  712. }
  713. })
  714. })
  715. }, 0)
  716. })
  717. },
  718. dataURLtoFile(dataurl) { // 生成Blob
  719. var arr = dataurl.split(',');
  720. var mime = arr[0].match(/:(.*?);/)[1];
  721. var bstr = atob(arr[1]);
  722. var n = bstr.length;
  723. var u8arr = new Uint8Array(n);
  724. while (n--) {
  725. u8arr[n] = bstr.charCodeAt(n);
  726. }
  727. return new Blob([u8arr], {
  728. type: mime
  729. });
  730. },
  731. upload(file) {
  732. let formData = new FormData();
  733. const params = {
  734. upload_type: 'images'
  735. };
  736. params['r'] = 'common/file/upload';
  737. for (let i in this.fields) {
  738. formData.append(i, this.fields[i]);
  739. }
  740. formData.append('file', file, "image.png");
  741. return new Promise(successCallback => {
  742. this.$request({
  743. headers: {
  744. 'Content-Type': 'multipart/form-data'
  745. },
  746. params: params,
  747. method: 'post',
  748. data: formData,
  749. }).then(e => {
  750. successCallback(e.data)
  751. }).catch(e => {
  752. file._complete = true;
  753. });
  754. })
  755. },
  756. deepNode(contain) {
  757. if (contain.tagName === "IMG") {
  758. contain.src = contain.src + '?t=' + Date.now()
  759. contain.setAttribute('crossorigin', 'anonymous')
  760. }
  761. if (contain.children && contain.children.length) {
  762. [].forEach.call(contain.children, (item => {
  763. this.deepNode(item)
  764. }))
  765. }
  766. },
  767. pageSet() {
  768. PageShareData.pageIndex = true
  769. this.$eventBus.$emit("onPageClick")
  770. var index = 0;
  771. this.activeIndex = index
  772. this.activeItem = this.previewList[index]
  773. // this.changeDiyItem()
  774. },
  775. detail(id) {
  776. // 详情
  777. this.loading = true
  778. request({
  779. params: {
  780. r: 'bargain/default/diy-index'
  781. },
  782. method: 'get',
  783. }).then(res => {
  784. const {
  785. data
  786. } = res.data
  787. if (res.data.code != 0) {
  788. this.$message({
  789. message: res.data.msg,
  790. type: 'error'
  791. });
  792. return false
  793. }
  794. const previewList = JSON.parse(data.content)
  795. this.previewList = previewList
  796. }).finally(res => {
  797. this.loading = false
  798. })
  799. },
  800. save: debounce(function(isLoading = true) {
  801. // 对接保存数据接口
  802. if (this.saveFlag) {
  803. console.log("重复提交");
  804. return
  805. }
  806. const title = this.previewList[0].data.name
  807. if (!title) {
  808. this.$message({
  809. message: '请输入标题',
  810. type: 'warning'
  811. });
  812. return
  813. }
  814. isLoading && (this.saveLoading = true)
  815. this.saveFlag = true // 防止重复提交限制
  816. request({
  817. params: {
  818. r: '/bargain/default/diy-index'
  819. },
  820. method: 'post',
  821. data: {
  822. // id: getQuery('id') || null,
  823. // title,
  824. content: JSON.stringify(this.previewList),
  825. // cover: this.previewSrc
  826. }
  827. }).then(res => {
  828. if (res.data.code != 0) {
  829. this.$message({
  830. message: res.data.msg,
  831. type: 'error'
  832. });
  833. } else {
  834. this.$message({
  835. message: res.data.msg,
  836. type: 'success'
  837. });
  838. }
  839. }).finally(res => {
  840. this.saveFlag = false
  841. this.saveLoading = false
  842. })
  843. },
  844. 400),
  845. init() {
  846. this.$eventBus.$on("diy-preview-copy", (item = {}) => {
  847. this.copy(item)
  848. })
  849. this.$eventBus.$on("diy-preview-operateFn", (item = {}) => {
  850. this.operateFn(item)
  851. })
  852. this.$eventBus.$on("diy-back-set", () => {
  853. this.updateView(null, -1)
  854. })
  855. },
  856. validateCount(e) {
  857. // 校验组件能添加几个
  858. var msg = "";
  859. var list = this.previewList.filter(item => {
  860. return item.funcListItem.type === e.type
  861. });
  862. if (e && e.count && list.length >= e.count) {
  863. msg = `该组件只能添加${e.count}个哦`
  864. }
  865. if (msg) {
  866. this.$message({
  867. message: msg,
  868. type: 'warning'
  869. });
  870. }
  871. return msg
  872. },
  873. setInitObj(data, e) {
  874. if (!e.permission) {
  875. return false
  876. }
  877. const target = typeof e.permission === "object" ? e.permission : JSON.parse(e.permission)
  878. for (const key in target) {
  879. data[key] = e.permission[key]
  880. }
  881. },
  882. onClick(e) {
  883. const flag = this.validateCount(e) // 校验组件可添加个数
  884. if (flag) return // 不通过
  885. var initObj = {
  886. id: getRandomId(this.previewList),
  887. data: {}, // 对应数据
  888. computedStyle: {}, // 对应组件样式-尺寸
  889. permission: [], // 展示权限列表 ==> 对应用户等级or其他
  890. funcListItem: e,
  891. isShow: true, // 前端是否显示
  892. is_show_permission: true, // 是否显示权限
  893. } // 创建的初始数据
  894. this.setInitObj(initObj, e) // 设置可操作方法
  895. if (e.is_top) { // 置顶组件操作 - 不操作以前===>
  896. const index = this.previewList.findIndex(item => {
  897. const e1 = item.funcListItem || {}
  898. return e1.is_top === undefined || (e1.is_top >= e.is_top)
  899. })
  900. this.activeItem = initObj
  901. if (index < 0) {
  902. this.previewList.push(initObj)
  903. this.activeIndex = Math.max(0, this.previewList.length - 1)
  904. } else {
  905. this.previewList.splice(index, 0, initObj)
  906. this.activeIndex = index
  907. }
  908. return false
  909. }
  910. // if(this.isAlearComponents()){
  911. // return false
  912. // }
  913. // 不置顶组件添加
  914. this.activeIndex = Math.max(this.getIsTopArrLength() - 1, this.activeIndex) // 限制普通组件到istop前面
  915. var length = this.previewList.length;
  916. var isLast = length - 1 === this.activeIndex;
  917. this.previewList = this.addIndexItem(this.previewList, initObj, this.activeIndex)
  918. if (length && (isLast || this.activeIndex == -1)) {
  919. // 有值是否最后一项
  920. this.activeIndex = length - 1
  921. }
  922. this.activeItem = initObj
  923. this.activeIndex += 1
  924. },
  925. getIsTopArrLength() {
  926. return this.previewList.filter(item => {
  927. const e1 = item.funcListItem || {}
  928. return e1.is_top
  929. }).length
  930. },
  931. changeDiyItem(item, index, params = {}) {
  932. var {
  933. type
  934. } = params
  935. var list = deepClone(this.previewList) // 克隆一下
  936. if (type !== "isShow") {
  937. // 显示问题 -- 样式组件不让修改
  938. item.isShow = list[this.activeIndex].isShow;
  939. }
  940. // 直接全部覆盖
  941. list[this.activeIndex] = deepClone(item)
  942. this.activeItem = deepClone(item)
  943. this.previewList = list
  944. },
  945. selectItme(item, index) {
  946. if (this.activeIndex == index) {
  947. this.activeIndex = -1
  948. this.activeItem = null
  949. } else {
  950. this.activeIndex = index
  951. this.activeItem = item
  952. }
  953. this.$forceUpdate()
  954. },
  955. pushItem(list = [], target = {}) {
  956. var result = deepClone(list)
  957. result.push(target)
  958. return result
  959. },
  960. /**
  961. * 两种形式,一种push,追加index
  962. */
  963. addIndexItem(list = [], target = {}, index) { // 往指定的index添加item
  964. var result = deepClone(list)
  965. if (this.activeIndex < 0) {
  966. result.push(target)
  967. } else {
  968. var handleArr = [deepClone(result[index]), target]
  969. result.splice(index, 1, ...handleArr)
  970. }
  971. return result
  972. },
  973. // 操作相关
  974. copy() {
  975. if (!this.activeItem || this.activeItem.disabled || this.activeItem.is_copy === false) {
  976. return
  977. }
  978. var item = deepClone(this.activeItem)
  979. item.id = getRandomId()
  980. this.previewList = this.addIndexItem(this.previewList, item, this.activeIndex)
  981. },
  982. /**
  983. * @params {string} obj.type up - down - show
  984. */
  985. operateFn(obj) {
  986. const methods = {
  987. show: 'showItem',
  988. up: 'upItem',
  989. down: 'downItem',
  990. delete: 'deleteItem'
  991. }
  992. this[methods[obj.type]](obj.index)
  993. },
  994. showItem(index) {
  995. var item = deepClone(this.activeItem);
  996. item.isShow = !item.isShow
  997. this.changeDiyItem(item, index, {
  998. type: "isShow"
  999. })
  1000. },
  1001. updateView(item, index) {
  1002. this.activeItem = item
  1003. this.activeIndex = index
  1004. },
  1005. deleteItem(index) {
  1006. if (this.activeItem.disabled || this.activeItem.is_delete === false) {
  1007. return false
  1008. }
  1009. this.previewList.splice(index, 1);
  1010. // 删除后失去焦点
  1011. this.activeIndex = -1;
  1012. this.activeItem = null;
  1013. },
  1014. upItem(index) {
  1015. const topArr = this.previewList.filter(item => {
  1016. const e1 = item.funcListItem || {}
  1017. return e1.is_top
  1018. }) // 操作上限
  1019. const topIndex = Math.max(0, topArr.length)
  1020. if (index <= 0 || this.activeItem.disabled || this.activeItem.is_up === false) {
  1021. return false
  1022. } else if (index <= topIndex) {
  1023. alert("不可以再操作了噢");
  1024. return false
  1025. }
  1026. var previewList = deepClone(this.previewList);
  1027. this.previewList = this.swapItems(previewList, index, index - 1);
  1028. this.updateView(this.previewList[index - 1], index - 1);
  1029. },
  1030. downItem(index) {
  1031. if (this.activeItem.disabled || this.activeItem.is_down === false) {
  1032. return false
  1033. } else if (index === this.previewList.length - 1) {
  1034. alert("不可以再操作了噢");
  1035. return false
  1036. }
  1037. var previewList = deepClone(this.previewList);
  1038. this.previewList = this.swapItems(previewList, index, index + 1);
  1039. this.updateView(this.previewList[index + 1], index + 1);
  1040. },
  1041. swapItems(arr, index1, index2) {
  1042. var item = deepClone(arr[index1]);
  1043. arr[index1] = arr[index2]
  1044. arr[index2] = item
  1045. return arr
  1046. },
  1047. changeMove(e) {
  1048. const {
  1049. newIndex,
  1050. list
  1051. } = e
  1052. // 移动拖拽改变索引
  1053. this.previewList = deepClone(list);
  1054. this.updateView(this.previewList[newIndex], newIndex);
  1055. },
  1056. // 更新主页面
  1057. updateMainPage(item) {
  1058. this.mainPage = deepClone(item);
  1059. },
  1060. // 处理兼容老数据
  1061. addIsTop(list, type) {
  1062. const obj = {
  1063. ['nav']: 1,
  1064. ['member-info']: 2
  1065. }
  1066. const top = obj[type]
  1067. list.forEach(item => {
  1068. if (item.funcListItem.type === type && !item.funcListItem.is_top) {
  1069. item.funcListItem.is_top = top
  1070. }
  1071. });
  1072. return list
  1073. }
  1074. }
  1075. })
  1076. // 拦截页面卸载
  1077. window.onbeforeunload = function(e) {
  1078. var e = window.event || e;
  1079. e.returnValue = ("确定离开当前页面吗?");
  1080. }
  1081. </script>
  1082. <style>
  1083. #app {
  1084. display: flex;
  1085. flex-direction: column;
  1086. min-width: 1250px;
  1087. }
  1088. #diy-test-preview {
  1089. margin: auto;
  1090. }
  1091. .diy-header-backs {
  1092. cursor: pointer;
  1093. }
  1094. .fixed {
  1095. position: fixed;
  1096. left: 0;
  1097. top: 0;
  1098. width: 100%;
  1099. height: 100%;
  1100. z-index: 999;
  1101. }
  1102. .diy-header {
  1103. height: 60px;
  1104. background: #313131;
  1105. opacity: 1;
  1106. border-radius: 0px;
  1107. justify-content: space-between;
  1108. font-size: 13px;
  1109. font-family: PingFang SC;
  1110. font-weight: bold;
  1111. color: #FFFFFF;
  1112. padding: 0 30px;
  1113. }
  1114. .diy-contain {
  1115. flex: 1;
  1116. overflow: auto;
  1117. background: #F4F6F8;
  1118. justify-content: space-between;
  1119. }
  1120. .diy-func-list {
  1121. box-sizing: border-box;
  1122. width: 354px;
  1123. background: #FFFFFF;
  1124. border-radius: 0px;
  1125. }
  1126. .diy-preview {
  1127. flex: 1;
  1128. background: #F4F6F8;
  1129. justify-content: space-around;
  1130. }
  1131. .fill-box {
  1132. position: relative;
  1133. width: 467px;
  1134. height: 100%;
  1135. }
  1136. .flex {
  1137. display: flex;
  1138. }
  1139. .flex-x-center {
  1140. justify-content: center;
  1141. }
  1142. .flex-y-center {
  1143. align-items: center;
  1144. }
  1145. /* */
  1146. .fade-enter-active,
  1147. .fade-leave-active {
  1148. transition: opacity 1s;
  1149. }
  1150. .fade-enter,
  1151. .fade-leave-to {
  1152. opacity: 0;
  1153. }
  1154. .diy-text-font {
  1155. font-size: 13px;
  1156. font-weight: 700;
  1157. color: #999999;
  1158. }
  1159. .fill-border-top {
  1160. border-top: 10px solid #F4F6F8;
  1161. }
  1162. .fill-border-bottom {
  1163. border-bottom: 10px solid #F4F6F8;
  1164. }
  1165. /* 导航栏 */
  1166. .custom-page {
  1167. width: 181px;
  1168. height: 60px;
  1169. line-height: 60px;
  1170. background: var(--diy-theme);
  1171. font-weight: 700;
  1172. border-radius: 0px;
  1173. text-align: center;
  1174. }
  1175. .save {
  1176. font-size: 24px;
  1177. vertical-align: middle;
  1178. }
  1179. .save+span {
  1180. vertical-align: middle;
  1181. }
  1182. .sava_btn .el-button--primary {
  1183. background: var(--diy-theme);
  1184. border-color: var(--diy-theme);
  1185. padding: 8px 14px;
  1186. }
  1187. .diy_nav_right>div {
  1188. padding: 8px 14px;
  1189. }
  1190. .diy_nav_right .sava_btn {
  1191. padding: 0;
  1192. }
  1193. .page_set>i {
  1194. font-size: 24px;
  1195. vertical-align: middle;
  1196. }
  1197. .page_set>span {
  1198. vertical-align: middle;
  1199. }
  1200. .xiaoshou {
  1201. cursor: pointer;
  1202. }
  1203. :root {
  1204. /* 主题色 */
  1205. --diy-theme: #F10009;
  1206. /* 主要字体颜色 */
  1207. --diy-color: #F10009;
  1208. }
  1209. /* 修改ele默认样式 */
  1210. /* slider组件 */
  1211. .el-slider__bar {
  1212. background-color: var(--diy-theme);
  1213. }
  1214. .el-slider__button {
  1215. border-color: var(--diy-theme);
  1216. }
  1217. /* input组件 */
  1218. .el-input .el-input__inner:focus {
  1219. border: 1px solid var(--diy-theme);
  1220. }
  1221. /* textarea */
  1222. .el-textarea .el-textarea__inner:focus {
  1223. border: 1px solid var(--diy-theme);
  1224. }
  1225. /* radio单选组件 */
  1226. .el-radio__input.is-checked+.el-radio__label {
  1227. color: var(--diy-color);
  1228. }
  1229. .el-radio__input.is-checked .el-radio__inner {
  1230. border-color: var(--diy-theme);
  1231. background: var(--diy-theme);
  1232. }
  1233. .el-radio-button__orig-radio:checked+.el-radio-button__inner {
  1234. border-color: var(--diy-theme);
  1235. box-shadow: -1px 0 0 0 var(--diy-theme);
  1236. }
  1237. /* 多选 */
  1238. .el-checkbox__input.is-checked+.el-checkbox__label {
  1239. color: var(--diy-color);
  1240. }
  1241. .el-checkbox__input.is-checked .el-checkbox__inner,
  1242. .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  1243. border-color: var(--diy-theme);
  1244. background: var(--diy-theme);
  1245. }
  1246. </style>