list.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <template>
  2. <Card>
  3. <p slot="title">
  4. <Icon type="ios-toggle"></Icon>
  5. 任务列表
  6. </p>
  7. <Row style="margin-bottom: 10px">
  8. <Col span="24">
  9. <Button type="success" @click="getListData()">重新加载</Button>
  10. <Button type="success" @click="getAddModal">添加任务</Button>
  11. </Col>
  12. </Row>
  13. <Row>
  14. <Col span="6">
  15. <Form ref="searchForm" :model="searchForm">
  16. <FormItem prop="name" label="">
  17. <Input type="text" v-model="searchForm.name" placeholder="输入名称...">
  18. <Icon type="ios-search" slot="prepend"></Icon>
  19. </Input>
  20. </FormItem>
  21. </Form>
  22. </Col>
  23. <Col span="24">
  24. <Table border size="small" :loading="loading" :columns="columns" :data="data"></Table>
  25. <Modal
  26. class-name="vertical-center-modal"
  27. title="添加数据"
  28. v-model="addModal"
  29. :loading="true"
  30. :width="60"
  31. @on-visible-change="visibleChange()"
  32. :closable="true">
  33. <Form ref="addForm" :model="addForm" :rules="addFormRule">
  34. <Row :gutter="16">
  35. <Col span="24" v-show="addFormError !== null">
  36. <Alert show-icon type="error">{{addFormError}}</Alert>
  37. </Col>
  38. <Col span="24">
  39. <FormItem prop="switch" label="">
  40. <Switch size="large" v-model="addForm.switch" :true-value=1 :false-value=2>
  41. <span slot="open">ON</span>
  42. <span slot="close">OFF</span>
  43. </Switch>
  44. </FormItem>
  45. </Col>
  46. <Col span="24">
  47. <FormItem prop="type" label="">
  48. <Select v-model="addForm.type" style="width:100px">
  49. <Option :value="1" :key="1">shell</Option>
  50. <Option :value="2" :key="2">python</Option>
  51. </Select>
  52. </FormItem>
  53. </Col>
  54. <Col span="12">
  55. <FormItem prop="rule" label="">
  56. <Input type="text" v-model="addForm.rule" placeholder="输入需要执行的规则...">
  57. <Icon type="ios-pricetag-outline" slot="prepend"></Icon>
  58. </Input>
  59. </FormItem>
  60. </Col>
  61. <Col span="13">
  62. <FormItem prop="name" label="">
  63. <Input type="text" v-model="addForm.name" placeholder="输入名称...">
  64. <Icon type="ios-pricetag-outline" slot="prepend"></Icon>
  65. </Input>
  66. </FormItem>
  67. </Col>
  68. <Col span="24">
  69. <FormItem prop="command" label="">
  70. <Input type="textarea" :rows="15" v-model="addForm.command" placeholder="输入需要执行的命令...">
  71. <Icon type="ios-pricetag-outline" slot="prepend"></Icon>
  72. </Input>
  73. </FormItem>
  74. </Col>
  75. </Row>
  76. </Form>
  77. <div slot="footer">
  78. <Button type="success" size="large" :loading="addModalLoading" @click="add('addForm')">
  79. 确认添加
  80. </Button>
  81. <Button type="error" size="large" @click="addModal = false">关闭</Button>
  82. </div>
  83. </Modal>
  84. <Modal
  85. class-name="vertical-center-modal"
  86. title="修改数据"
  87. v-model="updateModal"
  88. :loading="true"
  89. :width="60"
  90. @on-visible-change="visibleChange()"
  91. :closable="true">
  92. <Form ref="updateForm" :model="updateForm" :rules="updateFormRule">
  93. <Row :gutter="16">
  94. <Col span="24" v-show="updateFormError !== null">
  95. <Alert show-icon type="error">{{updateFormError}}</Alert>
  96. </Col>
  97. <Col span="24">
  98. <FormItem prop="switch" label="">
  99. <Switch size="large" v-model="updateForm.switch" :true-value=1 :false-value=2>
  100. <span slot="open">ON</span>
  101. <span slot="close">OFF</span>
  102. </Switch>
  103. </FormItem>
  104. </Col>
  105. <Col span="24">
  106. <FormItem prop="type" label="">
  107. <Select v-model="updateForm.type" style="width:100px">
  108. <Option :value="1" :key="1">shell</Option>
  109. <Option :value="2" :key="2">python</Option>
  110. </Select>
  111. </FormItem>
  112. </Col>
  113. <Col span="12">
  114. <FormItem prop="rule" label="">
  115. <Input type="text" v-model="updateForm.rule" placeholder="输入需要执行的规则...">
  116. <Icon type="ios-pricetag-outline" slot="prepend"></Icon>
  117. </Input>
  118. </FormItem>
  119. </Col>
  120. <Col span="13">
  121. <FormItem prop="name" label="">
  122. <Input type="text" v-model="updateForm.name" placeholder="输入名称...">
  123. <Icon type="ios-pricetag-outline" slot="prepend"></Icon>
  124. </Input>
  125. </FormItem>
  126. </Col>
  127. <Col span="24">
  128. <FormItem prop="command" label="">
  129. <Input type="textarea" :rows="15" v-model="updateForm.command" placeholder="输入需要执行的命令...">
  130. <Icon type="ios-pricetag-outline" slot="prepend"></Icon>
  131. </Input>
  132. </FormItem>
  133. </Col>
  134. </Row>
  135. </Form>
  136. <div slot="footer">
  137. <Button type="success" size="large" :loading="updateModalLoading" @click="update('updateForm')">
  138. 确认修改
  139. </Button>
  140. <Button type="error" size="large" @click="updateModal = false">关闭</Button>
  141. </div>
  142. </Modal>
  143. <Modal
  144. class-name="vertical-center-modal"
  145. title="执行任务"
  146. v-model="execModal"
  147. :loading="true"
  148. :width="50"
  149. @on-visible-change="visibleChange()"
  150. :closable="true">
  151. <Row :gutter="16">
  152. <Col span="24" v-show="execError !== null">
  153. <Alert show-icon type="error">{{execError}}</Alert>
  154. </Col>
  155. <Col span="24" v-if="execId !== null">
  156. <drawer :execId="execId"></drawer>
  157. </Col>
  158. <Col span="24" v-else>
  159. <Itext v-if="execForm.type == 1" textData="脚本类型:shell" :lengthData=100 ></Itext>
  160. <Itext v-if="execForm.type == 2" textData="脚本类型:python" :lengthData=100 ></Itext>
  161. 脚本代码:
  162. <Itext :textData="execForm.command" :lengthData=100 ></Itext>
  163. </Col>
  164. </Row>
  165. <div slot="footer">
  166. <Button v-show="execId == null" type="success" size="large" :loading="execModalLoading" @click="exec('execForm')">
  167. 确认执行
  168. </Button>
  169. <Button type="error" size="large" @click="execModal = false">关闭</Button>
  170. </div>
  171. </Modal>
  172. </Col>
  173. </Row>
  174. <br/>
  175. <Row>
  176. <Col :xs="24" :lg="24">
  177. <Page
  178. :total="pageTotal"
  179. :page-size="pageSize"
  180. :page-size-opts="pageSizeOpts"
  181. placement="top"
  182. show-total
  183. show-sizer
  184. show-elevator
  185. @on-change="pageChange"
  186. @on-page-size-change="pageSizeChange">
  187. </Page>
  188. </Col>
  189. </Row>
  190. </Card>
  191. </template>
  192. <script>
  193. // import ajax from '../../../libs/ajax';
  194. // import message from '../../../libs/message';
  195. // import drawer from './drawer';
  196. // import Itext from './text'
  197. export default {
  198. components: {drawer,Itext},
  199. names: 'list',
  200. data() {
  201. return {
  202. loading: false,
  203. page: 1,
  204. pageSize: 15,
  205. pageTotal: 0,
  206. pageSizeOpts: [15, 20, 30, 40, 50],
  207. execModal: false,
  208. execModalLoading: false,
  209. execForm: {
  210. command: null,
  211. type: 1
  212. },
  213. execError: null,
  214. execId: null,
  215. execSuccessButton: true,
  216. updateModal: false,
  217. updateModalLoading: false,
  218. updateForm: {
  219. id: null,
  220. name: null,
  221. command: null,
  222. rule: null,
  223. switch: 1,
  224. type: 1
  225. },
  226. updateFormError: null,
  227. updateFormRule: {
  228. name: [
  229. {required: true, message: '名称不能为空', trigger: 'blur'}
  230. ],
  231. command: [
  232. {required: true, message: '命令不能为空', trigger: 'blur'}
  233. ],
  234. rule: [
  235. {required: true, message: '规则不能为空', trigger: 'blur'}
  236. ]
  237. },
  238. addModal: false,
  239. addModalLoading: false,
  240. addForm: {
  241. name: null,
  242. command: null,
  243. rule: null,
  244. switch: 1,
  245. type: 1
  246. },
  247. addFormError: null,
  248. addFormRule: {
  249. name: [
  250. {required: true, message: '名称不能为空', trigger: 'blur'}
  251. ],
  252. command: [
  253. {required: true, message: '命令不能为空', trigger: 'blur'}
  254. ],
  255. rule: [
  256. {required: true, message: '规则不能为空', trigger: 'blur'}
  257. ]
  258. },
  259. searchForm: {
  260. name: null,
  261. },
  262. columns: [
  263. {
  264. title: '名称',
  265. key: 'name',
  266. minWidth: 80,
  267. ellipsis: true
  268. },{
  269. title: '命令',
  270. key: 'command',
  271. minWidth: 240,
  272. ellipsis: true
  273. },
  274. {
  275. title: '时间规则',
  276. key: 'rule',
  277. width: 200,
  278. ellipsis: true,
  279. },
  280. {
  281. title: '脚本',
  282. key: 'type',
  283. width: 100,
  284. align: 'center',
  285. ellipsis: true,
  286. render: (h, params) => {
  287. return {
  288. 1: h('p', {
  289. style: {
  290. color: 'blue'
  291. }
  292. }, 'shell'),
  293. 2: h('p', {
  294. style: {
  295. color: 'blue'
  296. }
  297. }, 'python')
  298. }[params.row.type];
  299. }
  300. },
  301. {
  302. title: '开关',
  303. key: 'switch',
  304. width: 100,
  305. align: 'center',
  306. ellipsis: true,
  307. render: (h, params) => {
  308. return {
  309. 1: h('p', {
  310. style: {
  311. color: 'green'
  312. }
  313. }, '开启中'),
  314. 2: h('p', {
  315. style: {
  316. color: 'red'
  317. }
  318. }, '已关闭')
  319. }[params.row.switch];
  320. }
  321. },
  322. {
  323. title: '数据创建时间',
  324. key: 'create_time',
  325. width: 180,
  326. align: 'center',
  327. ellipsis: true
  328. },
  329. {
  330. title: '数据修改时间',
  331. key: 'update_time',
  332. width: 180,
  333. align: 'center',
  334. ellipsis: true
  335. },
  336. {
  337. title: '操作',
  338. key: 'action',
  339. width: 200,
  340. align: 'center',
  341. ellipsis: true,
  342. render: (h, params) => {
  343. return h('div', [
  344. h('Button', {
  345. props: {
  346. type: 'success',
  347. size: 'small'
  348. },
  349. style: {
  350. marginRight: '5px'
  351. },
  352. on: {
  353. click: () => {
  354. this.execModal = true;
  355. let index = params.index;
  356. this.execForm.command = this.data[index].command;
  357. this.execForm.type = parseInt(this.data[index].type);
  358. }
  359. }
  360. }, '立即运行'),
  361. h('Button', {
  362. props: {
  363. type: 'success',
  364. size: 'small'
  365. },
  366. style: {
  367. marginRight: '5px'
  368. },
  369. on: {
  370. click: () => {
  371. this.updateModal = true;
  372. let index = params.index;
  373. this.updateForm.id = this.data[index].id;
  374. this.updateForm.name = this.data[index].name;
  375. this.updateForm.command = this.data[index].command;
  376. this.updateForm.rule = this.data[index].rule;
  377. this.updateForm.switch = parseInt(this.data[index].switch);
  378. this.updateForm.type = parseInt(this.data[index].type);
  379. }
  380. }
  381. }, '修改'),
  382. h('Button', {
  383. props: {
  384. type: 'error',
  385. size: 'small'
  386. }
  387. }, [
  388. h('Poptip', {
  389. props: {
  390. confirm: true,
  391. transfer: true,
  392. placement: 'left',
  393. title: '确定要删除吗!',
  394. type: 'error',
  395. size: 'small'
  396. },
  397. on: {
  398. 'on-ok': () => {
  399. let index = params.index;
  400. this.remove(this.data[index].id);
  401. }
  402. }
  403. }, '删除')
  404. ])
  405. ]);
  406. }
  407. }
  408. ],
  409. data: [],
  410. async: null,
  411. userId: null
  412. };
  413. },
  414. watch: {
  415. page: function (newPage, oldPage) {
  416. this.getListData();
  417. },
  418. pageSize: function (newPageSize, oldPageSize) {
  419. this.getListData();
  420. },
  421. 'searchForm.name': function (newPageSize, oldPageSize) {
  422. this.getListData();
  423. }
  424. },
  425. methods: {
  426. visibleChange() {
  427. this.addFormError = null;
  428. this.updateFormError = null;
  429. this.execError = null;
  430. this.execId = null;
  431. },
  432. pageChange(index) {
  433. this.page = index;
  434. },
  435. pageSizeChange(index) {
  436. this.pageSize = index;
  437. },
  438. update(name) {
  439. this.$refs[name].validate((valid) => {
  440. if (valid) {
  441. this.updateData();
  442. }
  443. });
  444. },
  445. exec(name) {
  446. this.execData();
  447. },
  448. add(name) {
  449. this.$refs[name].validate((valid) => {
  450. if (valid) {
  451. this.addData(name);
  452. }
  453. });
  454. },
  455. remove (id) {
  456. this.deleteData(id);
  457. },
  458. getListData(loading = true, timeout = 300) {
  459. clearTimeout(this.async);
  460. if (loading === true) {
  461. this.loading = true;
  462. }
  463. this.async = setTimeout(() => {
  464. (new ajax()).send('/v1/task/index?page=' + this.page + '&per-page=' + this.pageSize, {
  465. 'name': this.searchForm.name
  466. }).then((response) => {
  467. var data = response.data;
  468. this.data = data.data.items;
  469. this.pageTotal = +data.data._meta.totalCount;
  470. this.loading = false;
  471. }).catch((error) => {
  472. this.loading = false;
  473. });
  474. }, timeout);
  475. },
  476. updateData() {
  477. this.updateModalLoading = true;
  478. this.async = setTimeout(() => {
  479. (new ajax()).send('/v1/task/update', {
  480. 'id': this.updateForm.id,
  481. 'name': this.updateForm.name,
  482. 'command': this.updateForm.command,
  483. 'rule': this.updateForm.rule,
  484. 'switch': this.updateForm.switch,
  485. 'type': this.updateForm.type
  486. }, 'post', false).then((response) => {
  487. var data = response.data;
  488. switch (data.success) {
  489. case true:
  490. this.getListData(false, 1);
  491. this.updateFormError = null;
  492. message.success('修改成功');
  493. break;
  494. case false:
  495. this.updateFormError = data.data.message;
  496. break;
  497. }
  498. this.updateModalLoading = false;
  499. }).catch((error) => {
  500. this.updateModalLoading = false;
  501. this.updateFormError = error.message;
  502. }).finally(function (callee) {
  503. });
  504. }, 1000);
  505. },
  506. addData(name) {
  507. this.addModalLoading = true;
  508. this.async = setTimeout(() => {
  509. (new ajax()).send('/v1/task/create', {
  510. 'name': this.addForm.name,
  511. 'command': this.addForm.command,
  512. 'rule': this.addForm.rule,
  513. 'switch': this.addForm.switch,
  514. 'type': this.addForm.type
  515. }, 'post', false).then((response) => {
  516. var data = response.data;
  517. switch (data.success) {
  518. case true:
  519. this.getListData(false, 1);
  520. this.addFormError = null;
  521. this.$refs[name].resetFields();
  522. message.success('添加成功');
  523. break;
  524. case false:
  525. this.addFormError = data.data.message;
  526. break;
  527. }
  528. this.addModalLoading = false;
  529. }).catch((error) => {
  530. this.addModalLoading = false;
  531. this.addFormError = error.message;
  532. }).finally(function (callee) {
  533. });
  534. }, 1000);
  535. },
  536. execData(name) {
  537. this.execModalLoading = true;
  538. this.async = setTimeout(() => {
  539. (new ajax()).send('/v1/task/exec', {
  540. 'command': this.execForm.command,
  541. 'type': this.execForm.type
  542. }, 'post', false).then((response) => {
  543. var data = response.data;
  544. console.log(data);
  545. switch (data.success) {
  546. case true:
  547. this.execError = null;
  548. message.success('入队成功');
  549. this.execId = data.data.id;
  550. break;
  551. case false:
  552. this.execError = data.data.message;
  553. break;
  554. }
  555. this.execModalLoading = false;
  556. }).catch((error) => {
  557. this.execModalLoading = false;
  558. this.execError = error.message;
  559. }).finally(function (callee) {
  560. });
  561. }, 1000);
  562. },
  563. getAddModal() {
  564. this.addModal = true;
  565. },
  566. deleteData (id) {
  567. (new ajax()).send('/v1/task/delete', {
  568. 'id': id
  569. }).then((response) => {
  570. var data = response.data;
  571. this.getListData(false, 1);
  572. message.success('删除成功');
  573. }).catch((error) => {
  574. });
  575. },
  576. },
  577. created() {
  578. this.getListData();
  579. }
  580. };
  581. </script>