parkManage.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // pages/parkMange/parkManage.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. active: 1, // 1停车区 2禁停区
  9. checked: false, //switch 选择
  10. parkList: [], //停车区列表
  11. noParkList: [], //禁停区列表
  12. showArea: false, //显示所属大区选择框
  13. showType: false, //显示区域类型
  14. areaList: [], //所属大区选项
  15. areaName: '', //大区名称
  16. typeList: [{ //区域类型选项
  17. type: 1,
  18. name: '禁停区'
  19. }, {
  20. type: 2,
  21. name: '停车区'
  22. }],
  23. typeName: '', //区域类型名称
  24. type: '', //区域类型
  25. parkName: '', //停车区域名称
  26. searchName: '', //搜索框停车区域名称
  27. parkNum: 0, //停车上限
  28. areaChecked: true, //默认区域状态
  29. showPop: false, //显示弹窗
  30. option1: [{
  31. text: '全部区域',
  32. value: 0,
  33. areaID: ''
  34. }],
  35. value1: 0,
  36. areaId: '', //弹窗区域id
  37. area_id: '', //全部区域id
  38. // status: true, //true为正常显示,false为显示删除按钮
  39. editShow: false, //显示 绘制围栏(false) || 保存(save)
  40. parkId: '',
  41. },
  42. /**
  43. * 生命周期函数--监听页面加载
  44. */
  45. onLoad: function(options) {
  46. if (options.type == 1) {
  47. this.setData({
  48. active: 2
  49. })
  50. } else if (options.type == 2) {
  51. this.setData({
  52. active: 1
  53. })
  54. }
  55. this.getAreas();
  56. let that = this;
  57. app.request('index', '', 'GET').then(res => {
  58. if (res.statusCode == 200) {
  59. var data = res.data
  60. for (var i = 0; i < data.wx_area.length; i++) {
  61. data.wx_area[i].value = i + 1
  62. }
  63. this.setData({
  64. areaList: res.data.wx_area,
  65. option1: that.data.option1.concat(data.wx_area)
  66. })
  67. console.log(that.data.areaList)
  68. }
  69. })
  70. },
  71. // 编辑停车区
  72. edit(e) {
  73. console.log(e);
  74. let type = e.currentTarget.dataset.item.type;
  75. let typeName = ''
  76. if (type == 1) {
  77. typeName = '禁停区'
  78. } else if (type == 2) {
  79. typeName = "停车区"
  80. }
  81. this.setData({
  82. showPop: true,
  83. editShow: true,
  84. parkId: e.currentTarget.dataset.item.id,
  85. areaName: e.currentTarget.dataset.item.area_name,
  86. parkName: e.currentTarget.dataset.item.name,
  87. typeName: typeName,
  88. areaChecked: e.currentTarget.dataset.item.status,
  89. parkNum: e.currentTarget.dataset.item.max_number,
  90. areaId: e.currentTarget.dataset.item.area_id,
  91. type: e.currentTarget.dataset.item.type
  92. })
  93. },
  94. //搜索输入停车点名称
  95. nameInp(e) {
  96. this.setData({
  97. searchName: e.detail.value
  98. })
  99. if (!this.data.searchName) {
  100. this.getAreas();
  101. }
  102. },
  103. //搜索
  104. seek() {
  105. let that = this;
  106. console.log(this.data.searchName)
  107. if (!this.data.searchName) {
  108. wx.showToast({
  109. title: '请输入停车点名称',
  110. icon: 'none'
  111. })
  112. } else {
  113. let type = ''
  114. if (this.data.active == 1) {
  115. type = 2
  116. } else {
  117. type = 1
  118. }
  119. let data = {
  120. name: that.data.searchName,
  121. type: type
  122. }
  123. app.request('parking', data, 'GET').then(res => {
  124. console.log(res)
  125. if (that.data.active == 1) {
  126. that.setData({
  127. parkList: res.data
  128. })
  129. } else if (that.data.active == 2) {
  130. that.setData({
  131. noParkList: res.data
  132. })
  133. }
  134. })
  135. }
  136. },
  137. // 删除停车区
  138. remove(e) {
  139. let that = this;
  140. let parkId = e.currentTarget.dataset.item.id;
  141. let type = e.currentTarget.dataset.item.type;
  142. let name = ''
  143. if (type == 1) {
  144. name = '禁停区'
  145. } else if (type == 2) {
  146. name = '停车区'
  147. }
  148. wx.showModal({
  149. title: '提示',
  150. content: '确定要删除【' + e.currentTarget.dataset.item.name + '】' + name + '?',
  151. success(res) {
  152. if (res.confirm) {
  153. app.request('parking/' + parkId, '', 'DELETE').then(res => {
  154. if (res.statusCode == 204) {
  155. wx.showToast({
  156. title: '删除成功',
  157. icon: 'none',
  158. duration: 2000,
  159. success: function() {
  160. setTimeout(function() {
  161. that.getAreas();
  162. }, 2000)
  163. }
  164. })
  165. }
  166. })
  167. } else if (res.cancel) {
  168. wx.showToast({
  169. title: '取消成功',
  170. icon: 'none'
  171. })
  172. }
  173. }
  174. })
  175. },
  176. //手指触摸动作开始 记录起点X坐标
  177. touchstart: function(e) {
  178. //开始触摸时 重置所有删除
  179. this.data.parkList.forEach(function(v, i) {
  180. if (v.isTouchMove) //只操作为true的
  181. v.isTouchMove = false;
  182. })
  183. this.setData({
  184. startX: e.changedTouches[0].clientX,
  185. startY: e.changedTouches[0].clientY,
  186. parkList: this.data.parkList
  187. })
  188. },
  189. //滑动事件处理
  190. touchmove: function(e) {
  191. var that = this,
  192. index = e.currentTarget.dataset.index, //当前索引
  193. startX = that.data.startX, //开始X坐标
  194. startY = that.data.startY, //开始Y坐标
  195. touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
  196. touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
  197. //获取滑动角度
  198. angle = that.angle({
  199. X: startX,
  200. Y: startY
  201. }, {
  202. X: touchMoveX,
  203. Y: touchMoveY
  204. });
  205. that.data.parkList.forEach(function(v, i) {
  206. v.isTouchMove = false
  207. //滑动超过30度角 return
  208. if (Math.abs(angle) > 30) return;
  209. if (i == index) {
  210. if (touchMoveX > startX) //右滑
  211. v.isTouchMove = false
  212. else //左滑
  213. v.isTouchMove = true
  214. }
  215. })
  216. //更新数据
  217. that.setData({
  218. parkList: that.data.parkList
  219. })
  220. },
  221. //手指触摸动作开始 记录起点X坐标
  222. touchstart1: function(e) {
  223. //开始触摸时 重置所有删除
  224. this.data.noParkList.forEach(function(v, i) {
  225. if (v.isTouchMove) //只操作为true的
  226. v.isTouchMove = false;
  227. })
  228. this.setData({
  229. startX: e.changedTouches[0].clientX,
  230. startY: e.changedTouches[0].clientY,
  231. noParkList: this.data.noParkList
  232. })
  233. },
  234. //滑动事件处理
  235. touchmove1: function(e) {
  236. var that = this,
  237. index = e.currentTarget.dataset.index, //当前索引
  238. startX = that.data.startX, //开始X坐标
  239. startY = that.data.startY, //开始Y坐标
  240. touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
  241. touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
  242. //获取滑动角度
  243. angle = that.angle({
  244. X: startX,
  245. Y: startY
  246. }, {
  247. X: touchMoveX,
  248. Y: touchMoveY
  249. });
  250. that.data.noParkList.forEach(function(v, i) {
  251. v.isTouchMove = false
  252. //滑动超过30度角 return
  253. if (Math.abs(angle) > 30) return;
  254. if (i == index) {
  255. if (touchMoveX > startX) //右滑
  256. v.isTouchMove = false
  257. else //左滑
  258. v.isTouchMove = true
  259. }
  260. })
  261. //更新数据
  262. that.setData({
  263. noParkList: that.data.noParkList
  264. })
  265. },
  266. /**
  267. * 计算滑动角度
  268. * @param {Object} start 起点坐标
  269. * @param {Object} end 终点坐标
  270. */
  271. angle: function(start, end) {
  272. var dX = end.X - start.X,
  273. dY = end.Y - start.Y
  274. //返回角度 /Math.atan()返回数字的反正切值
  275. return 360 * Math.atan(dY / dX) / (2 * Math.PI);
  276. },
  277. //tab上方选择区域
  278. change: function(e) {
  279. wx.showLoading({
  280. title: '加载中',
  281. })
  282. var that = this;
  283. that.setData({
  284. area_id: that.data.option1[e.detail].areaID,
  285. searchName: ''
  286. })
  287. let data = '';
  288. let url = '';
  289. let tabType = ''
  290. if (that.data.active == 1) {
  291. tabType = 2
  292. } else if (that.data.active == 2) {
  293. tabType = 1
  294. }
  295. this.getAreas();
  296. },
  297. // 选择所属大区
  298. choiceArea() {
  299. this.setData({
  300. showArea: !this.data.showArea
  301. })
  302. },
  303. //获取所属大区名
  304. getAreaName(e) {
  305. this.setData({
  306. showArea: false,
  307. areaName: e.currentTarget.dataset.name,
  308. areaId: e.currentTarget.dataset.id
  309. })
  310. },
  311. //停车区域名称
  312. bindKeyInput(e) {
  313. this.setData({
  314. parkName: e.detail.value
  315. })
  316. },
  317. //选择区域类型
  318. choiceType() {
  319. this.setData({
  320. showType: !this.data.showType
  321. })
  322. },
  323. // 获取区域名
  324. getTypeName(e) {
  325. console.log(e);
  326. this.setData({
  327. showType: false,
  328. typeName: e.currentTarget.dataset.idx.name,
  329. type: e.currentTarget.dataset.idx.type
  330. })
  331. },
  332. // 停车上限
  333. numInput(e) {
  334. this.setData({
  335. parkNum: e.detail.value
  336. })
  337. },
  338. // 改变区域状态
  339. changeState() {
  340. this.setData({
  341. areaChecked: !this.data.areaChecked
  342. })
  343. },
  344. // 切换tab
  345. choice(e) {
  346. let that = this;
  347. console.log(that.data)
  348. console.log(that.data.area_id, 5555)
  349. that.setData({
  350. active: e.currentTarget.dataset.idx,
  351. })
  352. that.getAreas();
  353. },
  354. //添加停车区
  355. addPark() {
  356. let that = this;
  357. that.setData({
  358. areaName: '',
  359. parkName: '',
  360. parkNum: 0,
  361. areaChecked: true,
  362. editShow:false,
  363. })
  364. if (that.data.active == 1) {
  365. this.setData({
  366. typeName: '停车区',
  367. type: 2,
  368. })
  369. } else if (that.data.active == 2) {
  370. that.setData({
  371. typeName: '禁停区',
  372. type: 1
  373. })
  374. }
  375. this.setData({
  376. showPop: true
  377. })
  378. },
  379. // 获取所有停车区
  380. getAreas() {
  381. wx.showLoading({
  382. title: '加载中',
  383. })
  384. let that = this;
  385. let data = '';
  386. let url = '';
  387. let tabType = ''
  388. if (that.data.active == 1) {
  389. tabType = 2
  390. } else if (that.data.active == 2) {
  391. tabType = 1
  392. }
  393. if (that.data.area_id == '') {
  394. url = 'parking?type=' + tabType
  395. } else {
  396. url = 'parking?type=' + tabType + "&&area_id=" + that.data.area_id
  397. }
  398. app.request(url, '', 'GET').then(res => {
  399. if (res.statusCode == 200) {
  400. wx.hideLoading();
  401. if (that.data.active == 1) {
  402. that.setData({
  403. parkList: res.data
  404. })
  405. } else if (that.data.active == 2) {
  406. that.setData({
  407. noParkList: res.data
  408. })
  409. }
  410. }
  411. })
  412. },
  413. //控制开关按钮
  414. switchCon(e) {
  415. let that = this;
  416. let len = e.currentTarget.dataset.idx;
  417. let arr = '';
  418. if (that.data.active == 1) {
  419. arr = that.data.parkList;
  420. } else if (that.data.active == 2) {
  421. arr = that.data.noParkList
  422. }
  423. arr[len].status = !arr[len].status
  424. let park_id = e.currentTarget.dataset.id; //停车区id
  425. let status = '' //开关状态
  426. if (that.data.active == 1) {
  427. that.setData({
  428. parkList: arr
  429. })
  430. } else if (that.data.active == 2) {
  431. that.setData({
  432. noParkList: arr
  433. })
  434. }
  435. if (arr[len].status == false) {
  436. status = 0
  437. } else if (arr[len].status == true) {
  438. status = 1
  439. }
  440. let data = {
  441. status: status,
  442. id: park_id
  443. }
  444. app.request('parking/status', data, 'POST').then(res => {
  445. if (res.statusCode == 200) {
  446. wx.showToast({
  447. title: '修改成功',
  448. icon: 'none'
  449. })
  450. }
  451. })
  452. },
  453. //绘制围栏
  454. draw() {
  455. let that = this;
  456. if (!that.data.areaName) {
  457. wx.showToast({
  458. title: '请选择所属大区',
  459. icon: 'none'
  460. })
  461. } else if (!that.data.parkName) {
  462. wx.showToast({
  463. title: '停车区域名称不能为空',
  464. icon: 'none'
  465. })
  466. } else if (!that.data.typeName) {
  467. wx.showToast({
  468. title: '请选择区域类型',
  469. icon: 'none'
  470. })
  471. } else if (that.data.parkNum === '') {
  472. wx.showToast({
  473. title: '请输入停车上限',
  474. icon: 'none'
  475. })
  476. } else {
  477. if (that.data.editShow == false) { //绘制围栏
  478. wx.navigateTo({
  479. url: '/pages/parkArea/parkArea?area_id=' + that.data.areaId + "&&max_number=" + that.data.parkNum + "&&name=" + that.data.parkName + "&&status=" + that.data.areaChecked + "&&type=" + that.data.type,
  480. })
  481. } else if (that.data.editShow == true) { //保存
  482. let state = JSON.parse(that.data.areaChecked);
  483. if (state) {
  484. state = 1
  485. } else {
  486. state = 0
  487. }
  488. let data = {
  489. area_id: that.data.areaId,
  490. max_number: that.data.parkNum,
  491. name: that.data.parkName,
  492. status: state,
  493. type: that.data.type,
  494. }
  495. app.request('parking/' + that.data.parkId, data, 'PUT').then(res => {
  496. if (res.statusCode == 200) {
  497. console.log(that.data.status)
  498. wx.showToast({
  499. title: '保存成功',
  500. icon: 'none',
  501. duration: 2000,
  502. success: function() {
  503. setTimeout(function() {
  504. that.setData({
  505. showPop: false
  506. })
  507. that.getAreas();
  508. }, 2000)
  509. }
  510. })
  511. } else if (res.statusCode == 422) {
  512. wx.showToast({
  513. title: res.data.errors.name[0],
  514. icon: 'none'
  515. })
  516. }
  517. })
  518. }
  519. }
  520. },
  521. //取消
  522. cancel() {
  523. this.setData({
  524. showPop: false
  525. })
  526. },
  527. /**
  528. * 生命周期函数--监听页面初次渲染完成
  529. */
  530. onReady: function() {
  531. },
  532. /**
  533. * 生命周期函数--监听页面显示
  534. */
  535. onShow: function() {
  536. },
  537. /**
  538. * 生命周期函数--监听页面隐藏
  539. */
  540. onHide: function() {
  541. },
  542. /**
  543. * 生命周期函数--监听页面卸载
  544. */
  545. onUnload: function() {
  546. },
  547. /**
  548. * 页面相关事件处理函数--监听用户下拉动作
  549. */
  550. onPullDownRefresh: function() {
  551. },
  552. /**
  553. * 页面上拉触底事件的处理函数
  554. */
  555. onReachBottom: function() {
  556. },
  557. /**
  558. * 用户点击右上角分享
  559. */
  560. onShareAppMessage: function() {
  561. }
  562. })