SystemAdminServices.php 13 KB

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