提交 9b423bb4 authored 作者: caiwenxin's avatar caiwenxin

登陆 注册 检测升级 扣除金额,解除上级冻结金额

上级 d63e9ea2
......@@ -3,6 +3,7 @@
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\User;
/**
* 首页接口
......@@ -18,8 +19,8 @@ class Index extends Api
*/
public function index()
{
$res = \app\common\model\User::money(500,1,'测试');
dump($res);die;
$this->success('请求成功');
}
}
......@@ -2,12 +2,12 @@
namespace app\api\controller;
use app\admin\model\TtCode;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
/**
* 会员接口
*/
......@@ -37,15 +37,27 @@ class User extends Api
*/
public function login()
{
$account = $this->request->request('account');
$account = $this->request->request('username');
$password = $this->request->request('password');
if (!$account || !$password) {
$this->error(__('Invalid parameters'));
}
$ret = $this->auth->login($account, $password);
$ret = $this->auth->login($account, $password,\app\common\model\User::TYPE_TT);
if ($ret) {
$data = ['userinfo' => $this->auth->getUserinfo()];
//检查并处理押金
$res = \app\common\model\User::depositChange($data['userinfo']['id']);
switch ($res['code']){
case 1:
$this->success(__('Logged in successful'), $data);
break;
case 2:
$this->error('登录失败,账户异常');
break;
default:
$this->error('系统异常');
break;
}
} else {
$this->error($this->auth->getError());
}
......@@ -105,6 +117,7 @@ class User extends Api
$email = $this->request->request('email');
$mobile = $this->request->request('mobile');
$code = $this->request->request('code');
$code_tt = $this->request->request('code_tt');
if (!$username || !$password) {
$this->error(__('Invalid parameters'));
}
......@@ -118,8 +131,13 @@ class User extends Api
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
$ret = $this->auth->register($username, $password, $email, $mobile, []);
if ($ret) {
$ttCodeInfo = TtCode::where('code',$code_tt)->where('parent_id','>',0)->where('status',0)->find();
if(empty($ttCodeInfo)){
$this->error('无效的站长码');
}
$ret = $this->auth->register($username, $password, $email, $mobile, [],[],$ttCodeInfo['parent_id'],\app\common\model\User::TYPE_TT);
$resMoney = \app\common\model\User::money(10,$ttCodeInfo['parent_id'],'拉新注册');
if ($ret && $resMoney){
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Sign up successful'), $data);
} else {
......@@ -323,4 +341,6 @@ class User extends Api
$this->error($this->auth->getError());
}
}
}
......@@ -131,23 +131,23 @@ class Auth
* @param array $extend 扩展参数
* @return boolean
*/
public function register($username, $password, $email = '', $mobile = '',$popularizeId = '', $extend = [])
public function register($username, $password, $email = '', $mobile = '',$popularizeId = '', $extend = [],$parent_id='',$type=User::TYPE_YNS)
{
// 检测用户名、昵称、邮箱、手机号是否存在
if (User::getByUsername($username)) {
$this->setError('Username already exist');
$this->setError(__('Username already exist'));
return false;
}
if (User::getByNickname($username)) {
$this->setError('Nickname already exist');
$this->setError(__('Nickname already exist'));
return false;
}
if ($email && User::getByEmail($email)) {
$this->setError('Email already exist');
$this->setError(__('Email already exist'));
return false;
}
if ($mobile && User::getByMobile($mobile)) {
$this->setError('Mobile already exist');
$this->setError(__('Moile already existb'));
return false;
}
......@@ -163,6 +163,8 @@ class Auth
'level' => 1,
'score' => 0,
'avatar' => '',
'parent_id'=>$parent_id,
'type' =>$type
];
$params = array_merge($data, [
'nickname' => preg_match("/^1[3-9]{1}\d{9}$/",$username) ? substr_replace($username,'****',3,4) : $username,
......@@ -172,7 +174,9 @@ class Auth
'logintime' => $time,
'loginip' => $ip,
'prevtime' => $time,
'status' => 'normal'
'status' => 'normal',
'parent_id' => $parent_id,
'type' => $type
]);
$params['password'] = $this->getEncryptPassword($password, $params['salt']);
$params = array_merge($params, $extend);
......@@ -209,10 +213,10 @@ class Auth
* @param string $password 密码
* @return boolean
*/
public function login($account, $password)
public function login($account, $password,$type=1)
{
$field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
$user = User::get([$field => $account]);
$user = User::get([$field => $account,'type'=>$type]);
if (!$user) {
$this->setError('Account is incorrect');
return false;
......
<?php
namespace app\common\model;
use think\Model;
/**
* 平级分销
*/
class TtCode Extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $updateTime = false;
// 追加属性
protected $append = [
];
/**
* 修改邀请码状态
* @param $user_id
* @param $parent_id
* @param $code
* @return TtCode
*/
public static function updateCode($user_id,$parent_id,$code)
{
return self::where('code',$code)->update(['status'=>1,'user_id'=>$user_id,'parent_id'=>$parent_id]);
}
}
......@@ -2,6 +2,9 @@
namespace app\common\model;
use app\admin\controller\general\Profile;
use app\admin\model\TtCode;
use think\Db;
use think\Model;
/**
......@@ -20,6 +23,14 @@ class User extends Model
'url',
];
const NEW_REWARD = 500; //拉新奖励
const STATUS_TRUE = 'normal';//用户状态正常
const STATUS_FALSE = 'hidden'; //用户状态禁用
const TYPE_TT = 2;//陶特分销
const TYPE_YNS = 1;//陶特易农社
/**
* 获取个人URL
* @param string $value
......@@ -136,9 +147,26 @@ class User extends Model
return $level;
}
/**
* 减少余额
* @param $money 金额
* @param $user_id 用户ID
* @param $memo 注释
* @throws \think\exception\DbException
*/
public static function subMoney($money,$user_id,$memo)
{
$user = self::get($user_id);
if ($user && $money != 0) {
$before = $user->money;
$after = function_exists('bcsub') ? bcsub($user->money, $money, 2) : $user->user - $money;
if($after < 0) return false;
//更新会员信息
$user->save(['money' => $after]);
//写入日志
MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
return true;
}
}
/**
......@@ -147,17 +175,135 @@ class User extends Model
* @param int $user_id 会员ID
* @param string $memo 备注
*/
public static function frozen($money, $user_id, $memo)
public static function frozen($money, $user_id,$type)
{
$user = self::get($user_id);
if ($user && $money != 0) {
$before = $user->frozen;
//$after = $user->money + $money;
if($type == 1){
//增加冻结金额
$after = function_exists('bcadd') ? bcadd($user->frozen, $money, 2) : $user->frozen + $money;
//更新会员信息
$user->save(['frozen' => $after]);
//写入日志
return True;
}else{
//减少冻结金额
$after = function_exists('bcsub') ? bcsub($user->frozen, $money, 2) : $user->frozen - $money;
if($after < 0)return false;
$user->save(['frozen' => $after]);
return true;
}
}
}
/**
* 检测用户名是否存在
* @param $username
*/
public static function getByUsername($username)
{
return self::where('username',$username)->where('status',self::STATUS_TRUE)->find();
}
/**
* 检测昵称是否存在
* @param $nickname
*/
public static function getByNickname($nickname)
{
return self::where('nickname',$nickname)->where('status',self::STATUS_TRUE)->find();
}
/**
* 检测邮箱是否存在
* @param $nickname
*/
public static function getByEmail($email)
{
return self::where('email',$email)->where('status',self::STATUS_TRUE)->find();
}
/**
* 检测手机号是否存在
* @param $mobile
*/
public static function getByMobile($mobile)
{
return self::where('mobile',$mobile)->where('status',self::STATUS_TRUE)->find();
}
/**
* 扣除金额,解除上级冻结金额
* @throws \think\exception\DbException
*/
public static function depositChange($user_id)
{
$info = self::get($user_id);
Db::startTrans();
//获取创建30天后的时间戳
try{
$nextMonth = $info->createtime + 86400 * 30;
//判断是否交了押金
if($info->is_deposit == 1) ['code'=>1];
if($info->money >= self::NEW_REWARD){
//扣除用户余额
self::subMoney(self::NEW_REWARD,$user_id,'扣除服务费');
//解除上级冻结金额
self::frozen(self::NEW_REWARD,$info->parent_id,2);
//返还上级冻结金额
self::money(self::NEW_REWARD,$info->parent_id,'返回冻结金额');
//记录已交押金
self::where('id',$user_id)->update(['is_deposit'=>1]);
Db::commit();
return ['code'=>1];
}else if($nextMonth < time()){
//禁用账户
self::where('id',$user_id)->update(['status'=>self::STATUS_FALSE]);
//恢复站长码
TtCode::where('user_id',$user_id)->update(['status'=>0,'user_id'=>self::STATUS_FALSE]);
//将最后余额返还上级
self::money($info->money,$info->parent_id,'返回冻结金额');
//根据用户余额 解除上级冻结金额
self::frozen($info->money,$info->parent_id,2);
//清空用户余额
self::where('id',$user_id)->update(['money'=>0]);
Db::commit();
return ['code'=>2];
}else{
return ['code'=>1];
}
}catch (\Exception $e){
dump($e->getMessage());
Db::rollback();
return false;
}
}
/**
* 检测账户是否可以晋升 (缺少条件 :订单判断)
* @param $user_id
* @throws \think\Exception
* @throws \think\exception\DbException
*/
public static function testGroup($user_id)
{
$info = self::get($user_id);
switch ($info['group_id']){
case 3:
$count = self::where('parent_id',$user_id)->where('status',self::STATUS_TRUE)->where('group_id',3)->count();
if($count >= 5){
$group_id = 2;
}
break;
case 2:
$count = self::where('parent_id',$user_id)->where('status',self::STATUS_TRUE)->where('group_id',2)->count();
if($count >= 5){
$group_id = 1;
}
break;
}
}
}
......@@ -183,7 +183,7 @@ class User extends Frontend
$this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
return false;
}
if ($this->auth->login($account, $password)) {
if ($this->auth->login($account, $password,\app\common\model\User::TYPE_YNS)) {
$this->success(__('Logged in successful'), $url ? $url : url('index/order'));
} else {
$this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论