OrderController.php 42 KB

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