OrderController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. namespace shop\controllers;
  3. use biz\merchant\services\MerchantAssetService;
  4. use biz\merchant\services\MerchantExtendService;
  5. use biz\merchant\services\MerchantService;
  6. use biz\merchant\services\ShopService;
  7. use biz\order\classes\OrderClass;
  8. use biz\order\classes\OrderSendClass;
  9. use biz\order\services\OrderService;
  10. use biz\saas\services\RegionService;
  11. use biz\user\services\UserService;
  12. use common\components\configDict;
  13. use common\services\xhPayToolService;
  14. use Yii;
  15. use common\components\util;
  16. use common\components\stringUtil;
  17. use biz\goods\services\CategoryService;
  18. use biz\goods\services\GoodsCategoryService;
  19. use biz\goods\services\GoodsService;
  20. use biz\merchant\services\ExpressService;
  21. use biz\order\services\OrderGoodsService;
  22. use biz\promote\services\CouponService;
  23. use biz\user\services\UserAssetService;
  24. use common\components\httpUtil;
  25. use common\components\miniUtil;
  26. use yii\web\Controller;
  27. class OrderController extends BaseController
  28. {
  29. public $guestAccessAction = ['order-relate', 'fast-pay', 'create-order', 'list'];
  30. //商城下单操作 shish 2019.12.3
  31. public function actionCreateOrder()
  32. {
  33. $post = Yii::$app->request->post();
  34. $needSend = isset($post['needSend']) && is_numeric($post['needSend']) ? $post['needSend'] : 1;
  35. $orderType = $post['orderType'] ?? 1;
  36. $defaultShopId = MerchantService::getDefaultShopId($this->sj);
  37. if (empty($defaultShopId)) {
  38. util::fail('没有找到门店');
  39. }
  40. $post['merchantId'] = $this->sjId;
  41. $post['shopId'] = $defaultShopId;
  42. $post['payWay'] = $this->isWx == true ? 0 : 1;
  43. $now = time();
  44. $expireTime = $now + 1800;//订单30分钟后过期
  45. $post['deadline'] = $expireTime;
  46. $post['fromType'] = 1;//商城
  47. $customId = $post['customId'] ?? 0;
  48. if (!empty($customId)) {
  49. $customId = $this->customId;
  50. }
  51. $post['customId'] = $customId;
  52. //非门店订单
  53. $store = 0;
  54. $post['store'] = $store;
  55. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  56. if ($orderType == 1) {
  57. $goodsInfo = $post['goodsInfo'] ?? '';
  58. $goodsInfoArr = json_decode($goodsInfo, true);
  59. if (empty($goodsInfoArr)) {
  60. util::fail('请选择商品');
  61. }
  62. //总的商品金额
  63. $totalGoodsPrice = 0;
  64. $orderGoods = [];
  65. foreach ($goodsInfoArr as $key => $currentGoods) {
  66. $currentGoodsId = $currentGoods['goodsId'];
  67. $currentGoodsInfo = GoodsService::getGoodsInfo($currentGoodsId);
  68. if (empty($currentGoodsInfo)) {
  69. util::fail('没有找到商品');
  70. }
  71. $goodsStyleId = $currentGoods['goodsStyleId'];
  72. $goodsNum = $currentGoods['goodsNum'];
  73. //计算总金额
  74. $unitPrice = $currentGoodsInfo['price'];
  75. $goodsStyleList = isset($currentGoodsInfo['goodsStyleList']) ? $currentGoodsInfo['goodsStyleList'] : [];
  76. if (!empty($goodsStyleId)) {
  77. if (empty($goodsStyleList)) {
  78. util::fail('商品款式没有找到');
  79. }
  80. $styleIdList = array_keys($goodsStyleList);
  81. if (in_array($goodsStyleId, $styleIdList) == false) {
  82. util::fail('商品款式没有找到!');
  83. }
  84. $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
  85. }
  86. $goodsPrice = $unitPrice * $goodsNum;
  87. $totalGoodsPrice = stringUtil::calcAdd($totalGoodsPrice, $goodsPrice);
  88. $orderGoods[] = [
  89. 'goodsId' => $currentGoodsId,
  90. 'userId' => $this->adminId,
  91. 'merchantId' => $this->sjId,
  92. 'title' => $currentGoodsInfo['goodsName'],
  93. 'cover' => isset($goodsInfo['shortImgList']) && !empty($currentGoodsInfo['shortImgList']) ? current($currentGoodsInfo['shortImgList']) : '',
  94. 'unitPrice' => $unitPrice,
  95. 'num' => $goodsNum,
  96. 'goodsStyleName' => isset($currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
  97. 'goodsStyleId' => $goodsStyleId,
  98. 'createTime' => date("Y-m-d H:i:s"),
  99. ];
  100. }
  101. $user = $this->custom;
  102. $post['bookName'] = isset($user['name']) ? $user['name'] : '';
  103. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  104. $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  105. $post['bookMobile'] = $bookMobile;
  106. //计算运费
  107. $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
  108. $sendCost = 0;
  109. //按距离计算运费并且客户要求送的
  110. //运费计算方式
  111. $freightType = 0;
  112. if ($freightType == 0 && $needSend == 1) {
  113. $lat = $post['latitude'];//收货人纬度
  114. $lng = $post['longitude'];//收货人经度
  115. $respond = ExpressService::getUserDistance($lng, $lat, $defaultShopId, $this->sjExtend);
  116. $sendCost = isset($respond['fee']) ? $respond['fee'] : 0;
  117. }
  118. $post['sendCost'] = $sendCost;
  119. $prePrice = stringUtil::calcAdd($sendCost, $totalGoodsPrice);
  120. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  121. $sourceType = $this->isWx ? 0 : 1;
  122. if (httpUtil::isMiniProgram()) {
  123. $sourceType = 2;
  124. }
  125. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  126. $actPrice = $discountData['price'];
  127. $post['discountType'] = $discountData['discountType'];
  128. $post['discountAmount'] = $discountData['discountAmount'];
  129. $post['prePrice'] = $prePrice;
  130. $post['actPrice'] = $actPrice;
  131. $order = OrderClass::addOrder($post);
  132. $orderId = $order['id'];
  133. $orderSn = $order['orderSn'];
  134. foreach ($orderGoods as $key => $val) {
  135. $val['orderId'] = $orderId;
  136. $val['orderSn'] = $orderSn;
  137. OrderGoodsService::add($val);//创建订单商品列表
  138. }
  139. } else {
  140. $productData = $post['product'] ?? '';
  141. $product = json_decode($productData, true);
  142. $order = OrderClass::addOrder($post);
  143. $orderId = $order['id'];
  144. $orderSn = $order['orderSn'];
  145. }
  146. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'orderId' => $orderId]);
  147. }
  148. //下单要用到的相关信息 shish 2019.12.6
  149. public function actionOrderRelate()
  150. {
  151. $regionTree = RegionService::tree();
  152. $shop = ShopService::getDefaultShop($this->sj);
  153. $freight = MerchantExtendService::getFreight($this->sjExtend);
  154. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  155. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->sj];
  156. util::success($out);
  157. }
  158. //余额支付 shish 2019.12.6
  159. public function actionBalancePay()
  160. {
  161. $post = Yii::$app->request->post();
  162. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  163. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  164. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  165. //验证优惠券是否还有效
  166. if (!empty($couponId)) {
  167. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  168. }
  169. $order = OrderService::getByOrderSn($orderSn);
  170. $userId = $this->adminId;
  171. $user = UserService::getUserInfo($userId, false);
  172. //验证支付密码是否正确
  173. UserService::validPayPassword($payPassword, $user);
  174. //支付前验证订单有效性
  175. OrderService::checkBeforePay($order, $this->adminId);
  176. $typeList = configDict::getConfig('capitalType');
  177. $capitalType = $typeList['xhOrder']['id'];
  178. $totalFee = $order['prePrice'];
  179. $sourceType = 0;
  180. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  181. $actPrice = $discountData['price'];
  182. $callbackParams = [];
  183. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  184. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  185. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  186. }
  187. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  188. public function actionWxPay()
  189. {
  190. ini_set('date.timezone', 'Asia/Shanghai');
  191. $post = Yii::$app->request->post();
  192. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  193. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  194. $order = OrderService::getByOrderSn($orderSn);
  195. $orderId = $order['id'];
  196. //验证优惠券是否还有效
  197. if (!empty($couponId)) {
  198. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  199. }
  200. //支付前验证订单有效性
  201. OrderService::checkBeforePay($order, $this->adminId);
  202. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  203. $totalFee = $order['actPrice'];
  204. //小程序使用miniOpenId
  205. if (httpUtil::isMiniProgram()) {
  206. $openId = $this->user['miniOpenId'];
  207. } else {
  208. $openId = $this->user['openId'];
  209. }
  210. $typeList = configDict::getConfig('capitalType');
  211. $capitalType = $typeList['xhOrder']['id'];
  212. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  213. $wxPayType = 0;
  214. if (httpUtil::isMiniProgram()) {
  215. $wxPayType = 1;
  216. }
  217. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  218. //订单30分钟后过期
  219. $now = time();
  220. $expireTime = $now + 1800;
  221. $wx = Yii::getAlias("@vendor/weixin");
  222. require_once($wx . '/lib/WxPay.Api.php');
  223. require_once($wx . '/example/WxPay.JsApiPay.php');
  224. $input = new \WxPayUnifiedOrder();
  225. $input->SetBody($name);
  226. $input->SetOut_trade_no($orderSn);
  227. $input->SetTotal_fee($totalFee * 100);
  228. $input->SetTime_start(date("YmdHis", $now));
  229. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  230. $input->SetTime_expire(date("YmdHis", $expireTime));
  231. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  232. $input->SetTrade_type("JSAPI");
  233. //花卉宝代为申请的微信支付
  234. $merchantExtend = $this->sjExtend;
  235. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  236. $input->SetSub_openid($openId);
  237. } else {
  238. $input->SetOpenid($openId);
  239. }
  240. //小程序使用miniAppId
  241. if (httpUtil::isMiniProgram()) {
  242. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  243. }
  244. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  245. $tools = new \JsApiPay();
  246. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  247. $newParams = json_decode($jsApiParameters, true);
  248. //已请求微信不能修改价格
  249. $updateData = [];
  250. $updateData['modPrice'] = 0;
  251. if (empty($order['deadline'])) {
  252. $updateData['deadline'] = $expireTime;
  253. }
  254. OrderService::updateById($orderId, $updateData);
  255. util::success($newParams);
  256. }
  257. //快捷付款订单
  258. public function actionFastPay()
  259. {
  260. ini_set('date.timezone', 'Asia/Shanghai');
  261. $post = Yii::$app->request->post();
  262. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  263. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  264. //验证门店是否有效
  265. $shopInfo = ShopService::getById($shopId);
  266. ShopService::valid($shopInfo, $this->sjId);
  267. $post['shopId'] = $shopId;
  268. //默认门店订单
  269. $store = isset($post['store']) ? $post['store'] : 1;
  270. $post['store'] = $store;
  271. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  272. $sourceType = $this->isWx ? 0 : 1;
  273. if (httpUtil::isMiniProgram()) {
  274. $sourceType = 2;
  275. }
  276. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  277. //兼容旧系统sourceType=3表示朋友圈来源
  278. $fromType = $sourceType == 3 ? 2 : 0;
  279. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  280. $post['userId'] = $this->adminId;
  281. $custom = $this->custom;
  282. $post['bookName'] = $custom['userName'] ?? '';
  283. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  284. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  285. $post['bookMobile'] = $bookMobile;
  286. $prePrice = round($post['prePrice'], 2);
  287. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  288. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  289. $actPrice = $discountData['price'];
  290. $post['discountType'] = $discountData['discountType'];
  291. $post['discountAmount'] = $discountData['discountAmount'];
  292. $post['actPrice'] = $actPrice;
  293. $post['payStyle'] = 0;
  294. $post['merchantId'] = $this->sjId;
  295. $now = time();
  296. $expireTime = $now + 300;//订单5分钟后过期
  297. $post['createTime'] = date("Y-m-d H:i:s", $now);
  298. $post['deadline'] = $expireTime;
  299. if ($actPrice <= 0) {
  300. util::fail('请填写正确的金额');
  301. }
  302. //微信支付订单提交不能修改价格
  303. $post['modPrice'] = $this->isWx ? 0 : 1;
  304. $post['sourceType'] = $sourceType;
  305. $post['goodsNum'] = 1;
  306. $post['fromType'] = $fromType;
  307. $order = OrderService::addOrder($post);
  308. $orderId = $order['id'];
  309. $orderSn = $order['orderSn'];
  310. $merchantId = $this->sjId;
  311. if ($payWay == 0) {
  312. $orderId = $order['id'];
  313. $name = '购买商品';
  314. $totalFee = $actPrice;
  315. $user = UserService::getById($this->adminId);
  316. $openId = isset($user['openId']) ? $user['openId'] : 0;
  317. if (httpUtil::isMiniProgram()) {
  318. //小程序使用miniOpenId
  319. $openId = $user['miniOpenId'];
  320. }
  321. if (empty($openId)) {
  322. util::fail('没有找到客户的openId');
  323. }
  324. $typeList = configDict::getConfig('capitalType');
  325. $capitalType = $typeList['xhOrder']['id'];
  326. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  327. $wxPayType = 0;
  328. if (httpUtil::isMiniProgram()) {
  329. $wxPayType = 1;
  330. }
  331. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  332. $wx = Yii::getAlias("@vendor/weixin");
  333. require_once($wx . '/lib/WxPay.Api.php');
  334. require_once($wx . '/example/WxPay.JsApiPay.php');
  335. $input = new \WxPayUnifiedOrder();
  336. $input->SetBody($name);
  337. $input->SetOut_trade_no($orderSn);
  338. $input->SetTotal_fee($totalFee * 100);
  339. $input->SetTime_start(date("YmdHis", $now));
  340. $input->SetAttach($attach);
  341. //设置订单有效期5分钟
  342. $input->SetTime_expire(date("YmdHis", $expireTime));
  343. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  344. $input->SetTrade_type("JSAPI");
  345. //花卉宝代为申请的微信支付
  346. $merchantExtend = $this->sjExtend;
  347. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  348. $input->SetSub_openid($openId);
  349. } else {
  350. $input->SetOpenid($openId);
  351. }
  352. //小程序使用miniAppId
  353. if (httpUtil::isMiniProgram()) {
  354. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  355. }
  356. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  357. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  358. $tools = new \JsApiPay();
  359. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  360. $newParams = json_decode($jsApiParameters, true);
  361. $newParams['orderId'] = $orderId;
  362. util::success($newParams);
  363. } elseif ($payWay == 1) {
  364. $extend = MerchantExtendService::getByMerchantId($merchantId);
  365. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  366. util::fail('支付宝付款即将开通...');
  367. }
  368. $config = [
  369. //应用ID,您的APPID。
  370. 'app_id' => $extend['alipayPId'],
  371. //商户私钥,您的原始格式RSA私钥
  372. 'merchant_private_key' => $extend['alipayKey'],
  373. //异步通知地址
  374. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/alipay-callback",
  375. //同步跳转
  376. 'return_url' => "",
  377. //编码格式
  378. 'charset' => "UTF-8",
  379. //签名方式
  380. 'sign_type' => "RSA2",
  381. //支付宝网关
  382. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  383. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  384. 'alipay_public_key' => $extend['alipayPublicKey'],
  385. ];
  386. Yii::info(json_encode($config) . ' aplipay config');
  387. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  388. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  389. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  390. $totalFee = $order['actPrice'];
  391. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  392. $subject = '购买商品';//订单名称,必填
  393. $total_amount = $totalFee;//付款金额,必填
  394. $body = '';//商品描述,可空
  395. $timeout_express = "1m";//超时时间
  396. $typeList = configDict::getConfig('capitalType');
  397. $capitalType = $typeList['xhOrder']['id'];
  398. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  399. $passbackParams = urlencode($passbackParams);
  400. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  401. $payRequestBuilder->setBody($body);
  402. $payRequestBuilder->setSubject($subject);
  403. $payRequestBuilder->setOutTradeNo($out_trade_no);
  404. $payRequestBuilder->setTotalAmount($total_amount);
  405. $payRequestBuilder->setTimeExpress($timeout_express);
  406. //在异步通知时将该参数原样返回
  407. $payRequestBuilder->setPassbackParams($passbackParams);
  408. //同步通知
  409. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$merchantId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  410. //异步通知
  411. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/alipay-callback";
  412. $payResponse = new \AlipayTradeService($config);
  413. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  414. //直接将支付宝的html返回给前端
  415. echo $result;
  416. } elseif ($payWay == 2) {
  417. //余额支付,验证支付密码是否正确
  418. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  419. $user = UserService::getUserInfo($this->adminId, false);
  420. UserService::validPayPassword($payPassword, $user);
  421. $typeList = configDict::getConfig('capitalType');
  422. $capitalType = $typeList['xhOrder']['id'];
  423. $callbackParams = [];
  424. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  425. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  426. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  427. } else {
  428. util::fail('无效的支付方式');
  429. }
  430. }
  431. //订单评价
  432. public function actionComment()
  433. {
  434. $post = Yii::$app->request->post();
  435. $id = $post['id'];
  436. $order = OrderService::getById($id);
  437. OrderService::valid($order, $this->sjId);
  438. if ($order['grade'] > 0) {
  439. util::fail('您已经评过了');
  440. }
  441. OrderService::comment($post);
  442. util::complete('提交成功');
  443. }
  444. //订单详情 shish 2019.12.16
  445. public function actionDetail()
  446. {
  447. $id = Yii::$app->request->get('id', 0);
  448. $orderSn = Yii::$app->request->get('orderSn', '');
  449. $detail = [];
  450. if (!empty($id)) {
  451. $detail = OrderService::getOrderById($id);
  452. }
  453. if (!empty($orderSn)) {
  454. $detail = OrderService::getOrderBySn($orderSn);
  455. }
  456. OrderService::valid($detail, $this->sjId);
  457. util::success($detail);
  458. }
  459. public function actionList()
  460. {
  461. $get = Yii::$app->request->get();
  462. $search = isset($get['search']) ? $get['search'] : '';
  463. $where = [];
  464. $where['merchantId'] = $this->sjId;
  465. if (isset($get['status']) && $get['status'] != -1) {
  466. $where['status'] = $get['status'];
  467. }
  468. if (!empty($search)) {
  469. if (stringUtil::isMobile($search)) {
  470. $where['receiveMobile'] = $search;
  471. } else {
  472. $where['id'] = $search;
  473. }
  474. }
  475. $list = OrderService::getOrderList($where);
  476. $list['asset'] = MerchantAssetService::getByMerchantId($this->sjId);
  477. util::success($list);
  478. }
  479. //订单归类 shish 2019.12.17
  480. public function actionClassify()
  481. {
  482. $post = Yii::$app->request->post();
  483. $id = isset($post['id']) ? $post['id'] : 0;
  484. $order = OrderService::getById($id);
  485. OrderService::valid($order, $this->sjId);
  486. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  487. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  488. OrderService::classify($id, $categoryId, $usageId);
  489. util::complete();
  490. }
  491. //更新配送单 shish 2019.12.17
  492. public function actionUpdateSheet()
  493. {
  494. $post = Yii::$app->request->post();
  495. $id = isset($post['id']) ? $post['id'] : 0;
  496. unset($post['id']);
  497. $order = OrderService::getById($id);
  498. OrderService::valid($order, $this->sjId);
  499. OrderService::updateSheet($order, $post);
  500. util::complete('提交成功');
  501. }
  502. //商家收款与发货 shish 2019.12.18
  503. public function actionGathering()
  504. {
  505. $post = Yii::$app->request->post();
  506. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  507. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  508. $userId = isset($post['userId']) ? $post['userId'] : 0;
  509. $userInfo = UserService::getById($userId);
  510. if (empty($userInfo) || $userInfo['merchantId'] != $this->sjId) {
  511. util::fail('您选择的客户无效');
  512. }
  513. if ($price <= 0) {
  514. util::fail('请输入金额');
  515. }
  516. $post['prePrice'] = $price;
  517. $post['actPrice'] = $price;
  518. $post['payWay'] = $payWay;
  519. $post['sourceType'] = 1;
  520. $post['userId'] = $userId;
  521. $post['goodsNum'] = 1;
  522. $post['orderName'] = '商品';
  523. $shopId = MerchantService::getDefaultShopId($this->sj);
  524. $post['shopId'] = $shopId;
  525. $order = OrderService::addOrder($post);
  526. $id = $order['id'];
  527. $orderSn = $order['orderSn'];
  528. //流水类型列表
  529. $capitalTypeList = configDict::getConfig('capitalType');
  530. $capitalType = $capitalTypeList['xhOrder']['id'];
  531. $callbackParams = [];
  532. switch ($payWay) {
  533. case 0:
  534. xhPayToolService::weixinPay($callbackParams, $orderSn, $price, $capitalType, 0);
  535. break;
  536. case 1:
  537. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  538. break;
  539. case 2:
  540. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  541. break;
  542. default:
  543. util::fail('无效支付方式');
  544. }
  545. util::success($order);
  546. }
  547. //免发货管理 shish 2020.1.6
  548. public function actionWithoutSend()
  549. {
  550. $id = Yii::$app->request->get('id');
  551. $order = OrderService::getById($id);
  552. OrderService::valid($order, $this->sjId);
  553. //配送流程变更
  554. $currentFlow = OrderSendClass::withoutSend($order);
  555. util::success(['currentFlow' => $currentFlow]);
  556. }
  557. //修改价格 shish 2020.1.6
  558. public function actionUpdatePrice()
  559. {
  560. $id = Yii::$app->request->get('id');
  561. $price = Yii::$app->request->get('price', 1);
  562. $order = OrderService::getById($id);
  563. OrderService::valid($order, $this->sjId);
  564. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  565. util::fail('已发起支付,不能修改价格');
  566. }
  567. OrderService::updateById($id, ['actPrice' => $price]);
  568. util::complete('修改成功');
  569. }
  570. //配送单 shish 2020.2.29
  571. public function actionGetDeliverDetail()
  572. {
  573. $id = Yii::$app->request->get('id', 0);
  574. $order = OrderService::getById($id);
  575. OrderService::valid($order, $this->sjId);
  576. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  577. $reachPeriodId = $order['reachPeriod'];
  578. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  579. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  580. $shopId = $order['shopId'];
  581. $telephone = '';
  582. if (!empty($shopId)) {
  583. $shop = ShopService::getById($shopId);
  584. }
  585. $telephone = isset($shop['telephone']) ? $shop['telephone'] : '';
  586. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  587. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  588. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  589. $bookName = '--';
  590. $bookMobile = '--';
  591. }
  592. $data = [
  593. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  594. 'orderSn' => $order['orderSn'],
  595. 'receiveUserName' => $order['receiveUserName'],
  596. 'receiveMobile' => $order['receiveMobile'],
  597. 'receiveFullAddress' => $order['receiveAddress'] . $order['receiveFloor'] . "(" . $order['receiveFullAddress'] . ")",
  598. 'cardInfo' => $order['cardInfo'],
  599. 'bookMobile' => $bookMobile,
  600. 'bookName' => $bookName,
  601. 'remark' => $order['remark'],
  602. 'sendNum' => $order['sendNum'],
  603. 'telephone' => 13600903070,
  604. 'printTime' => '',
  605. 'img' => '',
  606. ];
  607. util::success($data);
  608. }
  609. }