merchantId);//首页推荐图片 $list = xhGoodsService::getLimitGoods($this->merchantId); //首页分类菜单 shish 2019.8.3 Yii::$app->params['merchantId'] = $this->merchantId; $categoryData = CategoryRelateService::getShowList(8); $chunkData = array_chunk($categoryData, 4); $category1 = $chunkData[0]; $category2 = $chunkData[1]; return $this->render('index', ['list' => $list, 'imgList' => $slide, 'category1' => $category1, 'category2' => $category2]); } public function actionGetIndexMore() { $all = xhGoodsService::getAllGoodsIds($this->merchantId); $recommend = xhRecommendService::getGoodsIds($this->merchantId); $remain = array_diff($all, $recommend); if (empty($remain)) { util::successInfo(); } $list = []; foreach ($remain as $val) { $info = xhGoodsService::getById($val); $list[] = $info; } util::successInfo('操作成功', 'A0001', ['list' => $list]); } //分类与用途 modify shish 2019.8.31 public function actionGoodsList() { $get = Yii::$app->request->get(); $id = isset($get['id']) ? $get['id'] : 0; $type = isset($get['type']) ? $get['type'] : 'category'; $category = []; $merchantId = $this->merchantId; $categoryName = '全部宝贝'; if (!empty($id)) { if ($type == 'category') { $category = CategoryRelateService::getById($id); if (empty($category)) { util::stop('类型无效'); } $categoryName = $category['showName']; $list = xhGoodsCategoryService::getGoodsListByCId($id); if ($category['merchantId'] != $merchantId) { util::stop('非法访问'); } } elseif ($type == 'usage') { $usage = UsageRelationService::getById($id); if (empty($usage)) { util::stop('用途无效'); } if ($usage['merchantId'] != $merchantId) { util::stop('非法访问'); } $respond = GoodsUsageService::getGoodsList($id); $list = $respond['goodsList']; $info = $respond['info']; $categoryName = $info['showName']; $category = $info; $category['categoryName'] = $categoryName; } else { util::fail('无效类型'); } } else { $list = xhGoodsService::getAllGoodsList($this->merchantId); } $this->webTitle = $categoryName; return $this->render('goodsList', ['list' => $list, 'category' => $category, 'type' => $type]); } public function actionGoodsDetail() { $this->layout = 'goodsMain'; $get = Yii::$app->request->get(); $id = isset($get['id']) ? $get['id'] : 0; $goods = xhGoodsService::getById($id); if (empty($goods) || $goods['merchantId'] != $this->merchantId) { util::stop('访问出错了'); } if (isset($get['cId']) && $get['cId'] != 0) { $category = xhCategoryService::getById($get['cId']); $categoryName = $category['categoryName']; $this->webTitle = $categoryName . '-' . $this->webTitle; } $this->webTitle = $goods['goodsName'] . '-' . $this->webTitle; //多选价格 if ($goods['multiPrice'] != '') { $multiPriceStr = $goods['multiPrice']; $sortGoodsPriceArr = $goodsPriceArr = xhGoodsPriceService::getByIds($multiPriceStr, $id); $goods['goodsPriceArr'] = $goodsPriceArr; $sortGoodsPriceArr = \common\components\util::arraySequence($sortGoodsPriceArr, 'price'); $maxPrice = $sortGoodsPriceArr[0]['price']; $temp = end($sortGoodsPriceArr); $minPrice = $temp['price']; $goods['price'] = $minPrice . '-' . $maxPrice; } return $this->render('goodsDetail', ['goods' => $goods, 'merchant' => $this->merchant]); } public function actionGoodsOrder() { $this->layout = 'goodsMain'; $post = Yii::$app->request->post(); //dump($post);die; if (isset($post['goodsInfo']) == false || empty($post['goodsInfo'])) { util::stop('没有商品信息'); } if (isset($post['sourceType']) == false) { util::stop('未知来源,不确定来自购物车还是单个商品'); } if (isset($post['sourceType']) && $post['sourceType'] == 'cart') { $post['sourceType'] = 0;//商城订单 } if (!is_numeric($post['sourceType'])) { util::stop('订单来源参数类型出错'); } $sourceType = $post['sourceType']; $goodsList = []; $totalPrice = 0; foreach ($post['goodsInfo'] as $key => $val) { $multiPriceId = 0; if (isset($val['check']) && !empty($val['check'])) { $goods = xhGoodsService::getById($val['goodsId']); if (isset($val['multiPriceId']) && $val['multiPriceId'] > 0) {//商品是否 多选价格 $multiPriceId = $val['multiPriceId']; $goodPrice = xhGoodsPriceService::getById($multiPriceId); //$goods['price'] = $goodPrice['price']; $goods['price'] = xhGoodsSettingService::changePrice($goods, $goodPrice['price']);//统一对商品进行价格等方面设置 $goods['subGoodsName'] = $goodPrice['title']; } $totalPrice += $val['num'] * $goods['price']; $goodsList[] = ['goods' => $goods, 'goodsId' => $val['goodsId'], 'num' => $val['num'], 'multiPriceId' => $multiPriceId]; } } if (empty($goodsList)) { return; } $userId = $this->userId; $userAsset = xhUserAssetService::getByUserId($userId); $discountPrice = $totalPrice; $discount = 0;//打几折 $level = $userAsset['memberLevel']; if ($level > 0) {//如果是会员 $gradeList = xhMerchantExtendService::getGradeList($this->merchantExtend); $discount = $gradeList[$level]['discount']; $discount = strlen($discount) == 1 ? $discount * 10 : $discount; $discountPrice = number_format($discount * $totalPrice / 100, 2); } $this->webTitle = '确认订单';//标题 $freight = configDict::getConfig('freight');//运费计算 $coupon = xhCouponService::getUserValidCoupon($userId, $discountPrice); return $this->render('goodsOrder', [ 'user' => $this->user, 'goodsInfo' => $goodsList, 'totalPrice' => $totalPrice, 'userAsset' => $userAsset, 'discount' => $discount, 'discountPrice' => $discountPrice, 'sourceType' => $sourceType, 'merchant' => $this->merchant, 'freight' => $freight, 'coupon' => $coupon, 'extend' => $this->merchantExtend, ]); } /** * 订单提交 */ public function actionCreateOrder() { $post = Yii::$app->request->post(); //dump($post);die; $userId = $this->userId; $post['userId'] = $userId; $post['payWay'] = $this->isWeixin == true ? 0 : 1; $lat2 = $post['receiveLat'];//收货人纬度 $lng2 = $post['receiveLong'];//收货人经度 $lat1 = $this->merchant['shopLat'];//花店纬度 $lng1 = $this->merchant['shopLong'];//花店经度 $calcDistance = util::getDistance($lat1, $lng1, $lat2, $lng2);//防止别人改动,PHP计算的距离会比腾讯js算的小,但能保证有一定准确性 $post['sendDistance'] = $post['sendDistance'] >= $calcDistance ? $post['sendDistance'] : $calcDistance;//二地距离 $sendDistance = $post['sendDistance']; $freight = configDict::getConfig('freight'); $firstDistance = $freight['firstDistance']; $firstPrice = $freight['firstPrice']; $nextDistance = $freight['nextDistance']; $nextPrice = $freight['nextPrice']; if ($sendDistance <= $firstDistance && $post['isCharge'] == 1) { $post['sendCost'] = $firstPrice; } else if ($post['isCharge'] == 1) { $subDistance = $sendDistance - $firstDistance; $addPrice = intval($subDistance / $nextDistance) * $nextPrice; $post['sendCost'] = $firstPrice + $addPrice; } if (empty($this->merchantExtend['payment'])) { util::failInfo('支付功能未开通,暂时无法购买哦~'); } $couponAmount = 0; $couponId = isset($post['couponId']) ? $post['couponId'] : 0;//代金劵 if (!empty($couponId)) { $coupon = xhCouponService::getById($couponId); $couponUserId = $coupon['userId']; if ($couponUserId != $userId) { util::failInfo('不是你的代金劵'); } if ($coupon['useStatus'] == 1 || $coupon['deadline'] < time()) { util::failInfo('代金劵已经失效了'); } $couponAmount = $coupon['amount']; } //不要将代金劵保存到订单表,付款成功后再保存进去!!! unset($post['couponId']); $post['merchantId'] = $this->merchantId; //店铺id $post['shopId'] = $this->merchant['defaultShopId']; $order = xhOrderService::add($post);//创建订单 if ($order == false) { util::failInfo('创建订单失败'); } $orderId = $order['id']; $oData = []; $goodsInfo = $post['goodsInfo']; $totalFee = 0;//计算总价格 $goodsIdList = []; $multiPriceIdList = []; $orderName = ''; $goodsNum = 0; foreach ($goodsInfo as $key => $val) { $multiPriceId = 0;//多种价格表Id $goodsId = $val['goodsId']; $num = $val['num']; $goodsNum += $num; $goods = xhGoodsService::getById($goodsId); if (empty($orderName)) { $orderName = $goods['goodsName']; } $price = $goods['price']; if ($val['multiPriceId'] != 0 && $goods['multiPrice'] != '') { $pos = strpos($goods['multiPrice'], $val['multiPriceId']); if ($pos === false) { util::failInfo('订单的商品不存在'); } $goodPrice = xhGoodsPriceService::getById($val['multiPriceId']); //$price = $goodPrice['price']; $price = xhGoodsSettingService::changePrice($goods, $goodPrice['price']);//统一对商品进行价格等方面设置 //多种价格规格 替换 默认(单价格) $goods['goodsName'] = $goodPrice['title']; //$goods['cover'] = $goodPrice['picture'];//后台图片上传功能未开发,暂时使用默认商品图片 $multiPriceId = $val['multiPriceId']; } $totalFee += $price * $num; $goodsIdList[] = $goodsId; $multiPriceIdList[] = $multiPriceId; $data = [ 'orderId' => $orderId, 'goodsId' => $goodsId, 'userId' => $userId, 'merchantId' => $this->merchantId, 'title' => $goods['goodsName'], 'cover' => $goods['cover'], 'unitPrice' => $price, 'num' => $num, 'multiPriceId' => $multiPriceId, 'createTime' => date("Y-m-d H:i:s"), ]; xhOrderGoodsService::add($data);//创建订单商品列表 } $sendCost = $post['sendCost'];//运费 $oData['goodsNum'] = $goodsNum;//订单的商品数量类型,单个还是多个的 $oData['prePrice'] = $totalFee + $sendCost; $oData['orderName'] = $goodsNum > 1 ? $orderName . '等' . $goodsNum . '件商品' : $orderName; if (isset($post['sourceType']) && $post['sourceType'] == 'cart') { if (!empty($goodsIdList)) { foreach ($goodsIdList as $key => $goodsId) { xhCartService::delByIds($userId, $goodsId, $multiPriceIdList[$key]);//删除购物车($userId, $goodsId, $goodsPriceId, $cartId) } } } $userAsset = xhUserAssetService::getByUserId($userId); $level = $userAsset['memberLevel']; if ($level > 0) { $gradeList = xhMerchantExtendService::getGradeList($this->merchantExtend); $discount = $gradeList[$level]['discount']; $discount = strlen($discount) == 1 ? $discount * 10 : $discount; $totalFee = number_format($discount * $totalFee / 100, 2); } $oData['actPrice'] = $totalFee + $sendCost - $couponAmount; xhOrderService::updateById($orderId, $oData); util::successInfo('订单创建成功', 'A0001', ['orderId' => $orderId, 'couponId' => $couponId]); } public function actionChoosePay() { header('content-type:text/html;charset=utf-8');//支付宝、微信需要 $this->layout = 'goodsMain'; $this->webTitle = '支付订单';//标题 $get = Yii::$app->request->get(); $orderId = isset($get['orderId']) ? $get['orderId'] : 0; $order = xhOrderService::getById($orderId); if ($order['merchantId'] != $this->merchantId) { util::stop('非法访问'); } $waitForPay = configDict::getConfig('payStatus', 'waitForPay'); if ($order['payStatus'] != $waitForPay) { if ($this->isWeixin) { $this->redirect(['success', 'account' => $this->merchant['id'], 'orderId' => $orderId]); } else { echo '已经支付成功'; } util::end(); } $userId = $order['userId']; $user = xhUserService::getById($userId); $totalFee = $order['actPrice']; $couponId = isset($get['couponId']) ? $get['couponId'] : 0; $asset = xhUserAssetService::getByUserId($userId); return $this->render('choosePay', ['orderId' => $orderId, 'asset' => $asset, 'couponId' => $couponId, 'totalFee' => $totalFee, 'user' => $user]); } /** * 获取微信支付的参数 */ public function actionGetWxParams() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $couponId = isset($post['couponId']) ? $post['couponId'] : 0; $now = time(); $expireTime = $now + 1800;//订单30分钟后过期 if ($this->merchantExtend['payment'] == 0) { util::failInfo('还没开通支付功能哟~'); } $orderId = isset($post['orderId']) ? $post['orderId'] : 0; $order = xhOrderService::getById($orderId); if (!empty($couponId)) { $coupon = xhCouponService::getById($couponId); if ($coupon['useStatus'] == 1 || $now > $coupon['deadline']) { util::failInfo('代金劵已经失效'); } if ($coupon['meetAmount'] > $order['prePrice']) { util::failInfo('消费金额不足' . $coupon['meetAmount'] . '元'); } } if ($order['userId'] != 0 && $order['userId'] != $this->userId) {//当 快速下单时,字段userId为0(未保存指定用户);用户操作后,就只能该用户访问 util::failInfo('非法访问'); } if (!empty($order['deadline']) && $now > $order['deadline']) { util::failInfo('订单已经过期'); } if (ceil($order['actPrice']) <= 0) { util::failInfo('消费金额小于(等于)0'); } $name = $order['orderName']; $totalFee = $order['actPrice']; $openId = $this->user['openId']; $typeList = configDict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType;//将流水类型、代金劵传过去 $weixin = Yii::getAlias("@vendor/weixin"); require_once($weixin . '/lib/WxPay.Api.php'); require_once($weixin . '/example/WxPay.JsApiPay.php'); $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetOut_trade_no($orderId); $input->SetTotal_fee($totalFee * 100); $input->SetTime_start(date("YmdHis", $now)); $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传 $input->SetTime_expire(date("YmdHis", $expireTime)); $input->SetNotify_url($this->frontUrl . '/notice/weixin-callback/'); $input->SetTrade_type("JSAPI"); //服务商 代设置微信支付,要添加的参数设置 if ($this->merchantExtend['generalMerchant'] == 1) { //$input->SetOpenid($openId); $input->SetSub_openid($openId); //自设置 微信支付 } else { $input->SetOpenid($openId); } $wxOrder = \WxPayApi::unifiedOrder($input, 6, $this->merchantExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $this->merchantExtend); $newParams = json_decode($jsApiParameters, true); $updateData = ['modPriceStatus' => 1];//已经请求微信不能修改价格 if (empty($order['deadline'])) { $updateData['deadline'] = $expireTime; } xhOrderService::updateById($orderId, $updateData);//已经请求微信不能修改价格 util::successInfo('操作成功', 'A0001', $newParams); } /** * 订单 - 支付宝支付 */ public function actionOrderAlipay() { header("Content-type: text/html; charset=utf-8"); $alipayWap = Yii::getAlias("@vendor/alipayWap"); require_once($alipayWap . '/wappay/service/AlipayTradeService.php'); require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php'); require_once($alipayWap . '/config.php'); $get = Yii::$app->request->get(); $orderId = isset($get['orderId']) ? $get['orderId'] : 0; $couponId = isset($get['couponId']) ? $get['couponId'] : 0; $orderData = xhOrderService::getById($orderId); $waitForPay = configDict::getConfig('payStatus', 'waitForPay'); if ($orderData['payStatus'] != $waitForPay) { echo '已经支付成功'; util::end(); } $merchantId = $orderData['merchantId']; $out_trade_no = $orderId;//商户订单号,商户网站订单系统中唯一订单号,必填 $subject = $orderData['orderName'];//订单名称,必填 $total_amount = $orderData['actPrice'];;//付款金额,必填 $body = '';//商品描述,可空 $timeout_express = "1m";//超时时间 $typeList = configDict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、代金劵传过去 $passbackParams = urlencode($passbackParams); $payRequestBuilder = new \AlipayTradeWapPayContentBuilder(); $payRequestBuilder->setBody($body); $payRequestBuilder->setSubject($subject); $payRequestBuilder->setOutTradeNo($out_trade_no); $payRequestBuilder->setTotalAmount($total_amount); $payRequestBuilder->setTimeExpress($timeout_express); $payRequestBuilder->setPassbackParams($passbackParams);//在异步通知时将该参数原样返回 $returnUrl = Yii::$app->params['frontUrl'] . "/notice/order-alipay-sync"; $payResponse = new \AlipayTradeService($config); $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $config['notify_url']); $this->renderPartial('alipay', ['result' => $result]); } public function actionBalancePay() { $post = Yii::$app->request->post(); $orderId = isset($post['orderId']) ? $post['orderId'] : 0; $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0; $couponId = isset($post['couponId']) ? $post['couponId'] : 0; $order = xhOrderService::getById($orderId); if ($order['merchantId'] != $this->merchantId || $order['userId'] != $this->userId) { util::failInfo('非法访问'); } $actPrice = $order['actPrice'];//不包括打折时需要付款金额 if (!empty($couponId)) { $coupon = xhCouponService::getById($couponId); if ($coupon['useStatus'] == 1 || $now > $coupon['deadline']) { util::failInfo('代金劵已经失效'); } if ($coupon['meetAmount'] > $actPrice) { util::failInfo('消费不足' . $coupon['meetAmount'] . '元'); } } if (empty($this->user['payPassword'])) { util::failInfo('您还没有设置支付密码'); } if (md5($payPassword) != $this->user['payPassword']) { util::failInfo('支付密码不正确'); } $typeList = configDict::getConfig('capitalType'); $capitalType = $typeList['xhOrder']['id']; $totalFee = $order['actPrice']; $return = xhPayToolService::balancePay($orderId, $totalFee, $capitalType, $couponId); if ($return['code'] == 'A0002') { util::failInfo($return['msg']); } echo json_encode($return); } public function actionSuccess() { $this->layout = 'goodsMain'; $get = Yii::$app->request->get(); $orderId = isset($get['orderId']) ? $get['orderId'] : 0; $this->webTitle = '支付成功';//标题 return $this->render('success', ['orderId' => $orderId]); } public function actionLogin() { return $this->render('login'); } //国兰证书课跳转临时使用 shish public function actionCert() { return $this->render('cert'); } public function actionCertTo() { header('Location: http://m.fjglhs.com/main/cert'); exit; } }