OrderController.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\custom\classes\CustomClass;
  7. use bizHd\goods\classes\GoodsClass;
  8. use bizHd\merchant\services\ShopService;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\classes\OrderSendClass;
  11. use bizHd\order\services\OrderService;
  12. use bizHd\saas\services\RegionService;
  13. use bizHd\user\services\UserService;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use common\components\dict;
  16. use common\components\dateUtil;
  17. use common\components\payUtil;
  18. use common\services\xhPayToolService;
  19. use Yii;
  20. use common\components\util;
  21. use common\components\stringUtil;
  22. use bizHd\promote\services\CouponService;
  23. use common\components\httpUtil;
  24. class OrderController extends BaseController
  25. {
  26. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  27. //发货
  28. public function actionSend()
  29. {
  30. $get = Yii::$app->request->get();
  31. $id = isset($get['id']) ? $get['id'] : 0;
  32. $order = OrderClass::getById($id, true);
  33. if (empty($order)) {
  34. util::fail('没有找到订单');
  35. }
  36. OrderClass::valid($order, $this->mainId);
  37. if ($order->status != 2) {
  38. util::fail('订单不是待发货状态,请刷新');
  39. }
  40. $order->status = 4;
  41. $order->save();
  42. util::complete('操作成功');
  43. }
  44. //打印订单 ssh 20220601
  45. public function actionPrintOrder()
  46. {
  47. $get = Yii::$app->request->get();
  48. $id = isset($get['id']) ? $get['id'] : 0;
  49. $order = OrderClass::getById($id, true);
  50. if (empty($order)) {
  51. util::fail('没有找到订单');
  52. }
  53. OrderClass::valid($order, $this->mainId);
  54. OrderClass::onlinePrint($order, true);
  55. util::complete();
  56. }
  57. //微信和支付宝付款码支付复查 ssh 20220422
  58. public function actionCodePayCheck()
  59. {
  60. ini_set('date.timezone', 'Asia/Shanghai');
  61. header("Content-type: text/html; charset=utf-8");
  62. $get = Yii::$app->request->get();
  63. $id = isset($get['id']) ? $get['id'] : 0;
  64. $order = OrderClass::getById($id, true);
  65. OrderClass::valid($order, $this->mainId);
  66. $orderSn = $order->orderSn ?? '';
  67. $totalFee = $order->mainPay ?? 0;
  68. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  69. util::success(['returnStatus' => 'SUCCESS']);
  70. }
  71. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  72. util::complete('订单不是待付款状态');
  73. }
  74. $connection = Yii::$app->db;
  75. $transaction = $connection->beginTransaction();
  76. try {
  77. $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
  78. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  79. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  80. //微信付款
  81. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  82. require_once($wx . '/lib/WxPay.Api.php');
  83. require_once($wx . '/example/WxPay.MicroPay.php');
  84. //获取微信商户号等信息
  85. $sjExtend = WxOpenClass::getMallWxInfo();
  86. $microPay = new \MicroPay();
  87. $payReturn = $microPay->query($orderSn, $sjExtend);
  88. if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
  89. if ($payReturn["trade_state"] == "SUCCESS") {
  90. //支付成功
  91. $transactionId = $payReturn['transaction_id'] ?? '';
  92. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  93. $order->save();
  94. $attach = '';
  95. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  96. $transaction->commit();
  97. //打印小票和语音播报
  98. $newOrder = OrderClass::getById($id, true);
  99. OrderClass::onlinePrint($newOrder);
  100. ShopExtClass::hdGatheringReport($newOrder);
  101. util::success(['returnStatus' => 'SUCCESS']);
  102. } else if ($payReturn["trade_state"] == "USERPAYING") {
  103. //用户支付中
  104. util::complete('正在付款,请稍候..');
  105. }
  106. }
  107. if ($payReturn["err_code"] == "ORDERNOTEXIST") {
  108. //交易订单号不存在
  109. util::complete('订单没有付款');
  110. } else {
  111. util::complete('系统错误,请稍候再试');
  112. }
  113. } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
  114. //支付宝付款
  115. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  116. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  117. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  118. // $aliPId = '2017041906821571';
  119. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDqI9jWHE1ZhkGmB1+8hKpeIlEeVGAeYVveUgssMORdA/YY8SWHk26KSIFXBrmdKjAcRJ8Q7ZHUmzIqAV+IMV+RD4J5mZHeJgdAECgYEA8J+Hp1M6eYbT4CPLXa//yRP6Xo+qSzrKHaRB1LRAjYhvKpx/TQetYpNCJ5bWpa+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7FjjIGkbwa0H3A8Ydnvw5z4A9TZjjHLjC2GtVGJReqHGglMECgYEAydddA4NRPxmXvIjjmLb1Ft1qw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7r50nGCCqXBtQsF4KITRzqrAoGAZBNyYNgBguRaEd1Sxw2gPmOUfvqiUCo6F4MJ+8xN8idn4FnaofcH8ic613PQPqpB/yYaxeqgIEfnvGY9ILXCuvbePfYqFmMwzALWvZ+v7uVKa2M7HstWCrq7O+m/lQFN/ut8ZnUDcBn4WwwHa/PjCYietetO2gpDRmAdSqrWGH8CgYBkwp+r6pkJzW60kHSZIlEKAe7oJMwLNC2ZqQ4MwGwzfBaH+E34kCuR8ZqYzyAIVOa/Nwi5By1Zvi1KzBwJmUUTJ3o7WCOE96EzSrmOZlqXNCwO/36Vh6dshhhE/birIlXJSP0xdzpBQy2ksyli9eGy8cdJ2Y72Wo+TjSclRbKiPQKBgQD1C/CJRQbahz6B3Sc4NOiFUQioM30qDKbDibzUq95u5PtQOx69+/h68tA2nj7ILZOXoh6/KF6LM9WK0QctY0HR5Uy4Uz8YkY/Y0L/LExSkNdpYqbpBc8DdIsKYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  123. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  124. $config = array(
  125. //签名方式,默认为RSA2(RSA2048)
  126. 'sign_type' => "RSA2",
  127. //支付宝公钥
  128. 'alipay_public_key' => $aliPublicKey,
  129. //商户私钥
  130. 'merchant_private_key' => $aliKey,
  131. //编码格式
  132. 'charset' => "UTF-8",
  133. //支付宝网关
  134. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  135. //应用ID
  136. 'app_id' => $aliPId,
  137. //异步通知地址,只有扫码支付预下单可用
  138. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  139. //最大查询重试次数
  140. 'MaxQueryRetry' => "10",
  141. //查询间隔
  142. 'QueryDuration' => "3"
  143. );
  144. //构造查询业务请求参数对象
  145. $queryContentBuilder = new \AlipayTradeQueryContentBuilder();
  146. $queryContentBuilder->setOutTradeNo($orderSn);
  147. //初始化类对象,调用queryTradeResult方法获取查询应答
  148. $queryResponse = new \AlipayTradeService($config);
  149. $queryResult = $queryResponse->queryTradeResult($queryContentBuilder);
  150. if ($queryResult->getTradeStatus() == 'SUCCESS') {
  151. $respond = $queryResult->getResponse();
  152. $transactionId = $respond->trade_no ?? '';
  153. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  154. $order->save();
  155. $attach = '';
  156. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  157. $transaction->commit();
  158. //打印小票和语音播报
  159. $newOrder = OrderClass::getById($id, true);
  160. OrderClass::onlinePrint($newOrder);
  161. ShopExtClass::hdGatheringReport($newOrder);
  162. util::success(['returnStatus' => 'SUCCESS']);
  163. }
  164. } else {
  165. util::complete('还没有付款哦');
  166. }
  167. } catch (\Exception $e) {
  168. $transaction->rollBack();
  169. Yii::error("支付失败原因:" . $e->getMessage());
  170. util::complete('支付失败');
  171. }
  172. }
  173. //微信和支付宝付款码支付 ssh 2021.4.11
  174. public function actionCodePay()
  175. {
  176. ini_set('date.timezone', 'Asia/Shanghai');
  177. header("Content-type: text/html; charset=utf-8");
  178. $get = Yii::$app->request->get();
  179. $id = isset($get['id']) ? $get['id'] : 0;
  180. $order = OrderClass::getById($id, true);
  181. OrderClass::valid($order, $this->mainId);
  182. $authCode = (string)$get['authCode'] ?? '';
  183. if (empty($authCode)) {
  184. util::fail('没有获取到支付码');
  185. }
  186. $wxPrefix = ['10', '11', '12', '13', '14', '15'];
  187. $isWx = false;
  188. foreach ($wxPrefix as $wxId) {
  189. if (strpos($authCode, $wxId) === 0) {
  190. $isWx = true;
  191. }
  192. }
  193. $orderSn = $order->orderSn ?? '';
  194. $outTradeNo = $orderSn;
  195. $subject = '购买商品';
  196. $totalFee = $order->mainPay ?? 0;
  197. $totalAmount = $totalFee;
  198. $body = "门店";
  199. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  200. $connection = Yii::$app->db;
  201. $transaction = $connection->beginTransaction();
  202. try {
  203. if ($isWx) {
  204. $wxPayWay = dict::getDict('payWay', 'wxPay');
  205. $order->payWay = $wxPayWay;
  206. $order->save();
  207. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  208. require_once($wx . '/lib/WxPay.Api.php');
  209. require_once($wx . '/example/WxPay.MicroPay.php');
  210. $input = new \WxPayMicroPay();
  211. $input->SetAuth_code($authCode);
  212. $input->SetBody($subject);
  213. $input->SetTotal_fee($totalFee * 100);
  214. $input->SetOut_trade_no($orderSn);
  215. //获取微信商户号等信息
  216. $sjExtend = WxOpenClass::getMallWxInfo();
  217. $microPay = new \MicroPay();
  218. $res = $microPay->pay($input, $sjExtend);
  219. if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS" && isset($res["result_code"]) && $res["result_code"] == 'SUCCESS') {
  220. $transactionId = $res['transaction_id'] ?? '';
  221. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  222. $order->save();
  223. $attach = '';
  224. payUtil::thirdPay($wxPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  225. $transaction->commit();
  226. //打印小票和语音播报
  227. $newOrder = OrderClass::getById($id, true);
  228. OrderClass::onlinePrint($newOrder);
  229. ShopExtClass::hdGatheringReport($newOrder);
  230. util::success(['returnStatus' => 'SUCCESS']);
  231. } else {
  232. Yii::info(json_encode($res));
  233. $returnMsg = $res['return_msg'] ?? '';
  234. Yii::info($returnMsg);
  235. $errCode = $res['err_code'] ?? '';
  236. $msg = '';
  237. if ($errCode == 'NOTENOUGH') {
  238. $msg = '余额不足';
  239. }
  240. if ($errCode == 'USERPAYING') {
  241. $msg = '等待客户输入支付密码';
  242. }
  243. util::success(['returnStatus' => 'FAILURE'], $msg);
  244. }
  245. } else {
  246. $aliPayWay = dict::getDict('payWay', 'alipay');
  247. $order->payWay = $aliPayWay;
  248. $order->save();
  249. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  250. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  251. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  252. // $aliPId = '2017041906821571';
  253. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDqI9jWHE1ZhkGmB1+8hKpeIlEeVGAeYVveUgssMORdA/YY8SWHk26KSIFXBrmdK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1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  258. $config = array(
  259. //签名方式,默认为RSA2(RSA2048)
  260. 'sign_type' => "RSA2",
  261. //支付宝公钥
  262. 'alipay_public_key' => $aliPublicKey,
  263. //商户私钥
  264. 'merchant_private_key' => $aliKey,
  265. //编码格式
  266. 'charset' => "UTF-8",
  267. //支付宝网关
  268. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  269. //应用ID
  270. 'app_id' => $aliPId,
  271. //异步通知地址,只有扫码支付预下单可用
  272. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  273. //最大查询重试次数
  274. 'MaxQueryRetry' => "10",
  275. //查询间隔
  276. 'QueryDuration' => "3"
  277. );
  278. // 支付超时,线下扫码交易定义为5分钟
  279. $timeExpress = "5m";
  280. // 创建请求builder,设置请求参数
  281. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  282. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  283. $barPayRequestBuilder->setTotalAmount($totalAmount);
  284. $barPayRequestBuilder->setAuthCode($authCode);
  285. $barPayRequestBuilder->setTimeExpress($timeExpress);
  286. $barPayRequestBuilder->setSubject($subject);
  287. $barPayRequestBuilder->setBody($body);
  288. // 调用barPay方法获取当面付应答
  289. $barPay = new \AlipayTradeService($config);
  290. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  291. if ($barPayResult->getTradeStatus() == 'SUCCESS') {
  292. $respond = $barPayResult->getResponse();
  293. $transactionId = $respond->trade_no ?? '';
  294. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  295. $order->save();
  296. $attach = '';
  297. payUtil::thirdPay($aliPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  298. $transaction->commit();
  299. //打印小票和语音播报
  300. $newOrder = OrderClass::getById($id, true);
  301. OrderClass::onlinePrint($newOrder);
  302. ShopExtClass::hdGatheringReport($newOrder);
  303. util::success(['returnStatus' => 'SUCCESS']);
  304. }
  305. util::success(['returnStatus' => 'FAILURE']);
  306. }
  307. } catch (\Exception $e) {
  308. $transaction->rollBack();
  309. Yii::error("支付失败原因:" . $e->getMessage());
  310. util::fail('支付失败');
  311. }
  312. }
  313. //开单操作 ssh 20220316
  314. public function actionCreateOrder()
  315. {
  316. $post = Yii::$app->request->post();
  317. $post['sjId'] = $this->sjId;
  318. $post['shopId'] = $this->shopId;
  319. $post['mainId'] = $this->mainId ?? 0;
  320. $now = time();
  321. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  322. $expireTime = $now + $orderValidTime;
  323. $post['deadline'] = $expireTime;
  324. $post['staffId'] = $this->shopAdminId ?? 0;
  325. $post['staffName'] = $this->shopAdmin->name ?? '';
  326. //默认情况开单员工和收款员工是同个人
  327. $staffId = $this->shopAdminId ?? 0;
  328. $post['shopAdminId'] = $staffId;
  329. if (isset($post['getStaffId']) == false || empty($post['getStaffId'])) {
  330. $post['getStaffId'] = $staffId;
  331. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  332. }
  333. $post['shopAdminName'] = $this->shopAdmin->name ?? '';
  334. $groupId = isset($post['groupId']) && !empty($post['groupId']) ? $post['groupId'] : 0;
  335. $post['fromType'] = $post['fromType'] ?? 1;
  336. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  337. if (isset($post['bookName']) == false || empty($post['bookName'])) {
  338. util::fail('请填写订花人姓名,没有填【无】');
  339. }
  340. }
  341. $customId = isset($post['customId']) && $post['customId'] > 0 ? $post['customId'] : 0;
  342. if (empty($customId)) {
  343. $customId = $this->shop->defaultCustomId ?? 0;
  344. }
  345. $custom = CustomClass::getById($customId);
  346. if (empty($custom)) {
  347. util::fail('没有找到客户哦');
  348. }
  349. $post['customId'] = $customId;
  350. if (isset($post['receiveMobile']) && !empty($post['receiveMobile'])) {
  351. if (stringUtil::isMobile($post['receiveMobile']) == false) {
  352. util::fail('请填写正确的收花人手机号');
  353. }
  354. }
  355. if (isset($post['bookMobile']) && !empty($post['bookMobile'])) {
  356. if (stringUtil::isMobile($post['bookMobile']) == false) {
  357. util::fail('请填写正确的订花人手机号');
  358. }
  359. }
  360. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  361. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  362. //收银台开单默认到店自取
  363. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  364. }
  365. $productJson = $post['product'] ?? '';
  366. $productList = [];
  367. if (!empty($productJson)) {
  368. $productList = json_decode($productJson, true);
  369. }
  370. if (empty($productList) && empty($groupId)) {
  371. //商家手机上开单并生成制作单
  372. if (isset($post['unitGoodsPrice']) && !empty($post['unitGoodsPrice'])) {
  373. $unitGoodsPrice = $post['unitGoodsPrice'];
  374. if (is_numeric($unitGoodsPrice) == false) {
  375. util::fail('请输入正确的单价');
  376. }
  377. $productList = GoodsClass::orderCreateGoods($post);
  378. }
  379. }
  380. if (empty($productList) && empty($groupId)) {
  381. if (isset($post['lsGoodsPrice']) == false || $post['lsGoodsPrice'] <= 0) {
  382. $lsGoodsPrice = $post['lsGoodsPrice'];
  383. if (is_numeric($lsGoodsPrice) == false) {
  384. util::fail('请输入正确的金额');
  385. }
  386. util::fail('请填写金额或选商品');
  387. }
  388. }
  389. $connection = Yii::$app->db;
  390. $transaction = $connection->beginTransaction();
  391. try {
  392. $post['product'] = $productList;
  393. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  394. $transaction->commit();
  395. //打印小票和语音播报
  396. $id = $return->id ?? 0;
  397. $order = OrderClass::getById($id, true);
  398. if ($order->fromType == 3 || $order->fromType == 4) {
  399. //除了客服号下单和美团下单,其它情况允许打小票
  400. } else {
  401. //前台打小票
  402. OrderClass::onlinePrint($order);
  403. }
  404. //前台提示收款多少钱
  405. ShopExtClass::hdGatheringReport($order);
  406. //前台提醒出示付款码
  407. if (isset($order->status) && $order->status == 1) {
  408. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  409. ShopExtClass::pleasePayReport($order);
  410. }
  411. }
  412. //新制作单提示
  413. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  414. ShopExtClass::newWorkRemind($shopExt, $order);
  415. util::success($return);
  416. } catch (\Exception $e) {
  417. $transaction->rollBack();
  418. Yii::error("失败原因:" . $e->getMessage());
  419. util::fail('下单失败');
  420. }
  421. }
  422. //下单要用到的相关信息 ssh 2019.12.6
  423. public function actionOrderRelate()
  424. {
  425. $regionTree = RegionService::tree();
  426. $shop = $this->shop->attributes;
  427. $freight = MerchantExtendService::getFreight($this->sjExtend);
  428. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  429. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  430. util::success($out);
  431. }
  432. //余额支付 ssh 2019.12.6
  433. public function actionBalancePay()
  434. {
  435. $post = Yii::$app->request->post();
  436. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  437. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  438. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  439. $order = OrderService::getByOrderSn($orderSn);
  440. $userId = $this->adminId;
  441. $user = UserService::getUserInfo($userId);
  442. //验证支付密码是否正确
  443. UserService::validPayPassword($payPassword, $user);
  444. //支付前验证订单有效性
  445. OrderService::checkBeforePay($order);
  446. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  447. $totalFee = $order['prePrice'];
  448. $sourceType = 0;
  449. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  450. $actPrice = $discountData['price'];
  451. $callbackParams = [];
  452. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  453. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  454. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  455. }
  456. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  457. public function actionWxPay()
  458. {
  459. ini_set('date.timezone', 'Asia/Shanghai');
  460. $post = Yii::$app->request->post();
  461. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  462. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  463. $order = OrderService::getByOrderSn($orderSn);
  464. $orderId = $order['id'];
  465. //验证优惠券是否还有效
  466. if (!empty($couponId)) {
  467. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  468. }
  469. //支付前验证订单有效性
  470. OrderService::checkBeforePay($order);
  471. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  472. $totalFee = $order['realPrice'];
  473. //小程序使用miniOpenId
  474. if (httpUtil::isMiniProgram()) {
  475. $openId = $this->user['miniOpenId'];
  476. } else {
  477. $openId = $this->user['openId'];
  478. }
  479. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  480. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  481. $wxPayType = 0;
  482. if (httpUtil::isMiniProgram()) {
  483. $wxPayType = 1;
  484. }
  485. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  486. //订单30分钟后过期
  487. $now = time();
  488. $expireTime = $now + 1800;
  489. $wx = Yii::getAlias("@vendor/weixin");
  490. require_once($wx . '/lib/WxPay.Api.php');
  491. require_once($wx . '/example/WxPay.JsApiPay.php');
  492. $input = new \WxPayUnifiedOrder();
  493. $input->SetBody($name);
  494. $input->SetOut_trade_no($orderSn);
  495. $input->SetTotal_fee($totalFee * 100);
  496. $input->SetTime_start(date("YmdHis", $now));
  497. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  498. $input->SetTime_expire(date("YmdHis", $expireTime));
  499. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  500. $input->SetTrade_type("JSAPI");
  501. //花卉宝代为申请的微信支付
  502. $merchantExtend = $this->sjExtend->attributes;
  503. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  504. $input->SetSub_openid($openId);
  505. } else {
  506. $input->SetOpenid($openId);
  507. }
  508. //小程序使用miniAppId
  509. if (httpUtil::isMiniProgram()) {
  510. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  511. }
  512. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  513. $tools = new \JsApiPay();
  514. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  515. $newParams = json_decode($jsApiParameters, true);
  516. //已请求微信不能修改价格
  517. $updateData = [];
  518. $updateData['modPrice'] = 0;
  519. if (empty($order['deadline'])) {
  520. $updateData['deadline'] = $expireTime;
  521. }
  522. OrderService::updateById($orderId, $updateData);
  523. util::success($newParams);
  524. }
  525. //快捷付款订单
  526. public function actionFastPay()
  527. {
  528. ini_set('date.timezone', 'Asia/Shanghai');
  529. $post = Yii::$app->request->post();
  530. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  531. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  532. //验证门店是否有效
  533. $shopInfo = ShopService::getById($shopId);
  534. ShopService::valid($shopInfo, $this->sjId);
  535. $post['shopId'] = $shopId;
  536. //默认门店订单
  537. $store = isset($post['store']) ? $post['store'] : 1;
  538. $post['store'] = $store;
  539. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  540. $sourceType = $this->isWx ? 0 : 1;
  541. if (httpUtil::isMiniProgram()) {
  542. $sourceType = 2;
  543. }
  544. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  545. //兼容旧系统sourceType=3表示朋友圈来源
  546. $fromType = $sourceType == 3 ? 2 : 0;
  547. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  548. $post['userId'] = $this->adminId;
  549. $custom = $this->custom;
  550. $post['bookName'] = $custom['userName'] ?? '';
  551. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  552. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  553. $post['bookMobile'] = $bookMobile;
  554. $prePrice = round($post['prePrice'], 2);
  555. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  556. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  557. $actPrice = $discountData['price'];
  558. $post['discountType'] = $discountData['discountType'];
  559. $post['discountAmount'] = $discountData['discountAmount'];
  560. $post['actPrice'] = $actPrice;
  561. $post['realPrice'] = $actPrice;
  562. $post['payStyle'] = 0;
  563. $post['sjId'] = $this->sjId;
  564. $now = time();
  565. $expireTime = $now + 300;//订单5分钟后过期
  566. $post['createTime'] = date("Y-m-d H:i:s", $now);
  567. $post['deadline'] = $expireTime;
  568. if ($actPrice <= 0) {
  569. util::fail('请填写正确的金额');
  570. }
  571. //微信支付订单提交不能修改价格
  572. $post['modPrice'] = $this->isWx ? 0 : 1;
  573. $post['sourceType'] = $sourceType;
  574. $post['goodsNum'] = 1;
  575. $post['fromType'] = $fromType;
  576. $order = OrderService::addOrder($post);
  577. $orderId = $order['id'];
  578. $orderSn = $order['orderSn'];
  579. $sjId = $this->sjId;
  580. if ($payWay == 0) {
  581. $orderId = $order['id'];
  582. $name = '购买商品';
  583. $totalFee = $actPrice;
  584. $user = UserService::getById($this->adminId);
  585. $openId = isset($user['openId']) ? $user['openId'] : 0;
  586. if (httpUtil::isMiniProgram()) {
  587. //小程序使用miniOpenId
  588. $openId = $user['miniOpenId'];
  589. }
  590. if (empty($openId)) {
  591. util::fail('没有找到客户的openId');
  592. }
  593. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  594. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  595. $wxPayType = 0;
  596. if (httpUtil::isMiniProgram()) {
  597. $wxPayType = 1;
  598. }
  599. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  600. $wx = Yii::getAlias("@vendor/weixin");
  601. require_once($wx . '/lib/WxPay.Api.php');
  602. require_once($wx . '/example/WxPay.JsApiPay.php');
  603. $input = new \WxPayUnifiedOrder();
  604. $input->SetBody($name);
  605. $input->SetOut_trade_no($orderSn);
  606. $input->SetTotal_fee($totalFee * 100);
  607. $input->SetTime_start(date("YmdHis", $now));
  608. $input->SetAttach($attach);
  609. //设置订单有效期5分钟
  610. $input->SetTime_expire(date("YmdHis", $expireTime));
  611. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  612. $input->SetTrade_type("JSAPI");
  613. //花卉宝代为申请的微信支付
  614. $merchantExtend = $this->sjExtend->attributes;
  615. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  616. $input->SetSub_openid($openId);
  617. } else {
  618. $input->SetOpenid($openId);
  619. }
  620. //小程序使用miniAppId
  621. if (httpUtil::isMiniProgram()) {
  622. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  623. }
  624. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  625. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  626. $tools = new \JsApiPay();
  627. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  628. $newParams = json_decode($jsApiParameters, true);
  629. $newParams['orderId'] = $orderId;
  630. util::success($newParams);
  631. } elseif ($payWay == 1) {
  632. $extend = MerchantExtendService::getBySjId($sjId);
  633. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  634. util::fail('支付宝付款即将开通...');
  635. }
  636. $config = [
  637. //应用ID,您的APPID。
  638. 'app_id' => $extend['alipayPId'],
  639. //商户私钥,您的原始格式RSA私钥
  640. 'merchant_private_key' => $extend['alipayKey'],
  641. //异步通知地址
  642. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  643. //同步跳转
  644. 'return_url' => "",
  645. //编码格式
  646. 'charset' => "UTF-8",
  647. //签名方式
  648. 'sign_type' => "RSA2",
  649. //支付宝网关
  650. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  651. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  652. 'alipay_public_key' => $extend['alipayPublicKey'],
  653. ];
  654. Yii::info(json_encode($config) . ' aplipay config');
  655. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  656. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  657. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  658. $totalFee = $order['realPrice'];
  659. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  660. $subject = '购买商品';//订单名称,必填
  661. $total_amount = $totalFee;//付款金额,必填
  662. $body = '';//商品描述,可空
  663. $timeout_express = "1m";//超时时间
  664. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  665. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  666. $passBackParams = urlencode($passBackParams);
  667. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  668. $payRequestBuilder->setBody($body);
  669. $payRequestBuilder->setSubject($subject);
  670. $payRequestBuilder->setOutTradeNo($out_trade_no);
  671. $payRequestBuilder->setTotalAmount($total_amount);
  672. $payRequestBuilder->setTimeExpress($timeout_express);
  673. //在异步通知时将该参数原样返回
  674. $payRequestBuilder->setPassbackParams($passBackParams);
  675. //同步通知
  676. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  677. //异步通知
  678. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  679. $payResponse = new \AlipayTradeService($config);
  680. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  681. //直接将支付宝的html返回给前端
  682. echo $result;
  683. } elseif ($payWay == 2) {
  684. //余额支付,验证支付密码是否正确
  685. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  686. $user = UserService::getUserInfo($this->adminId);
  687. UserService::validPayPassword($payPassword, $user);
  688. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  689. $callbackParams = [];
  690. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  691. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  692. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  693. } else {
  694. util::fail('无效的支付方式');
  695. }
  696. }
  697. //订单评价
  698. public function actionComment()
  699. {
  700. $post = Yii::$app->request->post();
  701. $id = $post['id'];
  702. $order = OrderService::getById($id);
  703. OrderService::valid($order, $this->mainId);
  704. if ($order['grade'] > 0) {
  705. util::fail('您已经评过了');
  706. }
  707. OrderService::comment($post);
  708. util::complete('提交成功');
  709. }
  710. //订单详情 ssh 2019.12.16
  711. public function actionDetail()
  712. {
  713. $id = Yii::$app->request->get('id', 0);
  714. $detail = OrderClass::getFullInfo($id);
  715. OrderService::valid($detail, $this->mainId);
  716. util::success($detail);
  717. }
  718. public function actionList()
  719. {
  720. $get = Yii::$app->request->get();
  721. $search = isset($get['search']) ? $get['search'] : '';
  722. $shopId = $this->shopId ?? 0;
  723. if (empty($shopId)) {
  724. //没有登录不显示数据
  725. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  726. }
  727. $where = [];
  728. $where['shopId'] = $shopId;
  729. $status = $get['status'] ?? 0;
  730. if (!empty($status)) {
  731. $where['status'] = $get['status'];
  732. }
  733. if (isset($get['shopAdminId']) && !empty($get['shopAdminId'])) {
  734. $where['shopAdminId'] = $get['shopAdminId'];
  735. }
  736. if (isset($get['searchText']) && !empty($get['searchText'])) {
  737. if (is_numeric($get['searchText'])) {
  738. $where['sendNum'] = $get['searchText'];
  739. } else {
  740. $where['bookName'] = $get['searchText'];
  741. }
  742. }
  743. if (!empty($search)) {
  744. if (stringUtil::isMobile($search)) {
  745. $where['bookMobile'] = $search;
  746. } else {
  747. $where['orderSn'] = $search;
  748. }
  749. }
  750. $searchTime = $get['searchTime'] ?? '';
  751. if (!empty($searchTime)) {
  752. $startTime = $get['startTime'] ?? '';
  753. $endTime = $get['endTime'] ?? '';
  754. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  755. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  756. }
  757. $list = OrderService::getOrderList($where);
  758. $list['shop'] = $this->shop->attributes ?? [];
  759. util::success($list);
  760. }
  761. //订单归类 ssh 2019.12.17
  762. public function actionClassify()
  763. {
  764. $post = Yii::$app->request->post();
  765. $id = isset($post['id']) ? $post['id'] : 0;
  766. $order = OrderService::getById($id);
  767. OrderService::valid($order, $this->mainId);
  768. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  769. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  770. OrderService::classify($id, $categoryId, $usageId);
  771. util::complete();
  772. }
  773. //更新配送单 ssh 2019.12.17
  774. public function actionUpdateSheet()
  775. {
  776. $post = Yii::$app->request->post();
  777. $id = isset($post['id']) ? $post['id'] : 0;
  778. unset($post['id']);
  779. $order = OrderService::getById($id);
  780. OrderService::valid($order, $this->mainId);
  781. OrderService::updateSheet($order, $post);
  782. util::complete('提交成功');
  783. }
  784. //商家收款与发货 ssh 2019.12.18
  785. public function actionGathering()
  786. {
  787. $post = Yii::$app->request->post();
  788. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  789. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  790. $userId = isset($post['userId']) ? $post['userId'] : 0;
  791. $userInfo = UserService::getById($userId);
  792. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  793. util::fail('您选择的客户无效');
  794. }
  795. if ($price <= 0) {
  796. util::fail('请输入金额');
  797. }
  798. $post['prePrice'] = $price;
  799. $post['actPrice'] = $price;
  800. $post['payWay'] = $payWay;
  801. $post['sourceType'] = 1;
  802. $post['userId'] = $userId;
  803. $post['goodsNum'] = 1;
  804. $post['orderName'] = '商品';
  805. $shopId = MerchantService::getDefaultShopId($this->sj);
  806. $post['shopId'] = $shopId;
  807. $order = OrderService::addOrder($post);
  808. $id = $order['id'];
  809. $orderSn = $order['orderSn'];
  810. //流水类型列表
  811. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  812. $callbackParams = [];
  813. switch ($payWay) {
  814. case 0:
  815. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  816. break;
  817. case 1:
  818. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  819. break;
  820. case 2:
  821. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  822. break;
  823. default:
  824. util::fail('无效支付方式');
  825. }
  826. util::success($order);
  827. }
  828. //免发货管理 ssh 2020.1.6
  829. public function actionWithoutSend()
  830. {
  831. $id = Yii::$app->request->get('id');
  832. $order = OrderService::getById($id);
  833. OrderService::valid($order, $this->mainId);
  834. //配送流程变更
  835. $currentFlow = OrderSendClass::withoutSend($order);
  836. util::success(['currentFlow' => $currentFlow]);
  837. }
  838. //修改价格 ssh 2020.1.6
  839. public function actionUpdatePrice()
  840. {
  841. $id = Yii::$app->request->get('id');
  842. $price = Yii::$app->request->get('price', 1);
  843. $order = OrderService::getById($id);
  844. OrderService::valid($order, $this->mainId);
  845. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  846. util::fail('已发起支付,不能修改价格');
  847. }
  848. OrderService::updateById($id, ['actPrice' => $price]);
  849. util::complete('修改成功');
  850. }
  851. //配送单 ssh 2020.2.29
  852. public function actionGetDeliverDetail()
  853. {
  854. $id = Yii::$app->request->get('id', 0);
  855. $order = OrderService::getById($id);
  856. OrderService::valid($order, $this->mainId);
  857. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  858. $reachPeriodId = $order['reachPeriod'];
  859. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  860. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  861. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  862. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  863. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  864. $bookName = '--';
  865. $bookMobile = '--';
  866. }
  867. $data = [
  868. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  869. 'orderSn' => $order['orderSn'],
  870. 'receiveUserName' => $order['receiveUserName'],
  871. 'receiveMobile' => $order['receiveMobile'],
  872. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  873. 'cardInfo' => $order['cardInfo'],
  874. 'bookMobile' => $bookMobile,
  875. 'bookName' => $bookName,
  876. 'remark' => $order['remark'],
  877. 'sendNum' => $order['sendNum'],
  878. 'telephone' => 18030142050,
  879. 'printTime' => '',
  880. 'img' => '',
  881. ];
  882. util::success($data);
  883. }
  884. //运费计算 lqh 2021.4.12 暂反回固定
  885. public function actionFreight()
  886. {
  887. util::success(['sedCost' => 10]);
  888. }
  889. }