index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <template>
  2. <div class="h5-editor-container">
  3. <div class="h5-editor">
  4. </div>
  5. <el-row :gutter="24">
  6. <el-col :span="2" class="button-body">
  7. <div v-for="item in componentList">
  8. <p/>
  9. <div>
  10. {{ item.groupName }}
  11. </div>
  12. <el-button class="button-tag" @click="add(item)" v-for="item in item.comps">
  13. <!-- 添加图标和文字 -->
  14. <i :class="item.icon" style="margin-right: 5px"></i>
  15. {{ item.label }}
  16. </el-button>
  17. </div>
  18. </el-col>
  19. <el-col :span="12">
  20. <div class="parent-container">
  21. <div class="view-body">
  22. <draggable v-model="list" @end="end">
  23. <div v-for="(item, index) in list" :key="index" @click="select(index)" class="view-item">
  24. <component :is="item.type" :config="item"/>
  25. </div>
  26. </draggable>
  27. </div>
  28. </div>
  29. </el-col>
  30. <el-col :span="10" style="overflow-y: auto;height:80vh;">
  31. <form-wrapper
  32. :form="form"
  33. :index="index"
  34. :list="list"
  35. @update:form="updateForm"
  36. @delete="del"
  37. />
  38. </el-col>
  39. </el-row>
  40. </div>
  41. </template>
  42. <script>
  43. import draggable from 'vuedraggable'
  44. import H5Text from '@/components/H5/h5-text.vue'
  45. import H5Image from '@/components/H5/h5-image.vue'
  46. import H5Button from '@/components/H5/h5-button.vue'
  47. import H5Sep from '@/components/H5/h5-sep.vue'
  48. import H5Countdown from '@/components/H5/h5-countdown.vue'
  49. import FormWrapper from '@/components/H5/FormWrapper.vue'
  50. import H5Chat from '@/components/H5/h5-chat.vue'
  51. import AgentAvatar from '@/assets/images/customer.png?inline'
  52. export default {
  53. components: {
  54. draggable, // 对应模板中的<h5-text>
  55. H5Text, // 对应模板中的<h5-text>
  56. H5Button, // 对应模板中的<h5-text>
  57. H5Image,// 对应模板中的<h5-image>
  58. H5Sep,// 对应模板中的<h5-sep>
  59. H5Countdown,// 对应模板中的<h5-countdown>
  60. FormWrapper,
  61. H5Chat
  62. },
  63. mounted() {
  64. this.initData(this.json)
  65. },
  66. props: {
  67. json: {
  68. type: String,
  69. default: '[]'
  70. }
  71. },
  72. data() {
  73. return {
  74. componentList: [{
  75. groupName: '基础控件',
  76. comps: [
  77. {
  78. type: 'h5-text',
  79. label: '文本',
  80. icon: 'el-icon-document' // 文档图标更符合文本语义
  81. },
  82. {
  83. type: 'h5-image',
  84. label: '图片',
  85. icon: 'el-icon-picture' // 明确的图片图标
  86. },
  87. {
  88. type: 'h5-sep',
  89. label: '分割线',
  90. icon: 'el-icon-minus' // 减号图标模拟分割线效果
  91. }
  92. ]
  93. }, {
  94. groupName: '营销控件',
  95. comps: [
  96. {
  97. type: 'h5-countdown',
  98. label: '倒计时',
  99. icon: 'el-icon-timer' // 计时器图标表示倒计时
  100. },
  101. {
  102. type: 'h5-chat',
  103. label: '互动问答',
  104. icon: 'el-icon-chat-square' // 购物袋表示购买相关
  105. }
  106. ]
  107. }],
  108. list: [],
  109. index: 0,
  110. form: {}
  111. }
  112. },
  113. methods: {
  114. initData(json) {
  115. this.list = JSON.parse(json)
  116. },
  117. end() {
  118. },
  119. add(item) {
  120. let data = {
  121. type: item.type,
  122. active: false,
  123. name: item.label,
  124. fixe: false,
  125. classText: ['parent-div'],
  126. addWxFun: false,
  127. workUrl: ''
  128. }
  129. if (item.type === 'h5-text') {
  130. data.content = '默认文本'
  131. data.style = {
  132. textAlign: 'left',
  133. fontSize: 20,
  134. background: '#FFF',
  135. color: '#000',
  136. fontWeight: 'none',
  137. fontStyle: 'none',
  138. textDecoration: 'none'
  139. }
  140. } else if (item.type === 'h5-image') {
  141. data.url = null
  142. } else if (item.type === 'h5-button') {
  143. data.content = '默认文本'
  144. } else if (item.type === 'h5-sep') {
  145. data.classText = [...data.classText, 'divider']
  146. data.style = {
  147. height: '10px',
  148. background: 'rgb(228, 231, 237)'
  149. }
  150. } else if (item.type === 'h5-countdown') {
  151. data.days = 0
  152. data.hours = 0
  153. data.minutes = 0
  154. data.seconds = 0
  155. data.countdownMode = 1
  156. } else if (item.type === 'h5-chat') {
  157. data = {...data,
  158. ...{
  159. messages: [
  160. {
  161. id: 1,
  162. sender: "agent",
  163. text: "您好!欢迎报名【中老年健康养生大讲堂】,请仔细答一下问题,便于帮您分配专业的老师进行指导。",
  164. welcome: true
  165. },
  166. {
  167. id: 2,
  168. sender: "agent",
  169. text: "您的年龄?",
  170. options: [
  171. { id: 1, text: "45-55岁" },
  172. { id: 2, text: "55-60岁" },
  173. { id: 3, text: "60-65岁" },
  174. { id: 4, text: "65岁以上" }
  175. ],
  176. userSelection: null
  177. },
  178. {
  179. id: 3,
  180. sender: "user",
  181. text: '45-55岁',
  182. userSelection: { id: 1, text: "45-55岁" }
  183. },
  184. {
  185. id: 4,
  186. sender: "agent",
  187. text: "更想通过课程学习到哪些知识?",
  188. options: [
  189. { id: 1, text: "食疗食补" },
  190. { id: 2, text: "经络疏通" },
  191. { id: 3, text: "脏腑调养" },
  192. { id: 4, text: "启蒙养生" },
  193. { id: 5, text: "以上所有" }
  194. ],
  195. userSelection: null
  196. },
  197. {
  198. id: 5,
  199. sender: "user",
  200. text: '食疗食补',
  201. userSelection: { id: 1, text: "食疗食补" }
  202. }
  203. ],
  204. agentMsg:[{
  205. id: 1,
  206. sender: "agent",
  207. text: "您好!欢迎报名【中老年健康养生大讲堂】,请仔细答一下问题,便于帮您分配专业的老师进行指导。",
  208. welcome: true
  209. },
  210. {
  211. id: 2,
  212. sender: "agent",
  213. text: "您的年龄?",
  214. options: [
  215. { id: 1, text: "45-55岁" },
  216. { id: 2, text: "55-60岁" },
  217. { id: 3, text: "60-65岁" },
  218. { id: 4, text: "65岁以上" }
  219. ],
  220. userSelection: null
  221. },
  222. {
  223. id: 4,
  224. sender: "agent",
  225. text: "更想通过课程学习到哪些知识?",
  226. options: [
  227. { id: 1, text: "食疗食补" },
  228. { id: 2, text: "经络疏通" },
  229. { id: 3, text: "脏腑调养" },
  230. { id: 4, text: "启蒙养生" },
  231. { id: 5, text: "以上所有" }
  232. ],
  233. userSelection: null
  234. }
  235. ],
  236. style: {
  237. avatar: window.location.origin+AgentAvatar,
  238. buttonColor: '#409EFF',
  239. textColor: ''
  240. }
  241. }
  242. }
  243. console.log(data)
  244. let h5chat = this.list.some(item => item.type && item.type.includes('h5-chat'))
  245. if (h5chat) {
  246. alert('全局只能允许有一个互动问答!')
  247. return
  248. }
  249. }
  250. console.log(this.list)
  251. if (this.index !== null && this.index >= 0 && this.index < this.list.length) {
  252. this.list.splice(this.index + 1, 0, data)
  253. } else {
  254. this.list.push(data)
  255. }
  256. },
  257. select(index) {
  258. this.index = index
  259. let className = 'active'
  260. this.clearClass(className)
  261. this.addClass(className)
  262. this.$set(this.list[index], 'active', true) // 确保响应式更新
  263. // 直接绑定list中的对象(关键修改)
  264. this.form = this.list[index]
  265. },
  266. clearClass(classText) {
  267. this.list.forEach(item => {
  268. // 推荐使用 $set
  269. this.$set(item, 'classText', item.classText.filter(c => c !== classText))
  270. })
  271. },
  272. addClass(classText) {
  273. this.list[this.index].classText.push(classText)
  274. },
  275. del() {
  276. this.list.splice(this.index, 1)
  277. this.form = {}
  278. },
  279. updateForm(newForm) {
  280. // 更新表单数据
  281. if (this.index === undefined || !this.list[this.index]) {
  282. console.error('Invalid index or list item')
  283. return
  284. }
  285. console.log('newForm:', newForm) // 检查 newForm 数据
  286. Object.assign(this.form, newForm)
  287. // 更新列表中的数据
  288. for (const key in newForm) {
  289. if (Object.hasOwnProperty.call(newForm, key)) {
  290. // 如果是嵌套对象,递归更新
  291. if (typeof newForm[key] === 'object' && newForm[key] !== null) {
  292. for (const nestedKey in newForm[key]) {
  293. this.$set(this.list[this.index][key], nestedKey, newForm[key][nestedKey])
  294. }
  295. } else {
  296. this.$set(this.list[this.index], key, newForm[key])
  297. }
  298. }
  299. }
  300. console.log('Updated list:', this.list[this.index])
  301. }
  302. }
  303. }
  304. </script>
  305. <style lang="scss" scoped>
  306. .button-body {
  307. text-align: center;
  308. margin: 0 auto;
  309. .button-tag:first-child {
  310. margin-top: 0 !important;
  311. }
  312. .button-tag {
  313. margin-left: 0;
  314. margin-top: 10px;
  315. }
  316. }
  317. /* 外层容器 (父级div) */
  318. .parent-container {
  319. background: #eff3f5;
  320. padding: 40px 0;
  321. width: 100%;
  322. /* 关键设置 */
  323. height: 80vh;
  324. overflow-y: auto; /* 滚动条出在这里 */
  325. border: 1px solid #DCDEE2;
  326. }
  327. /* 定制滚动条样式 */
  328. .parent-container::-webkit-scrollbar {
  329. width: 6px; /* 滚动条宽度 */
  330. }
  331. .parent-container::-webkit-scrollbar-track {
  332. background: #f1f1f1; /* 滚动条轨道背景色 */
  333. border-radius: 3px; /* 轨道圆角 */
  334. }
  335. .parent-container::-webkit-scrollbar-thumb {
  336. background: #888; /* 滚动条滑块颜色 */
  337. border-radius: 3px; /* 滑块圆角 */
  338. }
  339. .parent-container::-webkit-scrollbar-thumb:hover {
  340. background: #555; /* 滑块悬停颜色 */
  341. }
  342. .view-body {
  343. width: 375px; /* 明确具体值(会覆盖下面的width:100%) */
  344. height: auto; /* 改掉height:100%,否则会根据父级高度计算*/
  345. /* 清除非必须设置 */
  346. overflow-y: visible; /* 保持默认 */
  347. /* 其他属性 */
  348. margin: 0 auto; /* 代替外层flex的justify-content */
  349. background: #fff;
  350. position: relative;
  351. padding: 0;
  352. }
  353. .icon.svg {
  354. cursor: pointer;
  355. margin-left: 20px;
  356. width: 20px;
  357. height: 20px;
  358. }
  359. .icon.svg.active {
  360. color: #02ff9b;
  361. }
  362. .icon-span {
  363. }
  364. .divider {
  365. width: 100%;
  366. background: #DCDEE2;
  367. height: 10px;
  368. }
  369. .button-tag {
  370. /* 新增图标样式 */
  371. .el-icon {
  372. margin-right: 6px; /* 图标与文本间距 */
  373. font-size: 16px; /* 统一图标大小 */
  374. vertical-align: -2px; /* 垂直居中补偿 */
  375. }
  376. &.is-plain .el-icon {
  377. color: #666; /* 浅色主题下保持可视性 */
  378. }
  379. }
  380. </style>