PtOrderController.php 17 KB

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