likeshop/server/application/api/validate/DistributionCode.php
2025-07-27 21:42:34 +08:00

88 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\api\validate;
use app\admin\logic\UserSettingLogic;
use app\common\server\ConfigServer;
use think\Db;
use think\Validate;
class DistributionCode extends Validate
{
protected $rule = [
'code' => 'require|checkCode',
];
protected $message = [
'code.require' => '请输入邀请码',
];
/**
* 填写邀请码验证
* @param $code
* @param $other
* @param $data
* @return bool|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
protected function checkCode($code, $other, $data)
{
$config = UserSettingLogic::getConfig();
if(!$config['is_open']) {
return '系统已关闭邀请功能';
}
$user = \app\common\model\User::where(['id'=>$data['user_id'], 'del'=>0])->findOrEmpty();
if($user->isEmpty()) {
return '无法获取当前用户信息';
}
if(!empty($user['first_leader'])) {
return '已有邀请人';
}
$firstLader = \app\common\model\User::field('id,is_distribution,level,ancestor_relation')
->where('distribution_code', $code)
->findOrEmpty();
if($firstLader->isEmpty()) {
return '无效的邀请码';
}
if($firstLader['id'] == $data['user_id']) {
return '不能填自己的邀请码';
}
// qualifications-邀请资格 【1-全部用户 2-指定等级用户】
$invite_appoint_user = ConfigServer::get('invite', 'invite_appoint_user', []);
if(in_array(2, $config['qualifications']) && !in_array($firstLader['level'],$invite_appoint_user)) {
return '邀请下级资格未达到要求';
}
// 如果当前用户id出现在邀请人的祖先链路中代表当前用户是已经是邀请人的上级或祖先级同时意味着邀请人是当前用户的后代级别
// 不能将自己的上级设置为自己的后代级用户
$ancestorArr = explode(',', $firstLader['ancestor_relation']);
if(!empty($ancestorArr) && in_array($data['user_id'], $ancestorArr)) {
return '不能填写自己下级的邀请码';
}
return true;
}
}