OrderController.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. <?php
  2. namespace mall\controllers;
  3. use biz\product\classes\ProductClass;
  4. use bizHd\custom\classes\HdClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizMall\custom\classes\CustomClass;
  7. use bizMall\goods\classes\GoodsClass;
  8. use bizMall\goods\classes\GoodsSettingClass;
  9. use bizMall\item\classes\ItemClass;
  10. use bizMall\order\classes\OrderClass;
  11. use bizMall\order\classes\OrderForwardClass;
  12. use bizMall\order\classes\OrderItemClass;
  13. use bizMall\order\services\OrderService;
  14. use bizMall\promote\services\CouponService;
  15. use bizMall\saas\services\RegionService;
  16. use bizMall\shop\classes\MainClass;
  17. use bizMall\shop\classes\ShopExtClass;
  18. use bizMall\user\services\UserService;
  19. use common\components\dateUtil;
  20. use common\components\dict;
  21. use common\components\imgUtil;
  22. use common\components\noticeUtil;
  23. use common\components\orderSn;
  24. use common\components\stringUtil;
  25. use common\services\xhPayToolService;
  26. use Yii;
  27. use common\components\util;
  28. use common\components\lakala\Lakala;
  29. use common\components\IntraCityExpress;
  30. class OrderController extends BaseController
  31. {
  32. //二维码收款使用
  33. public $guestAccess = ['order-relate', 'fast-pay'];
  34. //记账单列表 ssh 20250627
  35. public function actionDebtList()
  36. {
  37. $get = Yii::$app->request->get();
  38. $id = $get['id'] ?? 0;
  39. $where = [];
  40. $where['userId'] = $this->userId;
  41. if (!empty($id)) {
  42. $hd = \bizMall\hd\classes\HdClass::getById($id, true);
  43. if (empty($hd)) {
  44. util::fail('无效的花店');
  45. }
  46. $where['hdId'] = $id;
  47. }
  48. $where['debt'] = 1;
  49. $searchTime = $get['searchTime'] ?? '';
  50. if (!empty($searchTime)) {
  51. $startTime = $get['startTime'] ?? '';
  52. $endTime = $get['endTime'] ?? '';
  53. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  54. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  55. }
  56. $respond = OrderClass::getDebtList($where);
  57. util::success($respond);
  58. }
  59. //新计算运费
  60. public function actionNewFreight()
  61. {
  62. $post = Yii::$app->request->post();
  63. $hasMap = dict::getDict('hasMap');
  64. if ($hasMap == 0) {
  65. util::fail('暂不支持跑腿功能');
  66. }
  67. $mainData = $this->main;
  68. $wxStoreId = $mainData->wxStoreId ?? '';
  69. if (empty($wxStoreId)) {
  70. util::fail('商家还没有开通跑腿');
  71. }
  72. $cargoName = $post['cargo_name']; // 商品名称
  73. $cargo = [
  74. 'cargo_name' => $cargoName, // 商品名称
  75. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  76. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  77. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  78. 'cargo_num' => intval($post['package_num'])
  79. ];
  80. $originalLng = doubleval($post['user_lng']);
  81. $originalLat = doubleval($post['user_lat']);
  82. $orderData = [
  83. 'wx_store_id' => $wxStoreId,
  84. 'user_name' => $post['user_name'],
  85. 'user_phone' => $post['user_phone'],
  86. 'user_lng' => $originalLng,
  87. 'user_lat' => $originalLat,
  88. 'user_address' => $post['user_address'],
  89. 'cargo' => $cargo,
  90. ];
  91. $result = IntraCityExpress::preAddOrder($orderData);
  92. if ($result['errcode'] === 0) {
  93. util::success($result, "查询成功");
  94. } else {
  95. Yii::error("运费查询失败:" . json_encode($result));
  96. util::fail('运费查询失败' . json_encode($result));
  97. }
  98. }
  99. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  100. public function actionFreight()
  101. {
  102. $get = Yii::$app->request->get();
  103. $shop = $this->shop;
  104. $shopLat = isset($shop->lat) ? $shop->lat : '';
  105. $shopLong = isset($shop->long) ? $shop->long : '';
  106. // 判断地图功能是否正常。0 表示地图定位不可用,全部经纬度数据失效
  107. if (dict::getDict('hasMap') == 0) {
  108. $shopLat = '';
  109. $shopLong = '';
  110. }
  111. if (empty($shopLat) || empty($shopLong)) {
  112. util::success([
  113. 'distance' => 0,
  114. 'showDistance' => 0,
  115. 'fee' => 0,
  116. 'hasMap' => dict::getDict('hasMap'),
  117. 'shop' => 'fail' //门店引起的失败
  118. ]);
  119. }
  120. $userLat = $get['lat'] ?? '';
  121. $userLong = $get['long'] ?? '';
  122. if (empty($userLat) || empty($userLong)) {
  123. util::success([
  124. 'distance' => 0,
  125. 'showDistance' => 0,
  126. 'fee' => 0,
  127. 'hasMap' => dict::getDict('hasMap'),
  128. 'client' => 'fail' //用户端定位失败
  129. ]);
  130. }
  131. $weight = $get['weight'] ?? 0;
  132. if (empty($weight)) {
  133. util::success([
  134. 'distance' => 0,
  135. 'showDistance' => 0,
  136. 'fee' => 0,
  137. 'hasMap' => dict::getDict('hasMap'),
  138. 'weight' => 'fail' //重量不对
  139. ]);
  140. }
  141. //data: {distance: "321", showDistance: "0.3", fee: "12.00"}
  142. $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
  143. util::success($respond);
  144. }
  145. //购买花材 ssh 20220511
  146. public function actionBuyItem()
  147. {
  148. $post = Yii::$app->request->post();
  149. $post['sjId'] = $this->sjId;
  150. $post['shopId'] = $this->shopId;
  151. $post['payWay'] = $this->isWx == true ? 0 : 1;
  152. $post['mainId'] = $this->mainId ?? 0;
  153. $post['userId'] = $this->userId ?? 0;
  154. //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
  155. $mainId = $this->mainId;
  156. if (getenv('YII_ENV') == 'production') {
  157. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  158. util::fail('暂时无法访问');
  159. }
  160. } else {
  161. if (in_array($mainId, [0, 1])) {
  162. util::fail('暂时无法访问');
  163. }
  164. }
  165. $hdId = $post['hdId'] ?? 0;
  166. $hd = HdClass::getById($hdId, true);
  167. if (empty($hd)) {
  168. util::fail('没有找到花店');
  169. }
  170. if ($hd->shopId != $this->shopId) {
  171. util::fail('不是你的花店');
  172. }
  173. $post['hdName'] = $hd->name ?? '';
  174. $customId = $hd->customId ?? 0;
  175. $custom = CustomClass::getById($customId, true);
  176. if (empty($custom)) {
  177. util::fail('没有找到客户');
  178. }
  179. $customName = $custom->name ?? '';
  180. $post['customId'] = $customId;
  181. $post['customName'] = $customName;
  182. $post['customNamePy'] = stringUtil::py($customName);
  183. $now = time();
  184. //订单30分钟后过期
  185. $expireTime = $now + 1800;
  186. $post['deadline'] = $expireTime;
  187. //商城
  188. $post['fromType'] = dict::getDict('fromType', 'mall');
  189. $productJson = $post['product'] ?? '';
  190. if (empty($productJson) && empty($groupId)) {
  191. util::fail('请选择商品');
  192. }
  193. $productList = json_decode($productJson, true);
  194. if (empty($productList)) {
  195. util::fail('请选择商品');
  196. }
  197. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  198. $productInfoList = productClass::getByIds($ids, null, 'id');
  199. if (!empty($productInfoList)) {
  200. $presellData = [];
  201. $nowTime = strtotime(date("Y-m-d"));
  202. foreach ($productInfoList as $currentInfo) {
  203. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $this->mainId) {
  204. util::fail('只能选择同一家的商品哦');
  205. }
  206. $currentName = $currentInfo['name'] ?? '';
  207. $presell = $currentInfo['presell'] ?? 0;
  208. $presellData[] = $presell;
  209. if ($presell == 1) {
  210. if (empty($post['reachDate'])) {
  211. util::fail('请选择配送或取货日期');
  212. }
  213. $sendTimeWant = $post['reachDate'];
  214. if (strtotime($sendTimeWant) < $nowTime) {
  215. util::fail('不能选择过去时间');
  216. }
  217. $hasDateStr = $currentInfo['presellDate'] ?? [];
  218. $hasDate = explode(',', trim($hasDateStr));
  219. if (empty($hasDate)) {
  220. util::fail('预售日期有问题,请提醒门店');
  221. }
  222. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  223. util::fail("有预售花材在{$sendTimeWant}没到货");
  224. }
  225. $post['presell'] = 1;
  226. }
  227. }
  228. if (count($productInfoList) != count($ids)) {
  229. util::fail('存在无效商品');
  230. }
  231. $presellData = array_unique($presellData);
  232. if (count($presellData) > 1) {
  233. util::fail('预售和非预售花材不能一起下单');
  234. }
  235. } else {
  236. util::fail('花材不存在');
  237. }
  238. $connection = Yii::$app->db;
  239. $transaction = $connection->beginTransaction();
  240. try {
  241. $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME');
  242. $post['deadline'] = time() + $orderValidTime;
  243. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  244. $post['needPrint'] = dict::getDict('needPrint', 'need');
  245. $user = $this->user;
  246. $post['bookMobile'] = $user->mobile ?? '';
  247. $post['bookName'] = $user->name ?? '';
  248. $hasPay = 0;
  249. $modifyPrice = 0;
  250. //各个等级对应的price addPrice字段
  251. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  252. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  253. $totalWeight = 0;
  254. foreach ($productList as $eleKey => $element) {
  255. $level = 0;
  256. $productId = $element['productId'];
  257. $current = $productInfoList[$productId];
  258. if (empty($current)) {
  259. util::fail('有商品信息没有找到');
  260. }
  261. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($current, $level, $priceMap, $addPriceMap);
  262. $num = $element['num'] ?? 0;
  263. $currentTotal = bcmul($price, $num, 2);
  264. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  265. $productList[$eleKey]['unitPrice'] = $price;
  266. $weight = $current['weight'] ?? 0;
  267. $currentWeight = bcmul($num, $weight, 2);
  268. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  269. }
  270. $post['product'] = $productList;
  271. //不能前端传运费过来,只能通过跑腿接口计算出来
  272. unset($post['sendCost']);
  273. if (isset($post['sendType']) && $post['sendType'] == 2) {
  274. $hasMap = dict::getDict('hasMap');
  275. if ($hasMap == 0) {
  276. util::fail('不能使用跑腿');
  277. }
  278. $cargoName = '花材'; // 商品名称
  279. $cargo = [
  280. 'cargo_name' => $cargoName, // 商品名称
  281. 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  282. 'cargo_price' => intval($modifyPrice * 100), // 单位:分 -- 把元转换为分
  283. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  284. 'cargo_num' => 1
  285. ];
  286. $customName = $custom->name;
  287. $customMobile = $custom->mobile;
  288. if (empty($post['long']) || empty($post['lat'])) {
  289. util::fail('位置错误');
  290. }
  291. $originalLng = doubleval($post['long']);
  292. $originalLat = doubleval($post['lat']);
  293. $city = $post['city'] ?? '';
  294. $dist = $post['dist'] ?? '';
  295. $address = $post['address'] ?? '';
  296. $showAddress = $post['showAddress'] ?? '';
  297. $userAddress = $city . $dist . $address . "({$showAddress})";
  298. $main = $this->main;
  299. $wxStoreId = $main->wxStoreId ?? 0;
  300. if (empty($wxStoreId)) {
  301. util::fail('商家还没有开通跑腿功能');
  302. }
  303. $orderData = [
  304. 'wx_store_id' => $wxStoreId,
  305. 'user_name' => $customName,
  306. 'user_phone' => $customMobile,
  307. 'user_lng' => $originalLng,
  308. 'user_lat' => $originalLat,
  309. 'user_address' => $userAddress,
  310. 'cargo' => $cargo,
  311. ];
  312. $result = IntraCityExpress::preAddOrder($orderData);
  313. $errCode = $result['errcode'] ?? 1;
  314. if ($errCode == 0) {
  315. $errMsg = $result['errmsg'] ?? '';
  316. util::fail('获取运费错误:' . $errMsg);
  317. }
  318. $fee = $result['est_fee'] ?? 0;
  319. //距离,好像是米
  320. $distance = $result['distance'] ?? 0;
  321. $sendCost = bcdiv($fee, 100, 2);
  322. $post['sendCost'] = $sendCost;
  323. noticeUtil::push("运费:{$sendCost} 重量:{$totalWeight} 距离:{$distance} {$customName} {$customMobile} 经纬 {$originalLng} {$originalLat}", '15280215347');
  324. }
  325. $post['modifyPrice'] = $modifyPrice;
  326. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  327. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  328. if (isset($return->shopId)) {
  329. if ($return->shopId == 7610) {
  330. if ($return->actPrice < 200) {
  331. if ($return->sendType == 0) {
  332. util::fail('满200元支持送货上门');
  333. }
  334. }
  335. }
  336. }
  337. if ($return->sendType == 2) {
  338. if (in_array($return->shopId, [2, 726, 1490, 13626, 7856, 7688])) {
  339. util::fail('暂时不能使用跑腿');
  340. }
  341. }
  342. $transaction->commit();
  343. $orderSn = $return->orderSn ?? '';
  344. $orderPrice = $return->orderPrice ?? 0;
  345. $id = $return->id ?? 0;
  346. $getPayType = dict::getDict('getPayType');
  347. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
  348. } catch (\Exception $e) {
  349. $transaction->rollBack();
  350. Yii::error("失败原因:" . $e->getMessage());
  351. util::fail('下单失败');
  352. }
  353. }
  354. //商城下单操作 ssh 2019.12.3
  355. public function actionCreateOrder()
  356. {
  357. $post = Yii::$app->request->post();
  358. $goodsId = $post['goodsId'] ?? 0;
  359. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  360. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  361. if (empty($goodsId)) {
  362. util::fail('请选择商品');
  363. }
  364. $goodsInfo = GoodsClass::getById($goodsId, true);
  365. if (empty($goodsInfo)) {
  366. util::fail('没有找到商品');
  367. }
  368. $stock = $goodsInfo->stock ?? 0;
  369. if ($stock <= 0) {
  370. util::fail('库存不足');
  371. }
  372. //事务处理
  373. $connection = Yii::$app->db;
  374. $transaction = $connection->beginTransaction();
  375. try {
  376. $custom = $this->custom;
  377. if (empty($custom)) {
  378. util::fail('个人信息缺失');
  379. }
  380. $customId = $custom->id ?? 0;
  381. $hd = $this->hd;
  382. $post['customId'] = $customId;
  383. $customName = $custom->name ?? '';
  384. $post['customName'] = $customName;
  385. $post['customNamePy'] = stringUtil::py($customName);
  386. $post['hdId'] = $hd->id ?? 0;
  387. $post['hdName'] = $hd->name ?? '';
  388. $user = $this->user;
  389. $post['bookName'] = $user->name ?? '';
  390. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  391. $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
  392. $post['bookMobile'] = $bookMobile;
  393. $post['payWay'] = 0;
  394. //计算运费
  395. //$sendDistance = 0;
  396. //$sendCost = 0;
  397. $shop = $this->shop;
  398. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  399. $lat = $post['lat'] ?? '';
  400. $lng = $post['long'] ?? '';
  401. if ((empty($lat) || empty($lng)) && dict::getDict('hasMap') == 1) { // 当 hasMap 为1时,经纬数据是必要的
  402. util::fail('缺少经纬数据');
  403. }
  404. $address = $post['address'] ?? '';
  405. $floor = $post['floor'] ?? '';
  406. $fullAddress = $address . $floor;
  407. $post['fullAddress'] = $fullAddress;
  408. $shopLat = $shop->lat ?? '';
  409. $shopLong = $shop->long ?? '';
  410. if (empty($shopLat) || empty($shopLong)) {
  411. //util::fail('请完成门店地址');
  412. }
  413. //花束重量默认1.5
  414. $weight = 1.5;
  415. //$disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
  416. //$sendCost = $disRespond['fee'] ?? 0;
  417. //$sendDistance = $disRespond['distance'] ?? 0;
  418. }
  419. $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
  420. $sendCost = isset($post['sendCost']) ? floatval($post['sendCost']) : 0;
  421. $post['sendCost'] = $sendCost;
  422. $post['sjId'] = $this->sjId;
  423. $post['shopId'] = $this->shopId;
  424. $mainId = $this->mainId;
  425. $post['mainId'] = $mainId;
  426. $post['userId'] = $this->userId;
  427. //计算总金额
  428. $unitPrice = $goodsInfo->price ?? 0;
  429. if ($unitPrice <= 0) {
  430. util::fail('没有价格,无法下单');
  431. }
  432. $rise = GoodsSettingClass::getRise($mainId);
  433. //根据涨价设置进行自动涨价
  434. if (!empty($rise)) {
  435. $price = $unitPrice;
  436. if ($rise['riseType'] == 0) {
  437. //百分比
  438. $newPrice = $price + sprintf("%.1f", ($price * $rise['riseAmount'] / 100));
  439. } else {
  440. //金额
  441. $newPrice = $price + $rise['riseAmount'];
  442. }
  443. $unitPrice = $newPrice;
  444. }
  445. $goodsPrice = $unitPrice * $goodsNum;
  446. $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  447. //非门店订单
  448. $post['store'] = 0;
  449. $sourceType = 2;
  450. $couponId = !empty($post['couponId']) ? $post['couponId'] : 0;
  451. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  452. $actPrice = $discountData['price'];
  453. $post['modifyPrice'] = $actPrice;
  454. $post['discountType'] = $discountData['discountType'];
  455. $post['discountAmount'] = $discountData['discountAmount'];
  456. //非自取订单考虑配送时间
  457. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  458. $sendType = $post['sendType'] ?? 0;
  459. if ($sendType != 1) {
  460. if (empty($post['reachPeriod'])) {
  461. util::fail('请选择配送时间哈');
  462. }
  463. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  464. }
  465. $now = time();
  466. $expireTime = $now + 1800;//订单30分钟后过期
  467. $post['deadline'] = $expireTime;
  468. $orderSn = orderSn::getOrderSn();
  469. $post['orderSn'] = $orderSn;
  470. $post['fromType'] = dict::getDict('fromType', 'mall');
  471. $post['prePrice'] = $prePrice;
  472. $post['orderPrice'] = $actPrice;
  473. $post['actPrice'] = $actPrice;
  474. $post['realPrice'] = $actPrice;
  475. $post['mainPay'] = $actPrice;
  476. $post['cash'] = 0;
  477. $post['mainId'] = $mainId;
  478. $post['needPrint'] = dict::getDict('needPrint', 'need');
  479. $hasPay = 0;
  480. $product = [['productId' => $goodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
  481. $post['product'] = $product;
  482. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  483. $orderId = $return->id;
  484. $orderSn = $return->orderSn;
  485. $transaction->commit();
  486. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'id' => $orderId]);
  487. } catch (Exception $e) {
  488. $transaction->rollBack();
  489. Yii::info("失败原因:" . $e->getMessage());
  490. util::fail('下单失败');
  491. }
  492. }
  493. //下单要用到的相关信息 ssh 2019.12.6
  494. public function actionOrderRelate()
  495. {
  496. $regionTree = RegionService::tree();
  497. //门店名称
  498. $shop = !empty($this->shop) ? $this->shop->attributes : [];
  499. $sjName = $shop['merchantName'] ?? '';
  500. $shopName = $shop['shopName'] ?? '';
  501. $shop['showName'] = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  502. $avatar = $shop['avatar'] ?? '';
  503. $smallAvatar = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  504. $shop['smallAvatar'] = $smallAvatar;
  505. $freight = ['first' => 5, 'add' => 2];
  506. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  507. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  508. util::success($out);
  509. }
  510. //余额支付 ssh 20250410
  511. public function actionBalancePay()
  512. {
  513. $post = Yii::$app->request->post();
  514. $orderSn = $post['orderSn'] ?? '';
  515. //避免重复提交
  516. util::checkRepeatCommit($orderSn, 10);
  517. $connection = Yii::$app->db;
  518. $transaction = $connection->beginTransaction();
  519. try {
  520. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  521. if (empty($order)) {
  522. util::fail('没有找到订单');
  523. }
  524. if ($order->shopId != $this->shopId) {
  525. util::fail('不是你的订单');
  526. }
  527. $payWay = dict::getDict('payWay', 'balancePay');
  528. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  529. $transaction->commit();
  530. util::complete('支付成功');
  531. } catch (\Exception $e) {
  532. $transaction->rollBack();
  533. Yii::error("支付原因:" . $e->getMessage());
  534. util::fail('支付失败');
  535. }
  536. }
  537. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  538. public function actionWxPay()
  539. {
  540. ini_set('date.timezone', 'Asia/Shanghai');
  541. $post = Yii::$app->request->post();
  542. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  543. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  544. $order = OrderService::getByOrderSn($orderSn);
  545. $orderId = $order['id'];
  546. //验证优惠券是否还有效
  547. if (!empty($couponId)) {
  548. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  549. }
  550. //支付前验证订单有效性
  551. OrderService::checkBeforePay($order);
  552. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  553. $shop = $this->shop;
  554. if (empty($shop)) {
  555. util::fail('没有找到门店信息');
  556. }
  557. if (isset($shop->shopName) && !empty($shop->shopName)) {
  558. $name .= '(' . $shop->shopName . ')';
  559. }
  560. $totalFee = $order['mainPay'];
  561. $openId = '';
  562. if (!empty($post['miniOpenId'])) {
  563. $openId = $post['miniOpenId'];
  564. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  565. }
  566. if (empty($openId)) {
  567. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  568. $openId = $this->user['miniOpenId'];
  569. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  570. }
  571. }
  572. if (empty($openId)) {
  573. util::fail('没有找到openId');
  574. }
  575. $now = time();
  576. $expireTime = $now + 1800;
  577. $updateData = [];
  578. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  579. $oldOrderSn = $orderSn;
  580. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  581. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  582. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  583. $params = [
  584. 'appid' => 'OP00002119',
  585. 'serial_no' => '018b08cfddbd',
  586. 'merchant_no' => $shop->lklSjNo,
  587. 'term_no' => $shop->lklScanTermNo,
  588. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  589. 'lklCertificatePath' => $lklCertificatePath,
  590. ];
  591. $laResource = new Lakala($params);
  592. $closeParams = [
  593. 'orderSn' => $oldOrderSn,
  594. ];
  595. $response = $laResource->close($closeParams);
  596. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  597. $msg = $response['msg'] ?? '';
  598. $lklSjNo = $shop->lklSjNo ?? '';
  599. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  600. util::fail('出错了哦,请重新下一单');
  601. }
  602. $orderSn = orderSn::getOrderSn();
  603. $updateData['orderSn'] = $orderSn;
  604. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  605. } else {
  606. //已请求的不能再修改价格
  607. $updateData['modPrice'] = 0;
  608. }
  609. if (empty($order['deadline'])) {
  610. $updateData['deadline'] = $expireTime;
  611. }
  612. if (!empty($updateData)) {
  613. OrderService::updateById($orderId, $updateData);
  614. }
  615. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  616. $sjExtend = WxOpenClass::getMallWxInfo();
  617. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  618. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  619. $params = [
  620. 'appid' => 'OP00002119',
  621. 'serial_no' => '018b08cfddbd',
  622. 'merchant_no' => $shop->lklSjNo,
  623. 'term_no' => $shop->lklScanTermNo,
  624. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  625. 'lklCertificatePath' => $lklCertificatePath,
  626. ];
  627. $laResource = new Lakala($params);
  628. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  629. $wxParams = [
  630. 'orderSn' => $orderSn,
  631. 'amount' => $totalFee,
  632. 'capitalType' => $capitalType,
  633. 'notifyUrl' => $notifyUrl,
  634. 'wxAppId' => $sjExtend['miniAppId'],
  635. 'openId' => $openId,
  636. 'subject' => $name,
  637. 'couponId' => $couponId,
  638. ];
  639. $response = $laResource->driveWxPay($wxParams);
  640. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  641. $errMsg = $response['msg'] ?? '';
  642. noticeUtil::push($errMsg, '15280215347');
  643. util::fail('支付失败');
  644. }
  645. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  646. $appId = $newParams['app_id'] ?? '';
  647. $nonceStr = $newParams['nonce_str'] ?? '';
  648. $paySign = $newParams['pay_sign'] ?? '';
  649. $package = $newParams['package'] ?? '';
  650. $signType = $newParams['sign_type'] ?? '';
  651. $timeStamp = $newParams['time_stamp'] ?? '';
  652. $new = [
  653. 'id' => $orderId,
  654. 'appId' => $appId,
  655. 'nonceStr' => $nonceStr,
  656. 'paySign' => $paySign,
  657. 'package' => $package,
  658. 'signType' => $signType,
  659. 'timeStamp' => $timeStamp,
  660. 'orderSn' => $orderSn,
  661. ];
  662. util::success($new);
  663. }
  664. //快捷付款订单
  665. public function actionFastPay()
  666. {
  667. ini_set('date.timezone', 'Asia/Shanghai');
  668. $post = Yii::$app->request->post();
  669. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  670. $shopId = $this->shopId;
  671. $shop = $this->shop;
  672. if (empty($shop)) {
  673. util::fail('没有门店信息');
  674. }
  675. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  676. $gatheringItem = !empty($ext) ? $ext->gatheringItem : 0;
  677. if (empty($gatheringItem)) {
  678. util::fail('请设置付款用的商品');
  679. }
  680. $gatheringItemInfo = ItemClass::getById($gatheringItem, true);
  681. if (empty($gatheringItemInfo)) {
  682. util::fail('付款用的商品没有设置');
  683. }
  684. $ptItemId = $gatheringItemInfo->itemId ?? 0;
  685. $classId = $gatheringItemInfo->classId ?? 0;
  686. //收款码是否算收入由ext gatherRepeat决定
  687. $post['repeat'] = $ext->gatherRepeat ?? 0;
  688. $customId = $this->customId;
  689. $custom = $this->custom;
  690. $hdId = $this->hdId;
  691. $hd = $this->hd;
  692. if (!empty($customId) && !empty($hdId)) {
  693. //考虑没有登录时的付款
  694. $shop = $this->shop;
  695. $defaultCustomId = $shop->defaultCustomId ?? 0;
  696. if (empty($defaultCustomId)) {
  697. util::fail('请设置快捷开单的客户');
  698. }
  699. $customId = $defaultCustomId;
  700. $custom = CustomClass::getById($defaultCustomId, true);
  701. if (empty($custom)) {
  702. util::fail('请设置快捷客户');
  703. }
  704. $hdId = $custom->hdId;
  705. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  706. if (empty($hd)) {
  707. util::fail('没有花店信息呢!');
  708. }
  709. }
  710. $post['hdId'] = $hdId;
  711. $post['hdName'] = $hd->name ?? '';
  712. $customName = $custom->name ?? '';
  713. $customPy = $custom->py ?? '';
  714. $post['customName'] = $customName;
  715. $post['customNamePy'] = $customPy;
  716. $post['gatheringItem'] = $gatheringItem;
  717. $post['shopId'] = $shopId;
  718. $post['mainId'] = $this->mainId;
  719. $post['sjId'] = $this->sjId;
  720. $post['store'] = 0;
  721. $post['customId'] = $customId;
  722. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  723. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  724. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  725. $bookMobile = empty($bookMobile) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  726. $post['bookMobile'] = $bookMobile;
  727. $prePrice = round($post['prePrice'], 2);
  728. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  729. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => 0, 'couponId' => $couponId, 'userId' => $this->userId]);
  730. $actPrice = $discountData['price'];
  731. $post['discountType'] = $discountData['discountType'];
  732. $post['discountAmount'] = $discountData['discountAmount'];
  733. $post['orderPrice'] = $actPrice;
  734. $post['actPrice'] = $actPrice;
  735. $post['realPrice'] = $actPrice;
  736. $post['mainPay'] = $actPrice;
  737. $post['modifyPrice'] = $actPrice;
  738. $post['userId'] = $this->userId;
  739. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  740. $now = time();
  741. $expireTime = $now + 300;//订单5分钟后过期
  742. $post['createTime'] = date("Y-m-d H:i:s", $now);
  743. $post['deadline'] = $expireTime;
  744. if ($actPrice <= 0) {
  745. util::fail('请填写付款金额');
  746. }
  747. $post['goodsNum'] = 1;
  748. $product = [[
  749. 'productId' => $gatheringItem,
  750. 'bigNum' => 1,
  751. 'smallNum' => '',
  752. 'num' => 1,
  753. 'unitType' => 0,
  754. 'property' => 1,
  755. 'unitPrice' => $actPrice,
  756. 'itemId' => $ptItemId,
  757. 'classId' => $classId
  758. ]];
  759. $post['product'] = $product;
  760. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : '00-00-00';
  761. $post['onlinePay'] = dict::getDict('onlinePay', 'yes');
  762. //前端未修改时这边做兼容
  763. if (!empty($post['receiveAddress'])) {
  764. $post['address'] = $post['receiveAddress'];
  765. if (!empty($post['receiveProvince'])) {
  766. $post['province'] = $post['receiveProvince'];
  767. }
  768. if (!empty($post['receiveCity'])) {
  769. $post['city'] = $post['receiveCity'];
  770. }
  771. if (!empty($post['receiveFloor'])) {
  772. $post['floor'] = $post['receiveFloor'];
  773. }
  774. if (!empty($post['latitude'])) {
  775. $post['lat'] = $post['latitude'];
  776. }
  777. if (!empty($post['longitude'])) {
  778. $post['long'] = $post['longitude'];
  779. }
  780. $city = $post['receiveCity'] ?? '';
  781. $address = $post['receiveAddress'];
  782. $floor = $post['receiveFloor'] ?? '';
  783. $post['fullAddress'] = $city . $address . $floor;
  784. }
  785. $hasPay = 0;
  786. $post['sendType'] = 1;
  787. $post['orderType'] = 2;//花材订单
  788. $post['dealPrice'] = 1;
  789. $post['fromType'] = 1;
  790. $order = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  791. $orderId = $order->id ?? 0;
  792. $orderSn = $order->orderSn ?? '';
  793. if ($payWay == 0) {
  794. //微信
  795. $name = '购买商品';
  796. $totalFee = $actPrice;
  797. $user = UserService::getById($this->userId);
  798. //小程序使用miniOpenId
  799. $openId = $user['miniOpenId'];
  800. if (empty($openId)) {
  801. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  802. }
  803. if (empty($openId)) {
  804. util::fail('没有微信信息');
  805. }
  806. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  807. $sjExtend = WxOpenClass::getMallWxInfo();
  808. $shop = $this->shop;
  809. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  810. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  811. $params = [
  812. 'appid' => 'OP00002119',
  813. 'serial_no' => '018b08cfddbd',
  814. 'merchant_no' => $shop->lklSjNo,
  815. 'term_no' => $shop->lklScanTermNo,
  816. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  817. 'lklCertificatePath' => $lklCertificatePath,
  818. ];
  819. $laResource = new Lakala($params);
  820. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  821. $wxParams = [
  822. 'orderSn' => $orderSn,
  823. 'amount' => $totalFee,
  824. 'capitalType' => $capitalType,
  825. 'notifyUrl' => $notifyUrl,
  826. 'wxAppId' => $sjExtend['miniAppId'],
  827. 'openId' => $openId,
  828. 'subject' => $name,
  829. 'couponId' => $couponId,
  830. ];
  831. $response = $laResource->driveWxPay($wxParams);
  832. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  833. $errMsg = $response['msg'] ?? '';
  834. noticeUtil::push($errMsg, '15280215347');
  835. util::fail('支付失败');
  836. }
  837. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  838. $appId = $newParams['app_id'] ?? '';
  839. $nonceStr = $newParams['nonce_str'] ?? '';
  840. $paySign = $newParams['pay_sign'] ?? '';
  841. $package = $newParams['package'] ?? '';
  842. $signType = $newParams['sign_type'] ?? '';
  843. $timeStamp = $newParams['time_stamp'] ?? '';
  844. $new = [
  845. 'id' => $orderId,
  846. 'appId' => $appId,
  847. 'nonceStr' => $nonceStr,
  848. 'paySign' => $paySign,
  849. 'package' => $package,
  850. 'signType' => $signType,
  851. 'timeStamp' => $timeStamp,
  852. 'orderSn' => $orderSn,
  853. ];
  854. util::success($new);
  855. } elseif ($payWay == 1) {
  856. //支付宝
  857. $totalFee = $actPrice;
  858. $subject = '购买花材';
  859. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  860. $shop = $this->shop;
  861. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  862. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  863. $params = [
  864. 'appid' => 'OP00002119',
  865. 'serial_no' => '018b08cfddbd',
  866. 'merchant_no' => $shop->lklSjNo,
  867. 'term_no' => $shop->lklB2BTermNo,
  868. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  869. 'lklCertificatePath' => $lklCertificatePath,
  870. ];
  871. $laResource = new Lakala($params);
  872. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  873. $cashParams = [
  874. 'orderSn' => $orderSn,
  875. 'amount' => $totalFee,
  876. 'capitalType' => $capitalType,
  877. 'notifyUrl' => $notifyUrl,
  878. 'subject' => $subject,
  879. ];
  880. $response = $laResource->cashierPay($cashParams);
  881. if (!isset($response['code']) || $response['code'] != '000000') {
  882. util::fail('支付失败了');
  883. }
  884. $url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  885. if (empty($url)) {
  886. util::fail('支付失败');
  887. }
  888. util::success(['url' => $url]);
  889. } elseif ($payWay == 2) {
  890. util::fail('无效的支付方式');
  891. //余额支付,验证支付密码是否正确
  892. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  893. $user = UserService::getUserInfo($this->userId);
  894. UserService::validPayPassword($payPassword, $user);
  895. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  896. $callbackParams = [];
  897. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  898. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  899. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  900. } else {
  901. util::fail('无效的支付方式');
  902. }
  903. }
  904. //订单列表 ssh 2019.12.12
  905. public function actionList()
  906. {
  907. $get = Yii::$app->request->get();
  908. $status = $get['status'] ?? 0;
  909. $userId = $this->userId;
  910. $where = ['userId' => $userId];
  911. if (!empty($status)) {
  912. $where['status'] = $status;
  913. }
  914. $list = OrderService::getOrderList($where);
  915. util::success($list);
  916. }
  917. //订单评价
  918. public function actionComment()
  919. {
  920. $post = Yii::$app->request->post();
  921. $id = $post['id'];
  922. $order = OrderClass::getById($id);
  923. if ($order['grade'] > 0) {
  924. util::fail('您已经评过了');
  925. }
  926. OrderService::comment($post);
  927. util::complete('提交成功');
  928. }
  929. //订单详情 ssh 2019.12.16
  930. public function actionDetail()
  931. {
  932. $id = Yii::$app->request->get('id', 0);
  933. $detail = OrderClass::getOrderById($id);
  934. $userId = $this->userId;
  935. if ($detail['userId'] != $userId) {
  936. util::fail('不是你的单,不能查看');
  937. }
  938. $shOrderMap = [];
  939. if ($detail['hasForward'] == 1) {
  940. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  941. }
  942. $detail['shOrderMap'] = $shOrderMap;
  943. util::success($detail);
  944. }
  945. //获取详情 ssh 20220512
  946. public function actionInfo()
  947. {
  948. $sn = Yii::$app->request->get('orderSn', '');
  949. $detail = OrderClass::getOrderBySn($sn);
  950. util::success($detail);
  951. }
  952. }