factory.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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="新增工厂" v-if="addFormVisible" :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" placeholder="请输入工厂名称"></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')" placeholder="请输入工厂地址"></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="编辑工厂" v-if="editFormVisible" :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" placeholder="请输入工厂名称"></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')" placeholder="请输入工厂地址"></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. pageNum: 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(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() {
  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() {
  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(index, row) {
  217. this.$confirm('确认删除该工厂吗?', '提示', {
  218. type: 'warning'
  219. }).then(() => {
  220. this.http.post(this.port.base.delFactory, {
  221. id: row.id
  222. }, res => {
  223. if (res.code == "ok") {
  224. this.$message({
  225. message: '删除成功',
  226. type: 'success'
  227. });
  228. this.getFactory();
  229. } else {
  230. this.$message({
  231. message: res.msg,
  232. type: 'error'
  233. });
  234. }
  235. }, error => {
  236. this.$message({
  237. message: error,
  238. type: 'error'
  239. });
  240. })
  241. });
  242. },
  243. //显示编辑界面
  244. handleEdit: function (index, row) {
  245. this.editFormVisible = true;
  246. this.noSub = false;
  247. this.editForm = {
  248. id: row.id,
  249. factoryName: row.factoryName,
  250. factoryArea: row.factoryArea,
  251. yLng: row.yLng,
  252. xLat: row.xLat,
  253. flag: 1
  254. };
  255. },
  256. //编辑
  257. editSubmit: function () {
  258. this.$refs.editForm.validate((valid) => {
  259. if (valid) {
  260. if(this.noSub){
  261. this.$message({
  262. message: '定位失败!',
  263. type: 'error'
  264. });
  265. } else {
  266. this.editLoading = true;
  267. this.http.post(this.port.base.addFactory, this.editForm , res => {
  268. this.editLoading = false;
  269. this.editFormVisible = false;
  270. if (res.code == "ok") {
  271. this.$message({
  272. message: '修改成功',
  273. type: 'success'
  274. });
  275. this.getFactory();
  276. } else {
  277. this.$message({
  278. message: res.msg,
  279. type: 'error'
  280. });
  281. }
  282. }, error => {
  283. this.editLoading = false;
  284. this.editFormVisible = false;
  285. this.$message({
  286. message: error,
  287. type: 'error'
  288. });
  289. })
  290. }
  291. }
  292. });
  293. },
  294. // 获取经纬度
  295. markLocation: function(address,mapId) {
  296. var _this = this;
  297. AMap.plugin('AMap.Geocoder', function() {
  298. var geocoder = new AMap.Geocoder();
  299. geocoder.getLocation(address, function(status, result) {
  300. if (status === 'complete' && result.info === 'OK') {
  301. // 经纬度
  302. var lng = result.geocodes[0].location.lng;
  303. var lat = result.geocodes[0].location.lat;
  304. _this.noSub = false;
  305. if(mapId == "addContainer") {
  306. _this.addForm.yLng = lng;
  307. _this.addForm.xLat = lat;
  308. } else {
  309. _this.editForm.yLng = lng;
  310. _this.editForm.xLat = lat;
  311. }
  312. // 地图实例
  313. var map = new AMap.Map(mapId, {
  314. resizeEnable: true, // 允许缩放
  315. center: [lng, lat], // 设置地图的中心点
  316. zoom: 15        // 设置地图的缩放级别,0 - 20
  317. });
  318. // 添加标记
  319. var marker = new AMap.Marker({
  320. map: map,
  321. position: new AMap.LngLat(lng, lat), // 经纬度
  322. });
  323. } else {
  324. _this.noSub = true;
  325. //console.log('定位失败!');
  326. }
  327. });
  328. });
  329. }
  330. },
  331. created() {
  332. let height = window.innerHeight;
  333. this.tableHeight = height - 210;
  334. const that = this;
  335. window.onresize = function temp() {
  336. that.tableHeight = window.innerHeight - 210;
  337. };
  338. },
  339. mounted() {
  340. this.getFactory();
  341. }
  342. }
  343. </script>
  344. <style scoped>
  345. .formMap {
  346. height: 400px;
  347. }
  348. </style>