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. $this->assign('status',\app\common\model\Mobile::$status);
  36. }
  37. public function import()
  38. {
  39. MobileImport::import(input('file'),$this->auth->id);
  40. $this->success();
  41. }
  42. /**
  43. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  44. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  45. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  46. */
  47. /**
  48. * 查看
  49. */
  50. public function index()
  51. {
  52. //当前是否为关联查询
  53. $this->relationSearch = false;
  54. //设置过滤方法
  55. $this->request->filter(['strip_tags', 'trim']);
  56. if ($this->request->isAjax()) {
  57. //如果发送的来源是Selectpage,则转发到Selectpage
  58. if ($this->request->request('keyField')) {
  59. return $this->selectpage();
  60. }
  61. list($where, $sort, $order, $offset, $limit) = $this->buildindexparams();
  62. $list = $this->model
  63. ->with(['info','proxy'])
  64. ->where($where)
  65. ->where('type',1)
  66. ->order($sort, $order)
  67. ->paginate($limit);
  68. foreach ($list as $row) {
  69. $rules=[];
  70. foreach (MobileConstant::getFilters() as $rule=>$field){
  71. foreach (array_values($field) as $column){
  72. if($row[$column]==1){
  73. $rules[]=$rule;
  74. }
  75. }
  76. }
  77. $row['rules']=array_values(array_unique($rules));
  78. }
  79. $result = array("total" => $list->total(), "rows" => $list->items());
  80. return json($result);
  81. }
  82. $this->assign('no_type',array_column(MobileConstant::getNoType(),'name','id'));
  83. $this->assign('network',array_column(MobileConstant::getNetwork(),'name','id'));
  84. $this->assign('filters',MobileConstant::getFilters());
  85. return $this->view->fetch();
  86. }
  87. public function constant(){
  88. return json([
  89. 'no_type'=>MobileConstant::getNoType(),
  90. ]);
  91. }
  92. #置顶推荐
  93. public function batch(){
  94. $this->validate($data=input(),[
  95. 'id'=>'require',
  96. 'field'=>['require','in:rec_time,top_time'],
  97. 'status'=>'require',
  98. ]);
  99. $time=$data['status']?time():null;
  100. $this->model->where('id',$data['id'])->update([
  101. $data['field']=>$time
  102. ]);
  103. $this->success('','',[
  104. 'status'=>$time,
  105. ]);
  106. }
  107. #设为特价
  108. public function setdiscount($ids){
  109. if($this->request->isGet()){
  110. return view();
  111. }else{
  112. $this->validate($data=input('row/a'),[
  113. 'activity_time_end'=>['date','requireIf:activity_forever,0'],
  114. 'activity_forever'=>['require','in:0,1'],
  115. ]);
  116. if($data['activity_forever']){
  117. $activity_time_end=null;
  118. }else {
  119. if (strtotime($data['activity_time_end']) <= time()) {
  120. $this->error('请选择将来时间');
  121. }
  122. $activity_time_end=$data['activity_time_end'];
  123. }
  124. $this->model->whereIn('id',$ids)->update([
  125. 'is_activity'=>1,
  126. 'activity_time_end'=>$activity_time_end,
  127. ]);
  128. $this->success();
  129. }
  130. }
  131. #取消设为特价
  132. public function cancelsetdiscount(){
  133. $ids=input('ids/a');
  134. if($ids){
  135. $this->model->whereIn('id',$ids)->update([
  136. 'is_activity'=>0,
  137. 'activity_time_end'=>null,
  138. ]);
  139. }
  140. $this->success('');
  141. }
  142. #预占
  143. public function takeit($ids){
  144. $mobile=$this->model->find($ids);
  145. if($this->request->isGet()){
  146. $this->assign('row',$mobile);
  147. return view();
  148. }else{
  149. Db::startTrans();
  150. $data=input('row/a');
  151. $city=$data['city'];
  152. if($city){
  153. $ex=explode('/',$city);
  154. list($data['province'],$data['city'])=$ex;
  155. $data['province_id']=\app\common\model\Area::getIdByName($data['province']);
  156. $data['city_id']=\app\common\model\Area::getIdByName($data['city']);
  157. }
  158. (new MobilePriceLogService)->setMobile($mobile)->setAdminId($this->auth->id)->setBeforePrice($mobile['amount_base'])->setAfterPrice($data['amount_base'])->log();
  159. $mobile->allowField(true)->save($data);
  160. $describe=$data['describe']??'';
  161. $mobile->info()->update(compact('describe'));
  162. Db::commit();
  163. $this->success();
  164. }
  165. }
  166. public function edit($ids=null){
  167. $mobile=$this->model->find($ids);
  168. if($this->request->isGet()){
  169. $this->assign('row',$mobile);
  170. return view();
  171. }else{
  172. Db::startTrans();
  173. $data=input('row/a');
  174. $city=$data['city'];
  175. if($city){
  176. $ex=explode('/',$city);
  177. list($data['province'],$data['city'])=$ex;
  178. $data['province_id']=\app\common\model\Area::getIdByName($data['province']);
  179. $data['city_id']=\app\common\model\Area::getIdByName($data['city']);
  180. }
  181. (new MobilePriceLogService)->setMobile($mobile)->setAdminId($this->auth->id)->setBeforePrice($mobile['amount_base'])->setAfterPrice($data['amount_base'])->log();
  182. $mobile->allowField(true)->save($data);
  183. $describe=$data['describe']??'';
  184. $mobile->info()->update(compact('describe'));
  185. Db::commit();
  186. $this->success();
  187. }
  188. }
  189. protected function buildindexparams($searchfields = null, $relationSearch = null)
  190. {
  191. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  192. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  193. $search = $this->request->get("search", '');
  194. $filter = $this->request->get("filter", '');
  195. $op = $this->request->get("op", '', 'trim');
  196. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  197. $order = $this->request->get("order", "DESC");
  198. $offset = $this->request->get("offset/d", 0);
  199. $limit = $this->request->get("limit/d", 999999);
  200. //新增自动计算页码
  201. $page = $limit ? intval($offset / $limit) + 1 : 1;
  202. if ($this->request->has("page")) {
  203. $page = $this->request->get("page/d", 1);
  204. }
  205. $this->request->get([config('paginate.var_page') => $page]);
  206. $filter = (array)json_decode($filter, true);
  207. $op = (array)json_decode($op, true);
  208. $filter = $filter ? $filter : [];
  209. $where = [];
  210. $alias = [];
  211. $bind = [];
  212. $name = '';
  213. $aliasName = '';
  214. if (!empty($this->model) && $this->relationSearch) {
  215. $name = $this->model->getTable();
  216. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  217. $aliasName = $alias[$name] . '.';
  218. }
  219. $sortArr = explode(',', $sort);
  220. foreach ($sortArr as $index => & $item) {
  221. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  222. }
  223. unset($item);
  224. $sort = implode(',', $sortArr);
  225. $adminIds = $this->getDataLimitAdminIds();
  226. if (is_array($adminIds)) {
  227. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  228. }
  229. if ($search) {
  230. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  231. foreach ($searcharr as $k => &$v) {
  232. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  233. }
  234. unset($v);
  235. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  236. }
  237. $index = 0;
  238. foreach ($filter as $k => $v) {
  239. if($k=='rules'){
  240. continue;
  241. }
  242. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  243. continue;
  244. }
  245. $sym = isset($op[$k]) ? $op[$k] : '=';
  246. if (stripos($k, ".") === false) {
  247. $k = $aliasName . $k;
  248. }
  249. $v = !is_array($v) ? trim($v) : $v;
  250. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  251. //null和空字符串特殊处理
  252. if (!is_array($v)) {
  253. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  254. $sym = strtoupper($v);
  255. }
  256. if (in_array($v, ['""', "''"])) {
  257. $v = '';
  258. $sym = '=';
  259. }
  260. }
  261. switch ($sym) {
  262. case '=':
  263. case '<>':
  264. $where[] = [$k, $sym, (string)$v];
  265. break;
  266. case 'LIKE':
  267. case 'NOT LIKE':
  268. case 'LIKE %...%':
  269. case 'NOT LIKE %...%':
  270. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  271. break;
  272. case '>':
  273. case '>=':
  274. case '<':
  275. case '<=':
  276. $where[] = [$k, $sym, intval($v)];
  277. break;
  278. case 'FINDIN':
  279. case 'FINDINSET':
  280. case 'FIND_IN_SET':
  281. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  282. $findArr = array_values($v);
  283. foreach ($findArr as $idx => $item) {
  284. $bindName = "item_" . $index . "_" . $idx;
  285. $bind[$bindName] = $item;
  286. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  287. }
  288. break;
  289. case 'IN':
  290. case 'IN(...)':
  291. case 'NOT IN':
  292. case 'NOT IN(...)':
  293. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  294. break;
  295. case 'BETWEEN':
  296. case 'NOT BETWEEN':
  297. $arr = array_slice(explode(',', $v), 0, 2);
  298. if (stripos($v, ',') === false || !array_filter($arr)) {
  299. continue 2;
  300. }
  301. //当出现一边为空时改变操作符
  302. if ($arr[0] === '') {
  303. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  304. $arr = $arr[1];
  305. } elseif ($arr[1] === '') {
  306. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  307. $arr = $arr[0];
  308. }
  309. $where[] = [$k, $sym, $arr];
  310. break;
  311. case 'RANGE':
  312. case 'NOT RANGE':
  313. $v = str_replace(' - ', ',', $v);
  314. $arr = array_slice(explode(',', $v), 0, 2);
  315. if (stripos($v, ',') === false || !array_filter($arr)) {
  316. continue 2;
  317. }
  318. //当出现一边为空时改变操作符
  319. if ($arr[0] === '') {
  320. $sym = $sym == 'RANGE' ? '<=' : '>';
  321. $arr = $arr[1];
  322. } elseif ($arr[1] === '') {
  323. $sym = $sym == 'RANGE' ? '>=' : '<';
  324. $arr = $arr[0];
  325. }
  326. $tableArr = explode('.', $k);
  327. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  328. //修复关联模型下时间无法搜索的BUG
  329. $relation = Loader::parseName($tableArr[0], 1, false);
  330. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  331. }
  332. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  333. break;
  334. case 'NULL':
  335. case 'IS NULL':
  336. case 'NOT NULL':
  337. case 'IS NOT NULL':
  338. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  339. break;
  340. default:
  341. break;
  342. }
  343. $index++;
  344. }
  345. if (!empty($this->model)) {
  346. $this->model->alias($alias);
  347. }
  348. if(isset($filter['rules']) && $filter['rules']){
  349. $temp=[];
  350. foreach (MobileConstant::getFilters()[$filter['rules']] as $pos=>$column){
  351. $temp[]=$column;
  352. }
  353. $where[]=[implode("|",$temp),1];
  354. }
  355. $model = $this->model;
  356. $where = function ($query) use ($where, $alias, $bind, &$model) {
  357. if (!empty($model)) {
  358. $model->alias($alias);
  359. $model->bind($bind);
  360. }
  361. foreach ($where as $k => $v) {
  362. if (is_array($v)) {
  363. call_user_func_array([$query, 'where'], $v);
  364. } else {
  365. $query->where($v);
  366. }
  367. }
  368. };
  369. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  370. }
  371. public function status(){
  372. return \app\common\model\Mobile::$status;
  373. }
  374. }