template.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. import {getSiteInfo,getSiteCategory,selectWebsiteDepartment,selectWebsiteArea,getWebsiteArticlesList,
  2. selectWebsiteArticleInfo,getWebsiteCategory} from '@/api/cms'
  3. import { Message } from 'element-ui'; //注意在这里引入是非常不符合规范的
  4. import Vue from 'vue'; //导入Vue 因为我们要进行深层次的json修改,深拷贝的数据无法令视图更新
  5. //所以我们需要使用Vue的$set方法来强制视图更新,在vuex中没有this,这里我们需要通过引入vue来使用vue.$set
  6. //引入随机模板json
  7. import randomTemplateJson from '@/utils/templateJson/index/1.js';//首页
  8. const state = {
  9. //0.全局配置 start------------------------------------------------------------>
  10. editWebsiteId: "",//当前编辑网站的id
  11. stepStatus: true,//是否显示现在进行到哪一步:true=显示 false=不显示
  12. //0.全局配置 end------------------------------------------------------------>
  13. //1.画布数据 start------------------------------------------------------------>
  14. pageStatus:1,//当前编辑哪个页面 1=首页 2=分类页 3=列表页 4=详情页 5=搜索页 6=自定义列表页 7=自定义详情页
  15. menuType:1, //当前菜单显示板块还是组件 1=板块 2=组件
  16. previewStatus:false,//是否预览
  17. gridKey:0,//使用gridKey来强制更新视图
  18. pageData: { //自助建站拖拽板块的数据,注意,这里并不是提交到后台的数据
  19. index:[
  20. ],//首页
  21. class:[],//分类页
  22. list:[],//列表页bang
  23. article:[],//详情页
  24. search:[],//搜索页
  25. aloneList:[],//自定义列表页
  26. aloneArticle:[],//自定义详情页
  27. },
  28. // layout: [
  29. // // i = id
  30. // // w = 最大宽度是12
  31. // // { i: "0", x: 0, y: 0, w: 12, h: 2, content:""},
  32. // // { i: "1", x: 0, y: 0, w: 12, h: 2, content:""},
  33. // ],
  34. //1.画布数据 end------------------------------------------------------------>
  35. //2.站点数据 start------------------------------------------------------------>
  36. webSiteInfo:"",//网站信息
  37. webSiteMenu:"",//网站包含的导航池
  38. area:{//地区
  39. economize:[],//省区
  40. market:[],//市区
  41. county:[]//县区
  42. },
  43. departmentList:"",//职能部门
  44. //2.站点数据 end------------------------------------------------------------>
  45. //3.画布构造 start------------------------------------------------------------>
  46. editSectorY:0,//当前组件在画布中的位置
  47. mouseXY:{//鼠标位置
  48. "x": 12,
  49. "y": 33
  50. },
  51. DragPos:{//创建的占位符大小
  52. "x": null,
  53. "y": null,
  54. "w": 12,
  55. "h": 2,
  56. "i": null
  57. },
  58. //获得gridlayout对象
  59. gridlayoutObj:null,
  60. //3.画布构造 end------------------------------------------------------------>
  61. //4.构造的网站数据 start------------------------------------------------------------>
  62. editWindowStatus:false,//编辑组件弹出框是否显示
  63. editComponentWindowStatus:false,//选择组件样式弹出框是否显示
  64. editWebsiteTemplateJsonWindow:false,//当前准备提交的数据 后期删除
  65. editWindowTitle:"",//编辑弹出框标题
  66. editSectorId:0,//当前正在编辑的板块id
  67. editDataSort:0,//当前正在编辑的数据位置
  68. editComponentSort:0,//当前正在编辑的组件id
  69. editComponentType:0,//当前正在编辑的组件类型
  70. editComponentSize:0,//当前组件展示的条数
  71. editComponentStyle:0,//当前编辑的组件样式
  72. editWebsiteCategory:[],//当前网站全部关联导航
  73. webSiteData:{
  74. //1.base网站基本信息
  75. base:{
  76. websiteId:1,//网站id
  77. },
  78. //2.style信息
  79. style:{
  80. styleId:1,//风格id
  81. },
  82. //3.板块信息 header,menu,footer 是页面自带的无需构建
  83. template:{
  84. //index = 首页 class=分类页 list=列表页 article=详情页 search=搜索页 aloneList=自定义列表页 aloneArticle=自定义详情页
  85. index:[],
  86. class:[],//分类页
  87. list:[],//列表页
  88. article:[],//详情页
  89. search:[],//搜索页
  90. aloneList:[],//自定义列表页
  91. aloneArticle:[],//自定义详情页
  92. }
  93. },
  94. //4.构造的网站数据 end------------------------------------------------------------>
  95. //5.组件回显数据 start------------------------------------------------------------>
  96. componentViewData:{
  97. pid_arr:[],//导航池
  98. pageSize:"",//展示条数
  99. adName:"",//广告名称
  100. titleName:"",//标题名称
  101. }
  102. //5.组件回显数据 start------------------------------------------------------------>
  103. }
  104. const mutations = {
  105. //0.全局配置 start------------------------------------------------------------>
  106. //设置网站的id
  107. setEditWebsiteId(state,id){
  108. state.editWebsiteId = id;
  109. },
  110. //展示步骤
  111. showStepStatus(state){
  112. state.stepStatus = true;
  113. },
  114. //隐藏步骤
  115. hiddenStepStatus(state){
  116. state.stepStatus = false;
  117. },
  118. //设置步骤
  119. setPageStatus(state,num){
  120. state.pageStatus = num;
  121. },
  122. //设置预览状态
  123. setPreviewStatus(state){
  124. state.previewStatus = !state.previewStatus;
  125. },
  126. //打开编辑组件弹出框
  127. setEditWindowStatus(state,data){
  128. state.editWindowStatus = true;
  129. state.editSectorId = data.id;
  130. state.editSectorY = data.y;
  131. state.editDataSort = data.dataSort;
  132. state.editComponentSort = data.sort;
  133. state.editComponentType = data.type;
  134. state.editComponentSize = data.size;
  135. },
  136. //关闭编辑组件弹出框
  137. closeEditWindowStatus(state){
  138. state.editWindowStatus = false;
  139. },
  140. //打开选择组件样式弹出框
  141. setComponentStyleStatus(state,data){
  142. state.editComponentWindowStatus = true;
  143. state.editSectorId = data.id;
  144. state.editDataSort = data.dataSort;
  145. state.editComponentSort = data.sort;
  146. state.editComponentType = data.type;
  147. state.editComponentStyle = data.style;
  148. },
  149. //关闭选择组件样式弹出框
  150. closeComponentStyleStatus(state){
  151. state.editComponentWindowStatus = false;
  152. },
  153. //关闭提交数据弹出框
  154. closeEditWebsiteTemplateJsonWindow(state){
  155. state.editWebsiteTemplateJsonWindow = false;
  156. },
  157. //选择组件样式
  158. selectComponentStyleNumber(state,data){
  159. state.editComponentStyle = data;
  160. },
  161. //设置组件回显数据
  162. setComponentViewData(state,data){
  163. //首页
  164. if(state.pageStatus == 1){
  165. //找到要修改的板块
  166. const targetModuleIndex = state.pageData.index.findIndex(module => module.i === data.id);
  167. //判断组件类型 1=普通新闻 2=tabs新闻选项卡 3=广告 4=友情链接 6=静态组件 7=文本 8=评论
  168. if(state.editComponentType == 1){
  169. state.componentViewData.pid_arr = state.pageData.index[targetModuleIndex].content.componentList[data.sort].componentData.category_id;
  170. state.componentViewData.pageSize = state.editComponentSize;
  171. }
  172. //2=tabs新闻选项卡
  173. if(state.editComponentType == 2){
  174. state.componentViewData.pid_arr = state.pageData.index[targetModuleIndex].content.componentList[data.sort].componentData.category_id;
  175. state.componentViewData.pageSize = state.editComponentSize;
  176. }
  177. //3=广告名称
  178. if(state.editComponentType == 3){
  179. state.componentViewData.adName = state.pageData.index[targetModuleIndex].content.componentList[data.sort].componentData.text;
  180. }
  181. //7=标题
  182. if(state.editComponentType == 7){
  183. state.componentViewData.titleName = state.pageData.index[targetModuleIndex].content.componentList[data.sort].componentData.text;
  184. }
  185. }
  186. //pageStatus==2 分类页
  187. if(state.pageStatus == 2){}
  188. //pageStatus==3 列表页
  189. if(state.pageStatus == 3){}
  190. //pageStatus==4 详情页
  191. if(state.pageStatus == 4){}
  192. //pageStatus==5 搜索页
  193. if(state.pageStatus == 5){}
  194. //pageStatus==6 自定义列表页
  195. if(state.pageStatus == 6){}
  196. //pageStatus==7 自定义详情页
  197. if(state.pageStatus == 7){}
  198. },
  199. //0.全局配置 start------------------------------------------------------------>
  200. //1.配置模块 start------------------------------------------------------------>
  201. //获得gridlayout对象
  202. setGridlayoutObj(state,data){
  203. state.gridlayoutObj = data;
  204. },
  205. //添加板块
  206. addModule(state,data){
  207. //pageStatus==1 首页
  208. if(state.pageStatus == 1){
  209. //判断当前一共有多少个模块最多能添加10个
  210. if(state.pageData.index.length >= 10){
  211. Message.error('最多只能添加10个模块!');
  212. return;
  213. }else{
  214. //判断是拖拽的还是点击添加进来的 click=点击
  215. if(data.source == "click"){
  216. console.log("通过点击添加一个板块");
  217. //通过时间戳生成id
  218. const currentTimestamp = Date.now();
  219. let id = currentTimestamp;
  220. //data.type 组件名称 data.h 模块高度
  221. //计算当前布局的最大 y 值
  222. const maxY = Math.max(...state.pageData.index.map(item => item.y), 0);
  223. console.log(data);
  224. //设置数据在构建json中的位置
  225. let dataSort = state.pageData.index.length;
  226. //添加板块id
  227. state.pageData.index.push({
  228. //i: state.pageData.index.length, //base 0.0.1 此方法会导致重复的id出现
  229. i: id,
  230. x: 0,
  231. y: maxY + 1,
  232. w: 12,
  233. h: data.h,
  234. type: data.type,
  235. //sectorData:data.jsonData //base 0.0.1 此问题导致深层数据无法被视图渲染
  236. content:data.jsonData,
  237. dataSort:dataSort
  238. });
  239. console.log("当前添加模块的dataSort为:" + dataSort);
  240. //当前的页面构建数据
  241. console.log(state.pageData.index);
  242. }
  243. //drag=拖拽
  244. if(data.source == "drag"){
  245. //拖拽添加
  246. console.log("通过拖拽添加一个板块");
  247. //通过时间戳生成id
  248. const currentTimestamp = Date.now();
  249. let id = currentTimestamp;
  250. //data.type 组件名称 data.h 模块高度
  251. //计算当前布局的最大 y 值
  252. const maxY = Math.max(...state.pageData.index.map(item => item.y), 0);
  253. console.log(data);
  254. //设置数据在构建json中的位置
  255. let dataSort = state.pageData.index.length;
  256. //添加板块id
  257. state.pageData.index.push({
  258. //i: state.pageData.index.length, //base 0.0.1 此方法会导致重复的id出现
  259. i: id,
  260. x: data.x,
  261. y: data.y,
  262. w: 12,
  263. h: data.h,
  264. type: data.type,
  265. //sectorData:data.jsonData //base 0.0.1 此问题导致深层数据无法被视图渲染
  266. content:data.jsonData,
  267. dataSort:dataSort
  268. });
  269. console.log("当前添加模块的dataSort为:" + dataSort);
  270. //当前的页面构建数据
  271. console.log(state.pageData.index);
  272. //需要调查一下这里最后的1,1
  273. state.gridlayoutObj.dragEvent('dragend', data.i, data.x, data.y, 1,1);
  274. try {
  275. state.gridlayoutObj.$children[state.pageData.index.length].$refs.item.style.display="block";
  276. } catch {}
  277. }
  278. }
  279. }
  280. //pageStatus==2 分类页
  281. if(state.pageStatus == 2){}
  282. //pageStatus==3 列表页
  283. if(state.pageStatus == 3){}
  284. //pageStatus==4 详情页
  285. if(state.pageStatus == 4){}
  286. //pageStatus==5 搜索页
  287. if(state.pageStatus == 5){}
  288. //pageStatus==6 自定义列表页
  289. if(state.pageStatus == 6){}
  290. //pageStatus==7 自定义详情页
  291. if(state.pageStatus == 7){}
  292. },
  293. //删除板块
  294. deleteModule(state,data){
  295. //data.i = id
  296. //data.dataSort = dataSort
  297. //pageStatus==1 首页
  298. if(state.pageStatus == 1){
  299. //必须通过重新查找id的方式来删除
  300. const indexToRemove = state.pageData.index.findIndex(item => item.i === data.i);
  301. if (indexToRemove !== -1) {
  302. state.pageData.index.splice(indexToRemove, 1);
  303. Message.success('模块已删除!');
  304. console.log('模块已删除,当前的页面构建数据为:', state.pageData.index);
  305. } else {
  306. Message.warning('未找到要删除的模块!');
  307. }
  308. // base 0.0.2 如果使用splice方法删除sort,当你就剩下最后一个的时候,会出现找不到那个元素的问题
  309. // state.pageData.index.splice(data.dataSort,1);
  310. // Message.success('模块已删除!');
  311. // console.log(state.pageData.index);
  312. }
  313. //pageStatus==2 分类页
  314. if(state.pageStatus == 2){}
  315. //pageStatus==3 列表页
  316. if(state.pageStatus == 3){}
  317. //pageStatus==4 详情页
  318. if(state.pageStatus == 4){}
  319. //pageStatus==5 搜索页
  320. if(state.pageStatus == 5){}
  321. //pageStatus==6 自定义列表页
  322. if(state.pageStatus == 6){}
  323. //pageStatus==7 自定义详情页
  324. if(state.pageStatus == 7){}
  325. },
  326. //设置组件样式
  327. setComponentStyleNumber(state,data){
  328. let id = state.editSectorId;
  329. let dataSort = state.editDataSort;
  330. let sort = state.editComponentSort;
  331. let num = state.editComponentStyle;
  332. console.log(id,dataSort,sort,num);
  333. //pageStatus==1 首页
  334. if(state.pageStatus == 1){
  335. // 获取当前模块数据并进行深拷贝
  336. let module = JSON.parse(JSON.stringify(state.pageData.index[dataSort]));
  337. //module.content.componentList[sort].component_style = num;
  338. //确保修改属性时 Vue 能监控到变化
  339. Vue.set(module.content.componentList, sort, {
  340. ...module.content.componentList[sort],
  341. component_style: num
  342. });
  343. // 拷贝后直接替换原来的板块
  344. //state.pageData.index[id] = module;
  345. // 使用Vue.set来强制视图更新
  346. Vue.set(state.pageData.index, dataSort, module);
  347. console.log("当前的板块数据为:");
  348. console.log(state.pageData.index);
  349. }
  350. //pageStatus==2 分类页
  351. if(state.pageStatus == 2){}
  352. //pageStatus==3 列表页
  353. if(state.pageStatus == 3){}
  354. //pageStatus==4 详情页
  355. if(state.pageStatus == 4){}
  356. //pageStatus==5 搜索页
  357. if(state.pageStatus == 5){}
  358. //pageStatus==6 自定义列表页
  359. if(state.pageStatus == 6){}
  360. //pageStatus==7 自定义详情页
  361. if(state.pageStatus == 7){}
  362. },
  363. //保存组件设置的数据
  364. saveComponentData(state,data){
  365. //传入的板块id
  366. console.log("当前编辑板块:" + data.id)
  367. //pageStatus==1 首页
  368. if(state.pageStatus == 1){
  369. const targetModuleIndex = state.pageData.index.findIndex(module => module.i === data.id);
  370. console.log("要修改的模块id为:") //找到要修改的板块
  371. console.log(data.sort) //找到要修改的板块
  372. console.log(targetModuleIndex) //找到要修改的板块
  373. //console.log(targetModuleIndex) //找到要修改的板块
  374. //state.pageData.index[targetModuleIndex].content.componentList[data.sort] //当前编辑的组件
  375. //data.data.pid_arr //导航池
  376. //state.editComponentSize //展示条数
  377. //判断数据的类型 1=普通新闻 2=tabs新闻选项卡 3=广告 4=友情链接 6=静态组件 7=文本 8=评论
  378. //1=普通新闻
  379. //注意,vue有一个bug就是,当你修改复杂对象的时候,如果对象的内部是数组
  380. //vue可能无法检测到变化,而当你修改无法被检测变化的数据时,就会把所有数据都修改成相同的值
  381. //解决办法是通过vue的set方法将整个对象,即你的数组外层那个对象进行深拷贝,然后修改完再替换回去
  382. if(state.editComponentType == 1){
  383. // 获取当前模块数据并进行深拷贝
  384. let module = JSON.parse(JSON.stringify(state.pageData.index[targetModuleIndex])); // 深拷贝整个模块
  385. // 更新 category_id 和 pageSize
  386. module.content.componentList[data.sort].componentData.category_id = data.data.pid_arr; // 设置 category_id
  387. module.content.componentList[data.sort].componentData.pageSize = state.editComponentSize; // 设置 pageSize
  388. // 使用Vue.set来强制视图更新
  389. Vue.set(state.pageData.index, targetModuleIndex, module); // 替换为深拷贝的模块
  390. console.log("当前的板块数据为:");
  391. console.log(state.pageData.index);
  392. }
  393. //2=tabs新闻选项卡
  394. if(state.editComponentType == 2){
  395. // 获取当前模块数据并进行深拷贝
  396. let module = JSON.parse(JSON.stringify(state.pageData.index[targetModuleIndex])); // 深拷贝整个模块
  397. // 更新 category_id 和 pageSize
  398. module.content.componentList[data.sort].componentData.category_id = data.data.pid_arr; // 设置 category_id
  399. module.content.componentList[data.sort].componentData.pageSize = state.editComponentSize; // 设置 pageSize
  400. // 使用Vue.set来强制视图更新
  401. Vue.set(state.pageData.index, targetModuleIndex, module); // 替换为深拷贝的模块
  402. console.log("当前的板块数据为:");
  403. console.log(state.pageData.index);
  404. }
  405. //3=广告
  406. if(state.editComponentType == 3){;
  407. let module = JSON.parse(JSON.stringify(state.pageData.index[targetModuleIndex]));
  408. module.content.componentList[data.sort].componentData.text = data.data.adName;
  409. Vue.set(state.pageData.index, targetModuleIndex, module);
  410. console.log("当前的板块数据为:");
  411. console.log(state.pageData.index);
  412. }
  413. //4=友情链接
  414. //if(state.editComponentType == 4){}
  415. //6=静态组件
  416. //if(state.editComponentType == 6){}
  417. //7=文本
  418. if(state.editComponentType == 7){
  419. let module = JSON.parse(JSON.stringify(state.pageData.index[targetModuleIndex]));
  420. module.content.componentList[data.sort].componentData.text = data.data.titleName;
  421. Vue.set(state.pageData.index, targetModuleIndex, module);
  422. console.log("当前的板块数据为:");
  423. console.log(state.pageData.index);
  424. }
  425. //8=评论
  426. //if(state.editComponentType == 8){}
  427. }
  428. //pageStatus==2 分类页
  429. if(state.pageStatus == 2){}
  430. //pageStatus==3 列表页
  431. if(state.pageStatus == 3){}
  432. //pageStatus==4 详情页
  433. if(state.pageStatus == 4){}
  434. //pageStatus==5 搜索页
  435. if(state.pageStatus == 5){}
  436. //pageStatus==6 自定义列表页
  437. if(state.pageStatus == 6){}
  438. //pageStatus==7 自定义详情页
  439. if(state.pageStatus == 7){}
  440. },
  441. //通过按钮移动板块 (暂时停用)
  442. moveModule(state, data) {
  443. Message.error("该功能暂时禁用")
  444. // pageStatus == 1 首页
  445. // if (state.pageStatus == 1) {
  446. // const moveType = data.moveType;
  447. // const moduleId = data.i;
  448. // const modules = state.pageData.index;
  449. // // 深拷贝 modules,避免直接操作原数据
  450. // const newModules = JSON.parse(JSON.stringify(modules));
  451. // // 查找模块在数组中的实际索引
  452. // const thisIndex = newModules.findIndex(module => module.i === moduleId);
  453. // if (thisIndex === -1) {
  454. // Message.error('模块未找到!');
  455. // return;
  456. // }
  457. // const thisY = newModules[thisIndex].y;
  458. // let thatY;
  459. // if (moveType === 'up') {
  460. // thatY = Math.max(
  461. // ...newModules.filter(item => item.y < thisY).map(item => item.y)
  462. // );
  463. // if (thatY === -Infinity) {
  464. // Message.error('已经是顶部了!');
  465. // return;
  466. // }
  467. // const thatIndex = newModules.findIndex(item => item.y === thatY);
  468. // if (thatIndex === -1) {
  469. // Message.error('上方模块未找到!');
  470. // return;
  471. // }
  472. // // 交换y值
  473. // [newModules[thisIndex].y, newModules[thatIndex].y] = [newModules[thatIndex].y, newModules[thisIndex].y];
  474. // } else if (moveType === 'down') {
  475. // thatY = Math.min(
  476. // ...newModules.filter(item => item.y > thisY).map(item => item.y)
  477. // );
  478. // if (thatY === Infinity) {
  479. // Message.error('已经是底部了!');
  480. // return;
  481. // }
  482. // const thatIndex = newModules.findIndex(item => item.y === thatY);
  483. // if (thatIndex === -1) {
  484. // Message.error('下方模块未找到!');
  485. // return;
  486. // }
  487. // // 交换y值
  488. // [newModules[thisIndex].y, newModules[thatIndex].y] = [newModules[thatIndex].y, newModules[thisIndex].y];
  489. // } else {
  490. // Message.error('未知的移动类型!');
  491. // return;
  492. // }
  493. // // 交换后对模块进行排序以确保顺序一致
  494. // newModules.sort((a, b) => a.y - b.y);
  495. // // 使用 Vue.set 强制更新数组,确保视图更新
  496. // //Vue.set(state.pageData, 'index', newModules); // 这里更新整个数组
  497. // state.pageData.index = newModules;
  498. // //state.gridKey+=1;
  499. // console.log(state.pageData.index); // 打印查看是否已修改
  500. // }
  501. // // pageStatus == 2 分类页
  502. // if (state.pageStatus == 2) {}
  503. // // pageStatus == 3 列表页
  504. // if (state.pageStatus == 3) {}
  505. // // pageStatus == 4 详情页
  506. // if (state.pageStatus == 4) {}
  507. // // pageStatus == 5 搜索页
  508. // if (state.pageStatus == 5) {}
  509. // // pageStatus == 6 自定义列表页
  510. // if (state.pageStatus == 6) {}
  511. // // pageStatus == 7 自定义详情页
  512. // if (state.pageStatus == 7) {}
  513. },
  514. //从外部拖拽板块
  515. drag(state, data) {
  516. // pageStatus == 1 首页
  517. if (state.pageStatus == 1) {
  518. let parentRect = document.getElementById('content').getBoundingClientRect();
  519. //console.log(parentRect); 获取画布
  520. let mouseInGrid = false;
  521. if (((state.mouseXY.x > parentRect.left) && (state.mouseXY.x < parentRect.right)) && ((state.mouseXY.y > parentRect.top) && (state.mouseXY.y < parentRect.bottom))) {
  522. mouseInGrid = true;
  523. }
  524. //state.gridlayoutObj
  525. //如果没有占位符就创建一个
  526. if (mouseInGrid === true && (state.pageData.index.findIndex(item => item.i === 'drop')) === -1) {
  527. state.pageData.index.push({
  528. x: (state.pageData.index.length * 2) % (this.colNum || 12),
  529. y: state.pageData.index.length + (this.colNum || 12), // puts it at the bottom
  530. w: 12,
  531. h: 2,
  532. i: 'drop',
  533. });
  534. }
  535. //如果已经存在占位符
  536. let index = state.pageData.index.findIndex(item => item.i === 'drop');
  537. if (index !== -1) {
  538. try {
  539. state.gridlayoutObj.$children[state.pageData.index.length].$refs.item.style.display = "none";
  540. } catch {
  541. }
  542. let el = state.gridlayoutObj.$children[index];
  543. el.dragging = {"top": state.mouseXY.y - parentRect.top, "left": state.mouseXY.x - parentRect.left};
  544. let new_pos = el.calcXY(state.mouseXY.y - parentRect.top, state.mouseXY.x - parentRect.left);
  545. if (mouseInGrid === true) {
  546. state.gridlayoutObj.dragEvent('dragstart', 'drop', new_pos.x, new_pos.y, 2, 12);
  547. state.DragPos.i = String(index);
  548. state.DragPos.x = state.pageData.index[index].x;
  549. state.DragPos.y = state.pageData.index[index].y;
  550. }
  551. if (mouseInGrid === false) {
  552. state.gridlayoutObj.dragEvent('dragend', 'drop', new_pos.x, new_pos.y, 2, 12);
  553. state.pageData.index = state.pageData.index.filter(obj => obj.i !== 'drop');
  554. }
  555. }
  556. }
  557. // pageStatus == 2 分类页
  558. if (state.pageStatus == 2) {}
  559. // pageStatus == 3 列表页
  560. if (state.pageStatus == 3) {}
  561. // pageStatus == 4 详情页
  562. if (state.pageStatus == 4) {}
  563. // pageStatus == 5 搜索页
  564. if (state.pageStatus == 5) {}
  565. // pageStatus == 6 自定义列表页
  566. if (state.pageStatus == 6) {}
  567. // pageStatus == 7 自定义详情页
  568. if (state.pageStatus == 7) {}
  569. },
  570. //拖拽结束 定位落点
  571. dragend(state, data) {
  572. // pageStatus == 1 首页
  573. if (state.pageStatus == 1) {
  574. //获取画布尺寸
  575. let parentRect = document.getElementById('content').getBoundingClientRect();
  576. //判断是否在画布内
  577. let mouseInGrid = false;
  578. if (((state.mouseXY.x > parentRect.left) && (state.mouseXY.x < parentRect.right)) && ((state.mouseXY.y > parentRect.top) && (state.mouseXY.y < parentRect.bottom))) {
  579. mouseInGrid = true;
  580. }
  581. //如果在画布内,开始创建元素
  582. if (mouseInGrid === true) {
  583. alert(`Dropped element props:\n${JSON.stringify(state.DragPos, ['x', 'y', 'w', 'h'], 2)}`);
  584. state.gridlayoutObj.dragEvent('dragend', 'drop', state.DragPos.x, state.DragPos.y, 1, 1);
  585. state.pageData.index = state.pageData.index.filter(obj => obj.i !== 'drop');
  586. // UNCOMMENT below if you want to add a grid-item
  587. /*
  588. this.layout.push({
  589. x: DragPos.x,
  590. y: DragPos.y,
  591. w: 1,
  592. h: 1,
  593. i: DragPos.i,
  594. });
  595. this.$refs.gridLayout.dragEvent('dragend', DragPos.i, DragPos.x,DragPos.y,1,1);
  596. try {
  597. this.$refs.gridLayout.$children[this.layout.length].$refs.item.style.display="block";
  598. } catch {
  599. }
  600. */
  601. let sendData = {
  602. source:"drag",//添加时判断此模块来自外部拖拽,而不是点击
  603. type:data.type,
  604. h:data.h,
  605. jsonData:data.jsonData,
  606. x: state.DragPos.x,
  607. y: state.DragPos.y,
  608. w: 12,
  609. }
  610. //开发到这里了,注意我可能还需要上面那个 this.$refs.gridLayout.dragEvent方法
  611. this.commit('template/addModule',sendData);
  612. }
  613. }
  614. // pageStatus == 2 分类页
  615. if (state.pageStatus == 2) {}
  616. // pageStatus == 3 列表页
  617. if (state.pageStatus == 3) {}
  618. // pageStatus == 4 详情页
  619. if (state.pageStatus == 4) {}
  620. // pageStatus == 5 搜索页
  621. if (state.pageStatus == 5) {}
  622. // pageStatus == 6 自定义列表页
  623. if (state.pageStatus == 6) {}
  624. // pageStatus == 7 自定义详情页
  625. if (state.pageStatus == 7) {}
  626. },
  627. //修改首页板块高度
  628. setModuleHeight(state,data){
  629. // pageStatus == 1 首页
  630. if (state.pageStatus == 1) {
  631. console.log(data);
  632. state.pageData.index[data.i].h = data.h;
  633. console.log(state.pageData.index[data.i]);
  634. }
  635. // pageStatus == 2 分类页
  636. if (state.pageStatus == 2) {}
  637. // pageStatus == 3 列表页
  638. if (state.pageStatus == 3) {}
  639. // pageStatus == 4 详情页
  640. if (state.pageStatus == 4) {}
  641. // pageStatus == 5 搜索页
  642. if (state.pageStatus == 5) {}
  643. // pageStatus == 6 自定义列表页
  644. if (state.pageStatus == 6) {}
  645. // pageStatus == 7 自定义详情页
  646. if (state.pageStatus == 7) {}
  647. },
  648. //格式化模板信息
  649. formatTemplateInfo(state, data) {
  650. // 深拷贝数据,避免直接修改原数据
  651. let clonedData = JSON.parse(JSON.stringify(data));
  652. // 按照 clonedData.data.y 的大小排序
  653. clonedData.data.sort((a, b) => a.y - b.y);
  654. //console.log(clonedData, clonedData);
  655. let websiteData = [];
  656. // 获取板块的 sort
  657. for (let index = 0; index < clonedData.data.length; index++) {
  658. console.log(clonedData.data[index].content);
  659. // 使用 Vue.set 来确保属性变更能被 Vue 追踪
  660. Vue.set(clonedData.data[index].content, 'sort', Number(index) + 1);
  661. if (data.type == "index") {
  662. // 不要把数据直接保存到 state.webSiteData.template 中,否则无法删除
  663. websiteData.push(clonedData.data[index].content);
  664. }
  665. }
  666. // 保存到对应的页面 json 中
  667. if (data.type == "index") {
  668. state.webSiteData.template.index = websiteData;
  669. }
  670. },
  671. //保存模板
  672. saveTemplate(state) {
  673. //临时展示保存的数据 后期移除
  674. state.editWebsiteTemplateJsonWindow = true;
  675. //调用mutations中的方法无需使用$store
  676. //格式化index的信息
  677. this.commit('template/formatTemplateInfo',{data:state.pageData.index,type:"index"});
  678. console.log(state.webSiteData);
  679. },
  680. //随机生成模板
  681. randomTemplate(state){
  682. console.log("随机生成模板");
  683. const randomIndex = Math.floor(Math.random() * 3); // Generates a random number between 1 and 2
  684. console.log(`随机生成的数字是: ${randomIndex}`);
  685. Message.warning("该功能研发中");
  686. // pageStatus == 1 首页
  687. if (state.pageStatus == 1) {}
  688. // pageStatus == 2 分类页
  689. if (state.pageStatus == 2) {}
  690. // pageStatus == 3 列表页
  691. if (state.pageStatus == 3) {}
  692. // pageStatus == 4 详情页
  693. if (state.pageStatus == 4) {}
  694. // pageStatus == 5 搜索页
  695. if (state.pageStatus == 5) {}
  696. // pageStatus == 6 自定义列表页
  697. if (state.pageStatus == 6) {}
  698. // pageStatus == 7 自定义详情页
  699. if (state.pageStatus == 7) {}
  700. },
  701. //1.配置模块 end------------------------------------------------------------>
  702. //2.获取站点信息 start------------------------------------------------------------>
  703. //获取站点详情
  704. setWebsiteInfo(state,data){
  705. state.webSiteInfo = data;
  706. },
  707. //获取站点导航池
  708. setGetSiteCategory(state,data) {
  709. state.webSiteMenu = data;
  710. },
  711. //设置地区
  712. setArea(state,data){
  713. //都没有的时候返回的是省
  714. if(data.province==undefined){
  715. state.area.economize = data;
  716. }
  717. //没有region的时候返回的是市
  718. if(data.province!=undefined&&data.city!=undefined&&data.region==undefined){
  719. state.area.market = data.city;
  720. }
  721. //有region的时候返回的是县
  722. if(data.province!=undefined&&data.city!=undefined&&data.region!=undefined){
  723. state.area.county = data.region;
  724. }
  725. },
  726. //获取全部导航
  727. setWebsiteCategory(state,data){
  728. let arrayData = []
  729. for(let item of data){
  730. let options = {
  731. key:item.id,
  732. label:item.name,
  733. value:item.id
  734. }
  735. arrayData.push(options);
  736. }
  737. state.editWebsiteCategory = arrayData;
  738. },
  739. //2.获取站点信息 end------------------------------------------------------------>
  740. }
  741. const actions = {
  742. //1.站点数据 start------------------------------------------------------------>
  743. //获取网站基本信息
  744. getSiteInfo({commit},data){
  745. return new Promise((resolve, reject) => {
  746. getSiteInfo(data).then(response => {
  747. commit('setWebsiteInfo', response.data);
  748. resolve(response)
  749. }).catch(error => {
  750. reject(error)
  751. })
  752. })
  753. },
  754. //获取网站导航池
  755. getSiteCategory({commit},data){
  756. return new Promise((resolve, reject) => {
  757. getSiteCategory(data).then(response => {
  758. commit('setGetSiteCategory', response.data);
  759. resolve(response)
  760. }).catch(error => {
  761. reject(error)
  762. })
  763. })
  764. },
  765. //设置行政区划
  766. selectWebsiteArea({commit},data){
  767. return new Promise((resolve, reject) => {
  768. selectWebsiteArea(data).then(response => {
  769. commit('setArea', response.data);
  770. resolve(response)
  771. }).catch(error => {
  772. reject(error)
  773. })
  774. })
  775. },
  776. //获取新闻列表
  777. getWebsiteArticlesList({commit},data){
  778. return new Promise((resolve, reject) => {
  779. getWebsiteArticlesList(data).then(response => {
  780. resolve(response)
  781. }).catch(error => {
  782. reject(error)
  783. })
  784. })
  785. },
  786. //获取新闻详情
  787. selectWebsiteArticleInfo({commit},data){
  788. return new Promise((resolve, reject) => {
  789. selectWebsiteArticleInfo(data).then(response => {
  790. resolve(response)
  791. }).catch(error => {
  792. reject(error)
  793. })
  794. })
  795. },
  796. //获取全部导航池
  797. getWebsiteCategory({commit},data){
  798. return new Promise((resolve, reject) => {
  799. getWebsiteCategory(data).then(response => {
  800. commit('setWebsiteCategory', response.data);
  801. resolve(response)
  802. }).catch(error => {
  803. reject(error)
  804. })
  805. })
  806. },
  807. //1.站点数据 end------------------------------------------------------------>
  808. }
  809. export default {
  810. namespaced: true,
  811. state,
  812. mutations,
  813. actions
  814. }