// +---------------------------------------------------------------------- namespace app\controller\api\store\merchant; use app\common\model\store\order\StoreOrderInterest; use app\common\repositories\store\MerchantTakeRepository; use app\common\repositories\store\product\ProductRepository; use app\common\repositories\system\config\ConfigValueRepository; use app\common\repositories\system\financial\FinancialRepository; use app\common\repositories\system\merchant\MerchantRepository; use app\common\repositories\user\UserMerchantRepository; use app\validate\merchant\MerchantFinancialAccountValidate; use app\validate\merchant\MerchantTakeValidate; use app\validate\merchant\MerchantUpdateValidate; use crmeb\jobs\ChangeMerchantStatusJob; use think\App; use crmeb\basic\BaseController; use app\common\repositories\system\merchant\MerchantRepository as repository; use think\facade\Db; use think\facade\Queue; use app\common\model\system\merchant\Merchant as MerchantModel; class Merchant extends BaseController { protected $repository; protected $userInfo; /** * ProductCategory constructor. * @param App $app * @param repository $repository */ public function __construct(App $app, repository $repository) { parent::__construct($app); $this->repository = $repository; $this->userInfo =$this->request->isLogin() ? $this->request->userInfo():null; } /** * @Author:Qinii * @Date: 2020/5/27 * @return mixed */ public function lst() { [$page, $limit] = $this->getPage(); $where = $this->request->params(['keyword', 'order', 'is_best', 'location', 'category_id', 'type_id', 'type_code', 'is_trader', 'street_id', 'credit_buy']); if (empty($where['type_id'])) { //$where['type_id'] = [MerchantModel::TypeCloudWarehouse, MerchantModel::TypeStore, MerchantModel::TypeSupplyChain, MerchantModel::TypePlatform]; $where['type_id'] = Db::name('MerchantType')->where('is_allow_apply', 1)->where('type_code', '<>', MerchantModel::TypeCode['TypeSupplyChain'])->column('mer_type_id'); } return app('json')->success($this->repository->getList($where, $page, $limit, $this->userInfo)); } /** * @Author:Qinii * @Date: 2020/5/29 * @param $id * @return mixed */ public function detail($id) { if (!$this->repository->apiGetOne($id)) return app('json')->fail('店铺已打烊'); if ($this->request->isLogin()) { app()->make(UserMerchantRepository::class)->updateLastTime($this->request->uid(), intval($id)); } return app('json')->success($this->repository->detail($id, $this->userInfo)); } public function systemDetail() { $config = systemConfig(['site_logo', 'site_name','login_logo']); return app('json')->success([ 'mer_avatar' => $config['login_logo'], 'mer_name' => $config['site_name'], 'mer_id' => 0, ]); } /** * @Author:Qinii * @Date: 2020/5/29 * @param $id * @return mixed */ public function productList($id) { [$page, $limit] = $this->getPage(); $where = $this->request->params(['keyword','order','mer_cate_id','cate_id', 'order', 'price_on', 'price_off', 'brand_id', 'pid']); if(!$this->repository->apiGetOne($id)) return app('json')->fail(' 店铺已打烊'); return app('json')->success($this->repository->productList($id,$where, $page, $limit,$this->userInfo)); } /** * @Author:Qinii * @Date: 2020/5/29 * @param int $id * @return mixed */ public function categoryList($id) { if(!$this->repository->merExists((int)$id)) return app('json')->fail('店铺已打烊'); return app('json')->success($this->repository->categoryList($id)); } public function qrcode($id) { if(!$this->repository->merExists($id)) return app('json')->fail('店铺已打烊'); $url = $this->request->param('type') == 'routine' ? $this->repository->routineQrcode(intval($id)) : $this->repository->wxQrcode(intval($id)); return app('json')->success(compact('url')); } public function localLst() { [$page, $limit] = $this->getPage(); $where = $this->request->params(['keyword', 'order', 'is_best', 'location', 'category_id', 'type_id']); $where['delivery_way'] = 1; return app('json')->success($this->repository->getList($where, $page, $limit, $this->userInfo)); } /** * @param MerchantUpdateValidate $validate * @return mixed * @author xaboy * @day 2020/6/25 */ public function update(MerchantUpdateValidate $validate, MerchantTakeValidate $takeValidate, MerchantTakeRepository $repository) { $type = $this->request->param('type',1); $id = $this->request->param('id'); if(empty($id)){ return app('json')->fail('参数错误'); } $merchant = Db::name('merchant')->where('mer_id',$id)->find(); if ($type == 2) { $data = $this->request->params([ 'mer_info', 'mer_certificate', 'service_phone', 'mer_avatar', 'mer_banner', 'mer_state', 'mini_banner', 'mer_keyword', 'mer_address', 'long', 'lat', ['delivery_way',[2]], 'credit_buy', 'settle_cycle', 'interest_rate', ]); if(empty($data['service_phone'])) { return app('json')->fail('客户电话不能为空'); } // 如果手机号不存在,则使用入驻时的手机号 $data['service_phone'] = empty($data['service_phone'])?$merchant['mer_phone']:$data['service_phone']; $validate->check($data); $sys_bases_status = systemConfig('sys_bases_status') === '0' ? 0 : 1; if ($sys_bases_status && empty($data['mer_certificate'])) return app('json')->fail('店铺资质不可为空'); $merCertificate = merchantConfig($id, 'mer_certificate'); if (!is_array($merCertificate)) { $merCertificate = explode( ',', $merCertificate); } $merCertificate[0] = $data['mer_certificate']; app()->make(ConfigValueRepository::class)->setFormData([ 'mer_certificate' => $merCertificate ], $id); unset($data['mer_certificate']); foreach ($data['delivery_way'] as $datum) { if ($datum == 1) { $takeData = $this->request->params(['mer_take_status', 'mer_take_location', 'mer_take_day', 'mer_take_time']); $takeData['mer_take_name'] = $merchant['mer_name']; $takeData['mer_take_address'] = $data['mer_address']; $takeData['mer_take_phone'] = $merchant['mer_phone']; $takeValidate->check($takeData); $repository->set($id, $takeData); break; } } $delivery_way = implode(',',$data['delivery_way']); if (count($data['delivery_way']) == 1 && $data['delivery_way'] != $merchant['delivery_way']) { app()->make(ProductRepository::class)->getSearch([]) ->where('mer_id',$merchant['mer_id']) ->update(['delivery_way' => $delivery_way]); } $data['delivery_way'] = $delivery_way; } else { $data = $this->request->params(['mer_state']); if ($merchant['is_margin'] == 1 && $data['mer_state'] == 1) return app('json')->fail('开启店铺前请先支付保证金'); if ($data['mer_state'] && !$merchant['sub_mchid'] && systemConfig('open_wx_combine')) return app('json')->fail('开启店铺前请先完成微信子商户入驻'); } Db::name('merchant')->where('mer_id',$id)->update($data); Queue::push(ChangeMerchantStatusJob::class, $id); return app('json')->success('修改成功'); } /** * @return mixed * @author xaboy * @day 2020/7/21 */ public function info(MerchantTakeRepository $repository) { $id = $this->request->param('id'); if(empty($id)){ return app('json')->fail('参数错误'); } $data = Db::name('merchant')->where('mer_id',$id)->find(); $data['mer_certificate'] = merchantConfig($id, 'mer_certificate'); $data['mer_certificate'] = $data['mer_certificate'][0] ?? ''; // $append = ['merchantCategory', 'merchantType', 'mer_certificate']; // if ($merchant['is_margin'] == -10) // $append[] = 'refundMarginOrder'; // $data = $merchant->append($append)->hidden(['mark', 'reg_admin_id', 'sort'])->toArray(); $delivery = $repository->get($id) + systemConfig(['tx_map_key']); $data = array_merge($data,$delivery); $data['sys_bases_status'] = systemConfig('sys_bases_status') === '0' ? 0 : 1; $data['type_code'] = Db::name('MerchantType')->where('mer_type_id', $data['type_id'])->value('type_code'); return app('json')->success($data); } /** * @return mixed * @author xaboy * @day 2020/7/21 */ public function marginInfo(MerchantTakeRepository $repository) { $id = $this->request->param('id'); if(empty($id)){ return app('json')->fail('参数不能为空'); } $merchantInfo = Db::name('merchant')->where('mer_id',$id)->field('uid,mer_id,type_id,mer_name,margin,paid_margin,is_margin')->find(); if(empty($merchantInfo)){ return app('json')->fail('参数错误'); } $merchantInfo['unpaid_margin'] = $merchantInfo['margin']; return app('json')->success($merchantInfo); } public function apply($merId){ $merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $merId])->field('uid,mer_id,mer_name,mer_money,financial_bank,financial_wechat,financial_alipay,financial_type')->find(); if (($msg = $this->checkAuth($merchant)) !== true) { return app('json')->fail($msg); } $extract_minimum_line = systemConfig('extract_minimum_line') ?: 0; $extract_minimum_num = systemConfig('extract_minimum_num'); $_line = bcsub($merchant->mer_money,$extract_minimum_line,2); $_extract = ($_line < 0) ? 0 : $_line; $data = [ 'mer_id' => $merchant->mer_id,//商户id 'mer_name' => $merchant->mer_name,//商户名称 'mer_money' => $merchant->mer_money,//商户余额 'extract_minimum_line' => $extract_minimum_line,//提现最低额度 'extract_minimum_num' => $extract_minimum_num,//提现最低次数 'extract_money' => $_extract,//可提现金额 'financial_bank_name' => $merchant->financial_bank->name ?? '',//银行卡信息 'financial_bank_bank' => $merchant->financial_bank->bank ?? '',//银行卡信息 'financial_bank_code' => $merchant->financial_bank->bank_code ?? '',//银行卡信息 'financial_bank_branch' => $merchant->financial_bank->bank_branch ?? '',//开户行 'financial_type' => $merchant->financial_type,//提现方式 ]; return app('json')->success($data); } /** * 提交提现申请 * @param $merId * @return mixed * @throws \FormBuilder\exception\FormBuilderException */ public function createApply($merId) { $data = $this->request->param(['extract_money','financial_type', 'financial_bank_name', 'financial_bank_bank', 'financial_bank_code', 'financial_bank_branch']); $merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $merId])->field('reg_admin_id,uid,mer_id,mer_name,mer_money,financial_bank,financial_wechat,financial_alipay,financial_type')->find(); if (($msg = $this->checkAuth($merchant)) !== true) { return app('json')->fail($msg); } $bankInfo = [ 'name' => $data['financial_bank_name'], 'bank' => $data['financial_bank_bank'], 'bank_code' => $data['financial_bank_code'], 'bank_branch' => $data['financial_bank_branch'], ]; $merchant->update(['financial_bank' => json_encode($bankInfo, JSON_UNESCAPED_UNICODE)], ['mer_id' => $merId]); $data['mer_admin_id'] = $merchant['reg_admin_id']; unset($data['financial_bank_name'], $data['financial_bank_bank'], $data['financial_bank_code'], $data['financial_bank_branch']); app()->make(FinancialRepository::class)->saveApply($merId,$data); return app('json')->success('申请成功'); } public function listApply($merId) { $merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $merId])->field('reg_admin_id,uid,mer_id,mer_name,mer_money,financial_bank,financial_wechat,financial_alipay,financial_type')->find(); if (($msg = $this->checkAuth($merchant)) !== true) { return app('json')->fail($msg); } [$page, $limit] = $this->getPage(); $where['mer_id'] = $merId; $data = app()->make(FinancialRepository::class)->getAdminList($where,$page,$limit); return app('json')->success($data); } public function account($merId) { $merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $merId])->field('uid,mer_id,mer_name,mer_money,financial_bank,financial_wechat,financial_alipay,financial_type')->find(); if (($msg = $this->checkAuth($merchant)) !== true) { return app('json')->fail($msg); } $data = [ 'financial_bank' => $merchant->financial_bank,//银行卡信息 'financial_wechat' => $merchant->financial_wechat,//微信信息 'financial_alipay' => $merchant->financial_alipay,//支付宝信息 'financial_type' => $merchant->financial_type,//提现方式 ]; return app('json')->success($data); } public function account_info($merId) { $data = $this->request->param(['name','bank','bank_code','financial_type']); app()->make(MerchantFinancialAccountValidate::class)->check($data); $merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $merId])->field('uid,mer_id,mer_name,mer_money,financial_bank,financial_wechat,financial_alipay,financial_type')->find(); if (($msg = $this->checkAuth($merchant)) !== true) { return app('json')->fail($msg); } $update = [ 'name' => $data['name'], 'bank' => $data['bank'], 'bank_code' => $data['bank_code'], ]; app()->make(MerchantRepository::class)->update($merId,['financial_bank' => json_encode($update),'financial_type' => 1]); return app('json')->success('提交成功'); } public function checkAuth($merchant) { if ($this->userInfo['uid'] != $merchant->uid) { return '你不是管理员无法进行提现操作'; } $unSettleCount = StoreOrderInterest::where('mer_id', $merchant->mer_id)->where('status', StoreOrderInterest::STATUS_UNSETTLED)->whereTime('start_time', '<=', time())->count(); if ($unSettleCount > 0) { return '有未结清的订单,请结清订单后再进行提现'; } return true; } }