RoleLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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, model\RoleAuthIndex, model\Role, logic\BaseLogic};
  21. use think\facade\Config;
  22. use think\facade\Db;
  23. /**
  24. * 角色逻辑层
  25. * Class RoleLogic
  26. * @package app\adminapi\logic\auth
  27. */
  28. class RoleLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 添加角色
  32. * @param $params
  33. * @author cjhao
  34. * @date 2021/8/25 16:08
  35. */
  36. public function add(array $params)
  37. {
  38. Db::startTrans();
  39. try{
  40. $authKeys = $params['auth_keys'];
  41. //处理规格值
  42. array_walk($authKeys, function (&$auth){
  43. $auth = ['auth_key'=>$auth];
  44. });
  45. $role = new Role();
  46. $role->name = $params['name'];
  47. $role->desc = $params['desc'];
  48. $role->save();
  49. $role->roleAuthIndex()->saveAll($authKeys);
  50. Db::commit();
  51. return true;
  52. } catch (\Exception $e) {
  53. Db::rollback();
  54. return $e->getMessage();
  55. }
  56. }
  57. /**
  58. * @notes 编辑角色
  59. * @param array $params
  60. * @return bool|string
  61. * @author cjhao
  62. * @date 2021/8/25 20:47
  63. */
  64. public function edit(array $params)
  65. {
  66. Db::startTrans();
  67. try{
  68. $authKeys = $params['auth_keys'];
  69. //处理规格值
  70. array_walk($authKeys, function (&$auth){
  71. $auth = ['auth_key'=>$auth];
  72. });
  73. $role = Role::find($params['id']);
  74. RoleAuthIndex::where(['role_id'=>$params['id']])->delete();
  75. $role->name = $params['name'];
  76. $role->desc = $params['desc'];
  77. $role->save();
  78. $role->roleAuthIndex()->saveAll($authKeys);
  79. (new AdminAuthCache())->deleteTag();
  80. Db::commit();
  81. return true;
  82. } catch (\Exception $e) {
  83. Db::rollback();
  84. return $e->getMessage();
  85. }
  86. }
  87. /**
  88. * @notes 删除角色
  89. * @param int $id
  90. * @return bool
  91. * @author cjhao
  92. * @date 2021/8/25 20:47
  93. */
  94. public static function delete(int $id)
  95. {
  96. Role::destroy(['id'=>$id]);
  97. (new AdminAuthCache())->deleteTag();
  98. return true;
  99. }
  100. /**
  101. * @notes 角色详情
  102. * @param int $id
  103. * @return array
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. * @author cjhao
  108. * @date 2021/8/25 17:58
  109. */
  110. public function detail(int $id):array
  111. {
  112. $detail = Role::field('id,name,desc')
  113. ->find($id);
  114. $authList = $detail->roleAuthIndex()->select()->toArray();
  115. $authKeys = array_column($authList,'auth_key');
  116. $detail->auth_keys = $authKeys;
  117. return $detail->toArray();
  118. }
  119. }