template.js 11 KB

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