index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <wrapper
  3. fill
  4. margin
  5. padding
  6. background
  7. >
  8. <schema-table
  9. ref="table"
  10. :schema="schema"
  11. />
  12. <confirm-dialog
  13. ref="editDialog"
  14. :title="dialogTitle"
  15. @confirm="onSave"
  16. >
  17. <div class="c-grid-form u-align-self--center">
  18. <span class="c-grid-form__label u-required">名称</span>
  19. <el-input
  20. v-model.trim="template.templateName"
  21. placeholder="最多30个字符"
  22. maxlength="30"
  23. clearable
  24. />
  25. <span class="c-grid-form__label u-required">内容</span>
  26. <el-input
  27. ref="textarea"
  28. v-model.trim="template.templateContent"
  29. type="textarea"
  30. :rows="3"
  31. />
  32. <div class="c-grid-form__row">
  33. <button
  34. class="o-button"
  35. @click="addPlaceholder"
  36. >
  37. <i class="o-button__icon el-icon-circle-plus-outline" />
  38. 新增占位符
  39. </button>
  40. </div>
  41. <div
  42. v-for="(value, key) in template.keywordMap"
  43. :key="key"
  44. class="c-grid-form__row"
  45. >
  46. <span class="c-grid-form__label u-required c-grid-form__text">{{ "{" + key + "}" }}名称</span>
  47. <el-input
  48. v-model.trim="value.keywordName"
  49. placeholder="最多30个字符"
  50. maxlength="30"
  51. clearable
  52. >
  53. <template #append>
  54. <i
  55. class="c-sibling-item o-icon el-icon-plus has-active"
  56. @click="onAddPlaceholder(key)"
  57. />
  58. <i
  59. class="c-sibling-item o-icon el-icon-delete has-active"
  60. @click="onDelPlaceholder(key)"
  61. />
  62. </template>
  63. </el-input>
  64. </div>
  65. </div>
  66. </confirm-dialog>
  67. </wrapper>
  68. </template>
  69. <script>
  70. import {
  71. getBroadcastTemplates,
  72. addBroadcastTemplate,
  73. updateBroadcastTemplate,
  74. delBroadcastTemplate,
  75. getBroadcastTemplate
  76. } from '../api'
  77. export default {
  78. name: 'BroadcastTemplate',
  79. data () {
  80. return {
  81. schema: {
  82. list: getBroadcastTemplates,
  83. buttons: [{ type: 'add', on: this.onAdd }],
  84. filters: [
  85. { key: 'templateName', type: 'search', placeholder: '模板名称' }
  86. ],
  87. cols: [
  88. { prop: 'templateName', label: '模板名称' },
  89. { prop: 'createTime', label: '创建时间' },
  90. { type: 'invoke', render: [
  91. { label: '编辑', on: this.onEdit },
  92. { label: '删除', on: this.onDel }
  93. ] }
  94. ]
  95. },
  96. template: {}
  97. }
  98. },
  99. computed: {
  100. dialogTitle () {
  101. return this.template.templateId ? '编辑模板' : '新增模板'
  102. }
  103. },
  104. methods: {
  105. getCursorIndex () {
  106. return this.$refs.textarea.$el.children[0].selectionStart || this.template.templateContent.length
  107. },
  108. addTemplateContent (content) {
  109. const arr = this.template.templateContent.split('')
  110. arr.splice(this.getCursorIndex(), 0, content)
  111. this.template.templateContent = arr.join('')
  112. },
  113. onAddPlaceholder (key) {
  114. this.addTemplateContent(`{${key}}`)
  115. },
  116. onDelPlaceholder (delKey) {
  117. this.template.optionalNumbers -= 1
  118. const max = this.template.optionalNumbers
  119. this.template.templateContent = this.template.templateContent.replace(
  120. new RegExp(`\\{${delKey}\\}`, 'g'),
  121. ''
  122. )
  123. for (let i = delKey + 1; i <= max; i++) {
  124. this.template.templateContent = this.template.templateContent.replace(
  125. new RegExp(`\\{${i}\\}`, 'g'),
  126. `{${i - 1}}`
  127. )
  128. }
  129. this.template.keywordMap.splice(delKey, 1)
  130. },
  131. addPlaceholder () {
  132. this.template.keywordMap.push({
  133. type: 0,
  134. keywordName: ''
  135. })
  136. this.addTemplateContent(`{${this.template.optionalNumbers}}`)
  137. this.template.optionalNumbers += 1
  138. },
  139. onAdd () {
  140. this.template = {
  141. type: 0,
  142. templateName: '',
  143. templateContent: '',
  144. optionalNumbers: 0,
  145. keywordMap: []
  146. }
  147. this.$refs.editDialog.show()
  148. },
  149. onEdit ({ templateId }) {
  150. getBroadcastTemplate(templateId).then(({ data }) => {
  151. const { templateId, type, templateName, templateContent, keywordMap, optionalNumbers } = data
  152. const arr = []
  153. for (let i = 0; i < optionalNumbers; i++) {
  154. arr.push(keywordMap[i])
  155. }
  156. this.template = {
  157. templateId, templateName, templateContent, type, optionalNumbers,
  158. keywordMap: arr
  159. }
  160. this.$refs.editDialog.show()
  161. })
  162. },
  163. onSave (done) {
  164. const { templateId, templateName, templateContent, keywordMap, type, optionalNumbers } = this.template
  165. if (!templateName) {
  166. this.$message({
  167. type: 'warning',
  168. message: '请填写模板名称'
  169. })
  170. return
  171. }
  172. if (!templateContent) {
  173. this.$message({
  174. type: 'warning',
  175. message: '请填写模板内容'
  176. })
  177. return
  178. }
  179. const map = {}
  180. for (let i = 0; i < optionalNumbers; i++) {
  181. const keyword = keywordMap[i]
  182. if (!keyword.keywordName) {
  183. this.$message({
  184. type: 'warning',
  185. message: `请填写占位符{${i}}名称`
  186. })
  187. return
  188. }
  189. if (!new RegExp(`\\{${i}\\}`).test(templateContent)) {
  190. this.$message({
  191. type: 'warning',
  192. message: `模板内容缺少占位符{${i}}`
  193. })
  194. return
  195. }
  196. map[i] = keyword
  197. }
  198. if (templateId) {
  199. updateBroadcastTemplate({
  200. templateId,
  201. type,
  202. templateName,
  203. templateContent,
  204. optionalNumbers,
  205. keywordMap: map
  206. }).then(() => {
  207. done()
  208. this.$refs.table.pageTo()
  209. })
  210. } else {
  211. addBroadcastTemplate({
  212. type,
  213. templateName,
  214. templateContent,
  215. optionalNumbers,
  216. keywordMap: map
  217. }).then(() => {
  218. done()
  219. this.$refs.table.resetCondition({ templateName })
  220. })
  221. }
  222. },
  223. onDel ({ templateId, templateName }) {
  224. this.$confirm(
  225. `删除模板${templateName}?`,
  226. { type: 'warning' }
  227. ).then(() => {
  228. delBroadcastTemplate(templateId).then(() => {
  229. this.$message({
  230. type: 'success',
  231. message: '删除成功'
  232. })
  233. this.$refs.table.decrease(1)
  234. })
  235. })
  236. }
  237. }
  238. }
  239. </script>