123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace app\common\model;
- use think\db\Query;
- /**
- * @property Role $role 关联角色
- */
- class Admin extends Base
- {
- /**
- * 关联角色
- * @return \think\model\relation\HasOne
- */
- public function role()
- {
- return $this->hasOne(Role::class, "id", "role_id");
- }
- /**
- * 修改管理员信息
- * @param $id
- * @param $phone
- * @param $roleId
- * @param $valid
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- 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);
- }
- /**
- * 删除数据
- * @param $ids
- * @return array|mixed
- */
- public static function del($ids)
- {
- $whereDelete = [];
- $whereDelete[] = ["id", "in", $ids];
- $updateData = [
- "delete_time" => getNow(),
- ];
- // Log::record("whereDelete".print_r($whereDelete,true),"debug");
- // Log::record("updateData".print_r($updateData,true),"debug");
- $res = (new Admin())->where($whereDelete)->update($updateData);
- if ($res === false) {
- return returnFormat(999, "删除失败:数据库写入失败");
- }
- return returnFormat(0, "", $res);
- }
- /**
- * 添加管理员
- * @param $name
- * @param $password
- * @param $phone
- * @param $roleId
- * @param $valid
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- 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);
- }
- /**
- * 获取管理员列表
- * @param $keyword
- * @param $listRow
- * @return void
- * @throws \think\db\exception\DbException
- */
- 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);
- }
- /**
- * 加密密码
- * @param $pwd
- * @param $salt
- * @return string
- */
- private static function md5($salt, $pwd)
- {
- $str = md5($salt . $pwd);
- return $str;
- }
- /**
- * 登录接口
- * @param string $name
- * @param string $password
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- 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();//更新token
- $admin->save();
- return returnFormat(0, "", $admin);
- }
- /**
- * 获取token
- * @return string
- */
- public function getToken()
- {
- $expireDays = 7;//过期时间,单位天
- //token: md5([用户名][当前时间])|[用户id]|[过期时间]
- $token = base64_encode(md5($this->login_name . getNow()) . "|" . $this->id . "|" . (time() + 86400 * $expireDays));
- return $token;
- }
- }
|