PtOrderController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers\pt;
  8. use AopClient;
  9. use app\models\Option;
  10. use app\models\Order;
  11. use app\models\OrderRefund;
  12. use app\models\StoreAliMini;
  13. use app\modules\client\behaviors\Auth;
  14. use app\modules\client\models\v1\OrderRefundForm;
  15. use app\modules\alliance\controllers\BaseController;
  16. use app\modules\client\models\v1\ExpressDetailForm;
  17. use app\modules\alliance\models\pt\order\OrderPayDataForm;
  18. use app\modules\alliance\models\pt\order\OrderSubmitForm;
  19. use app\modules\alliance\models\pt\order\OrderSubmitPreviewForm;
  20. use app\modules\client\models\v1\OrderCommentForm;
  21. use app\modules\client\models\v1\OrderCommentPreview;
  22. use app\modules\alliance\models\OrderConfirmForm;
  23. use app\modules\alliance\models\OrderDetailForm;
  24. use app\modules\alliance\models\OrderListForm;
  25. use app\modules\client\models\v1\OrderRefundDetailForm;
  26. use app\modules\client\models\v1\OrderRefundPreviewForm;
  27. use app\modules\client\models\v1\OrderRefundSendForm;
  28. use app\modules\alliance\models\OrderRevokeForm;
  29. use app\modules\alliance\models\pt\PtActivityOrderForm;
  30. use app\modules\client\models\v1\SaasOrderForm;
  31. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppQrcodeCreateRequest;
  32. use app\utils\Delivery\Delivery;
  33. use app\utils\Delivery\Alipay\ADelivery;
  34. use app\utils\ShareQrcode;
  35. use yii\base\BaseObject;
  36. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  37. class PtOrderController extends BaseController
  38. {
  39. public function behaviors()
  40. {
  41. return parent::behaviors();
  42. }
  43. public function actionCheckBuyer()
  44. {
  45. $goods_ids = input_params('goods_ids', []);
  46. $address_id = input_params('address_id', 0);
  47. if(empty($goods_ids) || empty($address_id)){
  48. return $this->asJson([
  49. 'code' => 1,
  50. 'msg' => '参数错误',
  51. ]);
  52. }
  53. $address = \app\models\Address::findOne($address_id);
  54. $res = \app\models\OrderRulesBuyerLocation::checkLocationGoodsList($goods_ids, $address, get_user_id());
  55. return $this->asJson([
  56. 'code' => 0,
  57. 'msg' => 'ok',
  58. 'data' => $res,
  59. ]);
  60. }
  61. /**
  62. * 新-订单提交前的预览页面
  63. * @return \yii\web\Response
  64. */
  65. public function actionSubmitPreview()
  66. {
  67. $form = new OrderSubmitPreviewForm();
  68. $form->attributes = post_params();
  69. $form->store_id = get_store_id();
  70. $form->saas_id = get_saas_user_id();
  71. return $this->asJson($form->search());
  72. }
  73. /**
  74. * 新-订单提交
  75. * @return \yii\web\Response
  76. */
  77. public function actionSubmit()
  78. {
  79. $form = new OrderSubmitForm();
  80. $form->attributes = post_params();
  81. $form->store_id = get_store_id();
  82. $form->saas_id = get_saas_user_id();
  83. $form->saas_user = get_saas_user();
  84. $form->send_price = post_params('send_price');
  85. $form->take_price = post_params('take_price');
  86. return $this->asJson($form->save());
  87. }
  88. /**
  89. * 订单支付数据
  90. * @return \yii\web\Response
  91. */
  92. public function actionPayData()
  93. {
  94. $form = new OrderPayDataForm();
  95. $form->attributes = get_params();
  96. $form->store_id = get_store_id();
  97. $form->saas_id = get_saas_user_id();
  98. $form->saas_user = get_saas_user();
  99. return $this->asJson($form->search());
  100. }
  101. /**
  102. * 订单列表
  103. * @return \yii\web\Response
  104. */
  105. public function actionList()
  106. {
  107. $form = new PtActivityOrderForm();
  108. $form->attributes = get_params();
  109. $form->store_id = get_store_id();
  110. return $this->asJson($form->search());
  111. }
  112. /**
  113. * 订单列表
  114. * @return \yii\web\Response
  115. */
  116. public function actionOrderInfo()
  117. {
  118. $form = new PtActivityOrderForm();
  119. $form->attributes = get_params();
  120. return $this->asJson($form->orderInfo());
  121. }
  122. /**
  123. * 订单取消
  124. * @return \yii\web\Response
  125. */
  126. public function actionRevoke()
  127. {
  128. $form = new OrderRevokeForm();
  129. $form->attributes = get_params();
  130. $form->store_id = get_store_id();
  131. $form->user_id = get_user_id();
  132. return $this->asJson($form->save());
  133. }
  134. /**
  135. * 订单详情
  136. * @return \yii\web\Response
  137. */
  138. public function actionDetail()
  139. {
  140. $form = new OrderDetailForm();
  141. $form->attributes = get_params();
  142. $form->store_id = get_store_id();
  143. if (!empty(get_params('userId'))) {
  144. $form->user_id = get_params('userId');
  145. } else {
  146. $form->user_id = get_user_id();
  147. }
  148. return $this->asJson($form->search());
  149. }
  150. /**
  151. * 获取核销码
  152. * @return \yii\web\Response
  153. */
  154. public function actionGetQrcode()
  155. {
  156. $order_no = get_params('order_no');
  157. $order_type = get_params('order_type', '');
  158. $page = "user/clerk/clerk";
  159. if ($order_type) {
  160. $scene = "order_no:{$order_no},type:{$order_type}";
  161. } else {
  162. $scene = "{$order_no}";
  163. }
  164. if (is_wechat_platform()) {
  165. $file_name = ShareQrcode::wxQrcode($page,$scene);
  166. }
  167. if (is_alipay_platform()) {
  168. if ($order_type) {
  169. $scene = "order_no:{$order_no},type:{$order_type}";
  170. } else {
  171. $scene = "order_no={$order_no}";
  172. }
  173. $request = new AlipayOpenAppQrcodeCreateRequest();
  174. $param = [
  175. 'url_param' => 'user/clerk/clerk',
  176. 'query_param' => $scene,
  177. 'describe' => "小程序开发上线使用"
  178. ];
  179. $aliConfigQr = $this->aliConfigQr($request, $param);
  180. $file_name['url_path'] = "";
  181. if ($aliConfigQr->code === "10000") {
  182. $file_name['url_path'] = $aliConfigQr->qr_code_url_circle_blue;
  183. }
  184. }
  185. $res = [
  186. 'code' => 0,
  187. 'msg' => 'success',
  188. 'data' => [
  189. 'url' => $file_name['url_path'],
  190. ]
  191. ];
  192. return $this->asJson($res);
  193. }
  194. /**
  195. * 订单确认收货
  196. */
  197. public function actionConfirm()
  198. {
  199. $form = new OrderConfirmForm();
  200. $form->attributes = get_params();
  201. $form->store_id = get_store_id();
  202. $form->user_id = get_user_id();
  203. return $this->asJson($form->save());
  204. }
  205. /**
  206. * 评论预览页面
  207. */
  208. public function actionCommentPreview()
  209. {
  210. $form = new OrderCommentPreview();
  211. $form->attributes = get_params();
  212. $form->store_id = get_store_id();
  213. $form->user_id = get_user_id();
  214. return $this->asJson($form->search());
  215. }
  216. /**
  217. * 评论提交
  218. */
  219. public function actionComment()
  220. {
  221. $form = new OrderCommentForm();
  222. $form->attributes = post_params();
  223. $form->store_id = get_store_id();
  224. $form->user_id = get_user_id();
  225. return $this->asJson($form->save());
  226. }
  227. /**
  228. * 售后页面
  229. */
  230. public function actionRefundPreview()
  231. {
  232. $form = new OrderRefundPreviewForm();
  233. $form->attributes = get_params();
  234. $form->store_id = get_store_id();
  235. $form->user_id = get_user_id();
  236. return $this->asJson($form->search());
  237. }
  238. /**
  239. * 售后订单列表
  240. */
  241. public function actionRefundList() {
  242. $form = new OrderListForm();
  243. $form->store_id = get_store_id();
  244. $form->user_id = get_user_id();
  245. $form->order_id = get_params('order_id');
  246. $form->status = get_params('status');
  247. return $this->asJson($form->getNewRefundList());
  248. }
  249. /**
  250. * 售后订单列表
  251. */
  252. public function actionIntegralList() {
  253. $form = new OrderListForm();
  254. $form->store_id = get_store_id();
  255. $form->user_id = get_user_id();
  256. return $this->asJson($form->getIntegralList());
  257. }
  258. /**
  259. * 售后提交
  260. */
  261. public function actionRefund()
  262. {
  263. $form = new OrderRefundForm();
  264. $form->attributes = post_params();
  265. $form->store_id = get_store_id();
  266. $form->user_id = get_user_id();
  267. return $this->asJson($form->save());
  268. }
  269. /**
  270. * 售后订单详情
  271. */
  272. public function actionRefundDetail()
  273. {
  274. $form = new OrderRefundDetailForm();
  275. $form->attributes = get_params();
  276. $form->store_id = get_store_id();
  277. $form->user_id = get_user_id();
  278. return $this->asJson($form->search());
  279. }
  280. /**
  281. * 售后订单取消
  282. */
  283. public function actionRefundCancel()
  284. {
  285. $order_refund_id = post_params('order_refund_id');
  286. $orderRefund = OrderRefund::findOne(['id' => $order_refund_id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  287. if (!$orderRefund) {
  288. return $this->asJson([
  289. 'code' => 1,
  290. 'msg' => '订单不存在'
  291. ]);
  292. }
  293. if (in_array($orderRefund->status, [0, 3]) && $orderRefund->is_user_send == 0) {
  294. $orderRefund->is_user_cancel = 1;
  295. if ($orderRefund->save()) {
  296. return $this->asJson([
  297. 'code' => 0,
  298. 'msg' => '取消成功'
  299. ]);
  300. } else {
  301. return $this->asJson([
  302. 'code' => 1,
  303. 'msg' => $orderRefund->errors[0]
  304. ]);
  305. }
  306. }
  307. return $this->asJson([
  308. 'code' => 1,
  309. 'msg' => '取消失败,当前订单在流程中'
  310. ]);
  311. }
  312. /**
  313. * 售后订单删除
  314. */
  315. public function actionRefundDelete()
  316. {
  317. $order_refund_id = post_params('order_refund_id');
  318. $orderRefund = OrderRefund::findOne(['id' => $order_refund_id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  319. if (!$orderRefund) {
  320. return $this->asJson([
  321. 'code' => 1,
  322. 'msg' => '订单不存在'
  323. ]);
  324. }
  325. OrderRefund::updateAll(['user_delete' => 1], ['id' => $order_refund_id]);
  326. return $this->asJson([
  327. 'code' => 0,
  328. 'msg' => '删除成功'
  329. ]);
  330. }
  331. /**
  332. * 全返订单列表
  333. * @return \yii\web\Response
  334. */
  335. public function actionQuanfan()
  336. {
  337. $form = new OrderListForm();
  338. $form->attributes = get_params();
  339. $form->store_id = $this->store_id;
  340. $form->user_id = get_user_id();
  341. return $this->asJson($form->quanfan());
  342. }
  343. /**
  344. * 售后订单用户发货
  345. * @return \yii\web\Response
  346. */
  347. public function actionRefundSend()
  348. {
  349. $form = new OrderRefundSendForm();
  350. $form->attributes = post_params();
  351. $form->user_id = get_user_id();
  352. return $this->asJson($form->save());
  353. }
  354. /**
  355. * 订单物流信息
  356. */
  357. public function actionExpressDetail()
  358. {
  359. $form = new ExpressDetailForm();
  360. $form->attributes = get_params();
  361. $form->store_id = get_store_id();
  362. $form->user_id = get_user_id();
  363. return $this->asJson($form->search());
  364. }
  365. /**
  366. * saas订单列表
  367. * @return \yii\web\Response
  368. */
  369. public function actionSaas() {
  370. $form = new SaasOrderForm();
  371. $form->attributes = get_params();
  372. $form->user_id = get_user_id();
  373. $form->status = get_params('status');
  374. return $this->asJson($form->getList());
  375. }
  376. /**
  377. * 同城配送预下单
  378. * @return \yii\web\Response
  379. */
  380. public function actionPreSubmitOrder() {
  381. $form = new OrderSubmitForm();
  382. $form->attributes = post_params();
  383. $form->store_id = get_store_id();
  384. $form->user_id = get_user_id();
  385. $form->user = get_user();
  386. return $this->asJson($form->preOrder());
  387. }
  388. /**
  389. * 预取消订单
  390. * @return \yii\web\Response
  391. */
  392. public function actionPreCancelOrder() {
  393. $order_id = post_params('order_id');
  394. $reason_id = post_params('reason_id', 5);
  395. $reason = post_params('reason', '其他原因');
  396. $order = Order::findOne($order_id);
  397. if (!$order) {
  398. $this->asJson([
  399. 'code' => 1,
  400. 'msg' => '订单不存在'
  401. ]);
  402. }
  403. if($order->alipay_trade_no){
  404. $res = ADelivery::cancelOrder(1, get_store_id(), get_mini_id(), get_saas_user()->ali_user_id, $order->order_no, $reason_id, $reason);;
  405. return $res;
  406. }
  407. $result = Delivery::preCancelOrder($order->order_no, $reason_id, $reason);
  408. return $this->asJson($result);
  409. }
  410. /**
  411. * 取消订单
  412. * @return \yii\web\Response
  413. */
  414. public function actionCancelOrder() {
  415. $order_id = post_params('order_id');
  416. $reason_id = post_params('reason_id', 5);
  417. $reason = post_params('reason', '其他原因');
  418. $order = Order::findOne($order_id);
  419. if (!$order) {
  420. $this->asJson([
  421. 'code' => 1,
  422. 'msg' => '订单不存在'
  423. ]);
  424. }
  425. if($order->alipay_trade_no){
  426. $res = ADelivery::cancelOrder(0, get_store_id(), get_mini_id(), get_saas_user()->ali_user_id, $order->order_no, $reason_id, $reason);;
  427. return $res;
  428. }
  429. $result = Delivery::cancelOrder($order->order_no, $reason_id, $reason);
  430. return $this->asJson($result);
  431. }
  432. /**
  433. * @return \yii\web\Response
  434. */
  435. public function actionUserDelete() {
  436. $order_id = post_params('order_id');
  437. $order = Order::findOne(['id' => $order_id, 'user_delete' => 1]);
  438. if ($order) {
  439. return $this->asJson([
  440. 'code' => 1,
  441. 'msg' => '订单已删除'
  442. ]);
  443. }
  444. Order::updateAll(['user_delete' => 1], ['id' => $order_id]);
  445. return $this->asJson([
  446. 'code' => 0,
  447. 'msg' => '删除成功'
  448. ]);
  449. }
  450. public function actionFriendPay() {
  451. $order_id = post_params('order_id');
  452. $order = Order::findOne(['id' => $order_id, 'is_delete' => 0]);
  453. \Yii::warning(['-------- 代付 -------', [$order_id, get_user_id()]]);
  454. if (get_user_id() != $order->user_id) {
  455. $order->pay_user_id = get_user_id();
  456. $order->save();
  457. }
  458. return $this->asJson([
  459. 'code' => 0,
  460. 'msg' => 'success'
  461. ]);
  462. }
  463. public function aliConfigQr($request, $data = [])
  464. {
  465. try {
  466. $third_appid = Option::get("alipay_appid", 0, 'saas')['value'];
  467. $third_private_key = Option::get("alipay_app_private_key", 0, 'saas')['value'];
  468. $third_public_key = Option::get("alipay_public_key", 0, 'saas')['value'];
  469. $data = json_encode($data);
  470. $config_ali = Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value'];
  471. $config_ali = json_decode($config_ali, true);
  472. $aop = new AopClient();
  473. //如果商城的支付宝小程序信息存在,则使用支付宝小程序的配置信息
  474. if (!empty($config_ali['app_id']) && !empty($config_ali['app_private_key']) && !empty($config_ali['app_public_key'])) {
  475. $aop->appId = $config_ali['app_id'];
  476. $aop->rsaPrivateKey = $config_ali['app_private_key'];
  477. $aop->alipayrsaPublicKey = $config_ali['app_public_key'];
  478. } else {
  479. //使用三方的配置信息
  480. $aop->appId = $third_appid;
  481. $aop->rsaPrivateKey = $third_private_key;
  482. $aop->alipayrsaPublicKey = $third_public_key;
  483. $ali_mini = StoreAliMini::find()->where(['auth_app_id' => $config_ali['app_id'], 'is_cancel' => 0, 'store_id' => get_store_id()])->asArray()->one();
  484. if (empty($ali_mini)) {
  485. throw new \Exception("查找小程序信息失败");
  486. }
  487. $token = $ali_mini['auth_token'];
  488. }
  489. $aop->encryptType = "AES";
  490. $aop->apiVersion = '1.0';
  491. $aop->signType = 'RSA2';
  492. $aop->postCharset = 'utf-8';
  493. $aop->format = 'json';
  494. $request->setBizContent($data);
  495. if (!empty($token)) {
  496. $result = $aop->execute ($request, null, $token);
  497. } else {
  498. $result = $aop->execute ($request);
  499. }
  500. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  501. return $result->$responseNode;
  502. } catch (\Exception $e) {
  503. return [
  504. 'code' => 1,
  505. 'msg' => $e->getMessage()
  506. ];
  507. }
  508. }
  509. }