| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153 |
- <?php
- namespace mall\controllers;
- use biz\product\classes\ProductClass;
- use biz\shop\classes\ShopClass;
- use biz\wx\classes\WxMessageClass;
- use bizHd\custom\classes\HdClass;
- use bizHd\express\classes\HdDeliveryOrderClass;
- use bizHd\order\classes\ScanPayClass;
- use bizHd\wx\classes\WxOpenClass;
- use bizMall\custom\classes\CustomClass;
- use bizMall\goods\classes\GoodsClass;
- use bizMall\order\classes\OrderClass;
- use bizMall\order\classes\OrderForwardClass;
- use bizMall\order\classes\OrderItemClass;
- use bizMall\order\services\OrderService;
- use bizMall\promote\services\CouponService;
- use bizMall\saas\services\RegionService;
- use bizMall\shop\classes\ShopExtClass;
- use bizMall\user\services\UserService;
- use common\components\dateUtil;
- use common\components\delivery\util\DeliveryQuoteUtil;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\stringUtil;
- use Yii;
- use common\components\util;
- use common\components\lakala\Lakala;
- use common\components\IntraCityExpress;
- class OrderController extends BaseController
- {
- //二维码收款使用
- public $guestAccess = ['order-relate', 'fast-pay'];
- //记账单列表 ssh 20250627
- public function actionDebtList()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $where = [];
- $where['userId'] = $this->userId;
- if (!empty($id)) {
- $hd = \bizMall\hd\classes\HdClass::getById($id, true);
- if (empty($hd)) {
- util::fail('无效的花店');
- }
- $where['hdId'] = $id;
- }
- $where['debt'] = 1;
- $searchTime = $get['searchTime'] ?? '';
- if (!empty($searchTime)) {
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $respond = OrderClass::getDebtList($where);
- util::success($respond);
- }
- //新计算运费
- public function actionNewFreight()
- {
- $post = Yii::$app->request->post();
- $hasMap = dict::getDict('hasMap');
- if ($hasMap == 0) {
- util::fail('暂不支持跑腿功能');
- }
- $mainData = $this->main;
- $wxStoreId = $mainData->wxStoreId ?? '';
- if (empty($wxStoreId)) {
- util::fail('商家还没有开通跑腿');
- }
- $cargoName = $post['cargo_name']; // 商品名称
- $cargo = [
- 'cargo_name' => $cargoName, // 商品名称
- 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
- 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
- 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
- 'cargo_num' => intval($post['package_num'])
- ];
- $originalLng = doubleval($post['user_lng']);
- $originalLat = doubleval($post['user_lat']);
- $userName = !empty($post['user_name']) ? $post['user_name'] : '游客';
- $orderData = [
- 'wx_store_id' => $wxStoreId,
- 'user_name' => $userName,
- 'user_phone' => $post['user_phone'],
- 'user_lng' => $originalLng,
- 'user_lat' => $originalLat,
- 'user_address' => $post['user_address'],
- 'cargo' => $cargo,
- ];
- //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
- $result = IntraCityExpress::preAddOrder($orderData);
- if ($result['errcode'] === 0) {
- util::success($result, "查询成功");
- } else {
- Yii::error("运费查询失败:" . json_encode($result));
- $errMsg = $result['errmsg'] ?? '';
- $errCode = $result['errcode'] ?? '';
- noticeUtil::push("运费查询失败,原因:" . $errMsg . ",编号:" . $errCode, '15280215347');
- util::fail('运费查询失败,请确认是否超过20公斤,编号896');
- }
- }
- //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
- public function actionFreight()
- {
- $get = Yii::$app->request->get();
- $shop = $this->shop;
- $shopLat = isset($shop->lat) ? $shop->lat : '';
- $shopLong = isset($shop->long) ? $shop->long : '';
- // 判断地图功能是否正常。0 表示地图定位不可用,全部经纬度数据失效
- if (dict::getDict('hasMap') == 0) {
- $shopLat = '';
- $shopLong = '';
- }
- if (empty($shopLat) || empty($shopLong)) {
- util::success([
- 'distance' => 0,
- 'showDistance' => 0,
- 'fee' => 0,
- 'hasMap' => dict::getDict('hasMap'),
- 'shop' => 'fail' //门店引起的失败
- ]);
- }
- $userLat = $get['lat'] ?? '';
- $userLong = $get['long'] ?? '';
- if (empty($userLat) || empty($userLong)) {
- util::success([
- 'distance' => 0,
- 'showDistance' => 0,
- 'fee' => 0,
- 'hasMap' => dict::getDict('hasMap'),
- 'client' => 'fail' //用户端定位失败
- ]);
- }
- $weight = $get['weight'] ?? 0;
- if (empty($weight)) {
- util::success([
- 'distance' => 0,
- 'showDistance' => 0,
- 'fee' => 0,
- 'hasMap' => dict::getDict('hasMap'),
- 'weight' => 'fail' //重量不对
- ]);
- }
- //data: {distance: "321", showDistance: "0.3", fee: "12.00"}
- $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
- util::success($respond);
- }
- //购买花材 ssh 20220511
- public function actionBuyItem()
- {
- $post = Yii::$app->request->post();
- $post['sjId'] = $this->sjId;
- $post['shopId'] = $this->shopId;
- $post['payWay'] = 0;
- $post['mainId'] = $this->mainId ?? 0;
- $post['userId'] = $this->userId ?? 0;
- //避免重复提交
- $userId = $this->userId;
- util::checkRepeatCommit($userId, 3);
- $shop = $this->shop;
- $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
- if ($openShop == 0) {
- util::fail('已休店');
- }
- //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
- $mainId = $this->mainId;
- if (getenv('YII_ENV') == 'production') {
- if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
- util::fail('暂时无法访问');
- }
- } else {
- if (in_array($mainId, [0, 1])) {
- util::fail('暂时无法访问');
- }
- }
- $hdId = $post['hdId'] ?? 0;
- $hd = HdClass::getById($hdId, true);
- if (empty($hd)) {
- util::fail('没有找到花店');
- }
- if ($hd->shopId != $this->shopId) {
- util::fail('不是你的花店');
- }
- $post['hdName'] = $hd->name ?? '';
- $customId = $hd->customId ?? 0;
- $custom = CustomClass::getById($customId, true);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- $customName = $custom->name ?? '';
- $post['customId'] = $customId;
- $post['customName'] = $customName;
- $post['customNamePy'] = stringUtil::py($customName);
- $now = time();
- //订单30分钟后过期
- $expireTime = $now + 1800;
- $post['deadline'] = $expireTime;
- //商城
- $post['fromType'] = dict::getDict('fromType', 'mall');
- $productJson = $post['product'] ?? '';
- if (empty($productJson) && empty($groupId)) {
- util::fail('请选择商品');
- }
- $productList = json_decode($productJson, true);
- if (empty($productList)) {
- util::fail('请选择商品');
- }
- $ids = array_unique(array_filter(array_column($productList, 'productId')));
- $productInfoList = productClass::getByIds($ids, null, 'id');
- if (!empty($productInfoList)) {
- $presellData = [];
- $nowTime = strtotime(date("Y-m-d"));
- foreach ($productInfoList as $currentInfo) {
- if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $this->mainId) {
- util::fail('只能选择同一家的商品哦');
- }
- $currentName = $currentInfo['name'] ?? '';
- $presell = $currentInfo['presell'] ?? 0;
- $presellData[] = $presell;
- if ($presell == 1) {
- if (empty($post['reachDate'])) {
- util::fail('请选择配送或取货日期');
- }
- $sendTimeWant = $post['reachDate'];
- if (strtotime($sendTimeWant) < $nowTime) {
- util::fail('不能选择过去时间');
- }
- $hasDateStr = $currentInfo['presellDate'] ?? [];
- $hasDate = explode(',', trim($hasDateStr));
- if (empty($hasDate)) {
- util::fail('预售日期有问题,请提醒门店');
- }
- if (!in_array(strtotime($sendTimeWant), $hasDate)) {
- util::fail("有预售花材在{$sendTimeWant}没到货");
- }
- $post['presell'] = 1;
- }
- }
- if (count($productInfoList) != count($ids)) {
- util::fail('存在无效商品');
- }
- $presellData = array_unique($presellData);
- if (count($presellData) > 1) {
- util::fail('预售和非预售花材不能一起下单');
- }
- } else {
- util::fail('花材不存在');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME');
- $post['deadline'] = time() + $orderValidTime;
- $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
- $post['needPrint'] = dict::getDict('needPrint', 'need');
- $user = $this->user;
- $post['bookMobile'] = $user->mobile ?? '';
- $post['bookName'] = $user->name ?? '';
- $hasPay = 0;
- $modifyPrice = 0;
- //各个等级对应的price addPrice字段
- $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
- $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
- $totalWeight = 0;
- $totalNum = 0;
- foreach ($productList as $eleKey => $element) {
- $level = 0;
- $productId = $element['productId'];
- $current = $productInfoList[$productId];
- if (empty($current)) {
- util::fail('有商品信息没有找到');
- }
- $price = \bizGhs\product\classes\ProductClass::getFinalPrice($current, $level, $priceMap, $addPriceMap);
- $num = $element['num'] ?? 0;
- $currentTotal = bcmul($price, $num, 2);
- $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
- $productList[$eleKey]['unitPrice'] = $price;
- $weight = $current['weight'] ?? 0;
- $currentWeight = bcmul($num, $weight, 2);
- $totalWeight = bcadd($totalWeight, $currentWeight, 2);
- $totalNum = bcadd($totalNum, $num);
- }
- $post['product'] = $productList;
- //不能前端传运费过来,只能通过跑腿接口计算出来
- unset($post['sendCost']);
- $mapSet = ShopClass::hasIntraCity($this->shop);
- $openIntraCity = $mapSet['openIntraCity'] ?? 0;
- if (isset($post['sendType']) && $post['sendType'] == 2 && $openIntraCity != 0) {
- if ($openIntraCity == 2) {
- util::fail('不能使用跑腿');
- }
- $cargoName = '花材'; // 商品名称
- $cargo = [
- 'cargo_name' => $cargoName, // 商品名称
- 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
- 'cargo_price' => intval($modifyPrice * 100), // 单位:分 -- 把元转换为分
- 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
- 'cargo_num' => $totalNum
- ];
- $customName = $custom->name;
- $customMobile = $custom->mobile;
- if (empty($post['long']) || empty($post['lat'])) {
- util::fail('位置错误');
- }
- // $originalLng = doubleval($post['long']);
- // $originalLat = doubleval($post['lat']);
- // $city = $post['city'] ?? '';
- // $dist = $post['dist'] ?? '';
- // $address = $post['address'] ?? '';
- // $showAddress = $post['showAddress'] ?? '';
- // $userAddress = $city . $dist . $address . "({$showAddress})";
- // $main = $this->main;
- $shop = $this->shop;
- $shopLat = $shop->lat ?? '';
- $shopLong = $shop->long ?? '';
- if (empty($shopLat) || empty($shopLong)) {
- util::fail('商家门店地址定位未设置,编号263');
- }
- // $wxStoreId = $main->wxStoreId ?? 0;
- // if (empty($wxStoreId)) {
- // util::fail('商家还没有开通跑腿功能');
- // }
- // $orderData = [
- // 'wx_store_id' => $wxStoreId,
- // 'user_name' => $customName,
- // 'user_phone' => $customMobile,
- // 'user_lng' => $originalLng,
- // 'user_lat' => $originalLat,
- // 'user_address' => $userAddress,
- // 'cargo' => $cargo,
- // ];
- // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
- // $result = IntraCityExpress::preAddOrder($orderData);
- // $errCode = $result['errcode'] ?? 1;
- // if ($errCode != 0) {
- // $errMsg = $result['errmsg'] ?? '';
- // util::fail('获取运费错误:' . $errMsg);
- // }
- // $fee = $result['est_fee'] ?? 0;
- // $distance = $result['distance'] ?? 0;
- // $sendCost = bcdiv($fee, 100, 2);
- $distance = 0;
- $sendCost = 0;
- //生成随机订单号,不要跟原来的混起来,跑腿-销售单-
- $prefix = 'PT-XSD-' . $this->mainId . '-';
- $orderSn = $prefix . round(microtime(true) * 1000);
- $ghsInfo = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
- // 这儿要调用跑腿接口计算运费和距离
- try {
- if (empty($post['deliveryPlatform'])) {
- util::fail('请选择跑腿');
- }
- $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
- 'productList' => $productList,
- 'deliveryPlatform' => $post['deliveryPlatform'],
- 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
- 'ghsInfo' => $ghsInfo,
- 'custom' => $this->user, // $custom 数据有问题,要是 user
- 'order' => [
- 'orderSn' => $orderSn,
- 'goodsType' => 1, //商品类型:0花束 1花材
- 'itemTotalAmount' => $modifyPrice,
- 'remark' => $post['remark'] ?? '',
- ],
- 'mainId' => $this->mainId,
- 'productCount' => $totalNum,
- ]);
- $sendCost = $quoteResult['sendCost'];
- $distance = $quoteResult['sendDistance'];
- } catch (\Exception $e) {
- util::fail($e->getMessage());
- }
- $post['sendCost'] = $sendCost;
- $post['sendDistance'] = $distance;
- //noticeUtil::push("零售订单(花材),获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$distance}m 姓名:{$customName} 手机号 {$customMobile} 金额:{$modifyPrice} 数量:{$totalNum}", '15280215347');
- $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
- }
- $post['modifyPrice'] = $modifyPrice;
- //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
- $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
- if (isset($return->shopId)) {
- if ($return->shopId == 7610) {
- if ($return->actPrice < 200) {
- if ($return->sendType == 0) {
- util::fail('满200元支持送货上门');
- }
- }
- }
- }
- if ($return->sendType == 2) {
- $ext = ShopExtClass::getByCondition(['shopId' => $return->shopId], true, null, 'id,shopId,thirdSend');
- if ($ext->thirdSend == 1) {
- util::fail('本店暂不能使用跑腿呢');
- }
- }
- $transaction->commit();
-
- $orderSn = $return->orderSn ?? '';
- $orderPrice = $return->orderPrice ?? 0;
- $id = $return->id ?? 0;
- $getPayType = dict::getDict('getPayType');
- util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::error("失败原因:" . $e->getMessage());
- util::fail('下单失败');
- }
- }
- //商城下单操作 ssh 2019.12.3
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- //避免重复提交
- $userId = $this->userId;
- util::checkRepeatCommit($userId, 3);
- $shop = $this->shop;
- $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
- if ($openShop == 0) {
- util::fail('已休店');
- }
- $goodsId = $post['goodsId'] ?? 0;
- $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
- $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
- if (empty($goodsId)) {
- util::fail('请选择商品');
- }
- $mainId = $this->mainId;
- $goodsInfo = GoodsClass::getById($goodsId, true);
- if (empty($goodsInfo)) {
- util::fail('没有找到商品');
- }
- if ($goodsInfo->mainId != $mainId) {
- util::fail('不是你的商品');
- }
- $stock = $goodsInfo->stock ?? 0;
- if ($stock <= 0) {
- util::fail('库存不足');
- }
- //事务处理
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $custom = $this->custom;
- if (empty($custom)) {
- util::fail('个人信息缺失');
- }
- $customId = $custom->id ?? 0;
- $hd = $this->hd;
- $post['customId'] = $customId;
- $customName = $custom->name ?? '';
- $post['customName'] = $customName;
- $post['customNamePy'] = stringUtil::py($customName);
- $post['hdId'] = $hd->id ?? 0;
- $post['hdName'] = $hd->name ?? '';
- $user = $this->user;
- $post['bookName'] = $user->name ?? '';
- $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
- $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
- $post['bookMobile'] = $bookMobile;
- $post['payWay'] = 0;
- $shop = $this->shop;
- $params = [];
- $goodsData = $goodsInfo->attributes;
- $ret = \bizHd\goods\classes\GoodsClass::getFinalPrice($goodsData, $shop, $custom, $params);
- $goodsInfo->priceType = $ret['priceType'] ?? 0;
- if ($goodsInfo->priceType == 0) {
- util::fail('没有价格,不能下单哈');
- }
- $unitPrice = $ret['price'] ?? 0;
- if ($unitPrice <= 0) {
- util::fail('没有价格,不能下单呢');
- }
- //计算总金额
- $goodsPrice = $unitPrice * $goodsNum;
- //计算运费
- $sendDistance = 0;
- $sendCost = 0;
- if ($sendType == dict::getDict('sendType', 'thirdSend')) {
- $receiveUserName = $post['receiveUserName'] ?? '';
- if (empty($receiveUserName)) {
- util::fail('请填写收花人名字');
- }
- $receiveMobile = $post['receiveMobile'] ?? '';
- if (empty($receiveMobile)) {
- util::fail('请填写收花人手机号');
- }
- if (!stringUtil::isMobile($receiveMobile)) {
- util::fail('收花人手机号错误');
- }
- }
- $mapSet = ShopClass::hasIntraCity($shop);
- $openIntraCity = $mapSet['openIntraCity'] ?? 0;
- if ($sendType == dict::getDict('sendType', 'thirdSend') && $openIntraCity != 0) {
- // if (dict::getDict('hasMap') == 0) {
- // util::fail('不能使用跑腿,请选其它方式');
- // }
- // $lat = $post['lat'] ?? '';
- // $lng = $post['long'] ?? '';
- // if ((empty($lat) || empty($lng))) { // 当 hasMap 为1时,经纬数据是必要的
- // util::fail('请填写正确地址');
- // }
- // $address = $post['address'] ?? '';
- // $floor = $post['floor'] ?? '';
- // $fullAddress = $address . $floor;
- // $post['fullAddress'] = $fullAddress;
- // $shopLat = $shop->lat ?? '';
- // $shopLong = $shop->long ?? '';
- // if (empty($shopLat) || empty($shopLong)) {
- // util::fail('商家门店地址定位未设置,编号8966');
- // }
- // $weight = $goodsInfo->weight ?? 0;
- // if ($weight <= 0) {
- // util::fail('商品没有重量');
- // }
- // $totalWeight = bcmul($weight, $goodsNum, 2);
- // $cargoName = '鲜花'; // 商品名称
- // $cargo = [
- // 'cargo_name' => $cargoName, // 商品名称
- // 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
- // 'cargo_price' => intval($goodsPrice * 100), // 单位:分 -- 把元转换为分
- // 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
- // 'cargo_num' => $goodsNum
- // ];
- // $customName = $custom->name;
- // $customMobile = $custom->mobile;
- // if (empty($post['long']) || empty($post['lat'])) {
- // util::fail('位置错误');
- // }
- // $originalLng = doubleval($post['long']);
- // $originalLat = doubleval($post['lat']);
- // $city = $post['city'] ?? '';
- // $dist = $post['dist'] ?? '';
- // $address = $post['address'] ?? '';
- // $showAddress = $post['showAddress'] ?? '';
- // $userAddress = $city . $dist . $address . "({$showAddress})";
- // $main = $this->main;
- // $shopLat = $shop->lat ?? '';
- // $shopLong = $shop->long ?? '';
- // if (empty($shopLat) || empty($shopLong)) {
- // util::fail('商家门店地址定位未设置,编号6880');
- // }
- // $wxStoreId = $main->wxStoreId ?? 0;
- // if (empty($wxStoreId)) {
- // util::fail('商家还没有开通跑腿功能');
- // }
- // $orderData = [
- // 'wx_store_id' => $wxStoreId,
- // 'user_name' => $customName,
- // 'user_phone' => $customMobile,
- // 'user_lng' => $originalLng,
- // 'user_lat' => $originalLat,
- // 'user_address' => $userAddress,
- // 'cargo' => $cargo,
- // ];
- // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
- // $result = IntraCityExpress::preAddOrder($orderData);
- // $errCode = $result['errcode'] ?? 1;
- // if ($errCode != 0) {
- // $errMsg = $result['errmsg'] ?? '';
- // util::fail('获取运费错误:' . $errMsg);
- // }
- // $fee = $result['est_fee'] ?? 0;
- // $sendDistance = $result['distance'] ?? 0;
- // $sendCost = bcdiv($fee, 100, 2);
- // //免运费的不收运费
- // if ($goodsInfo->freightType == 1) {
- // $sendCost = 0;
- // }
- // noticeUtil::push("零售订单!获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$sendDistance} 姓名:{$customName} 手机号 {$customMobile} 经纬 {$originalLng} {$originalLat} 金额:{$goodsPrice} 数量:{$goodsNum}", '15280215347');
- if ($openIntraCity == 2) {
- util::fail('不能使用跑腿');
- }
- $cutomInfo = [
- 'name' => $post['receiveUserName'],
- 'mobile' => $post['receiveMobile'],
- 'fullAddress' => $post['address'],
- 'floor' => $post['floor'],
- 'dist' => $post[''] ?? '',
- 'lat' => $post['lat'],
- 'long' => $post['long'],
- 'address' => $post['address'],
- 'city' => $post['city'],
- ];
- $cutomInfo = (object)$cutomInfo;
- // 生成随机花束订单号
- $prefix = 'KD_CS-' . $this->mainId . '-';
- $orderSn = $prefix . round(microtime(true) * 1000);
- // 使用 DeliveryQuoteUtil 获取配送报价
- try {
- if ($goodsInfo->weight > 5) {
- $goodsInfo->weight = 5;
- }
- if (empty($post['deliveryPlatform'])) {
- util::fail('请选择跑腿');
- }
- $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
- 'productList' => [['bigNum' => $goodsNum, 'weight' => $goodsInfo->weight]],
- 'deliveryPlatform' => $post['deliveryPlatform'],
- 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
- 'ghsInfo' => ['mainId' => $mainId, 'shopId' => $this->shopId],
- 'custom' => $cutomInfo,
- 'order' => [
- 'orderSn' => $orderSn,
- 'goodsType' => 0, //商品类型:0花束 1花材
- 'itemTotalAmount' => $goodsPrice,
- 'remark' => $post['remark'] ?? '',
- 'freightType' => $goodsInfo->freightType,
- ],
- 'mainId' => $this->mainId,
- 'productCount' => $goodsNum,
- ]);
- $sendCost = $quoteResult['sendCost'];
- $sendDistance = $quoteResult['sendDistance'];
- noticeUtil::push("零售订单(花束),获取运费:{$sendCost} 重量:{$goodsInfo->weight} 距离:{$sendDistance}m 姓名:{$customName} 金额:{$goodsPrice} 数量:{$goodsNum}");
- } catch (\Exception $e) {
- util::fail($e->getMessage());
- }
- }
- $post['sendDistance'] = $sendDistance;
- $post['sendCost'] = $sendCost;
- $post['sjId'] = $this->sjId;
- $post['shopId'] = $this->shopId;
- $post['mainId'] = $mainId;
- $post['userId'] = $this->userId;
- if ($goodsInfo->priceType == 0) {
- util::fail('没有价格,不能下单');
- }
- $modifyPrice = stringUtil::calcAdd($sendCost, $goodsPrice);
- //非门店订单
- $post['store'] = 0;
- $post['modifyPrice'] = $modifyPrice;
- //非自取订单考虑配送时间
- $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
- $sendType = $post['sendType'] ?? 0;
- if ($sendType != 1) {
- if (empty($post['reachPeriod'])) {
- util::fail('请选择配送时间哈');
- }
- $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
- }
- $now = time();
- $expireTime = $now + 1800;//订单30分钟后过期
- $post['deadline'] = $expireTime;
- $orderSn = orderSn::getOrderSn();
- $post['orderSn'] = $orderSn;
- $post['fromType'] = dict::getDict('fromType', 'mall');
- $post['cash'] = 0;
- $post['mainId'] = $mainId;
- $post['needPrint'] = dict::getDict('needPrint', 'need');
- $hasPay = 0;
- $product = [['productId' => $goodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
- $post['product'] = $product;
- $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
- $orderId = $return->id;
- $orderSn = $return->orderSn;
- $actPrice = $return->actPrice ?? 0;
- $transaction->commit();
- util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => 0, 'id' => $orderId]);
- } catch (Exception $e) {
- $transaction->rollBack();
- Yii::info("失败原因:" . $e->getMessage());
- util::fail('下单失败');
- }
- }
- //下单要用到的相关信息 ssh 2019.12.6
- public function actionOrderRelate()
- {
- $regionTree = RegionService::tree();
- //门店名称
- $shop = !empty($this->shop) ? $this->shop->attributes : [];
- $sjName = $shop['merchantName'] ?? '';
- $shopName = $shop['shopName'] ?? '';
- $shop['showName'] = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
- $avatar = $shop['avatar'] ?? '';
- $smallAvatar = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $shop['smallAvatar'] = $smallAvatar;
- $freight = ['first' => 5, 'add' => 2];
- $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
- $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
- util::success($out);
- }
- //余额支付 ssh 20250410
- public function actionBalancePay()
- {
- $post = Yii::$app->request->post();
- $orderSn = $post['orderSn'] ?? '';
- //避免重复提交
- util::checkRepeatCommit($orderSn, 5);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order->shopId != $this->shopId) {
- util::fail('不是你的订单');
- }
- $payWay = dict::getDict('payWay', 'balancePay');
- \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
- $transaction->commit();
- //解决重复通知,重要
- $cacheKey = 'mall_order_balance_pay_' . $orderSn;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (empty($has)) {
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
- //打印小票和语音播报
- $order = \bizHd\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn], true);
- if (!empty($order)) {
- \bizHd\order\classes\OrderClass::onlinePrint($order);
- //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
- HdDeliveryOrderClass::payAfter($order);
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (in_array($order->fromType, [1, 3])) {
- //您有新的收入提醒
- WxMessageClass::gatheringIncomeInform($shop, $order);
- }
- if ($order->fromType == 2) {
- //来自商城的新订单微信通知
- WxMessageClass::hdNewOrderInform($shop, $order);
- }
- }
- }
- util::complete('支付成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::error("支付原因:" . $e->getMessage());
- util::fail('支付失败');
- }
- }
- //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
- public function actionWxPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $post = Yii::$app->request->post();
- $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
- $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
- $order = OrderService::getByOrderSn($orderSn);
- $orderId = $order['id'];
- //验证优惠券是否还有效
- if (!empty($couponId)) {
- CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
- }
- //支付前验证订单有效性
- OrderService::checkBeforePay($order);
- $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
- $shop = $this->shop;
- if (empty($shop)) {
- util::fail('没有找到门店信息');
- }
- if (isset($shop->shopName) && !empty($shop->shopName)) {
- $name .= '(' . $shop->shopName . ')';
- }
- $totalFee = $order['mainPay'];
- $openId = '';
- if (!empty($post['miniOpenId'])) {
- $openId = $post['miniOpenId'];
- //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
- }
- if (empty($openId)) {
- if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
- $openId = $this->user['miniOpenId'];
- //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
- }
- }
- if (empty($openId)) {
- util::fail('没有找到openId');
- }
- $now = time();
- $expireTime = $now + 1800;
- $updateData = [];
- if (isset($order['modPrice']) && $order['modPrice'] == 0) {
- $oldOrderSn = $orderSn;
- //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = [
- 'orderSn' => $oldOrderSn,
- ];
- $response = $laResource->close($closeParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $msg = $response['msg'] ?? '';
- $lklSjNo = $shop->lklSjNo ?? '';
- noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
- util::fail('出错了哦,请重新下一单');
- }
- $orderSn = orderSn::getOrderSn();
- $updateData['orderSn'] = $orderSn;
- OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
- } else {
- //已请求的不能再修改价格
- $updateData['modPrice'] = 0;
- }
- if (empty($order['deadline'])) {
- $updateData['deadline'] = $expireTime;
- }
- if (!empty($updateData)) {
- OrderService::updateById($orderId, $updateData);
- }
- $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
- $sjExtend = WxOpenClass::getMallWxInfo();
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
- $wxParams = [
- 'orderSn' => $orderSn,
- 'amount' => $totalFee,
- 'capitalType' => $capitalType,
- 'notifyUrl' => $notifyUrl,
- 'wxAppId' => $sjExtend['miniAppId'],
- 'openId' => $openId,
- 'subject' => $name,
- 'couponId' => $couponId,
- ];
- $response = $laResource->driveWxPay($wxParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push($errMsg, '15280215347');
- util::fail('支付失败');
- }
- $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
- $appId = $newParams['app_id'] ?? '';
- $nonceStr = $newParams['nonce_str'] ?? '';
- $paySign = $newParams['pay_sign'] ?? '';
- $package = $newParams['package'] ?? '';
- $signType = $newParams['sign_type'] ?? '';
- $timeStamp = $newParams['time_stamp'] ?? '';
- $new = [
- 'id' => $orderId,
- 'appId' => $appId,
- 'nonceStr' => $nonceStr,
- 'paySign' => $paySign,
- 'package' => $package,
- 'signType' => $signType,
- 'timeStamp' => $timeStamp,
- 'orderSn' => $orderSn,
- ];
- util::success($new);
- }
- //快捷付款订单
- public function actionFastPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $post = Yii::$app->request->post();
- $payWay = $post['payWay'] ?? 0;
- $shopId = $this->shopId;
- $shop = $this->shop;
- if (empty($shop)) {
- util::fail('没有门店信息');
- }
- $customId = $this->customId;
- $custom = $this->custom;
- $hdId = $this->hdId;
- $hd = $this->hd;
- if (empty($customId) || empty($hdId)) {
- //考虑没有登录时的付款
- $defaultCustomId = $shop->defaultCustomId ?? 0;
- if (empty($defaultCustomId)) {
- util::fail('请设置快捷开单的客户');
- }
- $customId = $defaultCustomId;
- $custom = CustomClass::getById($defaultCustomId, true);
- if (empty($custom)) {
- util::fail('无效的快捷客户');
- }
- $hdId = $custom->hdId;
- $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
- if (empty($hd)) {
- util::fail('没有花店信息呢!');
- }
- }
- $post['hdId'] = $hdId;
- $post['hdName'] = $hd->name ?? '';
- $customName = $custom->name ?? '';
- $customPy = $custom->py ?? '';
- $post['customName'] = $customName;
- $post['customNamePy'] = $customPy;
- $post['shopId'] = $shopId;
- $post['mainId'] = $this->mainId;
- $post['customId'] = $customId;
- $prePrice = round($post['prePrice'], 2);
- $actPrice = $prePrice;
- if ($actPrice <= 0) {
- util::fail('请填写付款金额');
- }
- $post['actPrice'] = $actPrice;
- $post['userId'] = $this->userId;
- $now = time();
- $expireTime = $now + 300;//订单5分钟后过期
- $post['createTime'] = date("Y-m-d H:i:s", $now);
- $post['deadline'] = date("Y-m-d H:i:s", $expireTime);
- $order = ScanPayClass::addOrder($post);
- $orderId = $order->id ?? 0;
- $orderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'scanPay', 'id');
- if ($payWay == 0) {
- //微信
- $name = '购买鲜花,扫码付款';
- $totalFee = $actPrice;
- $user = UserService::getById($this->userId);
- //小程序使用miniOpenId
- $openId = $user['miniOpenId'];
- if (empty($openId)) {
- $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
- }
- if (empty($openId)) {
- util::fail('没有微信信息');
- }
- $sjExtend = WxOpenClass::getMallWxInfo();
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
- $wxParams = [
- 'orderSn' => $orderSn,
- 'amount' => $totalFee,
- 'capitalType' => $capitalType,
- 'notifyUrl' => $notifyUrl,
- 'wxAppId' => $sjExtend['miniAppId'],
- 'openId' => $openId,
- 'subject' => $name,
- 'couponId' => 0,
- ];
- $response = $laResource->driveWxPay($wxParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push($errMsg, '15280215347');
- util::fail('支付失败');
- }
- $newParams = $response['resp_data']['acc_resp_fields'] ?? [];
- $appId = $newParams['app_id'] ?? '';
- $nonceStr = $newParams['nonce_str'] ?? '';
- $paySign = $newParams['pay_sign'] ?? '';
- $package = $newParams['package'] ?? '';
- $signType = $newParams['sign_type'] ?? '';
- $timeStamp = $newParams['time_stamp'] ?? '';
- $new = [
- 'id' => $orderId,
- 'appId' => $appId,
- 'nonceStr' => $nonceStr,
- 'paySign' => $paySign,
- 'package' => $package,
- 'signType' => $signType,
- 'timeStamp' => $timeStamp,
- 'orderSn' => $orderSn,
- ];
- util::success($new);
- } elseif ($payWay == 1) {
- //支付宝
- $totalFee = $actPrice;
- $subject = '购买鲜花,扫码付款';
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklB2BTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
- $cashParams = [
- 'orderSn' => $orderSn,
- 'amount' => $totalFee,
- 'capitalType' => $capitalType,
- 'notifyUrl' => $notifyUrl,
- 'subject' => $subject,
- ];
- $response = $laResource->cashierPay($cashParams);
- if (!isset($response['code']) || $response['code'] != '000000') {
- util::fail('支付失败了');
- }
- $url = $response['resp_data']['counter_url'] ?? '';
- if (empty($url)) {
- util::fail('支付失败');
- }
- util::success(['url' => $url]);
- } elseif ($payWay == 2) {
- //余额支付
- util::fail('暂不支持');
- } else {
- util::fail('无效的支付方式');
- }
- }
- //订单列表 ssh 2019.12.12
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $status = $get['status'] ?? 0;
- $userId = $this->userId;
- $where = ['userId' => $userId];
- if (!empty($status)) {
- $where['status'] = $status;
- }
- $list = OrderService::getOrderList($where);
- util::success($list);
- }
- //订单评价
- public function actionComment()
- {
- $post = Yii::$app->request->post();
- $id = $post['id'];
- $order = OrderClass::getById($id);
- if ($order['grade'] > 0) {
- util::fail('您已经评过了');
- }
- OrderService::comment($post);
- util::complete('提交成功');
- }
- //订单详情 ssh 2019.12.16
- public function actionDetail()
- {
- $id = Yii::$app->request->get('id', 0);
- $detail = OrderClass::getOrderById($id);
- $userId = $this->userId;
- if ($detail['userId'] != $userId) {
- util::fail('不是你的单,不能查看');
- }
- $shOrderMap = [];
- if ($detail['hasForward'] == 1) {
- $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
- }
- $detail['shOrderMap'] = $shOrderMap;
- util::success($detail);
- }
- //获取详情 ssh 20220512
- public function actionInfo()
- {
- $sn = Yii::$app->request->get('orderSn', '');
- $detail = OrderClass::getOrderBySn($sn);
- util::success($detail);
- }
- }
|