ParamDialog.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <el-dialog :title="title" :visible.sync="visible" width="520px" class="param-dialog-wrapper" :append-to-body="true"
  3. :close-on-click-modal="false" :key="dialogKey" custom-class="custom-param-dialog" :show-close="false">
  4. <div class="dialog-container">
  5. <div class="dialog-header">
  6. <h2 class="dialog-title">{{ title }}</h2>
  7. <button class="custom-close-btn" @click="cancel">
  8. <svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
  9. <path d="M13 1L1 13M1 1L13 13" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
  10. </svg>
  11. </button>
  12. </div>
  13. <el-form :model="form" :rules="rules" ref="form" label-width="auto" label-position="left" class="param-form">
  14. <el-form-item :label="$t('paramDialog.paramCode')" prop="paramCode" class="form-item">
  15. <el-input v-model="form.paramCode" :placeholder="$t('paramDialog.paramCodePlaceholder')"
  16. class="custom-input"></el-input>
  17. </el-form-item>
  18. <el-form-item :label="$t('paramDialog.valueType')" prop="valueType" class="form-item">
  19. <el-select
  20. v-model="form.valueType"
  21. :placeholder="$t('paramDialog.valueTypePlaceholder')"
  22. class="custom-select"
  23. >
  24. <el-option
  25. v-for="item in valueTypeOptions"
  26. :key="item.value"
  27. :label="$t(`paramDialog.${item.value}Type`)"
  28. :value="item.value"
  29. />
  30. </el-select>
  31. </el-form-item>
  32. <el-form-item :label="$t('paramDialog.paramValue')" prop="paramValue" class="form-item">
  33. <el-input
  34. v-if="form.valueType !== 'json' && form.valueType !== 'array'"
  35. v-model="form.paramValue"
  36. :placeholder="$t('paramDialog.paramValuePlaceholder')"
  37. class="custom-input"
  38. ></el-input>
  39. <el-input
  40. v-else
  41. type="textarea"
  42. v-model="form.paramValue"
  43. :placeholder="$t('paramDialog.paramValuePlaceholder')"
  44. :rows="6"
  45. class="custom-textarea"
  46. ></el-input>
  47. </el-form-item>
  48. <el-form-item :label="$t('paramDialog.remark')" prop="remark" class="form-item remark-item">
  49. <el-input type="textarea" v-model="form.remark" :placeholder="$t('paramDialog.remarkPlaceholder')" :rows="3"
  50. class="custom-textarea"></el-input>
  51. </el-form-item>
  52. </el-form>
  53. <div class="dialog-footer">
  54. <el-button type="primary" @click="submit" class="save-btn" :loading="saving" :disabled="saving">
  55. {{ $t('paramDialog.save') }}
  56. </el-button>
  57. <el-button @click="cancel" class="cancel-btn">
  58. {{ $t('paramDialog.cancel') }}
  59. </el-button>
  60. </div>
  61. </div>
  62. </el-dialog>
  63. </template>
  64. <script>
  65. export default {
  66. props: {
  67. title: {
  68. type: String,
  69. default: '新增参数'
  70. },
  71. visible: {
  72. type: Boolean,
  73. default: false
  74. },
  75. form: {
  76. type: Object,
  77. default: () => ({
  78. id: null,
  79. paramCode: '',
  80. paramValue: '',
  81. valueType: 'string',
  82. remark: ''
  83. })
  84. }
  85. },
  86. data() {
  87. return {
  88. dialogKey: Date.now(),
  89. saving: false,
  90. valueTypeOptions: [
  91. { value: 'string' },
  92. { value: 'number' },
  93. { value: 'boolean' },
  94. { value: 'array' },
  95. { value: 'json' }
  96. ],
  97. rules: {
  98. paramCode: [
  99. { required: true, message: this.$t('paramDialog.requiredParamCode'), trigger: "blur" }
  100. ],
  101. paramValue: [
  102. { required: true, message: this.$t('paramDialog.requiredParamValue'), trigger: "blur" }
  103. ],
  104. valueType: [
  105. { required: true, message: this.$t('paramDialog.requiredValueType'), trigger: "change" }
  106. ]
  107. }
  108. };
  109. },
  110. methods: {
  111. submit() {
  112. this.$refs.form.validate((valid) => {
  113. if (valid) {
  114. const submitData = { ...this.form };
  115. // 如果是 array 类型,校验格式并转换
  116. if (submitData.valueType === 'array' && submitData.paramValue) {
  117. const lines = submitData.paramValue.split('\n').filter(line => line.trim());
  118. // 检查除最后一行外的每行是否以分号结尾
  119. for (let i = 0; i < lines.length - 1; i++) {
  120. if (!lines[i].trim().endsWith(';')) {
  121. this.$message.error('数组格式错误,需要使用英文分号结尾');
  122. return;
  123. }
  124. }
  125. const items = lines
  126. .map(item => item.trim().replace(/;$/, ''))
  127. .filter(item => item);
  128. submitData.paramValue = items.join(';');
  129. }
  130. // 如果是 json 类型,压缩 JSON 格式后再提交
  131. else if (submitData.valueType === 'json' && submitData.paramValue) {
  132. try {
  133. const parsed = JSON.parse(submitData.paramValue);
  134. submitData.paramValue = JSON.stringify(parsed);
  135. } catch (e) {
  136. // 如果解析失败,保持原值
  137. }
  138. }
  139. this.saving = true; // 开始加载
  140. this.$emit('submit', submitData);
  141. }
  142. });
  143. },
  144. cancel() {
  145. this.saving = false; // 取消时重置状态
  146. this.dialogKey = Date.now();
  147. this.$emit('cancel');
  148. },
  149. // 提供给父组件调用以重置saving状态
  150. resetSaving() {
  151. this.saving = false;
  152. }
  153. },
  154. watch: {
  155. visible(newVal) {
  156. if (newVal) {
  157. if (this.form.paramValue) {
  158. // 如果是 json 类型,格式化显示
  159. if (this.form.valueType === 'json') {
  160. try {
  161. const parsed = JSON.parse(this.form.paramValue);
  162. this.form.paramValue = JSON.stringify(parsed, null, 2);
  163. } catch (e) {
  164. // 如果解析失败,保持原值
  165. }
  166. }
  167. // 如果是 array 类型,将分号分隔的字符串转换为每行一个项目
  168. else if (this.form.valueType === 'array') {
  169. const items = this.form.paramValue.split(';').filter(item => item.trim());
  170. this.form.paramValue = items.join(';\n');
  171. }
  172. }
  173. } else {
  174. // 当对话框关闭时,重置saving状态
  175. this.saving = false;
  176. }
  177. }
  178. }
  179. };
  180. </script>
  181. <style>
  182. .custom-param-dialog {
  183. border-radius: 16px !important;
  184. overflow: hidden;
  185. box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15) !important;
  186. border: none !important;
  187. .el-dialog__header {
  188. display: none;
  189. }
  190. .el-dialog__body {
  191. padding: 0 !important;
  192. border-radius: 16px;
  193. }
  194. }
  195. </style>
  196. <style scoped lang="scss">
  197. .param-dialog-wrapper {
  198. .dialog-container {
  199. padding: 24px 32px;
  200. background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  201. }
  202. .dialog-header {
  203. position: relative;
  204. margin-bottom: 24px;
  205. text-align: center;
  206. }
  207. .dialog-title {
  208. font-size: 20px;
  209. color: #1e293b;
  210. margin: 0;
  211. padding: 0;
  212. font-weight: 600;
  213. letter-spacing: 0.5px;
  214. }
  215. .custom-close-btn {
  216. position: absolute;
  217. top: -8px;
  218. right: -8px;
  219. width: 32px;
  220. height: 32px;
  221. border-radius: 50%;
  222. border: none;
  223. background: #f1f5f9;
  224. color: #64748b;
  225. cursor: pointer;
  226. display: flex;
  227. align-items: center;
  228. justify-content: center;
  229. padding: 0;
  230. outline: none;
  231. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  232. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  233. &:hover {
  234. color: #ffffff;
  235. background: #ef4444;
  236. transform: rotate(90deg);
  237. box-shadow: 0 4px 6px rgba(239, 68, 68, 0.2);
  238. }
  239. svg {
  240. transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  241. }
  242. }
  243. .param-form {
  244. .form-item {
  245. margin-bottom: 20px;
  246. :deep(.el-form-item__label) {
  247. color: #475569;
  248. font-weight: 500;
  249. padding-right: 12px;
  250. text-align: right;
  251. font-size: 14px;
  252. letter-spacing: 0.2px;
  253. }
  254. }
  255. .custom-input {
  256. :deep(.el-input__inner) {
  257. background-color: #ffffff;
  258. border-radius: 8px;
  259. border: 1px solid #e2e8f0;
  260. height: 42px;
  261. padding: 0 14px;
  262. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  263. font-size: 14px;
  264. color: #334155;
  265. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  266. &:focus {
  267. border-color: #3b82f6;
  268. box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
  269. background-color: #ffffff;
  270. }
  271. &::placeholder {
  272. color: #94a3b8;
  273. font-weight: 400;
  274. }
  275. }
  276. }
  277. .custom-select {
  278. width: 100%;
  279. :deep(.el-input__inner) {
  280. background-color: #ffffff;
  281. border-radius: 8px;
  282. border: 1px solid #e2e8f0;
  283. height: 42px;
  284. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  285. font-size: 14px;
  286. color: #334155;
  287. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  288. &:focus {
  289. border-color: #3b82f6;
  290. box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
  291. background-color: #ffffff;
  292. }
  293. &::placeholder {
  294. color: #94a3b8;
  295. font-weight: 400;
  296. }
  297. }
  298. }
  299. .custom-textarea {
  300. :deep(.el-textarea__inner) {
  301. background-color: #ffffff;
  302. border-radius: 8px;
  303. border: 1px solid #e2e8f0;
  304. padding: 12px 14px;
  305. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  306. font-size: 14px;
  307. color: #334155;
  308. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  309. line-height: 1.5;
  310. &:focus {
  311. border-color: #3b82f6;
  312. box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
  313. background-color: #ffffff;
  314. }
  315. &::placeholder {
  316. color: #94a3b8;
  317. font-weight: 400;
  318. }
  319. }
  320. }
  321. .remark-item :deep(.el-form-item__label) {
  322. margin-top: -4px;
  323. }
  324. }
  325. .dialog-footer {
  326. display: flex;
  327. justify-content: center;
  328. padding: 16px 0 0;
  329. margin-top: 16px;
  330. .save-btn {
  331. width: 120px;
  332. height: 42px;
  333. font-size: 14px;
  334. font-weight: 500;
  335. border-radius: 8px;
  336. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  337. background: #3b82f6;
  338. color: white;
  339. border: none;
  340. letter-spacing: 0.5px;
  341. box-shadow: 0 2px 4px rgba(59, 130, 246, 0.2);
  342. &:hover {
  343. background: #2563eb;
  344. transform: translateY(-1px);
  345. box-shadow: 0 4px 6px rgba(59, 130, 246, 0.3);
  346. }
  347. &:active {
  348. transform: translateY(0);
  349. box-shadow: 0 2px 3px rgba(59, 130, 246, 0.2);
  350. }
  351. }
  352. .cancel-btn {
  353. width: 120px;
  354. height: 42px;
  355. font-size: 14px;
  356. font-weight: 500;
  357. border-radius: 8px;
  358. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  359. background: #ffffff;
  360. color: #64748b;
  361. border: 1px solid #e2e8f0;
  362. margin-left: 16px;
  363. letter-spacing: 0.5px;
  364. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  365. &:hover {
  366. background: #f8fafc;
  367. color: #475569;
  368. border-color: #cbd5e1;
  369. transform: translateY(-1px);
  370. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  371. }
  372. &:active {
  373. transform: translateY(0);
  374. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  375. }
  376. }
  377. }
  378. }
  379. </style>