SystemAdminServices.php 16 KB

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