OrderController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. namespace backend\controllers;
  3. use biz\order\services\OrderService;
  4. use Yii;
  5. use yii\data\Pagination;
  6. use yii\helpers\Json;
  7. use common\models\xhOrder;
  8. use common\models\xhOrderGoods;
  9. use common\components\util;
  10. use common\components\stringUtil;
  11. use common\components\weixinUtil;
  12. use common\services\xhOrderService;
  13. use common\services\xhGoodsService;
  14. use common\services\xhTMessageService;
  15. use common\services\xhUserService;
  16. use common\services\xhUserAlipayService;
  17. use common\components\page;
  18. use common\components\configDict;
  19. class OrderController extends BackendController
  20. {
  21. public function actionComplete()
  22. {
  23. $get = Yii::$app->request->get();
  24. $query = xhOrder::find();
  25. $merchantId = $this->merchantId;
  26. $query->where(['merchantId' => $merchantId]);
  27. $currentShopId = $this->admin['currentShopId'];
  28. $query->andWhere(['shopId' => $currentShopId]);
  29. if (isset($get['payStatus']) && $get['payStatus'] >= 0) {
  30. $query->andWhere(['payStatus' => $get['payStatus']]);
  31. }
  32. $content = '';
  33. if (isset($get['content']) && !empty($get['content'])) {
  34. $content = $get['content'];
  35. if (stringUtil::isMobieNumber($content)) {
  36. $query->andWhere(['bookMobile' => $content]);
  37. } elseif (is_numeric($content)) {
  38. $query->andWhere(['id' => $content]);
  39. } else {
  40. $query->andWhere(['like', 'bookName', $content]);
  41. }
  42. }
  43. $query->orderBy('createTime desc');
  44. $countQuery = clone $query;
  45. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  46. $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  47. $reachPeriodName = configDict::getConfig('reachPeriodName');
  48. $payStatusName = configDict::getConfig('payStatusName');
  49. return $this->render('complete', ['models' => $models, 'payStatusName' => $payStatusName, 'reachPeriodName' => $reachPeriodName, 'pages' => $pages, 'content' => $content, 'merchant' => $this->merchant]);
  50. }
  51. public function actionUnInform()
  52. {
  53. $get = Yii::$app->request->get();
  54. $query = xhOrder::find();
  55. $merchantId = $this->merchantId;
  56. $query->where(['merchantId' => $merchantId]);
  57. $currentShopId = $this->admin['currentShopId'];
  58. $query->andWhere(['shopId' => $currentShopId]);
  59. if (isset($get['payStatus']) && $get['payStatus'] >= 0) {
  60. $query->andWhere(['payStatus' => $get['payStatus']]);
  61. }
  62. $content = '';
  63. if (isset($get['content']) && !empty($get['content'])) {
  64. $content = $get['content'];
  65. if (stringUtil::isMobieNumber($content)) {
  66. $query->andWhere(['bookMobile' => $content]);
  67. } elseif (is_numeric($content)) {
  68. $query->andWhere(['id' => $content]);
  69. } else {
  70. $query->andWhere(['like', 'bookName', $content]);
  71. }
  72. }
  73. $query->orderBy('createTime desc');
  74. $countQuery = clone $query;
  75. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  76. $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  77. $reachPeriodName = configDict::getConfig('reachPeriodName');
  78. $payStatusName = configDict::getConfig('payStatusName');
  79. return $this->render('unInform', ['models' => $models, 'payStatusName' => $payStatusName, 'reachPeriodName' => $reachPeriodName, 'pages' => $pages, 'content' => $content, 'merchant' => $this->merchant]);
  80. }
  81. public function actionUnSend()
  82. {
  83. $get = Yii::$app->request->get();
  84. $query = xhOrder::find();
  85. $merchantId = $this->merchantId;
  86. $query->where(['merchantId' => $merchantId]);
  87. $currentShopId = $this->admin['currentShopId'];
  88. $query->andWhere(['shopId' => $currentShopId]);
  89. if (isset($get['payStatus']) && $get['payStatus'] >= 0) {
  90. $query->andWhere(['payStatus' => $get['payStatus']]);
  91. }
  92. $content = '';
  93. if (isset($get['content']) && !empty($get['content'])) {
  94. $content = $get['content'];
  95. if (stringUtil::isMobieNumber($content)) {
  96. $query->andWhere(['bookMobile' => $content]);
  97. } elseif (is_numeric($content)) {
  98. $query->andWhere(['id' => $content]);
  99. } else {
  100. $query->andWhere(['like', 'bookName', $content]);
  101. }
  102. }
  103. $query->orderBy('createTime desc');
  104. $countQuery = clone $query;
  105. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  106. $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  107. $reachPeriodName = configDict::getConfig('reachPeriodName');
  108. $payStatusName = configDict::getConfig('payStatusName');
  109. return $this->render('unSend', ['models' => $models, 'payStatusName' => $payStatusName, 'reachPeriodName' => $reachPeriodName, 'pages' => $pages, 'content' => $content, 'merchant' => $this->merchant]);
  110. }
  111. public function actionUnPay()
  112. {
  113. $get = Yii::$app->request->get();
  114. $query = xhOrder::find();
  115. $merchantId = $this->merchantId;
  116. $query->where(['merchantId' => $merchantId]);
  117. $currentShopId = $this->admin['currentShopId'];
  118. $query->andWhere(['shopId' => $currentShopId]);
  119. if (isset($get['payStatus']) && $get['payStatus'] >= 0) {
  120. $query->andWhere(['payStatus' => $get['payStatus']]);
  121. }
  122. $content = '';
  123. if (isset($get['content']) && !empty($get['content'])) {
  124. $content = $get['content'];
  125. if (stringUtil::isMobieNumber($content)) {
  126. $query->andWhere(['bookMobile' => $content]);
  127. } elseif (is_numeric($content)) {
  128. $query->andWhere(['id' => $content]);
  129. } else {
  130. $query->andWhere(['like', 'bookName', $content]);
  131. }
  132. }
  133. $query->orderBy('createTime desc');
  134. $countQuery = clone $query;
  135. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  136. $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  137. $reachPeriodName = configDict::getConfig('reachPeriodName');
  138. $payStatusName = configDict::getConfig('payStatusName');
  139. return $this->render('unPay', ['models' => $models, 'payStatusName' => $payStatusName, 'reachPeriodName' => $reachPeriodName, 'pages' => $pages, 'content' => $content, 'merchant' => $this->merchant]);
  140. }
  141. public function actionList()
  142. {
  143. $get = Yii::$app->request->get();
  144. $query = xhOrder::find();
  145. $merchantId = $this->merchantId;
  146. $query->where(['merchantId' => $merchantId]);
  147. $currentShopId = $this->admin['currentShopId'];
  148. $query->andWhere(['shopId' => $currentShopId]);
  149. if (isset($get['payStatus']) && $get['payStatus'] >= 0) {
  150. $query->andWhere(['payStatus' => $get['payStatus']]);
  151. }
  152. $content = '';
  153. if (isset($get['content']) && !empty($get['content'])) {
  154. $content = $get['content'];
  155. if (stringUtil::isMobieNumber($content)) {
  156. $query->andWhere(['bookMobile' => $content]);
  157. } elseif (is_numeric($content)) {
  158. $query->andWhere(['id' => $content]);
  159. } else {
  160. $query->andWhere(['like', 'bookName', $content]);
  161. }
  162. }
  163. $query->orderBy('createTime desc');
  164. $countQuery = clone $query;
  165. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  166. $models = $query->offset($pages->offset)->orderBy('addTime desc')->limit($pages->limit)->asArray()->all();
  167. $reachPeriodName = configDict::getConfig('reachPeriodName');
  168. $payStatusName = configDict::getConfig('payStatusName');
  169. $usageList = $this->usageList;
  170. $categoryList = $this->categoryList;
  171. return $this->render('list', ['models' => $models, 'payStatusName' => $payStatusName, 'reachPeriodName' => $reachPeriodName,
  172. 'pages' => $pages, 'content' => $content, 'merchant' => $this->merchant, 'usageList' => $usageList, 'categoryList' => $categoryList]);
  173. }
  174. /**
  175. * 网上订单明细
  176. */
  177. public function actionDetail()
  178. {
  179. $get = Yii::$app->request->get();
  180. $id = isset($get['id']) ? $get['id'] : 0;
  181. $order = xhOrderService::getById($id);
  182. if (empty($order) || $order['merchantId'] != $this->merchantId) {
  183. util::stop('访问出错,请联系网站管理员');
  184. }
  185. $goodsList = xhOrderGoods::getAllByCondition(['orderId' => $id]);
  186. foreach ($goodsList as $key => $good) {
  187. $goodsId = $good['goodsId'];
  188. $goodsData = xhGoodsService::getById($goodsId);
  189. if ($goodsData['goodsName'] != $good['title']) {
  190. $subTitle = $good['title'];
  191. $goodsList[$key]['title'] = $goodsData['goodsName'];
  192. $goodsList[$key]['subTitle'] = $subTitle;
  193. }
  194. }
  195. $reachPeriodName = configDict::getConfig('reachPeriodName');//送达时间点 上午 下午 晚上
  196. $userId = 0;
  197. $userInfo = [];
  198. if (!empty($order['userId'])) {
  199. $userId = $order['userId'];
  200. $userInfo = xhUserService::getById($userId);
  201. }
  202. $payWayNameList = configDict::getConfig('payWayName');
  203. $alipayWay = configDict::getConfig('payWay', 'alipay');
  204. $alipayInfo = [];
  205. if ($order['payWay'] == $alipayWay) {
  206. $alipayId = $order['alipayId'];
  207. $merchantId = $order['merchantId'];
  208. $alipayInfo = xhUserAlipayService::getByIds($alipayId, $merchantId);
  209. }
  210. $sendStatusName = configDict::getConfig('sendStatusName');
  211. return $this->render('detail', ['sendStatusName' => $sendStatusName, 'userInfo' => $userInfo, 'alipayWay' => $alipayWay, 'alipayInfo' => $alipayInfo, 'payWayNameList' => $payWayNameList, 'order' => $order, 'reachPeriodName' => $reachPeriodName, 'goodsList' => $goodsList, 'merchant' => $this->merchant]);
  212. }
  213. public function actionUpdatePayStatus()
  214. {
  215. $get = Yii::$app->request->get();
  216. $id = isset($get['id']) ? $get['id'] : 0;
  217. $order = xhOrderService::getById($id);
  218. if (empty($order) || $order['merchantId'] != $this->merchantId) {
  219. util::failInfo('非法访问');
  220. }
  221. $status = isset($get['status']) ? $get['status'] : 0;
  222. if ($order['payStatus'] == 0) {
  223. //util::failInfo('客户还没有付款');
  224. $userId = $order['userId'];
  225. $user = xhUserService::getById($userId);
  226. if (empty($user)) {
  227. util::failInfo('客户信息出错-用户ID未保存');
  228. }
  229. }
  230. xhOrderService::updateById($id, ['sendStatus' => $status]);
  231. if ($status == 2) {//已经送达,通知订花人
  232. $userId = $order['userId'];
  233. $user = xhUserService::getById($userId);
  234. $openId = $user['openId'];
  235. $merchantId = $user['merchantId'];
  236. if (xhUserService::isSubscribe($userId)) {
  237. $allTM = xhTMessageService::getList($merchantId);
  238. $shortTempId = 'OPENTM201205968';//订单送达通知
  239. if (isset($allTM[$shortTempId])) {
  240. $tempId = $allTM[$shortTempId];
  241. $data = ["touser" => $openId, "template_id" => $tempId,
  242. "url" => Yii::$app->params['frontUrl'] . "/center/order-detail?id={$order['id']}&account={$this->merchantId}",
  243. "data" => [
  244. "first" => ["value" => '您的订单已送达', "color" => "#173177"],
  245. "keyword1" => ["value" => (string)$order['id'], "color" => "#173177"],
  246. "keyword2" => ["value" => $order['receiveUserName'], "color" => "#173177"],
  247. "keyword3" => ["value" => '已送达', "color" => "#173177"],
  248. "remark" => ["value" => '感谢您惠顾,祝你生活愉快!', "color" => "#173177"]]];
  249. weixinUtil::sendTaskInform($data, $this->merchant);
  250. }
  251. }
  252. }
  253. echo Json::encode(['code' => 'A0001', 'msg' => '操作成功', 'data' => []]);
  254. }
  255. /**
  256. * 订单修改
  257. * @param $id
  258. */
  259. public function actionEditOrder($id)
  260. {
  261. $orderData = xhOrderService::getById($id);
  262. if (empty($orderData)) {
  263. util::failInfo('此订单不存在');
  264. }
  265. $orderData['reachTime'] = substr($orderData['reachTime'], 0, 10);
  266. util::successInfo('获取成功', 'A0001', $orderData);
  267. }
  268. /**
  269. * 保存订单修改
  270. * @param $id
  271. */
  272. public function actionSaveEditOrder()
  273. {
  274. $updateData = [];
  275. $post = Yii::$app->request->post();
  276. $orderData = xhOrderService::getById($post['id']);
  277. if (empty($orderData)) {
  278. util::failInfo('此订单不存在');
  279. }
  280. //已付款 或 微信中打开支付链接的,不可修改价格
  281. $status = $orderData['actPrice'] != $post['consumeAmount'] ? true : false;
  282. if ($orderData['modPriceStatus'] == 1 && $status) {
  283. util::failInfo('订单已锁定,不能修改价格');
  284. } else if ($orderData['payStatus'] == 1 && $status) {
  285. util::failInfo('订单已付款,不能修改价格');
  286. }
  287. xhOrderService::formatPost($updateData, $post);
  288. $re = xhOrderService::updateById($post['id'], $updateData);
  289. if ($re['status']) {
  290. $msg = '保存成功';
  291. } else {
  292. $msg = '表单数据未变更';
  293. }
  294. util::successInfo($msg, 'A0001', $orderData);
  295. }
  296. /**
  297. * 打印订单
  298. */
  299. public function actionPrint()
  300. {
  301. $get = Yii::$app->request->get();
  302. $id = isset($get['id']) ? $get['id'] : 0;
  303. $order = xhOrderService::getById($id);
  304. if (empty($order) || $order['merchantId'] != $this->merchantId) {
  305. echo '访问出错了,请联系网站管理员';
  306. Yii::$app->end();
  307. }
  308. $goodsList = xhOrderGoods::getAllByCondition(['orderId' => $id]);
  309. $list = [];
  310. foreach ($goodsList as $val) {
  311. $list[] = ['name' => $val['title'], 'num' => $val['num']];
  312. }
  313. return $this->renderPartial('print', ['order' => $order, 'merchant' => $this->merchant, 'goodsList' => $list]);
  314. }
  315. /**
  316. * 更新打印状态
  317. */
  318. public function actionUpdatePrintStatus()
  319. {
  320. $get = Yii::$app->request->get();
  321. $status = isset($get['status']) ? $get['status'] : 1;
  322. $id = isset($get['id']) ? $get['id'] : 0;
  323. $order = xhOrderService::getById($id);
  324. if (empty($order) || $order['merchantId'] != $this->merchantId) {
  325. echo Json::encode(['code' => 'A0002', 'data' => '', 'msg' => '出错了,请联系网站管理员']);
  326. Yii::$app->end();
  327. }
  328. xhOrderService::updateById($id, ['printStatus' => $status]);
  329. echo Json::encode(['code' => 'A0001', 'data' => '', 'msg' => 'success']);
  330. }
  331. /**
  332. * 修改价格
  333. */
  334. public function actionModifyPrice()
  335. {
  336. $get = Yii::$app->request->get();
  337. $actPrice = isset($get['actPrice']) ? $get['actPrice'] : 10000;
  338. $id = isset($get['id']) ? $get['id'] : 0;
  339. $order = xhOrderService::getById($id, $this->merchantId);
  340. /*if($order['merchantId'] != $this->merchantId){
  341. util::failInfo('非法访问');
  342. }*/
  343. if ($order['modPriceStatus'] == 1) {
  344. util::failInfo('订单已锁定,不能修改价格');
  345. }
  346. $prePrice = $order['actPrice'];
  347. xhOrderService::updateById($id, ['actPrice' => $actPrice]);
  348. $userId = $order['userId'];
  349. $user = xhUserService::getById($userId);
  350. if (empty($user)) {
  351. echo Json::encode(['code' => 'A0001', 'data' => '', 'msg' => "修改成功"]);
  352. exit();
  353. }
  354. $openId = $user['openId'];
  355. $merchantId = $user['merchantId'];
  356. //价格修改通知
  357. $allTM = xhTMessageService::getList($merchantId);
  358. $shortTempId = 'OPENTM401761342';
  359. if (isset($allTM[$shortTempId])) {
  360. $tempId = $allTM[$shortTempId];
  361. $data = [
  362. "touser" => $openId,
  363. "template_id" => $tempId,//模板id,从公众平台后台取得
  364. "url" => $this->frontUrl . "/center/order-detail?id={$order['id']}&account={$this->merchantId}",
  365. "data" => [
  366. "first" => ["value" => '您好,你购买的商品价格已修改。', "color" => "#173177"],
  367. "keyword1" => ["value" => (string)$order['id'], "color" => "#173177"],
  368. "keyword2" => ["value" => $prePrice, "color" => "#173177"],
  369. "keyword3" => ["value" => number_format($actPrice, 2), "color" => "#173177"],
  370. "remark" => ["value" => '点此进行支付。', "color" => "#173177"]]];
  371. weixinUtil::sendTaskInform($data, $this->merchant);
  372. }
  373. echo Json::encode(['code' => 'A0001', 'data' => '', 'msg' => "修改成功,已通知客户"]);
  374. }
  375. /**
  376. * 修改商品的配送编号
  377. */
  378. public function actionModifySendNum()
  379. {
  380. $get = Yii::$app->request->get();
  381. $id = isset($get['id']) ? $get['id'] : 0;
  382. $num = isset($get['num']) ? $get['num'] : '';
  383. $order = xhOrderService::getById($id);
  384. if ($order['merchantId'] != $this->merchantId) {
  385. util::failInfo('修改失败');
  386. }
  387. xhOrderService::updateById($id, ['sendNum' => $num]);
  388. util::successInfo('修改成功');
  389. }
  390. public function actionUserOrderList()
  391. {
  392. $get = Yii::$app->request->get();
  393. $userId = isset($get['id']) ? $get['id'] : 0;
  394. $query = xhOrder::find();
  395. $query->where(['userId' => $userId]);
  396. $countQuery = clone $query;
  397. $count = $countQuery->count();
  398. $page = isset($get['page']) ? $get['page'] : 1;
  399. $pageSize = 20;
  400. $offset = ($page - 1) * $pageSize;
  401. $models = $query->offset($offset)->orderBy('createTime desc')->limit($pageSize)->asArray()->all();
  402. $page = new page($count, $page, $pageSize);
  403. $paging = $page->fpage();
  404. $reachPeriodName = configDict::getConfig('reachPeriodName');
  405. $payStatusName = configDict::getConfig('payStatusName');
  406. return $this->render('userOrderList', ['models' => $models, 'paging' => $paging, 'payStatusName' => $payStatusName, 'reachPeriodName' => $reachPeriodName]);
  407. }
  408. //设置订单的分类和用途 shish 2019.8.18
  409. public function actionSetStyle()
  410. {
  411. $this->errorExportJson();
  412. $get = Yii::$app->request->get();
  413. $ret = OrderService::setOrderStyle($get);
  414. if ($ret == false) {
  415. util::sendFail($this->error->getError());
  416. }
  417. util::sendSuccess();
  418. }
  419. }