123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- class City extends Backend
- {
-
-
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\City;
- }
- public function import()
- {
- parent::import();
- }
-
-
-
- public function index()
- {
-
- $this->relationSearch = false;
-
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
-
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
-
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- $row->visible(['id','name','create_time']);
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
-
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- $params['eng'] = self::getFirstCharters($params['name']);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- Db::startTrans();
- try {
-
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
-
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params['eng'] = self::getFirstCharters($params['name']);
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
-
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
-
- function getFirstCharters($str)
- {
- if (empty($str)) {
- return '';
- }
-
- $temp_str = substr($str,0,1);
- if(ord($temp_str) > 127){
- $str = substr($str,0,3);
- }else{
- $str = $temp_str;
- $fchar = ord($str{0});
- if ($fchar >= ord('A') && $fchar <= ord('z')){
- return strtoupper($temp_str);
- }else{
- return null;
- }
- }
- $s1 = iconv('UTF-8', 'gb2312//IGNORE', $str);
- if(empty($s1)){
- return null;
- }
- $s2 = iconv('gb2312', 'UTF-8', $s1);
- if(empty($s2)){
- return null;
- }
- $s = $s2 == $str ? $s1 : $str;
- $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
- if ($asc >= -20319 && $asc <= -20284)
- return 'A';
- if ($asc >= -20283 && $asc <= -19776)
- return 'B';
- if ($asc >= -19775 && $asc <= -19219)
- return 'C';
- if ($asc >= -19218 && $asc <= -18711)
- return 'D';
- if ($asc >= -18710 && $asc <= -18527)
- return 'E';
- if ($asc >= -18526 && $asc <= -18240)
- return 'F';
- if ($asc >= -18239 && $asc <= -17923)
- return 'G';
- if ($asc >= -17922 && $asc <= -17418)
- return 'H';
- if ($asc >= -17417 && $asc <= -16475)
- return 'J';
- if ($asc >= -16474 && $asc <= -16213)
- return 'K';
- if ($asc >= -16212 && $asc <= -15641)
- return 'L';
- if ($asc >= -15640 && $asc <= -15166)
- return 'M';
- if ($asc >= -15165 && $asc <= -14923)
- return 'N';
- if ($asc >= -14922 && $asc <= -14915)
- return 'O';
- if ($asc >= -14914 && $asc <= -14631)
- return 'P';
- if ($asc >= -14630 && $asc <= -14150)
- return 'Q';
- if ($asc >= -14149 && $asc <= -14091)
- return 'R';
- if ($asc >= -14090 && $asc <= -13319)
- return 'S';
- if ($asc >= -13318 && $asc <= -12839)
- return 'T';
- if ($asc >= -12838 && $asc <= -12557)
- return 'W';
- if ($asc >= -12556 && $asc <= -11848)
- return 'X';
- if ($asc >= -11847 && $asc <= -11056)
- return 'Y';
- if ($asc >= -11055 && $asc <= -10247)
- return 'Z';
- return rare_words($asc);
- }
-
- public function indexx()
- {
- $data = Db::name('city')->field('id,name')->select();
- if($this->request->request("keyValue")){
- return ['total'=>1, 'list'=>$data[$this->request->request("keyValue")-1]];
- }
- return json(['list' => $data, 'total' => count($data)]);
- }
-
- public function indexxx()
- {
- $data = Db::name('city')->field('id,name')->select();
- return json($data);
- }
-
- public function indexxxx()
- {
- $data = Db::name('city')->field('id value,name')->select();
- return $this->success('','',$data);
- }
- }
|