OrderController.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. $totalNum = 0;
  255. foreach ($productList as $eleKey => $element) {
  256. $level = 0;
  257. $productId = $element['productId'];
  258. $current = $productInfoList[$productId];
  259. if (empty($current)) {
  260. util::fail('有商品信息没有找到');
  261. }
  262. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($current, $level, $priceMap, $addPriceMap);
  263. $num = $element['num'] ?? 0;
  264. $currentTotal = bcmul($price, $num, 2);
  265. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  266. $productList[$eleKey]['unitPrice'] = $price;
  267. $weight = $current['weight'] ?? 0;
  268. $currentWeight = bcmul($num, $weight, 2);
  269. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  270. $totalNum = bcadd($totalNum, $num);
  271. }
  272. $post['product'] = $productList;
  273. //不能前端传运费过来,只能通过跑腿接口计算出来
  274. unset($post['sendCost']);
  275. if (isset($post['sendType']) && $post['sendType'] == 2) {
  276. $hasMap = dict::getDict('hasMap');
  277. if ($hasMap == 0) {
  278. util::fail('不能使用跑腿');
  279. }
  280. $cargoName = '花材'; // 商品名称
  281. $cargo = [
  282. 'cargo_name' => $cargoName, // 商品名称
  283. 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  284. 'cargo_price' => intval($modifyPrice * 100), // 单位:分 -- 把元转换为分
  285. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  286. 'cargo_num' => $totalNum
  287. ];
  288. $customName = $custom->name;
  289. $customMobile = $custom->mobile;
  290. if (empty($post['long']) || empty($post['lat'])) {
  291. util::fail('位置错误');
  292. }
  293. $originalLng = doubleval($post['long']);
  294. $originalLat = doubleval($post['lat']);
  295. $city = $post['city'] ?? '';
  296. $dist = $post['dist'] ?? '';
  297. $address = $post['address'] ?? '';
  298. $showAddress = $post['showAddress'] ?? '';
  299. $userAddress = $city . $dist . $address . "({$showAddress})";
  300. $main = $this->main;
  301. $wxStoreId = $main->wxStoreId ?? 0;
  302. if (empty($wxStoreId)) {
  303. util::fail('商家还没有开通跑腿功能');
  304. }
  305. $orderData = [
  306. 'wx_store_id' => $wxStoreId,
  307. 'user_name' => $customName,
  308. 'user_phone' => $customMobile,
  309. 'user_lng' => $originalLng,
  310. 'user_lat' => $originalLat,
  311. 'user_address' => $userAddress,
  312. 'cargo' => $cargo,
  313. ];
  314. $result = IntraCityExpress::preAddOrder($orderData);
  315. $errCode = $result['errcode'] ?? 1;
  316. if ($errCode != 0) {
  317. $errMsg = $result['errmsg'] ?? '';
  318. util::fail('获取运费错误:' . $errMsg);
  319. }
  320. $fee = $result['est_fee'] ?? 0;
  321. //距离,好像是米
  322. $distance = $result['distance'] ?? 0;
  323. $sendCost = bcdiv($fee, 100, 2);
  324. $post['sendCost'] = $sendCost;
  325. noticeUtil::push("运费:{$sendCost} 重量:{$totalWeight} 距离:{$distance} 姓名:{$customName} 手机号 {$customMobile} 经纬 {$originalLng} {$originalLat} 金额:{$modifyPrice} 数量:{$totalNum}", '15280215347');
  326. $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
  327. }
  328. $post['modifyPrice'] = $modifyPrice;
  329. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  330. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  331. if (isset($return->shopId)) {
  332. if ($return->shopId == 7610) {
  333. if ($return->actPrice < 200) {
  334. if ($return->sendType == 0) {
  335. util::fail('满200元支持送货上门');
  336. }
  337. }
  338. }
  339. }
  340. if ($return->sendType == 2) {
  341. if (in_array($return->shopId, [2, 726, 1490, 13626, 7856, 7688])) {
  342. util::fail('暂时不能使用跑腿');
  343. }
  344. }
  345. $transaction->commit();
  346. $orderSn = $return->orderSn ?? '';
  347. $orderPrice = $return->orderPrice ?? 0;
  348. $id = $return->id ?? 0;
  349. $getPayType = dict::getDict('getPayType');
  350. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
  351. } catch (\Exception $e) {
  352. $transaction->rollBack();
  353. Yii::error("失败原因:" . $e->getMessage());
  354. util::fail('下单失败');
  355. }
  356. }
  357. //商城下单操作 ssh 2019.12.3
  358. public function actionCreateOrder()
  359. {
  360. $post = Yii::$app->request->post();
  361. $goodsId = $post['goodsId'] ?? 0;
  362. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  363. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  364. if (empty($goodsId)) {
  365. util::fail('请选择商品');
  366. }
  367. $goodsInfo = GoodsClass::getById($goodsId, true);
  368. if (empty($goodsInfo)) {
  369. util::fail('没有找到商品');
  370. }
  371. $stock = $goodsInfo->stock ?? 0;
  372. if ($stock <= 0) {
  373. util::fail('库存不足');
  374. }
  375. //事务处理
  376. $connection = Yii::$app->db;
  377. $transaction = $connection->beginTransaction();
  378. try {
  379. $custom = $this->custom;
  380. if (empty($custom)) {
  381. util::fail('个人信息缺失');
  382. }
  383. $customId = $custom->id ?? 0;
  384. $hd = $this->hd;
  385. $post['customId'] = $customId;
  386. $customName = $custom->name ?? '';
  387. $post['customName'] = $customName;
  388. $post['customNamePy'] = stringUtil::py($customName);
  389. $post['hdId'] = $hd->id ?? 0;
  390. $post['hdName'] = $hd->name ?? '';
  391. $user = $this->user;
  392. $post['bookName'] = $user->name ?? '';
  393. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  394. $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
  395. $post['bookMobile'] = $bookMobile;
  396. $post['payWay'] = 0;
  397. //计算运费
  398. //$sendDistance = 0;
  399. //$sendCost = 0;
  400. $shop = $this->shop;
  401. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  402. $lat = $post['lat'] ?? '';
  403. $lng = $post['long'] ?? '';
  404. if ((empty($lat) || empty($lng)) && dict::getDict('hasMap') == 1) { // 当 hasMap 为1时,经纬数据是必要的
  405. util::fail('缺少经纬数据');
  406. }
  407. $address = $post['address'] ?? '';
  408. $floor = $post['floor'] ?? '';
  409. $fullAddress = $address . $floor;
  410. $post['fullAddress'] = $fullAddress;
  411. $shopLat = $shop->lat ?? '';
  412. $shopLong = $shop->long ?? '';
  413. if (empty($shopLat) || empty($shopLong)) {
  414. //util::fail('请完成门店地址');
  415. }
  416. //花束重量默认1.5
  417. $weight = 1.5;
  418. //$disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
  419. //$sendCost = $disRespond['fee'] ?? 0;
  420. //$sendDistance = $disRespond['distance'] ?? 0;
  421. }
  422. $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
  423. $sendCost = isset($post['sendCost']) ? floatval($post['sendCost']) : 0;
  424. $post['sendCost'] = $sendCost;
  425. $post['sjId'] = $this->sjId;
  426. $post['shopId'] = $this->shopId;
  427. $mainId = $this->mainId;
  428. $post['mainId'] = $mainId;
  429. $post['userId'] = $this->userId;
  430. //计算总金额
  431. $unitPrice = $goodsInfo->price ?? 0;
  432. if ($unitPrice <= 0) {
  433. util::fail('没有价格,无法下单');
  434. }
  435. $rise = GoodsSettingClass::getRise($mainId);
  436. //根据涨价设置进行自动涨价
  437. if (!empty($rise)) {
  438. $price = $unitPrice;
  439. if ($rise['riseType'] == 0) {
  440. //百分比
  441. $newPrice = $price + sprintf("%.1f", ($price * $rise['riseAmount'] / 100));
  442. } else {
  443. //金额
  444. $newPrice = $price + $rise['riseAmount'];
  445. }
  446. $unitPrice = $newPrice;
  447. }
  448. $goodsPrice = $unitPrice * $goodsNum;
  449. $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  450. //非门店订单
  451. $post['store'] = 0;
  452. $sourceType = 2;
  453. $couponId = !empty($post['couponId']) ? $post['couponId'] : 0;
  454. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
  455. $actPrice = $discountData['price'];
  456. $post['modifyPrice'] = $actPrice;
  457. $post['discountType'] = $discountData['discountType'];
  458. $post['discountAmount'] = $discountData['discountAmount'];
  459. //非自取订单考虑配送时间
  460. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  461. $sendType = $post['sendType'] ?? 0;
  462. if ($sendType != 1) {
  463. if (empty($post['reachPeriod'])) {
  464. util::fail('请选择配送时间哈');
  465. }
  466. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  467. }
  468. $now = time();
  469. $expireTime = $now + 1800;//订单30分钟后过期
  470. $post['deadline'] = $expireTime;
  471. $orderSn = orderSn::getOrderSn();
  472. $post['orderSn'] = $orderSn;
  473. $post['fromType'] = dict::getDict('fromType', 'mall');
  474. $post['prePrice'] = $prePrice;
  475. $post['orderPrice'] = $actPrice;
  476. $post['actPrice'] = $actPrice;
  477. $post['realPrice'] = $actPrice;
  478. $post['mainPay'] = $actPrice;
  479. $post['cash'] = 0;
  480. $post['mainId'] = $mainId;
  481. $post['needPrint'] = dict::getDict('needPrint', 'need');
  482. $hasPay = 0;
  483. $product = [['productId' => $goodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
  484. $post['product'] = $product;
  485. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  486. $orderId = $return->id;
  487. $orderSn = $return->orderSn;
  488. $transaction->commit();
  489. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'id' => $orderId]);
  490. } catch (Exception $e) {
  491. $transaction->rollBack();
  492. Yii::info("失败原因:" . $e->getMessage());
  493. util::fail('下单失败');
  494. }
  495. }
  496. //下单要用到的相关信息 ssh 2019.12.6
  497. public function actionOrderRelate()
  498. {
  499. $regionTree = RegionService::tree();
  500. //门店名称
  501. $shop = !empty($this->shop) ? $this->shop->attributes : [];
  502. $sjName = $shop['merchantName'] ?? '';
  503. $shopName = $shop['shopName'] ?? '';
  504. $shop['showName'] = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  505. $avatar = $shop['avatar'] ?? '';
  506. $smallAvatar = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  507. $shop['smallAvatar'] = $smallAvatar;
  508. $freight = ['first' => 5, 'add' => 2];
  509. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  510. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  511. util::success($out);
  512. }
  513. //余额支付 ssh 20250410
  514. public function actionBalancePay()
  515. {
  516. $post = Yii::$app->request->post();
  517. $orderSn = $post['orderSn'] ?? '';
  518. //避免重复提交
  519. util::checkRepeatCommit($orderSn, 10);
  520. $connection = Yii::$app->db;
  521. $transaction = $connection->beginTransaction();
  522. try {
  523. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  524. if (empty($order)) {
  525. util::fail('没有找到订单');
  526. }
  527. if ($order->shopId != $this->shopId) {
  528. util::fail('不是你的订单');
  529. }
  530. $payWay = dict::getDict('payWay', 'balancePay');
  531. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  532. $transaction->commit();
  533. util::complete('支付成功');
  534. } catch (\Exception $e) {
  535. $transaction->rollBack();
  536. Yii::error("支付原因:" . $e->getMessage());
  537. util::fail('支付失败');
  538. }
  539. }
  540. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  541. public function actionWxPay()
  542. {
  543. ini_set('date.timezone', 'Asia/Shanghai');
  544. $post = Yii::$app->request->post();
  545. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  546. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  547. $order = OrderService::getByOrderSn($orderSn);
  548. $orderId = $order['id'];
  549. //验证优惠券是否还有效
  550. if (!empty($couponId)) {
  551. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  552. }
  553. //支付前验证订单有效性
  554. OrderService::checkBeforePay($order);
  555. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  556. $shop = $this->shop;
  557. if (empty($shop)) {
  558. util::fail('没有找到门店信息');
  559. }
  560. if (isset($shop->shopName) && !empty($shop->shopName)) {
  561. $name .= '(' . $shop->shopName . ')';
  562. }
  563. $totalFee = $order['mainPay'];
  564. $openId = '';
  565. if (!empty($post['miniOpenId'])) {
  566. $openId = $post['miniOpenId'];
  567. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  568. }
  569. if (empty($openId)) {
  570. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  571. $openId = $this->user['miniOpenId'];
  572. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  573. }
  574. }
  575. if (empty($openId)) {
  576. util::fail('没有找到openId');
  577. }
  578. $now = time();
  579. $expireTime = $now + 1800;
  580. $updateData = [];
  581. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  582. $oldOrderSn = $orderSn;
  583. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  584. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  585. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  586. $params = [
  587. 'appid' => 'OP00002119',
  588. 'serial_no' => '018b08cfddbd',
  589. 'merchant_no' => $shop->lklSjNo,
  590. 'term_no' => $shop->lklScanTermNo,
  591. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  592. 'lklCertificatePath' => $lklCertificatePath,
  593. ];
  594. $laResource = new Lakala($params);
  595. $closeParams = [
  596. 'orderSn' => $oldOrderSn,
  597. ];
  598. $response = $laResource->close($closeParams);
  599. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  600. $msg = $response['msg'] ?? '';
  601. $lklSjNo = $shop->lklSjNo ?? '';
  602. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  603. util::fail('出错了哦,请重新下一单');
  604. }
  605. $orderSn = orderSn::getOrderSn();
  606. $updateData['orderSn'] = $orderSn;
  607. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  608. } else {
  609. //已请求的不能再修改价格
  610. $updateData['modPrice'] = 0;
  611. }
  612. if (empty($order['deadline'])) {
  613. $updateData['deadline'] = $expireTime;
  614. }
  615. if (!empty($updateData)) {
  616. OrderService::updateById($orderId, $updateData);
  617. }
  618. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  619. $sjExtend = WxOpenClass::getMallWxInfo();
  620. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  621. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  622. $params = [
  623. 'appid' => 'OP00002119',
  624. 'serial_no' => '018b08cfddbd',
  625. 'merchant_no' => $shop->lklSjNo,
  626. 'term_no' => $shop->lklScanTermNo,
  627. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  628. 'lklCertificatePath' => $lklCertificatePath,
  629. ];
  630. $laResource = new Lakala($params);
  631. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  632. $wxParams = [
  633. 'orderSn' => $orderSn,
  634. 'amount' => $totalFee,
  635. 'capitalType' => $capitalType,
  636. 'notifyUrl' => $notifyUrl,
  637. 'wxAppId' => $sjExtend['miniAppId'],
  638. 'openId' => $openId,
  639. 'subject' => $name,
  640. 'couponId' => $couponId,
  641. ];
  642. $response = $laResource->driveWxPay($wxParams);
  643. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  644. $errMsg = $response['msg'] ?? '';
  645. noticeUtil::push($errMsg, '15280215347');
  646. util::fail('支付失败');
  647. }
  648. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  649. $appId = $newParams['app_id'] ?? '';
  650. $nonceStr = $newParams['nonce_str'] ?? '';
  651. $paySign = $newParams['pay_sign'] ?? '';
  652. $package = $newParams['package'] ?? '';
  653. $signType = $newParams['sign_type'] ?? '';
  654. $timeStamp = $newParams['time_stamp'] ?? '';
  655. $new = [
  656. 'id' => $orderId,
  657. 'appId' => $appId,
  658. 'nonceStr' => $nonceStr,
  659. 'paySign' => $paySign,
  660. 'package' => $package,
  661. 'signType' => $signType,
  662. 'timeStamp' => $timeStamp,
  663. 'orderSn' => $orderSn,
  664. ];
  665. util::success($new);
  666. }
  667. //快捷付款订单
  668. public function actionFastPay()
  669. {
  670. ini_set('date.timezone', 'Asia/Shanghai');
  671. $post = Yii::$app->request->post();
  672. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  673. $shopId = $this->shopId;
  674. $shop = $this->shop;
  675. if (empty($shop)) {
  676. util::fail('没有门店信息');
  677. }
  678. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  679. $gatheringItem = !empty($ext) ? $ext->gatheringItem : 0;
  680. if (empty($gatheringItem)) {
  681. util::fail('请设置付款用的商品');
  682. }
  683. $gatheringItemInfo = ItemClass::getById($gatheringItem, true);
  684. if (empty($gatheringItemInfo)) {
  685. util::fail('付款用的商品没有设置');
  686. }
  687. $ptItemId = $gatheringItemInfo->itemId ?? 0;
  688. $classId = $gatheringItemInfo->classId ?? 0;
  689. //收款码是否算收入由ext gatherRepeat决定
  690. $post['repeat'] = $ext->gatherRepeat ?? 0;
  691. $customId = $this->customId;
  692. $custom = $this->custom;
  693. $hdId = $this->hdId;
  694. $hd = $this->hd;
  695. if (!empty($customId) && !empty($hdId)) {
  696. //考虑没有登录时的付款
  697. $shop = $this->shop;
  698. $defaultCustomId = $shop->defaultCustomId ?? 0;
  699. if (empty($defaultCustomId)) {
  700. util::fail('请设置快捷开单的客户');
  701. }
  702. $customId = $defaultCustomId;
  703. $custom = CustomClass::getById($defaultCustomId, true);
  704. if (empty($custom)) {
  705. util::fail('请设置快捷客户');
  706. }
  707. $hdId = $custom->hdId;
  708. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  709. if (empty($hd)) {
  710. util::fail('没有花店信息呢!');
  711. }
  712. }
  713. $post['hdId'] = $hdId;
  714. $post['hdName'] = $hd->name ?? '';
  715. $customName = $custom->name ?? '';
  716. $customPy = $custom->py ?? '';
  717. $post['customName'] = $customName;
  718. $post['customNamePy'] = $customPy;
  719. $post['gatheringItem'] = $gatheringItem;
  720. $post['shopId'] = $shopId;
  721. $post['mainId'] = $this->mainId;
  722. $post['sjId'] = $this->sjId;
  723. $post['store'] = 0;
  724. $post['customId'] = $customId;
  725. $user = isset($this->user->attributes) ? $this->user->attributes : [];
  726. $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
  727. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  728. $bookMobile = empty($bookMobile) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
  729. $post['bookMobile'] = $bookMobile;
  730. $prePrice = round($post['prePrice'], 2);
  731. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  732. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => 0, 'couponId' => $couponId, 'userId' => $this->userId]);
  733. $actPrice = $discountData['price'];
  734. $post['discountType'] = $discountData['discountType'];
  735. $post['discountAmount'] = $discountData['discountAmount'];
  736. $post['orderPrice'] = $actPrice;
  737. $post['actPrice'] = $actPrice;
  738. $post['realPrice'] = $actPrice;
  739. $post['mainPay'] = $actPrice;
  740. $post['modifyPrice'] = $actPrice;
  741. $post['userId'] = $this->userId;
  742. $post['needPrint'] = dict::getDict('needPrint', 'noNeed');
  743. $now = time();
  744. $expireTime = $now + 300;//订单5分钟后过期
  745. $post['createTime'] = date("Y-m-d H:i:s", $now);
  746. $post['deadline'] = $expireTime;
  747. if ($actPrice <= 0) {
  748. util::fail('请填写付款金额');
  749. }
  750. $post['goodsNum'] = 1;
  751. $product = [[
  752. 'productId' => $gatheringItem,
  753. 'bigNum' => 1,
  754. 'smallNum' => '',
  755. 'num' => 1,
  756. 'unitType' => 0,
  757. 'property' => 1,
  758. 'unitPrice' => $actPrice,
  759. 'itemId' => $ptItemId,
  760. 'classId' => $classId
  761. ]];
  762. $post['product'] = $product;
  763. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : '00-00-00';
  764. $post['onlinePay'] = dict::getDict('onlinePay', 'yes');
  765. //前端未修改时这边做兼容
  766. if (!empty($post['receiveAddress'])) {
  767. $post['address'] = $post['receiveAddress'];
  768. if (!empty($post['receiveProvince'])) {
  769. $post['province'] = $post['receiveProvince'];
  770. }
  771. if (!empty($post['receiveCity'])) {
  772. $post['city'] = $post['receiveCity'];
  773. }
  774. if (!empty($post['receiveFloor'])) {
  775. $post['floor'] = $post['receiveFloor'];
  776. }
  777. if (!empty($post['latitude'])) {
  778. $post['lat'] = $post['latitude'];
  779. }
  780. if (!empty($post['longitude'])) {
  781. $post['long'] = $post['longitude'];
  782. }
  783. $city = $post['receiveCity'] ?? '';
  784. $address = $post['receiveAddress'];
  785. $floor = $post['receiveFloor'] ?? '';
  786. $post['fullAddress'] = $city . $address . $floor;
  787. }
  788. $hasPay = 0;
  789. $post['sendType'] = 1;
  790. $post['orderType'] = 2;//花材订单
  791. $post['dealPrice'] = 1;
  792. $post['fromType'] = 1;
  793. $order = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  794. $orderId = $order->id ?? 0;
  795. $orderSn = $order->orderSn ?? '';
  796. if ($payWay == 0) {
  797. //微信
  798. $name = '购买商品';
  799. $totalFee = $actPrice;
  800. $user = UserService::getById($this->userId);
  801. //小程序使用miniOpenId
  802. $openId = $user['miniOpenId'];
  803. if (empty($openId)) {
  804. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  805. }
  806. if (empty($openId)) {
  807. util::fail('没有微信信息');
  808. }
  809. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  810. $sjExtend = WxOpenClass::getMallWxInfo();
  811. $shop = $this->shop;
  812. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  813. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  814. $params = [
  815. 'appid' => 'OP00002119',
  816. 'serial_no' => '018b08cfddbd',
  817. 'merchant_no' => $shop->lklSjNo,
  818. 'term_no' => $shop->lklScanTermNo,
  819. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  820. 'lklCertificatePath' => $lklCertificatePath,
  821. ];
  822. $laResource = new Lakala($params);
  823. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  824. $wxParams = [
  825. 'orderSn' => $orderSn,
  826. 'amount' => $totalFee,
  827. 'capitalType' => $capitalType,
  828. 'notifyUrl' => $notifyUrl,
  829. 'wxAppId' => $sjExtend['miniAppId'],
  830. 'openId' => $openId,
  831. 'subject' => $name,
  832. 'couponId' => $couponId,
  833. ];
  834. $response = $laResource->driveWxPay($wxParams);
  835. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  836. $errMsg = $response['msg'] ?? '';
  837. noticeUtil::push($errMsg, '15280215347');
  838. util::fail('支付失败');
  839. }
  840. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  841. $appId = $newParams['app_id'] ?? '';
  842. $nonceStr = $newParams['nonce_str'] ?? '';
  843. $paySign = $newParams['pay_sign'] ?? '';
  844. $package = $newParams['package'] ?? '';
  845. $signType = $newParams['sign_type'] ?? '';
  846. $timeStamp = $newParams['time_stamp'] ?? '';
  847. $new = [
  848. 'id' => $orderId,
  849. 'appId' => $appId,
  850. 'nonceStr' => $nonceStr,
  851. 'paySign' => $paySign,
  852. 'package' => $package,
  853. 'signType' => $signType,
  854. 'timeStamp' => $timeStamp,
  855. 'orderSn' => $orderSn,
  856. ];
  857. util::success($new);
  858. } elseif ($payWay == 1) {
  859. //支付宝
  860. $totalFee = $actPrice;
  861. $subject = '购买花材';
  862. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  863. $shop = $this->shop;
  864. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  865. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  866. $params = [
  867. 'appid' => 'OP00002119',
  868. 'serial_no' => '018b08cfddbd',
  869. 'merchant_no' => $shop->lklSjNo,
  870. 'term_no' => $shop->lklB2BTermNo,
  871. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  872. 'lklCertificatePath' => $lklCertificatePath,
  873. ];
  874. $laResource = new Lakala($params);
  875. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  876. $cashParams = [
  877. 'orderSn' => $orderSn,
  878. 'amount' => $totalFee,
  879. 'capitalType' => $capitalType,
  880. 'notifyUrl' => $notifyUrl,
  881. 'subject' => $subject,
  882. ];
  883. $response = $laResource->cashierPay($cashParams);
  884. if (!isset($response['code']) || $response['code'] != '000000') {
  885. util::fail('支付失败了');
  886. }
  887. $url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  888. if (empty($url)) {
  889. util::fail('支付失败');
  890. }
  891. util::success(['url' => $url]);
  892. } elseif ($payWay == 2) {
  893. util::fail('无效的支付方式');
  894. //余额支付,验证支付密码是否正确
  895. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  896. $user = UserService::getUserInfo($this->userId);
  897. UserService::validPayPassword($payPassword, $user);
  898. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  899. $callbackParams = [];
  900. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  901. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  902. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  903. } else {
  904. util::fail('无效的支付方式');
  905. }
  906. }
  907. //订单列表 ssh 2019.12.12
  908. public function actionList()
  909. {
  910. $get = Yii::$app->request->get();
  911. $status = $get['status'] ?? 0;
  912. $userId = $this->userId;
  913. $where = ['userId' => $userId];
  914. if (!empty($status)) {
  915. $where['status'] = $status;
  916. }
  917. $list = OrderService::getOrderList($where);
  918. util::success($list);
  919. }
  920. //订单评价
  921. public function actionComment()
  922. {
  923. $post = Yii::$app->request->post();
  924. $id = $post['id'];
  925. $order = OrderClass::getById($id);
  926. if ($order['grade'] > 0) {
  927. util::fail('您已经评过了');
  928. }
  929. OrderService::comment($post);
  930. util::complete('提交成功');
  931. }
  932. //订单详情 ssh 2019.12.16
  933. public function actionDetail()
  934. {
  935. $id = Yii::$app->request->get('id', 0);
  936. $detail = OrderClass::getOrderById($id);
  937. $userId = $this->userId;
  938. if ($detail['userId'] != $userId) {
  939. util::fail('不是你的单,不能查看');
  940. }
  941. $shOrderMap = [];
  942. if ($detail['hasForward'] == 1) {
  943. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  944. }
  945. $detail['shOrderMap'] = $shOrderMap;
  946. util::success($detail);
  947. }
  948. //获取详情 ssh 20220512
  949. public function actionInfo()
  950. {
  951. $sn = Yii::$app->request->get('orderSn', '');
  952. $detail = OrderClass::getOrderBySn($sn);
  953. util::success($detail);
  954. }
  955. }