| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- <?php
- namespace mobile\controllers;
- use biz\goods\services\CategoryRelateService;
- use biz\goods\services\GoodsUsageService;
- use biz\goods\services\UsageRelationService;
- use Yii;
- use common\components\configDict;
- use common\components\util;
- use common\services\xhCartService;
- use common\services\xhGoodsCategoryService;
- use common\services\xhGoodsService;
- use common\services\xhRecommendService;
- use common\services\xhUserAssetService;
- use common\services\xhCouponService;
- use common\services\xhCategoryService;
- use common\services\xhPayToolService;
- use common\services\xhOrderGoodsService;
- use common\services\xhOrderService;
- use common\services\xhGoodsPriceService;
- use common\services\xhMerchantExtendService;
- use common\services\xhUserService;
- use common\services\xhGoodsSettingService;
- class MobileController extends MobileendController
- {
-
- public $withoutLogin = ['index', 'goods-list', 'goods-detail', 'article', 'article-list', 'login', 'order-alipay', 'choose-pay', 'get-index-more','cert','cert-to'];
-
- public function actionIndex()
- {
- $slide = xhRecommendService::getSlide($this->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;
- }
- }
|