OrderController.php 42 KB

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