Mobile.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 Mobile 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);
  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','proxy'])
  63. ->where($where)
  64. ->where('type',1)
  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_values(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. #置顶推荐
  92. public function batch(){
  93. $this->validate($data=input(),[
  94. 'id'=>'require',
  95. 'field'=>['require','in:rec_time,top_time'],
  96. 'status'=>'require',
  97. ]);
  98. $time=$data['status']?time():null;
  99. $this->model->where('id',$data['id'])->update([
  100. $data['field']=>$time
  101. ]);
  102. $this->success('','',[
  103. 'status'=>$time,
  104. ]);
  105. }
  106. #设为特价
  107. public function setdiscount($ids){
  108. if($this->request->isGet()){
  109. return view();
  110. }else{
  111. $this->validate($data=input('row/a'),[
  112. 'activity_time_end'=>['date','requireIf:activity_forever,0'],
  113. 'activity_forever'=>['require','in:0,1'],
  114. ]);
  115. if($data['activity_forever']){
  116. $activity_time_end=null;
  117. }else {
  118. if (strtotime($data['activity_time_end']) <= time()) {
  119. $this->error('请选择将来时间');
  120. }
  121. $activity_time_end=$data['activity_time_end'];
  122. }
  123. $this->model->whereIn('id',$ids)->update([
  124. 'is_activity'=>1,
  125. 'activity_time_end'=>$activity_time_end,
  126. ]);
  127. $this->success();
  128. }
  129. }
  130. #取消设为特价
  131. public function cancelsetdiscount(){
  132. $ids=input('ids/a');
  133. if($ids){
  134. $this->model->whereIn('id',$ids)->update([
  135. 'is_activity'=>0,
  136. 'activity_time_end'=>null,
  137. ]);
  138. }
  139. $this->success('');
  140. }
  141. #预占
  142. public function takeit($ids){
  143. $mobile=$this->model->find($ids);
  144. if($this->request->isGet()){
  145. $this->assign('row',$mobile);
  146. return view();
  147. }else{
  148. Db::startTrans();
  149. $data=input('row/a');
  150. $city=$data['city'];
  151. if($city){
  152. $ex=explode('/',$city);
  153. list($data['province'],$data['city'])=$ex;
  154. $data['province_id']=\app\common\model\Area::getIdByName($data['province']);
  155. $data['city_id']=\app\common\model\Area::getIdByName($data['city']);
  156. }
  157. (new MobilePriceLogService)->setMobile($mobile)->setAdminId($this->auth->id)->setBeforePrice($mobile['amount_base'])->setAfterPrice($data['amount_base'])->log();
  158. $mobile->allowField(true)->save($data);
  159. $describe=$data['describe']??'';
  160. $mobile->info()->update(compact('describe'));
  161. Db::commit();
  162. $this->success();
  163. }
  164. }
  165. public function edit($ids=null){
  166. $mobile=$this->model->find($ids);
  167. if($this->request->isGet()){
  168. $this->assign('row',$mobile);
  169. return view();
  170. }else{
  171. Db::startTrans();
  172. $data=input('row/a');
  173. $city=$data['city'];
  174. if($city){
  175. $ex=explode('/',$city);
  176. list($data['province'],$data['city'])=$ex;
  177. $data['province_id']=\app\common\model\Area::getIdByName($data['province']);
  178. $data['city_id']=\app\common\model\Area::getIdByName($data['city']);
  179. }
  180. (new MobilePriceLogService)->setMobile($mobile)->setAdminId($this->auth->id)->setBeforePrice($mobile['amount_base'])->setAfterPrice($data['amount_base'])->log();
  181. $mobile->allowField(true)->save($data);
  182. $describe=$data['describe']??'';
  183. $mobile->info()->update(compact('describe'));
  184. Db::commit();
  185. $this->success();
  186. }
  187. }
  188. protected function buildindexparams($searchfields = null, $relationSearch = null)
  189. {
  190. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  191. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  192. $search = $this->request->get("search", '');
  193. $filter = $this->request->get("filter", '');
  194. $op = $this->request->get("op", '', 'trim');
  195. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  196. $order = $this->request->get("order", "DESC");
  197. $offset = $this->request->get("offset/d", 0);
  198. $limit = $this->request->get("limit/d", 999999);
  199. //新增自动计算页码
  200. $page = $limit ? intval($offset / $limit) + 1 : 1;
  201. if ($this->request->has("page")) {
  202. $page = $this->request->get("page/d", 1);
  203. }
  204. $this->request->get([config('paginate.var_page') => $page]);
  205. $filter = (array)json_decode($filter, true);
  206. $op = (array)json_decode($op, true);
  207. $filter = $filter ? $filter : [];
  208. $where = [];
  209. $alias = [];
  210. $bind = [];
  211. $name = '';
  212. $aliasName = '';
  213. if (!empty($this->model) && $this->relationSearch) {
  214. $name = $this->model->getTable();
  215. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  216. $aliasName = $alias[$name] . '.';
  217. }
  218. $sortArr = explode(',', $sort);
  219. foreach ($sortArr as $index => & $item) {
  220. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  221. }
  222. unset($item);
  223. $sort = implode(',', $sortArr);
  224. $adminIds = $this->getDataLimitAdminIds();
  225. if (is_array($adminIds)) {
  226. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  227. }
  228. if ($search) {
  229. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  230. foreach ($searcharr as $k => &$v) {
  231. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  232. }
  233. unset($v);
  234. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  235. }
  236. $index = 0;
  237. foreach ($filter as $k => $v) {
  238. if($k=='rules'){
  239. continue;
  240. }
  241. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  242. continue;
  243. }
  244. $sym = isset($op[$k]) ? $op[$k] : '=';
  245. if (stripos($k, ".") === false) {
  246. $k = $aliasName . $k;
  247. }
  248. $v = !is_array($v) ? trim($v) : $v;
  249. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  250. //null和空字符串特殊处理
  251. if (!is_array($v)) {
  252. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  253. $sym = strtoupper($v);
  254. }
  255. if (in_array($v, ['""', "''"])) {
  256. $v = '';
  257. $sym = '=';
  258. }
  259. }
  260. switch ($sym) {
  261. case '=':
  262. case '<>':
  263. $where[] = [$k, $sym, (string)$v];
  264. break;
  265. case 'LIKE':
  266. case 'NOT LIKE':
  267. case 'LIKE %...%':
  268. case 'NOT LIKE %...%':
  269. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  270. break;
  271. case '>':
  272. case '>=':
  273. case '<':
  274. case '<=':
  275. $where[] = [$k, $sym, intval($v)];
  276. break;
  277. case 'FINDIN':
  278. case 'FINDINSET':
  279. case 'FIND_IN_SET':
  280. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  281. $findArr = array_values($v);
  282. foreach ($findArr as $idx => $item) {
  283. $bindName = "item_" . $index . "_" . $idx;
  284. $bind[$bindName] = $item;
  285. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  286. }
  287. break;
  288. case 'IN':
  289. case 'IN(...)':
  290. case 'NOT IN':
  291. case 'NOT IN(...)':
  292. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  293. break;
  294. case 'BETWEEN':
  295. case 'NOT BETWEEN':
  296. $arr = array_slice(explode(',', $v), 0, 2);
  297. if (stripos($v, ',') === false || !array_filter($arr)) {
  298. continue 2;
  299. }
  300. //当出现一边为空时改变操作符
  301. if ($arr[0] === '') {
  302. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  303. $arr = $arr[1];
  304. } elseif ($arr[1] === '') {
  305. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  306. $arr = $arr[0];
  307. }
  308. $where[] = [$k, $sym, $arr];
  309. break;
  310. case 'RANGE':
  311. case 'NOT RANGE':
  312. $v = str_replace(' - ', ',', $v);
  313. $arr = array_slice(explode(',', $v), 0, 2);
  314. if (stripos($v, ',') === false || !array_filter($arr)) {
  315. continue 2;
  316. }
  317. //当出现一边为空时改变操作符
  318. if ($arr[0] === '') {
  319. $sym = $sym == 'RANGE' ? '<=' : '>';
  320. $arr = $arr[1];
  321. } elseif ($arr[1] === '') {
  322. $sym = $sym == 'RANGE' ? '>=' : '<';
  323. $arr = $arr[0];
  324. }
  325. $tableArr = explode('.', $k);
  326. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  327. //修复关联模型下时间无法搜索的BUG
  328. $relation = Loader::parseName($tableArr[0], 1, false);
  329. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  330. }
  331. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  332. break;
  333. case 'NULL':
  334. case 'IS NULL':
  335. case 'NOT NULL':
  336. case 'IS NOT NULL':
  337. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  338. break;
  339. default:
  340. break;
  341. }
  342. $index++;
  343. }
  344. if (!empty($this->model)) {
  345. $this->model->alias($alias);
  346. }
  347. if(isset($filter['rules']) && $filter['rules']){
  348. $temp=[];
  349. foreach (MobileConstant::getFilters()[$filter['rules']] as $pos=>$column){
  350. $temp[]=$column;
  351. }
  352. $where[]=[implode("|",$temp),1];
  353. }
  354. $model = $this->model;
  355. $where = function ($query) use ($where, $alias, $bind, &$model) {
  356. if (!empty($model)) {
  357. $model->alias($alias);
  358. $model->bind($bind);
  359. }
  360. foreach ($where as $k => $v) {
  361. if (is_array($v)) {
  362. call_user_func_array([$query, 'where'], $v);
  363. } else {
  364. $query->where($v);
  365. }
  366. }
  367. };
  368. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  369. }
  370. public function status(){
  371. return \app\common\model\Mobile::$status;
  372. }
  373. }