Admin.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\common\model;
  3. use think\db\Query;
  4. /**
  5. * @property Role $role 关联角色
  6. */
  7. class Admin extends Base
  8. {
  9. /**
  10. * 关联角色
  11. * @return \think\model\relation\HasOne
  12. */
  13. public function role()
  14. {
  15. return $this->hasOne(Role::class, "id", "role_id");
  16. }
  17. /**
  18. * 修改管理员信息
  19. * @param $id
  20. * @param $phone
  21. * @param $roleId
  22. * @param $valid
  23. * @return array|mixed
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public static function edit($id, $phone, $roleId, $valid)
  29. {
  30. $admin = Admin::find($id);
  31. if (!$admin) {
  32. return returnFormat(999, "记录未找到");
  33. }
  34. $wherePhone = [];
  35. $wherePhone[] = ["phone", "=", $phone];
  36. $wherePhone[] = ["id", "<>", $id];
  37. $exit = Admin::where($wherePhone)->find();
  38. if ($exit) {
  39. return returnFormat(999, "该手机号已被其它管理员绑定,请更换手机号");
  40. }
  41. $admin->phone = $phone;
  42. $admin->role_id = $roleId;
  43. $admin->valid = $valid;
  44. $res = $admin->save();
  45. if ($res === false) {
  46. return returnFormat(999, "提交失败:数据库写入失败");
  47. }
  48. return returnFormat(0, "", $admin);
  49. }
  50. public static function resetPwd($id,$password){
  51. $admin = Admin::find($id);
  52. if (!$admin) {
  53. return returnFormat(999, "记录未找到");
  54. }
  55. $admin->password = self::md5($admin->salt, $password);
  56. $res = $admin->save();
  57. if ($res === false) {
  58. return returnFormat(999, "提交失败:数据库写入失败");
  59. }
  60. return returnFormat(0, "", $admin);
  61. }
  62. /**
  63. * 删除数据
  64. * @param $ids
  65. * @return array|mixed
  66. */
  67. public static function del($ids)
  68. {
  69. $whereDelete = [];
  70. $whereDelete[] = ["id", "in", $ids];
  71. $updateData = [
  72. "delete_time" => getNow(),
  73. ];
  74. // Log::record("whereDelete".print_r($whereDelete,true),"debug");
  75. // Log::record("updateData".print_r($updateData,true),"debug");
  76. $res = (new Admin())->where($whereDelete)->update($updateData);
  77. if ($res === false) {
  78. return returnFormat(999, "删除失败:数据库写入失败");
  79. }
  80. return returnFormat(0, "", $res);
  81. }
  82. /**
  83. * 添加管理员
  84. * @param $name
  85. * @param $password
  86. * @param $phone
  87. * @param $roleId
  88. * @param $valid
  89. * @return array|mixed
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public static function add($name, $password, $phone, $roleId, $valid)
  95. {
  96. $admin = Admin::where("name", $name)->find();
  97. if ($admin) {
  98. return returnFormat(999, "账号已存在,请修改账号");
  99. }
  100. $admin = Admin::where("phone", $phone)->find();
  101. if ($admin) {
  102. return returnFormat(999, "手机号已存在,请修改手机号");
  103. }
  104. $admin = new Admin([
  105. "name" => $name,
  106. "phone" => $phone,
  107. "role_id" => $roleId,
  108. "valid" => $valid,
  109. ]);
  110. $salt = rand(1000, 9999);
  111. $admin->salt = $salt;
  112. $admin->password = self::md5($salt, $password);
  113. $res = $admin->save();
  114. if ($res === false) {
  115. return returnFormat(999, "提交失败:数据库写入失败");
  116. }
  117. return returnFormat(0, "", $admin);
  118. }
  119. /**
  120. * 获取管理员列表
  121. * @param $keyword
  122. * @param $listRow
  123. * @return void
  124. * @throws \think\db\exception\DbException
  125. */
  126. public static function getList($keyword = "", $listRow = 20)
  127. {
  128. $where = [];
  129. if ($keyword) {
  130. $where[] = ["name|phone", "like", "%" . $keyword . "%"];
  131. }
  132. $list = Admin::with(['role' => function (Query $query) {
  133. $query->field("id,name,valid");
  134. }])->where($where)->order("id desc")->paginate($listRow);
  135. return returnFormat(0, '', $list);
  136. }
  137. /**
  138. * 加密密码
  139. * @param $pwd
  140. * @param $salt
  141. * @return string
  142. */
  143. private static function md5($salt, $pwd)
  144. {
  145. $str = md5($salt . $pwd);
  146. return $str;
  147. }
  148. /**
  149. * 登录接口
  150. * @param string $name
  151. * @param string $password
  152. * @return array|mixed
  153. * @throws \think\db\exception\DataNotFoundException
  154. * @throws \think\db\exception\DbException
  155. * @throws \think\db\exception\ModelNotFoundException
  156. */
  157. public static function login(string $name, string $password)
  158. {
  159. $admin = self::where('name|phone', '=', $name)->find();
  160. if (!$admin) {
  161. return returnFormat(999, "用户未找到");
  162. }
  163. if ($admin->getAttr('password') != self::md5($admin->getAttr('salt'), $password)) {
  164. return returnFormat(999, '登录密码不正确' . $admin->getAttr('salt') . $password);
  165. }
  166. if (!$admin->valid) {
  167. return returnFormat(999, "账号被禁用,请联系管理员");
  168. }
  169. //更新登录信息
  170. $admin->login_count = $admin->login_count + 1;
  171. $admin->login_last_time = getNow();
  172. $admin->token = $admin->getToken();//更新token
  173. $admin->save();
  174. return returnFormat(0, "", $admin);
  175. }
  176. /**
  177. * 获取token
  178. * @return string
  179. */
  180. public function getToken()
  181. {
  182. $expireDays = 7;//过期时间,单位天
  183. //token: md5([用户名][当前时间])|[用户id]|[过期时间]
  184. $token = base64_encode(md5($this->login_name . getNow()) . "|" . $this->id . "|" . (time() + 86400 * $expireDays));
  185. return $token;
  186. }
  187. }