From 9d83d8397ba1324817c3e41e0d35fd764dd96bd9 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Thu, 9 Dec 2021 13:36:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=85=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/api/Base.php | 2 + app/controller/api/Business.php | 138 +++++++++++++++++++++++--- app/controller/api/User.php | 2 + app/controller/manager/Business.php | 6 +- app/repository/RechargeRepository.php | 4 +- app/validate/BusinessValidate.php | 6 +- 6 files changed, 140 insertions(+), 18 deletions(-) diff --git a/app/controller/api/Base.php b/app/controller/api/Base.php index 76023c4..e72f6ab 100644 --- a/app/controller/api/Base.php +++ b/app/controller/api/Base.php @@ -3,6 +3,7 @@ namespace app\controller\api; use app\controller\BaseController; +use think\facade\Config; use think\response\Json; /** @@ -22,6 +23,7 @@ class Base extends BaseController 'jwt', 'apiLogin' => ['except' => $this->noNeedLogin] ]; + Config::load("extra/wechat","wechat"); } public function __call($method, $args) diff --git a/app/controller/api/Business.php b/app/controller/api/Business.php index b07c6cf..10b347c 100644 --- a/app/controller/api/Business.php +++ b/app/controller/api/Business.php @@ -4,15 +4,21 @@ namespace app\controller\api; use app\exception\RepositoryException; use app\model\BusinessCircle; use app\model\Category; +use app\model\Recharge as RechargeModel; use app\repository\AccountRepository; use app\repository\BusinessRepository; use app\repository\CouponRepository; use app\repository\DictionaryRepository; +use app\repository\RechargeRepository; +use app\service\wx\WechatPay; use app\validate\BusinessValidate; use think\Collection; +use think\Exception; +use think\facade\Db; use think\exception\ValidateException; -use app\model\{Business as BusinessModel, Account as AccountModel, CouponMain}; +use app\model\{Account, Business as BusinessModel, Account as AccountModel, CouponMain}; use think\response\Json; +use function EasyWeChat\Kernel\Support\generate_sign; /** * 商家端:商家 @@ -34,20 +40,21 @@ class Business extends Base public function registerBusiness() { $params = [ - 'type'=> $this->request->param('type/d', 0), + 'business_license'=> $this->request->param('business_license', ''), + 'background'=> $this->request->param('background', ''), 'business_name'=> $this->request->param('business_name', ''), 'business_subtitle'=> $this->request->param('business_subtitle', ''), - 'business_license'=> $this->request->param('business_license', ''), - 'contact_name'=> $this->request->param('contact_name', ''), - 'contact_phone'=> $this->request->param('contact_phone', ''), - 'lat'=> $this->request->param('lat', ''), - 'lng'=> $this->request->param('lng', ''), 'province'=> $this->request->param('province', ''), 'city'=> $this->request->param('city', ''), 'county'=> $this->request->param('county', ''), 'business_address' => $this->request->param('business_address', ''), + 'lat'=> $this->request->param('lat', ''), + 'lng'=> $this->request->param('lng', ''), 'business_circle_id' => $this->request->param('business_circle_id/d', 0), 'agency_code' => $this->request->param('agency_code', ''), + 'type'=> $this->request->param('type/d', 0), + 'contact_name'=> $this->request->param('contact_name', ''), + 'contact_phone'=> $this->request->param('contact_phone', ''), ]; $accountId = $this->request->user['user_id'] ?? 0; @@ -63,6 +70,9 @@ class Business extends Base if (empty($account)) { throw new ValidateException('无效请求!'); } + if ($account->type !== Account::type_consumer) { + throw new ValidateException('您已经是商家或者员工,不能认证'); + } if (!$validate->scene('apiRegister')->check($params)) { throw new ValidateException($validate->getError()); @@ -88,6 +98,8 @@ class Business extends Base } elseif ($agencyBusiness['enable'] == self::BOOL_TRUE) { throw new ValidateException('该代理商已被封禁!'); } + + $params['is_assign'] = 1; } $business = null; @@ -95,11 +107,14 @@ class Business extends Base $business = $busRepo->findOneByWhere(['code'=> $account['business_code']]); } - $params['create_time'] = date('Y-m-d H:i:s'); - $params['is_delete'] = 0; - $params['state'] = BusinessModel::state_reviewing; - $params['enable'] = 0; - $params['type_name'] = $businessCategory['name']; + $params['create_time'] = date('Y-m-d H:i:s'); + $params['is_delete'] = 0; + $params['state'] = BusinessModel::state_reviewing; + $params['enable'] = 0; + $params['total_recharge'] = 0; + $params['balance'] = 0; + $params['is_agency'] = 0; + $params['type_name'] = $businessCategory['name']; if ($business) { if ($business['state'] == BusinessModel::state_reviewing) { @@ -108,7 +123,7 @@ class Business extends Base // 更新审批信息,重新审批 $params['update_time'] = date('Y-m-d H:i:s'); - $business = $business->save($params); + $business->save($params); } else { // 添加审批记录 @@ -273,6 +288,103 @@ class Business extends Base return $this->json(0,"success",$data); } + + + /** + * 检查是有已经有商家审核了 + * */ + public function checkAuth() + { + $accountId = $this->request->user['user_id'] ?? 0; + $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + if(empty($account)){ + return $this->json(6001,"登录失效"); + } + + //如果有商家 并且审核失败或者 + if (isset($account->business) && !empty($account->business) && in_array($account->business->state, [BusinessModel::state_off, BusinessModel::state_reviewing,])) { + if ($account->business->state == BusinessModel::state_off) { + return $this->json(4001, "认证被退回,请重新填写资料:" . $account->business->reason,["business"=>$account->business]); + } + return $this->json(4001, " 正在认证中请耐心等待",["business"=>$account->business]); + } + + //检查有没有加入一个商家的申请 + //TODO.... + + return $this->json(); + } + + + /** + * 充值 + * */ + public function recharge() + { + $accountId = $this->request->user['user_id'] ?? 0; + $money = input("money/f",1); + $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + if(empty($account)||empty($account->business)){ + return $this->json(6001,"登录失效"); + } + if ($money <= 0) { + return $this->json(4001,"充值金额有误"); + } + $money = floor($money*100)/100; + Db::startTrans(); + try { + + //创建充值订单 + if (!$order = RechargeRepository::getInstance()->createOrder($account->business_code, $account->open_id,$money)) { + throw new RepositoryException('订单创建失败,请稍后重试'); + } + //生成支付 + $time = time(); + $res = WechatPay::getInstance()->order->unify([ + 'body' => '商家充值', + 'openid' => $account->open_id, + 'out_trade_no' => $order->order_num, + 'total_fee' => $money * 100, + 'trade_type' => 'JSAPI', + 'timeStamp' => $time, + 'product_id' => $order->id, + 'sign_type' => "MD5", + 'notify_url' => $this->request->domain() . "/api/recharge/notify.html", + ]); + + if (!isset($res['prepay_id'])) { + throw new RepositoryException('订单创建失败,请稍后重试'); + } + $payData = [ + "appId" =>config("wechat.applets_appId"), + "timeStamp" =>$time."", + 'nonceStr' => randomStr(0,16), + 'package' => 'prepay_id='.$res['prepay_id'], + "signType" =>'MD5', + ]; + + $payData['sign'] = generate_sign($payData, config("wechat.key")); + + Db::commit(); + return $this->json(0, "success", [ + "payData" => $payData + ]); + + }catch (RepositoryException $e){ + Db::rollback(); + return $this->json(4001,"发起充值失败"); + }catch (Exception $e){ + echo $e->getMessage(); + Db::rollback(); + return $this->json(5001,"发起充值失败"); + } + + } + } diff --git a/app/controller/api/User.php b/app/controller/api/User.php index bf48f84..b429a7b 100644 --- a/app/controller/api/User.php +++ b/app/controller/api/User.php @@ -162,6 +162,7 @@ class User extends Base 'balance' => $account['business']['balance'], 'enable' => $account['business']['enable'], ], + "alertMoney"=>config("wechat.balance") ]; return $this->json(0, 'success', $businessRes); @@ -174,6 +175,7 @@ class User extends Base 'userType' => $account['type'], 'userTypeDes' => Account::accountTypeDescList()[$account['type']] ?? '游客', 'couponCount' => $couponCount, + "alertMoney"=>config("wechat.balance") ]; diff --git a/app/controller/manager/Business.php b/app/controller/manager/Business.php index 94f7970..dd5c808 100644 --- a/app/controller/manager/Business.php +++ b/app/controller/manager/Business.php @@ -311,7 +311,11 @@ class Business extends Base Db::startTrans(); try { $business->save(["state" => $state, "reason" => $reason]); - $account->save(["type" => Account::type_business]); + //通过 就改变用户为商家 + if($state == BusinessModel::state_on){ + $account->save(["type" => Account::type_business]); + } + Db::commit(); return $this->json(); } catch (RepositoryException $e) { diff --git a/app/repository/RechargeRepository.php b/app/repository/RechargeRepository.php index 60ad429..ce64603 100644 --- a/app/repository/RechargeRepository.php +++ b/app/repository/RechargeRepository.php @@ -42,12 +42,14 @@ class RechargeRepository extends Repository /** * 创建一个支付订单 * @param $businessCode + * @param $openId * @param $money * @return Recharge|Model */ - public function createOrder($businessCode, $money) + public function createOrder($businessCode,$openId,$money) { $data = [ + "open_id" => $openId, "order_num" => createUuid(), "business_code" => $businessCode, "money" => $money, diff --git a/app/validate/BusinessValidate.php b/app/validate/BusinessValidate.php index f5661d7..995c18f 100644 --- a/app/validate/BusinessValidate.php +++ b/app/validate/BusinessValidate.php @@ -16,9 +16,9 @@ class BusinessValidate extends Validate 'lat' => 'require|between:-90,90', 'lng' => 'require|between:-180,180', - 'province|所属省份' => 'max:100', - 'city|所属城市' => 'max:100', - 'county|所属区县' => 'max:100', + 'province|所属省份' => 'require', + 'city|所属城市' => 'require', + 'county|所属区县' => 'require', 'business_address|商家地址' => 'require|max:250', 'characteristic|商家特色' => 'max:250',