SystemAdminServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\admin;
  12. use app\jobs\CheckQueueJob;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\product\product\StoreProductReplyServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\system\log\SystemFileServices;
  18. use app\services\user\UserExtractServices;
  19. use crmeb\exceptions\AdminException;
  20. use app\dao\system\admin\SystemAdminDao;
  21. use app\services\system\SystemMenusServices;
  22. use app\services\other\CacheServices;
  23. use crmeb\services\CacheService;
  24. use crmeb\services\FormBuilder;
  25. use crmeb\services\workerman\ChannelService;
  26. use think\facade\Config;
  27. use think\facade\Event;
  28. use think\Model;
  29. /**
  30. * 管理员service
  31. * Class SystemAdminServices
  32. * @package app\services\system\admin
  33. * @method getAdminIds(int $level) 根据管理员等级获取管理员id
  34. * @method getOrdAdmin(string $field, int $level) 获取低于等级的管理员名称和id
  35. */
  36. class SystemAdminServices extends BaseServices
  37. {
  38. /**
  39. * form表单创建
  40. * @var FormBuilder
  41. */
  42. protected $builder;
  43. /**
  44. * SystemAdminServices constructor.
  45. * @param SystemAdminDao $dao
  46. */
  47. public function __construct(SystemAdminDao $dao, FormBuilder $builder)
  48. {
  49. $this->dao = $dao;
  50. $this->builder = $builder;
  51. }
  52. /**
  53. * 管理员登陆
  54. * @param string $account
  55. * @param string $password
  56. * @return array|bool|Model
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function verifyLogin(string $account, string $password)
  62. {
  63. $adminInfo = $this->dao->accountByAdmin($account);
  64. if (!$adminInfo || !password_verify($password, $adminInfo->pwd)) return false;
  65. if (!$adminInfo->status) {
  66. throw new AdminException('您已被禁止登录');
  67. }
  68. $adminInfo->last_time = time();
  69. $adminInfo->last_ip = app('request')->ip();
  70. $adminInfo->login_count++;
  71. $adminInfo->save();
  72. return $adminInfo;
  73. }
  74. /**
  75. * 文件管理员登陆
  76. * @param string $account
  77. * @param string $password
  78. * @return array|Model
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function verifyFileLogin(string $account, string $password)
  84. {
  85. $adminInfo = $this->dao->accountByAdmin($account);
  86. if (!$adminInfo) {
  87. throw new AdminException('管理员不存在');
  88. }
  89. if (!$adminInfo->status) {
  90. throw new AdminException('您已被禁止登录');
  91. }
  92. if (!password_verify($password, $adminInfo->file_pwd)) {
  93. throw new AdminException('账号或密码错误');
  94. }
  95. $adminInfo->last_time = time();
  96. $adminInfo->last_ip = app('request')->ip();
  97. $adminInfo->login_count++;
  98. $adminInfo->save();
  99. return $adminInfo;
  100. }
  101. /**
  102. * 后台登陆获取菜单获取token
  103. * @param string $account
  104. * @param string $password
  105. * @param string $type
  106. * @return array|bool
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function login(string $account, string $password, string $type, string $key = '')
  112. {
  113. $adminInfo = $this->verifyLogin($account, $password);
  114. if (!$adminInfo) return false;
  115. $tokenInfo = $this->createToken($adminInfo->id, $type, $adminInfo->pwd);
  116. /** @var SystemMenusServices $services */
  117. $services = app()->make(SystemMenusServices::class);
  118. [$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']);
  119. $remind = Config::get('app.console_remind', false);
  120. if ($remind) {
  121. [$queue, $timer] = Event::until('AdminLoginListener', [$key]);
  122. }
  123. //自定义事件-管理员登录
  124. event('CustomEventListener', ['admin_login', [
  125. 'id' => $adminInfo->getData('id'),
  126. 'account' => $adminInfo->getData('account'),
  127. 'head_pic' => get_file_link($adminInfo->getData('head_pic')),
  128. 'level' => $adminInfo->getData('level'),
  129. 'real_name' => $adminInfo->getData('real_name'),
  130. 'login_time' => date('Y-m-d H:i:s'),
  131. ]]);
  132. return [
  133. 'token' => $tokenInfo['token'],
  134. 'expires_time' => $tokenInfo['params']['exp'],
  135. 'menus' => $menus,
  136. 'unique_auth' => $uniqueAuth,
  137. 'user_info' => [
  138. 'id' => $adminInfo->getData('id'),
  139. 'account' => $adminInfo->getData('account'),
  140. 'head_pic' => get_file_link($adminInfo->getData('head_pic')),
  141. 'level' => $adminInfo->getData('level'),
  142. 'real_name' => $adminInfo->getData('real_name'),
  143. ],
  144. 'logo' => sys_config('site_logo'),
  145. 'logo_square' => sys_config('site_logo_square'),
  146. 'version' => get_crmeb_version(),
  147. 'newOrderAudioLink' => get_file_link(sys_config('new_order_audio_link', '')),
  148. 'queue' => $queue ?? true,
  149. 'timer' => $timer ?? true,
  150. 'site_name' => sys_config('site_name'),
  151. 'site_func' => sys_config('model_checkbox', ['seckill', 'bargain', 'combination']),
  152. ];
  153. }
  154. /**
  155. * 获取登陆前的login等信息
  156. * @return array
  157. */
  158. public function getLoginInfo()
  159. {
  160. $key = uniqid();
  161. CheckQueueJob::dispatchSecs(1, [$key]);
  162. $data = [
  163. 'slide' => sys_data('admin_login_slide') ?? [],
  164. 'logo_square' => sys_config('site_logo_square'), //透明
  165. 'logo_rectangle' => sys_config('site_logo'), //方形
  166. 'login_logo' => sys_config('login_logo'), //登陆
  167. 'site_name' => sys_config('site_name'),
  168. 'copyright' => sys_config('nncnL_crmeb_copyright', ''),
  169. 'version' => get_crmeb_version(),
  170. 'key' => $key,
  171. 'login_captcha' => 0
  172. ];
  173. try {
  174. $cacheServices = app()->make(CacheServices::class);
  175. if (!$cacheServices->checkDbCache('write_md5', get_crmeb_version_vode())) {
  176. // 执行写入数据
  177. app()->make(SystemFileServices::class)->writeMd5();
  178. $cacheServices->setDbCache('write_md5', get_crmeb_version_vode());
  179. }
  180. } catch (\ReflectionException $e) {
  181. }
  182. if (CacheService::get('login_captcha', 1) > 1) {
  183. $data['login_captcha'] = 1;
  184. }
  185. return $data;
  186. }
  187. /**
  188. * 管理员列表
  189. * @param array $where
  190. * @return array
  191. */
  192. public function getAdminList(array $where)
  193. {
  194. [$page, $limit] = $this->getPageValue();
  195. $list = $this->dao->getList($where, $page, $limit);
  196. $count = $this->dao->count($where);
  197. /** @var SystemRoleServices $service */
  198. $service = app()->make(SystemRoleServices::class);
  199. $allRole = $service->getRoleArray();
  200. foreach ($list as &$item) {
  201. if ($item['roles']) {
  202. $roles = [];
  203. foreach ($item['roles'] as $id) {
  204. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  205. }
  206. if ($roles) {
  207. $item['roles'] = implode(',', $roles);
  208. } else {
  209. $item['roles'] = '';
  210. }
  211. }
  212. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  213. $item['_last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '';
  214. }
  215. return compact('list', 'count');
  216. }
  217. /**
  218. * 创建管理员表单
  219. * @param int $level
  220. * @param array $formData
  221. * @return mixed
  222. * @throws \FormBuilder\Exception\FormBuilderException
  223. */
  224. public function createAdminForm(int $level, array $formData = [])
  225. {
  226. $f[] = $this->builder->input('account', '管理员账号', $formData['account'] ?? '')->required('请填写管理员账号');
  227. if (empty($formData)) {
  228. $f[] = $this->builder->input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  229. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  230. } else {
  231. $f[] = $this->builder->input('pwd', '管理员密码')->type('password');
  232. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password');
  233. }
  234. $f[] = $this->builder->input('real_name', '管理员姓名', $formData['real_name'] ?? '')->required('请输入管理员姓名');
  235. /** @var SystemRoleServices $service */
  236. $service = app()->make(SystemRoleServices::class);
  237. $options = $service->getRoleFormSelect($level);
  238. if (isset($formData['roles'])) {
  239. foreach ($formData['roles'] as &$item) {
  240. $item = intval($item);
  241. }
  242. }
  243. $f[] = $this->builder->select('roles', '管理员角色', $formData['roles'] ?? [])->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择管理员角色');
  244. $f[] = $this->builder->radio('status', '状态', $formData['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  245. return $f;
  246. }
  247. /**
  248. * 添加管理员form表单获取
  249. * @param int $level
  250. * @return array
  251. * @throws \FormBuilder\Exception\FormBuilderException
  252. */
  253. public function createForm(int $level)
  254. {
  255. return create_form('管理员添加', $this->createAdminForm($level), $this->url('/setting/admin'));
  256. }
  257. /**
  258. * 创建管理员
  259. * @param array $data
  260. * @return bool
  261. */
  262. public function create(array $data)
  263. {
  264. if ($data['conf_pwd'] != $data['pwd']) {
  265. throw new AdminException('两次输入的密码不一致');
  266. }
  267. unset($data['conf_pwd']);
  268. if (strlen(trim($data['pwd'])) < 6 || strlen(trim($data['pwd'])) > 32) {
  269. throw new AdminException('账号密码必须是在6到32位之间');
  270. }
  271. if ($this->dao->count(['account' => $data['account'], 'is_del' => 0])) {
  272. throw new AdminException('管理员账号已存在');
  273. }
  274. $data['pwd'] = $this->passwordHash($data['pwd']);
  275. $data['add_time'] = time();
  276. $data['roles'] = implode(',', $data['roles']);
  277. $data['head_pic'] = '/statics/system_images/admin_head_pic.png';
  278. return $this->transaction(function () use ($data) {
  279. if ($this->dao->save($data)) {
  280. return true;
  281. } else {
  282. throw new AdminException('添加失败');
  283. }
  284. });
  285. }
  286. /**
  287. * 修改管理员表单
  288. * @param int $level
  289. * @param int $id
  290. * @return array
  291. * @throws \FormBuilder\Exception\FormBuilderException
  292. */
  293. public function updateForm(int $level, int $id)
  294. {
  295. $adminInfo = $this->dao->get($id);
  296. if (!$adminInfo) {
  297. throw new AdminException('管理员不存在');
  298. }
  299. if ($adminInfo->is_del) {
  300. throw new AdminException('管理员已经删除');
  301. }
  302. return create_form('管理员修改', $this->createAdminForm($level, $adminInfo->toArray()), $this->url('/setting/admin/' . $id), 'PUT');
  303. }
  304. /**
  305. * 修改管理员
  306. * @param int $id
  307. * @param array $data
  308. * @return bool
  309. */
  310. public function save(int $id, array $data)
  311. {
  312. if (!$adminInfo = $this->dao->get($id)) {
  313. throw new AdminException('管理员不存在');
  314. }
  315. if ($adminInfo->is_del) {
  316. throw new AdminException('管理员已经删除');
  317. }
  318. //修改密码
  319. if ($data['pwd']) {
  320. if (!$data['conf_pwd']) {
  321. throw new AdminException('请输入确认密码');
  322. }
  323. if ($data['conf_pwd'] != $data['pwd']) {
  324. throw new AdminException('两次输入的密码不一致');
  325. }
  326. if (strlen(trim($data['pwd'])) < 6 || strlen(trim($data['pwd'])) > 32) {
  327. throw new AdminException('账号密码必须是在6到32位之间');
  328. }
  329. $adminInfo->pwd = $this->passwordHash($data['pwd']);
  330. }
  331. //修改账号
  332. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->dao->isAccountUsable($data['account'], $id)) {
  333. throw new AdminException('管理员账号已存在');
  334. }
  335. if (isset($data['roles'])) {
  336. $adminInfo->roles = implode(',', $data['roles']);
  337. }
  338. $adminInfo->real_name = $data['real_name'] ?? $adminInfo->real_name;
  339. $adminInfo->account = $data['account'] ?? $adminInfo->account;
  340. $adminInfo->status = $data['status'];
  341. if ($adminInfo->save()) {
  342. return true;
  343. } else {
  344. return false;
  345. }
  346. }
  347. /**
  348. * 修改当前管理员信息
  349. * @param int $id
  350. * @param array $data
  351. * @return bool
  352. */
  353. public function updateAdmin(int $id, array $data)
  354. {
  355. $adminInfo = $this->dao->get($id);
  356. if (!$adminInfo)
  357. throw new AdminException('管理员信息未查到');
  358. if ($adminInfo->is_del) {
  359. throw new AdminException('管理员已经删除');
  360. }
  361. if (!$data['real_name'])
  362. throw new AdminException('管理员姓名不能为空');
  363. if ($data['pwd']) {
  364. if (!password_verify($data['pwd'], $adminInfo['pwd']))
  365. throw new AdminException('原始密码错误');
  366. if (!$data['new_pwd'])
  367. throw new AdminException('请输入新密码');
  368. if (!$data['conf_pwd'])
  369. throw new AdminException('请输入确认密码');
  370. if ($data['new_pwd'] != $data['conf_pwd'])
  371. throw new AdminException('两次输入的密码不一致');
  372. $adminInfo->pwd = $this->passwordHash($data['new_pwd']);
  373. }
  374. $adminInfo->real_name = $data['real_name'];
  375. $adminInfo->head_pic = $data['head_pic'];
  376. if ($adminInfo->save()) {
  377. CacheService::clear();
  378. return true;
  379. } else {
  380. return false;
  381. }
  382. }
  383. /**
  384. * 设置当前管理员文件管理密码
  385. * @param int $id
  386. * @param array $data
  387. * @return bool
  388. */
  389. public function setFilePassword(int $id, array $data)
  390. {
  391. $adminInfo = $this->dao->get($id);
  392. if (!$adminInfo)
  393. throw new AdminException('管理员信息未查到');
  394. if ($adminInfo->is_del) {
  395. throw new AdminException('管理员已经删除');
  396. }
  397. if ($data['file_pwd']) {
  398. if ($adminInfo->level != 0) throw new AdminException('没有权限');
  399. if (!$data['conf_file_pwd'])
  400. throw new AdminException('请输入确认密码');
  401. if ($data['file_pwd'] != $data['conf_file_pwd'])
  402. throw new AdminException('两次输入的密码不一致');
  403. $adminInfo->file_pwd = $this->passwordHash($data['file_pwd']);
  404. }
  405. if ($adminInfo->save())
  406. return true;
  407. else
  408. return false;
  409. }
  410. /** 后台订单下单,评论,支付成功,后台消息提醒
  411. * @param $event
  412. */
  413. public function adminNewPush()
  414. {
  415. try {
  416. /** @var StoreOrderServices $orderServices */
  417. $orderServices = app()->make(StoreOrderServices::class);
  418. $data['ordernum'] = $orderServices->count(['is_del' => 0, 'status' => 1, 'shipping_type' => 1]);
  419. /** @var StoreProductServices $productServices */
  420. $productServices = app()->make(StoreProductServices::class);
  421. $data['inventory'] = $productServices->count(['type' => 5]);
  422. /** @var StoreProductReplyServices $replyServices */
  423. $replyServices = app()->make(StoreProductReplyServices::class);
  424. $data['commentnum'] = $replyServices->count(['is_reply' => 0]);
  425. /** @var UserExtractServices $extractServices */
  426. $extractServices = app()->make(UserExtractServices::class);
  427. $data['reflectnum'] = $extractServices->getCount(['status' => 0]); //提现
  428. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  429. ChannelService::instance()->send('ADMIN_NEW_PUSH', $data);
  430. } catch (\Exception $e) {
  431. }
  432. }
  433. }