SystemAdminServices.php 15 KB

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