| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | 
							- <?php
 
- namespace app\common\model;
 
- use think\db\Query;
 
- class Admin extends Base
 
- {
 
-     
 
-     public function role()
 
-     {
 
-         return $this->hasOne(Role::class, "id", "role_id");
 
-     }
 
-     
 
-     public static function edit($id, $phone, $roleId, $valid)
 
-     {
 
-         $admin = Admin::find($id);
 
-         if (!$admin) {
 
-             return returnFormat(999, "记录未找到");
 
-         }
 
-         $wherePhone = [];
 
-         $wherePhone[] = ["phone", "=", $phone];
 
-         $wherePhone[] = ["id", "<>", $id];
 
-         $exit = Admin::where($wherePhone)->find();
 
-         if ($exit) {
 
-             return returnFormat(999, "该手机号已被其它管理员绑定,请更换手机号");
 
-         }
 
-         $admin->phone = $phone;
 
-         $admin->role_id = $roleId;
 
-         $admin->valid = $valid;
 
-         $res = $admin->save();
 
-         if ($res === false) {
 
-             return returnFormat(999, "提交失败:数据库写入失败");
 
-         }
 
-         return returnFormat(0, "", $admin);
 
-     }
 
-     public static function resetPwd($id,$password){
 
-         $admin = Admin::find($id);
 
-         if (!$admin) {
 
-             return returnFormat(999, "记录未找到");
 
-         }
 
-         $admin->password = self::md5($admin->salt, $password);
 
-         $res = $admin->save();
 
-         if ($res === false) {
 
-             return returnFormat(999, "提交失败:数据库写入失败");
 
-         }
 
-         return returnFormat(0, "", $admin);
 
-     }
 
-     
 
-     public static function del($ids)
 
-     {
 
-         $whereDelete = [];
 
-         $whereDelete[] = ["id", "in", $ids];
 
-         $updateData = [
 
-             "delete_time" => getNow(),
 
-         ];
 
-         $res = (new Admin())->where($whereDelete)->update($updateData);
 
-         if ($res === false) {
 
-             return returnFormat(999, "删除失败:数据库写入失败");
 
-         }
 
-         return returnFormat(0, "", $res);
 
-     }
 
-     
 
-     public static function add($name, $password, $phone, $roleId, $valid)
 
-     {
 
-         $admin = Admin::where("name", $name)->find();
 
-         if ($admin) {
 
-             return returnFormat(999, "账号已存在,请修改账号");
 
-         }
 
-         $admin = Admin::where("phone", $phone)->find();
 
-         if ($admin) {
 
-             return returnFormat(999, "手机号已存在,请修改手机号");
 
-         }
 
-         $admin = new Admin([
 
-             "name" => $name,
 
-             "phone" => $phone,
 
-             "role_id" => $roleId,
 
-             "valid" => $valid,
 
-         ]);
 
-         $salt = rand(1000, 9999);
 
-         $admin->salt = $salt;
 
-         $admin->password = self::md5($salt, $password);
 
-         $res = $admin->save();
 
-         if ($res === false) {
 
-             return returnFormat(999, "提交失败:数据库写入失败");
 
-         }
 
-         return returnFormat(0, "", $admin);
 
-     }
 
-     
 
-     public static function getList($keyword = "", $listRow = 20)
 
-     {
 
-         $where = [];
 
-         if ($keyword) {
 
-             $where[] = ["name|phone", "like", "%" . $keyword . "%"];
 
-         }
 
-         $list = Admin::with(['role' => function (Query $query) {
 
-             $query->field("id,name,valid");
 
-         }])->where($where)->order("id desc")->paginate($listRow);
 
-         return returnFormat(0, '', $list);
 
-     }
 
-     
 
-     private static function md5($salt, $pwd)
 
-     {
 
-         $str = md5($salt . $pwd);
 
-         return $str;
 
-     }
 
-     
 
-     public static function login(string $name, string $password)
 
-     {
 
-         $admin = self::where('name|phone', '=', $name)->find();
 
-         if (!$admin) {
 
-             return returnFormat(999, "用户未找到");
 
-         }
 
-         if ($admin->getAttr('password') != self::md5($admin->getAttr('salt'), $password)) {
 
-             return returnFormat(999, '登录密码不正确' . $admin->getAttr('salt') . $password);
 
-         }
 
-         if (!$admin->valid) {
 
-             return returnFormat(999, "账号被禁用,请联系管理员");
 
-         }
 
-         
 
-         $admin->login_count = $admin->login_count + 1;
 
-         $admin->login_last_time = getNow();
 
-         $admin->token = $admin->getToken();
 
-         $admin->save();
 
-         return returnFormat(0, "", $admin);
 
-     }
 
-     
 
-     public function getToken()
 
-     {
 
-         $expireDays = 7;
 
-         
 
-         $token = base64_encode(md5($this->login_name . getNow()) . "|" . $this->id . "|" . (time() + 86400 * $expireDays));
 
-         return $token;
 
-     }
 
- }
 
 
  |