Org.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  46. if(!$ret){
  47. $this->error = '操作失败';
  48. return false;
  49. }
  50. $info = Db::name('org')->where('id',$id)->find();
  51. if($info && $info['type'] == 2 && !$info['wxqrcode']){
  52. $this->createWxQrcode($id);
  53. }
  54. return true;
  55. }
  56. private function createWxQrcode($orgId){
  57. try{
  58. $config = config('app.wx_mini_config');
  59. $app = \EasyWeChat\Factory::miniProgram($config);
  60. $dir = '/uploads/qrcode';
  61. $response = $app->app_code->getUnlimit('org_'.$orgId, [
  62. 'page' => 'pages/index/index',
  63. 'width' => 600,
  64. ]);
  65. trace($response);
  66. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  67. $response->saveAs('.'.$dir, 'org'.$orgId.'.png');
  68. }else{
  69. exception('生成失败');
  70. }
  71. $file = $dir.'/org'.$orgId.'.png';
  72. Db::name('org')->where('id',$orgId)->setField('wxqrcode',$file);
  73. }catch (\Exception $e){
  74. trace('小程序码:'.$e->getMessage());
  75. }
  76. }
  77. public function authSave(){
  78. $orgId = input('orgId/d',0);
  79. if($orgId <= 0){
  80. $this->error = '参数错误';
  81. return false;
  82. }
  83. $ids = input('ids/a',[]);
  84. $appids = input('appids',[]);
  85. $ids = array_filter($ids,'check_val_empty');
  86. $appids = array_filter($appids,'check_val_empty');
  87. $data = [
  88. 'auths' => $ids?implode(',',$ids):'',
  89. 'appauths' => $appids?implode(',',$appids):'',
  90. 'update_time' => date('Y-m-d H:i:s')
  91. ];
  92. $ret = Db::name('org')->where('id',$orgId)->update($data);
  93. if(!$ret){
  94. $this->error = '参数错误';
  95. return false;
  96. }
  97. return true;
  98. }
  99. public function showAllTree(){
  100. //部门的就不显示了
  101. $lists = Db::name('org')->where('del',0)->order('sort asc,id asc')->select();
  102. if (empty($lists)) {
  103. return array();
  104. }
  105. $tree = Phptree::makeTree(($lists), array(
  106. 'primary_key'=>'id',
  107. 'parent_key'=>'parent_id',
  108. 'expanded' => true
  109. ));
  110. return $tree;
  111. }
  112. // 获取org有效权限 type 1=后台权限 2=app权限
  113. public function getOrgAuths($orgId,$type=1){
  114. if($type == 1){
  115. $auths = db('org')->where('id',$orgId)->value('auths');
  116. $auths = $auths?explode(',',$auths):[];
  117. if($auths){
  118. $auths = Db::name('menu')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  119. }
  120. }else{
  121. $auths = db('org')->where('id',$orgId)->value('appauths');
  122. $auths = $auths?explode(',',$auths):[];
  123. if($auths){
  124. $auths = Db::name('app_icon')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  125. }
  126. }
  127. return $auths?$auths:[];
  128. }
  129. // 获取系统内未删除的所有项目
  130. public function getAllOrgs(){
  131. $lists = Db::name('org')
  132. ->where('type',2)
  133. ->where('del',0)
  134. ->field('id,name as title')
  135. ->select();
  136. return $lists?$lists:[];
  137. }
  138. // 根据角色获取组织列表
  139. public function getListByRoles($userId){
  140. $rolesId = Db::name('user_roles')
  141. ->where('user_id',$userId)
  142. ->value('roles_id');
  143. if($rolesId == 1){ // 超级管理员
  144. }else if($rolesId == 2){ // 总公司管理员
  145. $orgs = Db::name('user')->where('id',$userId)->value('orgs');
  146. $orgs = $orgs?explode(',',$orgs):[];
  147. if($orgs){
  148. $map[] = ['id','in',$orgs];
  149. }else{
  150. $map[] = ['id','=',0];
  151. }
  152. }else{ // 组织管理员
  153. $orgId = Db::name('user_org')->where('user_id',$userId)->value('org_id');
  154. if($orgId){
  155. $map[] = ['id','=',$orgId];
  156. }else{
  157. $map[] = ['id','=',0];
  158. }
  159. }
  160. $map[] = ['type','=',2];
  161. $map[] = ['del','=',0];
  162. $map[] = ['enable','=',1];
  163. $lists = Db::name('org')
  164. ->where($map)
  165. ->field('id,name')
  166. ->select();
  167. return $lists?$lists:[];
  168. }
  169. // 根据角色获取组织tree
  170. public function getTreeByRoles($rolesId,$userId){
  171. if($rolesId == 1){ // 超级管理员
  172. }else if($rolesId == 2){ // 总公司管理员
  173. $orgs = Db::name('user')->where('id',$userId)->value('orgs');
  174. $orgs = $orgs?explode(',',$orgs):[];
  175. if($orgs){
  176. $map[] = ['id','in',$orgs];
  177. }else{
  178. $map[] = ['id','=',0];
  179. }
  180. }else{ // 组织管理员
  181. $orgId = Db::name('user_org')->where('user_id',$userId)->value('org_id');
  182. if($orgId){
  183. $map[] = ['id','=',$orgId];
  184. }else{
  185. $map[] = ['id','=',0];
  186. }
  187. }
  188. $map[] = ['type','=',2];
  189. $map[] = ['del','=',0];
  190. $map[] = ['enable','=',1];
  191. $ids = Db::name('org')
  192. ->where($map)
  193. ->column('id');
  194. $alls = $ids?$ids:[];
  195. foreach ($ids as $v){
  196. $pids = $this->getParentIds($v);
  197. $alls = array_unique(array_merge($alls,$pids));
  198. }
  199. if($alls){
  200. $lists = Db::name('org')->where('del',0)->where('enable',1)->where('id','in',$alls)->order('sort asc,id asc')->select();
  201. if (!empty($lists)) {
  202. $tree = Phptree::makeTree(($lists), array(
  203. 'primary_key'=>'id',
  204. 'parent_key'=>'parent_id',
  205. 'expanded' => true
  206. ));
  207. return $tree;
  208. }
  209. }
  210. return [];
  211. }
  212. private function getParentIds($id,&$result=[]){
  213. $pid = Db::name('org')->where('id',$id)->value('parent_id');
  214. if($pid > 0){
  215. $result[] = $pid;
  216. $this->getParentIds($pid,$result);
  217. }
  218. return $result;
  219. }
  220. public function getInfo($id){
  221. $info = $this->where('id',$id)
  222. ->find()->toArray();
  223. return $info;
  224. }
  225. }