City.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 城市设置
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class City extends Backend
  13. {
  14. /**
  15. * City模型对象
  16. * @var \app\admin\model\City
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\City;
  23. }
  24. public function import()
  25. {
  26. parent::import();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = false;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->visible(['id','name','create_time']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $params = $this->request->post("row/a");
  67. if ($params) {
  68. $params = $this->preExcludeFields($params);
  69. $params['eng'] = self::getFirstCharters($params['name']);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $params[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  80. $this->model->validateFailException(true)->validate($validate);
  81. }
  82. $result = $this->model->allowField(true)->save($params);
  83. Db::commit();
  84. } catch (ValidateException $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. } catch (PDOException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (Exception $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. }
  94. if ($result !== false) {
  95. $this->success();
  96. } else {
  97. $this->error(__('No rows were inserted'));
  98. }
  99. }
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. return $this->view->fetch();
  103. }
  104. /**
  105. * 编辑
  106. */
  107. public function edit($ids = null)
  108. {
  109. $row = $this->model->get($ids);
  110. if (!$row) {
  111. $this->error(__('No Results were found'));
  112. }
  113. $adminIds = $this->getDataLimitAdminIds();
  114. if (is_array($adminIds)) {
  115. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. }
  119. if ($this->request->isPost()) {
  120. $params = $this->request->post("row/a");
  121. if ($params) {
  122. $params['eng'] = self::getFirstCharters($params['name']);
  123. $params = $this->preExcludeFields($params);
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException(true)->validate($validate);
  132. }
  133. $result = $row->allowField(true)->save($params);
  134. Db::commit();
  135. } catch (ValidateException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (PDOException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. if ($result !== false) {
  146. $this->success();
  147. } else {
  148. $this->error(__('No rows were updated'));
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $this->view->assign("row", $row);
  154. return $this->view->fetch();
  155. }
  156. //获取汉字的首字母
  157. function getFirstCharters($str)
  158. {
  159. if (empty($str)) {
  160. return '';
  161. }
  162. //取出参数字符串中的首个字符
  163. $temp_str = substr($str,0,1);
  164. if(ord($temp_str) > 127){
  165. $str = substr($str,0,3);
  166. }else{
  167. $str = $temp_str;
  168. $fchar = ord($str{0});
  169. if ($fchar >= ord('A') && $fchar <= ord('z')){
  170. return strtoupper($temp_str);
  171. }else{
  172. return null;
  173. }
  174. }
  175. $s1 = iconv('UTF-8', 'gb2312//IGNORE', $str);
  176. if(empty($s1)){
  177. return null;
  178. }
  179. $s2 = iconv('gb2312', 'UTF-8', $s1);
  180. if(empty($s2)){
  181. return null;
  182. }
  183. $s = $s2 == $str ? $s1 : $str;
  184. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  185. if ($asc >= -20319 && $asc <= -20284)
  186. return 'A';
  187. if ($asc >= -20283 && $asc <= -19776)
  188. return 'B';
  189. if ($asc >= -19775 && $asc <= -19219)
  190. return 'C';
  191. if ($asc >= -19218 && $asc <= -18711)
  192. return 'D';
  193. if ($asc >= -18710 && $asc <= -18527)
  194. return 'E';
  195. if ($asc >= -18526 && $asc <= -18240)
  196. return 'F';
  197. if ($asc >= -18239 && $asc <= -17923)
  198. return 'G';
  199. if ($asc >= -17922 && $asc <= -17418)
  200. return 'H';
  201. if ($asc >= -17417 && $asc <= -16475)
  202. return 'J';
  203. if ($asc >= -16474 && $asc <= -16213)
  204. return 'K';
  205. if ($asc >= -16212 && $asc <= -15641)
  206. return 'L';
  207. if ($asc >= -15640 && $asc <= -15166)
  208. return 'M';
  209. if ($asc >= -15165 && $asc <= -14923)
  210. return 'N';
  211. if ($asc >= -14922 && $asc <= -14915)
  212. return 'O';
  213. if ($asc >= -14914 && $asc <= -14631)
  214. return 'P';
  215. if ($asc >= -14630 && $asc <= -14150)
  216. return 'Q';
  217. if ($asc >= -14149 && $asc <= -14091)
  218. return 'R';
  219. if ($asc >= -14090 && $asc <= -13319)
  220. return 'S';
  221. if ($asc >= -13318 && $asc <= -12839)
  222. return 'T';
  223. if ($asc >= -12838 && $asc <= -12557)
  224. return 'W';
  225. if ($asc >= -12556 && $asc <= -11848)
  226. return 'X';
  227. if ($asc >= -11847 && $asc <= -11056)
  228. return 'Y';
  229. if ($asc >= -11055 && $asc <= -10247)
  230. return 'Z';
  231. return rare_words($asc);
  232. }
  233. // 添加删除一级列表返回数据格式
  234. public function indexx()
  235. {
  236. $data = Db::name('city')->field('id,name')->select();
  237. if($this->request->request("keyValue")){
  238. return ['total'=>1, 'list'=>$data[$this->request->request("keyValue")-1]];
  239. }
  240. return json(['list' => $data, 'total' => count($data)]);
  241. }
  242. // 一级js搜索返回数据格式
  243. public function indexxx()
  244. {
  245. $data = Db::name('city')->field('id,name')->select();
  246. return json($data);
  247. }
  248. // js二级联动搜索和添加删除二级联动返回数据格式
  249. public function indexxxx()
  250. {
  251. $data = Db::name('city')->field('id value,name')->select();
  252. return $this->success('','',$data);
  253. }
  254. }