OrderController.php 42 KB

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