OrderController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace platform\controllers;
  3. use common\services\xhMerchantService;
  4. use common\components\configDict;
  5. use Yii;
  6. use yii\data\Pagination;
  7. use yii\helpers\Json;
  8. use common\models\xhOrder;
  9. use common\models\xhOrderGoods;
  10. use common\components\util;
  11. use common\components\stringUtil;
  12. use common\components\weixinUtil;
  13. use common\services\xhOrderService;
  14. use common\services\xhGoodsService;
  15. use common\services\xhTMessageService;
  16. use common\services\xhUserService;
  17. use common\services\xhUserAlipayService;
  18. use common\services\xhUserAssetService;
  19. use common\services\xhUserUniteService;
  20. use common\services\xhUserIntegralService;
  21. use common\services\xhMerchantCapitalService;
  22. use common\components\page;
  23. class OrderController extends PlatformController
  24. {
  25. public function actionList()
  26. {
  27. $get = Yii::$app->request->get();
  28. $query = xhOrder::find();
  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('list',['models' => $models,'payStatusName'=>$payStatusName,'reachPeriodName'=>$reachPeriodName,'pages' => $pages,'content' => $content]);
  50. }
  51. /**
  52. * 网上订单明细
  53. */
  54. public function actionDetail()
  55. {
  56. $get = Yii::$app->request->get();
  57. $id = isset($get['id']) ? $get['id'] : 0;
  58. $order = xhOrderService::getById($id);
  59. $goodsList = xhOrderGoods::getAllByCondition(['orderId' => $id]);
  60. foreach ($goodsList as $key => $good){
  61. $goodsId = $good['goodsId'];
  62. $goodsData = xhGoodsService::getById($goodsId);
  63. if($goodsData['goodsName'] != $good['title']){
  64. $subTitle = $good['title'];
  65. $goodsList[$key]['title'] = $goodsData['goodsName'];
  66. $goodsList[$key]['subTitle'] = $subTitle;
  67. }
  68. }
  69. $reachPeriodName = configDict::getConfig('reachPeriodName');//送达时间点 上午 下午 晚上
  70. $userId = 0;
  71. $userInfo = [];
  72. if(!empty($order['userId'])){
  73. $userId = $order['userId'];
  74. $userInfo = xhUserService::getById($userId);
  75. }
  76. $merchantId = $order['merchantId'];
  77. $payWayNameList = configDict::getConfig('payWayName');
  78. $alipayWay = configDict::getConfig('payWay','alipay');
  79. $alipayInfo = [];
  80. if($order['payWay'] == $alipayWay){
  81. $alipayId = $order['alipayId'];
  82. $alipayInfo = xhUserAlipayService::getByIds($alipayId,$merchantId);
  83. }
  84. $merchant = xhMerchantService::getById($merchantId);
  85. return $this->render('detail',['userInfo'=>$userInfo,'alipayWay'=>$alipayWay,'alipayInfo'=>$alipayInfo,'payWayNameList'=>$payWayNameList,'order' => $order,'reachPeriodName'=>$reachPeriodName,'goodsList' => $goodsList,'merchant' => $merchant]);
  86. }
  87. public function actionUpdatePayStatus()
  88. {
  89. $get = Yii::$app->request->get();
  90. $id = isset($get['id']) ? $get['id'] : 0;
  91. $order = xhOrderService::getById($id);
  92. if(empty($order) || $order['merchantId'] != $this->merchantId){
  93. util::failInfo('非法访问');
  94. }
  95. $status = isset($get['status']) ? $get['status'] : 0;
  96. if($order['payStatus'] == 0){
  97. //util::failInfo('客户还没有付款');
  98. $userId = $order['userId'];
  99. $user = xhUserService::getById($userId);
  100. if(empty($user)){
  101. util::failInfo('客户信息出错-用户ID未保存');
  102. }
  103. }
  104. xhOrderService::updateById($id, ['sendStatus' =>$status]);
  105. if($status == 2){//已经送达,通知订花人
  106. $userId = $order['userId'];
  107. $user = xhUserService::getById($userId);
  108. $openId = $user['openId'];
  109. $merchantId = $user['merchantId'];
  110. if(xhUserService::isSubscribe($userId)){
  111. $allTM = xhTMessageService::getList($merchantId);
  112. $shortTempId = 'OPENTM201205968';//订单送达通知
  113. $tempId = $allTM[$shortTempId];
  114. $data = ["touser" => $openId,"template_id" => $tempId,
  115. "url" => Yii::$app->params['frontUrl']."/center/order-detail?id={$order['id']}&account={$this->merchantId}",
  116. "data" => [
  117. "first" => ["value" => '您的订单已送达',"color" => "#173177"],
  118. "keyword1" => ["value" => (string)$order['id'],"color" => "#173177"],
  119. "keyword2" => ["value" => $order['receiveUserName'],"color" => "#173177"],
  120. "keyword3" => ["value" => '已送达',"color" => "#173177"],
  121. "remark" => ["value" => '感谢您惠顾,祝你生活愉快!',"color" => "#173177"]]];
  122. weixinUtil::sendTaskInform($data,$this->merchant);
  123. }
  124. }
  125. echo Json::encode(['code' => 'A0001','msg' =>'操作成功','data' =>[]]);
  126. }
  127. /**
  128. * 订单修改
  129. * @param $id
  130. */
  131. public function actionEditOrder($id){
  132. $orderData = xhOrderService::getById($id);
  133. if(empty($orderData)){
  134. util::failInfo('此订单不存在');
  135. }
  136. $orderData['reachTime'] = substr($orderData['reachTime'], 0, 10);
  137. util::successInfo('获取成功', 'A0001', $orderData);
  138. }
  139. /**
  140. * 保存订单修改
  141. * @param $id
  142. */
  143. public function actionSaveEditOrder(){
  144. $updateData = [];
  145. $post = Yii::$app->request->post();
  146. $orderData = xhOrderService::getById($post['id']);
  147. if(empty($orderData)){
  148. util::failInfo('此订单不存在');
  149. }
  150. //已付款 或 微信中打开支付链接的,不可修改价格
  151. $status = $orderData['actPrice'] != $post['consumeAmount'] ? true : false;
  152. if($orderData['modPriceStatus'] == 1 && $status){
  153. util::failInfo('订单已锁定,不能修改价格');
  154. }else if($orderData['payStatus'] == 1 && $status){
  155. util::failInfo('订单已付款,不能修改价格');
  156. }
  157. xhOrderService::formatPost($updateData, $post);
  158. $re = xhOrderService::updateById($post['id'], $updateData);
  159. if($re == 1){
  160. $msg = '保存成功';
  161. }else{
  162. $msg = '表单数据未变更';
  163. }
  164. util::successInfo($msg, 'A0001', $orderData);
  165. }
  166. /**
  167. * 打印订单
  168. */
  169. public function actionPrint()
  170. {
  171. $get = Yii::$app->request->get();
  172. $id = isset($get['id']) ? $get['id'] : 0;
  173. $order = xhOrderService::getById($id);
  174. if(empty($order) || $order['merchantId'] != $this->merchantId){
  175. echo '访问出错了,请联系网站管理员';
  176. Yii::$app->end();
  177. }
  178. $goodsList = xhOrderGoods::getAllByCondition(['orderId' => $id]);
  179. $list = [];
  180. foreach($goodsList as $val){
  181. $list[] = ['name' => $val['title'],'num' => $val['num']];
  182. }
  183. return $this->renderPartial('print',['order' => $order,'merchant' => $this->merchant,'goodsList' => $list]);
  184. }
  185. /**
  186. * 更新打印状态
  187. */
  188. public function actionUpdatePrintStatus()
  189. {
  190. $get = Yii::$app->request->get();
  191. $status = isset($get['status']) ? $get['status'] : 1;
  192. $id = isset($get['id']) ? $get['id'] : 0;
  193. $order = xhOrderService::getById($id);
  194. if(empty($order) || $order['merchantId'] != $this->merchantId){
  195. echo Json::encode(['code' => 'A0002','data' => '','msg' => '出错了,请联系网站管理员']);
  196. Yii::$app->end();
  197. }
  198. xhOrderService::updateById($id, ['printStatus' => $status]);
  199. echo Json::encode(['code' => 'A0001','data' => '','msg' => 'success']);
  200. }
  201. /**
  202. * 修改价格
  203. */
  204. public function actionModifyPrice()
  205. {
  206. $get = Yii::$app->request->get();
  207. $actPrice = isset($get['actPrice']) ? $get['actPrice'] : 10000;
  208. $id = isset($get['id']) ? $get['id'] : 0;
  209. $order = xhOrderService::getById($id, $this->merchantId);
  210. /*if($order['merchantId'] != $this->merchantId){
  211. util::failInfo('非法访问');
  212. }*/
  213. if($order['modPriceStatus'] == 1){
  214. util::failInfo('订单已锁定,不能修改价格');
  215. }
  216. $prePrice = $order['actPrice'];
  217. xhOrderService::updateById($id, ['actPrice' => $actPrice]);
  218. $userId = $order['userId'];
  219. $user = xhUserService::getById($userId);
  220. if(empty($user)){
  221. echo Json::encode(['code' => 'A0001','data' => '','msg' => "修改成功"]);
  222. exit();
  223. }
  224. $openId = $user['openId'];
  225. $merchantId = $user['merchantId'];
  226. //价格修改通知
  227. $allTM = xhTMessageService::getList($merchantId);
  228. $shortTempId = 'OPENTM401761342';
  229. $tempId = $allTM[$shortTempId];
  230. $data = [
  231. "touser" => $openId,
  232. "template_id" => $tempId,//模板id,从公众平台后台取得
  233. "url" => $this->frontUrl."/center/order-detail?id={$order['id']}&account={$merchantId}",
  234. "data" => [
  235. "first" => ["value" => '您好,你购买的商品价格已修改。',"color" => "#173177"],
  236. "keyword1" => ["value" => (string)$order['id'],"color" => "#173177"],
  237. "keyword2" => ["value" => $prePrice,"color" => "#173177"],
  238. "keyword3" => ["value" => number_format($actPrice,2),"color" => "#173177"],
  239. "remark" => ["value" => '点此进行支付。',"color" => "#173177"]]];
  240. weixinUtil::sendTaskInform($data,$this->merchant);
  241. echo Json::encode(['code' => 'A0001','data' => '','msg' => "修改成功,已通知客户"]);
  242. }
  243. /**
  244. * 修改商品的配送编号
  245. */
  246. public function actionModifySendNum()
  247. {
  248. $get = Yii::$app->request->get();
  249. $id = isset($get['id']) ? $get['id'] : 0;
  250. $num = isset($get['num']) ? $get['num'] : '';
  251. $order = xhOrderService::getById($id);
  252. if($order['merchantId'] != $this->merchantId){
  253. util::failInfo('修改失败');
  254. }
  255. xhOrderService::updateById($id, ['sendNum'=>$num]);
  256. util::successInfo('修改成功');
  257. }
  258. public function actionUserOrderList()
  259. {
  260. $get = Yii::$app->request->get();
  261. $userId = isset($get['id']) ? $get['id'] : 0;
  262. $user = xhUserService::getById($userId,$this->merchantId);
  263. $query = xhOrder::find();
  264. $query->where(['userId' =>$userId]);
  265. $countQuery = clone $query;
  266. $count = $countQuery->count();
  267. $page = isset($get['page']) ? $get['page'] : 1;
  268. $pageSize = 20;
  269. $offset = ($page - 1) * $pageSize;
  270. $models = $query->offset($offset)->orderBy('createTime desc')->limit($pageSize)->asArray()->all();
  271. $page = new page($count,$page,$pageSize);
  272. $paging = $page->fpage();
  273. $reachPeriodName = configDict::getConfig('reachPeriodName');
  274. $payStatusName = configDict::getConfig('payStatusName');
  275. return $this->render('userOrderList',['models' => $models,'paging' => $paging,'payStatusName'=>$payStatusName,'reachPeriodName'=>$reachPeriodName]);
  276. }
  277. }