OrderController.php 43 KB

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