AdminLogic.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\auth;
  20. use app\common\cache\AdminAuthCache;
  21. use app\common\enum\YesNoEnum;
  22. use app\common\logic\BaseLogic;
  23. use app\common\model\Admin;
  24. use app\common\model\AdminSession;
  25. use app\common\cache\AdminTokenCache;
  26. use app\common\service\FileService;
  27. use think\Exception;
  28. use think\facade\Config;
  29. use think\facade\Db;
  30. class AdminLogic extends BaseLogic
  31. {
  32. /**
  33. * @notes 添加管理员
  34. * @param $params
  35. * @author Tab
  36. * @date 2021/7/13 11:44
  37. */
  38. public static function add($params)
  39. {
  40. $time = time();
  41. $passwordSalt = Config::get('project.unique_identification');
  42. $password = create_password($params['password'], $passwordSalt);
  43. $avatar = isset($params['avatar']) && !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : config('project.default_image.admin_avatar');
  44. $data = [
  45. 'name' => $params['name'],
  46. 'account' => $params['account'],
  47. 'avatar' => $avatar,
  48. 'password' => $password,
  49. 'role_id' => $params['role_id'],
  50. 'create_time' => $time,
  51. 'disable' => $params['disable'],
  52. 'multipoint_login' => $params['multipoint_login'],
  53. ];
  54. Admin::create($data);
  55. }
  56. /**
  57. * @notes 编辑管理员
  58. * @param $params
  59. * @return bool
  60. * @author Tab
  61. * @date 2021/7/13 11:47
  62. */
  63. public static function edit($params)
  64. {
  65. Db::startTrans();
  66. try {
  67. // admin账号不允许被禁用
  68. if ($params['account'] == 'admin' && $params['disable'] == YesNoEnum::YES) {
  69. throw new \Exception("超级管理员不允许被禁用");
  70. }
  71. $avatar = isset($params['avatar']) && !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : '';
  72. $admin = Admin::find($params['id']);
  73. $roleId = $params['role_id'];
  74. //超级管理员不允许修改菜单权限
  75. if(1 == $admin['root']){
  76. $roleId = $admin['role_id'];
  77. }
  78. $data = [
  79. 'id' => $params['id'],
  80. 'name' => $params['name'],
  81. 'account' => $params['account'],
  82. 'role_id' => $roleId,
  83. 'disable' => $params['disable'],
  84. 'avatar' => $avatar,
  85. 'multipoint_login' => $params['multipoint_login']
  86. ];
  87. if (!empty($params['password'])) {
  88. $passwordSalt = Config::get('project.unique_identification');
  89. $data['password'] = create_password($params['password'], $passwordSalt);
  90. }
  91. $role_id = Admin::where('id', $params['id'])->value('role_id');
  92. if ($params['disable'] == 1 || $role_id != $params['role_id']) {
  93. // 禁用或更换角色后,让之前登录的token都过期(无论是否支持多处登录)
  94. $tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
  95. foreach ($tokenArr as $token) {
  96. self::expireToken($token['token']);
  97. }
  98. }
  99. Admin::update($data);
  100. (new AdminAuthCache($params['id']))->clearAuthCache();
  101. Db::commit();
  102. return true;
  103. } catch (\Exception $e) {
  104. Db::rollback();
  105. self::setError($e->getMessage());
  106. return false;
  107. }
  108. }
  109. /**
  110. * @notes 删除管理员
  111. * @param $params
  112. * @return bool
  113. * @author Tab
  114. * @date 2021/7/13 11:50
  115. */
  116. public static function delete($params)
  117. {
  118. Db::startTrans();
  119. try {
  120. $admin = Admin::findOrEmpty($params['id']);
  121. if ($admin->root == YesNoEnum::YES) {
  122. throw new \Exception("超级管理员不允许被删除");
  123. }
  124. Admin::destroy($params['id']);
  125. // 删除后,让之前登录的token都过期(无论是否支持多处登录)
  126. $tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
  127. foreach ($tokenArr as $token) {
  128. self::expireToken($token['token']);
  129. }
  130. (new AdminAuthCache($params['id']))->clearAuthCache();
  131. Db::commit();
  132. return true;
  133. } catch (\Exception $e) {
  134. Db::rollback();
  135. self::setError($e->getMessage());
  136. return false;
  137. }
  138. }
  139. /**
  140. * @notes 将token变为无效
  141. * @param $token
  142. * @return false
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. * @author Tab
  147. * @date 2021/7/13 11:50
  148. */
  149. public static function expireToken($token)
  150. {
  151. $adminSession = AdminSession::where('token', '=', $token)
  152. ->with('admin')
  153. ->find();
  154. if (empty($adminSession)) {
  155. return false;
  156. }
  157. $time = time();
  158. $adminSession->expire_time = $time;
  159. $adminSession->update_time = $time;
  160. $adminSession->save();
  161. (new AdminTokenCache())->deleteAdminInfo($token);
  162. }
  163. /**
  164. * @notes 查看管理员详情
  165. * @param $params
  166. * @return array
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. * @author Tab
  171. * @date 2021/7/13 11:52
  172. */
  173. public static function detail($params)
  174. {
  175. return Admin::field('account,root,name,role_id,disable,multipoint_login,avatar')->find($params['id'])->toArray();
  176. }
  177. /**
  178. * @notes 获取管理员基本信息
  179. * @param $params
  180. * @return Admin
  181. * @author cjhao
  182. * @date 2022/4/21 15:05
  183. */
  184. public static function getAdminInfo($adminIid){
  185. return Admin::field('id,name,avatar')->find($adminIid)->toArray();
  186. }
  187. /**
  188. * @notes 修改管理员密码
  189. * @param $params
  190. * @param $adminId
  191. * @return bool|string
  192. * @author cjhao
  193. * @date 2022/4/21 15:16
  194. */
  195. public static function resetPassword($params,$adminId){
  196. try{
  197. $passwordSalt = Config::get('project.unique_identification');
  198. $password = create_password($params['password'], $passwordSalt);
  199. Admin::update(['password'=>$password],['id'=>$adminId]);
  200. return true;
  201. }catch (Exception $e) {
  202. return $e->getMessage();
  203. }
  204. }
  205. }