request->post(); $ghsId = $post['ghsId'] ?? 0; //供应商 $ghsInfo = GhsClass::getById($ghsId); if (empty($ghsInfo)) { util::fail('没有找到供应商'); } //增加判定,若是快递配置,则判定供应商是否开通达达,若未开通,则不能现在配送 $getType = $post['getType'] ?? 0; if ($getType == 1) { //配送 $ghsShopId = $ghsInfo['shopId'] ?? 0; $ghsSjId = $ghsInfo['sjId'] ?? 0; $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId); if (!$shopNo) { util::fail("商家还未开通配送服务"); } //计算运费, 先粗略限制,判定sendCost不能为空, 后续应该重新再算运费 if (!$post['sendCost']) { util::fail("请确认运费"); } // $post['sendCost'] = } $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { $currentShopId = $ghsInfo['shopId'] ?? 0; $post['sjId'] = $this->sjId; $post['shopId'] = $this->shopId; $post['shopAdminId'] = $this->shopAdminId; $post['ghsId'] = $ghsId; $post['ghsName'] = $ghsInfo['name'] ?? ''; $post['ghsMobile'] = $ghsInfo['mobile'] ?? ''; $post['ghsAvatar'] = $ghsInfo['avatar'] ?? ''; $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? ''; $customId = $ghsInfo['customId'] ?? 0; $post['customId'] = $customId; $shopAdmin = $this->shopAdmin->attributes; $name = $shopAdmin['name'] ?? ''; $post['shopAdminName'] = $name; $product = $post['product'] ?? ''; $productList = json_decode($product, true); //判断product数据是不是同个供应商的 ProductClass::valid($productList, $currentShopId); $post['product'] = $productList; $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0; $respond = PurchaseService::createPurchase($post, $ghsInfo); $orderSn = $respond->orderSn ?? ''; $actPrice = $respond->actPrice ?? ''; $realPrice = $respond->realPrice ?? ''; $transaction->commit(); $data = [ 'orderSn' => $orderSn, 'actPrice' => $actPrice, 'realPrice' => $realPrice, 'hasBalancePay' => 1, 'hasPayPassword' => 0, 'hasDebtPay' => 1, ]; util::success($data); } catch (Exception $e) { $transaction->rollBack(); util::fail(); } } //延期支付 ssh 2021.1.24 public function actionDebtPay() { $get = Yii::$app->request->get(); $orderSn = $get['orderSn'] ?? 0; $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true); PurchaseService::valid($info, $this->shopId); $ghsId = $info->ghsId; $ghs = GhsClass::getGhsInfo($ghsId); $customId = $ghs['customId'] ?? 0; $custom = CustomClass::getCustom($customId); if (empty($custom)) { util::fail('没有找到客户'); } if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) { util::fail('您没有延期付款权限'); } $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { //延期支付 PurchaseService::debt($info); $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); util::fail('支付失败'); } util::success($info->attributes); } //余额支付 ssh 2021.1.24 public function actionBalancePay() { $get = Yii::$app->request->get(); $orderSn = $get['orderSn'] ?? 0; $password = $get['password'] ?? ''; // 重新获取,不加密的用户数据 $admin = AdminClass::getById($this->adminId, true); if (password_verify($password, $admin->payPassword) == false) { util::fail('密码错误'); } $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true); if (empty($info)) { util::fail('没有找到采购单'); } PurchaseService::valid($info, $this->shopId); $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { //余额支付 purchaseService::balancePay($info); $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); util::fail('支付失败'); } util::success($info->attributes); } //采购记录 ssh 2021.1.24 public function actionList() { $get = Yii::$app->request->get(); $where = ['shopId' => $this->shopId]; $status = $get['status'] ?? 0; if (!empty($status)) { $where['status'] = $status; } $respond = PurchaseService::getPurchaseList($where); $respond['shop'] = $this->shop->attributes; util::success($respond); } //采购详情 ssh 2021.01.25 public function actionDetail() { $get = Yii::$app->request->get(); $id = $get['id'] ?? 0; $info = PurchaseService::getInfo($id, true, true); if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) { util::fail('没有权限访问'); } util::success($info); } //可用的支付方式 ssh 2021.1.25 public function actionPayWay() { $button = [ ['type' => 'wxPay', 'show' => 1, 'disable' => 1], ['type' => 'debtPay', 'show' => 1, 'disable' => 0], ['type' => 'balancePay', 'show' => 1, 'disable' => 0], ]; util::success(['button' => $button]); } //待结款明细 ssh 2021.1.26 public function actionDebtList() { $id = Yii::$app->request->get('id', 0); $info = GhsClass::getGhsInfo($id); GhsClass::valid($info, $this->shopId); $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES]; $respond = PurchaseService::getDebtList($where); util::success($respond); } //微信支付 shish 2021.4.11 public function actionWxPay() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $couponId = $post['couponId'] ?? 0; $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0; $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true); if (empty($order)) { util::fail('订单号无效'); } $id = $order->id; $order->changePrice = 2; $order->save(); $deadline = $order->deadline; $current = date("Y-m-d H:i:s"); if ((strtotime($deadline) + 20) < strtotime($current)) { util::fail('订单已经失效'); } $name = $orderSn; $totalFee = $order->realPrice; //强制使用小程序的miniOpenId $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : ''; if (empty($openId)) { util::fail('没有openId'); } $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id'); //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调 $wxPayType = 0; if (httpUtil::isMiniProgram()) { $wxPayType = 1; } $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType; //订单30分钟后过期 $now = time(); $expireTime = $now + 1800; $wx = Yii::getAlias("@vendor/weixin"); require_once($wx . '/lib/WxPay.Api.php'); require_once($wx . '/example/WxPay.JsApiPay.php'); $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetOut_trade_no($orderSn); $input->SetTotal_fee($totalFee * 100); $input->SetTime_start(date("YmdHis", $now)); //将流水类型、代金劵等传给微信再回传 $input->SetAttach($attach); $input->SetTime_expire(date("YmdHis", $expireTime)); $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/'); $input->SetTrade_type("JSAPI"); //花卉宝代为申请的微信支付 $merchantExtend = WxOpenClass::getWxInfo(); if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) { $input->SetSub_openid($openId); } else { $input->SetOpenid($openId); } //强制使用小程序的miniAppId $merchantExtend['wxAppId'] = $merchantExtend['miniAppId']; $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend); $newParams = json_decode($jsApiParameters, true); $newParams['id'] = $id; util::success($newParams); } //运费计算 linqh 2021.4.12 暂时返回固定值 public function actionFreight() { $post = Yii::$app->request->post(); $ghsId = $post['ghsId'] ?? 0; //供应商 $ghsInfo = GhsClass::getById($ghsId); if (empty($ghsInfo)) { util::fail('没有找到供应商'); } //花材信息 $productJson = $post['product'] ?? ''; // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]'; if (empty($productJson)) { util::fail('请选择花材'); } $productList = json_decode($productJson, true); if (empty($productList)) { util::fail('请选择花材'); } $productList = PurchaseClass::mergeItemInfo($productList); $weight = 0; $price = 0; $productData = ProductClass::getProductMapData($productList); $bigNum = 0; foreach ($productList as $key => $val) { $productId = $val['productId']; $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']); $bigNum += $val['bigNum']; $weight = bcadd($w, $weight, 2); $ratio = $productData[$productId]['ratio'] ?? 0; //大小数量合并多少扎 $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio); //售卖价格 $itemPrice = $productData[$productId]['price'] ?? 0; //合计价格 $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2); } $ghsShopId = $ghsInfo['shopId'] ?? 0; $ghsSjId = $ghsInfo['sjId'] ?? 0; $sendTime = $post['sendTime'] ?? ''; if (!empty($sendTime)) { //配送时间不为空 // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约 $sendTimeInt = strtotime($sendTime); $now = time(); $diff = $sendTimeInt - $now; //因服务器写入时间,改为至少提前6分钟 if ($diff <= 360) { util::fail('配送时间至少提前6分钟'); } $sendTime = date('Y-m-d H:i', strtotime($sendTime)); } else { $sendTime = date('Y-m-d H:i', strtotime("+30 minute")); } //找供应商的客户信息,用里面的配送相关地址信息 $customId = $ghsInfo['customId'] ?? 0; $customInfo = CustomClass::getById($customId); if (empty($customInfo)) { util::fail('没有找到客户'); } //判断商家是否开启达达,若未开启,则使用自定义运费计算 $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId); if (!$shopNo) { //使用自定义运费 //util::fail('供应商暂未开通配送'); $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight); util::success(['sedCost' => $cost]); } //组装订单信息 $info = []; //发送放的商家信息,即供应商的相关信息 $info['sjId'] = $ghsSjId; $info['shopId'] = $ghsShopId; $info['actPrice'] = $price;//价格 $info['weight'] = $weight; $info['orderSn'] = \common\components\orderSn::getGhsOrderSn(); $info['bigNum'] = $bigNum; //配送地址,当前零售端的门店地址 $info['customName'] = $customInfo['name'] ?? ''; $info['customMobile'] = $customInfo['mobile'] ?? ''; $info['province'] = $customInfo['province'] ?? ''; $info['city'] = $customInfo['city'] ?? '';; $info['address'] = $customInfo['address'] ?? '';; $info['floor'] = $customInfo['floor'] ?? '';; $info['fullAddress'] = $customInfo['fullAddress'] ?? '';; $info['lat'] = $customInfo['lat'] ?? '';; $info['long'] = $customInfo['long'] ?? '';; $info['sendTime'] = $sendTime; $res = DadaExpressServices::queryDeliverFee($info); util::success(['sedCost' => $res['fee']]); } }