Special.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * 渠道商菜单管理
  4. * Created by: PhpStorm
  5. * User: lytian
  6. * Date: 2019/10/24
  7. * Time: 14:52
  8. */
  9. namespace app\admin\controller\auth;
  10. use app\common\controller\Backend;
  11. class Special extends Backend
  12. {
  13. protected $model = null;
  14. protected $relationModel = null;
  15. protected $channelMenuModel = null;
  16. protected $noNeedLogin = ['import'];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model("AuthRule");
  21. $this->relationModel = model("SpecialMenuRelation");
  22. $this->channelMenuModel = model("ChannelMenuList");
  23. }
  24. /**
  25. * 查看
  26. */
  27. public function index()
  28. {
  29. $this->relationSearch = true;
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags']);
  32. if ($this->request->isAjax()) {
  33. //如果发送的来源是Selectpage,则转发到Selectpage
  34. if ($this->request->request('pkey_name')) {
  35. return $this->selectpage();
  36. }
  37. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  38. $maps = [
  39. 'is_special' => ['eq', 1]
  40. ];
  41. $total = $this->model
  42. ->field("auth_rule.*, special_menu_relation.channel_id")
  43. ->join('special_menu_relation smr', 'smr.auth_rule_id=auth_rule.id', 'left')
  44. ->where($where)
  45. ->where($maps)
  46. ->order($sort, $order)
  47. ->count();
  48. $list = $this->model
  49. ->field("auth_rule.*, special_menu_relation.channel_id")
  50. ->join('special_menu_relation smr', 'smr.auth_rule_id=auth_rule.id', 'left')
  51. ->where($where)
  52. ->where($maps)
  53. ->order($sort, $order)
  54. ->limit($offset, $limit)
  55. ->select();
  56. if ($list) {
  57. foreach ($list as $row) {
  58. //查询关联的角色
  59. $groupRows = model("AuthGroup")->field("id")->where('FIND_IN_SET(:id,rules)',['id' => $row['id']])->select();
  60. $row['group_id'] = $groupRows ? implode(',', array_column($groupRows, 'id')) : '';
  61. }
  62. }
  63. $result = array("total" => $total, "rows" => $list);
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. public function operate()
  69. {
  70. if ($this->request->isAjax()) {
  71. $authRuleId = $this->request->param('auth_rule_id');
  72. $channelIds = $this->request->param('channel_ids');
  73. $operate = $this->request->param('method');
  74. $row = $this->relationModel->where('auth_rule_id', 'eq', $authRuleId)->find();
  75. if ($row) {
  76. //更新
  77. $menuRow = $this->channelMenuModel->where('id', 'eq', $row['channel_id'])->find();
  78. $hasChannelIds = explode(',', $menuRow['channel_id']);
  79. if ($hasChannelIds) {
  80. if ($operate == 'add') {
  81. if ($channelIds == "*") {
  82. $allIdsArr = model('AuthGroup')
  83. ->join('auth_group_access','auth_group.id=auth_group_access.group_id')
  84. ->field('uid')->where('FIND_IN_SET(:id,rules)',['id' => $authRuleId])->select();
  85. $updateChannelIds = $allIdsArr ? implode(',', array_column($allIdsArr, 'uid')) : "*";
  86. } else {
  87. $channelIds = explode(',', $channelIds);
  88. $updateChannelIds = implode(',', array_unique(array_merge($hasChannelIds, $channelIds)));
  89. }
  90. } else {
  91. if ($channelIds == "*") {
  92. $updateChannelIds = '';
  93. } else {
  94. $channelIds = explode(',', $channelIds);
  95. $updateChannelIdsArr = array_diff($hasChannelIds, $channelIds);
  96. $updateChannelIds = '';
  97. if ($updateChannelIdsArr) {
  98. $updateChannelIds = implode(',', $updateChannelIdsArr);
  99. }
  100. }
  101. }
  102. }
  103. model("ChannelMenuList")->update(['channel_id' => $updateChannelIds, 'updatetime' => time()],
  104. ['id' => $menuRow['id']]);
  105. } else {
  106. //新增
  107. $insertId = $this->channelMenuModel->allowField(true)->insertGetId([
  108. 'channel_id' => $channelIds,
  109. 'updatetime' => time(),
  110. 'createtime' => time()
  111. ]);
  112. $data = [
  113. 'auth_rule_id' => $authRuleId,
  114. 'channel_id' => $insertId,
  115. 'updatetime' => time(),
  116. 'createtime' => time(),
  117. ];
  118. $this->relationModel->allowField(true)->insertGetId($data);
  119. }
  120. }
  121. exit(json_encode(['code' => 200]));
  122. }
  123. /**
  124. * 导入
  125. */
  126. public function import()
  127. {
  128. if ($this->request->isPost()) {
  129. $params = $this->request->param('row/a');
  130. if (!$params['image']) {
  131. $this->error(__('Parameter %s can not be empty', 'file'));
  132. }
  133. $id = $params['ids'];
  134. $rule_id = $params['rule_id'];
  135. if (!$id || !$rule_id) {
  136. $this->error(__('缺少参数'));
  137. }
  138. $filePath = ROOT_PATH . DS . 'public' . DS . $params['image'];
  139. if (!is_file($filePath)) {
  140. $this->error(__('No results were found'));
  141. }
  142. $PHPReader = new \PHPExcel_Reader_Excel2007();
  143. if (!$PHPReader->canRead($filePath)) {
  144. $PHPReader = new \PHPExcel_Reader_Excel5();
  145. if (!$PHPReader->canRead($filePath)) {
  146. $PHPReader = new \PHPExcel_Reader_CSV();
  147. if (!$PHPReader->canRead($filePath)) {
  148. $this->error(__('Unknown data format'));
  149. }
  150. }
  151. }
  152. $PHPExcel = $PHPReader->load($filePath); //加载文件
  153. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  154. //$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  155. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  156. $maxColumnNumber = 3;
  157. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  158. for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
  159. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  160. $fields[] = $val;
  161. }
  162. }
  163. $fieldArr = ['id' => 'id', '昵称' => 'nickname', '用户名(账号)' => 'username'];
  164. $insert = [];
  165. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  166. $values = [];
  167. for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
  168. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  169. $values[] = is_null($val) ? '' : $val;
  170. }
  171. $row = [];
  172. $temp = array_combine($fields, $values);
  173. foreach ($temp as $k => $v) {
  174. if (isset($fieldArr[$k]) && $k !== '') {
  175. $row[$fieldArr[$k]] = $v;
  176. }
  177. }
  178. if ($row) {
  179. $insert[] = $row;
  180. }
  181. }
  182. $list = [];
  183. if ($insert) {
  184. //进行过滤
  185. $groupRows = model("AuthGroup")->field("id")->where('FIND_IN_SET(:id,rules)',['id' => $rule_id])->select();
  186. $groupIds = array_column($groupRows, 'id');
  187. foreach ($insert as $item) {
  188. if ($item['id']) {
  189. $admin = model("Admin")
  190. ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
  191. ->where("admin.id", 'eq', $item['id'])
  192. ->where('auth_group_access.group_id', 'in', implode(',', $groupIds))
  193. ->find();
  194. } else if ($item['username']) {
  195. $admin = model("Admin")
  196. ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
  197. ->where("admin.username", 'eq', $item['username'])
  198. ->where('auth_group_access.group_id', 'in', implode(',', $groupIds))
  199. ->find();
  200. } else if ($item['nickname']) {
  201. $admin = model("Admin")
  202. ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
  203. ->where("admin.nickname", 'eq', $item['nickname'])
  204. ->where('auth_group_access.group_id', 'in', implode(',', $groupIds))
  205. ->find();
  206. }
  207. if ($admin) {
  208. $list[] = $item;
  209. }
  210. }
  211. }
  212. if (!$list) {
  213. $this->error(__('No rows were updated'));
  214. }
  215. try {
  216. $channelIds = array_filter(array_unique(array_column($list, 'id')));
  217. $menuRow = $this->channelMenuModel->where('id', 'eq', $id)->find();
  218. $hasChannelIds = explode(',', $menuRow['channel_id']);
  219. $hastotal = 0;
  220. if ($hasChannelIds) {
  221. $hastotal = count($hasChannelIds);
  222. $total = count(array_unique(array_merge($hasChannelIds, $channelIds)));
  223. if ($hastotal == $total) {
  224. $this->error(__('No rows were updated'));
  225. }
  226. $updateChannelIds = implode(',', array_unique(array_merge($hasChannelIds, $channelIds)));
  227. model("ChannelMenuList")->update(['channel_id' => $updateChannelIds, 'updatetime' => time()], ['id' => $menuRow['id']]);
  228. } else {
  229. $total = count($channelIds);
  230. $insertId = $this->channelMenuModel->allowField(true)->insertGetId([
  231. 'channel_id' => $channelIds,
  232. 'updatetime' => time(),
  233. 'createtime' => time()
  234. ]);
  235. $data = [
  236. 'auth_rule_id' => $rule_id,
  237. 'channel_id' => $insertId,
  238. 'updatetime' => time(),
  239. 'createtime' => time(),
  240. ];
  241. $this->relationModel->allowField(true)->insertGetId($data);
  242. }
  243. } catch (\think\exception\PDOException $exception) {
  244. $this->error($exception->getMessage());
  245. }
  246. $successTotal = $total - $hastotal;
  247. $this->success("本次上传共关联{$successTotal}个账号");
  248. }
  249. $this->assign('ids', $this->request->param('ids'));
  250. $this->assign('rule_id', $this->request->param('rule_id'));
  251. return $this->view->fetch();
  252. }
  253. }