425 lines
18 KiB
PHP
Raw Normal View History

2023-05-10 13:38:51 +08:00
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\store\merchant;
use app\common\model\store\order\StoreOrderInterest;
2023-05-10 13:38:51 +08:00
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;
2023-06-05 16:05:49 +08:00
use app\common\model\system\merchant\Merchant as MerchantModel;
2023-11-07 15:44:54 +08:00
use crmeb\services\QrcodeService;
use app\common\repositories\system\attachment\AttachmentRepository;
2023-11-09 17:38:55 +08:00
use app\common\repositories\user\UserBillRepository;
2023-05-10 13:38:51 +08:00
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;
2023-12-04 16:40:55 +08:00
$this->userInfo = $this->request->isLogin() ? $this->request->userInfo() : null;
2023-05-10 13:38:51 +08:00
}
/**
* @Author:Qinii
* @Date: 2020/5/27
* @return mixed
*/
public function lst()
{
[$page, $limit] = $this->getPage();
2023-12-04 16:40:55 +08:00
$where = $this->request->params(['merchant_category_id', 'keyword', 'order', 'is_best', 'location', 'category_id', 'type_id', 'type_code', 'is_trader', 'street_id', 'credit_buy']);
2023-06-06 17:00:44 +08:00
if (empty($where['type_id'])) {
2023-12-04 16:40:55 +08:00
$where['type_id'] = [10, 17];
2023-06-06 17:00:44 +08:00
}
2023-12-04 16:40:55 +08:00
if ($where['merchant_category_id'] > 0) {
// $arr= Db::name('store_category')->where(['pid'=>$where['store_category_id'],'mer_id'=>0,'level'=>1,'is_show'=>1])->column('cate_name');
// if($arr){
// $category_id=Db::name('merchant_category')->whereIn('category_name',$arr)->column('merchant_category_id');
// if($category_id){
// $where['category_id']=implode(',',$category_id);
// }
// }
$where['category_id'] = $where['merchant_category_id'];
unset($where['merchant_category_id']);
2023-12-04 16:01:22 +08:00
}
2023-05-10 13:38:51 +08:00
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()
{
2023-12-04 16:40:55 +08:00
$config = systemConfig(['site_logo', 'site_name', 'login_logo']);
2023-05-10 13:38:51 +08:00
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();
2023-12-04 16:40:55 +08:00
$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));
2023-05-10 13:38:51 +08:00
}
/**
* @Author:Qinii
* @Date: 2020/5/29
* @param int $id
* @return mixed
*/
public function categoryList($id)
{
2024-02-28 14:17:45 +08:00
$pid = $this->request->get('pid', 0);
2023-12-04 16:40:55 +08:00
if (!$this->repository->merExists((int)$id))
2023-05-10 13:38:51 +08:00
return app('json')->fail('店铺已打烊');
2024-02-28 14:17:45 +08:00
return app('json')->success($this->repository->categoryList($id, $pid));
2023-05-10 13:38:51 +08:00
}
public function qrcode($id)
{
2023-12-04 16:40:55 +08:00
if (!$this->repository->merExists($id))
2023-05-10 13:38:51 +08:00
return app('json')->fail('店铺已打烊');
2023-12-04 16:40:55 +08:00
if ($this->request->param('type') == 'routine') {
$url = $this->repository->routineQrcode(intval($id));
} elseif ($this->request->param('type') == 'app') {
2023-11-07 15:44:54 +08:00
$attachmentRepository = app()->make(AttachmentRepository::class);
2023-12-04 16:40:55 +08:00
$name = 'merchant_code_' . $id . '.png';
2023-11-07 15:44:54 +08:00
$imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
2023-12-04 16:40:55 +08:00
if ($imageInfo) {
$url = $imageInfo['attachment_src'];
} else {
$info = app()->make(QrcodeService::class)->getQRCodePath(systemConfig('site_url') . 'pages/store/home/index?id=' . $id, $name);
2023-11-07 15:44:54 +08:00
$attachmentRepository->create(systemConfig('upload_type') ?: 1, -2, $id, [
'attachment_category_id' => 0,
'attachment_name' => $info['name'],
'attachment_src' => $info['dir']
]);
2023-12-04 16:40:55 +08:00
$url = $info['dir'];
2023-11-07 15:44:54 +08:00
}
2023-12-04 16:40:55 +08:00
} else {
$url = $this->repository->wxQrcode(intval($id));
2023-11-07 15:44:54 +08:00
}
2023-05-10 13:38:51 +08:00
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)
{
2023-12-04 16:40:55 +08:00
$type = $this->request->param('type', 1);
2023-05-10 13:38:51 +08:00
$id = $this->request->param('id');
2023-12-04 16:40:55 +08:00
if (empty($id)) {
2023-05-10 13:38:51 +08:00
return app('json')->fail('参数错误');
}
2023-12-04 16:40:55 +08:00
$merchant = Db::name('merchant')->where('mer_id', $id)->find();
2023-05-10 13:38:51 +08:00
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',
2023-12-04 16:40:55 +08:00
['delivery_way', [2]],
2023-07-10 10:58:37 +08:00
'credit_buy',
'settle_cycle',
'interest_rate',
2023-05-10 13:38:51 +08:00
]);
2023-12-04 16:40:55 +08:00
if (empty($data['service_phone'])) {
2023-09-22 17:35:13 +08:00
return app('json')->fail('客户电话不能为空');
}
2023-05-10 13:38:51 +08:00
// 如果手机号不存在,则使用入驻时的手机号
2023-12-04 16:40:55 +08:00
$data['service_phone'] = empty($data['service_phone']) ? $merchant['mer_phone'] : $data['service_phone'];
2023-05-10 13:38:51 +08:00
$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('店铺资质不可为空');
2023-05-31 17:38:47 +08:00
$merCertificate = merchantConfig($id, 'mer_certificate');
if (!is_array($merCertificate)) {
2023-12-04 16:40:55 +08:00
$merCertificate = explode(',', $merCertificate);
2023-05-31 17:38:47 +08:00
}
$merCertificate[0] = $data['mer_certificate'];
2023-05-10 13:38:51 +08:00
app()->make(ConfigValueRepository::class)->setFormData([
2023-05-31 17:38:47 +08:00
'mer_certificate' => $merCertificate
2023-05-10 13:38:51 +08:00
], $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;
}
}
2023-12-04 16:40:55 +08:00
$delivery_way = implode(',', $data['delivery_way']);
2023-05-10 13:38:51 +08:00
if (count($data['delivery_way']) == 1 && $data['delivery_way'] != $merchant['delivery_way']) {
app()->make(ProductRepository::class)->getSearch([])
2023-12-04 16:40:55 +08:00
->where('mer_id', $merchant['mer_id'])
2023-05-10 13:38:51 +08:00
->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)
2023-10-26 15:08:25 +08:00
return app('json')->fail('开启店铺前请先支付押金');
2023-05-10 13:38:51 +08:00
if ($data['mer_state'] && !$merchant['sub_mchid'] && systemConfig('open_wx_combine'))
return app('json')->fail('开启店铺前请先完成微信子商户入驻');
}
2023-12-04 16:40:55 +08:00
Db::name('merchant')->where('mer_id', $id)->update($data);
2023-05-10 13:38:51 +08:00
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');
2023-12-04 16:40:55 +08:00
if (empty($id)) {
2023-05-10 13:38:51 +08:00
return app('json')->fail('参数错误');
}
2023-12-23 11:15:54 +08:00
$merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $id])->find();
2023-05-10 13:38:51 +08:00
2023-12-23 11:15:54 +08:00
// $data = Db::name('merchant')->where('mer_id', $id)->find();
$merchant['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';
2023-05-10 13:38:51 +08:00
2023-12-23 11:15:54 +08:00
$data = $merchant->append($append)->hidden(['mark', 'reg_admin_id', 'sort','financial_bank'])->toArray();
2023-05-10 13:38:51 +08:00
$delivery = $repository->get($id) + systemConfig(['tx_map_key']);
2023-12-04 16:40:55 +08:00
$data = array_merge($data, $delivery);
2023-05-10 13:38:51 +08:00
$data['sys_bases_status'] = systemConfig('sys_bases_status') === '0' ? 0 : 1;
2023-09-05 16:37:30 +08:00
$data['type_code'] = Db::name('MerchantType')->where('mer_type_id', $data['type_id'])->value('type_code');
2023-05-10 13:38:51 +08:00
return app('json')->success($data);
}
2023-09-23 14:00:19 +08:00
/**
* @return mixed
* @author xaboy
* @day 2020/7/21
*/
public function marginInfo(MerchantTakeRepository $repository)
{
$id = $this->request->param('id');
2023-12-04 16:40:55 +08:00
if (empty($id)) {
2023-09-26 14:07:53 +08:00
return app('json')->fail('参数不能为空');
}
2023-12-04 16:40:55 +08:00
$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)) {
2023-09-23 14:00:19 +08:00
return app('json')->fail('参数错误');
}
2023-09-27 20:16:15 +08:00
$margin = Db::name('MerchantType')->where('mer_type_id', $merchantInfo['type_id'])->value('margin');
2023-12-04 16:40:55 +08:00
$merchantInfo['unpaid_margin'] = bcsub($margin, $merchantInfo['paid_margin'], 2);
if ($merchantInfo['margin'] == 0) {
$merchantInfo['margin'] = $merchantInfo['unpaid_margin'];
2023-09-27 20:17:49 +08:00
}
2023-09-26 14:07:53 +08:00
return app('json')->success($merchantInfo);
2023-09-23 14:00:19 +08:00
}
2023-12-04 16:40:55 +08:00
public function apply($merId)
{
2023-12-28 18:31:05 +08:00
$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,ot_margin')->find();
2023-07-07 11:24:00 +08:00
if (($msg = $this->checkAuth($merchant)) !== true) {
return app('json')->fail($msg);
2023-05-10 13:38:51 +08:00
}
$extract_minimum_line = systemConfig('extract_minimum_line') ?: 0;
$extract_minimum_num = systemConfig('extract_minimum_num');
2023-12-04 16:40:55 +08:00
$_line = bcsub($merchant->mer_money, $extract_minimum_line, 2);
2023-05-10 13:38:51 +08:00
$_extract = ($_line < 0) ? 0 : $_line;
2023-11-09 17:38:55 +08:00
$merLockMoney = app()->make(UserBillRepository::class)->merchantLickMoney($merId);
2023-05-10 13:38:51 +08:00
$data = [
2023-12-04 16:40:55 +08:00
'mer_id' => $merchant->mer_id, //商户id
'mer_name' => $merchant->mer_name, //商户名称
'mer_money' => $merchant->mer_money, //商户余额
'lock_money' => $merLockMoney, //冻结金额
'lock_time' => systemConfig('mer_lock_time'), //冻结金额
'extract_minimum_line' => $extract_minimum_line, //提现最低额度
'extract_minimum_num' => $extract_minimum_num, //提现最低次数
'extract_money' => $_extract, //可提现金额
2023-12-29 19:09:30 +08:00
'financial_bank_name' => $merchant->financial_bank->name ?? '', //银行账户姓名
'financial_bank_bank' => $merchant->financial_bank->bank ?? '', //开户行
2023-12-29 10:27:31 +08:00
'financial_bank_code' => $merchant->financial_bank->bank_code ?? '', //银行账号
2023-12-29 19:09:30 +08:00
'financial_bank_branch' => $merchant->financial_bank->bank_branch ?? '', //开户行地址
2023-12-04 16:40:55 +08:00
'financial_type' => $merchant->financial_type, //提现方式
2023-12-28 18:31:05 +08:00
'ot_margin' => $merchant->ot_margin, //提现方式
2023-05-10 13:38:51 +08:00
];
return app('json')->success($data);
}
2023-06-13 16:37:21 +08:00
/**
* 提交提现申请
* @param $merId
* @return mixed
* @throws \FormBuilder\exception\FormBuilderException
*/
2023-05-10 13:38:51 +08:00
public function createApply($merId)
{
2023-12-04 16:40:55 +08:00
$data = $this->request->param(['extract_money', 'financial_type', 'financial_bank_name', 'financial_bank_bank', 'financial_bank_code', 'financial_bank_branch']);
2023-07-13 11:42:10 +08:00
$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) {
2023-07-07 11:24:00 +08:00
return app('json')->fail($msg);
}
2023-06-13 16:37:21 +08:00
$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]);
2023-05-10 13:38:51 +08:00
$data['mer_admin_id'] = $merchant['reg_admin_id'];
2023-06-13 16:37:21 +08:00
unset($data['financial_bank_name'], $data['financial_bank_bank'], $data['financial_bank_code'], $data['financial_bank_branch']);
2023-12-04 16:40:55 +08:00
app()->make(FinancialRepository::class)->saveApply($merId, $data);
2023-05-10 13:38:51 +08:00
return app('json')->success('申请成功');
}
public function listApply($merId)
{
2023-07-13 11:42:10 +08:00
$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) {
2023-07-07 11:24:00 +08:00
return app('json')->fail($msg);
2023-05-10 13:38:51 +08:00
}
[$page, $limit] = $this->getPage();
$where['mer_id'] = $merId;
2023-12-04 16:40:55 +08:00
$data = app()->make(FinancialRepository::class)->getAdminList($where, $page, $limit);
2023-05-10 13:38:51 +08:00
return app('json')->success($data);
}
public function account($merId)
{
2023-07-13 11:42:10 +08:00
$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) {
2023-07-07 11:24:00 +08:00
return app('json')->fail($msg);
2023-05-10 13:38:51 +08:00
}
$data = [
2023-12-04 16:40:55 +08:00
'financial_bank' => $merchant->financial_bank, //银行卡信息
'financial_wechat' => $merchant->financial_wechat, //微信信息
'financial_alipay' => $merchant->financial_alipay, //支付宝信息
'financial_type' => $merchant->financial_type, //提现方式
2023-05-10 13:38:51 +08:00
];
return app('json')->success($data);
}
public function account_info($merId)
{
2023-12-04 16:40:55 +08:00
$data = $this->request->param(['name', 'bank', 'bank_code', 'financial_type']);
2023-05-10 13:38:51 +08:00
app()->make(MerchantFinancialAccountValidate::class)->check($data);
2023-07-13 11:42:10 +08:00
$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) {
2023-07-07 11:24:00 +08:00
return app('json')->fail($msg);
2023-05-10 13:38:51 +08:00
}
$update = [
'name' => $data['name'],
'bank' => $data['bank'],
'bank_code' => $data['bank_code'],
];
2023-12-04 16:40:55 +08:00
app()->make(MerchantRepository::class)->update($merId, ['financial_bank' => json_encode($update), 'financial_type' => 1]);
2023-05-10 13:38:51 +08:00
return app('json')->success('提交成功');
}
2023-07-07 11:24:00 +08:00
public function checkAuth($merchant)
{
if ($this->userInfo['uid'] != $merchant->uid) {
return '你不是管理员无法进行提现操作';
}
2023-07-15 16:10:52 +08:00
$unSettleCount = StoreOrderInterest::where('mer_id', $merchant->mer_id)->where('status', StoreOrderInterest::STATUS_UNSETTLED)->whereTime('start_time', '<=', time())->count();
if ($unSettleCount > 0) {
2023-07-07 11:24:00 +08:00
return '有未结清的订单,请结清订单后再进行提现';
}
return true;
}
2023-05-10 13:38:51 +08:00
}