Browse Source

商家分离,另一套登录系统

lizhen_gitee 1 year ago
parent
commit
dc463abea6

+ 6 - 6
application/api/controller/company/User.php → application/api/controller/coach/User.php

@@ -19,14 +19,14 @@ class User extends Apic
 
 
     //员工手机+密码登录
-    public function accountlogin(){
-        $mobile   = $this->request->post('mobile');
-        $password = $this->request->post('password');
-        $openid = $this->request->post('openid','');
-        if (!$mobile || !$password || !$openid) {
+    public function emaillogin()
+    {
+        $account = input('account');
+        $password = input('password');
+        if (!$account || !$password) {
             $this->error(__('Invalid parameters'));
         }
-        $ret = $this->auth->login($mobile, $password, $openid);
+        $ret = $this->auth->login($account, $password);
         if ($ret) {
             $data = $this->auth->getUserinfo();
             $this->success(__('Logged in successful'), $data);

+ 23 - 75
application/common/library/Authcoach.php

@@ -3,7 +3,6 @@
 namespace app\common\library;
 
 use app\common\model\Coach;
-use app\common\model\UserRule;
 use fast\Random;
 use think\Config;
 use think\Db;
@@ -156,9 +155,9 @@ class Authcoach
      * @param string $password 密码
      * @return boolean
      */
-    public function login($account, $password, $openid)
+    public function login($account, $password)
     {
-        $field = 'mobile';
+        $field = 'email';
         $user = Coach::get([$field => $account]);
         if (!$user) {
             $this->setError('Account is incorrect');
@@ -175,7 +174,7 @@ class Authcoach
         }
 
         //直接登录员工
-        return $this->direct($user->id,$openid);
+        return $this->direct($user->id);
     }
 
     /**
@@ -198,89 +197,45 @@ class Authcoach
         return true;
     }
 
-    /**
-     * 修改密码
-     * @param string $newpassword       新密码
-     * @param string $oldpassword       旧密码
-     * @param bool   $ignoreoldpassword 忽略旧密码
-     * @return boolean
-     */
-    public function resetpwd($newpassword)
-    {
-        if (!$this->_logined) {
-            $this->setError('You are not logged in');
-            return false;
-        }
-
-        Db::startTrans();
-
-        $salt = Random::alnum();
-        $newpassword = $this->getEncryptPassword($newpassword, $salt);
-        //unset($this->_user['company']);
-        $this->_user->save(['password' => $newpassword, 'salt' => $salt]);
-
-        Tokencoach::delete($this->_token);
-
-        //同步到admin
-        $admin = [
-            'password' => $newpassword,
-            'salt'     => $salt,
-            'updatetime' => time(),
-        ];
-        $admin_rs = Db::name('admin')->where('staff_id',$this->_user->id)->update($admin);
-        if($admin_rs === false){
-            Db::rollback();
-            $this->setError('重置失败');
-            return false;
-        }
-
-        Db::commit();
-
-        return true;
-
-    }
 
     /**
      * 直接登录账号
      * @param int $user_id
      * @return boolean
      */
-    public function direct($staff_id,$openid = '')
+    public function direct($user_id)
     {
-        $user = CompanyStaff::get($staff_id);
+        $user = Coach::get($user_id);
         if ($user) {
-            if(!$user->company_id){
-                return false;
-            }
-            $companyinfo = Company::get($user->company_id);
-            if(!$companyinfo){
-                return false;
-            }
-            if($companyinfo->status != 1){
-                $this->setError('当前门店未通过审核');
-                return false;
-            }
-
             Db::startTrans();
             try {
+                $ip = request()->ip();
+                $time = time();
 
-                if(!empty($openid)){
-                    $user->openid = $openid;
-                    $user->save();
+                //判断连续登录和最大连续登录
+                if ($user->logintime < \fast\Date::unixtime('day')) {
+                    $user->successions = $user->logintime < \fast\Date::unixtime('day', -1) ? 1 : $user->successions + 1;
+                    $user->maxsuccessions = max($user->successions, $user->maxsuccessions);
                 }
-                $user->openid = '';
 
-				$user->company = $companyinfo;// 追加公司
+                $user->prevtime = $user->logintime;
+                //记录本次登录的IP和时间
+                $user->loginip = $ip;
+                $user->logintime = $time;
+                //重置登录失败次数
+                $user->loginfailure = 0;
+
+                $user->save();
 
                 $this->_user = $user;
 
                 $this->_token = Random::uuid();
-                Tokencompany::set($this->_token, $user->id, $this->keeptime);
+                Tokencoach::set($this->_token, $user->id, $this->keeptime);
 
                 $this->_logined = true;
 
                 //登录成功的事件
-                Hook::listen("company_login_successed", $this->_user);
+                Hook::listen("user_login_successed", $this->_user);
                 Db::commit();
             } catch (Exception $e) {
                 Db::rollback();
@@ -324,17 +279,10 @@ class Authcoach
         $data = $this->_user->toArray();
         $allowFields = $this->getAllowFields();
         $userinfo = array_intersect_key($data, array_flip($allowFields));
-        $userinfo = array_merge($userinfo, Tokencompany::get($this->_token));
+        $userinfo = array_merge($userinfo, Tokencoach::get($this->_token));
 
         //追加
-        $userinfo['company']['image'] = one_domain_image($userinfo['company']['image']);
-
-        /////////////////////////////////////
-        //个人信息
-        $strattime = strtotime(date('Y-m-d'));
-        $endtime   = $strattime + 86399;
-
-
+        $userinfo['avatar'] = one_domain_image($userinfo['avatar']);
 
         return $userinfo;
     }

+ 1 - 1
application/common/model/Coach.php

@@ -12,7 +12,7 @@ class Coach extends Model
 {
 
     // 表名
-    protected $name = 'company';
+    protected $name = 'coach';
     // 开启自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';
     // 定义时间戳字段名

+ 1 - 129
application/common/model/User.php

@@ -17,135 +17,7 @@ class User extends Model
     protected $createTime = 'createtime';
     protected $updateTime = 'updatetime';
     // 追加属性
-    protected $append = [
-        'url',
-    ];
+    protected $append = [];
 
-    /**
-     * 获取个人URL
-     * @param string $value
-     * @param array  $data
-     * @return string
-     */
-    public function getUrlAttr($value, $data)
-    {
-        return "/u/" . $data['id'];
-    }
 
-    /**
-     * 获取头像
-     * @param string $value
-     * @param array  $data
-     * @return string
-     */
-    public function getAvatarAttr($value, $data)
-    {
-        if (!$value) {
-            //如果不需要启用首字母头像,请使用
-            //$value = '/assets/img/avatar.png';
-            $value = letter_avatar($data['nickname']);
-        }
-        return $value;
-    }
-
-    /**
-     * 获取会员的组别
-     */
-    public function getGroupAttr($value, $data)
-    {
-        return UserGroup::get($data['group_id']);
-    }
-
-    /**
-     * 获取验证字段数组值
-     * @param string $value
-     * @param array  $data
-     * @return  object
-     */
-    public function getVerificationAttr($value, $data)
-    {
-        $value = array_filter((array)json_decode($value, true));
-        $value = array_merge(['email' => 0, 'mobile' => 0], $value);
-        return (object)$value;
-    }
-
-    /**
-     * 设置验证字段
-     * @param mixed $value
-     * @return string
-     */
-    public function setVerificationAttr($value)
-    {
-        $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
-        return $value;
-    }
-
-    /**
-     * 变更会员余额
-     * @param int    $money   余额
-     * @param int    $user_id 会员ID
-     * @param string $memo    备注
-     */
-    public static function money($money, $user_id, $memo)
-    {
-        Db::startTrans();
-        try {
-            $user = self::lock(true)->find($user_id);
-            if ($user && $money != 0) {
-                $before = $user->money;
-                //$after = $user->money + $money;
-                $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
-                //更新会员信息
-                $user->save(['money' => $after]);
-                //写入日志
-                MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
-            }
-            Db::commit();
-        } catch (\Exception $e) {
-            Db::rollback();
-        }
-    }
-
-    /**
-     * 变更会员积分
-     * @param int    $score   积分
-     * @param int    $user_id 会员ID
-     * @param string $memo    备注
-     */
-    public static function score($score, $user_id, $memo)
-    {
-        Db::startTrans();
-        try {
-            $user = self::lock(true)->find($user_id);
-            if ($user && $score != 0) {
-                $before = $user->score;
-                $after = $user->score + $score;
-                $level = self::nextlevel($after);
-                //更新会员信息
-                $user->save(['score' => $after, 'level' => $level]);
-                //写入日志
-                ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
-            }
-            Db::commit();
-        } catch (\Exception $e) {
-            Db::rollback();
-        }
-    }
-
-    /**
-     * 根据积分获取等级
-     * @param int $score 积分
-     * @return int
-     */
-    public static function nextlevel($score = 0)
-    {
-        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
-        $level = 1;
-        foreach ($lv as $key => $value) {
-            if ($score >= $value) {
-                $level = $key;
-            }
-        }
-        return $level;
-    }
 }