pasture.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <template>
  2. <div class="pasture">
  3. <div class="rect rect-form">
  4. <el-form
  5. :inline="true"
  6. :model="dataForm"
  7. size="mini">
  8. <el-form-item style="width: 500px">
  9. <el-input
  10. v-model="dataForm.key"
  11. placeholder="请输入名称/地址/负责人/建立日期"
  12. clearable style="width: 500px">
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button
  17. icon="el-icon-search"
  18. @click="getDataList()">
  19. 查 询
  20. </el-button>
  21. </el-form-item>
  22. </el-form>
  23. </div>
  24. <div class="rect rect-table">
  25. <el-form
  26. :inline="true"
  27. :model="dataForm"
  28. size="mini">
  29. <el-form-item>
  30. <el-button
  31. type="primary"
  32. icon="el-icon-plus"
  33. @click="addOrUpdateHandle()">
  34. 新 增
  35. </el-button>
  36. </el-form-item>
  37. <el-form-item>
  38. <el-button
  39. icon="el-icon-delete"
  40. type="danger"
  41. @click="deleteHandle()"
  42. :disabled="selectionDataList.length <= 0">
  43. 删 除
  44. </el-button>
  45. </el-form-item>
  46. </el-form>
  47. <el-table
  48. height="540"
  49. border
  50. stripe
  51. :data="dataList"
  52. @selection-change="selectionChangeHandle"
  53. v-loading="dataListLoading"
  54. style="width: 100%"
  55. :header-cell-style="{background:'rgb(245,245,245)',color:'#000',height: '45px',fontSize: '13px',fontWeight: 'normal',borderBottom: '1px solid #ccc'}"
  56. :cell-style="{color: '#888',fontSize: '13px'}"
  57. size="mini">
  58. <el-table-column
  59. type="selection"
  60. header-align="center"
  61. align="center"
  62. width="50">
  63. </el-table-column>
  64. <el-table-column
  65. prop="name"
  66. header-align="left"
  67. align="left"
  68. label="名称">
  69. </el-table-column>
  70. <el-table-column
  71. prop="frontLocation"
  72. header-align="left"
  73. align="left"
  74. label="地址">
  75. <template slot-scope="scope">
  76. {{ scope.row.frontLocation? scope.row.frontLocation.split(',').join(''): '' }}
  77. </template>
  78. </el-table-column>
  79. <el-table-column
  80. prop="manager"
  81. header-align="center"
  82. align="center"
  83. label="负责人"
  84. width="150">
  85. </el-table-column>
  86. <el-table-column
  87. prop="buildTime"
  88. header-align="center"
  89. align="center"
  90. :show-overflow-tooltip="true"
  91. label="建立日期"
  92. width="150">
  93. </el-table-column>
  94. <el-table-column
  95. header-align="center"
  96. align="center"
  97. label="操作">
  98. <template slot-scope="scope">
  99. <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
  100. <el-button type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
  101. </template>
  102. </el-table-column>
  103. </el-table>
  104. <div class="block">
  105. <el-pagination
  106. @size-change="sizeChangeHandle"
  107. @current-change="currentChangeHandle"
  108. :current-page="pageIndex"
  109. :page-sizes="[10, 20, 30, 50, 100]"
  110. :page-size="pageSize"
  111. layout="total, sizes, prev, pager, next, jumper"
  112. :total="totalPage">
  113. </el-pagination>
  114. </div>
  115. <el-dialog
  116. :title="!form.id ? '新增' : '修改'"
  117. :close-on-click-modal="false"
  118. :visible.sync="visible"
  119. @close="cancel"
  120. width="600px">
  121. <el-form
  122. :model="form"
  123. :rules="dataRule"
  124. ref="form"
  125. @keyup.enter.native="formSubmit()"
  126. label-width="80px"
  127. size="mini"
  128. style="margin-left: 20px;width: 500px">
  129. <el-form-item label="牧场名称" prop="name">
  130. <el-input v-model="form.name" placeholder="牧场名称"></el-input>
  131. </el-form-item>
  132. <!-- FIXME: 管理员下拉 -->
  133. <el-form-item label="管理员" prop="manager">
  134. <el-input v-model="form.manager" placeholder="牧场管理员"></el-input>
  135. </el-form-item>
  136. <el-form-item label="省市区" prop="selectedOptions">
  137. <el-cascader
  138. :options="options"
  139. v-model="form.selectedOptions"
  140. @change="getlocation"
  141. placeholder="请选择省市区"
  142. style="width: 100%">
  143. </el-cascader>
  144. </el-form-item>
  145. <el-form-item label="具体地址" prop="location2">
  146. <el-input v-model="form.location2" placeholder="地址"></el-input>
  147. </el-form-item>
  148. <el-form-item label="建立日期" prop="buildTime">
  149. <el-date-picker
  150. v-model="form.buildTime"
  151. type="date"
  152. value-format="yyyy-MM-dd"
  153. placeholder="选择建立日期"
  154. style="width: 100%">
  155. </el-date-picker>
  156. </el-form-item>
  157. </el-form>
  158. <span slot="footer" class="dialog-footer">
  159. <el-button size="mini" @click="cancel">关闭</el-button>
  160. <el-button size="mini" type="primary" @click="formSubmit()">确定</el-button>
  161. </span>
  162. </el-dialog>
  163. </div>
  164. </div>
  165. </template>
  166. <script>
  167. import { regionData, CodeToText } from "element-china-area-data"
  168. export default {
  169. data () {
  170. return {
  171. dataForm: {
  172. key: ''
  173. },
  174. dataList: [
  175. {
  176. buildTime: '',
  177. id: '',
  178. location: '',
  179. manager: '',
  180. name: '',
  181. }
  182. ],
  183. pageIndex: 1,
  184. pageSize: 10,
  185. totalPage: 0,
  186. dataListLoading: false,
  187. selectionDataList: [],
  188. visible: false,
  189. form: {
  190. id: '',
  191. location1: '',
  192. location2: '',
  193. manager: '',
  194. name: '',
  195. buildTime: '',
  196. selectedOptions: [],
  197. loc: ''
  198. },
  199. options: regionData,
  200. selectedOptions: [],
  201. loc: '',
  202. dataRule: { // 校验规则
  203. name: [
  204. { required: true, message: '牧场名称不能为空', trigger: 'blur' },
  205. { min: 1, max: 50, message: '长度在 1 到 50 个字符之间', trigger: ['blur', 'change'] },
  206. { type: 'string' , message: '只允许输入中英文、数字、-与_', pattern: /[\w\u4E00-\u9FA5\-]+$/g }
  207. ],
  208. manager: [
  209. { required: true, message: '管理员不能为空', trigger: 'blur' },
  210. { min: 1, max: 5, message: '长度在 1 到 5 个字符之间', trigger: ['blur', 'change'] },
  211. { type: 'string' , message: '只允许输入中英文、数字、-与_', pattern: /[\w\u4E00-\u9FA5\-]+$/g }
  212. ],
  213. selectedOptions: [
  214. { required: true, message: '省市区不能为空', trigger: 'blur' }
  215. ],
  216. location2: [
  217. { required: true, message: '具体地址不能为空', trigger: 'blur' },
  218. { type : 'string' , message: '只允许输入中英文、数字、_、!与!的输入', pattern: /[\w\u4E00-\u9FA5\!\!]+$/g }
  219. ],
  220. buildTime: [
  221. { required: true, message: '日期不能为空', trigger: 'blur' }
  222. ]
  223. }
  224. }
  225. },
  226. activated () {
  227. this.getDataList()
  228. },
  229. methods: {
  230. // 变色
  231. tableRowClassName({row, rowIndex}) {
  232. console.log(row);
  233. if (rowIndex%2 === 0) {
  234. return 'warning-row';
  235. }
  236. return '';
  237. },
  238. comLocation(location) {
  239. let arr = location.split(",")
  240. let loc = ''
  241. if (arr.length === 1) {
  242. return location
  243. }
  244. for (let i = 0; i < arr.length - 1; i++) {
  245. loc += CodeToText[arr[i]]
  246. }
  247. loc += arr[arr.length - 1]
  248. return loc
  249. },
  250. // 获取数据列表
  251. getDataList () {
  252. this.dataListLoading = true
  253. this.$http({
  254. url: this.$http.adornUrl('/management/farm/list'),
  255. method: 'post',
  256. params: this.$http.adornParams({
  257. page: this.pageIndex,
  258. limit: this.pageSize,
  259. keyword: this.dataForm.key
  260. })
  261. }).then(({ data }) => {
  262. if (data && data.code === 0) {
  263. this.dataList = data.page.list
  264. this.totalPage = data.page.totalCount
  265. } else {
  266. this.dataList = []
  267. this.totalPage = 0
  268. this.$message.error(data.msg)
  269. }
  270. this.dataListLoading = false
  271. })
  272. },
  273. // 每页数
  274. sizeChangeHandle (val) {
  275. this.pageSize = val
  276. this.pageIndex = 1
  277. this.getDataList()
  278. },
  279. // 获取地址
  280. getlocation () {
  281. var loc = "";
  282. this.form.location1 = ''
  283. for (let i = 0; i < this.form.selectedOptions.length; i++) {
  284. loc += CodeToText[this.form.selectedOptions[i]]
  285. this.form.location1 += this.form.selectedOptions[i] + ','
  286. }
  287. this.loc = loc
  288. console.log(loc);
  289. },
  290. // 当前页
  291. currentChangeHandle (val) {
  292. this.pageIndex = val
  293. this.getDataList()
  294. },
  295. // 新增 or 修改牧场
  296. addOrUpdateHandle (row) {
  297. // 显示牧场新增 or 修改面板
  298. this.visible = true
  299. if (row) {
  300. let location = row.location
  301. let arr = location.split(",")
  302. this.form.selectedOptions = []
  303. if (arr.length === 1) {
  304. return
  305. }
  306. this.form.location1 = ''
  307. for (let i = 0; i < arr.length - 1; i++) {
  308. this.form.selectedOptions.push(arr[i])
  309. this.form.location1 += arr[i] + ','
  310. }
  311. this.form.buildTime = row.buildTime
  312. this.form.id = row.id
  313. this.form.manager = row.manager
  314. this.form.name = row.name
  315. this.form.location2 = arr[arr.length - 1]
  316. }
  317. },
  318. // 选择n个牧场
  319. selectionChangeHandle (val) {
  320. this.selectionDataList = val
  321. // val.forEach(item => {
  322. // this.selectionDataList.push(item.id)
  323. // });
  324. },
  325. // 删除n个牧场
  326. deleteHandle (pasture) {
  327. let ids = pasture? [pasture.id]: this.selectionDataList.map(item => item.id)
  328. let names = pasture? [pasture.name]: this.selectionDataList.map(item => item.name)
  329. console.log(names);
  330. this.$confirm(`确定${pasture ? '删除' : '批量删除'}${names.join()}牧场?`, '提示', {
  331. confirmButtonText: '确定',
  332. cancelButtonText: '取消',
  333. type: 'warning'
  334. }).then(() => {
  335. // 删除操作
  336. if (this.selectionDataList.length < 0) {
  337. return
  338. }
  339. this.$http({
  340. url: this.$http.adornUrl('/management/farm/delete'),
  341. method: 'post',
  342. data: this.$http.adornData(ids, false)
  343. }).then(result => {
  344. if (result.data.code === 0) {
  345. this.$message({
  346. message: '成功删除牧场',
  347. type: 'success',
  348. duration: 1000
  349. })
  350. // pageIndex修正
  351. this.totalPage -= this.selectionDataList.length
  352. let pages = Math.ceil(this.totalPage / this.pageSize)
  353. this.pageIndex = this.pageIndex > pages? pages: this.pageIndex
  354. this.pageIndex = this.pageIndex < 1? pages: this.pageIndex
  355. } else {
  356. this.$confirm(result.data.msg, '删除失败', {
  357. confirmButtonText: '确定',
  358. cancelButtonText: '取消',
  359. type: 'warning'
  360. }).then(() => {
  361. console.log('牧场删除失败');
  362. }).catch(() => {})
  363. }
  364. this.resetForm()
  365. this.selectionDataList = []
  366. })
  367. }).catch(() => {})
  368. },
  369. formSubmit () {
  370. if (this.form.id) {
  371. /** 修改 */
  372. // 输入是否完整
  373. if (!this.confirmComplete()) {
  374. return
  375. }
  376. // 输入长度是否合法
  377. if (!this.confirmLength()) {
  378. return
  379. }
  380. // 输入是否合法
  381. if (!this.confirmInput()) {
  382. return
  383. }
  384. // 进行修改操作
  385. this.$http({
  386. url: this.$http.adornUrl('/management/farm/update'),
  387. method: 'post',
  388. data: this.$http.adornData({
  389. id: this.form.id,
  390. location: this.form.location,
  391. manager: this.form.manager,
  392. name: this.form.name,
  393. buildTime: this.form.buildTime,
  394. frontLocation: this.form.loc
  395. })
  396. }).then(result => {
  397. if (result.data.code === 0) {
  398. this.resetForm();
  399. this.visible = false;
  400. this.$message({
  401. message: '成功修改牧场信息',
  402. type: 'success',
  403. duration: 1000
  404. })
  405. } else {
  406. if (result.data.code === 600) {
  407. let msg = result.data.msg.split('-')
  408. this.$message({
  409. message: `牧场名称 <p style="color:#333; display:inline">${msg[0]}</p> 已经存在!`,
  410. type: "error",
  411. duration: 1000,
  412. dangerouslyUseHTMLString: true
  413. })
  414. return
  415. }
  416. this.$message.error(result.data.msg);
  417. }
  418. })
  419. } else {
  420. /** 新增 */
  421. // 输入是否完整
  422. if (!this.confirmComplete(1)) {
  423. return
  424. }
  425. // 输入长度是否合法
  426. if (!this.confirmLength()) {
  427. return
  428. }
  429. // 输入是否合法
  430. if (!this.confirmInput()) {
  431. return
  432. }
  433. // 进行新增操作
  434. this.$http({
  435. url: this.$http.adornUrl('/management/farm/save'),
  436. method: 'post',
  437. data: this.$http.adornData({
  438. location: this.form.location,
  439. manager: this.form.manager,
  440. name: this.form.name,
  441. buildTime: this.form.buildTime,
  442. frontLocation: this.form.loc
  443. })
  444. }).then(result => {
  445. if (result.data.code === 0) {
  446. // 新增pageIndex的修正
  447. this.totalPage++
  448. let pages = Math.ceil(this.totalPage / this.pageSize)
  449. this.pageIndex = this.pageIndex < pages? pages: this.pageIndex
  450. this.resetForm();
  451. this.visible = false;
  452. this.$message({
  453. message: '成功添加牧场',
  454. type: 'success',
  455. duration: 1000
  456. })
  457. } else {
  458. if (result.data.code === 600) {
  459. let msg = result.data.msg.split('-')
  460. this.$message({
  461. message: `牧场名称 <p style="color:#333; display:inline">${msg[0]}</p> 已经存在!`,
  462. type: "error",
  463. duration: 1000,
  464. dangerouslyUseHTMLString: true
  465. })
  466. return
  467. }
  468. this.$message.error(result.data.msg)
  469. }
  470. })
  471. }
  472. },
  473. // 清空form
  474. resetForm () {
  475. for (let i in this.form) {
  476. this.form[i] = ''
  477. }
  478. this.form.selectedOptions = []
  479. this.getDataList()
  480. },
  481. // 取消
  482. cancel () {
  483. this.visible = false
  484. // 如果新增,则不保留form
  485. if (this.form.id) {
  486. this.resetForm()
  487. }
  488. },
  489. // 校验输入是否完整
  490. validComplete (val) {
  491. // var loc = "";
  492. // this.form.location1 = ''
  493. // for (let i = 0; i < this.form.selectedOptions.length; i++) {
  494. // loc += CodeToText[this.form.selectedOptions[i]]
  495. // this.form.location1 += this.form.selectedOptions[i] + ','
  496. // }
  497. // this.loc = loc
  498. // this.form.location = this.form.location1 + this.form.location2
  499. // this.form.loc = this.loc + this.form.location2
  500. // val && (delete this.form.id)
  501. // for (let i in this.form) {
  502. // if (!this.form[i].toString()) {
  503. // return false
  504. // }
  505. // }
  506. // return true
  507. /** 给地址加上, */
  508. var loc = [];
  509. this.form.location1 = ''
  510. for (let i = 0; i < this.form.selectedOptions.length; i++) {
  511. loc.push(CodeToText[this.form.selectedOptions[i]])
  512. this.form.location1 += this.form.selectedOptions[i] + ','
  513. }
  514. this.loc = loc
  515. this.form.location = this.form.location1 + this.form.location2
  516. this.form.loc = loc.join(',') + ',' + this.form.location2
  517. val && (delete this.form.id)
  518. for (let i in this.form) {
  519. if (!this.form[i].toString()) {
  520. return false
  521. }
  522. }
  523. return true
  524. },
  525. // 如不完整,则提示
  526. confirmComplete (val) {
  527. if (this.validComplete(val)) {
  528. return true
  529. }
  530. this.$confirm(`所有项均为必填项`, '提示', {
  531. confirmButtonText: '确定',
  532. cancelButtonText: '取消',
  533. type: 'warning'
  534. }).then(() => {
  535. console.log('牧场输入不完整');
  536. }).catch(() => {})
  537. return false
  538. },
  539. // 校验输入是否合法
  540. validInput () {
  541. // 只允许中文、英文、数字、-、_的输入,取反
  542. const reg = /[^\w\u4E00-\u9FA5\_\-]/g
  543. if (reg.test(this.form.name)) {
  544. return 0
  545. }
  546. if (reg.test(this.form.manager)) {
  547. return 1
  548. }
  549. // 只允许中文、英文、下划线、!、!的输入
  550. const a = /[^\w\u4E00-\u9FA5\!\!]/g
  551. if (a.test(this.form.location2)) {
  552. return 2
  553. }
  554. // 不允许纯数字、字母的出现
  555. const b = /^[\w]*([a-zA-Z][0-9]|[0-9][a-zA-Z])[\w]*$/
  556. // 不允许纯符号的出现
  557. const c = /^[\_\-\!\!]*[\_\-\!\!]*$/
  558. if (b.test(this.form.name) || b.test(this.form.manager) || b.test(this.form.location2) || c.test(this.form.name) || c.test(this.form.manager) || c.test(this.form.location2)) {
  559. return 3
  560. }
  561. // 建立日期不得大于今日
  562. let build = new Date(this.form.buildTime)
  563. let now = new Date()
  564. if (build > now) {
  565. console.log('建立日期有误');
  566. return 4
  567. }
  568. return 5
  569. },
  570. // 如不合法,则提示
  571. confirmInput () {
  572. if (this.validInput() === 5) {
  573. return true
  574. }
  575. let msg = [
  576. '牧场名称只允许中英文、数字、-、_的输入',
  577. '管理员只允许中英文、数字、-、_的输入',
  578. '具体地址只允许中英文、数字、_、!与!的输入',
  579. '不允许输入纯数字、纯字母或纯符号',
  580. '建立日期选择有误'
  581. ]
  582. this.$confirm(msg[this.validInput()], '提示', {
  583. confirmButtonText: '确定',
  584. cancelButtonText: '取消',
  585. type: 'warning'
  586. }).then(() => {
  587. console.log('输入不合法');
  588. }).catch(() => {})
  589. return false
  590. },
  591. // 长度校验
  592. validLength () {
  593. if (this.form.name.length > 50) {
  594. return false
  595. }
  596. if (this.form.manager.length > 5) {
  597. return false
  598. }
  599. return true
  600. },
  601. // 如长度不合法,则提示
  602. confirmLength () {
  603. if (this.validLength()) {
  604. return true
  605. }
  606. this.$confirm(`牧场名称长度限制50,管理员姓名长度限制5`, '提示', {
  607. confirmButtonText: '确定',
  608. cancelButtonText: '取消',
  609. type: 'warning'
  610. }).then(() => {
  611. console.log('输入不合法');
  612. }).catch(() => {})
  613. return false
  614. }
  615. }
  616. }
  617. </script>
  618. <style scoped>
  619. .rect {
  620. background-color: #fff;
  621. padding: 30px 15px;
  622. border-radius: 5px;
  623. border: 1px solid #e8e8e8;
  624. }
  625. .rect-form {
  626. padding-bottom: 10px;
  627. }
  628. .rect-table {
  629. margin-top: 20px;
  630. }
  631. .demo-table-expand {
  632. font-size: 0;
  633. margin-left: 40px;
  634. }
  635. .demo-table-expand label {
  636. width: 90px;
  637. color: #99a9bf;
  638. }
  639. .demo-table-expand .el-form-item {
  640. margin-right: 0;
  641. margin-bottom: 0;
  642. width: 80%;
  643. }
  644. .el-table .height {
  645. background: rgba(254, 254, 254, 0.5);
  646. }
  647. /deep/ .el-table--mini td, .el-table--mini th {
  648. padding: 3px 0 !important;
  649. height: 20px !important;
  650. }
  651. /deep/ .el-checkbox__input.is-checked+.el-checkbox__label {
  652. color: rgb(24,144,255);
  653. background-color: rgb(24,144,255);
  654. border-color: rgb(24,144,255);
  655. }
  656. /deep/.el-table .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  657. background-color: rgb(24,144,255);
  658. border-color: rgb(24,144,255);
  659. }
  660. /deep/.el-table .el-checkbox__inner:hover {
  661. border-color: rgb(24,144,255);
  662. }
  663. /deep/.el-table .el-checkbox__input.is-focus .el-checkbox__inner {
  664. border-color: rgb(24,144,255);
  665. }
  666. /deep/.el-table #select .cell .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  667. background-color: rgb(24,144,255);
  668. border-color: rgb(24,144,255);
  669. }
  670. </style>