template.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import {getSiteInfo,getSiteCategory,selectWebsiteDepartment,selectWebsiteArea,getWebsiteArticlesList,
  2. selectWebsiteArticleInfo} from '@/api/cms'
  3. import { Message } from 'element-ui'; //注意在这里引入是非常不符合规范的
  4. const state = {
  5. //0.全局配置 start------------------------------------------------------------>
  6. editWebsiteId: "",//当前编辑网站的id
  7. stepStatus: true,//是否显示现在进行到哪一步:true=显示 false=不显示
  8. //0.全局配置 end------------------------------------------------------------>
  9. //1.画布数据 start------------------------------------------------------------>
  10. pageStatus:1,//当前编辑哪个页面 1=首页 2=分类页 3=列表页 4=详情页 5=搜索页 6=自定义列表页 7=自定义详情页
  11. menuType:1, //当前菜单显示板块还是组件 1=板块 2=组件
  12. pageData: { //自助建站拖拽板块的数据,注意,这里并不是提交到后台的数据
  13. index:[
  14. ],//首页
  15. class:[],//分类页
  16. list:[],//列表页
  17. article:[],//详情页
  18. search:[],//搜索页
  19. aloneList:[],//自定义列表页
  20. aloneArticle:[],//自定义详情页
  21. },
  22. // layout: [
  23. // // i = id
  24. // // w = 最大宽度是12
  25. // // { i: "0", x: 0, y: 0, w: 12, h: 2, content:""},
  26. // // { i: "1", x: 0, y: 0, w: 12, h: 2, content:""},
  27. // ],
  28. //1.画布数据 end------------------------------------------------------------>
  29. //2.站点数据 start------------------------------------------------------------>
  30. webSiteInfo:"",//网站信息
  31. webSiteMenu:"",//网站包含的导航池
  32. area:{//地区
  33. economize:[],//省区
  34. market:[],//市区
  35. county:[]//县区
  36. },
  37. departmentList:"",//职能部门
  38. //2.站点数据 end------------------------------------------------------------>
  39. //3.网站数据 start------------------------------------------------------------>
  40. editWindowStatus:false,//编辑弹出框是否显示
  41. editWindowTitle:"",//编辑弹出框标题
  42. editSectorId:0,//当前正在编辑的板块id
  43. editComponentSort:0,//当前正在编辑的组件id
  44. editComponentType:0,//当前正在编辑的组件类型
  45. editComponentSize:0,//当前组件展示的条数
  46. webSiteData:{
  47. //1.base网站基本信息
  48. base:{
  49. websiteId:1,//网站id
  50. },
  51. //2.style信息
  52. style:{
  53. styleId:1,//风格id
  54. },
  55. //3.板块信息 header,menu,footer 是页面自带的无需构建
  56. template:{
  57. //index = 首页 class=分类页 list=列表页 article=详情页 search=搜索页 aloneList=自定义列表页 aloneArticle=自定义详情页
  58. index:[],
  59. class:[],//分类页
  60. list:[],//列表页
  61. article:[],//详情页
  62. search:[],//搜索页
  63. aloneList:[],//自定义列表页
  64. aloneArticle:[],//自定义详情页
  65. }
  66. }
  67. //3.网站数据 end------------------------------------------------------------>
  68. }
  69. const mutations = {
  70. //0.全局配置 start------------------------------------------------------------>
  71. //设置创建网站的id
  72. setEditWebsiteId(state,id){
  73. state.editWebsiteId = id;
  74. },
  75. //展示步骤
  76. showStepStatus(state){
  77. state.stepStatus = true;
  78. },
  79. //隐藏步骤
  80. hiddenStepStatus(state){
  81. state.stepStatus = false;
  82. },
  83. setPageStatus(state,num){
  84. state.pageStatus = num;
  85. },
  86. //打开编辑弹出框
  87. setEditWindowStatus(state,data){
  88. console.log(data);
  89. state.editWindowStatus = true;
  90. state.editSectorId = data.id;
  91. state.editComponentSort = data.sort;
  92. state.editComponentType = data.type;
  93. state.editComponentSize = data.size;
  94. },
  95. //关闭编辑弹出框
  96. closeEditWindowStatus(state){
  97. state.editWindowStatus = false;
  98. state.editSectorId = "";
  99. state.editComponentSort = "";
  100. state.editComponentType = "";
  101. state.editComponentSize = "";
  102. },
  103. //0.全局配置 start------------------------------------------------------------>
  104. //1.配置模块 start------------------------------------------------------------>
  105. //添加首页板块
  106. addIndexModule(state,data){
  107. //判断当前一共有多少个模块最多能添加10个
  108. if(state.pageData.index.length >= 10){
  109. Message.error('最多只能添加10个模块!');
  110. return;
  111. }else{
  112. //data.type 组件名称 data.h 模块高度
  113. //计算当前布局的最大 y 值
  114. const maxY = Math.max(...state.pageData.index.map(item => item.y), 0);
  115. //添加板块id
  116. state.pageData.index.push({
  117. i: state.pageData.index.length,
  118. x: 0,
  119. y: maxY + 1,
  120. w: 12,
  121. h: data.h,
  122. type: data.type,
  123. content:"123",
  124. sectorData:data.jsonData
  125. });
  126. console.log(state.pageData.index.length);
  127. data.jsonData.sectorId = state.pageData.index.length;
  128. console.log(data.jsonData);
  129. }
  130. },
  131. //删除首页板块
  132. deleteIndexModule(state,data){
  133. for(let i = 0; i < state.pageData.index.length; i++) {
  134. if(state.pageData.index[i].i == data.i) {
  135. state.pageData.index.splice(i, 1);
  136. }
  137. }
  138. Message.success('模块已删除!');
  139. },
  140. //修改首页板块高度
  141. setIndexModuleHeight(state,data){
  142. console.log(data);
  143. state.pageData.index[data.i].h = data.h;
  144. console.log(state.pageData.index[data.i]);
  145. },
  146. moveIndexModule(state, data) {
  147. const moveType = data.moveType;
  148. const moduleId = data.i;
  149. const modules = state.pageData.index;
  150. // 查找模块在数组中的实际索引
  151. const thisIndex = modules.findIndex(module => module.i === moduleId);
  152. if (thisIndex === -1) {
  153. Message.error('模块未找到!');
  154. return;
  155. }
  156. const thisY = modules[thisIndex].y;
  157. if (moveType === 'up') {
  158. // 查找比当前模块 y 值小的最大的 y 值
  159. const thatY = Math.max(
  160. ...modules
  161. .filter(item => item.y < thisY)
  162. .map(item => item.y)
  163. );
  164. if (thatY === -Infinity) {
  165. Message.error('已经是顶部了!');
  166. return;
  167. }
  168. // 查找具有 thatY 的模块索引
  169. const thatIndex = modules.findIndex(item => item.y === thatY);
  170. if (thatIndex === -1) {
  171. Message.error('上方模块未找到!');
  172. return;
  173. }
  174. // 交换 y 值
  175. [modules[thisIndex].y, modules[thatIndex].y] = [modules[thatIndex].y, modules[thisIndex].y];
  176. } else if (moveType === 'down') {
  177. // 查找比当前模块 y 值大的最小的 y 值
  178. const thatY = Math.min(
  179. ...modules
  180. .filter(item => item.y > thisY)
  181. .map(item => item.y)
  182. );
  183. if (thatY === Infinity) {
  184. Message.error('已经是底部了!');
  185. return;
  186. }
  187. // 查找具有 thatY 的模块索引
  188. const thatIndex = modules.findIndex(item => item.y === thatY);
  189. if (thatIndex === -1) {
  190. Message.error('下方模块未找到!');
  191. return;
  192. }
  193. // 交换 y 值
  194. [modules[thisIndex].y, modules[thatIndex].y] = [modules[thatIndex].y, modules[thisIndex].y];
  195. } else {
  196. Message.error('未知的移动类型!');
  197. return;
  198. }
  199. // 交换后对模块进行排序以确保顺序一致
  200. state.pageData.index.sort((a, b) => a.y - b.y);
  201. },
  202. //添加模块
  203. // addModule(state,data) {
  204. // //data.type 组件名称 data.h 模块高度
  205. // // 计算当前布局的最大 y 值
  206. // const maxY = Math.max(...state.layout.map(item => item.y), 0);
  207. // state.layout.push({
  208. // i: state.layout.length,
  209. // x: 0,
  210. // y: maxY + 1,
  211. // w: 12,
  212. // h: data.h,
  213. // type: data.type,
  214. // content:""
  215. // });
  216. // },
  217. //删除模块
  218. // deleteModule(item) {
  219. // //找到对应的模块删除掉
  220. // for(let i = 0; i < state.layout.length; i++) {
  221. // if(state.layout[i].i == item.i) {
  222. // state.layout.splice(i, 1);
  223. // }
  224. // }
  225. // },
  226. //移动模块
  227. // moveIndexModule(state,data){
  228. // console.log(state.pageData.index[data.i]);
  229. // if(data.moveType == 'up'){
  230. // }else{
  231. // if(state.pageData.index[data.i].y === Math.max(...state.pageData.index.map(item => item.y))){
  232. // Message.error('已经是底部了!');
  233. // }else{
  234. // console.log(state.pageData.index);
  235. // //当前选择的模块
  236. // let thisIndex = data.i;
  237. // let thisY = state.pageData.index[thisIndex].y;
  238. // //找到下方的模块的y值
  239. // //注意,这里不能用i去定位,因为模块是通过y值去排序的,跟数据的位置,i的位置都没有关系
  240. // const thatY = Math.min(...state.pageData.index
  241. // .filter(item => item.y > thisY)
  242. // .map(item => item.y),
  243. // Infinity);
  244. // //再通过y值找到对应的模块i
  245. // const thatIndex = state.pageData.index.findIndex(item => item.y === thatY);
  246. // //最后交换位置
  247. // state.pageData.index[thisIndex].y = thatY;
  248. // state.pageData.index[thatIndex].y = thisY;
  249. // }
  250. // }
  251. // },
  252. //格式化模板信息
  253. formatTemplateInfo(state, data) {
  254. //console.log(data.type);
  255. //按照data.data.y的大小排序
  256. data.data.sort((a, b) => a.y - b.y);
  257. //获取板块的sort
  258. for (let index in data.data) {
  259. console.log(data.data[index].sectorData);
  260. data.data[index].sectorData.sort = Number(index)+1;
  261. if(data.type=="index"){
  262. state.webSiteData.template.index.push(data.data[index].sectorData)
  263. }
  264. }
  265. },
  266. //保存模板
  267. saveTemplate(state) {
  268. //调用mutations中的方法无需使用$store
  269. //格式化index的信息
  270. this.commit('template/formatTemplateInfo',{data:state.pageData.index,type:"index"});
  271. console.log(state.webSiteData);
  272. },
  273. //1.配置模块 end------------------------------------------------------------>
  274. //2.获取站点信息 start------------------------------------------------------------>
  275. //获取站点详情
  276. setWebsiteInfo(state,data){
  277. state.webSiteInfo = data;
  278. },
  279. //获取站点导航池
  280. setGetSiteCategory(state,data) {
  281. state.webSiteMenu = data;
  282. },
  283. //设置地区
  284. setArea(state,data){
  285. //都没有的时候返回的是省
  286. if(data.province==undefined){
  287. state.area.economize = data;
  288. }
  289. //没有region的时候返回的是市
  290. if(data.province!=undefined&&data.city!=undefined&&data.region==undefined){
  291. state.area.market = data.city;
  292. }
  293. //有region的时候返回的是县
  294. if(data.province!=undefined&&data.city!=undefined&&data.region!=undefined){
  295. state.area.county = data.region;
  296. }
  297. },
  298. //2.获取站点信息 end------------------------------------------------------------>
  299. }
  300. const actions = {
  301. //1.站点数据 start------------------------------------------------------------>
  302. //获取网站基本信息
  303. getSiteInfo({commit},data){
  304. return new Promise((resolve, reject) => {
  305. getSiteInfo(data).then(response => {
  306. commit('setWebsiteInfo', response.data);
  307. resolve(response)
  308. }).catch(error => {
  309. reject(error)
  310. })
  311. })
  312. },
  313. //获取网站导航池
  314. getSiteCategory({commit},data){
  315. return new Promise((resolve, reject) => {
  316. getSiteCategory(data).then(response => {
  317. commit('setGetSiteCategory', response.data);
  318. resolve(response)
  319. }).catch(error => {
  320. reject(error)
  321. })
  322. })
  323. },
  324. //设置行政区划
  325. selectWebsiteArea({commit},data){
  326. return new Promise((resolve, reject) => {
  327. selectWebsiteArea(data).then(response => {
  328. commit('setArea', response.data);
  329. resolve(response)
  330. }).catch(error => {
  331. reject(error)
  332. })
  333. })
  334. },
  335. //获取新闻列表
  336. getWebsiteArticlesList({commit},data){
  337. return new Promise((resolve, reject) => {
  338. getWebsiteArticlesList(data).then(response => {
  339. resolve(response)
  340. }).catch(error => {
  341. reject(error)
  342. })
  343. })
  344. },
  345. //获取新闻详情
  346. selectWebsiteArticleInfo({commit},data){
  347. return new Promise((resolve, reject) => {
  348. selectWebsiteArticleInfo(data).then(response => {
  349. resolve(response)
  350. }).catch(error => {
  351. reject(error)
  352. })
  353. })
  354. }
  355. //1.站点数据 end------------------------------------------------------------>
  356. }
  357. export default {
  358. namespaced: true,
  359. state,
  360. mutations,
  361. actions
  362. }