OrderController.php 43 KB

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