0
0

Org.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use tools\Phptree;
  5. class Org extends Base
  6. {
  7. public function addSave(){
  8. $data = request()->post();
  9. $result = validate('Org')->check($data,[],'add');
  10. if(true !== $result){
  11. $this->error = validate('Org')->getError();
  12. return false;
  13. }
  14. if(!empty($data['location'])){
  15. $location = explode('-',$data['location']);
  16. $data['lat'] = $location[0];
  17. $data['lng'] = $location[1];
  18. }
  19. $data['create_time'] = date('Y-m-d H:i:s');
  20. $ret = $this->allowField(true)->save($data);
  21. if(!$ret){
  22. $this->error = '操作失败';
  23. return false;
  24. }
  25. if($data['type'] == 2){
  26. $this->createWxQrcode($this->id);
  27. }
  28. return true;
  29. }
  30. public function editSave(){
  31. $data = request()->post();
  32. $result = validate('Org')->check($data,[],'edit');
  33. if(true !== $result){
  34. $this->error = validate('Org')->getError();
  35. return false;
  36. }
  37. $data['update_time'] = date('Y-m-d H:i:s');
  38. if(!empty($data['location'])){
  39. $location = explode('-',$data['location']);
  40. $data['lat'] = $location[0];
  41. $data['lng'] = $location[1];
  42. }
  43. $id = $data['id'];
  44. unset($data['id']);
  45. $info = Db::name('org')->where('id',$id)->find();
  46. $data['parent_id'] = $info['parent_id'];
  47. if(isset($data['fpid'])){
  48. if($data['fpid'] != $info['parent_id']){
  49. if(isset($data['fpid']) && !empty($data['fpid'])){
  50. $data['parent_id'] = $data['fpid'];
  51. }
  52. }
  53. }
  54. $subIds = $this->getAllNextId($id);
  55. if(in_array($data['parent_id'],$subIds)){
  56. $this->error = '父集不能调整到子集下面';
  57. return false;
  58. }
  59. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  60. if(!$ret){
  61. $this->error = '操作失败';
  62. return false;
  63. }
  64. $info = Db::name('org')->where('id',$id)->find();
  65. if($info && $info['type'] == 2 && !$info['wxqrcode']){
  66. $this->createWxQrcode($id);
  67. }
  68. return true;
  69. }
  70. private function createWxQrcode($orgId){
  71. try{
  72. $config = config('app.wx_mini_config');
  73. $app = \EasyWeChat\Factory::miniProgram($config);
  74. $dir = '/uploads/qrcode';
  75. $response = $app->app_code->getUnlimit('org_'.$orgId, [
  76. 'page' => 'pages/splash/splash',
  77. 'width' => 600,
  78. ]);
  79. trace($response);
  80. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  81. $response->saveAs('.'.$dir, 'org'.$orgId.'.png');
  82. }else{
  83. exception('生成失败');
  84. }
  85. $file = $dir.'/org'.$orgId.'.png';
  86. Db::name('org')->where('id',$orgId)->setField('wxqrcode',$file);
  87. }catch (\Exception $e){
  88. trace('小程序码:'.$e->getMessage());
  89. }
  90. }
  91. public function authSave(){
  92. $orgId = input('orgId/d',0);
  93. if($orgId <= 0){
  94. $this->error = '参数错误';
  95. return false;
  96. }
  97. $ids = input('ids/a',[]);
  98. $appids = input('appids',[]);
  99. $ids = array_filter($ids,'check_val_empty');
  100. $appids = array_filter($appids,'check_val_empty');
  101. $data = [
  102. 'auths' => $ids?implode(',',$ids):'',
  103. 'appauths' => $appids?implode(',',$appids):'',
  104. 'update_time' => date('Y-m-d H:i:s')
  105. ];
  106. $ret = Db::name('org')->where('id',$orgId)->update($data);
  107. if(!$ret){
  108. $this->error = '参数错误';
  109. return false;
  110. }
  111. return true;
  112. }
  113. public function showAllTree(){
  114. //部门的就不显示了
  115. $lists = Db::name('org')->where('del',0)->order('sort asc,id asc')->select();
  116. if (empty($lists)) {
  117. return array();
  118. }
  119. $tree = Phptree::makeTree(($lists), array(
  120. 'primary_key'=>'id',
  121. 'parent_key'=>'parent_id',
  122. 'expanded' => true
  123. ));
  124. return $tree;
  125. }
  126. // 获取org有效权限 type 1=后台权限 2=app权限
  127. public function getOrgAuths($orgId,$type=1){
  128. if($type == 1){
  129. $auths = db('org')->where('id',$orgId)->value('auths');
  130. $auths = $auths?explode(',',$auths):[];
  131. if($auths){
  132. $auths = Db::name('menu')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  133. }
  134. }else{
  135. $auths = db('org')->where('id',$orgId)->value('appauths');
  136. $auths = $auths?explode(',',$auths):[];
  137. if($auths){
  138. $auths = Db::name('app_icon')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  139. }
  140. }
  141. return $auths?$auths:[];
  142. }
  143. // 获取系统内未删除的所有项目
  144. public function getAllOrgs(){
  145. $lists = Db::name('org')
  146. ->where('type',2)
  147. ->where('del',0)
  148. ->field('id,name as title')
  149. ->select();
  150. return $lists?$lists:[];
  151. }
  152. // 根据角色获取组织列表
  153. public function getListByRoles($userId){
  154. $rolesId = Db::name('user_roles')
  155. ->where('user_id',$userId)
  156. ->value('roles_id');
  157. if($rolesId == 1){ // 超级管理员
  158. }else if($rolesId == 2){ // 总公司管理员
  159. $orgs = Db::name('user')->where('id',$userId)->value('orgs');
  160. $orgs = $orgs?explode(',',$orgs):[];
  161. if($orgs){
  162. $map[] = ['id','in',$orgs];
  163. }else{
  164. $map[] = ['id','=',0];
  165. }
  166. }else{ // 组织管理员
  167. $orgId = Db::name('user_org')->where('user_id',$userId)->value('org_id');
  168. if($orgId){
  169. $map[] = ['id','=',$orgId];
  170. }else{
  171. $map[] = ['id','=',0];
  172. }
  173. }
  174. $map[] = ['type','=',2];
  175. $map[] = ['del','=',0];
  176. $map[] = ['enable','=',1];
  177. $lists = Db::name('org')
  178. ->where($map)
  179. ->field('id,name')
  180. ->select();
  181. return $lists?$lists:[];
  182. }
  183. // 根据角色获取组织tree
  184. public function getTreeByRoles($rolesId,$userId){
  185. if($rolesId == 1){ // 超级管理员
  186. }else if($rolesId == 2){ // 总公司管理员
  187. $orgs = Db::name('user')->where('id',$userId)->value('orgs');
  188. $orgs = $orgs?explode(',',$orgs):[];
  189. if($orgs){
  190. $map[] = ['id','in',$orgs];
  191. }else{
  192. $map[] = ['id','=',0];
  193. }
  194. }else{ // 组织管理员
  195. $orgId = Db::name('user_org')->where('user_id',$userId)->value('org_id');
  196. if($orgId){
  197. $map[] = ['id','=',$orgId];
  198. }else{
  199. $map[] = ['id','=',0];
  200. }
  201. }
  202. $map[] = ['type','=',2];
  203. $map[] = ['del','=',0];
  204. $map[] = ['enable','=',1];
  205. $ids = Db::name('org')
  206. ->where($map)
  207. ->column('id');
  208. $alls = $ids?$ids:[];
  209. foreach ($ids as $v){
  210. $pids = $this->getParentIds($v);
  211. $alls = array_unique(array_merge($alls,$pids));
  212. }
  213. if($alls){
  214. $lists = Db::name('org')->where('del',0)->where('enable',1)->where('id','in',$alls)->order('sort asc,id asc')->select();
  215. if (!empty($lists)) {
  216. $tree = Phptree::makeTree(($lists), array(
  217. 'primary_key'=>'id',
  218. 'parent_key'=>'parent_id',
  219. 'expanded' => true
  220. ));
  221. return $tree;
  222. }
  223. }
  224. return [];
  225. }
  226. private function getParentIds($id,&$result=[]){
  227. $pid = Db::name('org')->where('id',$id)->value('parent_id');
  228. if($pid > 0){
  229. $result[] = $pid;
  230. $this->getParentIds($pid,$result);
  231. }
  232. return $result;
  233. }
  234. public function getInfo($id){
  235. $info = $this->where('id',$id)
  236. ->find()->toArray();
  237. return $info;
  238. }
  239. // 获取所有下级的id集合
  240. public function getAllNextId($id,$data=[]){
  241. $pids = DB::name('org')->where('parent_id',$id)->column('id');
  242. if(count($pids)>0){
  243. foreach($pids as $v){
  244. $data[] = $v;
  245. $data = $this->getAllNextId($v,$data); //注意写$data 返回给上级
  246. }
  247. }
  248. return isset($data) && !empty($data) ? $data : [];
  249. }
  250. }