OrderController.php 27 KB

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