RoleModel.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * CommentModel.php UTF-8
  4. * 评论
  5. *
  6. * @date : 2017/11/24 16:21
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\user;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\AgentConst;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class RoleModel extends CommonModel {
  18. protected $name = 'role';
  19. protected $cache_tag = 'role_id_key';
  20. public function getRoleLevelAttr($value) {
  21. }
  22. /**
  23. * 获取角色类型说明
  24. *
  25. * @param $value
  26. *
  27. * @return string
  28. */
  29. public function getRoleTypeTextAttr($value) {
  30. if (AgentConst::ROLE_TYPE_ADMIN == $value) {
  31. return '超级管理员';
  32. } elseif (AgentConst::ROLE_TYPE_MANAGER == $value) {
  33. return '管理员';
  34. } elseif (AgentConst::ROLE_TYPE_GROUP == $value) {
  35. return '一级渠道';
  36. } elseif (AgentConst::ROLE_TYPE_AGENT == $value) {
  37. return '二级渠道';
  38. } elseif (AgentConst::ROLE_TYPE_MEMBER == $value) {
  39. return '玩家渠道';
  40. } elseif (AgentConst::ROLE_TYPE_PC == $value) {
  41. return '渠道专员';
  42. } elseif (AgentConst::ROLE_TYPE_CP == $value) {
  43. return 'CP';
  44. }
  45. return '';
  46. }
  47. /**
  48. * 获取角色名称
  49. *
  50. * @param $where
  51. * @param bool $inc_type
  52. *
  53. * @return array
  54. */
  55. public function getIdNames($where = [], $inc_type = false) {
  56. $_tag = $this->cache_tag;
  57. $_cache_key = md5($_tag.json_encode($where));
  58. $_field = 'name';
  59. if ($inc_type) {
  60. $_cache_key = 'inc_type'.$_cache_key;
  61. $_field = "name,role_type,role_type role_type_text";
  62. }
  63. $_roles = $this->where($where)
  64. ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
  65. ->column($_field, 'id');
  66. if ($inc_type) {
  67. foreach ($_roles as $_k => $_v) {
  68. $_roles[$_k]['role_type_text'] = $this->getRoleTypeTextAttr($_v['role_type']);
  69. }
  70. }
  71. return $_roles;
  72. }
  73. /**
  74. *
  75. * @param $role_id
  76. *
  77. * @return bool|mixed
  78. */
  79. public function getRoleTypeById($role_id) {
  80. if (empty($role_id)) {
  81. return false;
  82. }
  83. $_map['id'] = $role_id;
  84. $_key = 'role_type_'.$role_id;
  85. $_tag = $this->cache_tag;
  86. $_role_type = $this->where($_map)->cache($_key, CommonConst::CONST_DAY_SECONDS, $_tag)->value('role_type');
  87. if (empty($_role_type)) {
  88. return false;
  89. }
  90. return intval($_role_type);
  91. }
  92. /**
  93. * 通过角色类型获取角色ID
  94. *
  95. * @param $role_type
  96. *
  97. * @return array
  98. */
  99. public function getIdsByRoleType($role_type) {
  100. $_map = [];
  101. $_role_type = $role_type;
  102. if (is_numeric($role_type) && !empty($role_type)) {
  103. $_role_type = [$role_type];
  104. }
  105. if (!empty($role_type)) {
  106. $_map['role_type'] = ['in', $_role_type];
  107. }
  108. $_id_arr = $this->where($_map)->column('id');
  109. return $_id_arr;
  110. }
  111. public function addRole($data) {
  112. $_rs = $this->insert($data);
  113. if ($_rs != false) {
  114. Cache::clear($_tag = $this->cache_tag);
  115. }
  116. return $_rs;
  117. }
  118. public function updateRole($data) {
  119. $_rs = $this->update($data);
  120. if ($_rs != false) {
  121. Cache::clear($_tag = $this->cache_tag);
  122. }
  123. return $_rs;
  124. }
  125. public function deleteRole($id) {
  126. $_rs = $this->where(['id' => $id])->delete();
  127. if ($_rs != false) {
  128. Cache::clear($_tag = $this->cache_tag);
  129. }
  130. return $_rs;
  131. }
  132. }