SaasStaffForm.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\Admin;
  4. use app\models\AuthRole;
  5. use app\models\SaasAdminRole;
  6. use app\models\SaasAuthRole;
  7. use Yii;
  8. use yii\base\Model;
  9. class SaasStaffForm extends Model
  10. {
  11. public $id;
  12. public $data;
  13. public $edit_data;
  14. public $name;
  15. public $status;
  16. public $describe;
  17. public function rules()
  18. {
  19. return [
  20. [['id'], 'number'],
  21. [['data', 'name', 'describe', 'edit_data'], 'string'],
  22. [['status'], 'integer']
  23. ];
  24. }
  25. /**
  26. * 创建角色
  27. * @return array
  28. */
  29. public function create()
  30. {
  31. if ($this->validate()) {
  32. if (SaasAuthRole::findOne(['name' => $this->name])) {
  33. return [
  34. 'code' => 1,
  35. 'msg' => '角色名称已经存在.',
  36. ];
  37. }
  38. $data = json_decode($this->data, true);
  39. array_unshift($data, 'dashboard');
  40. $role = new SaasAuthRole();
  41. $role->name = $this->name;
  42. $role->data = json_encode($data);
  43. $role->edit_data = $this->edit_data;
  44. $role->status = $this->status;
  45. if ($this->describe) {
  46. $role->describe = $this->describe;
  47. }
  48. if ($role->save()) {
  49. return [
  50. 'code' => 0,
  51. 'msg' => '创建成功'
  52. ];
  53. }
  54. return [
  55. 'code' => 1,
  56. 'msg' => '创建失败',
  57. ];
  58. }
  59. return [
  60. 'code' => 1,
  61. 'msg' => $this->getErrorSummary(false)[0],
  62. ];
  63. }
  64. /**
  65. * 编辑角色
  66. * @return array
  67. */
  68. public function edit()
  69. {
  70. if ($this->validate()) {
  71. $role = SaasAuthRole::findOne($this->id);
  72. if (! $role) {
  73. return [
  74. 'code' => 1,
  75. 'msg' => '角色不存在',
  76. ];
  77. }
  78. $data = json_decode($this->data, true);
  79. array_unshift($data, 'dashboard');
  80. $role->name = $this->name;
  81. $role->data = json_encode($data);
  82. $role->edit_data = $this->edit_data;
  83. $role->describe = $this->describe;
  84. $role->status = $this->status;
  85. if ($role->save()) {
  86. return [
  87. 'code' => 0,
  88. 'msg' => '保存成功',
  89. ];
  90. }
  91. return [
  92. 'code' => 1,
  93. 'msg' => '保存失败',
  94. ];
  95. }
  96. return [
  97. 'code' => 1,
  98. 'msg' => $this->getErrorSummary(false)[0],
  99. ];
  100. }
  101. //员工账户列表
  102. /**
  103. * 获取员工账号列表
  104. * @return array
  105. * @throws \yii\base\InvalidConfigException
  106. */
  107. public function getAdminList()
  108. {
  109. try {
  110. $query = Admin::find()
  111. ->where([
  112. 'is_delete' => Admin::ADMIN_NORMAL,
  113. 'type' => ADMIN::ADMIN_TYPE_SAAS_STAFF
  114. ])
  115. ->select('id, created_at, username, mobile, remark, email, name, avatar');
  116. $pagination = pagination_make($query);
  117. $admins = $pagination['list'];
  118. foreach ($admins as $key => $admin) {
  119. $admins[$key]['created_at'] = date('Y-m-d H:i:s', $admin['created_at']);
  120. $admins[$key]['roles'] = Admin::findOne($admin['id'])->getSaasRoles()->select('id, name')->all();
  121. }
  122. $roles = SaasAuthRole::find()
  123. ->select('id, name')
  124. ->where([
  125. 'status' => SaasAuthRole::STATUS_NORMAL
  126. ])
  127. ->asArray()
  128. ->all();
  129. return [
  130. 'code' => 0,
  131. 'msg' => 'success',
  132. 'data' => [
  133. 'data' => $admins,
  134. 'roles' => $roles,
  135. 'pageNo' => $pagination['pageNo'],
  136. 'totalCount' => $pagination['totalCount'],
  137. ],
  138. ];
  139. } catch (\Exception $e) {
  140. return [
  141. 'code' => 1,
  142. 'msg' => $e->getMessage()
  143. ];
  144. }
  145. }
  146. //角色删除
  147. public function deteleRole()
  148. {
  149. try {
  150. if (!$this->id) {
  151. return [
  152. 'code' => 1,
  153. 'msg' => '请提供角色ID',
  154. ];
  155. }
  156. $role = SaasAuthRole::findOne($this->id);
  157. if (!$role) {
  158. throw new \Exception('角色不存在');
  159. }
  160. $adminCount = $role->getAdmins()->count();
  161. if ($adminCount > 0) {
  162. throw new \Exception('该角色还有员工使用,不能删除');
  163. }
  164. if ($role->delete()) {
  165. return [
  166. 'code' => 0,
  167. 'msg' => '删除成功',
  168. ];
  169. }
  170. throw new \Exception('删除失败');
  171. } catch (\Exception $e) {
  172. return [
  173. 'code' => 1,
  174. 'msg' => $e->getMessage(),
  175. ];
  176. }
  177. }
  178. /**
  179. * 获取角色列表
  180. * @return array
  181. */
  182. public function getRoles()
  183. {
  184. $query = SaasAuthRole::find();
  185. $pagination = pagination_make($query);
  186. $roles = $pagination['list'];
  187. foreach ($roles as $key => $role) {
  188. $roles[$key]['data'] = json_decode($role['data']);
  189. $roles[$key]['edit_data'] = [];
  190. if (!empty($role['edit_data'])) {
  191. $roles[$key]['edit_data'] = json_decode($role['edit_data'], true);
  192. }
  193. }
  194. return [
  195. 'code' => 0,
  196. 'msg' => 'success',
  197. 'data' => [
  198. 'data' => $roles,
  199. 'pageNo' => $pagination['pageNo'],
  200. 'totalCount' => $pagination['totalCount'],
  201. ],
  202. ];
  203. }
  204. /**
  205. * 格式化权限
  206. * @param array $permission
  207. * @param $key
  208. */
  209. public static function formatPermission(array &$permission, $key = null)
  210. {
  211. foreach ($permission as &$value) {
  212. if ($key) {
  213. if (isset($value['type']) && $value['type'] == 'action') {
  214. $action = explode('_', $key);
  215. $action = array_pop($action);
  216. $value['key'] = $key . '_' . $action . '@' . $value['key'];
  217. } else {
  218. $value['key'] = $key . '_' . $value['key'];
  219. }
  220. }
  221. if (isset($value['children']) && count($value['children']) > 0) {
  222. static::formatPermission($value['children'], $value['key']);
  223. }
  224. }
  225. }
  226. /**
  227. * 获取账号拥有的权限
  228. * @param null $params
  229. * @return array
  230. */
  231. public static function getAdminPermission($params = null)
  232. {
  233. $adminPermission = $params;
  234. if (! $params) {
  235. $admin = Yii::$app->jwt->getAdmin();
  236. $adminPermission = SaasAdminRole::find()->alias('ar')->where(['ar.admin_id' => $admin->id])
  237. ->leftJoin(['ad' => SaasAuthRole::tableName()], 'ad.id=ar.role_id')->select('ad.data')->column();
  238. if (count($adminPermission) > 0) {
  239. foreach ($adminPermission as &$v) {
  240. $v = json_decode($v);
  241. }
  242. $adminPermission = array_unique(array_merge(...$adminPermission));
  243. }
  244. }
  245. $result = [];
  246. foreach ($adminPermission as $value) {
  247. if (\Yii::$app->prod_is_dandianpu()) { // 供应链版本不显示联盟菜单
  248. if (strpos($value, 'saasAlliance') !== false) {
  249. continue;
  250. }
  251. }
  252. $permission = explode('_', $value);
  253. foreach ($permission as $v) {
  254. if (! isset($result[$v])) {
  255. if (strpos($v, '@') !== false) {
  256. $ex = explode('@', $v);
  257. $result[$ex[0]]['actionEntitySet'][] = [
  258. 'action' => $ex[1],
  259. ];
  260. } else {
  261. $result[$v] = [
  262. 'permissionId' => $v,
  263. ];
  264. }
  265. }
  266. }
  267. }
  268. return array_values($result);
  269. }
  270. /**
  271. * 获取所有权限key
  272. * @param $params
  273. * @param $result
  274. */
  275. public static function getAllPermission($params, &$result)
  276. {
  277. foreach ($params as $value) {
  278. $result[] = $value['key'];
  279. if (isset($value['children'])) {
  280. static::getAllPermission($value['children'], $result);
  281. }
  282. }
  283. }
  284. }