canvasContainer.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div class="canvas">
  3. <div class="topBox">
  4. <!--<a class="btn-black">返回店铺</a>-->
  5. <ul>
  6. <li v-for="(item,index) in deviceList" :key="index" @click="toggleDevice(item.id)" :class="{'on':terminal == item.id}">
  7. <i class="iconfont" :class="'icon-' + item.name"></i>
  8. </li>
  9. </ul>
  10. <el-button class="btn-save" type="primary" @click="canvasSave">保存画布</el-button>
  11. </div>
  12. <div class="bottomWarp">
  13. <div class="leftBox">
  14. <left-bar></left-bar>
  15. </div>
  16. <div class="mainContentWarp">
  17. <div class="mainContent" :class="'view-' + terminal">
  18. <shop-layout :terminal="terminal" :typeId="typeId" :shopId="shopId" @showRightBox="showRightBox"></shop-layout>
  19. </div>
  20. </div>
  21. <div class="RightBox">
  22. <tool-panel v-if="comChoose"></tool-panel>
  23. <div v-else class="noChoose">
  24. <div> <i class="iconfont icon-kong"></i><p>没有选定的组件<br>请拖拽左侧组件栏添加或者选择一个组件</p></div>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import leftBar from '../components/leftBar/panel.vue'
  32. import toolPanel from '../components/toolBar/toolPanel'
  33. import ShopLayout from '../components/canvasEditPage'
  34. import { mapGetters, mapMutations } from 'vuex'
  35. import api from '@@/components/canvasShow/config/api'
  36. import {sendReqMixin} from '@@/components/canvasShow/config/mixin'
  37. import Cookies from 'js-cookie'
  38. export default {
  39. name: 'canvasContainer',
  40. mixins: [sendReqMixin],
  41. components: {
  42. ShopLayout,
  43. leftBar,
  44. toolPanel
  45. },
  46. data () {
  47. return {
  48. comChoose: false,
  49. deviceList: [{
  50. id: 1,
  51. name: 'xiaochengxu'
  52. }, {
  53. id: 2,
  54. name: 'h5'
  55. }, {
  56. id: 4,
  57. name: 'pc'
  58. }, {
  59. id: 3,
  60. name: 'app'
  61. }],
  62. canvasId: '',
  63. shopId: 0
  64. }
  65. },
  66. computed: {
  67. ...mapGetters([
  68. 'terminal',
  69. 'componentsData',
  70. 'typeId'
  71. ])
  72. },
  73. mounted () {
  74. this.shopId = parseInt(Cookies.get('shopID'))
  75. this.canvasGet()
  76. },
  77. methods: {
  78. ...mapMutations({
  79. setTerminal: 'SET_TERMINAL',
  80. setActiveComponent: 'SET_ACTIVECOMPONENT',
  81. setComponentsData: 'SET_COMPONENTSDATA'
  82. }),
  83. toggleDevice (id) {
  84. this.setActiveComponent({})
  85. this.setTerminal(id)
  86. this.canvasGet()
  87. },
  88. // 保存画布
  89. canvasSave () {
  90. // 删除非必要的字符
  91. let cloneComponentsData = JSON.parse(JSON.stringify(this.componentsData))
  92. for (let i = 0; i < cloneComponentsData.length; i++) {
  93. delete cloneComponentsData[i].iconClass
  94. if (cloneComponentsData[i].type === 'productList') {
  95. cloneComponentsData[i].componentContent.productData.imgTextData = [] // 清除展示数据
  96. }
  97. }
  98. console.log(cloneComponentsData);
  99. var paramsData = {
  100. terminal: this.terminal,
  101. json: JSON.stringify(cloneComponentsData)
  102. }
  103. if (this.canvasId) {
  104. paramsData.canvasId = this.canvasId
  105. }
  106. if (this.shopId && this.typeId == 3) {
  107. paramsData.shopId = this.shopId
  108. }
  109. paramsData.type = this.typeId
  110. console.log(paramsData)
  111. let params = {
  112. url: api.saveCanvas,
  113. method: 'POST',
  114. data: paramsData
  115. }
  116. this.sendReq(params, (res) => {
  117. if (res.message) {
  118. this.$message.error(res.message)
  119. } else {
  120. this.$message({
  121. message: '保存成功!',
  122. type: 'success'
  123. })
  124. }
  125. })
  126. },
  127. // 读取画布
  128. canvasGet () {
  129. var _this = this
  130. this.setComponentsData([])
  131. var apiUrl = api.getCanvas + '?terminal=' + this.terminal + '&type=' + this.typeId
  132. if (this.shopId && this.typeId == 3) {
  133. apiUrl += '&shopId=' + this.shopId
  134. }
  135. let params = {
  136. url: apiUrl,
  137. method: 'GET'
  138. }
  139. this.sendReq(params, (res) => {
  140. if(typeof(uni) !== 'undefined'){
  141. uni.setStorage({key: 'sendNum',data: 0});
  142. } else {
  143. localStorage.setItem('sendNum', 0)
  144. }
  145. if (JSON.stringify(res.data) !== '{}') {
  146. _this.canvasId = res.data.canvasId
  147. var componentsData = JSON.parse(res.data.json)
  148. for (let i = 0; i < componentsData.length; i++) {
  149. componentsData[i].componentContent.hasDatas = true
  150. }
  151. _this.setComponentsData(componentsData)
  152. } else {
  153. _this.canvasId = ''
  154. }
  155. },(err)=>{
  156. })
  157. },
  158. // 右侧工具栏显隐
  159. showRightBox (flag) {
  160. this.comChoose = flag
  161. }
  162. }
  163. }
  164. </script>
  165. <style lang="scss" scoped>
  166. .canvas {
  167. position: relative;
  168. display: flex;
  169. flex-direction: column;
  170. height: 100%;
  171. .topBox{
  172. height: 52px;
  173. line-height: 52px;
  174. border-bottom: 1px solid #F0F3F4;
  175. position: relative;
  176. display: flex;
  177. justify-content: center;
  178. .btn-black{
  179. position: absolute;
  180. left: 20px;
  181. top: 0;
  182. }
  183. li{
  184. width: 56px;
  185. height: 52px;
  186. cursor: pointer;
  187. text-align: center;
  188. display: inline-block;
  189. .iconfont{
  190. font-size: 24px;
  191. }
  192. &:hover,&.on{
  193. background-color: $mainColor;
  194. color: #fff;
  195. }
  196. }
  197. .btn-save{
  198. position: absolute;
  199. right: 20px;
  200. top: 5px;
  201. }
  202. }
  203. .bottomWarp{
  204. flex: 1;
  205. display: flex;
  206. height: 0;
  207. }
  208. .leftBox {
  209. height: 100%;
  210. overflow-y: auto;
  211. overflow-x: hidden;
  212. }
  213. .mainContentWarp{
  214. background-color: #F0F3F4;
  215. overflow: auto;
  216. height: 100%;
  217. flex: 1;
  218. .mainContent{
  219. margin: 0 auto;
  220. max-width: 100%;
  221. width: 750px;
  222. &.view-4{
  223. width: 1300px;
  224. }
  225. }
  226. }
  227. .RightBox {
  228. height: 100%;
  229. overflow: auto;
  230. .noChoose{
  231. width: 320px;
  232. display: flex;
  233. height: 100%;
  234. -webkit-box-align: center;
  235. align-items: center;
  236. -webkit-box-pack: center;
  237. justify-content: center;
  238. color: #999;
  239. text-align: center;
  240. font-size: 16px;
  241. line-height: 1.8;
  242. .iconfont{
  243. font-size: 100px;
  244. color: $mainColor;
  245. }
  246. }
  247. }
  248. }
  249. </style>