OrderController.php 43 KB

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