OrderController.php 53 KB

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