You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

510 lines
19 KiB

  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\admin\model\unishop\Area;
  4. use app\admin\model\unishop\OrderRefund;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\Hook;
  11. /**
  12. * 订单管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Order extends Backend
  17. {
  18. /**
  19. * 是否是关联查询
  20. */
  21. protected $relationSearch = true;
  22. /**
  23. * Order模型对象
  24. * @var \app\admin\model\unishop\Order
  25. */
  26. protected $model = null;
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\admin\model\unishop\Order;
  31. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  32. $this->view->assign("statusList", $this->model->getStatusList());
  33. $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
  34. }
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags']);
  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. $total = $this->model
  49. ->alias('order')
  50. ->join('user', 'user.id = order.user_id')
  51. ->where($where)
  52. ->count();
  53. $list = $this->model
  54. ->alias('order')
  55. ->join('user', 'user.id = order.user_id')
  56. ->where($where)
  57. ->order($sort, $order)
  58. ->limit($offset, $limit)
  59. ->field('order.*,user.username')
  60. ->select();
  61. $list = collection($list)->toArray();
  62. foreach ($list as &$item) {
  63. $item['id'] = (string)$item['id']; // 整形数字太大js会失准
  64. $item['user'] = [];
  65. $item['user']['username'] = $item['username'] ? $item['username'] : __('Tourist');
  66. $item['have_paid_status'] = $item['have_paid'];
  67. $item['have_delivered_status'] = $item['have_delivered'];
  68. $item['have_received_status'] = $item['have_received'];
  69. $item['have_commented_status'] = $item['have_commented'];
  70. }
  71. $result = array("total" => $total, "rows" => $list);
  72. return json($result);
  73. }
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 生成查询所需要的条件,排序方式
  78. * @param mixed $searchfields 快速查询的字段
  79. * @param boolean $relationSearch 是否关联查询
  80. * @return array
  81. */
  82. protected function buildparams($searchfields = null, $relationSearch = null)
  83. {
  84. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  85. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  86. $search = $this->request->get("search", '');
  87. $filter = $this->request->get("filter", '');
  88. $op = $this->request->get("op", '', 'trim');
  89. $sort = $this->request->get("sort", "id");
  90. $order = $this->request->get("order", "DESC");
  91. $offset = $this->request->get("offset", 0);
  92. $limit = $this->request->get("limit", 0);
  93. $filter = (array)json_decode($filter, true);
  94. $op = (array)json_decode($op, true);
  95. $filter = $filter ? $filter : [];
  96. $where = [];
  97. $tableName = '';
  98. if ($relationSearch) {
  99. if (!empty($this->model)) {
  100. $name = \think\Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  101. $tableName = '' . $name . '.';
  102. }
  103. $sortArr = explode(',', $sort);
  104. foreach ($sortArr as $index => & $item) {
  105. $item = stripos($item, ".") === false ? $tableName . trim($item) : $item;
  106. }
  107. unset($item);
  108. $sort = implode(',', $sortArr);
  109. }
  110. $adminIds = $this->getDataLimitAdminIds();
  111. if (is_array($adminIds)) {
  112. $where[] = [$tableName . $this->dataLimitField, 'in', $adminIds];
  113. }
  114. if ($search) {
  115. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  116. foreach ($searcharr as $k => &$v) {
  117. $v = stripos($v, ".") === false ? $tableName . $v : $v;
  118. }
  119. unset($v);
  120. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  121. }
  122. foreach ($filter as $k => $v) {
  123. // 搜索订单状态
  124. if (in_array($k, ['have_paid_status', 'have_delivered_status', 'have_received_status', 'have_commented_status'])) {
  125. switch ($k) {
  126. case 'have_paid_status':
  127. $k = 'have_paid';
  128. break;
  129. case 'have_delivered_status':
  130. $k = 'have_delivered';
  131. break;
  132. case 'have_received_status':
  133. $k = 'have_received';
  134. break;
  135. case 'have_commented_status':
  136. $k = 'have_commented';
  137. break;
  138. }
  139. $v == 0 ? ($op[$k] = '=') : ($op[$k] = '>');
  140. $v = 0;
  141. }
  142. $sym = isset($op[$k]) ? $op[$k] : '=';
  143. if (stripos($k, ".") === false) {
  144. $k = $tableName . $k;
  145. }
  146. $v = !is_array($v) ? trim($v) : $v;
  147. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  148. switch ($sym) {
  149. case '=':
  150. case '<>':
  151. $where[] = [$k, $sym, (string)$v];
  152. break;
  153. case 'LIKE':
  154. case 'NOT LIKE':
  155. case 'LIKE %...%':
  156. case 'NOT LIKE %...%':
  157. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  158. break;
  159. case '>':
  160. case '>=':
  161. case '<':
  162. case '<=':
  163. $where[] = [$k, $sym, intval($v)];
  164. break;
  165. case 'FINDIN':
  166. case 'FINDINSET':
  167. case 'FIND_IN_SET':
  168. $where[] = "FIND_IN_SET('{$v}', " . ($relationSearch ? $k : '`' . str_replace('.', '`.`', $k) . '`') . ")";
  169. break;
  170. case 'IN':
  171. case 'IN(...)':
  172. case 'NOT IN':
  173. case 'NOT IN(...)':
  174. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  175. break;
  176. case 'BETWEEN':
  177. case 'NOT BETWEEN':
  178. $arr = array_slice(explode(',', $v), 0, 2);
  179. if (stripos($v, ',') === false || !array_filter($arr)) {
  180. continue 2;
  181. }
  182. //当出现一边为空时改变操作符
  183. if ($arr[0] === '') {
  184. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  185. $arr = $arr[1];
  186. } elseif ($arr[1] === '') {
  187. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  188. $arr = $arr[0];
  189. }
  190. $where[] = [$k, $sym, $arr];
  191. break;
  192. case 'RANGE':
  193. case 'NOT RANGE':
  194. $v = str_replace(' - ', ',', $v);
  195. $arr = array_slice(explode(',', $v), 0, 2);
  196. if (stripos($v, ',') === false || !array_filter($arr)) {
  197. continue 2;
  198. }
  199. //当出现一边为空时改变操作符
  200. if ($arr[0] === '') {
  201. $sym = $sym == 'RANGE' ? '<=' : '>';
  202. $arr = $arr[1];
  203. } elseif ($arr[1] === '') {
  204. $sym = $sym == 'RANGE' ? '>=' : '<';
  205. $arr = $arr[0];
  206. }
  207. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr];
  208. break;
  209. case 'LIKE':
  210. case 'LIKE %...%':
  211. $where[] = [$k, 'LIKE', "%{$v}%"];
  212. break;
  213. case 'NULL':
  214. case 'IS NULL':
  215. case 'NOT NULL':
  216. case 'IS NOT NULL':
  217. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. $where = function ($query) use ($where) {
  224. foreach ($where as $k => $v) {
  225. if (is_array($v)) {
  226. call_user_func_array([$query, 'where'], $v);
  227. } else {
  228. $query->where($v);
  229. }
  230. }
  231. };
  232. return [$where, $sort, $order, $offset, $limit];
  233. }
  234. /**
  235. * 编辑
  236. */
  237. public function edit($ids = null)
  238. {
  239. $row = $this->model->get($ids);
  240. if (!$row) {
  241. $this->error(__('No Results were found'));
  242. }
  243. $adminIds = $this->getDataLimitAdminIds();
  244. if (is_array($adminIds)) {
  245. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  246. $this->error(__('You have no permission'));
  247. }
  248. }
  249. if ($this->request->isPost()) {
  250. $params = $this->request->post("row/a");
  251. if ($params) {
  252. $params = $this->preExcludeFields($params);
  253. $result = false;
  254. Db::startTrans();
  255. try {
  256. //是否采用模型验证
  257. if ($this->modelValidate) {
  258. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  259. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  260. $row->validateFailException(true)->validate($validate);
  261. }
  262. $updatetime = $this->request->post('updatetime');
  263. // 乐观锁
  264. $result = $this->model->allowField(true)->save($params, ['id' => $ids, 'updatetime' => $updatetime]);
  265. if (!$result) {
  266. throw new Exception(__('Data had been update before saved, close windows and do it again'));
  267. }
  268. Db::commit();
  269. } catch (ValidateException $e) {
  270. Db::rollback();
  271. $this->error($e->getMessage());
  272. } catch (PDOException $e) {
  273. Db::rollback();
  274. $this->error($e->getMessage());
  275. } catch (Exception $e) {
  276. Db::rollback();
  277. $this->error($e->getMessage());
  278. }
  279. if ($result !== false) {
  280. $this->success();
  281. } else {
  282. $this->error(__('No rows were updated'));
  283. }
  284. }
  285. $this->error(__('Parameter %s can not be empty', ''));
  286. }
  287. $this->view->assign("row", $row);
  288. return $this->view->fetch();
  289. }
  290. /**
  291. * 物流管理
  292. */
  293. public function delivery($ids = null)
  294. {
  295. $row = $this->model->get($ids, ['extend']);
  296. if (!$row) {
  297. $this->error(__('No Results were found'));
  298. }
  299. $adminIds = $this->getDataLimitAdminIds();
  300. if (is_array($adminIds)) {
  301. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  302. $this->error(__('You have no permission'));
  303. }
  304. }
  305. if ($this->request->isPost()) {
  306. $result = false;
  307. Db::startTrans();
  308. try {
  309. $express_number = $this->request->post('express_number');
  310. $have_delivered = $express_number ? time() : 0;
  311. $res1 = $row->allowField(true)->save(['have_delivered' => $have_delivered]);
  312. $res2 = $row->extend->allowField(true)->save(['express_number' => $express_number]);
  313. if ($res1 && $res2) {
  314. $result = true;
  315. } else {
  316. throw new Exception(__('No rows were updated'));
  317. }
  318. Db::commit();
  319. } catch (ValidateException $e) {
  320. Db::rollback();
  321. $this->error($e->getMessage());
  322. } catch (PDOException $e) {
  323. Db::rollback();
  324. $this->error($e->getMessage());
  325. } catch (Exception $e) {
  326. Db::rollback();
  327. $this->error($e->getMessage());
  328. }
  329. if ($result !== false) {
  330. $this->success();
  331. } else {
  332. $this->error(__('No rows were updated'));
  333. }
  334. $this->error(__('Parameter %s can not be empty', ''));
  335. }
  336. $address = json_decode($row->extend->address_json,true);
  337. if ($address) {
  338. $area = (new Area)->whereIn('id',[$address['province_id'],$address['city_id'],$address['area_id']])->column('name', 'id');
  339. $row['addressText'] = $area[$address['province_id']].$area[$address['city_id']].$area[$address['area_id']].' '.$address['address'];
  340. $row['address'] = $address;
  341. }
  342. $this->view->assign("row", $row);
  343. return $this->view->fetch();
  344. }
  345. /**
  346. * 商品管理
  347. */
  348. public function product($ids = null)
  349. {
  350. if ($this->request->isPost()) {
  351. $this->success();
  352. }
  353. $row = $this->model->get($ids, ['product','evaluate']);
  354. $this->view->assign('product', $row->product);
  355. $evaluate = [];
  356. foreach ($row->evaluate as $key => $item) {
  357. $evaluate[$item['product_id']] = $item;
  358. }
  359. $this->view->assign('order', $row);
  360. $this->view->assign('evaluate', $evaluate);
  361. return $this->view->fetch();
  362. }
  363. /**
  364. * 退货管理
  365. */
  366. public function refund($ids = null)
  367. {
  368. $row = $this->model->get($ids, ['refund']);
  369. if ($row['status'] != \app\admin\model\unishop\Order::STATUS_REFUND) {
  370. $this->error(__('This order is not returned'));
  371. }
  372. if ($this->request->isPost()) {
  373. $params = $this->request->post("row/a");
  374. if ($params) {
  375. $params = $this->preExcludeFields($params);
  376. $result = false;
  377. Db::startTrans();
  378. try {
  379. // 退款
  380. if($params['refund_action'] == 1) {
  381. $params['had_refund'] = time();
  382. Hook::add('order_refund', 'addons\\unishop\\behavior\\Order');
  383. }
  384. $updatetime = $this->request->post('updatetime');
  385. // 乐观锁
  386. $result = $this->model->allowField(true)->save($params, ['id' => $ids, 'updatetime' => $updatetime]);
  387. if (!$result) {
  388. throw new Exception(__('Data had been update before saved, close windows and do it again'));
  389. }
  390. Db::commit();
  391. } catch (ValidateException $e) {
  392. Db::rollback();
  393. $this->error($e->getMessage());
  394. } catch (PDOException $e) {
  395. Db::rollback();
  396. $this->error($e->getMessage());
  397. } catch (Exception $e) {
  398. Db::rollback();
  399. $this->error($e->getMessage());
  400. }
  401. if ($result !== false) {
  402. Hook::listen('order_refund', $row);
  403. $this->success();
  404. } else {
  405. $this->error(__('No rows were updated'));
  406. }
  407. }
  408. $this->error(__('Parameter %s can not be empty', ''));
  409. }
  410. $products = $row->product;
  411. $refundProducts = $row->refundProduct;
  412. foreach ($products as &$product) {
  413. $product['choose'] = 0;
  414. foreach ($refundProducts as $refundProduct) {
  415. if ($product['id'] == $refundProduct['order_product_id']) {
  416. $product['choose'] = 1;
  417. }
  418. }
  419. }
  420. if ($row->refund) {
  421. $refund = $row->refund->append(['receiving_status_text', 'service_type_text'])->toArray();
  422. } else {
  423. $refund = [
  424. 'service_type' => 0,
  425. 'express_number' => -1,
  426. 'receiving_status_text' => -1,
  427. 'receiving_status' => -1,
  428. 'service_type_text' => -1,
  429. 'amount' => -1,
  430. 'reason_type' => -1,
  431. 'refund_explain' => -1,
  432. ];
  433. }
  434. $this->view->assign('row', $row);
  435. $this->view->assign('product', $products);
  436. $this->view->assign('refund', $refund);
  437. return $this->view->fetch();
  438. }
  439. /**
  440. * 回收站
  441. */
  442. public function recyclebin()
  443. {
  444. //设置过滤方法
  445. $this->request->filter(['strip_tags']);
  446. if ($this->request->isAjax()) {
  447. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  448. $total = $this->model
  449. ->onlyTrashed()
  450. ->alias('order')
  451. ->join('user', 'user.id = order.user_id')
  452. ->where($where)
  453. ->count();
  454. $list = $this->model
  455. ->onlyTrashed()
  456. ->alias('order')
  457. ->join('user', 'user.id = order.user_id')
  458. ->where($where)
  459. ->field('order.*,user.username')
  460. ->order($sort, $order)
  461. ->limit($offset, $limit)
  462. ->select();
  463. $list = collection($list)->toArray();
  464. foreach ($list as &$item) {
  465. $item['id'] = (string)$item['id'];
  466. $item['user'] = [];
  467. $item['user']['username'] = $item['username'] ? $item['username'] : __('Tourist');
  468. $item['have_paid_status'] = $item['have_paid'];
  469. $item['have_delivered_status'] = $item['have_delivered'];
  470. $item['have_received_status'] = $item['have_received'];
  471. $item['have_commented_status'] = $item['have_commented'];
  472. }
  473. $result = array("total" => $total, "rows" => $list);
  474. return json($result);
  475. }
  476. return $this->view->fetch();
  477. }
  478. }