OrderController.php 54 KB

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