UsersRoles.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class UsersRoles extends Model
  5. {
  6. protected $connection = 'mysql';
  7. protected $pk = 'id';
  8. protected $name = 'users_roles';
  9. public static function getsonid($id)
  10. {
  11. $data = self::where(['weid' => weid(), 'pid' => $id])
  12. ->select()->toArray();
  13. $returndata = '';
  14. if (!empty($data)) {
  15. foreach ($data as $vo) {
  16. if ($returndata) {
  17. $returndata = $returndata . "," . $vo['id'];
  18. } else {
  19. $returndata = $vo['id'];
  20. }
  21. }
  22. }
  23. if (!empty($returndata)) {
  24. return explode(',', $returndata);
  25. }
  26. }
  27. public static function getadminids()
  28. {
  29. $data = self::where(['weid' => weid(), 'sid' => 0, 'ocid' => 0])
  30. ->field('id,title')->select()->toArray();
  31. $returndata = '';
  32. if (!empty($data)) {
  33. foreach ($data as $vo) {
  34. if ($returndata) {
  35. $returndata = $returndata . "," . $vo['id'];
  36. } else {
  37. $returndata = $vo['id'];
  38. }
  39. }
  40. }
  41. if (!empty($returndata)) {
  42. return explode(',', $returndata);
  43. }
  44. }
  45. public static function getallarray($where = [])
  46. {
  47. $query = self::where([
  48. 'weid' => weid(),
  49. 'ocid' => (int) $where['ocid'],
  50. 'tzid' => (int) $where['tzid'],
  51. 'sid' => (int) $where['sid']
  52. ])->field('id,title')->order('id asc');
  53. $list = $query->select()->toArray();
  54. if (empty($list)) {
  55. UsersRoles::datainitial();
  56. $list = $list = $query->select()->toArray();
  57. }
  58. $array = [];
  59. foreach ($list as $k => $v) {
  60. $array[$k]['val'] = $v['id'];
  61. $array[$k]['key'] = $v['title'];
  62. }
  63. return $array;
  64. }
  65. public static function getpcarray($where = [])
  66. {
  67. $data = self::field('id,pid,title')->where([
  68. 'weid' => weid(),
  69. 'ocid' => (int) $where['ocid'],
  70. 'tzid' => (int) $where['tzid'],
  71. 'sid' => (int) $where['sid']
  72. ])->order('id asc')->select()->toArray();
  73. $datalist = [];
  74. foreach ($data as $key => $vo) {
  75. $datalist[$key]['val'] = $vo['id'];
  76. $datalist[$key]['key'] = $vo['title'];
  77. $datalist[$key]['pid'] = $vo['pid'];
  78. }
  79. return $datalist;
  80. }
  81. public static function getPid($id)
  82. {
  83. $data = self::find($id);
  84. if (!empty($data)) {
  85. return $data->pid;
  86. }
  87. }
  88. public static function getdatarules($role_id)
  89. {
  90. $UsersRoles = self::find($role_id);
  91. if (!empty($UsersRoles)) {
  92. return $UsersRoles->datarules;
  93. }
  94. }
  95. public static function getinfo($user)
  96. {
  97. $UsersRoles = [];
  98. if ($user['role_id'] == 0) {
  99. if ($user['username'] == 'admin') {
  100. $UsersRoles['title'] = '创始人';
  101. $UsersRoles['is_console'] = 1;
  102. $UsersRoles['is_allrole'] = 1;
  103. $UsersRoles['access'] = 'all';
  104. $UsersRoles['status'] = 1;
  105. } elseif (!empty($user['sid'])) {
  106. //店铺创始人
  107. $storeusers = Users::where(['sid' => $user['sid']])->order('id asc')->find();
  108. if ($storeusers->uuid == $user['uuid']) {
  109. $UsersRoles['title'] = '店铺创始人';
  110. $UsersRoles['is_allrole'] = 1;
  111. $UsersRoles['access'] = 'all';
  112. $UsersRoles['status'] = 1;
  113. }
  114. } elseif (!empty($user['ocid'])) {
  115. //城市代理创始人
  116. $storeusers = Users::where(['ocid' => $user['ocid']])->order('id asc')->find();
  117. if ($storeusers->uuid == $user['uuid']) {
  118. $UsersRoles['title'] = '城市代理创始人';
  119. $UsersRoles['is_allrole'] = 1;
  120. $UsersRoles['access'] = 'all';
  121. $UsersRoles['status'] = 1;
  122. }
  123. } elseif (!empty($user['tzid'])) {
  124. //社区
  125. $storeusers = Users::where(['tzid' => $user['tzid']])->order('id asc')->find();
  126. if ($storeusers->uuid == $user['uuid']) {
  127. $UsersRoles['title'] = '社区创始人';
  128. $UsersRoles['is_allrole'] = 1;
  129. $UsersRoles['access'] = 'all';
  130. $UsersRoles['status'] = 1;
  131. }
  132. } else {
  133. $group_access = \think\facade\Db::name('auth_group_access')->where('uid', $user['id'])->find();
  134. if (!empty($group_access)) {
  135. $group = \think\facade\Db::name('auth_group')->where('id', $group_access['group_id'])->find();
  136. if (!empty($group)) {
  137. $UsersRoles = self::where(['title' => $group['name']])->find();
  138. if (!empty($UsersRoles)) {
  139. Users::update(['id' => $user['id'], 'role_id' => $UsersRoles['id']]);
  140. }
  141. }
  142. }
  143. }
  144. } else {
  145. $UsersRoles = self::find($user['role_id']);
  146. if (!empty($UsersRoles)) {
  147. $UsersRoles = $UsersRoles->toArray();
  148. if ($UsersRoles['weid'] > 0) {
  149. $UsersRoles['is_console'] = 0;
  150. }
  151. if (weid() > 0 && $UsersRoles['is_console'] == 1) {
  152. $UsersRoles['access'] = 'all';
  153. }
  154. if ($UsersRoles['is_allrole'] == 1) {
  155. $UsersRoles['access'] = 'all';
  156. }
  157. }
  158. }
  159. return $UsersRoles;
  160. }
  161. public static function datainitial()
  162. {
  163. $weid = weid();
  164. self::create([
  165. 'weid' => $weid,
  166. 'title' => '超级管理员',
  167. 'access' => 'all',
  168. 'status' => 1
  169. ]);
  170. }
  171. }