OrderController.php 41 KB

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