12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 角色员工关系
  4. // +----------------------------------------------------------------------
  5. // | Author:
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use app\admin\model\Common;
  9. use think\Db;
  10. class Access extends Common
  11. {
  12. /**
  13. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  14. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  15. */
  16. protected $name = 'admin_access';
  17. /**
  18. * [getDataList 获取列表]
  19. * @return [array]
  20. */
  21. public function userGroup($user_id, $groups, $action = '')
  22. {
  23. if (!$user_id) {
  24. $this->error = '参数错误';
  25. return false;
  26. }
  27. $this->startTrans();
  28. try {
  29. if ($action == 'update') {
  30. $this->where('user_id', $user_id)->delete();
  31. }
  32. foreach ($groups as $k => $v) {
  33. if (!db('admin_access')->where(['user_id' => $user_id,'group_id' => $v])->find()) {
  34. $userGroup['user_id'] = $user_id;
  35. $userGroup['group_id'] = $v;
  36. $userGroups[] = $userGroup;
  37. }
  38. }
  39. $this->saveAll($userGroups);
  40. $this->commit();
  41. return true;
  42. } catch(\Exception $e) {
  43. $this->rollback();
  44. $this->error = '编辑失败';
  45. return false;
  46. }
  47. }
  48. }