Org.php 9.6 KB

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