// +---------------------------------------------------------------------- namespace app\controller\api\store\merchant; use app\common\model\system\merchant\Merchant; use app\common\repositories\system\merchant\MerchantAdminRepository; use app\common\repositories\system\merchant\MerchantCategoryRepository; use app\common\repositories\system\merchant\MerchantRepository; use app\common\repositories\system\merchant\MerchantTypeRepository; use app\common\repositories\system\CacheRepository; use app\validate\api\MerchantIntentionValidate; use crmeb\services\SmsService; use crmeb\services\SwooleTaskService; use crmeb\services\YunxinSmsService; use think\App; use think\facade\Db; use think\facade\Log; use crmeb\basic\BaseController; use app\common\repositories\system\merchant\MerchantIntentionRepository as repository; use think\exception\ValidateException; class MerchantIntention extends BaseController { protected $repository; protected $userInfo; public function __construct(App $app, repository $repository) { parent::__construct($app); $this->repository = $repository; $this->userInfo = $this->request->isLogin() ? $this->request->userInfo() : null; } /** * 提交入驻申请 */ public function create() { $data = $this->checkParams(); if (!systemConfig('mer_intention_open')) { return app('json')->fail('未开启商户入驻'); } if ($this->userInfo) $data['uid'] = $this->userInfo->uid; if (($this->userInfo->phone ?? false) && ($this->userInfo->phone != ($data['phone'] ?? ''))) { throw new ValidateException('联系电话和注册手机不一致'); } $intentionInfo = Db::name('merchant_intention')->where('social_credit_code', $data['social_credit_code'])->where('status', '<>', 2)->find(); if ($intentionInfo) { throw new ValidateException('此统一社会信用代码已申请商户'); } $merInfo = Db::name('merchant_intention')->where('uid', $this->userInfo->uid)->where('status', 'in',[0,1])->where('is_del',0)->find(); if ($merInfo) { throw new ValidateException('该用户已申请商户,不可重复申请'); } $make = app()->make(MerchantRepository::class); if ($make->fieldExists('mer_name', $data['mer_name'])) throw new ValidateException('商户名称已存在,不可申请'); if ($make->fieldExists('mer_phone', $data['phone'])) throw new ValidateException('手机号已存在,不可申请'); $adminRepository = app()->make(MerchantAdminRepository::class); if ($adminRepository->fieldExists('account', $data['phone'])) throw new ValidateException('手机号已是管理员,不可申请'); $intention = $this->repository->create($data); SwooleTaskService::admin('notice', [ 'type' => 'new_intention', 'data' => [ 'title' => '商户入驻申请', 'message' => '您有一个新的商户入驻申请', 'id' => $intention->mer_intention_id ] ]); return app('json')->success('提交成功'); } /** * 个人提交入驻申请 */ public function PersonalStore() { $data = $this->request->params([ 'phone', 'mer_name', 'company_name', 'address', 'name', 'code', 'images', 'social_credit_code', 'area_id', 'street_id', 'village_id', 'is_nmsc', 'is_company', 'financial_bank' ]); if (!empty($data['financial_bank'])) { $data['financial_bank'] = json_encode($data['financial_bank']); } if (!systemConfig('mer_intention_open')) { return app('json')->fail('未开启商户入驻'); } if ($this->userInfo) $data['uid'] = $this->userInfo->uid; if (($this->userInfo->phone ?? false) && ($this->userInfo->phone != ($data['phone'] ?? ''))) { throw new ValidateException('联系电话和注册手机不一致'); } $merInfo = Db::name('merchant_intention')->where('uid', $this->userInfo->uid)->where('status', 'in',[0,1])->where('is_del',0)->find(); if ($merInfo) { throw new ValidateException('该用户已申请商户,不可重复申请'); } $make = app()->make(MerchantRepository::class); if ($make->fieldExists('mer_name', $data['mer_name'])) throw new ValidateException('商户名称已存在,不可申请'); if ($make->fieldExists('mer_phone', $data['phone'])) throw new ValidateException('手机号已存在,不可申请'); $adminRepository = app()->make(MerchantAdminRepository::class); if ($adminRepository->fieldExists('account', $data['phone'])) throw new ValidateException('手机号已是管理员,不可申请'); $mer_type_id=Db::name('merchant_type')->where('type_code','PersonalStore')->value('mer_type_id'); $data['status']=0; $data['type']=2; $data['mer_type_id']=$mer_type_id; $data['merchant_category_id']=2584; $intention = $this->repository->create($data); if($intention){ $datas['fail_msg']=''; $datas['create_mer']=1; $datas['status']=1; $this->repository->updateStatus($intention->mer_intention_id, $datas); return app('json')->success('开户成功'); }else{ return app('json')->fail('开户失败'); } } public function settlementApply() { $status = $this->request->post('status', 0); if ($this->userInfo) $data['uid'] = $this->userInfo->uid; if ($status == 1) { Db::name('merchant')->where('uid', $this->userInfo->uid)->update(['mer_settlement_agree_status' => 1]); } return app('json')->success('操作成功'); } public function businessApply() { $data = $this->request->params([ 'phone', 'mer_name', 'company_name', 'address', 'name', 'code', 'images', 'merchant_category_id', 'mer_type_id', 'social_credit_code', 'area_id', 'street_id', 'village_id', 'is_nmsc', 'bank_username', 'bank_code', 'bank_opening', 'bank_front', 'bank_back', 'cardno_front', 'cardno_back', ]); if (empty($data['bank_username']) || empty($data['bank_opening'])||empty($data['bank_code'])) { return app('json')->fail('请完善银行卡及身份信息'); } if (!systemConfig('mer_intention_open')) { return app('json')->fail('未开启商户入驻'); } if ($this->userInfo) $data['uid'] = $this->userInfo->uid; $settleIn = $this->repository->getWhere(['type' => 1, 'uid' => $data['uid'], 'is_del' => 0]); $merchant = Merchant::where('uid', $data['uid'])->where('is_del', 0)->find(); if (empty($settleIn) && empty($merchant)) { return app('json')->fail('请申请商户入驻申请!'); } if (!empty($settleIn) && $settleIn['status'] == 0) { return app('json')->fail('请等待商户入驻申请审核!'); } $merIntentionInfo = $this->repository->getWhere(['type' => 2, 'uid' => $data['uid'], 'is_del' => 0]); if (!empty($merIntentionInfo) && ($merIntentionInfo['status'] == 0)) { return app('json')->fail('商户交易已申请,正在审核中!'); } if (!empty($merIntentionInfo) && ($merIntentionInfo['status'] == 1)) { return app('json')->fail('商户交易申请已通过'); } $intenInfo['phone'] = $merchant['mer_phone']; $intenInfo['mer_name'] = $merchant['mer_name']; $intenInfo['company_name'] = $merchant['mer_name']; $intenInfo['name'] = $merchant['real_name']; $intenInfo['social_credit_code'] = ''; $intenInfo['images'] = []; $intenInfo['merchant_category_id'] = $merchant['category_id']; $intenInfo['mer_type_id'] = $merchant['type_id']; $intenInfo['area_id'] = $merchant['area_id']; $intenInfo['street_id'] = $merchant['street_id']; $intenInfo['village_id'] = $merchant['village_id']; $intenInfo['is_nmsc'] = $merchant['is_nmsc']; $intenInfo['address'] = $merchant['mer_address']; $intenInfo['bank_username'] = $data['bank_username']; $intenInfo['bank_opening'] = $data['bank_opening']; $intenInfo['bank_code'] = $data['bank_code']; $intenInfo['bank_front'] = $data['bank_front']; $intenInfo['bank_back'] = $data['bank_back']; $intenInfo['cardno_front'] = $data['cardno_front']; $intenInfo['cardno_back'] = $data['cardno_back']; $intenInfo['uid'] = $data['uid']; $intenInfo['type'] = 2; $intenInfo['status'] = 0; $intenInfo['create_time'] = date('Y-m-d H:i:s'); $this->repository->create($intenInfo); return app('json')->success('申请成功'); } //发送商户申请 public function sendMerIntentionApply($data, $postUrl) { Log::info("商户申请URL: {$postUrl}"); Log::info("发送商户申请信息: " . json_encode($data)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $postUrl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $resData = curl_exec($ch); curl_close($ch); $rtnData = [ 'ok' => false, 'msg' => '' ]; if (!empty($resData) && is_string($resData)) { Log::info("商户申请反馈信息" . $resData); $resInfo = json_decode($resData, true); if (!empty($resInfo['code']) && $resInfo['code'] == 1) { $rtnData['ok'] = true; } else { $rtnData['msg'] = $resInfo['msg']; } } return $rtnData; } public function update($id) { $merIntentionInfo = $this->repository->getWhere(['mer_intention_id' => (int)$id, 'uid' => $this->userInfo->uid, 'is_del' => 0]); if (!$merIntentionInfo) return app('json')->fail('数据不存在'); if ($merIntentionInfo['status'] == 1) { return app('json')->fail('已通过审核数据不能修改'); } $data = $this->checkParams(); if (!systemConfig('mer_intention_open')) { return app('json')->fail('未开启商户入驻'); } if (($this->userInfo->phone ?? false) && ($this->userInfo->phone != ($data['phone'] ?? ''))) { throw new ValidateException('联系电话和注册手机不一致'); } $intentionInfo = Db::name('merchant_intention')->where('social_credit_code', $data['social_credit_code'])->where('status', '<>', 2)->find(); if ($intentionInfo) { throw new ValidateException('此统一社会信用代码已申请商户'); } if (!empty($data['mer_phone'])) { unset($data['mer_phone']); } $make = app()->make(MerchantRepository::class); if ($make->fieldExists('mer_name', $data['mer_name'])) throw new ValidateException('商户名称已存在,不可申请'); $adminRepository = app()->make(MerchantAdminRepository::class); if ($adminRepository->fieldExists('account', $data['phone'])) throw new ValidateException('手机号已是管理员,不可申请'); $data['create_time'] = date('Y-m-d H:i:s', time()); $updateIntention=$this->repository->updateIntention((int)$id, $data); SwooleTaskService::admin('notice', [ 'type' => 'new_intention', 'data' => [ 'title' => '商户入驻申请', 'message' => '您有一个新的商户入驻申请', 'id' => $id ] ]); if($updateIntention){ return app('json')->success('修改成功'); }else{ return app('json')->success('修改失败'); } } public function lst() { [$page, $limit] = $this->getPage(); $data = $this->repository->getList(['uid' => $this->userInfo->uid,'type'=>[1,2]], $page, $limit); return app('json')->success($data); } function detail($id) { $data = $this->repository->detail((int)$id, $this->userInfo->uid); if (!$data) { return app('json')->fail('数据不存在'); } if ($data->status == 1) { $data['login_url'] = rtrim(systemConfig('site_url'), '/') . '/' . config('admin.merchant_prefix'); } return app('json')->success($data); } protected function checkParams() { $data = $this->request->params([ 'phone', 'mer_name', 'company_name', 'address', 'name', 'code', 'images', 'mer_type_id', 'merchant_category_id', 'social_credit_code', 'area_id', 'street_id', 'village_id', 'is_nmsc', 'is_company', 'bank_username', 'bank_code', 'bank_opening', 'bank_front', 'bank_back', 'cardno_front', 'cardno_back', 'financial_bank', ]); if (!empty($data['financial_bank'])) { $data['financial_bank'] = json_encode($data['financial_bank']); } app()->make(MerchantIntentionValidate::class)->check($data); $check = app()->make(SmsService::class)->checkSmsCode($data['phone'], $data['code'], 'intention'); $data['mer_type_id'] = (int)$data['mer_type_id']; if (!env('APP_DEBUG')) { if (!$check) throw new ValidateException('验证码不正确'); } // if (!app()->make(MerchantCategoryRepository::class)->get($data['merchant_category_id'])) throw new ValidateException('商户分类不存在'); if ($data['mer_type_id'] && !app()->make(MerchantTypeRepository::class)->exists($data['mer_type_id'])) { throw new ValidateException('店铺类型不存在'); } // if ($data['merchant_category_name'] == '') { // throw new ValidateException('商户分类,不能为空'); // } // $merchant_category_id=Db::name('merchant_category')->where('category_name',$data['merchant_category_name'])->value('merchant_category_id'); // if(!$merchant_category_id){ // throw new ValidateException('没有对应的商户分类,请联系工作人员添加'); // } // $data['merchant_category_id']=$merchant_category_id; unset($data['code']); return $data; } /** * 商户分类 * @Author:Qinii * @Date: 2020/9/15 * @return mixed */ public function cateLst($type=0) { $lst = app()->make(MerchantCategoryRepository::class)->getSelect(0,$type); return app('json')->success($lst); } /** * v2商户分类 * @Author:Qinii * @Date: 2020/9/15 * @return mixed */ public function v2CateLst() { $lst = app()->make(MerchantCategoryRepository::class)->getSelect(1); return app('json')->success($lst); } public function typeLst() { /** @var MerchantTypeRepository $repo */ $siftStore = $this->request->param('sift_store', 0); $repo = app()->make(MerchantTypeRepository::class); $repo->userApply = true; $lst = $repo->getSelect(false, $siftStore); return app('json')->success($lst); } }