add-or-update.vue.ftl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <#assign editor=false/>
  2. <template>
  3. <el-dialog :visible.sync="visible" :title="!dataForm.${pk.attrName} ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
  4. <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
  5. <#list columnList as column>
  6. <#if column.form>
  7. <#if column.formType == 'text'>
  8. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  9. <el-input v-model="dataForm.${column.attrName}" placeholder="${column.comment!}"></el-input>
  10. </el-form-item>
  11. <#elseif column.formType == 'textarea'>
  12. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  13. <el-input type="textarea" v-model="dataForm.${column.attrName}"></el-input>
  14. </el-form-item>
  15. <#elseif column.formType == 'editor'>
  16. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  17. <!-- 富文本编辑器, 容器 -->
  18. <div id="J_quillEditor"></div>
  19. <!-- 自定义上传图片功能 (使用element upload组件) -->
  20. <el-upload
  21. :action="uploadUrl"
  22. :show-file-list="false"
  23. :before-upload="uploadBeforeUploadHandle"
  24. :on-success="uploadSuccessHandle"
  25. style="display: none;">
  26. <el-button ref="uploadBtn" type="primary" size="small">{{ $t('upload.button') }}</el-button>
  27. </el-upload>
  28. </el-form-item>
  29. <#assign editor=true/>
  30. <#assign editorName="${column.attrName}"/>
  31. <#elseif column.formType == 'select'>
  32. <#if column.dictName??>
  33. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  34. <ren-select v-model="dataForm.${column.attrName}" dict-type="${column.dictName}" placeholder="${column.comment!}"></ren-select>
  35. </el-form-item>
  36. <#else>
  37. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  38. <el-select v-model="dataForm.${column.attrName}" placeholder="请选择">
  39. <el-option label="智汇口腔" value="0"></el-option>
  40. </el-select>
  41. </el-form-item>
  42. </#if>
  43. <#elseif column.formType == 'radio'>
  44. <#if column.dictName??>
  45. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  46. <ren-radio-group v-model="dataForm.${column.attrName}" dict-type="${column.dictName}"></ren-radio-group>
  47. </el-form-item>
  48. <#else>
  49. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  50. <el-radio-group v-model="dataForm.${column.attrName}">
  51. <el-radio :label="0">启用</el-radio>
  52. <el-radio :label="1">禁用</el-radio>
  53. </el-radio-group>
  54. </el-form-item>
  55. </#if>
  56. <#elseif column.formType == 'checkbox'>
  57. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  58. <el-checkbox-group v-model="dataForm.${column.attrName}">
  59. <el-checkbox label="启用" name="type"></el-checkbox>
  60. <el-checkbox label="禁用" name="type"></el-checkbox>
  61. </el-checkbox-group>
  62. </el-form-item>
  63. <#elseif column.formType == 'date'>
  64. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  65. <el-date-picker type="date" placeholder="${column.comment!}" v-model="dataForm.${column.attrName}"></el-date-picker>
  66. </el-form-item>
  67. <#elseif column.formType == 'datetime'>
  68. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  69. <el-date-picker type="datetime" placeholder="${column.comment!}" v-model="dataForm.${column.attrName}"></el-date-picker>
  70. </el-form-item>
  71. <#else>
  72. <el-form-item label="${column.comment!}" prop="${column.attrName}">
  73. <el-input v-model="dataForm.${column.attrName}" placeholder="${column.comment!}"></el-input>
  74. </el-form-item>
  75. </#if>
  76. </#if>
  77. </#list>
  78. </el-form>
  79. <template slot="footer">
  80. <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
  81. <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
  82. </template>
  83. </el-dialog>
  84. </template>
  85. <script>
  86. import debounce from 'lodash/debounce'
  87. <#if editor>
  88. import Cookies from 'js-cookie'
  89. import 'quill/dist/quill.snow.css'
  90. import Quill from 'quill'
  91. </#if>
  92. export default {
  93. data () {
  94. return {
  95. visible: false,
  96. <#if editor>
  97. quillEditor: null,
  98. quillEditorToolbarOptions: [
  99. ['bold', 'italic', 'underline', 'strike'],
  100. ['blockquote', 'code-block', 'image'],
  101. [{ 'header': 1 }, { 'header': 2 }],
  102. [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  103. [{ 'script': 'sub' }, { 'script': 'super' }],
  104. [{ 'indent': '-1' }, { 'indent': '+1' }],
  105. [{ 'direction': 'rtl' }],
  106. [{ 'size': ['small', false, 'large', 'huge'] }],
  107. [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  108. [{ 'color': [] }, { 'background': [] }],
  109. [{ 'font': [] }],
  110. [{ 'align': [] }],
  111. ['clean']
  112. ],
  113. uploadUrl: '',
  114. </#if>
  115. dataForm: {
  116. <#list columnList as column>
  117. ${column.attrName}: '',
  118. </#list>
  119. }
  120. }
  121. },
  122. computed: {
  123. dataRule () {
  124. <#if editor>
  125. const validate${editorName} = (rule, value, callback) => {
  126. if (this.quillEditor.getLength() <= 1) {
  127. return callback(new Error(this.$t('validate.required')))
  128. }
  129. callback()
  130. }
  131. </#if>
  132. return {
  133. <#list columnList as column>
  134. <#if column.form && column.required>
  135. <#if editor && column.attrName == editorName>
  136. ${column.attrName}: [
  137. { required: true, message: this.$t('validate.required'), trigger: 'blur' },
  138. { validator: validate${editorName}, trigger: 'blur' }
  139. ],
  140. <#else>
  141. ${column.attrName}: [
  142. { required: true, message: this.$t('validate.required'), trigger: 'blur' }
  143. ],
  144. </#if>
  145. </#if>
  146. </#list>
  147. }
  148. }
  149. },
  150. methods: {
  151. init () {
  152. this.visible = true
  153. this.$nextTick(() => {
  154. <#if editor>
  155. if (this.quillEditor) {
  156. this.quillEditor.deleteText(0, this.quillEditor.getLength())
  157. } else {
  158. this.quillEditorHandle()
  159. }
  160. </#if>
  161. this.$refs['dataForm'].resetFields()
  162. if (this.dataForm.${pk.attrName}) {
  163. this.getInfo()
  164. }
  165. })
  166. },
  167. // 获取信息
  168. getInfo () {
  169. this.$http.get('/${moduleName}/${classname}/' + this.dataForm.${pk.attrName}).then(({ data: res }) => {
  170. if (res.code !== 0) {
  171. return this.$message.error(res.msg)
  172. }
  173. this.dataForm = {
  174. ...this.dataForm,
  175. ...res.data
  176. }
  177. <#if editor>
  178. this.quillEditor.root.innerHTML = this.dataForm.${editorName}
  179. </#if>
  180. }).catch(() => {})
  181. },
  182. <#if editor>
  183. // 富文本编辑器
  184. quillEditorHandle () {
  185. this.quillEditor = new Quill('#J_quillEditor', {
  186. modules: {
  187. toolbar: this.quillEditorToolbarOptions
  188. },
  189. theme: 'snow'
  190. })
  191. // 自定义上传图片功能 (使用element upload组件)
  192. this.uploadUrl = window.SITE_CONFIG['apiURL'] + '/sys/oss/upload?token=' + Cookies.get('token')
  193. this.quillEditor.getModule('toolbar').addHandler('image', () => {
  194. this.$refs.uploadBtn.$el.click()
  195. })
  196. // 监听内容变化,动态赋值
  197. this.quillEditor.on('text-change', () => {
  198. this.dataForm.${editorName} = this.quillEditor.root.innerHTML
  199. })
  200. },
  201. // 上传图片之前
  202. uploadBeforeUploadHandle (file) {
  203. if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
  204. this.$message.error('只支持jpg、png、gif格式的图片!')
  205. return false
  206. }
  207. },
  208. // 上传图片成功
  209. uploadSuccessHandle (res, file, fileList) {
  210. if (res.code !== 0) {
  211. return this.$message.error(res.msg)
  212. }
  213. this.quillEditor.insertEmbed(this.quillEditor.getSelection().index, 'image', res.data.src)
  214. },
  215. </#if>
  216. // 表单提交
  217. dataFormSubmitHandle: debounce(function () {
  218. this.$refs['dataForm'].validate((valid) => {
  219. if (!valid) {
  220. return false
  221. }
  222. this.$http[!this.dataForm.${pk.attrName} ? 'post' : 'put']('/${moduleName}/${classname}/', this.dataForm).then(({ data: res }) => {
  223. if (res.code !== 0) {
  224. return this.$message.error(res.msg)
  225. }
  226. this.$message({
  227. message: this.$t('prompt.success'),
  228. type: 'success',
  229. duration: 500,
  230. onClose: () => {
  231. this.visible = false
  232. this.$emit('refreshDataList')
  233. }
  234. })
  235. }).catch(() => {})
  236. })
  237. }, 1000, { 'leading': true, 'trailing': false })
  238. }
  239. }
  240. </script>