MobileFlow.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\library\Auth;
  4. use app\common\controller\Backend;
  5. use app\common\library\MobileConstant;
  6. use app\common\service\MobileImport;
  7. use app\common\service\MobilePriceLogService;
  8. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  9. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  10. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  11. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  12. use think\App;
  13. use think\Db;
  14. use think\Exception;
  15. use think\exception\PDOException;
  16. use think\Loader;
  17. use think\Url;
  18. /**
  19. *
  20. *
  21. * @icon fa fa-mobile
  22. */
  23. class MobileFlow extends Backend
  24. {
  25. /**
  26. * Mobile模型对象
  27. * @var \app\admin\model\Mobile
  28. */
  29. protected $model = null;
  30. protected $relationSearch=true;
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = new \app\admin\model\Mobile;
  35. }
  36. public function import()
  37. {
  38. MobileImport::import(input('file'),$this->auth->id,2);
  39. $this->success();
  40. }
  41. /**
  42. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  43. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  44. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  45. */
  46. /**
  47. * 查看
  48. */
  49. public function index()
  50. {
  51. //当前是否为关联查询
  52. $this->relationSearch = false;
  53. //设置过滤方法
  54. $this->request->filter(['strip_tags', 'trim']);
  55. if ($this->request->isAjax()) {
  56. //如果发送的来源是Selectpage,则转发到Selectpage
  57. if ($this->request->request('keyField')) {
  58. return $this->selectpage();
  59. }
  60. list($where, $sort, $order, $offset, $limit) = $this->buildindexparams();
  61. $list = $this->model
  62. ->with(['info'])
  63. ->where('type',2)
  64. ->where($where)
  65. ->order($sort, $order)
  66. ->paginate($limit);
  67. foreach ($list as $row) {
  68. $rules=[];
  69. foreach (MobileConstant::getFilters() as $rule=>$field){
  70. foreach (array_values($field) as $column){
  71. if($row[$column]==1){
  72. $rules[]=$rule;
  73. }
  74. }
  75. }
  76. $row['rules']=array_unique($rules);
  77. }
  78. $result = array("total" => $list->total(), "rows" => $list->items());
  79. return json($result);
  80. }
  81. $this->assign('no_type',array_column(MobileConstant::getNoType(),'name','id'));
  82. $this->assign('network',array_column(MobileConstant::getNetwork(),'name','id'));
  83. $this->assign('filters',MobileConstant::getFilters());
  84. return $this->view->fetch();
  85. }
  86. public function constant(){
  87. return json([
  88. 'no_type'=>MobileConstant::getNoType(),
  89. ]);
  90. }
  91. public function edit($ids=null){
  92. $mobile=$this->model->find($ids);
  93. if($this->request->isGet()){
  94. $this->assign('row',$mobile);
  95. return view();
  96. }else{
  97. Db::startTrans();
  98. $data=input('row/a');
  99. $city=$data['city'];
  100. if($city){
  101. $ex=explode('/',$city);
  102. list($data['province'],$data['city'])=$ex;
  103. $data['province_id']=\app\common\model\Area::getIdByName($data['province']);
  104. $data['city_id']=\app\common\model\Area::getIdByName($data['city']);
  105. }
  106. $mobile->allowField(true)->save($data);
  107. $describe=$data['describe']??'';
  108. $content=$data['content']??'';
  109. $flow_year=$data['flow_year']??'';
  110. $fee=$data['fee']??'';
  111. $first_month_free=$data['first_month_free']??0;
  112. $flow_free_limit=$data['flow_free_limit']??0;
  113. $mobile->info()->update(compact('describe','content','flow_year','fee','first_month_free','flow_free_limit'));
  114. Db::commit();
  115. $this->success();
  116. }
  117. }
  118. protected function buildindexparams($searchfields = null, $relationSearch = null)
  119. {
  120. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  121. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  122. $search = $this->request->get("search", '');
  123. $filter = $this->request->get("filter", '');
  124. $op = $this->request->get("op", '', 'trim');
  125. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  126. $order = $this->request->get("order", "DESC");
  127. $offset = $this->request->get("offset/d", 0);
  128. $limit = $this->request->get("limit/d", 999999);
  129. //新增自动计算页码
  130. $page = $limit ? intval($offset / $limit) + 1 : 1;
  131. if ($this->request->has("page")) {
  132. $page = $this->request->get("page/d", 1);
  133. }
  134. $this->request->get([config('paginate.var_page') => $page]);
  135. $filter = (array)json_decode($filter, true);
  136. $op = (array)json_decode($op, true);
  137. $filter = $filter ? $filter : [];
  138. $where = [];
  139. $alias = [];
  140. $bind = [];
  141. $name = '';
  142. $aliasName = '';
  143. if (!empty($this->model) && $this->relationSearch) {
  144. $name = $this->model->getTable();
  145. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  146. $aliasName = $alias[$name] . '.';
  147. }
  148. $sortArr = explode(',', $sort);
  149. foreach ($sortArr as $index => & $item) {
  150. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  151. }
  152. unset($item);
  153. $sort = implode(',', $sortArr);
  154. $adminIds = $this->getDataLimitAdminIds();
  155. if (is_array($adminIds)) {
  156. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  157. }
  158. if ($search) {
  159. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  160. foreach ($searcharr as $k => &$v) {
  161. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  162. }
  163. unset($v);
  164. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  165. }
  166. $index = 0;
  167. foreach ($filter as $k => $v) {
  168. if($k=='rules'){
  169. continue;
  170. }
  171. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  172. continue;
  173. }
  174. $sym = isset($op[$k]) ? $op[$k] : '=';
  175. if (stripos($k, ".") === false) {
  176. $k = $aliasName . $k;
  177. }
  178. $v = !is_array($v) ? trim($v) : $v;
  179. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  180. //null和空字符串特殊处理
  181. if (!is_array($v)) {
  182. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  183. $sym = strtoupper($v);
  184. }
  185. if (in_array($v, ['""', "''"])) {
  186. $v = '';
  187. $sym = '=';
  188. }
  189. }
  190. switch ($sym) {
  191. case '=':
  192. case '<>':
  193. $where[] = [$k, $sym, (string)$v];
  194. break;
  195. case 'LIKE':
  196. case 'NOT LIKE':
  197. case 'LIKE %...%':
  198. case 'NOT LIKE %...%':
  199. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  200. break;
  201. case '>':
  202. case '>=':
  203. case '<':
  204. case '<=':
  205. $where[] = [$k, $sym, intval($v)];
  206. break;
  207. case 'FINDIN':
  208. case 'FINDINSET':
  209. case 'FIND_IN_SET':
  210. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  211. $findArr = array_values($v);
  212. foreach ($findArr as $idx => $item) {
  213. $bindName = "item_" . $index . "_" . $idx;
  214. $bind[$bindName] = $item;
  215. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  216. }
  217. break;
  218. case 'IN':
  219. case 'IN(...)':
  220. case 'NOT IN':
  221. case 'NOT IN(...)':
  222. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  223. break;
  224. case 'BETWEEN':
  225. case 'NOT BETWEEN':
  226. $arr = array_slice(explode(',', $v), 0, 2);
  227. if (stripos($v, ',') === false || !array_filter($arr)) {
  228. continue 2;
  229. }
  230. //当出现一边为空时改变操作符
  231. if ($arr[0] === '') {
  232. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  233. $arr = $arr[1];
  234. } elseif ($arr[1] === '') {
  235. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  236. $arr = $arr[0];
  237. }
  238. $where[] = [$k, $sym, $arr];
  239. break;
  240. case 'RANGE':
  241. case 'NOT RANGE':
  242. $v = str_replace(' - ', ',', $v);
  243. $arr = array_slice(explode(',', $v), 0, 2);
  244. if (stripos($v, ',') === false || !array_filter($arr)) {
  245. continue 2;
  246. }
  247. //当出现一边为空时改变操作符
  248. if ($arr[0] === '') {
  249. $sym = $sym == 'RANGE' ? '<=' : '>';
  250. $arr = $arr[1];
  251. } elseif ($arr[1] === '') {
  252. $sym = $sym == 'RANGE' ? '>=' : '<';
  253. $arr = $arr[0];
  254. }
  255. $tableArr = explode('.', $k);
  256. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  257. //修复关联模型下时间无法搜索的BUG
  258. $relation = Loader::parseName($tableArr[0], 1, false);
  259. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  260. }
  261. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  262. break;
  263. case 'NULL':
  264. case 'IS NULL':
  265. case 'NOT NULL':
  266. case 'IS NOT NULL':
  267. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  268. break;
  269. default:
  270. break;
  271. }
  272. $index++;
  273. }
  274. if (!empty($this->model)) {
  275. $this->model->alias($alias);
  276. }
  277. if(isset($filter['rules']) && $filter['rules']){
  278. $temp=[];
  279. foreach (MobileConstant::getFilters()[$filter['rules']] as $pos=>$column){
  280. $temp[]=$column;
  281. }
  282. $where[]=[implode("|",$temp),1];
  283. }
  284. $model = $this->model;
  285. $where = function ($query) use ($where, $alias, $bind, &$model) {
  286. if (!empty($model)) {
  287. $model->alias($alias);
  288. $model->bind($bind);
  289. }
  290. foreach ($where as $k => $v) {
  291. if (is_array($v)) {
  292. call_user_func_array([$query, 'where'], $v);
  293. } else {
  294. $query->where($v);
  295. }
  296. }
  297. };
  298. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  299. }
  300. }