factory.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <section>
  3. <!--工具条-->
  4. <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
  5. <el-form :inline="true" :model="filters">
  6. <el-form-item>
  7. <el-input v-model="filters.keyName" placeholder="请输入工厂名称进行搜索" clearable></el-input>
  8. </el-form-item>
  9. <el-form-item>
  10. <el-button type="primary" @click.native="getFactory">查询</el-button>
  11. </el-form-item>
  12. <el-form-item style="float:right;">
  13. <el-button type="primary" @click.native="handleAdd">新增</el-button>
  14. </el-form-item>
  15. </el-form>
  16. </el-col>
  17. <!--列表-->
  18. <el-table :data="list" highlight-current-row :height="tableHeight" v-loading="listLoading" style="width: 100%;">
  19. <el-table-column type="index" width="60"></el-table-column>
  20. <el-table-column prop="factoryName" label="工厂名称" width="300" sortable></el-table-column>
  21. <el-table-column prop="factoryArea" label="工厂地址" sortable></el-table-column>
  22. <el-table-column label="操作" width="150">
  23. <template slot-scope="scope">
  24. <el-button size="small" @click.native="handleEdit(scope.$index, scope.row)">编辑</el-button>
  25. <el-button type="danger" size="small" @click.native.native="handleDel(scope.$index, scope.row)">删除</el-button>
  26. </template>
  27. </el-table-column>
  28. </el-table>
  29. <!--工具条-->
  30. <el-col :span="24" class="toolbar">
  31. <el-pagination
  32. @size-change="handleSizeChange"
  33. @current-change="handleCurrentChange"
  34. :page-sizes="[20 , 50 , 80 , 100]"
  35. :page-size="20"
  36. layout="total, sizes, prev, pager, next"
  37. :total="total"
  38. style="float:right;">
  39. </el-pagination>
  40. </el-col>
  41. <!--新增界面-->
  42. <el-dialog title="新增工厂" :visible.sync="addFormVisible" :close-on-click-modal="false" customClass='customWidth'>
  43. <el-form :model="addForm" label-width="100px" :rules="formRules" ref="addForm" :inline="true" class="demo-form-inline">
  44. <el-form-item label="工厂名称" prop="factoryName">
  45. <el-input v-model="addForm.factoryName" autocomplete="off"></el-input>
  46. </el-form-item>
  47. <el-form-item label="工厂地址" prop="factoryArea">
  48. <el-input v-model="addForm.factoryArea" autocomplete="off" :change="changeFactoryArea('addContainer')"></el-input>
  49. </el-form-item>
  50. <div id="addContainer" class="formMap" v-if="addForm.factoryArea != ''"></div>
  51. </el-form>
  52. <div slot="footer" class="dialog-footer">
  53. <el-button @click.native="addFormVisible = false">取消</el-button>
  54. <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
  55. </div>
  56. </el-dialog>
  57. <!--编辑界面-->
  58. <el-dialog title="编辑工厂" :visible.sync="editFormVisible" :close-on-click-modal="false" customClass='customWidth'>
  59. <el-form :model="editForm" label-width="100px" :rules="formRules" ref="editForm" :inline="true" class="demo-form-inline">
  60. <el-form-item label="工厂名称" prop="factoryName">
  61. <el-input v-model="editForm.factoryName" autocomplete="off"></el-input>
  62. </el-form-item>
  63. <el-form-item label="工厂地址" prop="factoryArea">
  64. <el-input v-model="editForm.factoryArea" autocomplete="off" :change="changeFactoryArea('editContainer')"></el-input>
  65. </el-form-item>
  66. <div id="editContainer" class="formMap" v-if="editForm.factoryArea != ''"></div>
  67. </el-form>
  68. <div slot="footer" class="dialog-footer">
  69. <el-button @click.native="editFormVisible = false">取消</el-button>
  70. <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
  71. </div>
  72. </el-dialog>
  73. </section>
  74. </template>
  75. <script>
  76. import util from '../../common/js/util'
  77. export default {
  78. data() {
  79. return {
  80. filters: {
  81. keyName: ''
  82. },
  83. list: [],
  84. total: 0,
  85. page: 1,
  86. size: 20,
  87. listLoading: false,
  88. tableHeight: 0,
  89. noSub: false,
  90. formRules: {
  91. factoryName: [
  92. { required: true, message: '请输入公司名称', trigger: 'blur' }
  93. ],
  94. factoryArea: [
  95. { required: true, message: '请输入公司地址', trigger: 'blur' }
  96. ]
  97. },
  98. // 新增界面
  99. addFormVisible: false,
  100. addLoading: false,
  101. addForm: {
  102. factoryName: '',
  103. factoryArea: '',
  104. yLng: 0,
  105. xLat: 0,
  106. flag: 0
  107. },
  108. // 编辑界面
  109. editFormVisible: false,
  110. editLoading: false,
  111. editForm: {
  112. id: 0,
  113. factoryName: '',
  114. factoryArea: '',
  115. yLng: 0,
  116. xLat: 0,
  117. flag: 1
  118. }
  119. }
  120. },
  121. methods: {
  122. // 分页
  123. handleCurrentChange(val) {
  124. this.page = val;
  125. this.getFactory();
  126. },
  127. handleSizeChange(val) {
  128. this.size = val;
  129. this.getFactory();
  130. },
  131. //获取用户列表
  132. getFactory() {
  133. this.listLoading = true;
  134. this.http.post(this.port.base.factoryList, {
  135. keyName: this.filters.keyName,
  136. currentPage: this.page,
  137. pageSize: this.size
  138. }, res => {
  139. this.listLoading = false;
  140. if (res.code == "ok") {
  141. this.list = res.data.list;
  142. this.total = res.data.total;
  143. } else {
  144. this.$message({
  145. message: res.msg,
  146. type: 'error'
  147. });
  148. }
  149. }, error => {
  150. this.listLoading = false;
  151. this.$message({
  152. message: error,
  153. type: 'error'
  154. });
  155. })
  156. },
  157. //地址输入切换
  158. changeFactoryArea: function (mapId) {
  159. if(mapId == "addContainer"){
  160. this.markLocation(this.addForm.factoryArea, mapId);
  161. } else {
  162. this.markLocation(this.editForm.factoryArea, mapId);
  163. }
  164. },
  165. //显示新增界面
  166. handleAdd: function () {
  167. this.addFormVisible = true;
  168. this.noSub = false;
  169. this.addForm = {
  170. factoryName: '',
  171. factoryArea: '',
  172. yLng: 0,
  173. xLat: 0,
  174. flag: 0
  175. };
  176. },
  177. //新增
  178. addSubmit: function () {
  179. this.$refs.addForm.validate((valid) => {
  180. if (valid) {
  181. if(this.noSub){
  182. this.$message({
  183. message: '定位失败!',
  184. type: 'error'
  185. });
  186. } else {
  187. this.addLoading = true;
  188. this.http.post(this.port.base.addFactory, this.addForm , res => {
  189. this.addLoading = false;
  190. this.addFormVisible = false;
  191. if (res.code == "ok") {
  192. this.$message({
  193. message: '创建成功',
  194. type: 'success'
  195. });
  196. this.getFactory();
  197. } else {
  198. this.$message({
  199. message: res.msg,
  200. type: 'error'
  201. });
  202. }
  203. }, error => {
  204. this.addLoading = false;
  205. this.addFormVisible = false;
  206. this.$message({
  207. message: error,
  208. type: 'error'
  209. });
  210. })
  211. }
  212. }
  213. });
  214. },
  215. //删除
  216. handleDel: function (index, row) {
  217. this.$confirm('确认删除该工厂吗?', '提示', {
  218. type: 'warning'
  219. }).then(() => {
  220. var _this = this;
  221. this.addLoading = true;
  222. this.http.post(this.port.base.delFactory, {
  223. id: row.id
  224. }, res => {
  225. if (res.code == "ok") {
  226. this.$message({
  227. message: '删除成功',
  228. type: 'success'
  229. });
  230. this.getFactory();
  231. } else {
  232. this.$message({
  233. message: res.msg,
  234. type: 'error'
  235. });
  236. }
  237. }, error => {
  238. this.$message({
  239. message: error,
  240. type: 'error'
  241. });
  242. })
  243. });
  244. },
  245. //显示编辑界面
  246. handleEdit: function (index, row) {
  247. this.editFormVisible = true;
  248. this.noSub = false;
  249. this.editForm = {
  250. id: row.id,
  251. factoryName: row.factoryName,
  252. factoryArea: row.factoryArea,
  253. yLng: row.yLng,
  254. xLat: row.xLat,
  255. flag: 1
  256. };
  257. },
  258. //编辑
  259. editSubmit: function () {
  260. this.$refs.editForm.validate((valid) => {
  261. if (valid) {
  262. if(this.noSub){
  263. this.$message({
  264. message: '定位失败!',
  265. type: 'error'
  266. });
  267. } else {
  268. this.editLoading = true;
  269. this.http.post(this.port.base.addFactory, this.editForm , res => {
  270. this.editLoading = false;
  271. this.editFormVisible = false;
  272. if (res.code == "ok") {
  273. this.$message({
  274. message: '修改成功',
  275. type: 'success'
  276. });
  277. this.getFactory();
  278. } else {
  279. this.$message({
  280. message: res.msg,
  281. type: 'error'
  282. });
  283. }
  284. }, error => {
  285. this.editLoading = false;
  286. this.editFormVisible = false;
  287. this.$message({
  288. message: error,
  289. type: 'error'
  290. });
  291. })
  292. }
  293. }
  294. });
  295. },
  296. // 获取经纬度
  297. markLocation: function(address,mapId) {
  298. var _this = this;
  299. AMap.plugin('AMap.Geocoder', function() {
  300. var geocoder = new AMap.Geocoder();
  301. geocoder.getLocation(address, function(status, result) {
  302. if (status === 'complete' && result.info === 'OK') {
  303. // 经纬度
  304. var lng = result.geocodes[0].location.lng;
  305. var lat = result.geocodes[0].location.lat;
  306. _this.noSub = false;
  307. if(mapId == "addContainer") {
  308. _this.addForm.yLng = lng;
  309. _this.addForm.xLat = lat;
  310. } else {
  311. _this.editForm.yLng = lng;
  312. _this.editForm.xLat = lat;
  313. }
  314. // 地图实例
  315. var map = new AMap.Map(mapId, {
  316. resizeEnable: true, // 允许缩放
  317. center: [lng, lat], // 设置地图的中心点
  318. zoom: 15        // 设置地图的缩放级别,0 - 20
  319. });
  320. // 添加标记
  321. var marker = new AMap.Marker({
  322. map: map,
  323. position: new AMap.LngLat(lng, lat), // 经纬度
  324. });
  325. } else {
  326. _this.noSub = true;
  327. //console.log('定位失败!');
  328. }
  329. });
  330. });
  331. }
  332. },
  333. created() {
  334. let height = window.innerHeight;
  335. this.tableHeight = height - 240;
  336. },
  337. mounted() {
  338. this.getFactory();
  339. }
  340. }
  341. </script>
  342. <style scoped>
  343. .formMap {
  344. height: 400px;
  345. }
  346. </style>