BusinessService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/22
  8. * Time: 13:37
  9. */
  10. namespace business\common\services;
  11. use app\common\models\Member;
  12. use business\admin\menu\BusinessMenu;
  13. use business\common\models\Business;
  14. use business\common\models\Business as BusinessModel;
  15. use business\common\models\BusinessApply;
  16. use business\common\models\ManagerList;
  17. use app\common\helpers\Cache;
  18. use business\common\models\Staff;
  19. class BusinessService
  20. {
  21. const REDIS_KEY = 'qy_business';
  22. const BUSINESS_LIST_MSG = '请先选择要管理的企业';
  23. const LOGIN_MSG = '请登录';
  24. /*
  25. * 组装平台列表数据
  26. */
  27. public static function formPlatList($creater_id_list = [], $owner_id_list = [], $manager_id_list = [], $staff_id_list = [])
  28. {
  29. $business_id_list = array_values(array_unique(array_filter(array_merge($creater_id_list, $owner_id_list, $manager_id_list, $staff_id_list))));
  30. $platform_list = BusinessModel::uniacid()->select('id', 'logo_img', 'name')
  31. ->where('status', BusinessModel::STATUS_NORMAL)->whereIn('id', $business_id_list)->get();
  32. if ($platform_list->isNotEmpty()) {
  33. $name = empty(request()->name) && request()->name !== 0 && request()->name !== '0' ? '' : request()->name;
  34. $platform_list = $platform_list->map(function ($v) use ($creater_id_list, $owner_id_list, $manager_id_list, $staff_id_list, $name) {
  35. if ($name !== '' && !strstr($v->name, $name)) return null;
  36. $v->logo_img = yz_tomedia($v->logo_img);
  37. if (in_array($v->id, $owner_id_list)) {
  38. $v->identity = in_array($v->id, $creater_id_list) ? 5 : 4;
  39. } elseif (in_array($v->id, $creater_id_list)) {
  40. $v->identity = 3;
  41. } elseif (in_array($v->id, $manager_id_list)) {
  42. $v->identity = 2;
  43. } elseif (in_array($v->id, $staff_id_list)) {
  44. $v->identity = 1;
  45. } else {
  46. return null;
  47. }
  48. $v->identity_desc = BusinessModel::IDENTITY_DESC[$v->identity];
  49. $v->identity_type = 2;
  50. return $v;
  51. });
  52. }
  53. if (app('plugins')->isEnabled('yun-sign')) {
  54. $member = \app\frontend\models\Member::current();
  55. $person = [
  56. 'id' => 0,
  57. 'logo_img' => $member->avatar,
  58. 'name' => $member->nickname,
  59. 'identity' => 9999,
  60. 'identity_desc' => '个人空间',
  61. 'identity_type' => 1, //1个人 2企业
  62. ];
  63. $person = collect($person);
  64. $platform_list->push($person);
  65. }
  66. $platform_list = $platform_list->filter();
  67. $platform_list = $platform_list->sortByDesc('identity')->values();
  68. $apply_list = BusinessApply::uniacid()->select('id','name','logo_img')->uid(\YunShop::app()->getMemberId())->status(0)->get();
  69. if ($apply_list){
  70. $apply_list->each(function ($apply)use($platform_list){
  71. $apply->identity = 0;
  72. $apply->identity_desc = '待审核';
  73. $apply->identity_type = 3;
  74. $platform_list->push($apply);
  75. });
  76. }
  77. return $platform_list;
  78. }
  79. /*
  80. * 获取用户是否拥有某个接口的权限
  81. */
  82. public static function getPremissionByRoute($route, $module = 'admin')
  83. {
  84. $right_arr = self::checkBusinessRight();
  85. $can = 0;
  86. foreach ($right_arr['route'][$module] as $v) {
  87. if ($v['route'] == $route) {
  88. $can = $v['can'];
  89. break;
  90. }
  91. }
  92. return $can;
  93. }
  94. /*
  95. * 获取登录异常提示
  96. */
  97. public static function getLoginReturn()
  98. {
  99. return [
  100. 'extra' => '',
  101. 'i' => \YunShop::app()->uniacid,
  102. 'login_status' => 1,
  103. 'login_url' => self::getLoginUrl(),
  104. 'mid' => \request()->mid,
  105. 'scope' => '',
  106. 'type' => 5
  107. ];
  108. }
  109. /*
  110. * 获取企业管理异常提示
  111. */
  112. public static function getBusinessListReturn()
  113. {
  114. return [
  115. 'extra' => '',
  116. 'i' => \YunShop::app()->uniacid,
  117. 'login_status' => 2,
  118. 'login_url' => self::getBusinessListUrl(),
  119. 'mid' => \request()->mid,
  120. 'scope' => '',
  121. 'type' => 5
  122. ];
  123. }
  124. /*
  125. * 获取登录页面链接
  126. */
  127. public static function getLoginUrl()
  128. {
  129. return yzBusinessFullUrl('login', ['i' => \YunShop::app()->uniacid]);
  130. }
  131. /*
  132. * 获取平台列表页面链接
  133. */
  134. public static function getBusinessListUrl()
  135. {
  136. return yzBusinessFullUrl('business/index', ['i' => \YunShop::app()->uniacid]);
  137. }
  138. /*
  139. *确认企业是否已经认证
  140. */
  141. public static function checkBusinessAuth($id)
  142. {
  143. if (app('plugins')->isEnabled('yun-sign')) {
  144. if ($company_account = \Yunshop\YunSign\common\models\CompanyAccount::where('cid', $id)->first()) {
  145. if ($company_account->status == 1) {
  146. return true;
  147. }
  148. }
  149. }
  150. return false;
  151. }
  152. /*
  153. * 获取企业创建人uid
  154. */
  155. public static function getBusinessCreater($business_id = 0, $member_id = 0)
  156. {
  157. $business_id = $business_id ?: SettingService::getBusinessId();
  158. $business = BusinessModel::with(['hasOneMember' => function ($query) {
  159. $query->select('nickname', 'realname', 'avatar', 'uid', 'mobile');
  160. }])->find($business_id);
  161. if ($member_id) {
  162. return $member_id == $business->member_uid;
  163. }
  164. return $business->hasOneMember;
  165. }
  166. /*
  167. * 获取企业法人
  168. */
  169. public static function getBusinessOwner($business_id = 0, $member_id = 0)
  170. {
  171. if (!app('plugins')->isEnabled('yun-sign') || !$business_id = $business_id ?: SettingService::getBusinessId()) {
  172. return null;
  173. }
  174. $company_account = \Yunshop\YunSign\common\models\CompanyAccount::uniacid()->where('cid', $business_id)->where('status', 1)->first();
  175. if ($member_id) {
  176. return $company_account->uid == $member_id;
  177. }
  178. return $company_account->uid ? Member::uniacid()->find($company_account->uid) : null;
  179. }
  180. /*
  181. * 获取管理员
  182. */
  183. public static function getManager($business_id = 0, $member_id = 0)
  184. {
  185. $business_id = $business_id ?: SettingService::getBusinessId();
  186. // $member_id = $member_id ?: \YunShop::app()->getMemberId();
  187. $where = [['business_id', $business_id]];
  188. $with = [
  189. 'hasOneStaff' => function ($query) use ($business_id) {
  190. $query->where('business_id', $business_id);
  191. },
  192. 'hasOneMember' => function ($query) {
  193. $query->select('nickname', 'realname', 'avatar', 'uid', 'mobile');
  194. }
  195. ];
  196. if ($member_id) {
  197. $where[] = ['uid', $member_id];
  198. $manager_list = ManagerList::where($where)->with($with)->first();
  199. return $manager_list->hasOneStaff->uid ? $manager_list : null;
  200. }
  201. $manager_list = ManagerList::where($where)->with($with)->get();
  202. $manager_list = $manager_list->map(function ($v) {
  203. if ($v->hasOneStaff->uid) return $v;
  204. });
  205. return $manager_list->filter();
  206. }
  207. /*
  208. * 获取员工
  209. */
  210. public static function getStaff($business_id = 0, $member_id = 0)
  211. {
  212. $business_id = $business_id ?: SettingService::getBusinessId();
  213. $member_id = $member_id ?: \YunShop::app()->getMemberId();
  214. return Staff::where('business_id', $business_id)->where('disabled', 0)->where('uid', $member_id)->first();
  215. }
  216. public static function getModuleRedisKey($uniacid)
  217. {
  218. return self::REDIS_KEY . '_uniacidId_' . $uniacid;
  219. }
  220. public static function getBusinessRedisKey($business_id)
  221. {
  222. return self::REDIS_KEY . '_businessId_' . $business_id;
  223. }
  224. public static function getMemberRedisKey($member_id)
  225. {
  226. return self::REDIS_KEY . '_memberId_' . $member_id;
  227. }
  228. /*
  229. * 生成用户权限数据
  230. * identity:1员工 2管理员 3创建人 4法人 5创建人+法人
  231. */
  232. public static function checkBusinessRight($business_id = 0, $member_id = 0, $forget = 0)
  233. {
  234. $member_id = $member_id ?: \YunShop::app()->getMemberId();
  235. $business_id = $business_id ?: SettingService::getBusinessId();
  236. if (!$member_id || !$business_id) {
  237. return [];
  238. }
  239. $key = 'BusinessRight_MemberId' . $member_id . '_BusinessId' . $business_id;
  240. if ($forget) {
  241. self::flush(0, $member_id);
  242. }
  243. if ($return_data = Cache::get($key, null, [self::getModuleRedisKey(\YunShop::app()->uniacid), self::getBusinessRedisKey($business_id), self::getMemberRedisKey($member_id)])) {
  244. return $return_data;
  245. }
  246. $business = Business::uniacid()->find($business_id);
  247. $return_data = [
  248. 'business_id' => $business_id,
  249. 'member_id' => $member_id,
  250. 'business_name' => $business->name ?: '',
  251. ];
  252. if (self::getBusinessOwner($business_id, $member_id)) { //如果是法人
  253. $return_data['identity'] = self::getBusinessCreater($business_id, $member_id) ? 5 : 4; //判断是否同时是法人和创始人
  254. } elseif (self::getBusinessCreater($business_id, $member_id)) { //如果是创始人
  255. $return_data['identity'] = 3;
  256. } elseif (self::getManager($business_id, $member_id)) { //如果是管理员
  257. $return_data['identity'] = 2;
  258. } elseif (self::getStaff($business_id, $member_id)) { //如果是员工
  259. $return_data['identity'] = 1;
  260. }
  261. $return_data['is_cunstom'] = SpecialCheckService::isCustom($business_id, $member_id, $forget);
  262. if ($return_data['identity']) {
  263. $menu = SettingService::getMenu($business_id);
  264. $all_right = $return_data['identity'] > 1 ? 1 : 0;
  265. $return_data['route'] = self::checkBusinessRouteRight($menu['route'], $business_id, $member_id, $return_data); //获取路由权限
  266. $return_data['page_route'] = self::checkBusinessRouteRight($menu['page_route'], $business_id, $member_id, $return_data); //获取页面路由权限
  267. $return_data['page'] = self::checkBusinessPageRight($menu, $business_id, $member_id, $return_data); //获取页面侧边栏
  268. $department_id_arr = (new DepartmentPremissionService($business_id, $member_id))->getAllPremissionDepartmentId($all_right); //获取部门管理相关接口可用部门id
  269. $return_data['route_department_id'] = $department_id_arr ?: [];
  270. Cache::put($key, $return_data, 60, [self::getModuleRedisKey(\YunShop::app()->uniacid), self::getBusinessRedisKey($business_id), self::getMemberRedisKey($member_id)]);
  271. }
  272. return $return_data;
  273. }
  274. public static function checkPersonRight()
  275. {
  276. $menu_arr = \app\common\modules\shop\ShopConfig::current()->get('business_plugin_menu.YunSignPerson') ?: []; //获取插件路由和页面
  277. $class = $menu_arr['class'];
  278. $function = $menu_arr['function'];
  279. if (!method_exists($class, $function)) {
  280. return [];
  281. }
  282. $menuService = new BusinessMenu();
  283. $menu = $class::$function();
  284. $this_route = $menuService->getRoute($menu, 'YunSignPerson');
  285. $this_page = $menuService->getMenuPage($menu, 'YunSignPerson');
  286. $this_page = array_values(self::handlePage($this_page));
  287. $page_route = $menuService->getPageRoute($menu, 'YunSignPerson');
  288. $return_menu = ['route' => $this_route, 'page' => $this_page, 'page_route' => $page_route];
  289. return $return_menu;
  290. }
  291. public static function handlePage($this_page)
  292. {
  293. foreach ($this_page as $key => $item) {
  294. $this_page[$key]['name'] = $this_page[$key]['tab_name'];
  295. unset($this_page[$key]['tab_name']);
  296. unset($this_page[$key]['premit']);
  297. unset($this_page[$key]['module']);
  298. unset($this_page[$key]['identity']);
  299. unset($this_page[$key]['page_name']);
  300. unset($this_page[$key]['special_check']);
  301. if ($item['child']) {
  302. $item['child'] = array_values($item['child']);
  303. $this_page[$key]['child'] = self::handlePage($item['child']);
  304. }
  305. }
  306. return $this_page;
  307. }
  308. /*
  309. * 确认用户页面权限
  310. */
  311. public static function checkBusinessPageRight($menu = [], $business_id = 0, $uid = 0, $auth = [])
  312. {
  313. $menu = $menu ?: SettingService::getMenu();
  314. $page = $menu['page'] ?: [];
  315. $class = (new DepartmentPremissionService($business_id, $uid));
  316. $right_arr = $class->getRightArr();
  317. $return_data = [];
  318. foreach ($page as $k => $v) {
  319. if ($res = self::foreachPage($v, $right_arr, $auth)) {
  320. $return_data = array_merge($return_data, $res);
  321. }
  322. }
  323. return $return_data;
  324. }
  325. /*
  326. * 递归处理页面权限数据
  327. */
  328. public static function foreachPage(&$page_menu, &$right_arr, &$auth)
  329. {
  330. $identity = $auth['identity'];
  331. foreach ($page_menu as $k => $v) {
  332. if ($v['identity'] && $identity < $v['identity']) {
  333. unset($page_menu[$k]);
  334. continue;
  335. }
  336. if ($v['premit'] == 1 && $identity < 2 && $right_arr->where('route', $v['route'])->where('module', $v['module'])->isEmpty()) {
  337. unset($page_menu[$k]);
  338. continue;
  339. }
  340. if ($v['premit'] == 1 && $v['special_check']) { //如果不是客服账号,屏蔽芸客服相关路由
  341. if ($v['special_check'] == 'isCustom' && !$check_res = SpecialCheckService::isCustom($auth['business_id'], $auth['member_id'])) {
  342. unset($page_menu[$k]);
  343. continue;
  344. }
  345. if ($v['special_check'] == 'onlyBind' && !$check_res = SpecialCheckService::onlyBind($auth['business_id'])) {
  346. unset($page_menu[$k]);
  347. continue;
  348. }
  349. }
  350. $page_menu[$k] = [
  351. 'name' => $v['tab_name'],
  352. 'route' => $v['route'],
  353. 'child' => []
  354. ];
  355. if ($v['child']) {
  356. $page_menu[$k]['child'] = self::foreachPage($v['child'], $right_arr, $auth);
  357. }
  358. }
  359. $page_menu = array_values($page_menu);
  360. return $page_menu;
  361. }
  362. /*
  363. * 确认用户路由权限
  364. */
  365. public static function checkBusinessRouteRight($route = [], $business_id = 0, $uid = 0, $auth = [])
  366. {
  367. $identity = $auth['identity'];
  368. $all_right = $identity > 1 ? 1 : 0;
  369. $route = $route ?: [];
  370. $business_id = $business_id ?: SettingService::getBusinessId();
  371. $uid = $uid ?: \YunShop::app()->uniacid;
  372. if ($all_right) { //如果是管理员、企业主、法人,获取所有权限
  373. foreach ($route as $k => $v) {
  374. foreach ($route[$k] as $kk => &$vv) {
  375. $vv['can'] = 1;
  376. if ($identity < $vv['identity']) { //判断当前用户身份等級是否满足需求
  377. $vv['can'] = 0;
  378. }
  379. if (!$vv['can'] || !$vv['special_check']) {
  380. continue;
  381. }
  382. switch ($vv['special_check']) {
  383. case 'isCustom': //判断是否客服
  384. $vv['can'] = SpecialCheckService::isCustom($business_id, $uid) ? $vv['can'] : 0;
  385. break;
  386. case 'onlyBind':
  387. $vv['can'] = SpecialCheckService::onlyBind($business_id) ? $vv['can'] : 0;
  388. break;
  389. }
  390. }
  391. }
  392. } else {
  393. $class = (new DepartmentPremissionService($business_id, $uid));
  394. $right_arr = $class->getRightArr(); //获取允许访问的路由
  395. if (!$right_arr || $right_arr->isEmpty()) {
  396. return $route;
  397. }
  398. foreach ($route as $k => $v) {
  399. foreach ($route[$k] as $kk => &$vv) {
  400. if ($right_arr->where('route', $vv['route'])->isNotEmpty()) {
  401. $vv['can'] = 1;
  402. }
  403. if ($vv['identity'] && $identity < $vv['identity']) {
  404. $vv['can'] = 0;
  405. }
  406. if (!$vv['can'] || !$vv['special_check']) {
  407. continue;
  408. }
  409. switch ($vv['special_check']) {
  410. case 'isCustom':
  411. $vv['can'] = SpecialCheckService::isCustom($business_id, $uid) ? $vv['can'] : 0;
  412. break;
  413. case 'onlyBind':
  414. $vv['can'] = SpecialCheckService::onlyBind($business_id) ? $vv['can'] : 0;
  415. break;
  416. }
  417. }
  418. }
  419. }
  420. return $route;
  421. }
  422. /*
  423. * 清除企业相关缓存
  424. */
  425. public static function flush($business_id = 0, $member_id = 0, $uniacid = 0)
  426. {
  427. $tags = [];
  428. if ($business_id) {
  429. if (is_array($business_id)) {
  430. foreach ($business_id as $v) {
  431. $tags[] = self::getBusinessRedisKey($v);
  432. }
  433. } else {
  434. $tags[] = self::getBusinessRedisKey($business_id);
  435. }
  436. }
  437. if ($member_id) {
  438. if (is_array($member_id)) {
  439. foreach ($member_id as $v) {
  440. $tags[] = self::getMemberRedisKey($v);
  441. }
  442. } else {
  443. $tags[] = self::getMemberRedisKey($member_id);
  444. }
  445. }
  446. if ($uniacid) {
  447. if (is_array($uniacid)) {
  448. foreach ($uniacid as $v) {
  449. $tags[] = self::getModuleRedisKey($v);
  450. }
  451. } else {
  452. $tags[] = self::getModuleRedisKey($uniacid);
  453. }
  454. }
  455. if ($tags) {
  456. Cache::flush($tags);
  457. }
  458. }
  459. }