FarmAdmin.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <div style="box-sizing: border-box; padding: 20px;">
  3. <new-table :height="600" :title="title" :listData="farmList" :tableItems="tableItems" :shows="tableShows" @selectionChange="selectionChange">
  4. <template #rowStatus="scope">
  5. <el-switch v-model="scope.row.rowStatus" @change="handleSwitch($event, scope.row)"></el-switch>
  6. </template>
  7. <template #right>
  8. <el-button size="mini" type="primary" v-if="hasPerm('farmAdmin:add')" style="margin-right: 10px" @click="addFarm">添加牧场</el-button>
  9. <el-button size="mini" type="danger" v-if="hasPerm('farmAdmin:del')" style="margin-right: 10px" @click="delAll">批量删除</el-button>
  10. </template>
  11. <template #handler="scope">
  12. <el-button type="primary" size="mini" style="margin-right: 10px" @click="edit(scope.row)" v-if="hasPerm('farmAdmin:edit')">编辑</el-button>
  13. <el-popconfirm
  14. title="是否删除这个牧场"
  15. @confirm="del(scope.row)"
  16. >
  17. <el-button size="mini" slot="reference" v-if="hasPerm('farmAdmin:del')" type="danger">删除</el-button>
  18. </el-popconfirm>
  19. </template>
  20. </new-table>
  21. <el-dialog
  22. :title=" showType ? '编辑牧场' : '新增牧场'"
  23. :visible.sync="dialogVisible"
  24. width="30%">
  25. <div>
  26. <el-form :rules="rules" ref="ruleForm" size="mini" :model="form" label-width="80px">
  27. <el-form-item label="牧场名称" prop="farmName">
  28. <el-input v-model="form.farmName"></el-input>
  29. </el-form-item>
  30. <el-form-item label="省市区" prop="selectedOptions">
  31. <el-cascader
  32. :options="options"
  33. v-model="form.selectedOptions"
  34. @change="getlocation"
  35. placeholder="请选择省市区"
  36. style="width: 100%">
  37. </el-cascader>
  38. </el-form-item>
  39. </el-form>
  40. </div>
  41. <span slot="footer" class="dialog-footer">
  42. <el-button @click="reset">取 消</el-button>
  43. <el-button type="primary" @click="onSubmit('ruleForm')">确 定</el-button>
  44. </span>
  45. </el-dialog>
  46. </div>
  47. </template>
  48. <script>
  49. import { regionData, CodeToText } from "element-china-area-data"
  50. import { addFarm, editFarm, delFarm } from '@/utils/api.js';
  51. import NewTable from "@/components/newTable/NewTable";
  52. import { mapState, mapActions } from 'vuex';
  53. export default {
  54. name: "FarmAdmin",
  55. components: {
  56. NewTable
  57. },
  58. computed: {
  59. ...mapState(['farmList'])
  60. },
  61. data() {
  62. return {
  63. title: '牧场列表',
  64. options: regionData,
  65. tableItems: [
  66. {
  67. prop: 'farmName',
  68. label: '牧场名称',
  69. minWidth: '100',
  70. slotName: 'farmName'
  71. },
  72. {
  73. prop: 'frontLocation',
  74. label: '牧场地址',
  75. minWidth: '100',
  76. slotName: 'frontLocation'
  77. },
  78. {
  79. prop: 'rowStatus',
  80. label: '是否启用',
  81. minWidth: '100',
  82. slotName: 'rowStatus'
  83. },
  84. {
  85. label: '操作',
  86. minWidth: '100',
  87. slotName: 'handler'
  88. }
  89. ],
  90. tableShows: {
  91. showIndex: true,
  92. showSelect: true,
  93. },
  94. showType: false,
  95. dialogVisible: false,
  96. form: {
  97. farmName: '',
  98. selectedOptions: ''
  99. },
  100. rules: {
  101. farmName: [{ required: true, message: '请输入牧场名称', trigger: 'blur' }],
  102. selectedOptions: [{ required: true, message: '请输入牧场名称', trigger: 'change' }],
  103. },
  104. select: [],
  105. }
  106. },
  107. methods: {
  108. ...mapActions(['GetFarm']),
  109. init() {
  110. this.GetFarm();
  111. },
  112. addFarm() {
  113. this.dialogVisible = true;
  114. this.showType = false;
  115. },
  116. edit(row) {
  117. this.dialogVisible = true;
  118. this.showType = true;
  119. this.form.farmName = row.farmName;
  120. this.form.id = row.id;
  121. this.form.selectedOptions = row.location.split(',')
  122. },
  123. // 批量选择
  124. selectionChange(data) {
  125. this.select = [];
  126. if(data.length > 0) {
  127. data.forEach(item => {
  128. this.select.push(item.id)
  129. })
  130. } else {
  131. this.select = []
  132. }
  133. },
  134. delAll() {
  135. if(this.select.length > 0) {
  136. let str = this.select.toString();
  137. let params = {
  138. ids: str
  139. }
  140. this.$confirm('此操作将永久删除这些牧场, 是否继续?', '提示', {
  141. confirmButtonText: '确定',
  142. cancelButtonText: '取消',
  143. type: 'warning'
  144. }).then(() => {
  145. delFarm(params).then(res => {
  146. if(res.code === 10000) {
  147. this.$message.success('删除成功!');
  148. this.init();
  149. this.select = [];
  150. } else {
  151. this.$message.error('删除失败!');
  152. }
  153. })
  154. }).catch(() => {
  155. this.$message({
  156. type: 'info',
  157. message: '已取消删除'
  158. });
  159. });
  160. } else {
  161. this.$message.error('请先选择要删除的牧场');
  162. }
  163. },
  164. del(row) {
  165. let params = {
  166. ids: row.id
  167. }
  168. delFarm(params).then(res => {
  169. if(res.code === 10000) {
  170. this.$message.success('删除成功')
  171. this.init()
  172. } else {
  173. this.$message.error('删除失败')
  174. }
  175. })
  176. },
  177. // 确认
  178. onSubmit(formName) {
  179. this.$refs[formName].validate((valid) => {
  180. if (valid) {
  181. if(this.showType) {
  182. let params = {
  183. id: this.form.id,
  184. farmName: this.form.farmName,
  185. location: this.form.selectedOptions.join(','),
  186. frontLocation: this.loc
  187. }
  188. editFarm(params).then(res => {
  189. this.reset();
  190. if(res.code === 10000) {
  191. this.init();
  192. this.$message.success('编辑成功!')
  193. } else {
  194. this.$message.error('编辑失败');
  195. }
  196. })
  197. } else {
  198. let params = {
  199. farmName: this.form.farmName,
  200. location: this.form.selectedOptions.join(','),
  201. frontLocation: this.loc
  202. }
  203. addFarm(params).then(res => {
  204. this.reset();
  205. if(res.code === 10000) {
  206. this.init();
  207. this.$message.success('添加成功!')
  208. } else {
  209. this.$message.error('添加失败');
  210. }
  211. })
  212. }
  213. } else {
  214. console.log('error submit!!');
  215. return false;
  216. }
  217. });
  218. },
  219. // 重置
  220. reset() {
  221. this.dialogVisible = false;
  222. this.showType = false;
  223. this.form = {
  224. farmName: '',
  225. selectedOptions: ''
  226. }
  227. this.loc = ''
  228. },
  229. handleSwitch(val, data) {
  230. let params = {
  231. id: data.id,
  232. rowStatus: val
  233. }
  234. editFarm(params).then(res => {
  235. this.reset();
  236. if(res.code === 10000) {
  237. this.$message.success(res.message)
  238. } else {
  239. this.$message.error(res.message);
  240. }
  241. })
  242. },
  243. // 获取地址
  244. getlocation () {
  245. var loc = [];
  246. for (let i = 0; i < this.form.selectedOptions.length; i++) {
  247. loc.push(CodeToText[this.form.selectedOptions[i]])
  248. }
  249. this.loc = loc.join(',')
  250. },
  251. },
  252. mounted() {
  253. // this.init()
  254. }
  255. }
  256. </script>
  257. <style scoped>
  258. </style>