Create.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <div slot="body">
  5. <template v-if="params.type !== 'create'">
  6. <dialog-selected-tips :name="$t('dictionary.dns')" :count="params.data.length" :action="params.type === 'update' ? $t('network.text_130') : $t('network.text_155')" />
  7. <dialog-table :data="params.data" :columns="params.columns.slice(0, 4)" />
  8. </template>
  9. <a-form
  10. :form="form.fc">
  11. <a-form-item :label="$t('network.text_156')" v-bind="formItemLayout" v-if="enableType('A/AAAA', 'CNAME', 'SRV')" :extra="$t('network.text_157')">
  12. <a-input v-decorator="decorators.name" :placeholder="$t('network.text_158')" />
  13. </a-form-item>
  14. <a-form-item :label="$t('network.text_156')" v-bind="formItemLayout" v-if="enableType('PTR')" :extra="$t('network.text_157')">
  15. <template #extra>
  16. <div>{{$t('network.text_159')}}</div>
  17. </template>
  18. <a-input v-decorator="decorators.ptrName" :placeholder="$t('network.text_158')">
  19. <div slot="addonAfter">.in-addr.arpa</div>
  20. </a-input>
  21. </a-form-item>
  22. <a-form-item :label="$t('network.text_160')" v-bind="formItemLayout">
  23. <a-radio-group @change="recordTypeChange" v-model="formMsg.recordType">
  24. <a-radio-button :key="n" :value="item" v-for="(item, n) in recordTypeOptions">{{item}}</a-radio-button>
  25. </a-radio-group>
  26. </a-form-item>
  27. <a-form-item v-bind="formItemLayout">
  28. <span slot="label">{{$t('network.text_152')}}<a-tooltip class="item" effect="dark" placement="top">
  29. <a-icon type="info-circle" />
  30. <div slot="title">{{$t('network.text_161')}}<br />{{$t('network.text_162')}}<br />{{$t('network.text_163')}}<br />{{$t('network.text_164')}}</div>
  31. </a-tooltip>
  32. </span>
  33. <a-row :gutter="4" v-if="enableType('SRV')">
  34. <a-col :span="10" class="text-center">{{$t('network.text_156')}}</a-col>
  35. <a-col :span="4" class="text-center">{{$t('network.text_165')}}</a-col>
  36. <a-col :span="4" class="text-center">{{$t('network.text_166')}}</a-col>
  37. <a-col :span="4" class="text-center">{{$t('network.text_81')}}</a-col>
  38. </a-row>
  39. <a-row
  40. :gutter="4"
  41. :key="n"
  42. class="record-list mb-3"
  43. v-for="(obj, n) in recordList">
  44. <a-col :span="10" v-if="enableType('A/AAAA')">
  45. <a-form-item>
  46. <a-select :placeholder="$t('network.text_167')" v-decorator="recordConfig(n, obj.key)[`key[${n}]`]">
  47. <a-select-option
  48. :key="i"
  49. :value="item.value"
  50. v-for="(item, i) in recordOptions">
  51. {{item.label}}
  52. </a-select-option>
  53. </a-select>
  54. </a-form-item>
  55. </a-col>
  56. <a-col :span="10" v-if="enableType('A/AAAA', 'CNAME', 'PTR')">
  57. <a-form-item>
  58. <a-input :placeholder="computeText(obj, n)" v-decorator="recordConfig(n, obj.record)[`record[${n}]`]" />
  59. </a-form-item>
  60. </a-col>
  61. <div v-if="enableType('SRV')">
  62. <a-col :span="10">
  63. <a-form-item>
  64. <a-input :placeholder="computeText(obj, n)" v-decorator="recordConfig(n, obj.host)[`host[${n}]`]" />
  65. </a-form-item>
  66. </a-col>
  67. <a-col :span="4">
  68. <a-form-item>
  69. <a-input :max="65535" :min="1" type="number" v-decorator="recordConfig(n, obj.port)[`port[${n}]`]" />
  70. </a-form-item>
  71. </a-col>
  72. <a-col :span="4">
  73. <a-form-item>
  74. <a-input :min="0" type="number" v-decorator="recordConfig(n, obj.weight)[`weight[${n}]`]" />
  75. </a-form-item>
  76. </a-col>
  77. <a-col :span="4">
  78. <a-form-item>
  79. <a-input :min="0" type="number" v-decorator="recordConfig(n, obj.priority)[`priority[${n}]`]" />
  80. </a-form-item>
  81. </a-col>
  82. </div>
  83. <a-button v-if="enableType('A/AAAA', 'SRV')" shape="circle" icon="minus" size="small" @click="() => removeItem(obj, n)" class="mt-2 ml-1" />
  84. </a-row>
  85. <div class="d-flex align-items-center" v-if="enableType('A/AAAA', 'SRV') && remain > 0">
  86. <a-button type="primary" shape="circle" icon="plus" size="small" @click="addItem" />
  87. <a-button type="link" @click="addItem">{{$t('network.text_168')}}</a-button>
  88. <span class="network-count-tips">{{$t('network.text_169')}}<span class="remain-num">{{ remain }}</span>{{$t('network.text_170')}}</span>
  89. </div>
  90. </a-form-item>
  91. <a-form-item v-bind="formItemLayout">
  92. <span slot="label">
  93. TTL
  94. <a-tooltip class="item" effect="dark" placement="top">
  95. <a-icon type="info-circle" />
  96. <div slot="title">{{$t('network.text_171')}}</div>
  97. </a-tooltip>
  98. </span>
  99. <a-input-number
  100. v-decorator="decorators.ttl"
  101. :placeholder="$t('network.text_172')"
  102. :min="0"
  103. :max="86400" />{{$t('network.text_76')}}</a-form-item>
  104. </a-form>
  105. </div>
  106. <div slot="footer">
  107. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  108. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  109. </div>
  110. </base-dialog>
  111. </template>
  112. <script>
  113. import { mapGetters } from 'vuex'
  114. import { REGEXP } from '@/utils/validate'
  115. import DialogMixin from '@/mixins/dialog'
  116. import WindowsMixin from '@/mixins/windows'
  117. const { IPv4, IPv6, domain } = REGEXP
  118. const MAX_ITEMS = 6
  119. const RECORD_TYPE_MAP = {
  120. A: 'A/AAAA',
  121. AAAA: 'A/AAAA',
  122. CNAME: 'CNAME',
  123. PTR: 'PTR',
  124. SRV: 'SRV',
  125. }
  126. export default {
  127. name: 'DnsCreateDialog',
  128. mixins: [DialogMixin, WindowsMixin],
  129. data () {
  130. return {
  131. loading: false,
  132. form: {
  133. fc: this.$form.createForm(this),
  134. },
  135. formMsg: {
  136. recordType: 'A/AAAA',
  137. cacheRecord: {},
  138. },
  139. decorators: {
  140. name: [
  141. 'name',
  142. {
  143. validateFirst: true,
  144. validateTrigger: ['blur'],
  145. rules: [
  146. { required: true, message: this.$t('network.text_173') },
  147. { validator: this.checkName },
  148. // { validator: this.$validate('broadName') },
  149. ],
  150. },
  151. ],
  152. ptrName: [
  153. 'ptrName',
  154. {
  155. validateFirst: true,
  156. validateTrigger: ['blur'],
  157. rules: [
  158. { required: true, message: this.$t('network.text_173') },
  159. { validator: this.$validate('IPv4') },
  160. ],
  161. },
  162. ],
  163. ttl: [
  164. 'ttl',
  165. {
  166. initialValue: this.params.type === 'create' ? 600 : this.params.data[0].ttl,
  167. validateFirst: true,
  168. validateTrigger: ['blur'],
  169. rules: [
  170. { required: true, message: this.$t('network.text_174') },
  171. ],
  172. },
  173. ],
  174. },
  175. recordConfig: (i, recordData) => ({
  176. [`key[${i}]`]: [
  177. `key[${i}]`,
  178. {
  179. initialValue: this.recordList[0].key,
  180. },
  181. ],
  182. [`record[${i}]`]: [
  183. `record[${i}]`,
  184. {
  185. validateTrigger: ['blur'],
  186. initialValue: this.recordList[0].record,
  187. rules: [{
  188. required: true,
  189. message: this.$t('network.text_175'),
  190. }],
  191. },
  192. ],
  193. [`host[${i}]`]: [
  194. `host[${i}]`,
  195. {
  196. validateFirst: true,
  197. validateTrigger: ['blur'],
  198. rules: [{
  199. required: true,
  200. message: this.$t('network.text_158'),
  201. }, {
  202. validator: this.$validate('domain'),
  203. }],
  204. },
  205. ],
  206. [`port[${i}]`]: [
  207. `port[${i}]`,
  208. {
  209. initialValue: 1,
  210. validateTrigger: ['blur'],
  211. rules: [{
  212. required: true,
  213. message: this.$t('network.text_176'),
  214. }],
  215. },
  216. ],
  217. [`weight[${i}]`]: [
  218. `weight[${i}]`,
  219. {
  220. initialValue: 0,
  221. validateTrigger: ['blur'],
  222. rules: [{
  223. required: true,
  224. message: this.$t('network.text_177'),
  225. }],
  226. },
  227. ],
  228. [`priority[${i}]`]: [
  229. `priority[${i}]`,
  230. {
  231. initialValue: 0,
  232. validateTrigger: ['blur'],
  233. rules: [{
  234. required: true,
  235. message: this.$t('network.text_117'),
  236. }],
  237. },
  238. ],
  239. }),
  240. formItemLayout: {
  241. wrapperCol: {
  242. span: 20,
  243. },
  244. labelCol: {
  245. span: 4,
  246. },
  247. },
  248. recordOptions: [
  249. { value: 'A', label: 'A' },
  250. { value: 'AAAA', label: 'AAAA' },
  251. ],
  252. recordTypeOptions: ['A/AAAA', 'CNAME', 'PTR', 'SRV'],
  253. recordList: [{ key: 'A', record: '' }],
  254. }
  255. },
  256. computed: {
  257. ...mapGetters(['isAdminMode', 'scope', 'userInfo']),
  258. remain () {
  259. return MAX_ITEMS - this.recordList.length
  260. },
  261. },
  262. created () {
  263. this.dnsM = new this.$Manager('dnsrecords')
  264. this.dialogOpen()
  265. },
  266. methods: {
  267. checkName (rule, value, callback) {
  268. if (!value) {
  269. return callback(new Error(this.$t('network.text_158')))
  270. }
  271. if (IPv4.regexp.test(value) || !domain.regexp.test(value)) {
  272. return callback(new Error(this.$t('network.text_178')))
  273. }
  274. if (this.formMsg.recordType === 'SRV') {
  275. const parts = value.split('.')
  276. if (parts.length < 3) {
  277. return callback(new Error(this.$t('network.text_179')))
  278. }
  279. for (let i = 0; i < parts.length; i++) {
  280. if (i < 2 && (parts[i].length < 2 || parts[i][0] !== '_')) {
  281. return callback(new Error(this.$t('network.text_180')))
  282. } else if (i >= 2 && parts[i].length === 0) {
  283. return callback(new Error(this.$t('network.text_180')))
  284. }
  285. }
  286. }
  287. callback()
  288. },
  289. dialogOpen () {
  290. if (this.params.type === 'update' || this.params.type === 'clone') {
  291. this._updateFormValue(this.params.data[0])
  292. }
  293. },
  294. _updateFormValue (val) {
  295. const { recordList, recordType } = this._classify(val.records)
  296. this.recordList = recordList
  297. this.formMsg.recordType = recordType
  298. if (this.params.type === 'update') {
  299. if (recordType === 'PTR') {
  300. this.form.fc.getFieldDecorator('ptrName', { preserve: true, initialValue: val.name })
  301. } else {
  302. this.form.fc.getFieldDecorator('name', { preserve: true, initialValue: val.name })
  303. }
  304. }
  305. const ttl = val.ttl
  306. this.form.fc.getFieldDecorator('ttl', { preserve: true, initialValue: ttl })
  307. },
  308. _classify (recordStr) {
  309. const initArr = recordStr.split(',')
  310. const recordList = initArr.map(keyValue => {
  311. const arr = keyValue.split(':')
  312. const key = arr[0]
  313. if (key === 'SRV') {
  314. return {
  315. key,
  316. host: arr[1],
  317. port: arr[2],
  318. weight: arr[3],
  319. priority: arr[4],
  320. }
  321. } else {
  322. const record = keyValue
  323. .split(':')
  324. .slice(1)
  325. .join(':')
  326. return { key, record }
  327. }
  328. })
  329. const key = recordList[0].key // 拿第一个 key 做判断
  330. return {
  331. recordType: RECORD_TYPE_MAP[key],
  332. recordList,
  333. }
  334. },
  335. enableType (...arr) {
  336. const T = this.formMsg.recordType
  337. return Array.prototype.includes.call(arr, T)
  338. },
  339. recordTypeChange (e) {
  340. const type = e.target.value
  341. switch (type) {
  342. case 'A/AAAA':
  343. this.recordList = this.formMsg.cacheRecord[type] || [
  344. { key: 'A', record: '' },
  345. ]
  346. this.form.fc.setFieldsValue({
  347. key: [
  348. { key: 'A', record: '' },
  349. ],
  350. })
  351. break
  352. case 'CNAME':
  353. this.recordList = this.formMsg.cacheRecord[type] || [
  354. { key: 'CNAME', record: '' },
  355. ]
  356. this.form.fc.setFieldsValue({
  357. key: [
  358. { key: 'CNAME', record: '' },
  359. ],
  360. })
  361. break
  362. case 'PTR':
  363. this.recordList = this.formMsg.cacheRecord[type] || [
  364. { key: 'PTR', record: '' },
  365. ]
  366. this.form.fc.setFieldsValue({
  367. key: [
  368. { key: 'PTR', record: '' },
  369. ],
  370. })
  371. break
  372. case 'SRV':
  373. this.recordList = this.formMsg.cacheRecord[type] || [
  374. { key: 'SRV', host: '', port: 1, weight: 0, priority: 0 },
  375. ]
  376. this.form.fc.setFieldsValue({
  377. key: [
  378. { key: 'SRV', host: '', port: 1, weight: 0, priority: 0 },
  379. ],
  380. })
  381. break
  382. }
  383. this.$nextTick(() => {
  384. // this.form.fc.resetFields()
  385. if (type === 'SRV') {
  386. this.form.fc.validateFields(['name'])
  387. }
  388. })
  389. },
  390. addItem () {
  391. if (this.formMsg.recordType === 'A/AAAA') {
  392. this.recordList.push({ key: 'A', record: '' })
  393. } else if (this.formMsg.recordType === 'SRV') {
  394. this.recordList.push({
  395. key: 'SRV',
  396. host: '',
  397. port: 1,
  398. weight: 0,
  399. priority: 0,
  400. })
  401. }
  402. },
  403. removeItem (obj, n) {
  404. this.recordList.splice(n, 1)
  405. },
  406. computeText (obj, n) {
  407. const values = this.form.fc.getFieldsValue()
  408. if (values.key) {
  409. const key = values.key[n]
  410. if (key === 'A') {
  411. return this.$t('network.text_181')
  412. }
  413. if (key === 'AAAA') {
  414. return this.$t('network.text_182')
  415. }
  416. }
  417. if (values.host) {
  418. return this.$t('network.text_158')
  419. }
  420. if (this.formMsg.recordType === 'A/AAAA') {
  421. return this.$t('network.text_181')
  422. }
  423. return this.$t('network.text_175')
  424. },
  425. doCreate (data) {
  426. if (this.params.type === 'create') {
  427. return this.dnsM.create({ data })
  428. } else if (this.params.type === 'update') {
  429. return this.dnsM.update({ id: this.params.data[0].id, data })
  430. } else if (this.params.type === 'clone') {
  431. return this.dnsM.create({ data })
  432. }
  433. },
  434. genData (values) {
  435. let newValues = {}
  436. const recordList = []
  437. if (values.key) {
  438. values.key.map((item, index) => {
  439. const ops = {
  440. key: item,
  441. record: values.record[index],
  442. }
  443. recordList.push(ops)
  444. })
  445. }
  446. if (values.record && !values.key) {
  447. values.record.map((item) => {
  448. const ops = {
  449. record: item,
  450. }
  451. recordList.push(ops)
  452. })
  453. }
  454. if (values.host) {
  455. values.host.map((item, index) => {
  456. const ops = {
  457. host: item,
  458. port: values.port[index],
  459. weight: values.weight[index],
  460. priority: values.priority[index],
  461. }
  462. recordList.push(ops)
  463. })
  464. }
  465. newValues = {
  466. name: values.name ? values.name : values.ptrName,
  467. ttl: values.ttl,
  468. is_public: true,
  469. }
  470. const addParams = (recordTypeArr, callback) => recordTypeArr.forEach(callback)
  471. if (this.formMsg.recordType === 'A/AAAA') {
  472. const aType = recordList.filter(v => v.key === 'A')
  473. const aaaaType = recordList.filter(
  474. v => v.key === 'AAAA',
  475. )
  476. addParams(aType, (obj, i) => {
  477. newValues[`A.${i}`] = obj.record
  478. })
  479. addParams(aaaaType, (obj, i) => {
  480. newValues[`AAAA.${i}`] = obj.record
  481. })
  482. } else if (this.formMsg.recordType === 'CNAME') {
  483. newValues.CNAME = recordList[0].record
  484. } else if (this.formMsg.recordType === 'PTR') {
  485. newValues.name = `${newValues.ptrName}.in-addr.arpa`
  486. newValues.PTR = recordList[0].record
  487. } else if (this.formMsg.recordType === 'SRV') {
  488. addParams(recordList, (obj, i) => {
  489. newValues[`SRV.${i}`] = `${obj.host}:${obj.port}:${obj.weight}:${obj.priority}`
  490. })
  491. }
  492. if (values.key) this._validform(recordList)
  493. return newValues
  494. },
  495. async handleConfirm () {
  496. this.loading = true
  497. try {
  498. const values = await this.form.fc.validateFields()
  499. const newValues = this.genData(values)
  500. await this.doCreate(newValues)
  501. this.loading = false
  502. this.cancelDialog()
  503. this.params.refresh()
  504. } catch (error) {
  505. this.loading = false
  506. }
  507. },
  508. _validform (values) {
  509. let validformdata = false
  510. const itemValid = values.every(val => {
  511. if (val.key === 'A') {
  512. return IPv4.regexp.test(val.record)
  513. } else if (val.key === 'AAAA') {
  514. return IPv6.regexp.test(val.record)
  515. } else {
  516. return true
  517. }
  518. })
  519. if (!itemValid) {
  520. this.$message.error(this.$t('network.text_183'))
  521. }
  522. validformdata = itemValid
  523. return validformdata
  524. },
  525. },
  526. }
  527. </script>