SystemAdminServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. * 后台登陆获取菜单获取token
  76. * @param string $account
  77. * @param string $password
  78. * @param string $type
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function login(string $account, string $password, string $type, string $key = '')
  85. {
  86. $adminInfo = $this->verifyLogin($account, $password);
  87. $tokenInfo = $this->createToken($adminInfo->id, $type);
  88. /** @var SystemMenusServices $services */
  89. $services = app()->make(SystemMenusServices::class);
  90. [$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']);
  91. $remind = Config::get('app.console_remind', false);
  92. if ($remind) {
  93. [$queue, $timer] = Event::until('admin.login', [$key]);
  94. }
  95. return [
  96. 'token' => $tokenInfo['token'],
  97. 'expires_time' => $tokenInfo['params']['exp'],
  98. 'menus' => $menus,
  99. 'unique_auth' => $uniqueAuth,
  100. 'user_info' => [
  101. 'id' => $adminInfo->getData('id'),
  102. 'account' => $adminInfo->getData('account'),
  103. 'head_pic' => $adminInfo->getData('head_pic'),
  104. ],
  105. 'logo' => sys_config('site_logo'),
  106. 'logo_square' => sys_config('site_logo_square'),
  107. 'version' => get_crmeb_version(),
  108. 'newOrderAudioLink' => get_file_link(sys_config('new_order_audio_link', '')),
  109. 'queue' => $queue ?? true,
  110. 'timer' => $timer ?? true
  111. ];
  112. }
  113. /**
  114. * 获取登陆前的login等信息
  115. * @return array
  116. */
  117. public function getLoginInfo()
  118. {
  119. $key = uniqid();
  120. event('admin.info', [$key]);
  121. return [
  122. 'slide' => sys_data('admin_login_slide') ?? [],
  123. 'logo_square' => sys_config('site_logo_square'),//透明
  124. 'logo_rectangle' => sys_config('site_logo'),//方形
  125. 'login_logo' => sys_config('login_logo'),//登陆
  126. 'site_name' => sys_config('site_name'),
  127. 'key' => $key
  128. ];
  129. }
  130. /**
  131. * 管理员列表
  132. * @param array $where
  133. * @return array
  134. */
  135. public function getAdminList(array $where)
  136. {
  137. [$page, $limit] = $this->getPageValue();
  138. $list = $this->dao->getList($where, $page, $limit);
  139. $count = $this->dao->count($where);
  140. /** @var SystemRoleServices $service */
  141. $service = app()->make(SystemRoleServices::class);
  142. $allRole = $service->getRoleArray();
  143. foreach ($list as &$item) {
  144. if ($item['roles']) {
  145. $roles = [];
  146. foreach ($item['roles'] as $id) {
  147. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  148. }
  149. if ($roles) {
  150. $item['roles'] = implode(',', $roles);
  151. } else {
  152. $item['roles'] = '';
  153. }
  154. }
  155. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  156. $item['_last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '';
  157. }
  158. return compact('list', 'count');
  159. }
  160. /**
  161. * 创建管理员表单
  162. * @param int $level
  163. * @param array $formData
  164. * @return mixed
  165. * @throws \FormBuilder\Exception\FormBuilderException
  166. */
  167. public function createAdminForm(int $level, array $formData = [])
  168. {
  169. $f[] = $this->builder->input('account', '管理员账号', $formData['account'] ?? '')->required('请填写管理员账号');
  170. $f[] = $this->builder->input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  171. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  172. $f[] = $this->builder->input('real_name', '管理员姓名', $formData['real_name'] ?? '')->required('请输入管理员姓名');
  173. /** @var SystemRoleServices $service */
  174. $service = app()->make(SystemRoleServices::class);
  175. $options = $service->getRoleFormSelect($level);
  176. if (isset($formData['roles'])) {
  177. foreach ($formData['roles'] as &$item) {
  178. $item = intval($item);
  179. }
  180. }
  181. $f[] = $this->builder->select('roles', '管理员身份', $formData['roles'] ?? [])->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择管理员身份');
  182. $f[] = $this->builder->radio('status', '状态', $formData['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  183. return $f;
  184. }
  185. /**
  186. * 添加管理员form表单获取
  187. * @param int $level
  188. * @return array
  189. * @throws \FormBuilder\Exception\FormBuilderException
  190. */
  191. public function createForm(int $level)
  192. {
  193. return create_form('管理员添加', $this->createAdminForm($level), $this->url('/setting/admin'));
  194. }
  195. /**
  196. * 创建管理员
  197. * @param array $data
  198. * @return bool
  199. */
  200. public function create(array $data)
  201. {
  202. if ($data['conf_pwd'] != $data['pwd']) {
  203. throw new AdminException(400264);
  204. }
  205. unset($data['conf_pwd']);
  206. if ($this->dao->count(['account' => $data['account'], 'is_del' => 0])) {
  207. throw new AdminException(400596);
  208. }
  209. $data['pwd'] = $this->passwordHash($data['pwd']);
  210. $data['add_time'] = time();
  211. $data['roles'] = implode(',', $data['roles']);
  212. return $this->transaction(function () use ($data) {
  213. if ($this->dao->save($data)) {
  214. \think\facade\Cache::clear();
  215. return true;
  216. } else {
  217. throw new AdminException(100022);
  218. }
  219. });
  220. }
  221. /**
  222. * 修改管理员表单
  223. * @param int $level
  224. * @param int $id
  225. * @return array
  226. * @throws \FormBuilder\Exception\FormBuilderException
  227. */
  228. public function updateForm(int $level, int $id)
  229. {
  230. $adminInfo = $this->dao->get($id);
  231. if (!$adminInfo) {
  232. throw new AdminException(400594);
  233. }
  234. if ($adminInfo->is_del) {
  235. throw new AdminException(400452);
  236. }
  237. return create_form('管理员修改', $this->createAdminForm($level, $adminInfo->toArray()), $this->url('/setting/admin/' . $id), 'PUT');
  238. }
  239. /**
  240. * 修改管理员
  241. * @param int $id
  242. * @param array $data
  243. * @return bool
  244. */
  245. public function save(int $id, array $data)
  246. {
  247. if (!$adminInfo = $this->dao->get($id)) {
  248. throw new AdminException(400594);
  249. }
  250. if ($adminInfo->is_del) {
  251. throw new AdminException(400452);
  252. }
  253. //修改密码
  254. if ($data['pwd']) {
  255. if (!$data['conf_pwd']) {
  256. throw new AdminException(400263);
  257. }
  258. if ($data['conf_pwd'] != $data['pwd']) {
  259. throw new AdminException(400264);
  260. }
  261. $adminInfo->pwd = $this->passwordHash($data['pwd']);
  262. }
  263. //修改账号
  264. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->dao->isAccountUsable($data['account'], $id)) {
  265. throw new AdminException(400596);
  266. }
  267. if (isset($data['roles'])) {
  268. $adminInfo->roles = implode(',', $data['roles']);
  269. }
  270. $adminInfo->real_name = $data['real_name'] ?? $adminInfo->real_name;
  271. $adminInfo->account = $data['account'] ?? $adminInfo->account;
  272. $adminInfo->status = $data['status'];
  273. if ($adminInfo->save()) {
  274. \think\facade\Cache::clear();
  275. return true;
  276. } else {
  277. return false;
  278. }
  279. }
  280. /**
  281. * 修改当前管理员信息
  282. * @param int $id
  283. * @param array $data
  284. * @return bool
  285. */
  286. public function updateAdmin(int $id, array $data)
  287. {
  288. $adminInfo = $this->dao->get($id);
  289. if (!$adminInfo)
  290. throw new AdminException(400451);
  291. if ($adminInfo->is_del) {
  292. throw new AdminException(400452);
  293. }
  294. if (!$data['real_name'])
  295. throw new AdminException(400453);
  296. if ($data['pwd']) {
  297. if (!password_verify($data['pwd'], $adminInfo['pwd']))
  298. throw new AdminException(400597);
  299. if (!$data['new_pwd'])
  300. throw new AdminException(400598);
  301. if (!$data['conf_pwd'])
  302. throw new AdminException(400263);
  303. if ($data['new_pwd'] != $data['conf_pwd'])
  304. throw new AdminException(400264);
  305. $adminInfo->pwd = $this->passwordHash($data['new_pwd']);
  306. }
  307. $adminInfo->real_name = $data['real_name'];
  308. $adminInfo->head_pic = $data['head_pic'];
  309. if ($adminInfo->save())
  310. return true;
  311. else
  312. return false;
  313. }
  314. /** 后台订单下单,评论,支付成功,后台消息提醒
  315. * @param $event
  316. */
  317. public function adminNewPush()
  318. {
  319. try {
  320. /** @var StoreOrderServices $orderServices */
  321. $orderServices = app()->make(StoreOrderServices::class);
  322. $data['ordernum'] = $orderServices->count(['is_del' => 0, 'status' => 1, 'shipping_type' => 1]);
  323. /** @var StoreProductServices $productServices */
  324. $productServices = app()->make(StoreProductServices::class);
  325. $data['inventory'] = $productServices->count(['type' => 5]);
  326. /** @var StoreProductReplyServices $replyServices */
  327. $replyServices = app()->make(StoreProductReplyServices::class);
  328. $data['commentnum'] = $replyServices->count(['is_reply' => 0]);
  329. /** @var UserExtractServices $extractServices */
  330. $extractServices = app()->make(UserExtractServices::class);
  331. $data['reflectnum'] = $extractServices->getCount(['status' => 0]);//提现
  332. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  333. ChannelService::instance()->send('ADMIN_NEW_PUSH', $data);
  334. } catch (\Exception $e) {
  335. }
  336. }
  337. }