SystemAdminServices.php 14 KB

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