diff --git a/app/common/logic/store_order/StoreOrderLogic.php b/app/common/logic/store_order/StoreOrderLogic.php index c80890eb7..9df2d5a1e 100644 --- a/app/common/logic/store_order/StoreOrderLogic.php +++ b/app/common/logic/store_order/StoreOrderLogic.php @@ -12,7 +12,9 @@ use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_order\StoreOrder; use app\common\model\store_order_cart_info\StoreOrderCartInfo; use app\common\model\store_product_unit\StoreProductUnit; +use app\common\model\user\User; use app\common\service\pay\PayService; +use app\common\service\SmsService; use Exception; use support\Cache; use think\facade\Db; @@ -116,9 +118,12 @@ class StoreOrderLogic extends BaseLogic 'original_price'=>self::$total_price, 'deduction_price' => self::$activity_price ]; - if($user['user_ship']!=4){ - $order['is_vip']=1; + if($user){ + if($user['user_ship']!=4){ + $order['is_vip']=1; + } } + } catch (\Exception $e) { self::setError($e->getMessage()); return false; @@ -308,4 +313,26 @@ class StoreOrderLogic extends BaseLogic } + + public function dealSendSms($param) + { + $code = generateRandomCode(); + $phone = User::where('id',$param['uid'])->value('mobile'); + if(empty($phone)){ + throw new \Exception('用户未设置手机号'); + } + $template = getenv('SMS_TEMPLATE'); + + $check =(new SmsService())->client($phone,$template,$code); + if($check){ + $remark = $param['uid'].'_smsPay'; + Cache::set($remark,$code,5*60); + return true; + }else{ + return false; + } + + } + + } diff --git a/app/common/service/SmsService.php b/app/common/service/SmsService.php index 43566a1cc..d1270b1a9 100644 --- a/app/common/service/SmsService.php +++ b/app/common/service/SmsService.php @@ -50,13 +50,18 @@ class SmsService try{ $easySms = new EasySms($this->config); - $easySms->send($phone, [ + $res = $easySms->send($phone, [ 'content' => '您的验证码为: '.$code, 'template' => $template, 'data' => [ 'code' => $code ], ]); + if($res && $res['aliyun']['status'] == 'success'){ + return true; + }else{ + return false; + } }catch(NoGatewayAvailableException $e){ throw new BusinessException($e->getMessage()); } diff --git a/app/functions.php b/app/functions.php index 2328a71b2..0aacbdc0e 100644 --- a/app/functions.php +++ b/app/functions.php @@ -405,6 +405,20 @@ if (!function_exists('haversineDistance')) { } } +/** + * 随机验证码 + */ +if (!function_exists('generateRandomCode')) { + function generateRandomCode($length = 4) { + $code = ''; + for ($i = 0; $i < $length; $i++) { + $code .= random_int(0, 9); + } + return $code; + } +} + + if (!function_exists('reset_index')) { /** * 重置数组索引 diff --git a/app/store/controller/cart/CartController.php b/app/store/controller/cart/CartController.php index e190a385b..1e3ae32f1 100644 --- a/app/store/controller/cart/CartController.php +++ b/app/store/controller/cart/CartController.php @@ -24,7 +24,7 @@ class CartController extends BaseAdminController { $params = (new CartValidate())->post()->goCheck('StoreAdd'); $adminInfo = $this->adminInfo; - $params['uid'] = 0; + $params['uid'] = $this->request->post('uid')??0; $params['staff_id'] = $adminInfo['admin_id']; $params['store_id'] = $adminInfo['store_id']; $result = Cart::where(['uid' => 0,'staff_id'=>$adminInfo['admin_id'], 'store_id' => $adminInfo['store_id'], 'product_id' => $params['product_id'], 'is_fail' => 0, 'is_pay' => 0])->find(); diff --git a/app/store/controller/store_order/StoreOrderController.php b/app/store/controller/store_order/StoreOrderController.php index 3ba2bc34e..01399ff7a 100644 --- a/app/store/controller/store_order/StoreOrderController.php +++ b/app/store/controller/store_order/StoreOrderController.php @@ -4,6 +4,8 @@ namespace app\store\controller\store_order; use app\api\logic\order\OrderLogic; use app\api\validate\OrderValidate; +use app\common\model\order\Cart; +use app\common\model\user\User; use app\store\lists\store_order\StoreOrderLists; use app\common\controller\Definitions; use app\common\enum\PayEnum; @@ -15,6 +17,7 @@ use app\common\logic\store_order\StoreOrderLogic; use app\common\model\store_order\StoreOrder; use app\common\model\user_recharge\UserRecharge; use app\store\validate\store_order\StoreOrderValidate; +use support\Cache; use support\Log; use Webman\RedisQueue\Redis; @@ -124,11 +127,36 @@ class StoreOrderController extends BaseAdminController return $this->data($res); } + + public function checkSms() + { + $params = (new StoreOrderValidate())->post()->goCheck('check'); + + $params['store_id'] = $this->request->adminInfo['store_id']; //当前登录的店铺id,用于判断是否是当前店铺的订单 + $user= User::where('id',$params['uid'])->find(); + if(empty($user)){ + return $this->fail('无该用户请检查'); + } + $order = StoreOrderLogic::cartIdByOrderInfo($params['cartId'], null, $user, $params); + if($order['order']['pay_price'] > $user['purchase_funds']){ + return $this->fail('当前用户采购款不足支付'); + } + $res = (new StoreOrderLogic())->dealSendSms($params); + if($res){ + return $this->success('发送成功',[],1,1); + }else{ + return $this->fail('发送失败'); + } + + } + + public function createOrder() { $cartId = (array)$this->request->post('cart_id', []); $pay_type = (int)$this->request->post('pay_type'); $addressId = (int)$this->request->post('address_id'); + $sms_code = $this->request->post('sms_code',''); $auth_code = $this->request->post('auth_code'); //微信支付条码 $params = $this->request->post(); if ($auth_code == '' && $pay_type != PayEnum::CASH_PAY) { @@ -137,6 +165,23 @@ class StoreOrderController extends BaseAdminController if (count($cartId) > 100) { return $this->fail('购物车商品不能超过100个'); } + + if($pay_type == PayEnum::PURCHASE_FUNDS && $sms_code){ + if(empty($cartId)){ + return $this->fail('缺失购物车数据'); + } + $uid = Cart::where('id',$cartId[0])->value('uid'); + $remark = $uid.'_smsPay'; + $code = Cache::get($remark); + if(empty($code)){ + return $this->fail('验证码过期请重新发送'); + } + if ($code !== $sms_code) { + return $this->fail('验证码错误'); + } + + } + $params['store_id'] = $this->request->adminInfo['store_id']; //当前登录的店铺id,用于判断是否是当前店铺的订单 $order = StoreOrderLogic::createOrder($cartId, $addressId, null, $params); if ($order != false) { diff --git a/app/store/validate/store_order/StoreOrderValidate.php b/app/store/validate/store_order/StoreOrderValidate.php index f7b4eeb81..928c46581 100644 --- a/app/store/validate/store_order/StoreOrderValidate.php +++ b/app/store/validate/store_order/StoreOrderValidate.php @@ -21,6 +21,8 @@ class StoreOrderValidate extends BaseValidate protected $rule = [ 'id' => 'requireWithout:verify_code', 'verify_code' => 'requireWithout:id', + 'uid' => 'require|number', + 'cart_id' => 'require|array', ]; @@ -81,4 +83,9 @@ class StoreOrderValidate extends BaseValidate return $this->only(['id','verify_code']); } + public function sceneCheck() + { + return $this->only(['uid','cart_id']); + } + }