心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CityController.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * 城市分站控制器
  4. */
  5. namespace app\admin\controller\system;
  6. use core\basic\Controller;
  7. use app\admin\model\system\CityModel;
  8. use Overtrue\Pinyin\Pinyin;
  9. class CityController extends Controller
  10. {
  11. private $count;
  12. private $blank;
  13. private $outData = array();
  14. private $model;
  15. public function __construct()
  16. {
  17. $this->model = new CityModel();
  18. $lockFile = ROOT_PATH . '/data/city.lock';
  19. //如果不存在插件锁,则创建数据表
  20. if(!file_exists($lockFile)){
  21. $this->createTabkle();
  22. }
  23. }
  24. // 创建数据表
  25. public function createTabkle(){
  26. if (get_db_type() == 'sqlite') {
  27. $sql = file_get_contents(ROOT_PATH . '/city_sqlite_update.sql');
  28. $result = $this->model->amd($sql);
  29. } else {
  30. $sql = file_get_contents(ROOT_PATH . '/city_mysql_update.sql');
  31. //分割sql语句
  32. $sqlArr = explode(';', $sql);
  33. foreach ($sqlArr as $v) {
  34. if( $v!=='' ){
  35. $this->model->amd($v);
  36. }
  37. }
  38. $result = true;
  39. }
  40. if($result){
  41. file_put_contents(ROOT_PATH . '/data/city.lock', get_datetime());
  42. }
  43. }
  44. // 地区列表
  45. public function index()
  46. {
  47. $this->assign('list', true);
  48. $pid = 0;
  49. $cur_city = '顶级';
  50. if( get('pid') ){
  51. $pid = get('pid');
  52. $cur_city = $this->model->findCity($pid)->title;
  53. }
  54. $this->assign('pid', $pid);
  55. $this->assign('cur_city', $cur_city);
  56. $lists = $this->model->getList($pid);
  57. $this->assign('lists', $lists);
  58. $city_select = $lists;
  59. $this->assign('city_select', $city_select);
  60. $this->display('system/city.html');
  61. }
  62. // 内容栏目增加
  63. public function add()
  64. {
  65. // 修改操作
  66. if ($_POST) {
  67. if (! ! $mutititle = post('mutititle')) {
  68. $mutititle = str_replace(',', ',', $mutititle); //批量城市名
  69. $mutietitle = post('mutietitle');
  70. $mutietitle = str_replace(',', ',', $mutietitle);
  71. $pid = post('pid', 'int');
  72. $logo = post('logo');
  73. $weixin = post('weixin');
  74. if (! $mutititle) {
  75. alert_back('城市名称不能为空!');
  76. }
  77. if (! $mutietitle) {
  78. alert_back('英文名称不能为空!');
  79. }
  80. $titles = explode(',', $mutititle);
  81. $etitles = explode(',', $mutietitle);
  82. foreach ($titles as $key => $value) {
  83. // 检查名称
  84. if ($this->model->checkName("title='$value' or etitle='$etitles[$key]'")) {
  85. alert_back('城市名称已经存在,不能再使用!');
  86. }
  87. $data[] = array(
  88. 'pid' => $pid,
  89. 'title' => $value,
  90. 'etitle' => $etitles[$key],
  91. 'isurl' => '',
  92. 'status' => 1,
  93. 'sorting' => 255,
  94. 'istop' => 0,
  95. 'seo_title' => '',
  96. 'seo_keywords' => '',
  97. 'seo_description' => '',
  98. 'contact' => '',
  99. 'mobile' => '',
  100. 'phone' => '',
  101. 'fax' => '',
  102. 'email' => '',
  103. 'qq' => '',
  104. 'address' => '',
  105. 'logo' => $logo,
  106. 'weixin' => $weixin
  107. );
  108. }
  109. }else{
  110. // 获取数据
  111. $title = post('title');
  112. $etitle = post('etitle');
  113. $isurl = post('isurl');
  114. $pid = post('pid', 'int');
  115. $status = post('status', 'int');
  116. $istop = post('istop', 'int', '', '', 0);
  117. $seo_title = post('seo_title');
  118. $seo_keywords = post('seo_keywords');
  119. $seo_description = post('seo_description');
  120. $logo = post('logo');
  121. $weixin = post('weixin');
  122. $contact = post('contact');
  123. $mobile = post('mobile');
  124. $phone = post('phone');
  125. $fax = post('fax');
  126. $email = post('email');
  127. $qq = post('qq');
  128. $address = post('address');
  129. if (! $title) {
  130. alert_back('名称不能为空!');
  131. }
  132. if (! $etitle) {
  133. alert_back('英文名不能为空!');
  134. }
  135. // 检查名称
  136. if ($this->model->checkName("title='$title' or etitle='$etitle'")) {
  137. alert_back('名称已经存在,不能再使用!');
  138. }
  139. // 构建数据
  140. $data = array(
  141. 'pid' => $pid,
  142. 'title' => $title,
  143. 'etitle' => $etitle,
  144. 'isurl' => $isurl,
  145. 'status' => $status,
  146. 'sorting' => 255,
  147. 'istop' => $istop,
  148. 'seo_title' => $seo_title,
  149. 'seo_keywords' => $seo_keywords,
  150. 'seo_description' => $seo_description,
  151. 'contact' => $contact,
  152. 'mobile' => $mobile,
  153. 'phone' => $phone,
  154. 'fax' => $fax,
  155. 'email' => $email,
  156. 'qq' => $qq,
  157. 'address' => $address,
  158. 'logo' => $logo,
  159. 'weixin' => $weixin
  160. );
  161. }
  162. // 执行添加
  163. if ($this->model->addCity($data)) {
  164. $this->log('新增地区' . $id . '成功!');
  165. $url = $pid?'/pid/'.$pid:'';
  166. success('新增成功!', url('/admin/City/index'.$url));
  167. } else {
  168. location(- 1);
  169. }
  170. }
  171. }
  172. // 修改
  173. public function mod()
  174. {
  175. // 批量修改排序
  176. if (! ! $submit = post('submit')) {
  177. switch ($submit) {
  178. case 'sorting': // 修改列表排序
  179. $listall = post('listall');
  180. if ($listall) {
  181. $sorting = post('sorting');
  182. foreach ($listall as $key => $value) {
  183. if ($sorting[$key] === '' || ! is_numeric($sorting[$key]))
  184. $sorting[$key] = 255;
  185. $this->model->modCity($value, "sorting=" . $sorting[$key]);
  186. }
  187. $this->log('批量修改排序成功!');
  188. success('修改成功!', - 1);
  189. } else {
  190. alert_back('排序失败,无任何内容!');
  191. }
  192. break;
  193. }
  194. }
  195. if (! $id = get('id', 'int')) {
  196. error('传递的参数值错误!', - 1);
  197. }
  198. // 单独修改状态
  199. if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
  200. if ($this->model->modCity($id, "$field='$value'")) {
  201. $this->log('修改地区' . $id . '状态' . $value . '成功!');
  202. location(- 1);
  203. } else {
  204. $this->log('修改地区' . $id . '状态' . $value . '失败!');
  205. alert_back('修改失败!');
  206. }
  207. }
  208. // 修改操作
  209. if ($_POST) {
  210. // 获取数据
  211. $title = post('title');
  212. $etitle = post('etitle');
  213. $isurl = post('isurl');
  214. $pid = post('pid', 'int');
  215. $status = post('status', 'int');
  216. $istop = post('istop', 'int', '', '', 0);
  217. $seo_title = post('seo_title');
  218. $seo_keywords = post('seo_keywords');
  219. $seo_description = post('seo_description');
  220. $contact = post('contact');
  221. $mobile = post('mobile');
  222. $phone = post('phone');
  223. $fax = post('fax');
  224. $email = post('email');
  225. $qq = post('qq');
  226. $address = post('address');
  227. $logo = post('logo');
  228. $weixin = post('weixin');
  229. if($id==$pid){
  230. alert_back('区域混乱啦!');
  231. }
  232. if (! $title) {
  233. alert_back('名称不能为空!');
  234. }
  235. if (! $etitle) {
  236. alert_back('英文名不能为空!');
  237. }
  238. // 构建数据
  239. $data = array(
  240. 'pid' => $pid,
  241. 'title' => $title,
  242. 'etitle' => $etitle,
  243. 'isurl' => $isurl,
  244. 'status' => $status,
  245. 'istop' => $istop,
  246. 'seo_title' => $seo_title,
  247. 'seo_keywords' => $seo_keywords,
  248. 'seo_description' => $seo_description,
  249. 'contact' => $contact,
  250. 'mobile' => $mobile,
  251. 'phone' => $phone,
  252. 'fax' => $fax,
  253. 'email' => $email,
  254. 'qq' => $qq,
  255. 'address' => $address,
  256. 'logo' => $logo,
  257. 'weixin' => $weixin
  258. );
  259. // 执行添加
  260. if ($this->model->modCity($id, $data)) {
  261. $this->log('修改地区' . $id . '成功!');
  262. $url = $pid?'/pid/'.$pid:'';
  263. success('修改成功!', url('/admin/City/index'.$url));
  264. } else {
  265. location(- 1);
  266. }
  267. } else { // 调取修改内容
  268. $this->assign('mod', true);
  269. $city = $this->model->findCity($id);
  270. if (! $city) {
  271. error('编辑的内容已经不存在!', - 1);
  272. }
  273. if( $city->pid==0 ){
  274. $pid = 0;
  275. $cur_city = '顶级';
  276. }else{
  277. $parent_city = $this->model->findCity($city->pid);
  278. $pid = $parent_city->pid;
  279. $cur_city = $parent_city->title;
  280. }
  281. $this->assign('pid', $pid);
  282. $this->assign('cur_city', $cur_city);
  283. $city_select = $this->model->getList($parent_city->pid);
  284. $this->assign('city_select', $city_select);
  285. $this->assign('city', $city);
  286. $this->display('system/city.html');
  287. }
  288. }
  289. // 地区删除
  290. public function del(){
  291. // 执行批量删除
  292. if ($_POST) {
  293. if (! ! $list = post('list')) {
  294. if ($this->model->delCityList($list)) {
  295. $this->log('批量删除地区成功!');
  296. success('批量删除成功!', - 1);
  297. } else {
  298. $this->log('批量删除地区失败!');
  299. error('批量删除失败!', - 1);
  300. }
  301. } else {
  302. alert_back('请选择要删除的内容!');
  303. }
  304. }
  305. if (! $id = get('id', 'int')) {
  306. error('传递的参数值错误!', - 1);
  307. }
  308. if ($this->model->delCity($id)) {
  309. $this->log('删除地区' . $id . '成功!');
  310. success('删除成功!', - 1);
  311. } else {
  312. $this->log('删除地区' . $id . '失败!');
  313. error('删除失败!', - 1);
  314. }
  315. }
  316. /*
  317. * https://www.ledaxinli.com/admin.php?p=/City/exportData
  318. */
  319. public function exportData(){
  320. //echo ROOT_PATH;
  321. //require './strHandle/Pinyin.php';
  322. //$class = new Pinyin();
  323. //取每个汉字的首字母
  324. //$pinyin = $class->str2py('甄工');
  325. //取每个汉字的全部字母
  326. //$pinyin = $class->str2pys('需要转换的汉字');
  327. //var_dump($pinyin);die;
  328. //baiduSubmitResource('','');
  329. $pinyin = new Pinyin();
  330. //$res = $pinyin->convert('带着希望去旅行,比到达终点更美好');
  331. //var_dump($res);die;
  332. $pid = 3497;
  333. $filename = './static/backup/data/镇级.txt';
  334. $lines = file($filename);
  335. $sort = $this->model->getSortLast();
  336. foreach ($lines as $line) {
  337. //echo $line.'<br/>';
  338. //判断是否为空
  339. if(!empty($line)){
  340. //var_dump($line);
  341. //判断是否存在、
  342. $charToFind = "、";
  343. // 使用 strpos() 来查找字符
  344. $pos = strpos($line, $charToFind);
  345. if ($pos !== false) {
  346. $str = explode('、',$line);
  347. //var_dump($str);
  348. } else {
  349. $str = [$line];
  350. }
  351. //var_dump($str);
  352. $loop = 1;
  353. foreach ($str as $k=>$v){
  354. $v = trim($v); //去除
  355. //var_dump($v);
  356. $length = strlen($v);
  357. if(!empty($v) && $loop === 1 && $length>2){
  358. //去除字符 县 市 区 自治县
  359. $po = str_replace('县','',$v);
  360. $po = str_replace('市','',$po);
  361. $po = str_replace('区','',$po);
  362. $po = str_replace('自治','',$po);
  363. //转成拼音
  364. $py = $pinyin->convert($po);
  365. //var_dump($py);die;
  366. //增加排序号
  367. $sort = $sort + 10;
  368. //判断是否重复 取前面2个字 重复不加mm 需要不断重复判断 加数字(排序号) 名称也加数字
  369. $etitle = $py[0];
  370. if(isset($py[1])){
  371. $etitle = $etitle.$py[1];
  372. }
  373. $etitle = $this->model->etitleIsCf($etitle,$sort);
  374. $title = $this->model->titleIsCf($v,$sort);
  375. $title = str_replace('镇','',$title);
  376. //构建数据插入
  377. $insert_data = [
  378. 'title' => $title,
  379. 'pid' => $pid,
  380. 'stitle' => $title,
  381. 'etitle' => $etitle,
  382. 'sorting' => $sort,
  383. 'status' => 1,
  384. 'istop' => 0,
  385. 'isurl' => '',
  386. 'seo_title' => '',
  387. 'seo_keywords' => '',
  388. 'seo_description' => '',
  389. 'contact' => '',
  390. 'phone' => '',
  391. 'fax' => '',
  392. 'mobile' => '',
  393. 'email' => '',
  394. 'address' => '',
  395. 'qq' => '',
  396. 'logo' => '',
  397. 'weixin' => ''
  398. ];
  399. $run = $this->model->addRow($insert_data);
  400. echo $title.'------'.$run.'<br/>';
  401. }
  402. }
  403. }
  404. }
  405. }
  406. /*
  407. * https://www.ledaxinli.com/admin.php?p=/City/updateData
  408. */
  409. public function updateData(){
  410. //384 市 383 区
  411. //$this->model->getList($pid);
  412. //$this->model->editRow($insert_data);
  413. }
  414. }