설명 없음
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.

Citysite.php 71KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Db;
  15. use think\Page;
  16. use think\Cache;
  17. class Citysite extends Base
  18. {
  19. public static $top_city = [
  20. "山东" => ["青岛","济南","烟台","潍坊","临沂","济宁","淄博","威海","东营","德州","泰安","聊城","滨州","菏泽","枣庄"],
  21. "江苏" => ["苏州","南京","无锡","南通","常州","徐州","扬州","盐城","泰州","镇江","淮安","连云港","宿迁"],
  22. "广东" => ["深圳","广州","佛山","东莞","惠州","珠海","江门","茂名","中山","湛江"],
  23. "浙江" => ["杭州","宁波","温州","绍兴","嘉兴","台州","金华","湖州"],
  24. "河北" => ["唐山","石家庄","沧州","邯郸","保定","廊坊"],
  25. "河南" => ["郑州","洛阳","南阳","许昌","周口","新乡"],
  26. "湖南" => ["长沙","岳阳","常德","衡阳","株洲","郴州"],
  27. "福建" => ["福州","泉州","厦门","漳州"],
  28. "内蒙古" => ["鄂尔多斯","呼和浩特","包头"],
  29. "湖北" => ["武汉","襄阳","宜昌"],
  30. "辽宁" => ["大连","沈阳","鞍山"],
  31. "陕西" => ["西安","榆林","咸阳"],
  32. "安徽" => ["合肥","芜湖"],
  33. "广西" => ["南宁","柳州"],
  34. "贵州" => ["贵阳","遵义"],
  35. "黑龙江" => ["哈尔滨","大庆"],
  36. "吉林" => ["长春","吉林"],
  37. "新疆" => ["乌鲁木齐"],
  38. "江西" => ["南昌"],
  39. "四川" => ["成都"],
  40. "云南" => ["昆明"],
  41. "甘肃" => ["兰州"],
  42. "山西" => ["太原"],
  43. "北京" => [],
  44. "天津" => [],
  45. "上海" => [],
  46. "重庆" => [],
  47. ];
  48. private $web_citysite_open;
  49. // 禁用的目录名称
  50. private $disableDirname = [];
  51. public function _initialize(){
  52. parent::_initialize();
  53. $this->disableDirname = config('global.disable_dirname');
  54. $functionLogic = new \app\common\logic\FunctionLogic;
  55. $functionLogic->validate_authorfile(2);
  56. $this->web_citysite_open = tpCache('global.web_citysite_open');
  57. $this->assign('web_citysite_open', $this->web_citysite_open);
  58. }
  59. public function index()
  60. {
  61. $assign_data = array();
  62. $condition = array();
  63. // 获取到所有GET参数
  64. $param = input('param.');
  65. $parent_id = input('pid/d', 0);
  66. // 应用搜索条件
  67. foreach (['keywords','pid'] as $key) {
  68. $param[$key] = addslashes(trim($param[$key]));
  69. if (isset($param[$key]) && $param[$key] !== '') {
  70. if ($key == 'keywords') {
  71. $condition['name'] = array('LIKE', "%{$param[$key]}%");
  72. } else if ($key == 'pid') {
  73. $condition['parent_id'] = array('eq', $param[$key]);
  74. } else {
  75. $condition[$key] = array('eq', $param[$key]);
  76. }
  77. }
  78. }
  79. // 上一级区域名称
  80. $parentInfo = Db::name('citysite')->where(['id'=>$parent_id])->find();
  81. $parentLevel = !empty($parentInfo['level']) ? intval($parentInfo['level']) : 0;
  82. $condition['level'] = $parentLevel + 1;
  83. $regionM = Db::name('citysite');
  84. $count = $regionM->where($condition)->count('id');// 查询满足要求的总记录数
  85. $Page = $pager = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  86. $list = $regionM->where($condition)->order('sort_order asc, id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
  87. foreach ($list as $key => $val) {
  88. $val['siteurl'] = siteurl($val);
  89. $list[$key] = $val;
  90. }
  91. $show = $Page->show();// 分页显示输出
  92. $this->assign('page',$show);// 赋值分页输出
  93. $this->assign('list',$list);// 赋值数据集
  94. $this->assign('pager',$pager);// 赋值分页对象
  95. $this->assign('parentInfo',$parentInfo);
  96. return $this->fetch();
  97. }
  98. public function add(){
  99. if (IS_POST) {
  100. $post = input('post.');
  101. $post['name'] = trim($post['name']);
  102. $post['domain'] = preg_replace("/[^a-zA-Z0-9]+/", "", strtolower($post['domain']));
  103. // --存储数据
  104. $nowData = array(
  105. 'initial' => getFirstCharter($post['name']),
  106. 'seo_description' => !empty($post['seo_description']) ? $post['seo_description'] : '',
  107. 'sort_order' => 100,
  108. 'add_time' => getTime(),
  109. 'update_time' => getTime(),
  110. );
  111. if (!empty($post['city_id'])){
  112. $nowData['level'] = 3;
  113. $nowData['parent_id'] = intval($post['city_id']);
  114. $nowData['topid'] = intval($post['province_id']);
  115. } else if (!empty($post['province_id'])){
  116. $nowData['level'] = 2;
  117. $nowData['parent_id'] = intval($post['province_id']);
  118. $nowData['topid'] = intval($post['province_id']);
  119. } else {
  120. $nowData['level'] = 1;
  121. $nowData['parent_id'] = 0;
  122. $nowData['topid'] = 0;
  123. }
  124. $data = array_merge($post, $nowData);
  125. if (empty($data['name'])) {
  126. $this->error('区域名称不能为空!');
  127. }
  128. // 区域名称是否已存在
  129. $count = Db::name('citysite')->where([
  130. 'name' => $data['name'],
  131. 'parent_id' => $data['parent_id'],
  132. ])->count();
  133. if (!empty($count)) {
  134. $this->error('区域名称已存在,请更换!');
  135. }
  136. if (!empty($data['domain'])) {
  137. $count = Db::name('citysite')->where([
  138. 'domain' => $data['domain'],
  139. ])->count();
  140. if (!empty($count)) {
  141. $this->error('英文名称已存在!');
  142. }
  143. // 检测
  144. if (!empty($data['domain']) && !$this->domain_unique($data['domain'])) {
  145. $this->error('英文名称与系统内置冲突,请更改!');
  146. }
  147. /*--end*/
  148. } else {
  149. $this->error('英文名称不能为空!');
  150. }
  151. $insertId = M('citysite')->insertGetId($data);
  152. if (false !== $insertId) {
  153. \think\Cache::clear('citysite');
  154. // extra_cache('global_get_site_province_list', null);
  155. // extra_cache('global_get_site_city_list', null);
  156. // extra_cache('global_get_site_area_list', null);
  157. adminLog('新增区域:'.$data['name']);
  158. $this->success("操作成功");
  159. }else{
  160. $this->error("操作失败");
  161. }
  162. exit;
  163. }
  164. $pid = input('param.pid/d', 0);
  165. $region = array_reverse($this->getParentCitysiteId($pid));
  166. $assign_data['province_id'] = !empty($region[0]) ? $region[0] : 0;
  167. $assign_data['city_id'] = !empty($region[1]) ? $region[1] : 0;
  168. // 省份列表
  169. $province_all = $this->get_site_province_all();
  170. $assign_data['province_all'] = $province_all;
  171. $assign_data['rootDomain'] = $this->request->rootDomain().ROOT_DIR;
  172. $this->assign($assign_data);
  173. return $this->fetch();
  174. }
  175. //批量新增
  176. public function batch_add(){
  177. if (IS_POST) {
  178. $post = input('post.');
  179. $name = trim($post['name']);
  180. if (empty($name)) {
  181. $this->error('区域名称不能为空!');
  182. }
  183. $nameArr = explode("\r\n", $name);
  184. //去除数组空值、左右空格
  185. foreach ($nameArr as $key => $val) {
  186. $val = trim($val);
  187. if (empty($val)) {
  188. unset($nameArr[$key]);
  189. } else {
  190. $nameArr[$key] = $val;
  191. }
  192. }
  193. $nameArr = array_unique($nameArr); //去重
  194. if (!empty($post['city_id'])){
  195. $level = 3;
  196. $parent_id = intval($post['city_id']);
  197. $topid = intval($post['province_id']);
  198. } else if (!empty($post['province_id'])){
  199. $level = 2;
  200. $parent_id = intval($post['province_id']);
  201. $topid = intval($post['province_id']);
  202. } else {
  203. $level = 1;
  204. $parent_id = 0;
  205. $topid = 0;
  206. }
  207. $have_name = Db::name('citysite')->where(['parent_id' => $parent_id])->column('name');
  208. $addData = $insert_name = [];
  209. foreach ($nameArr as $key => $val) {
  210. if(empty($val) || in_array($val,$have_name))
  211. {
  212. continue;
  213. }
  214. $insert_name[] = $val;
  215. $domain = preg_replace("/[^a-zA-Z0-9]+/", "", get_pinyin($val));
  216. $domain = $this->rand_domain($domain);
  217. $addData[] = [
  218. 'name' => $val,
  219. 'domain' => $domain,
  220. 'level' => $level,
  221. 'parent_id' => $parent_id,
  222. 'topid' => $topid,
  223. 'is_open' => !empty($post['is_open']) ? intval($post['is_open']) : 0,
  224. 'showall' => !empty($post['showall']) ? intval($post['showall']) : 1,
  225. 'seoset' => !empty($post['seoset']) ? intval($post['seoset']) : 0,
  226. 'initial' => getFirstCharter($val),
  227. 'seo_title' => str_replace(['{region}','{区域}'], $val, $post['seo_title']),
  228. 'seo_keywords' => str_replace(['{region}','{区域}'], $val, $post['seo_keywords']),
  229. 'seo_description' => str_replace(['{region}','{区域}'], $val, $post['seo_description']),
  230. 'sort_order' => 100,
  231. 'add_time' => getTime(),
  232. 'update_time' => getTime(),
  233. ];
  234. }
  235. $res = Db::name('citysite')->insertAll($addData);
  236. if (false !== $res) {
  237. \think\Cache::clear('citysite');
  238. // extra_cache('global_get_site_province_list', null);
  239. // extra_cache('global_get_site_city_list', null);
  240. // extra_cache('global_get_site_area_list', null);
  241. adminLog('批量新增区域:'.implode(',',$insert_name));
  242. $this->success("操作成功");
  243. }else{
  244. $this->error("操作失败");
  245. }
  246. exit;
  247. }
  248. $pid = input('param.pid/d', 0);
  249. $region = array_reverse($this->getParentCitysiteId($pid));
  250. $assign_data['province_id'] = !empty($region[0]) ? $region[0] : 0;
  251. $assign_data['city_id'] = !empty($region[1]) ? $region[1] : 0;
  252. // 省份列表
  253. $province_all = $this->get_site_province_all();
  254. $assign_data['province_all'] = $province_all;
  255. $assign_data['rootDomain'] = $this->request->rootDomain().ROOT_DIR;
  256. $this->assign($assign_data);
  257. return $this->fetch();
  258. }
  259. public function edit(){
  260. if (IS_POST) {
  261. $post = input('post.');
  262. if(!empty($post['id'])){
  263. $post['id'] = intval($post['id']);
  264. $post['name'] = trim($post['name']);
  265. $post['domain'] = preg_replace("/[^a-zA-Z0-9]+/", "", strtolower($post['domain']));
  266. // --存储数据
  267. $nowData = array(
  268. 'initial' => getFirstCharter($post['name']),
  269. 'seo_description' => !empty($post['seo_description']) ? $post['seo_description'] : '',
  270. 'update_time' => getTime(),
  271. );
  272. if (!isset($post['province_id'])) $post['province_id'] = $post['old_province_id'];
  273. if (!isset($post['city_id'])) $post['city_id'] = $post['old_city_id'];
  274. if (!empty($post['city_id'])){
  275. $nowData['level'] = 3;
  276. $nowData['parent_id'] = intval($post['city_id']);
  277. $nowData['topid'] = intval($post['province_id']);
  278. } else if (!empty($post['province_id'])){
  279. $nowData['level'] = 2;
  280. $nowData['parent_id'] = intval($post['province_id']);
  281. $nowData['topid'] = intval($post['province_id']);
  282. } else {
  283. $nowData['level'] = 1;
  284. $nowData['parent_id'] = 0;
  285. $nowData['topid'] = 0;
  286. }
  287. $data = array_merge($post, $nowData);
  288. if (empty($data['name'])) {
  289. $this->error('区域名称不能为空!');
  290. }
  291. // 区域名称是否已存在
  292. $count = Db::name('citysite')->where([
  293. 'id' => ['NEQ', $data['id']],
  294. 'name' => $data['name'],
  295. 'parent_id' => $data['parent_id'],
  296. ])->count();
  297. if (!empty($count)) {
  298. $this->error('区域名称已存在,请更换!');
  299. }
  300. if (!empty($data['domain'])) {
  301. $count = Db::name('citysite')->where([
  302. 'domain' => $data['domain'],
  303. 'id' => ['NEQ', $data['id']],
  304. ])->count();
  305. if (!empty($count)) {
  306. $this->error('英文名称已存在!');
  307. }
  308. // 检测
  309. if (!empty($data['domain']) && !$this->domain_unique($data['domain'], $data['id'])) {
  310. $this->error('英文名称与系统内置冲突,请更改!');
  311. }
  312. /*--end*/
  313. } else {
  314. $this->error('英文名称不能为空!');
  315. }
  316. $r = M('citysite')->where([
  317. 'id' => $post['id'],
  318. ])
  319. ->cache(true, null, "citysite")
  320. ->update($data);
  321. if (false !== $r) {
  322. // 同步处理子级城市
  323. if (empty($post['province_id'])) {
  324. Db::name('citysite')->where([
  325. 'parent_id' => $post['id'],
  326. ])->update([
  327. 'level' => 2,
  328. 'topid' => $post['id'],
  329. 'update_time' => getTime(),
  330. ]);
  331. } else if (!empty($post['province_id']) && empty($post['city_id'])) {
  332. Db::name('citysite')->where([
  333. 'parent_id' => $post['id'],
  334. ])->update([
  335. 'level' => 3,
  336. 'topid' => $data['topid'],
  337. 'update_time' => getTime(),
  338. ]);
  339. }
  340. // extra_cache('global_get_site_province_list', null);
  341. // extra_cache('global_get_site_city_list', null);
  342. // extra_cache('global_get_site_area_list', null);
  343. adminLog('编辑区域:'.$data['name']);
  344. $this->success("操作成功");
  345. }
  346. }
  347. $this->error("操作失败");
  348. }
  349. $id = input('param.id/d', 0);
  350. $info = model("Citysite")->getInfo($id);
  351. $assign_data['field'] = $info;
  352. $region = array_reverse($this->getParentCitysiteId($info['parent_id']));
  353. $assign_data['province_id'] = !empty($region[0]) ? $region[0] : 0;
  354. $assign_data['city_id'] = !empty($region[1]) ? $region[1] : 0;
  355. // 省份列表
  356. $province_all = $this->get_site_province_all();
  357. $assign_data['province_all'] = $province_all;
  358. $assign_data['rootDomain'] = $this->request->rootDomain().ROOT_DIR;
  359. // 是否有下级以及层级
  360. $assign_data['childrenLevelCount'] = Db::name('citysite')->field('level')->where(['parent_id|topid'=>$id])->group('level')->count();
  361. $this->assign($assign_data);
  362. return $this->fetch();
  363. }
  364. public function conf(){
  365. if (IS_POST) {
  366. $post = $data = input('post.');
  367. foreach ($data as $key => $val) {
  368. $val = trim($val);
  369. $data[$key] = $val;
  370. }
  371. tpCache('site', $data);
  372. adminLog('多站点功能配置');
  373. $this->success("操作成功");
  374. }
  375. $assign_data = [];
  376. $row = tpCache('site');
  377. $assign_data['row'] = $row;
  378. // 站点区域
  379. $site_default_home = !empty($row['site_default_home']) ? intval($row['site_default_home']) : 0;
  380. $citysiteLogic = new \app\common\logic\CitysiteLogic;
  381. $assign_data['citysite_html'] = $citysiteLogic->citysite_list(0, $site_default_home, true, 0, array(), false);
  382. $assign_data['site_default_home'] = $site_default_home;
  383. $this->assign($assign_data);
  384. return $this->fetch();
  385. }
  386. /*
  387. * 开启关闭启用
  388. * 开启当前,判断当前是否为唯一开启,如果是,则将当前设置为默认区域
  389. * 关闭当前,判断当前是否为原来默认区域:如果是,则判断当前同级(相同上级)是否存在开启:如存在,设置为默认,如不存在:判断第一级是否存在开启:如存在,设置第一个为默认,如不存在,继续往下级查找。
  390. *
  391. * 至少必须存在一个开启区域
  392. */
  393. public function setStatus() {
  394. $id = input('id/d', 0);
  395. $status = input('status/d', 0);
  396. $list = Db::name("citysite")->where("status=1")->getField("id,status");
  397. if ($status == 0){
  398. if (count($list) == 1 && !empty($list[$id])){
  399. $this->error("至少存在一个开启区域!");
  400. }
  401. }
  402. Db::name('citysite')->where(['id'=>$id])->cache(true, null, "citysite")->update(['status'=>$status, 'update_time'=>getTime()]);
  403. $this->success("设置成功");
  404. /*
  405. $id = input('id/d', 0);
  406. $status = input('status/d', 0);
  407. $list = Db::name("citysite")->where("status=1")->order("level asc")->getField("id,parent_id,status,is_default,level");
  408. $count = count($list);
  409. $is_true = true;
  410. if ($status == 1){
  411. if ($count == 0 || ($count == 1 && empty($list[$id]))){
  412. $is_true = $this->setIsDefault($id);
  413. }
  414. }else{
  415. if ($count == 1 && !empty($list[$id])){
  416. $this->error("至少存在一个开启区域!".$status);
  417. }
  418. if (!empty($list[$id]) && $list[$id]['is_default'] == 1){
  419. $peer_id = $top_id = $any_id = 0;
  420. foreach ($list as $val){
  421. if (empty($peer_id) && $val['id']!= $id && $val['parent_id'] == $list[$id]['parent_id']){
  422. $peer_id = $val['id'];
  423. break;
  424. }
  425. if (empty($top_id) && $val['id']!= $id && $val['parent_id'] == 0){
  426. $top_id = $val['id'];
  427. }
  428. if (empty($any_id) && $val['id']!= $id){
  429. $any_id = $val['id'];
  430. }
  431. }
  432. if ($peer_id){
  433. $default_id = $peer_id;
  434. }else if($top_id){
  435. $default_id = $top_id;
  436. }else{
  437. $default_id = $any_id;
  438. }
  439. $is_true = $this->setIsDefault($default_id);
  440. }
  441. }
  442. if (!$is_true){
  443. $this->error("设置失败,请检查二级域名不能为空!");
  444. }
  445. Db::name('citysite')->where(['id'=>$id])->cache(true, null, "citysite")->update(['status'=>$status, 'update_time'=>getTime()]);
  446. $this->success("设置成功");
  447. */
  448. }
  449. /*
  450. * 设置默认区域
  451. */
  452. // private function setIsDefault($id){
  453. // $id = intval($id);
  454. // $subdomain = Db::name('citysite')->where(['id'=>$id])->getField('domain');
  455. // if ($this->web_citysite_open && empty($subdomain)) { //如果为开启状态,且二级域名为空,不允许设置
  456. // return false;
  457. // }
  458. // $is_true = Db::name('citysite')->where(['id'=>$id])->update(['is_default'=>1, 'update_time'=>getTime()]);
  459. // if ($is_true){
  460. // Db::name('citysite')->where(['id'=>['neq',$id]])->update(['is_default'=>0, 'update_time'=>getTime()]);
  461. // tpCache('site', ['site_default_home'=>$id]);
  462. // }
  463. // \think\Cache::clear('citysite');
  464. // return $is_true;
  465. // }
  466. /*
  467. * 设置是否默认
  468. */
  469. // public function setSortOrder(){
  470. // $id = input('id/d', 0);
  471. // $is_true = $this->setIsDefault($id);
  472. // if ($is_true){
  473. // $this->success("设置成功");
  474. // }else{
  475. // $this->error("设置失败,请检查二级域名不能为空!");
  476. // }
  477. // }
  478. //获取全部省份
  479. private function get_site_province_all()
  480. {
  481. $result = Db::name('citysite')->field('id, name')
  482. ->where('level',1)
  483. ->order("sort_order asc, id asc")
  484. ->getAllWithIndex('id');
  485. return $result;
  486. }
  487. /**
  488. * 获取子类列表
  489. */
  490. public function ajax_get_region($pid = 0, $level = 2, $siteid = '', $text = '--请选择--')
  491. {
  492. \think\Session::pause(); // 暂停session,防止session阻塞机制
  493. $data = model('Citysite')->getList($pid,'*','',$level);
  494. $html = "<option value=''>".urldecode($text)."</option>";
  495. foreach($data as $key=>$val){
  496. if ($val['id'] == $siteid) {
  497. unset($data[$key]);
  498. continue;
  499. }
  500. $html.="<option value='".$val['id']."'>".$val['name']."</option>";
  501. }
  502. $isempty = 0;
  503. if (empty($data)){
  504. $isempty = 1;
  505. }
  506. $this->success($html,'',['isempty'=>$isempty]);
  507. }
  508. /*
  509. * 获取区域列表(关联栏目)
  510. * pid 上级id
  511. * level 级别
  512. * relevance 关联模型(表名称),为空时表示不关联
  513. * text 不选择时显示text
  514. */
  515. public function ajax_get_region_arc($pid = 0,$level = 1,$channel = '9', $text = '--请选择--')
  516. {
  517. \think\Session::pause(); // 暂停session,防止session阻塞机制
  518. $regionIds = $this->getAllRegionIds($level,'',$channel);
  519. $data = Db::name('citysite')->field("*")
  520. ->where(["id"=>['in',$regionIds],'parent_id'=>$pid])
  521. ->select();
  522. if ($level == 1 && count($data) == 1){ //只存在一个省份
  523. $html = "<input type='hidden' id='province_id' name='province_id' value='".$data[0]['id']."'>";
  524. }else if ($level == 1){
  525. $html = "<select name='province_id' id='province_id'>";
  526. $html .= "<option value=''>".urldecode($text)."</option>";
  527. foreach($data as $key=>$val){
  528. $html.="<option value='".$val['id']."'>".$val['name']."</option>";
  529. }
  530. $html .= "</select>";
  531. }else{
  532. $html = "<select name='city_id' id='city_id'>";
  533. $html .= "<option value=''>".urldecode($text)."</option>";
  534. foreach($data as $key=>$val){
  535. $html.="<option value='".$val['id']."'>".$val['name']."</option>";
  536. }
  537. $html .= "</select>";
  538. }
  539. $this->success($html);
  540. }
  541. /*
  542. * 获取所有区域(id)集合
  543. */
  544. private function getAllRegionIds($level,$typeid = "",$channel = ""){
  545. $field = "province_id";
  546. if ($level == 2){
  547. $field = "city_id";
  548. }else if ($level == 3){
  549. $field = "area_id";
  550. }
  551. $where['status'] = 1;
  552. $where['is_del'] = 0;
  553. if (!empty($typeid)){
  554. $where['typeid'] = ['in',$typeid];
  555. }else if (!empty($channel)){
  556. $where['channel'] = ['in',$channel];
  557. }
  558. $regionIds = Db::name('archives')->where($where)->group($field)->getField($field,true);
  559. return $regionIds;
  560. }
  561. /**
  562. * 删除
  563. */
  564. public function del()
  565. {
  566. $id_arr = input('del_id/a');
  567. $id_arr = eyIntval($id_arr);
  568. if(IS_POST && !empty($id_arr)){
  569. // $count = Db::name('citysite')->where('parent_id','IN',$id_arr)->count();
  570. // if ($count > 0){
  571. // $this->error('所选区域有下级区域,请先删除下级区域');
  572. // }
  573. $result = Db::name('citysite')->where([
  574. 'id' => ['IN', $id_arr],
  575. ])->select();
  576. $level = 0;
  577. $name_list = [];
  578. foreach ($result as $key => $val) {
  579. empty($level) && $level = $val['level'];
  580. $name_list[] = $val['name'];
  581. }
  582. $r = Db::name('citysite')->where([
  583. 'id' => ['IN', $id_arr],
  584. ])
  585. ->cache(true, null, "citysite")
  586. ->delete();
  587. if($r !== false){
  588. /*默认区域被删除,自动处理主站默认区域为空*/
  589. $site_default_home = tpCache('global.site_default_home');
  590. if (!empty($site_default_home) && in_array($site_default_home, $id_arr)) {
  591. tpCache('site', ['site_default_home'=>0]);
  592. }
  593. /*end*/
  594. // 删除所有下级区域
  595. if (2 == $level) {
  596. Db::name('citysite')->where([
  597. 'parent_id' => ['IN', $id_arr],
  598. ])
  599. ->cache(true, null, "citysite")
  600. ->delete();
  601. } else if (1 == $level) {
  602. Db::name('citysite')->where([
  603. 'topid' => ['IN', $id_arr],
  604. ])
  605. ->cache(true, null, "citysite")
  606. ->delete();
  607. }
  608. // extra_cache('global_get_site_province_list', null);
  609. // extra_cache('global_get_site_city_list', null);
  610. // extra_cache('global_get_site_area_list', null);
  611. adminLog('删除区域:'.implode(',', $name_list));
  612. $this->success('删除成功');
  613. }
  614. }
  615. $this->error('删除失败');
  616. }
  617. /**
  618. * 判断子域名的唯一性
  619. */
  620. private function domain_unique($domain = '', $id = 0)
  621. {
  622. $result = Db::name('citysite')->field('id,domain')->getAllWithIndex('id');
  623. if (!empty($result)) {
  624. if (0 < $id) unset($result[$id]);
  625. !empty($result) && $result = get_arr_column($result, 'domain');
  626. }
  627. empty($result) && $result = [];
  628. $dirnames = Db::name('arctype')->column('dirname');
  629. foreach ($dirnames as $key => $val) {
  630. $dirnames[$key] = strtolower($val);
  631. }
  632. $disableDirname = array_merge($this->disableDirname, $dirnames, $result);
  633. if (in_array(strtolower($domain), $disableDirname)) {
  634. return false;
  635. }
  636. return true;
  637. }
  638. /**
  639. * 生成随机子域名,确保唯一性
  640. */
  641. private function rand_domain($domain = '')
  642. {
  643. if (empty($domain)) {
  644. $domain = strtolower(get_rand_str(6, 0, 1));
  645. }
  646. if (!$this->domain_unique($domain)) {
  647. $domain = $domain . mt_rand(0,9);
  648. return $this->rand_domain($domain);
  649. }
  650. return $domain;
  651. }
  652. /*
  653. * js打开获取子区域列表
  654. */
  655. public function ajaxSelectRegion(){
  656. $list = Db::name("citysite")->where("status=1")->select();
  657. $this->assign('list', $list);
  658. $this->assign('json_arctype_list', json_encode($list));
  659. $func = input('func/s');
  660. $assign_data['func'] = $func;
  661. $this->assign($assign_data);
  662. return $this->fetch();
  663. }
  664. /*
  665. * js获取region
  666. */
  667. public function ajaxGetOne($where = ""){
  668. return Db::name('citysite')->where($where)->find();
  669. }
  670. /**
  671. * 获取城市站点的所有上级区域id
  672. */
  673. private function getParentCitysiteId($id){
  674. $id = intval($id);
  675. static $regionArr = array();
  676. static $countnext = 0;
  677. $countnext++;
  678. $regionArr[] = $id;
  679. if(!empty($id)){
  680. $list = Db::name('citysite')->field('id,parent_id')->where('id',$id)->find();
  681. if($list && $list['parent_id']!=0){
  682. $this->getParentCitysiteId($list['parent_id']);
  683. }
  684. }
  685. $countnext--;
  686. $result = $regionArr;
  687. if($countnext == 0){
  688. $regionArr = array();
  689. }
  690. return $result;
  691. }
  692. /**
  693. * 获取区域的拼音
  694. */
  695. public function ajax_get_name_pinyin($name = '')
  696. {
  697. $pinyin = get_pinyin($name);
  698. $this->success('提取成功', null, ['pinyin'=>$pinyin]);
  699. }
  700. /**
  701. * 快速启用百强市
  702. */
  703. public function set_bqs_status()
  704. {
  705. $list = Db::name('citysite')->where(['level'=>['IN', [1,2]],'status'=>0])->select();
  706. if (!empty($list)) {
  707. $where = [];
  708. $top_city_arr = [];
  709. foreach (self::$top_city as $key => $val) {
  710. $where[] = " `name` LIKE '{$key}%' ";
  711. $top_city_arr = array_merge($top_city_arr, $val);
  712. }
  713. $where_str = implode(' OR ', $where);
  714. $provinceList = Db::name('citysite')->field('id,name')->where(['level'=>1])->where($where_str)->select();
  715. $province_ids = [];
  716. foreach ($provinceList as $key => $val) {
  717. foreach (self::$top_city as $_k => $_v) {
  718. if (stristr($val['name'], $_k)) {
  719. $provinceList[$_k] = $val;
  720. unset($provinceList[$key]);
  721. continue;
  722. }
  723. }
  724. $province_ids[] = $val['id'];
  725. }
  726. // 启用百强市涉及的省份
  727. Db::name('citysite')->where(['id'=>['IN', $province_ids]])->update(['status'=>1,'update_time'=>getTime()]);
  728. // 启用百强市
  729. foreach (self::$top_city as $key => $val) {
  730. $province_info = !empty($provinceList[$key]) ? $provinceList[$key] : [];
  731. if (empty($province_info)) {
  732. continue;
  733. }
  734. foreach ($val as $_k => $_v) {
  735. $where = [];
  736. foreach ($_v as $_k2 => $_v2) {
  737. $where[] = " `name` LIKE '{$_v2}%' ";
  738. }
  739. $where_str = implode(' OR ', $where);
  740. Db::name('citysite')->where([
  741. 'level' => 2,
  742. 'parent_id' => $province_info['id'],
  743. ])
  744. ->where($where_str)
  745. ->update(['status'=>1,'update_time'=>getTime()]);
  746. }
  747. }
  748. }
  749. $this->success('设置成功');
  750. }
  751. /**
  752. * 一键全部启用/禁用
  753. */
  754. public function set_city_status()
  755. {
  756. if (IS_AJAX_POST) {
  757. $status = input('param.status/d');
  758. $r = Db::name('citysite')->where([
  759. 'status' => empty($status) ? 1 : 0,
  760. ])->update([
  761. 'status' => empty($status) ? 0 : 1,
  762. 'update_time' => getTime(),
  763. ]);
  764. if ($r !== false) {
  765. delFile(RUNTIME_PATH);
  766. $this->success('操作成功');
  767. }
  768. }
  769. $this->error('操作失败');
  770. }
  771. /**
  772. * 批量设置地区
  773. * @return [type] [description]
  774. */
  775. public function batch_setcity()
  776. {
  777. if (IS_AJAX_POST) {
  778. $post = input('post.');
  779. if (empty($post['ids'])) {
  780. $this->error('请至少勾选一个区域');
  781. }
  782. $post['ids'] = str_replace(',', ',', $post['ids']);
  783. $ids = explode(',', $post['ids']);
  784. $inherit_province = !empty($post['inherit_province']) ? intval($post['inherit_province']) : 0;
  785. $inherit_city = !empty($post['inherit_city']) ? intval($post['inherit_city']) : 0;
  786. $inherit_area = !empty($post['inherit_area']) ? intval($post['inherit_area']) : 0;
  787. if (empty($inherit_province) && empty($inherit_city) && empty($inherit_area)) {
  788. $this->error('请勾选要操作的区域级别');
  789. }
  790. $updateData = [];
  791. if (-1 < $post['is_open']) {
  792. $updateData['is_open'] = !empty($post['is_open']) ? intval($post['is_open']) : 0;
  793. }
  794. if (-1 < $post['showall']) {
  795. $updateData['showall'] = !empty($post['showall']) ? intval($post['showall']) : 0;
  796. }
  797. if (-1 < $post['status']) {
  798. $updateData['status'] = !empty($post['status']) ? intval($post['status']) : 0;
  799. }
  800. if (1 == $post['seoset']) {
  801. $updateData['seoset'] = 0;
  802. } else if (0 === $post['seoset']) {
  803. $updateData['seo_title'] = '';
  804. $updateData['seo_keywords'] = '';
  805. $updateData['seo_description'] = '';
  806. }
  807. if (!empty($updateData)) {
  808. $updateData['update_time'] = getTime();
  809. } else { // 都保持原设置了
  810. $this->success('操作成功');
  811. }
  812. $err = 0;
  813. // 在一级列表时操作区域
  814. if ($post['level'] == 1) {
  815. // 设置一级区域
  816. if (!empty($inherit_province)) {
  817. $r = Db::name('citysite')->where([
  818. 'id' => ['IN', $ids],
  819. 'level' => 1,
  820. ])->update($updateData);
  821. if ($r === false) $err++;
  822. }
  823. // 设置二级区域
  824. if (!empty($inherit_city)) {
  825. $r = Db::name('citysite')->where([
  826. 'parent_id' => ['IN', $ids],
  827. 'level' => 2,
  828. ])->update($updateData);
  829. if ($r === false) $err++;
  830. }
  831. // 设置三级区域
  832. if (!empty($inherit_area)) {
  833. $cityids = Db::name('citysite')->where([
  834. 'parent_id' => ['IN', $ids],
  835. 'level' => 2,
  836. ])->column('id');
  837. if (!empty($cityids)) {
  838. $r = Db::name('citysite')->where([
  839. 'parent_id' => ['IN', $cityids],
  840. 'level' => 3,
  841. ])->update($updateData);
  842. if ($r === false) $err++;
  843. }
  844. }
  845. }
  846. // 在二级列表时操作区域
  847. else if ($post['level'] == 2) {
  848. // 设置一级区域
  849. if (!empty($inherit_province)) {
  850. $parent_ids = Db::name('citysite')->where([
  851. 'id' => ['IN', $ids],
  852. 'level' => 2,
  853. ])->column('parent_id');
  854. $r = Db::name('citysite')->where([
  855. 'id' => ['IN', $parent_ids],
  856. 'level' => 1,
  857. ])->update($updateData);
  858. if ($r === false) $err++;
  859. }
  860. // 设置二级区域
  861. if (!empty($inherit_city)) {
  862. $r = Db::name('citysite')->where([
  863. 'id' => ['IN', $ids],
  864. 'level' => 2,
  865. ])->update($updateData);
  866. if ($r === false) $err++;
  867. }
  868. // 设置三级区域
  869. if (!empty($inherit_area)) {
  870. $r = Db::name('citysite')->where([
  871. 'parent_id' => ['IN', $ids],
  872. 'level' => 3,
  873. ])->update($updateData);
  874. if ($r === false) $err++;
  875. }
  876. }
  877. // 在三级列表时操作区域
  878. else if ($post['level'] == 3) {
  879. // 设置一级区域
  880. if (!empty($inherit_province)) {
  881. $topids = Db::name('citysite')->where([
  882. 'id' => ['IN', $ids],
  883. 'level' => 3,
  884. ])->column('topid');
  885. $r = Db::name('citysite')->where([
  886. 'id' => ['IN', $topids],
  887. 'level' => 1,
  888. ])->update($updateData);
  889. if ($r === false) $err++;
  890. }
  891. // 设置二级区域
  892. if (!empty($inherit_city)) {
  893. $parent_ids = Db::name('citysite')->where([
  894. 'id' => ['IN', $ids],
  895. 'level' => 3,
  896. ])->column('parent_id');
  897. $r = Db::name('citysite')->where([
  898. 'id' => ['IN', $parent_ids],
  899. 'level' => 2,
  900. ])->update($updateData);
  901. if ($r === false) $err++;
  902. }
  903. // 设置三级区域
  904. if (!empty($inherit_area)) {
  905. $r = Db::name('citysite')->where([
  906. 'id' => ['IN', $ids],
  907. 'level' => 3,
  908. ])->update($updateData);
  909. if ($r === false) $err++;
  910. }
  911. }
  912. if (empty($err)) {
  913. adminLog('批量设置区域-id:'.$post['ids']);
  914. $this->success('操作成功');
  915. } else {
  916. $this->error('操作失败');
  917. }
  918. }
  919. $level = input('param.level/d', 0);
  920. $level++;
  921. $assign_data = [];
  922. $assign_data['level'] = $level;
  923. $this->assign($assign_data);
  924. return $this->fetch();
  925. }
  926. /**
  927. * 批量设置SEO
  928. * @return [type] [description]
  929. */
  930. public function batch_setcityseo()
  931. {
  932. if (IS_AJAX_POST) {
  933. $post = input('post.');
  934. if (empty($post['ids'])) {
  935. $this->error('请至少勾选一个区域');
  936. }
  937. $post['ids'] = str_replace(',', ',', $post['ids']);
  938. $ids = explode(',', $post['ids']);
  939. $inherit_province = !empty($post['inherit_province']) ? intval($post['inherit_province']) : 0;
  940. $inherit_city = !empty($post['inherit_city']) ? intval($post['inherit_city']) : 0;
  941. $inherit_area = !empty($post['inherit_area']) ? intval($post['inherit_area']) : 0;
  942. if (empty($inherit_province) && empty($inherit_city) && empty($inherit_area)) {
  943. $this->error('请勾选要操作的区域级别');
  944. }
  945. $err = 0;
  946. // 在一级列表时操作区域
  947. if ($post['level'] == 1) {
  948. // 设置一级区域
  949. if (!empty($inherit_province)) {
  950. $list = Db::name('citysite')->field('id,name')->where([
  951. 'id' => ['IN', $ids],
  952. 'level' => 1,
  953. ])->select();
  954. $this->batch_setcityseo_save($err, $list, $post);
  955. }
  956. // 设置二级区域
  957. if (!empty($inherit_city)) {
  958. $list = Db::name('citysite')->field('id,name')->where([
  959. 'parent_id' => ['IN', $ids],
  960. 'level' => 2,
  961. ])->select();
  962. $this->batch_setcityseo_save($err, $list, $post);
  963. }
  964. // 设置三级区域
  965. if (!empty($inherit_area)) {
  966. $cityids = Db::name('citysite')->where([
  967. 'parent_id' => ['IN', $ids],
  968. 'level' => 2,
  969. ])->column('id');
  970. if (!empty($cityids)) {
  971. $list = Db::name('citysite')->field('id,name')->where([
  972. 'parent_id' => ['IN', $cityids],
  973. 'level' => 3,
  974. ])->select();
  975. $this->batch_setcityseo_save($err, $list, $post);
  976. }
  977. }
  978. }
  979. // 在二级列表时操作区域
  980. else if ($post['level'] == 2) {
  981. // 设置一级区域
  982. if (!empty($inherit_province)) {
  983. $parent_ids = Db::name('citysite')->where([
  984. 'id' => ['IN', $ids],
  985. 'level' => 2,
  986. ])->column('parent_id');
  987. $list = Db::name('citysite')->field('id,name')->where([
  988. 'id' => ['IN', $parent_ids],
  989. 'level' => 1,
  990. ])->select();
  991. $this->batch_setcityseo_save($err, $list, $post);
  992. }
  993. // 设置二级区域
  994. if (!empty($inherit_city)) {
  995. $list = Db::name('citysite')->field('id,name')->where([
  996. 'id' => ['IN', $ids],
  997. 'level' => 2,
  998. ])->select();
  999. $this->batch_setcityseo_save($err, $list, $post);
  1000. }
  1001. // 设置三级区域
  1002. if (!empty($inherit_area)) {
  1003. $list = Db::name('citysite')->field('id,name')->where([
  1004. 'parent_id' => ['IN', $ids],
  1005. 'level' => 3,
  1006. ])->select();
  1007. $this->batch_setcityseo_save($err, $list, $post);
  1008. }
  1009. }
  1010. // 在三级列表时操作区域
  1011. else if ($post['level'] == 3) {
  1012. // 设置一级区域
  1013. if (!empty($inherit_province)) {
  1014. $topids = Db::name('citysite')->where([
  1015. 'id' => ['IN', $ids],
  1016. 'level' => 3,
  1017. ])->column('topid');
  1018. $list = Db::name('citysite')->field('id,name')->where([
  1019. 'id' => ['IN', $topids],
  1020. 'level' => 1,
  1021. ])->select();
  1022. $this->batch_setcityseo_save($err, $list, $post);
  1023. }
  1024. // 设置二级区域
  1025. if (!empty($inherit_city)) {
  1026. $parent_ids = Db::name('citysite')->where([
  1027. 'id' => ['IN', $ids],
  1028. 'level' => 3,
  1029. ])->column('parent_id');
  1030. $list = Db::name('citysite')->field('id,name')->where([
  1031. 'id' => ['IN', $parent_ids],
  1032. 'level' => 2,
  1033. ])->select();
  1034. $this->batch_setcityseo_save($err, $list, $post);
  1035. }
  1036. // 设置三级区域
  1037. if (!empty($inherit_area)) {
  1038. $list = Db::name('citysite')->field('id,name')->where([
  1039. 'id' => ['IN', $ids],
  1040. 'level' => 3,
  1041. ])->select();
  1042. $this->batch_setcityseo_save($err, $list, $post);
  1043. }
  1044. }
  1045. if (empty($err)) {
  1046. adminLog('批量设置区域-id:'.$post['ids']);
  1047. $this->success('操作成功');
  1048. } else {
  1049. $this->error('操作失败');
  1050. }
  1051. }
  1052. $level = input('param.level/d', 0);
  1053. $level++;
  1054. $assign_data = [];
  1055. $assign_data['level'] = $level;
  1056. $this->assign($assign_data);
  1057. return $this->fetch();
  1058. }
  1059. /**
  1060. * 批量设置SEO - 统一更新数据
  1061. * @param [type] &$err [description]
  1062. * @param array $list [description]
  1063. * @param array $post [description]
  1064. * @return [type] [description]
  1065. */
  1066. private function batch_setcityseo_save(&$err, $list = [], $post = [])
  1067. {
  1068. if (!empty($list)) {
  1069. $seo_title = !empty($post['seo_title']) ? trim($post['seo_title']) : '';
  1070. $seo_keywords = !empty($post['seo_keywords']) ? trim($post['seo_keywords']) : '';
  1071. $seo_description = !empty($post['seo_description']) ? trim($post['seo_description']) : '';
  1072. $updateData = [];
  1073. foreach ($list as $key => $val) {
  1074. $updateData[] = [
  1075. 'id' => $val['id'],
  1076. 'seo_title' => str_replace(['{region}','{区域}'], $val['name'], $seo_title),
  1077. 'seo_keywords' => str_replace(['{region}','{区域}'], $val['name'], $seo_keywords),
  1078. 'seo_description' => str_replace(['{region}','{区域}'], $val['name'], $seo_description),
  1079. 'update_time' => getTime(),
  1080. ];
  1081. }
  1082. $r = model('Citysite')->saveAll($updateData);
  1083. if ($r === false) $err++;
  1084. }
  1085. }
  1086. /**
  1087. * 获取地区表ey_region的城市列表
  1088. */
  1089. public function ajax_get_region_list($pid = 0, $level = 2, $region_id = '', $text = '--请选择--')
  1090. {
  1091. if ($pid == 1) { // 北京市
  1092. $pid = 2;
  1093. $level = 3;
  1094. } else if ($pid == 338) { // 天津市
  1095. $pid = 339;
  1096. $level = 3;
  1097. } else if ($pid == 10543) { // 上海市
  1098. $pid = 10544;
  1099. $level = 3;
  1100. } else if ($pid == 31929) { // 重庆市
  1101. $pid = [31930,32380];
  1102. $level = 3;
  1103. }
  1104. $data = model('Region')->getList($pid,'*','',$level);
  1105. $html = "<option value=''>".urldecode($text)."</option>";
  1106. foreach($data as $key=>$val){
  1107. if ($val['id'] == $region_id) {
  1108. unset($data[$key]);
  1109. continue;
  1110. }
  1111. // 当区域名称大于2两个字是,就去除末尾指定的字
  1112. if (strlen($val['name']) >= 9) {
  1113. $val['name'] = preg_replace('/(省|市|县|区|乡|镇|旗|州|农场)$/i', '', $val['name']);
  1114. }
  1115. $html.="<option value='".$val['id']."'>".$val['name']."</option>";
  1116. }
  1117. $isempty = 0;
  1118. if (empty($data)){
  1119. $isempty = 1;
  1120. }
  1121. $this->success($html,'',['isempty'=>$isempty]);
  1122. }
  1123. /**
  1124. * 处理区域的末尾多余文字
  1125. * @param string $name [description]
  1126. * @return [type] [description]
  1127. */
  1128. private function handle_name($name = '')
  1129. {
  1130. // 当区域名称大于2两个字是,就去除末尾指定的字
  1131. if (strlen($name) >= 9) {
  1132. $name = preg_replace('/(地区|壮族自治区|维吾尔自治区|回族自治区|自治区|市辖区|特别行政区)$/i', '', $name);
  1133. $name = preg_replace('/(省|市|县|区|乡|镇|旗|州|盟|行政单位|街道)$/i', '', $name);
  1134. }
  1135. return $name;
  1136. }
  1137. /**
  1138. * 一键导入全国城市
  1139. * @return [type] [description]
  1140. */
  1141. public function import_city()
  1142. {
  1143. //防止数据过程超时
  1144. function_exists('set_time_limit') && set_time_limit(0);
  1145. @ini_set('memory_limit','-1');
  1146. if (IS_POST) {
  1147. $post = input('post.');
  1148. $province_name = '';
  1149. $province_id = !empty($post['province_id']) ? intval($post['province_id']) : 0;
  1150. $cur_province_id = input('param.cur_province_id/d', 0);
  1151. if (!empty($cur_province_id)) {
  1152. $province_id = $cur_province_id;
  1153. }
  1154. $city_name = '';
  1155. $city_id = !empty($post['city_id']) ? intval($post['city_id']) : 0;
  1156. $seo_title = !empty($post['seo_title']) ? trim($post['seo_title']) : '';
  1157. $seo_keywords = !empty($post['seo_keywords']) ? trim($post['seo_keywords']) : '';
  1158. $seo_description = !empty($post['seo_description']) ? trim($post['seo_description']) : '';
  1159. /*---------------------读取要导入的城市列表 start---------------------*/
  1160. if (empty($province_id)) { // 导入全国城市
  1161. $level = 1;
  1162. $parent_id = 0;
  1163. $topid = 0;
  1164. $regionList = Db::name('region')->order('level asc, parent_id asc, id asc')->select();
  1165. $regionList = group_same_key($regionList, 'level');
  1166. // 北京、天津、上海、重庆,这四个省级市的二级删掉、三级区域转成二级
  1167. foreach ($regionList[2] as $key => $val) {
  1168. if ($val['id'] == 2) { // 北京市
  1169. unset($regionList[2][$key]);
  1170. $row = Db::name('region')->field('id,name,2 as level,1 as parent_id,initial')->where(['parent_id'=>2])->order('id asc')->select();
  1171. $regionList[2] = array_merge($regionList[2], $row);
  1172. } else if ($val['id'] == 339) { // 天津市
  1173. unset($regionList[2][$key]);
  1174. $row = Db::name('region')->field('id,name,2 as level,338 as parent_id,initial')->where(['parent_id'=>339])->order('id asc')->select();
  1175. $regionList[2] = array_merge($regionList[2], $row);
  1176. } else if ($val['id'] == 10544) { // 上海市
  1177. unset($regionList[2][$key]);
  1178. $row = Db::name('region')->field('id,name,2 as level,10543 as parent_id,initial')->where(['parent_id'=>10544])->order('id asc')->select();
  1179. $regionList[2] = array_merge($regionList[2], $row);
  1180. } else if ($val['id'] == 31930) { // 重庆市 - 市
  1181. unset($regionList[2][0]);
  1182. $row = Db::name('region')->field('id,name,2 as level,31929 as parent_id,initial')->where(['parent_id'=>31930])->order('id asc')->select();
  1183. $regionList[2] = array_merge($regionList[2], $row);
  1184. } else if ($val['id'] == 32380) { // 重庆市 - 县
  1185. unset($regionList[2][0]);
  1186. $row = Db::name('region')->field('id,name,2 as level,31929 as parent_id,initial')->where(['parent_id'=>32380])->order('id asc')->select();
  1187. $regionList[2] = array_merge($regionList[2], $row);
  1188. }
  1189. }
  1190. // 北京、天津、上海、重庆,这四个省级市的三级删掉
  1191. foreach ($regionList[3] as $key => $val) {
  1192. if (in_array($val['parent_id'], [2,339,10544,31930,32380])) {
  1193. unset($regionList[3][$key]);
  1194. }
  1195. }
  1196. }
  1197. else if (!empty($province_id) && empty($city_id)) { // 导入指定当前省份和下级的全部市县
  1198. // 要导入的省份名称
  1199. $province_name = Db::name('region')->where(['id'=>$province_id])->value('name');
  1200. $province_name = preg_replace('/(省|市)$/i', '', $province_name);
  1201. $level = 2;
  1202. $parent_id = (int)Db::name('citysite')->where(['level'=>1,'name'=>['LIKE', "{$province_name}%"]])->value('id');
  1203. $topid = $parent_id;
  1204. $region_ids = [$province_id];
  1205. $city_ids = Db::name('region')->where(['parent_id'=>$province_id])->column('id');
  1206. if (!empty($city_ids)) {
  1207. $region_ids = array_merge($region_ids, $city_ids);
  1208. $area_ids = Db::name('region')->where(['parent_id'=>['IN', $city_ids]])->column('id');
  1209. !empty($area_ids) && $region_ids = array_merge($region_ids, $area_ids);
  1210. }
  1211. $regionList = Db::name('region')->where(['id'=>['IN', $region_ids]])->order('level asc, parent_id asc, id asc')->select();
  1212. $regionList = group_same_key($regionList, 'level');
  1213. // 北京、天津、上海、重庆,这四个省级市的二级删掉、三级区域转成二级
  1214. foreach ($regionList[2] as $key => $val) {
  1215. if ($val['id'] == 2) { // 北京市
  1216. unset($regionList[2][$key]);
  1217. $row = Db::name('region')->field('id,name,2 as level,1 as parent_id,initial')->where(['parent_id'=>2])->order('id asc')->select();
  1218. $regionList[2] = array_merge($regionList[2], $row);
  1219. } else if ($val['id'] == 339) { // 天津市
  1220. unset($regionList[2][$key]);
  1221. $row = Db::name('region')->field('id,name,2 as level,338 as parent_id,initial')->where(['parent_id'=>339])->order('id asc')->select();
  1222. $regionList[2] = array_merge($regionList[2], $row);
  1223. } else if ($val['id'] == 10544) { // 上海市
  1224. unset($regionList[2][$key]);
  1225. $row = Db::name('region')->field('id,name,2 as level,10543 as parent_id,initial')->where(['parent_id'=>10544])->order('id asc')->select();
  1226. $regionList[2] = array_merge($regionList[2], $row);
  1227. } else if ($val['id'] == 31930) { // 重庆市 - 市
  1228. unset($regionList[2][0]);
  1229. $row = Db::name('region')->field('id,name,2 as level,31929 as parent_id,initial')->where(['parent_id'=>31930])->order('id asc')->select();
  1230. $regionList[2] = array_merge($regionList[2], $row);
  1231. } else if ($val['id'] == 32380) { // 重庆市 - 县
  1232. unset($regionList[2][0]);
  1233. $row = Db::name('region')->field('id,name,2 as level,31929 as parent_id,initial')->where(['parent_id'=>32380])->order('id asc')->select();
  1234. $regionList[2] = array_merge($regionList[2], $row);
  1235. }
  1236. }
  1237. // 北京、天津、上海、重庆,这四个省级市的三级删掉
  1238. foreach ($regionList[3] as $key => $val) {
  1239. if (in_array($val['parent_id'], [2,339,10544,31930,32380])) {
  1240. unset($regionList[3][$key]);
  1241. }
  1242. }
  1243. }
  1244. else if (!empty($province_id) && !empty($city_id)) { // 导入指定当前省份和下级的全部市县
  1245. $row = Db::name('region')->where(['id'=>['IN', [$province_id, $city_id]]])->getAllWithIndex('id');
  1246. // 要导入的省份名称
  1247. $province_name = preg_replace('/(省|市)$/i', '', $row[$province_id]['name']);
  1248. // 要导入的二级城市名称
  1249. $city_name = $this->handle_name($row[$province_id]['name']);
  1250. $level = 3;
  1251. $topid = (int)Db::name('citysite')->where(['level'=>1,'name'=>['LIKE', "{$province_name}%"]])->value('id');
  1252. $parent_id = (int)Db::name('citysite')->where(['parent_id'=>$topid,'level'=>2,'name'=>['LIKE', "{$city_name}%"]])->value('id');
  1253. $region_ids = [$province_id, $city_id];
  1254. $area_ids = Db::name('region')->where(['parent_id'=>$city_id])->column('id');
  1255. !empty($area_ids) && $region_ids = array_merge($region_ids, $area_ids);
  1256. $regionList = Db::name('region')->where(['id'=>['IN', $region_ids]])->order('level asc, parent_id asc, id asc')->select();
  1257. $regionList = group_same_key($regionList, 'level');
  1258. // 北京、天津、上海、重庆,这四个省级市的三级删掉
  1259. foreach ($regionList[3] as $key => $val) {
  1260. if (in_array($val['parent_id'], [2,339,10544,31930,32380])) {
  1261. unset($regionList[3][$key]);
  1262. }
  1263. }
  1264. }
  1265. /*---------------------读取要导入的城市列表 end---------------------*/
  1266. /*---------------------多城市站点的树形结构、每个层级的结构 start---------------------*/
  1267. $tree_site = $province_site = $city_site = $area_site = $domainArr = [];
  1268. $arr1 = $arr2 = $arr3 = $arr4 = [];
  1269. $row = Db::name('citysite')->field('id,name,parent_id,topid,level,domain')->order('level asc, parent_id asc, id asc')->select();
  1270. foreach ($row as $key => $val) {
  1271. $domainArr[] = $val['domain'];
  1272. $val['name'] = $this->handle_name($val['name']);
  1273. if ($val['level'] == 1) {
  1274. $val['child'] = [];
  1275. $arr1[$val['id']] = $val;
  1276. $province_site[$val['name']] = $val;
  1277. $tree_site[$val['name']] = $val;
  1278. } else if ($val['level'] == 2) {
  1279. $val['child'] = [];
  1280. $arr2[$val['parent_id']][$val['id']] = $val;
  1281. $arr4[$val['id']] = $val;
  1282. $parent_info = $arr1[$val['parent_id']];
  1283. $city_site[$parent_info['name']][$val['name']] = $val;
  1284. $tree_site[$parent_info['name']]['child'][$val['name']] = $val;
  1285. } else if ($val['level'] == 3) {
  1286. $val['child'] = [];
  1287. $arr3[$val['parent_id']][$val['id']] = $val;
  1288. $parent_info = $arr4[$val['parent_id']];
  1289. $top_info = $arr1[$val['topid']];
  1290. $area_site[$top_info['name']][$parent_info['name']][$val['name']] = $val;
  1291. $tree_site[$top_info['name']]['child'][$parent_info['name']]['child'][$val['name']] = $val;
  1292. }
  1293. }
  1294. /*---------------------多城市站点的树形结构、每个层级的结构 end---------------------*/
  1295. $err = 0;
  1296. // 批量添加一级区域
  1297. if (!empty($regionList[1])) {
  1298. $nameArr = [];
  1299. foreach ($regionList[1] as $key => $val) {
  1300. $is_add = true;
  1301. foreach ($province_site as $_k => $_v) {
  1302. if (stristr($val['name'], $_v['name'])) {
  1303. $is_add = false;
  1304. break;
  1305. }
  1306. }
  1307. if ($is_add) {
  1308. $nameArr[] = $this->handle_name($val['name']);
  1309. }
  1310. }
  1311. if (!empty($nameArr)) {
  1312. $addData = [];
  1313. foreach ($nameArr as $key => $val) {
  1314. $domain = preg_replace("/[^a-zA-Z0-9]+/", "", get_pinyin($val));
  1315. if (in_array($domain, $domainArr)) {
  1316. $domain = $this->rand_domain($domain);
  1317. }
  1318. $addData[] = [
  1319. 'name' => $val,
  1320. 'domain' => $domain,
  1321. 'level' => 1,
  1322. 'parent_id' => 0,
  1323. 'topid' => 0,
  1324. 'is_open' => !empty($post['is_open']) ? intval($post['is_open']) : 0,
  1325. 'showall' => !empty($post['showall']) ? intval($post['showall']) : 1,
  1326. 'seoset' => !empty($post['seoset']) ? intval($post['seoset']) : 0,
  1327. 'status' => !empty($post['status']) ? intval($post['status']) : 0,
  1328. 'initial' => getFirstCharter($val),
  1329. 'seo_title' => str_replace(['{region}','{区域}'], $val, $seo_title),
  1330. 'seo_keywords' => str_replace(['{region}','{区域}'], $val, $seo_keywords),
  1331. 'seo_description' => str_replace(['{region}','{区域}'], $val, $seo_description),
  1332. 'sort_order' => 100,
  1333. 'add_time' => getTime(),
  1334. 'update_time' => getTime(),
  1335. ];
  1336. }
  1337. $r = model('Citysite')->saveAll($addData);
  1338. if ($r === false) {
  1339. $err++;
  1340. } else {
  1341. foreach ($r as $k1 => $v1) {
  1342. $arr_new = $v1->getData();
  1343. $arr_new['child'] = [];
  1344. $arr1[$arr_new['id']] = $arr_new;
  1345. $province_site[$arr_new['name']] = $arr_new;
  1346. $tree_site[$arr_new['name']] = $arr_new;
  1347. }
  1348. }
  1349. }
  1350. }
  1351. // 批量添加二级区域
  1352. if (empty($err) && !empty($regionList[2])) {
  1353. $province_region = Db::name('region')->where(['level'=>1])->getAllWithIndex('id');
  1354. $nameArr = $parent_ids_arr = [];
  1355. foreach ($regionList[2] as $key => $val) {
  1356. $is_add = true;
  1357. $parent_info = $province_region[$val['parent_id']];
  1358. $parent_info['name'] = $this->handle_name($parent_info['name']);
  1359. foreach ($city_site[$parent_info['name']] as $_k => $_v) {
  1360. if (stristr($val['name'], $_v['name'])) {
  1361. $is_add = false;
  1362. break;
  1363. }
  1364. }
  1365. if ($is_add) {
  1366. $nameArr[] = $this->handle_name($val['name']);
  1367. $parent_name = $parent_info['name'];
  1368. $parent_ids_arr[] = $province_site[$parent_name]['id'];
  1369. }
  1370. }
  1371. if (!empty($nameArr)) {
  1372. $addData = [];
  1373. $domainArr = Db::name('citysite')->where(['id'=>['gt', 0]])->column('domain');
  1374. foreach ($nameArr as $key => $val) {
  1375. $domain = preg_replace("/[^a-zA-Z0-9]+/", "", get_pinyin($val));
  1376. if (in_array($domain, $domainArr)) {
  1377. $domain = $this->rand_domain($domain);
  1378. }
  1379. $addData[] = [
  1380. 'name' => $val,
  1381. 'domain' => $domain,
  1382. 'level' => 2,
  1383. 'parent_id' => $parent_ids_arr[$key],
  1384. 'topid' => $parent_ids_arr[$key],
  1385. 'is_open' => !empty($post['is_open']) ? intval($post['is_open']) : 0,
  1386. 'showall' => !empty($post['showall']) ? intval($post['showall']) : 1,
  1387. 'seoset' => !empty($post['seoset']) ? intval($post['seoset']) : 0,
  1388. 'status' => !empty($post['status']) ? intval($post['status']) : 0,
  1389. 'initial' => getFirstCharter($val),
  1390. 'seo_title' => str_replace(['{region}','{区域}'], $val, $seo_title),
  1391. 'seo_keywords' => str_replace(['{region}','{区域}'], $val, $seo_keywords),
  1392. 'seo_description' => str_replace(['{region}','{区域}'], $val, $seo_description),
  1393. 'sort_order' => 100,
  1394. 'add_time' => getTime(),
  1395. 'update_time' => getTime(),
  1396. ];
  1397. }
  1398. $r = model('Citysite')->saveAll($addData);
  1399. if ($r === false) {
  1400. $err++;
  1401. } else {
  1402. foreach ($r as $k1 => $v1) {
  1403. $arr_new = $v1->getData();
  1404. $arr_new['child'] = [];
  1405. $arr2[$arr_new['parent_id']][$arr_new['id']] = $arr_new;
  1406. $parent_info = $arr1[$arr_new['parent_id']];
  1407. $city_site[$parent_info['name']][$arr_new['name']] = $arr_new;
  1408. $tree_site[$parent_info['name']]['child'][$arr_new['name']] = $arr_new;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. // 批量添加三级区域
  1414. if (empty($err) && !empty($regionList[3])) {
  1415. $province_region = Db::name('region')->where(['level'=>1])->getAllWithIndex('id');
  1416. $city_region = Db::name('region')->where(['level'=>2])->getAllWithIndex('id');
  1417. $nameArr = $topids_arr = $parent_ids_arr = [];
  1418. foreach ($regionList[3] as $key => $val) {
  1419. $is_add = true;
  1420. $parent_info = $city_region[$val['parent_id']];
  1421. $parent_info['name'] = $this->handle_name($parent_info['name']);
  1422. $top_info = $province_region[$parent_info['parent_id']];
  1423. $top_info['name'] = $this->handle_name($top_info['name']);
  1424. foreach ($area_site[$top_info['name']][$parent_info['name']] as $_k => $_v) {
  1425. if (stristr($val['name'], $_v['name'])) {
  1426. $is_add = false;
  1427. break;
  1428. }
  1429. }
  1430. if ($is_add) {
  1431. $handle_name = $this->handle_name($val['name']);
  1432. if (!empty($handle_name)) {
  1433. $nameArr[] = $handle_name;
  1434. $top_name = $top_info['name'];
  1435. $topids_arr[] = $province_site[$top_name]['id'];
  1436. $parent_name = $parent_info['name'];
  1437. $parent_ids_arr[] = $city_site[$top_name][$parent_name]['id'];
  1438. }
  1439. }
  1440. }
  1441. if (!empty($nameArr)) {
  1442. $batch_num = 500; // 每次写入N条区域,避免写太多报500
  1443. $nameArr = array_chunk($nameArr, $batch_num);
  1444. $topids_arr = array_chunk($topids_arr, $batch_num);
  1445. $parent_ids_arr = array_chunk($parent_ids_arr, $batch_num);
  1446. foreach ($nameArr as $key_tmp => $val_tmp) {
  1447. if (!empty($val_tmp)) {
  1448. $addData = [];
  1449. $domainArr = Db::name('citysite')->where(['id'=>['gt', 0]])->column('domain');
  1450. foreach ($val_tmp as $key => $val) {
  1451. $domain = preg_replace("/[^a-zA-Z0-9]+/", "", get_pinyin($val));
  1452. if (in_array($domain, $domainArr)) {
  1453. $domain = $this->rand_domain($domain);
  1454. }
  1455. $addData[] = [
  1456. 'name' => $val,
  1457. 'domain' => $domain,
  1458. 'level' => 3,
  1459. 'parent_id' => $parent_ids_arr[$key_tmp][$key],
  1460. 'topid' => $topids_arr[$key_tmp][$key],
  1461. 'is_open' => !empty($post['is_open']) ? intval($post['is_open']) : 0,
  1462. 'showall' => !empty($post['showall']) ? intval($post['showall']) : 1,
  1463. 'seoset' => !empty($post['seoset']) ? intval($post['seoset']) : 0,
  1464. 'status' => !empty($post['status']) ? intval($post['status']) : 0,
  1465. 'initial' => getFirstCharter($val),
  1466. 'seo_title' => str_replace(['{region}','{区域}'], $val, $seo_title),
  1467. 'seo_keywords' => str_replace(['{region}','{区域}'], $val, $seo_keywords),
  1468. 'seo_description' => str_replace(['{region}','{区域}'], $val, $seo_description),
  1469. 'sort_order' => 100,
  1470. 'add_time' => getTime(),
  1471. 'update_time' => getTime(),
  1472. ];
  1473. }
  1474. $r = model('Citysite')->saveAll($addData);
  1475. if ($r === false) {
  1476. $err++;
  1477. }
  1478. }
  1479. }
  1480. }
  1481. }
  1482. if (empty($err)) {
  1483. $data = [];
  1484. // 导入全国的轮询一个个省份导入 start
  1485. if (!empty($cur_province_id)) {
  1486. $data['progress'] = 0;
  1487. $data['next_province_id'] = 0;
  1488. $num = 0;
  1489. $regionRow = Db::name('region')->where(['level'=>1])->order('id asc')->select();
  1490. foreach ($regionRow as $key => $val) {
  1491. $num++;
  1492. if ($cur_province_id < $val['id']) {
  1493. $data['next_province_id'] = $val['id'];
  1494. break;
  1495. }
  1496. }
  1497. $progress = $num / count($regionRow);
  1498. $progress = sprintf("%.2f", substr(sprintf("%.3f", $progress), 0, -1));
  1499. if ($progress == intval($progress)) {
  1500. $progress = intval($progress);
  1501. }
  1502. $data['progress'] = intval($progress * 100);
  1503. }
  1504. // 导入全国的轮询一个个省份导入 end
  1505. Cache::clear('citysite');
  1506. adminLog('一键导入全国城市');
  1507. $this->success("操作成功", null, $data);
  1508. }else{
  1509. $this->error("操作失败");
  1510. }
  1511. exit;
  1512. }
  1513. $assign_data = [];
  1514. $province_list = Db::name('region')->field('id, name')
  1515. ->where('level',1)
  1516. ->select();
  1517. foreach ($province_list as $key => $val) {
  1518. $val['name'] = preg_replace('/(省|市)$/i', '', $val['name']);
  1519. $province_list[$key] = $val;
  1520. }
  1521. $assign_data['province_list'] = $province_list;
  1522. $this->assign($assign_data);
  1523. return $this->fetch();
  1524. }
  1525. }