SystemCity.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemCity as CityModel;
  5. use crmeb\services\CacheService;
  6. use crmeb\services\FormBuilder as Form;
  7. use crmeb\services\JsonService as Json;
  8. use crmeb\services\UtilService as Util;
  9. use think\facade\Route as Url;
  10. class SystemCity extends AuthController
  11. {
  12. /**
  13. * 城市列表
  14. * @return string
  15. * @throws \Exception
  16. */
  17. public function index()
  18. {
  19. $params = Util::getMore([
  20. ['parent_id', 0]
  21. ], $this->request);
  22. $this->assign('pid',$params['parent_id']);
  23. $this->assign('list', CityModel::getList($params));
  24. $addurl = Url::buildUrl('add?parent_id=' . $params['parent_id']);
  25. $this->assign(compact('params', 'addurl'));
  26. return $this->fetch();
  27. }
  28. /**
  29. * 添加城市
  30. * @return string
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function add()
  36. {
  37. $data = Util::getMore([
  38. ['parent_id', 0]
  39. ]);
  40. if ($data['parent_id'] != 0) {
  41. $info = CityModel::where('city_id', $data['parent_id'])->find()->toArray();
  42. } else {
  43. $info = [
  44. "level" => 0,
  45. "city_id" => 0,
  46. "name" => '中国'
  47. ];
  48. }
  49. $field[] = Form::hidden('level', $info['level']);
  50. $field[] = Form::hidden('parent_id', $info['city_id']);
  51. $field[] = Form::input('parent_name', '上级名称', $info['name'])->readonly(true);
  52. $field[] = Form::input('name', '名称');
  53. $field[] = Form::input('merger_name', '合并名称')->placeholder('格式:陕西,西安,雁塔');
  54. $field[] = Form::input('area_code', '区号');
  55. $field[] = Form::input('lng', '经度');
  56. $field[] = Form::input('lat', '纬度');
  57. $form = Form::make_post_form('添加地区', $field, Url::buildUrl('save'), 3);
  58. $this->assign(compact('form'));
  59. return $this->fetch('public/form-builder');
  60. }
  61. /**
  62. * 保存
  63. */
  64. public function save()
  65. {
  66. $data = Util::postMore([
  67. ['id', 0],
  68. ['name', 0],
  69. ['merger_name', 0],
  70. ['area_code', 0],
  71. ['lng', 0],
  72. ['lat', 0],
  73. ['level', 0],
  74. ['parent_id', 0],
  75. ]);
  76. if (!$data['name']) return Json::fail('请输入城市名称');
  77. if (!$data['merger_name']) return Json::fail('请输入城市合并名称');
  78. if ($data['id'] == 0) {
  79. unset($data['id']);
  80. $data['level'] = $data['level'] + 1;
  81. $data['city_id'] = intval(CityModel::max('city_id') + 1);
  82. CityModel::create($data);
  83. return Json::successful('添加城市成功!');
  84. } else {
  85. unset($data['level']);
  86. unset($data['parent_id']);
  87. CityModel::where('id', $data['id'])->update($data);
  88. return Json::successful('修改城市成功!');
  89. }
  90. }
  91. /**
  92. * 修改城市
  93. * @return string
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function edit()
  99. {
  100. $data = Util::getMore([
  101. ['id', 0]
  102. ]);
  103. $info = CityModel::get($data['id'])->toArray();
  104. $info['parent_name'] = CityModel::where('city_id', $info['parent_id'])->value('name') ? : '中国';
  105. $field[] = Form::hidden('id', $info['id']);
  106. $field[] = Form::hidden('level', $info['level']);
  107. $field[] = Form::hidden('parent_id', $info['parent_id']);
  108. $field[] = Form::input('parent_name', '上级名称', $info['parent_name'])->readonly(true);
  109. $field[] = Form::input('name', '名称', $info['name']);
  110. $field[] = Form::input('merger_name', '合并名称', $info['merger_name'])->placeholder('格式:陕西,西安,雁塔');
  111. $field[] = Form::input('area_code', '区号', $info['area_code']);
  112. $field[] = Form::input('lng', '经度', $info['lng']);
  113. $field[] = Form::input('lat', '纬度', $info['lat']);
  114. $form = Form::make_post_form('添加地区', $field, Url::buildUrl('save'), 3);
  115. $this->assign(compact('form'));
  116. return $this->fetch('public/form-builder');
  117. }
  118. /**
  119. * 删除城市
  120. * @throws \Exception
  121. */
  122. public function delete()
  123. {
  124. $data = Util::getMore([
  125. ['city_id', 0]
  126. ]);
  127. CityModel::where('city_id', $data['city_id'])->whereOr('parent_id', $data['city_id'])->delete();
  128. return Json::successful('删除成功!');
  129. }
  130. /**
  131. * 清除城市缓存
  132. * @throws \Psr\SimpleCache\InvalidArgumentException
  133. */
  134. public function clean_cache()
  135. {
  136. $res = CacheService::delete('CITY_LIST');
  137. if ($res) {
  138. return Json::successful('清除成功!');
  139. } else {
  140. return Json::fail('清除失败!');
  141. }
  142. }
  143. }