OrderController.php 43 KB

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