429 lines
17 KiB
PHP
429 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
|
|
* =========================================================
|
|
* Copy right 2019-2029 成都云之牛科技有限公司, 保留所有权利。
|
|
* ----------------------------------------------
|
|
* 官方网址: https://www.cdcloudshop.com
|
|
* =========================================================
|
|
*/
|
|
|
|
namespace addon\fenxiao\model;
|
|
|
|
use app\model\BaseModel;
|
|
use app\model\member\Member;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 分销
|
|
*/
|
|
class FenxiaoApply extends BaseModel
|
|
{
|
|
|
|
public $fenxiao_status_zh = [
|
|
1 => '待审核',
|
|
2 => '已审核',
|
|
-1 => '拒绝',
|
|
];
|
|
|
|
/**
|
|
* 判断分销商名称是否存在
|
|
* @param $fenxiao_name
|
|
* @param $site_id
|
|
*/
|
|
public function existFenxiaoName($fenxiao_name, $site_id)
|
|
{
|
|
$res = model('fenxiao_apply')->getCount([['fenxiao_name', '=', $fenxiao_name], ['site_id', '=', $site_id]]);
|
|
if ($res > 0) {
|
|
return $this->error('', '该分销商名称已存在');
|
|
}
|
|
return $this->success();
|
|
}
|
|
|
|
/**
|
|
* 申请成为分销商
|
|
* @param $member_id
|
|
* @param $fenxiao_name
|
|
* @param $mobile
|
|
* @return array
|
|
*/
|
|
public function applyFenxiao($member_id, $site_id, $fenxiao_name = '', $mobile = '')
|
|
{
|
|
//判断该用户是否已经申请
|
|
$apply_info = model('fenxiao_apply')->getInfo([['member_id', '=', $member_id]], 'apply_id,status');
|
|
if (!empty($apply_info) && $apply_info['status'] != -1) {
|
|
return $this->error('', '已经申请过,请不要重复申请');
|
|
}
|
|
|
|
// 分销商基础设置
|
|
$config = new Config();
|
|
$basics_config = $config->getFenxiaoBasicsConfig($site_id);
|
|
$basics_config = $basics_config['data']['value'];
|
|
|
|
if (!$basics_config[ 'level' ]) return $this->error('', '未开启分销');
|
|
|
|
//获取分销配置信息
|
|
$fenxiao_config = $config->getFenxiaoConfig($site_id);
|
|
$fenxiao_config = $fenxiao_config['data']['value'];
|
|
|
|
//获取用户信息
|
|
$member_model = new Member();
|
|
$member_field = 'source_member,fenxiao_id,nickname,headimg,reg_time,order_money,order_complete_money,order_num,order_complete_num';
|
|
$member_info = $member_model->getMemberInfo([['member_id', '=', $member_id]], $member_field);
|
|
|
|
// 判断用户是否可以成为申请分销商
|
|
if ($fenxiao_config['fenxiao_condition'] == 2) {
|
|
if ($fenxiao_config['consume_condition'] == 1 && $fenxiao_config['consume_count'] > $member_info['data']['order_num']) {
|
|
return $this->error('', '您的消费次数未满足申请条件');
|
|
} elseif ($fenxiao_config['consume_condition'] == 2 && $fenxiao_config['consume_count'] > $member_info['data']['order_complete_num']) {
|
|
return $this->error('', '您的消费次数未满足申请条件');
|
|
}
|
|
} elseif ($fenxiao_config['fenxiao_condition'] == 3) {
|
|
if ($fenxiao_config['consume_condition'] == 1 && $fenxiao_config['consume_money'] > $member_info['data']['order_money']) {
|
|
return $this->error('', '您的消费金额未满足申请条件');
|
|
} elseif ($fenxiao_config['consume_condition'] == 2 && $fenxiao_config['consume_money'] > $member_info['data']['order_complete_money']) {
|
|
return $this->error('', '您的消费金额未满足申请条件');
|
|
}
|
|
} elseif ($fenxiao_config['fenxiao_condition'] == 4){ //购买指定商品
|
|
//获取用户购买商品是否在成为分销商指定商品区域
|
|
$alias = 'og';
|
|
$condition[] = ['og.member_id','=',$member_id];
|
|
$condition[] = ['og.site_id','=',$site_id];
|
|
$condition[] = ['og.goods_id','in',$fenxiao_config['goods_ids']];
|
|
$field = 'o.order_status,og.goods_id';
|
|
$join = [
|
|
[
|
|
'order o',
|
|
'o.order_id = og.order_id',
|
|
'inner'
|
|
],
|
|
|
|
];
|
|
if($fenxiao_config['consume_condition'] == 1){
|
|
$condition[] = ['o.order_status','in',['1','2','3','4','10']];
|
|
$order_data = model('order_goods')->getList($condition,$field,'',$alias,$join);
|
|
if(empty($order_data)){
|
|
return $this->error('', '您还未满足申请条件');
|
|
}
|
|
} elseif ($fenxiao_config['consume_condition'] == 2){
|
|
$condition[] = ['o.order_status','=','10'];
|
|
$order_data = model('order_goods')->getList($condition,$field,'',$alias,$join);
|
|
if(empty($order_data)){
|
|
return $this->error('', '您还未满足申请条件');
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($fenxiao_name)) $fenxiao_name = $member_info['data']['nickname'];
|
|
|
|
//获取分销等级信息
|
|
$level_model = new FenxiaoLevel();
|
|
$level_info = $level_model->getLevelInfo([['site_id', '=', $site_id], ['is_default', '=', 1]], 'level_id,level_name');
|
|
|
|
// 成为分销商是否需要审核
|
|
if ($basics_config['is_examine']) {
|
|
$apply_data = [
|
|
'site_id' => $site_id,
|
|
'fenxiao_name' => $fenxiao_name,
|
|
'parent' => $member_info['data']['fenxiao_id'],
|
|
'member_id' => $member_id,
|
|
'mobile' => $mobile,
|
|
'nickname' => $member_info['data']['nickname'],
|
|
'headimg' => $member_info['data']['headimg'],
|
|
'level_id' => $level_info['data']['level_id'],
|
|
'level_name' => $level_info['data']['level_name'],
|
|
'order_complete_money' => $member_info['data']['order_complete_money'],
|
|
'order_complete_num' => $member_info['data']['order_complete_num'],
|
|
'reg_time' => $member_info['data']['reg_time'],
|
|
'create_time' => time(),
|
|
'status' => 1
|
|
];
|
|
if (!empty($apply_info)) {
|
|
$res = model('fenxiao_apply')->update($apply_data, [['member_id', '=', $member_id]]);
|
|
} else {
|
|
$res = model('fenxiao_apply')->add($apply_data);
|
|
}
|
|
return $this->success($res);
|
|
} else {
|
|
$fenxiao_data = [
|
|
'site_id' => $site_id,
|
|
'fenxiao_name' => $fenxiao_name,
|
|
'mobile' => $mobile,
|
|
'member_id' => $member_id,
|
|
'parent' => $member_info['data']['fenxiao_id'],
|
|
'level_id' => $level_info['data']['level_id'],
|
|
'level_name' => $level_info['data']['level_name']
|
|
];
|
|
$fenxiao_model = new Fenxiao();
|
|
$res = $fenxiao_model->addFenxiao($fenxiao_data);
|
|
return $res;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 管理员添加分销商
|
|
*/
|
|
public function addFenxiao($fenxiao_data)
|
|
{
|
|
|
|
if (empty($fenxiao_data['fenxiao_name'])) {
|
|
return $this->error('', '分销商名称不能为空');
|
|
}
|
|
if (empty($fenxiao_data['level_id'])) {
|
|
return $this->error('', '请选择分销商等级');
|
|
}
|
|
if (empty($fenxiao_data['member_id'])) {
|
|
return $this->error('', '请选择关联会员');
|
|
}
|
|
|
|
//判断该用户是否已经申请
|
|
$apply_info = model('fenxiao_apply')->getInfo([['member_id', '=', $fenxiao_data['member_id']]], 'apply_id,status');
|
|
if (!empty($apply_info) && $apply_info['status'] != -1) {
|
|
return $this->error('', '已经申请过,请不要重复申请');
|
|
}
|
|
|
|
// 分销商基础设置
|
|
$config = new Config();
|
|
|
|
//获取分销配置信息
|
|
$fenxiao_config = $config->getFenxiaoConfig($fenxiao_data['site_id']);
|
|
$fenxiao_config = $fenxiao_config['data']['value'];
|
|
|
|
//获取用户信息
|
|
$member_model = new Member();
|
|
$member_field = 'source_member,fenxiao_id,nickname,headimg,reg_time,order_money,order_complete_money,order_num,order_complete_num';
|
|
$member_info = $member_model->getMemberInfo([['member_id', '=', $fenxiao_data['member_id']]], $member_field);
|
|
if (empty($member_info)) return $this->error('', '未获取到会员信息');
|
|
|
|
//获取分销等级信息
|
|
$level_model = new FenxiaoLevel();
|
|
$level_info = $level_model->getLevelInfo([['site_id', '=', $fenxiao_data['site_id']], ['level_id', '=', $fenxiao_data['level_id']]], 'level_name');
|
|
$fenxiao_data['level_name'] = $level_info['data']['level_name'];
|
|
|
|
$fenxiao_model = new Fenxiao();
|
|
return $fenxiao_model->addFenxiao($fenxiao_data);
|
|
|
|
}
|
|
|
|
/**
|
|
* 审核通过
|
|
* @param $fenxiao_id
|
|
* @return array
|
|
*/
|
|
public function pass($apply_id, $site_id)
|
|
{
|
|
$info = model('fenxiao_apply')->getInfo([['apply_id', '=', $apply_id], ['site_id', '=', $site_id]]);
|
|
if ($info['status'] == 2) {
|
|
return $this->success();
|
|
}
|
|
|
|
model('fenxiao_apply')->startTrans();
|
|
try {
|
|
$data = [
|
|
'status' => 2,
|
|
'update_time' => time(),
|
|
];
|
|
$res = model('fenxiao_apply')->update($data, [['apply_id', '=', $apply_id], ['site_id', '=', $site_id]]);
|
|
|
|
$fenxiao_data = [
|
|
'site_id' => $info['site_id'],
|
|
'fenxiao_name' => $info['fenxiao_name'],
|
|
'mobile' => $info['mobile'],
|
|
'member_id' => $info['member_id'],
|
|
'parent' => $info['parent'],
|
|
'level_id' => $info['level_id'],
|
|
'level_name' => $info['level_name']
|
|
];
|
|
|
|
$fenxiao_model = new Fenxiao();
|
|
$result = $fenxiao_model->addFenxiao($fenxiao_data);
|
|
if ($result['code'] != 0) {
|
|
model('fenxiao_apply')->rollback();
|
|
return $result;
|
|
}
|
|
|
|
model('fenxiao_apply')->commit();
|
|
|
|
return $this->success($res);
|
|
} catch (\Exception $e) {
|
|
model('fenxiao_apply')->rollback();
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 审核不通过
|
|
* @param $fenxiao_id
|
|
* @return array
|
|
*/
|
|
public function refuse($apply_id)
|
|
{
|
|
$data = [
|
|
'status' => -1,
|
|
'update_time' => time()
|
|
];
|
|
|
|
$res = model('fenxiao_apply')->update($data, [['apply_id', '=', $apply_id]]);
|
|
return $this->success($res);
|
|
}
|
|
|
|
/**
|
|
* 获取分销商申请信息
|
|
* @param array $condition
|
|
* @param string $field
|
|
* @return array
|
|
*/
|
|
public function getFenxiaoApplyInfo($condition = [], $field = '*')
|
|
{
|
|
$res = model('fenxiao_apply')->getInfo($condition, $field);
|
|
return $this->success($res);
|
|
}
|
|
|
|
/**
|
|
* 获取分销商申请分页列表
|
|
* @param array $condition
|
|
* @param number $page
|
|
* @param string $page_size
|
|
* @param string $order
|
|
* @param string $field
|
|
*/
|
|
public function getFenxiaoApplyPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [])
|
|
{
|
|
$list = model('fenxiao_apply')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
|
return $this->success($list);
|
|
}
|
|
|
|
/**
|
|
* 获取分销商申请数量
|
|
* @param array $condition
|
|
* @param string $field
|
|
*/
|
|
public function getFenxiaoApplyCount($condition = [], $field = 'id')
|
|
{
|
|
$count = model('fenxiao_apply')->getCount($condition, $field);
|
|
return $this->success($count);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Common: 获取代理申请
|
|
* Author: wu-hui
|
|
* Time: 2023/02/27 10:18
|
|
* @param array $condition
|
|
* @param string $field
|
|
* @return array
|
|
*/
|
|
public function getFenXiaoUpgradeApplyInfo($condition = [], $field = '*'){
|
|
$res = model('fenxiao_apply_upgrade')->getInfo($condition, $field);
|
|
return $this->success($res);
|
|
}
|
|
/**
|
|
* Common: 分销商申请升级
|
|
* Author: wu-hui
|
|
* Time: 2023/02/27 10:26
|
|
* @param $params
|
|
* @return array
|
|
*/
|
|
public function upgradeApply($params){
|
|
// 判断: 协议是否一致
|
|
$config = (new Config())->getFenxiaoBasicsConfig($params['site_id'])['data'];
|
|
$upgradeAgree = $config['value']['upgrade_agree'] ?? '';
|
|
if($params['upgrade_agree'] !== $upgradeAgree) return $this->error('','协议不一致,请检查输入内容是否和协议内容一直');
|
|
// 判断:当前用户是否为分销商
|
|
$fenXiaoInfo = (new Fenxiao())->getFenxiaoInfo([['member_id','=',$params['member_id']]])['data'];
|
|
if(!$fenXiaoInfo) return $this->error('','请先成为分销商!');
|
|
// 判断:如果存在审核中申请 禁止提交
|
|
$upgradeInfo = $this->getFenXiaoUpgradeApplyInfo([['member_id','=',$params['member_id']]])['data'];
|
|
if($upgradeInfo && $upgradeInfo['status'] == 1) return $this->error('','申请审核中,请勿重复提交!');
|
|
// 添加协议
|
|
$data = [
|
|
'username' => $params['username'],
|
|
'phone' => $params['phone'],
|
|
'id_card' => $params['id_card'],
|
|
'id_card_front' => $params['id_card_front'],
|
|
'id_card_reverse' => $params['id_card_reverse'],
|
|
'update_time' => time(),
|
|
'status' => 1,
|
|
];
|
|
if($upgradeInfo){
|
|
// 修改
|
|
model('fenxiao_apply_upgrade')->update($data, [['member_id','=',$params['member_id']]]);
|
|
}else{
|
|
// 添加
|
|
$data['site_id'] = $params['site_id'];
|
|
$data['fenxiao_id'] = $fenXiaoInfo['fenxiao_id'];
|
|
$data['member_id'] = $params['member_id'];
|
|
$data['create_time'] = time();
|
|
$data['site_id'] = $params['site_id'];
|
|
|
|
model('fenxiao_apply_upgrade')->add($data);
|
|
}
|
|
|
|
return $this->success();
|
|
}
|
|
/**
|
|
* Common: 分销商申请升级列表
|
|
* Author: wu-hui
|
|
* Time: 2023/02/27 11:39
|
|
* @param array $condition
|
|
* @param int $page
|
|
* @param int $page_size
|
|
* @param string $order
|
|
* @param string $field
|
|
* @param string $alias
|
|
* @param array $join
|
|
* @return array
|
|
*/
|
|
public function getPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'a.*', $alias = 'a', $join = []){
|
|
$list = model('fenxiao_apply_upgrade')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
|
return $this->success($list);
|
|
}
|
|
/**
|
|
* Common: 代理申请 审核
|
|
* Author: wu-hui
|
|
* Time: 2023/02/27 14:28
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function upgradeApplyExamine(){
|
|
//参数获取
|
|
$id = input('id');
|
|
$type = input('type');
|
|
$cause = input('cause','');
|
|
// 根据审核类型生成修改数据
|
|
$update['update_time'] = time();
|
|
if($type === 'pass') {
|
|
$update['status'] = 2;
|
|
// 审核通过
|
|
$fenXiaoId = (int)Db::name('fenxiao_apply_upgrade')->where('id',$id)->value('fenxiao_id');
|
|
if($fenXiaoId <= 0) return $this->error('','分销商不存在!');
|
|
$currentLevelId = (int)Db::name('fenxiao')->where('fenxiao_id',$fenXiaoId)->value('level_id');
|
|
// 升级:按照权重顺序升级 加一查询下一级内容 如果存在则升级成功,否则升级失败
|
|
$currentNum = (int)Db::name('fenxiao_level')->where('level_id',$currentLevelId)->value('level_num');
|
|
$currentNum++;
|
|
$next = Db::name('fenxiao_level')
|
|
->field('level_id,level_name')
|
|
->where('level_num',$currentNum)
|
|
->find();
|
|
// 修改分销商等级
|
|
model('fenxiao')->update([
|
|
'level_id' => $next['level_id'],
|
|
'level_name' => $next['level_name']
|
|
],[['fenxiao_id','=',$fenXiaoId]]);
|
|
}
|
|
else if($type === 'reject'){
|
|
// 审核不通过
|
|
$update['status'] = 3;
|
|
$update['reject_cause'] = $cause;
|
|
}
|
|
model('fenxiao_apply_upgrade')->update($update, [['id','=',$id]]);
|
|
|
|
return $this->success();
|
|
}
|
|
|
|
} |