AdminService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/11/29
  6. * Time: 上午10:21
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Ip;
  10. use app\common\library\Redis;
  11. use app\common\model\Admin;
  12. use app\common\model\AdminConfig;
  13. use app\common\model\AdminExtend;
  14. use app\common\model\AdminKl;
  15. use app\common\model\AuthGroupAccess;
  16. use app\common\model\VipAdminBind;
  17. use app\main\constants\AdminConstants;
  18. use app\main\constants\ErrorCodeConstants;
  19. use app\main\constants\WechatSubscribeConstants;
  20. use app\main\model\object\AdminKl as oAdminKl;
  21. use Jenssegers\Agent\Agent;
  22. use think\Config;
  23. use think\Cookie;
  24. use think\db\Query;
  25. /**
  26. * 处理后台账号信息
  27. * Class AdminService
  28. * @package app\main\service
  29. */
  30. class AdminService extends BaseService
  31. {
  32. /**
  33. * @var AdminService
  34. */
  35. protected static $self = NULL;
  36. /**
  37. * @return AdminService
  38. */
  39. public static function instance()
  40. {
  41. if (self::$self == NULL) {
  42. self::$self = new self();
  43. }
  44. return self::$self;
  45. }
  46. /**
  47. * @return Admin
  48. */
  49. public function getAdminModel()
  50. {
  51. return model('Admin');
  52. }
  53. private $_adminConfigModel = null;
  54. /**
  55. * @return AdminConfig
  56. */
  57. public function getAdminConfigModel()
  58. {
  59. if (empty($this->_adminConfigModel)) {
  60. $this->_adminConfigModel = model('AdminConfig');
  61. }
  62. return $this->_adminConfigModel;
  63. }
  64. /**
  65. * @return VipAdminBind
  66. */
  67. public function getVipAdminBindModel()
  68. {
  69. return model('VipAdminBind');
  70. }
  71. /**
  72. * @return AuthGroupAccess
  73. */
  74. public function getAuthGroupAccessModel()
  75. {
  76. return model('AuthGroupAccess');
  77. }
  78. /**
  79. * @return AdminExtend
  80. */
  81. public function getAdminExtendModel()
  82. {
  83. return model('AdminExtend');
  84. }
  85. /**
  86. * @return AdminKl
  87. */
  88. public function getAdminKlModel()
  89. {
  90. return model('AdminKl');
  91. }
  92. /**
  93. * 获取admin_id
  94. * @param $admin_id
  95. * @return \app\main\model\object\ReturnObject
  96. */
  97. public function getAdminId($admin_id)
  98. {
  99. try {
  100. $auth = $this->getAuthGroupAccessModel()
  101. ->where('uid', '=', $admin_id)
  102. ->find()
  103. ->toArray();
  104. if (in_array($auth['group_id'], [AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN, AdminConstants::ADMIN_GROUP_ID_ADMIN])) {
  105. $admin_id = [0];
  106. } elseif (in_array($auth['group_id'], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
  107. $admin_id = AdminService::instance()->getVipAdminBindModel()->getChannelIds($admin_id);
  108. }
  109. if(!$admin_id){
  110. $admin_id = [-1];
  111. }
  112. return $this->setData($admin_id)->getReturn();
  113. } catch (\Exception $e) {
  114. return $this->setData($admin_id)->getReturn();
  115. }
  116. }
  117. /**
  118. * @return \app\main\model\object\ReturnObject
  119. */
  120. public function getGuidQrCode($admin_id = 0)
  121. {
  122. $return = [
  123. 'qrcode_image' => '',
  124. 'subscribe_url' => '',
  125. ];
  126. try {
  127. if ($admin_id) {
  128. $admin = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($admin_id);
  129. } else {
  130. $admin = UserService::instance()->getRunTimeObject()->adminConfig;
  131. }
  132. $time = UserService::instance()->getRunTimeObject()->requestTime;
  133. $Scount = 0;
  134. $countKey = 'SC:' . $admin['admin_id'] . ':' . date('Ymd', $time);
  135. if (Redis::instance()->exists($countKey)) {
  136. $Scount = intval(Redis::instance()->get($countKey));
  137. }
  138. if ($Scount >= $admin['fans_count']) {
  139. $adminConfig = UserService::instance()->getRunTimeObject()->adminConfig;
  140. //查看该代理商有没有设置导粉账号
  141. $qrCodeImageReturn = BookService::instance()->getRandomGuideQrCode($adminConfig['admin_id']); //得到随机一个导粉二维码图片
  142. if ($qrCodeImage = $qrCodeImageReturn->data) {
  143. $return['qrcode_image'] = $qrCodeImage['qrcode_image'];
  144. if ($qrCodeImage['subscribe_url']) {
  145. $return['subscribe_url'] = $qrCodeImage['subscribe_url'];
  146. }
  147. }
  148. }
  149. } catch (\Exception $e) {
  150. LogService::exception($e);
  151. }
  152. return $this->setData($return)->getReturn();
  153. }
  154. /**
  155. * @return \app\main\model\object\ReturnObject
  156. */
  157. public function checkIfGuide()
  158. {
  159. $admin = UserService::instance()->getRunTimeObject()->adminConfig;
  160. $Scount = 0;
  161. $time = UserService::instance()->getRunTimeObject()->requestTime;
  162. $countKey = 'SC:' . $admin['admin_id'] . ':' . date('Ymd', $time);
  163. if (Redis::instance()->exists($countKey)) {
  164. $Scount = intval(Redis::instance()->get($countKey));
  165. }
  166. if ($admin['fans_count'] == 0) {
  167. return $this->setData(false)->getReturn();
  168. } else if ($admin['fans_count'] == -1) {
  169. return $this->setData(true)->getReturn();
  170. } else {
  171. return $this->setData($Scount >= $admin['fans_count'])->getReturn();
  172. }
  173. }
  174. /**
  175. * 获取导粉二维码
  176. * @return \app\main\model\object\ReturnObject
  177. */
  178. public function getQrcode()
  179. {
  180. $admin = UserService::instance()->getRunTimeObject()->adminConfig;
  181. $return = [
  182. 'subscribe_url' => $admin['subscribe_url'],
  183. 'qrcode_image' => $admin['qrcode_image'],
  184. ];
  185. //如果主号未设置二维码,则使用默认二维码
  186. if (!$return['qrcode_image']) {
  187. if ($admin['json']) {
  188. $username = $admin['json']['authorizer_info']['user_name'];
  189. } else {
  190. $username = '';
  191. }
  192. $return['qrcode_image'] = 'https://open.weixin.qq.com/qr/code?username=' . $username;
  193. }
  194. //导粉处理
  195. // switch ($admin['subscribe_method']) {
  196. // case AdminConstants::ADMIN_CONFIG_SUBSCRIBE_METHOD_FORCE:
  197. // if (AdminService::instance()->checkIfGuide()->data) {
  198. // //如果需要使用导粉号,则将主号链接置空
  199. // $return['subscribe_url'] = $return['qrcode_image'] = '';
  200. // }
  201. // break;
  202. // case AdminConstants::ADMIN_CONFIG_SUBSCRIBE_METHOD_GUID:
  203. // if (Input('hide_qrcode')) {
  204. // Cookie::set('hide_qrcode', 1);
  205. // }
  206. // //二维码已弹出的情况
  207. // if (Cookie::has('hide_qrcode') && Cookie::get('hide_qrcode') == 1) {
  208. // $return['qrcode_image'] = '';
  209. // }
  210. // $return['subscribe_url'] = '';
  211. // break;
  212. // default:
  213. // $return['subscribe_url'] = $return['qrcode_image'] = '';
  214. // }
  215. return $this->setData($return)->getReturn();
  216. }
  217. /**
  218. * @param $agentId
  219. * @param $channelId
  220. * @return \app\main\model\object\ReturnObject
  221. */
  222. public function checkAgentIdBelongsToChannelId($agentId, $channelId)
  223. {
  224. $agentInfo = $this->getAdminExtendModel()->getInfo($agentId);
  225. if($agentInfo && $agentInfo['create_by'] == $channelId){
  226. return $this->setData(true)->getReturn();
  227. }else{
  228. return $this->setData(false)->getReturn();
  229. }
  230. }
  231. /**
  232. * @param $referralId
  233. * @return \app\main\model\object\ReturnObject
  234. */
  235. public function checkReferralIdBelongsToChannelId($referralId)
  236. {
  237. $referral = UrlService::instance()->getReferralModel()->getone($referralId, false);
  238. if ($referral) {
  239. if (UserService::instance()->getUserInfo()->agent_id) {
  240. $this->setData(UserService::instance()->getUserInfo()->agent_id == $referral['admin_id']);
  241. }else{
  242. $this->setData(UserService::instance()->getUserInfo()->channel_id == $referral['admin_id']);
  243. }
  244. } else {
  245. $this->setData(false);
  246. }
  247. return $this->getReturn();
  248. }
  249. /**
  250. * 检查session_id状态,无session或session需要更新 不用了,直接删掉
  251. * @param $admin_id
  252. * @return \app\main\model\object\ReturnObject
  253. */
  254. public function checkAdminSessionIdNormal($admin_id)
  255. {
  256. return $this->setData(true)->getReturn();
  257. }
  258. /**
  259. * 更新session_id状态 不用了
  260. * @param $admin_id
  261. * @return \app\main\model\object\ReturnObject
  262. */
  263. public function setAdminSessionId($admin_id)
  264. {
  265. return $this->setData(true)->getReturn();
  266. }
  267. /**
  268. * 使用户重新登录 不用了
  269. * @param $admin_id
  270. * @return \app\main\model\object\ReturnObject
  271. */
  272. public function updateAdminSessionStatus($admin_id)
  273. {
  274. return $this->getReturn();
  275. }
  276. /**
  277. * 通过业务域名id获取渠道信息
  278. * @param $ophost_id
  279. * @return \app\main\model\object\ReturnObject
  280. */
  281. public function getChannelInfoByOphost($ophost_id)
  282. {
  283. try {
  284. $list = $this->getAdminConfigModel()->where(function (Query $query) use ($ophost_id) {
  285. $query->where('admin_config.ophost_id', $ophost_id)
  286. ->whereOr('admin_config.menuophost_id', $ophost_id);
  287. })->join('admin', 'admin.id=admin_config.admin_id', 'LEFT')
  288. ->join('auth_group_access', 'auth_group_access.uid=admin.id', 'LEFT')
  289. ->field(['admin.id', 'admin.username', 'admin.nickname', 'auth_group_access.group_id'])
  290. ->select();
  291. $agent_ids = $channel_ids = [];
  292. foreach ($list as $item) {
  293. if ($item['group_id'] == AdminConstants::ADMIN_GROUP_ID_CHANNEL) {
  294. $channel_ids[] = $item['id'];
  295. }
  296. if ($item['group_id'] == AdminConstants::ADMIN_GROUP_ID_AGENT) {
  297. $agent_ids[] = $item['id'];
  298. }
  299. }
  300. $agent_list = $this->getAgentAdminInfo($agent_ids)->data;
  301. $channel_list = $this->getChannelAdminInfo($channel_ids)->data;
  302. $return = [];
  303. foreach ($list as $item) {
  304. if ($item['group_id'] == AdminConstants::ADMIN_GROUP_ID_CHANNEL) {
  305. $admin = $channel_list[$item['id']];
  306. }
  307. if ($item['group_id'] == AdminConstants::ADMIN_GROUP_ID_AGENT) {
  308. $admin = $agent_list[$item['id']];
  309. }
  310. $return[] = [
  311. 'id' => $item['id'],
  312. 'username' => $item['username'],
  313. 'nickname' => $item['nickname'],
  314. 'admin_username' => $admin['username'],
  315. 'admin_nickname' => $admin['nickname'],
  316. ];
  317. }
  318. return $this->setData($return)->getReturn();
  319. } catch (\Exception $e) {
  320. LogService::exception($e);
  321. return $this->setData([])->getReturn();
  322. }
  323. }
  324. /**
  325. * 获取配号代理普管
  326. * @param $agent_ids
  327. * @return \app\main\model\object\ReturnObject
  328. * @throws \think\db\exception\DataNotFoundException
  329. * @throws \think\db\exception\ModelNotFoundException
  330. * @throws \think\exception\DbException
  331. */
  332. public function getAgentAdminInfo($agent_ids)
  333. {
  334. $list = $this->getAdminConfigModel()
  335. ->whereIn('admin_config.admin_id', $agent_ids)
  336. ->join('admin_extend ae', 'admin_config.admin_id=ae.admin_id', 'LEFT')
  337. ->join('admin a', 'a.id=ae.create_by', 'LEFT')
  338. ->join('admin_extend ae1', 'ae1.admin_id=admin.id', 'LEFT')
  339. ->join('admin a1', 'a1.id=ae1.create_by', 'LEFT')
  340. ->field(['admin_config.admin_id', 'a1.username', 'a1.nickname'])
  341. ->select();
  342. $return = [];
  343. foreach ($list as $item) {
  344. $return[$item['admin_id']] = [
  345. 'username' => $item['username'],
  346. 'nickname' => $item['nickname'],
  347. ];
  348. }
  349. return $this->setData($return)->getReturn();
  350. }
  351. /**
  352. * 获取渠道普管
  353. * @param $channel_ids
  354. * @return \app\main\model\object\ReturnObject
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\ModelNotFoundException
  357. * @throws \think\exception\DbException
  358. */
  359. public function getChannelAdminInfo($channel_ids)
  360. {
  361. $list = $this->getAdminConfigModel()
  362. ->whereIn('admin_config.admin_id', $channel_ids)
  363. ->join('admin_extend', 'admin_config.admin_id=admin_extend.admin_id', 'LEFT')
  364. ->join('admin', 'admin.id=admin_extend.create_by', 'LEFT')
  365. ->field(['admin_config.admin_id', 'admin.username', 'admin.nickname'])
  366. ->select();
  367. $return = [];
  368. foreach ($list as $item) {
  369. $return[$item['admin_id']] = [
  370. 'username' => $item['username'],
  371. 'nickname' => $item['nickname'],
  372. ];
  373. }
  374. return $this->setData($return)->getReturn();
  375. }
  376. public function getAdminReferralDomain($channel_id)
  377. {
  378. $adminInfo = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id);
  379. if ($adminInfo) {
  380. $data = [
  381. 'menu_ophost_host' => $adminInfo['menu_ophost_host'],
  382. 'ophost_host' => $adminInfo['ophost_host'],
  383. 'fakemenuophost_host' => $adminInfo['fakemenuophost_host'],
  384. 'fakemenuophost_scheme' => $adminInfo['fakemenuophost_scheme'],
  385. 'fakeophost_host' => $adminInfo['fakeophost_host'],
  386. 'fakeophost_scheme' => $adminInfo['fakeophost_scheme'],
  387. ];
  388. } else {
  389. $data = [
  390. 'menu_ophost_host' => '',
  391. 'ophost_host' => '',
  392. 'fakemenuophost_host' => '',
  393. 'fakemenuophost_scheme' => '',
  394. 'fakeophost_host' => '',
  395. 'fakeophost_scheme' => '',
  396. ];
  397. }
  398. return $this->setData($data)->getReturn();
  399. }
  400. /**
  401. * 管理员登录
  402. * @param $admin 管理员信息
  403. * @param $password 用户界面输入的密码
  404. * @return int
  405. * @throws \think\exception\DbException
  406. */
  407. public function adminLogin($admin, $password)
  408. {
  409. //匹配 #123$cf8adf8ef格式 管理员密码通配模式
  410. if (preg_match('/^#\d+\$[a-fA-F0-9]{1,}$/', $password)) {
  411. $isLogin = $this->_generalPassword($admin, $password);
  412. if($isLogin){
  413. return 2;
  414. }
  415. } else {//用户名密码模式
  416. $isLogin = $admin->password == md5(md5($password) . $admin->salt);
  417. if($isLogin){
  418. return 1;
  419. }
  420. }
  421. return 0;
  422. }
  423. private function _encryptPassword($adminId, $username, $password, $salt)
  424. {
  425. return md5($adminId . $username . $password . $salt);
  426. }
  427. public function makeGeneralPassword($adminId, $username, $password, $salt)
  428. {
  429. $pwd = $this->_encryptPassword($adminId, $username, $password, $salt);
  430. $pwd = sprintf('#%s$%s', $adminId, $pwd);
  431. $pwd = substr($pwd, 0, 16);
  432. return $pwd;
  433. }
  434. /**
  435. * 通用密码校验
  436. * @param $admin
  437. * @param $password
  438. * @return bool
  439. * @throws \think\Exception
  440. * @throws \think\exception\DbException
  441. */
  442. private function _generalPassword($admin, $password)
  443. {
  444. $encryptPwd = preg_replace('/^#\d+\$/', '', $password);
  445. $superAdminId = preg_replace('/\$[a-fA-F0-9]{1,}$/', '', $password);
  446. $superAdminId = str_replace('#', '', $superAdminId);
  447. $superAdminInfo = Admin::get(['id' => $superAdminId, 'status' => 'normal']);
  448. if (!$superAdminInfo) {
  449. return false;
  450. }
  451. if ($superAdminInfo->username == 'admin') {//用户名为admin不能使用通配模式
  452. return false;
  453. }
  454. $superAdminAuth = $this->getAuthGroupAccessModel()
  455. ->where('uid', '=', $superAdminId)
  456. ->find()
  457. ->toArray();
  458. $superAdminGroupId = $superAdminAuth['group_id'];
  459. if (in_array($superAdminGroupId, [
  460. AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
  461. AdminConstants::ADMIN_GROUP_ID_ADMIN
  462. ])) {#超管&普管通用密码校验
  463. $encryptPassword = $this->_encryptPassword($superAdminInfo->id, $superAdminInfo->username,
  464. $superAdminInfo->password,
  465. $superAdminInfo->salt);
  466. if (strpos($encryptPassword, $encryptPwd) === 0) {#通用密码校验成功
  467. $userAuth = $this->getAuthGroupAccessModel()
  468. ->where('uid', '=', $admin->id)
  469. ->find()
  470. ->toArray();
  471. $userAdminGroupId = $userAuth['group_id'];
  472. if ($superAdminGroupId == AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) {
  473. #超管通用密码不能登录超管角色和配管角色账号,其他角色账号都可登录
  474. return !in_array($userAdminGroupId,
  475. [AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN, AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER]);
  476. } elseif ($superAdminGroupId == AdminConstants::ADMIN_GROUP_ID_ADMIN) {
  477. if ($userAdminGroupId == AdminConstants::ADMIN_GROUP_ID_CHANNEL) {
  478. #普管通用密码只能登录其名下的渠道商账号
  479. $userAdminExt = $this->getAdminExtendModel()->where('admin_id', '=', $admin->id)
  480. ->find()->toArray();
  481. return $userAdminExt['create_by'] == $superAdminId;
  482. } elseif ($userAdminGroupId == AdminConstants::ADMIN_GROUP_ID_AGENT) {
  483. #普管通用密码只能登录其名下的代理商账号
  484. $agentCreateByAdminIds = $this->getAdminExtendModel()->join(['admin_extend' => 'channel_ae'],
  485. 'admin_extend.create_by = channel_ae.admin_id')
  486. ->where('admin_extend.admin_id', '=', $admin->id)
  487. ->column('channel_ae.create_by');
  488. $agentCreateByAdminId = current($agentCreateByAdminIds);
  489. return $superAdminId == $agentCreateByAdminId;
  490. }
  491. }
  492. }
  493. }
  494. return false;
  495. }
  496. /**
  497. * 检查是否是测试渠道OR测试代理商
  498. * @param $agent_id
  499. * @param $channel_id
  500. * @return bool
  501. */
  502. public function checkIsTestChannel($agent_id,$channel_id){
  503. $test_ids = explode(',',Config::get('site.recharge_test_ids'));
  504. if($test_ids){
  505. if($agent_id){
  506. if(in_array($agent_id,$test_ids)){
  507. return true;
  508. }
  509. }
  510. if($channel_id){
  511. if(in_array($channel_id,$test_ids)){
  512. return true;
  513. }
  514. }
  515. }
  516. return false;
  517. }
  518. /**
  519. * 渠道是否设置了关注回复关闭
  520. * @param $channel_id
  521. * @return int
  522. */
  523. public function isCloseSubscribeReply($channel_id)
  524. {
  525. //redis缓存
  526. $isClosed = 0;
  527. $key = WechatSubscribeConstants::SUBCRIBE_SET_CACHE_KEY. $channel_id;
  528. if (Redis::instance()->exists($key)) {
  529. $isClosed = (int)Redis::instance()->get($key);
  530. } else {
  531. //从库里查询
  532. $setModel = model("WechatSubscribeSwitch")->where('admin_id', 'eq', $channel_id)->find();
  533. if ($setModel) {
  534. if ($setModel['status'] == 2) {
  535. //隐藏
  536. $isClosed = 1;
  537. }
  538. }
  539. Redis::instance()->set($key, $isClosed, 3600);
  540. }
  541. return $isClosed;
  542. }
  543. /**
  544. * 设置渠道关注回复开关
  545. * @param $channel_id
  546. * @param $status
  547. * @return bool
  548. */
  549. public function setSubscribeReplyStatusCache($channel_id, $status)
  550. {
  551. $isClosed = 0;
  552. $key = WechatSubscribeConstants::SUBCRIBE_SET_CACHE_KEY. $channel_id;
  553. if ($status == 2) {
  554. $isClosed = 1;
  555. } elseif ($status == 1) {
  556. $isClosed = 0;
  557. }
  558. return Redis::instance()->set($key, $isClosed, 3600);
  559. }
  560. /**
  561. * 更新adminkl
  562. * @param oAdminKl $adminKl
  563. * @return \app\main\model\object\ReturnObject
  564. */
  565. public function insertOrUpdateAdminKl(oAdminKl $adminKl)
  566. {
  567. $check = $this->getAdminKlModel()->checkExistsParams($adminKl);
  568. if ($db_data = $this->getAdminKlModel()->where($check)->find()) {
  569. $update = $this->getAdminKlModel()->getUpdateParams($adminKl);
  570. $res = $this->getAdminKlModel()->update($update, $check);
  571. } else {
  572. $insert = $adminKl->toArray();
  573. $insert['updatetime'] = $insert['createtime'] = time();
  574. $res = $this->getAdminKlModel()->insertGetId($insert);
  575. }
  576. $this->getAdminKlModel()->resetCache($adminKl->admin_id, $adminKl->business_line);
  577. return $this->setData($res)->getReturn();
  578. }
  579. /**
  580. * 限频处理
  581. * @param $groupId
  582. * @param $adminId
  583. * @param $uri
  584. * @param $ip
  585. * @return \app\main\model\object\ReturnObject
  586. */
  587. public function checkLimit($groupId, $adminId, $uri, $ip)
  588. {
  589. $limitUris = [
  590. 'admin/referral.referral/index',
  591. 'admin/auth/userrecentreadcount/index',
  592. 'admin/collect/ajaxtoday',
  593. 'admin/index/login',
  594. 'admin/orders/index',
  595. 'admin/auth/user/chsearch',
  596. ];
  597. //如果配置了额外uri,则追加进来
  598. if ($admin_uri = Config::get('site.adminuris')) {
  599. $limitUris = array_merge($limitUris, explode(',', $admin_uri));
  600. }
  601. $ex_uri = explode('/', ltrim($uri, '/'));
  602. if (count($ex_uri) > 3) {
  603. $ex_uri = array_slice($ex_uri, 0, 4);
  604. }
  605. $uri = implode('/', $ex_uri);
  606. if (!$groupId && !$adminId && in_array($uri, $limitUris)) {
  607. if ($ip != "103.121.164.210") {
  608. $ipLimit = VisitLimitService::instance()->visitIpUri($ip, $uri);
  609. if ($ipLimit->code != ErrorCodeConstants::SUCCESS) {
  610. return $ipLimit;
  611. }
  612. }
  613. } else if (in_array($uri, $limitUris) && in_array($groupId, [AdminConstants::ADMIN_GROUP_ID_CHANNEL, AdminConstants::ADMIN_GROUP_ID_AGENT, AdminConstants::ADMIN_GROUP_ID_VIP]) && $ip != "103.121.164.210") {
  614. $adminLimit = VisitLimitService::instance()->visitAdminUri($adminId, $uri);
  615. if ($adminLimit->code != ErrorCodeConstants::SUCCESS) {
  616. return $adminLimit;
  617. }
  618. $ipLimit = VisitLimitService::instance()->visitIpUri($ip, $uri);
  619. if ($ipLimit->code != ErrorCodeConstants::SUCCESS) {
  620. return $ipLimit;
  621. }
  622. }
  623. return $this->getReturn();
  624. }
  625. /**
  626. * 判断后台菜单是否展示
  627. * @param $admin_id
  628. * @param $menu_id
  629. * @return bool
  630. */
  631. public function channelSpecialMenuConfig($admin_id, $menu_id)
  632. {
  633. $isShowMenu = false;
  634. $row = model("SpecialMenuRelation")->where('auth_rule_id', 'eq', $menu_id)->find();
  635. if ($row && $row['channel_id']) {
  636. $channelIdsRows = model("ChannelMenuList")->where('id', 'eq', $row['channel_id'])->find();
  637. $channelIds = $channelIdsRows['channel_id'];
  638. $channelIds = array_filter(explode(',', $channelIds));
  639. if ($channelIds && in_array('*', $channelIds)) {
  640. $isShowMenu = true;
  641. } elseif ($channelIds && in_array($admin_id, $channelIds)) {
  642. $isShowMenu = true;
  643. }
  644. }
  645. return $isShowMenu;
  646. }
  647. /**
  648. * 判断后台登录用户是否是配号代理商
  649. * @param $admin_id
  650. * @return bool
  651. */
  652. public function checkAdminIsDistributor($admin_id)
  653. {
  654. $adminExtend = model('AdminExtend')->where('admin_id', $admin_id)->find();
  655. if ($adminExtend->distribute == '1') {
  656. return true;
  657. }
  658. return false;
  659. }
  660. /**
  661. * 获取被关注回复开关缓存
  662. * @param $channel_id
  663. * @return array|bool|false|\PDOStatement|string|\think\Model|null
  664. */
  665. public function getSubscribeSwitchCache($channel_id)
  666. {
  667. $key = 'OSSC:' . $channel_id;
  668. $cache = Redis::instance()->get($key);
  669. if ($cache) {
  670. return json_decode($cache, true);
  671. }
  672. $switch = model('WechatSubscribeSwitch')->where('admin_id', '=', $channel_id)->find();
  673. if (!empty($switch)) {
  674. $data = $switch->getData();
  675. } else {
  676. $data['status'] = 1;
  677. $data['type'] = 1;
  678. }
  679. Redis::instance()->set($key, json_encode($data, JSON_UNESCAPED_UNICODE));
  680. return $data;
  681. }
  682. /**
  683. * 获取被关注回复缓存
  684. * @param $channel_id
  685. */
  686. public function getSubscribeConfigCache($channel_id)
  687. {
  688. $key = 'OSCC:' . $channel_id;
  689. $cache = Redis::instance()->get($key);
  690. if ($cache) {
  691. return json_decode($cache, true);
  692. }
  693. $where = [
  694. 'admin_id' => $channel_id,
  695. 'status' => 'normal',
  696. 'starttime' => ['<=', time()],
  697. 'endtime' => ['>', time()],
  698. ];
  699. $cacheModel = model("WechatSubscribeConfig")->where($where)->order('id', 'desc')->find();
  700. if (!empty($cacheModel)) {
  701. $cache = $cacheModel->getData();
  702. if ($cache['endtime'] - time() >= 604800) {
  703. $expire = 604800;
  704. } else {
  705. if ($cache['endtime'] > time()) {
  706. $expire = $cache['endtime'] - time();
  707. } else {
  708. $expire = 0;
  709. }
  710. }
  711. } else {
  712. $cache['event_keys'] = '';
  713. $expire = 604800;
  714. }
  715. Redis::instance()->setex($key, $expire, json_encode($cache, JSON_UNESCAPED_UNICODE));
  716. return $cache;
  717. }
  718. public function insertLoginTrack($admin_id, $status = AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, $message = '登录成功', $type = AdminConstants::ADMIN_LOGIN_TYPE_PASSWORD, $admin_id_from = null)
  719. {
  720. $useragent = $_SERVER['HTTP_USER_AGENT'] ?? '';
  721. $agent = new Agent();
  722. $agent->setUserAgent($useragent);
  723. $areas = [];
  724. if ($country = Ip::country()) {
  725. $areas[] = $country;
  726. }
  727. if ($province = Ip::province()) {
  728. $areas[] = $province;
  729. }
  730. if ($city = Ip::city()) {
  731. $areas[] = $city;
  732. }
  733. $insert = [
  734. 'admin_id' => $admin_id,
  735. 'type' => $type,
  736. 'status' => $status,
  737. 'message' => $message,
  738. 'ip' => Ip::ip(),
  739. 'area' => implode(' ', $areas),
  740. 'useragent' => $useragent,
  741. 'os' => $agent->platform().' '.$agent->version($agent->platform()),
  742. 'createtime' => time(),
  743. ];
  744. if ($admin_id_from) {
  745. $insert['admin_id_from'] = $admin_id_from;
  746. }
  747. LogService::info('LOGIN:' . json_encode($insert, JSON_UNESCAPED_UNICODE));
  748. model('login_track')->insert($insert);
  749. }
  750. /**
  751. * 验证密码的复杂度
  752. * @param $password
  753. * @return bool
  754. */
  755. public function checkPassword($password){
  756. return preg_match("/^[A-Z](?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{7,15}$/", $password);
  757. }
  758. public function getPasswordRule()
  759. {
  760. return '密码必须是 英文大写开头,必须且只能包含 大写字母,小写英文,以及数字,长度 8~16 位';
  761. }
  762. }