fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<template>
  <div>
    <hot-area-box @handle-preview="handlePreview">
      <template slot="hotContent">
        <set-up :diyDes="diyDes"></set-up>
      </template>
    </hot-area-box>
    <preview ref="preview"></preview>
  </div>
</template>
<script>
import setUp from './set-up/index.vue'
import { mapState, mapMutations } from 'vuex'
import { getDiyInfoById, editDiy, addDiy } from '@/api/diy.js'
import preview from './components/preview.vue'
import { useSceneMap } from './commonData'
import { submitValidate } from '@/utils/submitValidate'
import hotAreaBox from '../../components/hotAreaBox/index.vue'
export default {
  name: 'diy-index',
  components: {
    setUp,
    preview,
    hotAreaBox
  },
  data() {
    return {
      useSceneMap,
      diyDes: null,
      resCode: null
    }
  },
  computed: {
    ...mapState(['diyList'])
  },
  methods: {
    ...mapMutations(['initDiyList', 'resetDiy']),
    // 设置diy主题
    setDiyDes() {
      this.$prompt('请输入主题名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.{1,20}$/,
        inputErrorMessage: '主题名称不能为空,且不超过20字符'
      }).then(({ value }) => {
        if (this.resCode === '500') {
          this.addDiy(value)
        } else {
          this.diyDes = value
        }
      }).catch(() => {
        this.$router.push(`${this.$baseRouter}/diy/list`)
      })
    },
    async _getDiyInfoById(params) {
      const res = await getDiyInfoById(params)
      if (res.code === '0' && res.data) {
        this.initDiyList(JSON.parse(res.data.styleInfo))
      }
    },
    async handlePreview() {
      if (submitValidate(this.diyList, this.useSceneMap)) return // 表单验证
      const id = this.$route.query.id
      let res
      if (id) {
        res = await editDiy({
          id,
          styleInfo: JSON.stringify(this.diyList),
          diyType: '1'
        })
        if (res.code === '0') {
          const str = `${process.env.VUE_APP_H5_URL}/index2?id=${res.data.id}`
          this.$refs.preview.show(str)
        }
      } else {
        this.addDiy(this.diyDes)
      }
    },
    /**
     * 新增diy
     */
    async addDiy(diyDes) {
      const res = await addDiy({
        diyDes,
        styleInfo: JSON.stringify(this.diyList),
        diyType: '1'
      })
      if (res.code === '0') {
        this.$router.replace(`${this.$baseRouter}/diy/update?id=${res.data.id}`)
        const str = `${process.env.VUE_APP_H5_URL}/index2?id=${res.data.id}`
        this.$refs.preview.show(str)
      } else if (!this.$route.query.id && res.code !== '0') {
        this.resCode = '500'
        setTimeout(() => {
          this.setDiyDes()
        }, 1000)
      }
    }
  },
  created() {
    if (this.$route.query.id) {
      this._getDiyInfoById({ id: this.$route.query.id })
    } else {
      this.setDiyDes()// 设置diy主题
    }
  },
  destroyed() {
    this.resetDiy()
  }
}
</script>
<style scoped lang="scss">
/* 设置滚动条的样式 */
.diy-aside::-webkit-scrollbar {
  display: none;
  width: 0;
}
 
.diy-aside {
  overflow: hidden;
  border: 1px solid #333;
  height: 667px;
  overflow-y: auto;
  -ms-overflow-style:none;
  overflow:-moz-scrollbars-none;
  scrollbar-width: none;
}
.el_aside {
  text-align: center;
}
.preview_btn {
  margin-top: 10px;
}
</style>