SystemAdminServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 app\webscoket\SocketPush;
  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 app\services\system\SystemRoleServices;
  24. use crmeb\services\SystemConfigService;
  25. use think\facade\Cache;
  26. /**
  27. * 管理员service
  28. * Class SystemAdminServices
  29. * @package app\services\system\admin
  30. * @mixin SystemAdminDao
  31. */
  32. class SystemAdminServices extends BaseServices
  33. {
  34. /**
  35. * form表单创建
  36. * @var FormBuilder
  37. */
  38. protected $builder;
  39. /**
  40. * SystemAdminServices constructor.
  41. * @param SystemAdminDao $dao
  42. */
  43. public function __construct(SystemAdminDao $dao, FormBuilder $builder)
  44. {
  45. $this->dao = $dao;
  46. $this->builder = $builder;
  47. }
  48. /**
  49. * 管理员登陆
  50. * @param string $account
  51. * @param string $password
  52. * @param bool $is_mobile
  53. * @param int $adminType
  54. * @return array|\think\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, bool $is_mobile = false, int $adminType = 1)
  60. {
  61. $key = 'login_captcha_' . $account;
  62. if ($is_mobile) {
  63. $adminInfo = $this->dao->phoneByAdmin($account, $adminType);
  64. } else {
  65. $adminInfo = $this->dao->accountByAdmin($account, $adminType);
  66. }
  67. if (!$adminInfo) {
  68. Cache::inc($key);
  69. throw new AdminException('管理员不存在!');
  70. }
  71. if (!$adminInfo->status) {
  72. Cache::inc($key);
  73. throw new AdminException('您已被禁止登录!');
  74. }
  75. if (!$is_mobile && !password_verify($password, $adminInfo->pwd)) {
  76. Cache::inc($key);
  77. throw new AdminException('账号或密码错误,请重新输入');
  78. }
  79. $adminInfo->last_time = time();
  80. $adminInfo->last_ip = app('request')->ip();
  81. $adminInfo->login_count++;
  82. $adminInfo->save();
  83. return $adminInfo;
  84. }
  85. /**
  86. * 后台登陆获取菜单获取token
  87. * @param string $account
  88. * @param string $password
  89. * @param string $type
  90. * @return array
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. */
  95. public function login(string $account, string $password, string $type, bool $is_mobile = false)
  96. {
  97. $adminInfo = $this->verifyLogin($account, $password, $is_mobile, 1);
  98. $tokenInfo = $this->createToken($adminInfo->id, $type, $adminInfo['pwd']);
  99. /** @var SystemMenusServices $services */
  100. $services = app()->make(SystemMenusServices::class);
  101. [$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']);
  102. $data = SystemConfigService::more(['site_logo', 'site_logo_square', 'new_order_audio_link']);
  103. return [
  104. 'token' => $tokenInfo['token'],
  105. 'expires_time' => $tokenInfo['params']['exp'],
  106. 'menus' => $menus,
  107. 'unique_auth' => $uniqueAuth,
  108. 'user_info' => [
  109. 'id' => $adminInfo['id'],
  110. 'account' => $adminInfo['account'],
  111. 'head_pic' => $adminInfo['head_pic'],
  112. ],
  113. 'logo' => $data['site_logo'],
  114. 'logo_square' => $data['site_logo_square'],
  115. 'version' => get_crmeb_version(),
  116. 'newOrderAudioLink' => get_file_link($data['new_order_audio_link']),
  117. 'prefix' => config('admin.admin_prefix')
  118. ];
  119. }
  120. /**
  121. * 获取登陆前的login等信息
  122. * @return array
  123. */
  124. public function getLoginInfo()
  125. {
  126. $data = SystemConfigService::more(['admin_login_slide', 'site_logo_square', 'site_logo', 'login_logo']);
  127. return [
  128. 'slide' => sys_config('admin_login_slide') ?? [],
  129. 'logo_square' => $data['site_logo_square'] ?? '',//透明
  130. 'logo_rectangle' => $data['site_logo'] ?? '',//方形
  131. 'login_logo' => $data['login_logo'] ?? '',//登陆
  132. 'version' => get_crmeb_version(),
  133. 'upload_file_size_max' => config('upload.filesize'),//文件上传大小kb
  134. ];
  135. }
  136. /**
  137. * 管理员列表
  138. * @param array $where
  139. * @return array
  140. */
  141. public function getAdminList(array $where)
  142. {
  143. [$page, $limit] = $this->getPageValue();
  144. $list = $this->dao->getList($where, $page, $limit);
  145. $count = $this->dao->count($where);
  146. /** @var SystemRoleServices $service */
  147. $service = app()->make(SystemRoleServices::class);
  148. $allRole = $service->getRoleArray(['type' => 0]);
  149. foreach ($list as &$item) {
  150. if ($item['roles']) {
  151. $roles = [];
  152. foreach ($item['roles'] as $id) {
  153. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  154. }
  155. if ($roles) {
  156. $item['roles'] = implode(',', $roles);
  157. } else {
  158. $item['roles'] = '';
  159. }
  160. }
  161. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  162. $item['_last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '';
  163. }
  164. return compact('list', 'count');
  165. }
  166. /**
  167. * 创建管理员表单
  168. * @param int $level
  169. * @param array $formData
  170. * @return mixed
  171. * @throws \FormBuilder\Exception\FormBuilderException
  172. */
  173. public function createAdminForm(int $level, array $formData = [])
  174. {
  175. if (!$level) {
  176. $f[] = $this->builder->frameImage('head_pic', '头像', $this->url(config('admin.supplier_prefix') . '/widget.images/index', ['fodder' => 'head_pic'], true), $formData['head_pic'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  177. }
  178. $f[] = $this->builder->input('account', '管理员账号', $formData['account'] ?? '')->required('请填写管理员账号');
  179. if ($formData) {
  180. $f[] = $this->builder->input('pwd', '管理员密码')->type('password')->placeholder('不修改密码请留空');
  181. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->placeholder('不修改密码请留空');
  182. } else {
  183. $f[] = $this->builder->input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  184. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  185. }
  186. $f[] = $this->builder->input('real_name', '管理员姓名', $formData['real_name'] ?? '')->required('请输入管理员姓名');
  187. $f[] = $this->builder->input('phone', '管理员电话', $formData['phone'] ?? '')->required('请输入管理员电话');
  188. /** @var SystemRoleServices $service */
  189. $service = app()->make(SystemRoleServices::class);
  190. $options = $service->getRoleFormSelect($level);
  191. $roles = [];
  192. if ($formData && ($formData['roles'] ?? [])) {
  193. foreach ($formData['roles'] as $role) {
  194. $roles[] = (int)$role;
  195. }
  196. }
  197. if ($level) {
  198. $f[] = $this->builder->select('roles', '管理员身份', $roles)->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择管理员身份');
  199. }
  200. $f[] = $this->builder->radio('status', '状态', $formData['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  201. return $f;
  202. }
  203. /**
  204. * 添加管理员form表单获取
  205. * @param int $level
  206. * @return array
  207. * @throws \FormBuilder\Exception\FormBuilderException
  208. */
  209. public function createForm(int $level, string $url = '/setting/admin')
  210. {
  211. return create_form('管理员添加', $this->createAdminForm($level), $this->url($url));
  212. }
  213. /**
  214. * 创建管理员
  215. * @param array $data
  216. * @return bool
  217. */
  218. public function create(array $data)
  219. {
  220. if ($data['conf_pwd'] != $data['pwd']) {
  221. throw new AdminException('两次输入的密码不相同');
  222. }
  223. unset($data['conf_pwd']);
  224. if ($this->dao->count(['account' => $data['account'], 'admin_type' => $data['admin_type'] ?? 1, 'is_del' => 0])) {
  225. throw new AdminException('管理员账号已存在');
  226. }
  227. if ($this->dao->count(['phone' => $data['phone'], 'admin_type' => $data['admin_type'] ?? 1, 'is_del' => 0])) {
  228. throw new AdminException('管理员电话已存在');
  229. }
  230. $data['pwd'] = $this->passwordHash($data['pwd']);
  231. $data['add_time'] = time();
  232. $data['roles'] = implode(',', $data['roles']);
  233. return $this->transaction(function () use ($data) {
  234. if ($this->dao->save($data)) {
  235. \crmeb\services\CacheService::clear();
  236. return true;
  237. } else {
  238. throw new AdminException('添加失败');
  239. }
  240. });
  241. }
  242. /**
  243. * 修改管理员表单
  244. * @param int $level
  245. * @param int $id
  246. * @return array
  247. * @throws \FormBuilder\Exception\FormBuilderException
  248. */
  249. public function updateForm(int $level, int $id, string $url = '/setting/admin/')
  250. {
  251. $adminInfo = $this->dao->get($id);
  252. if (!$adminInfo) {
  253. throw new AdminException('管理员不存在!');
  254. }
  255. if ($adminInfo->is_del) {
  256. throw new AdminException('管理员已经删除');
  257. }
  258. return create_form('管理员修改', $this->createAdminForm($level, $adminInfo->toArray()), $this->url($url . $id), 'PUT');
  259. }
  260. /**
  261. * 修改管理员
  262. * @param int $id
  263. * @param array $data
  264. * @return bool
  265. */
  266. public function save(int $id, array $data)
  267. {
  268. if (!$adminInfo = $this->dao->get($id)) {
  269. throw new AdminException('管理员不存在,无法修改');
  270. }
  271. if ($adminInfo->is_del) {
  272. throw new AdminException('管理员已经删除');
  273. }
  274. //修改密码
  275. if ($data['pwd']) {
  276. if (!$data['conf_pwd']) {
  277. throw new AdminException('请输入确认密码');
  278. }
  279. if ($data['conf_pwd'] != $data['pwd']) {
  280. throw new AdminException('上次输入的密码不相同');
  281. }
  282. $adminInfo->pwd = $this->passwordHash($data['pwd']);
  283. }
  284. //修改账号
  285. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->dao->isAccountUsable($data['account'], $id)) {
  286. throw new AdminException('管理员账号已存在');
  287. }
  288. if (isset($data['phone']) && $data['phone'] != $adminInfo->phone && $this->dao->count(['phone' => $data['phone'], 'admin_type' => 1, 'is_del' => 0])) {
  289. throw new AdminException('管理员电话已存在');
  290. }
  291. if (isset($data['roles'])) {
  292. $adminInfo->roles = implode(',', $data['roles']);
  293. }
  294. $adminInfo->real_name = $data['real_name'] ?? $adminInfo->real_name;
  295. $adminInfo->phone = $data['phone'] ?? $adminInfo->phone;
  296. $adminInfo->account = $data['account'] ?? $adminInfo->account;
  297. $adminInfo->head_pic = $data['head_pic'] ?? $adminInfo->head_pic;
  298. $adminInfo->status = $data['status'];
  299. if ($adminInfo->save()) {
  300. \crmeb\services\CacheService::clear();
  301. return true;
  302. } else {
  303. return false;
  304. }
  305. }
  306. /**
  307. * 修改当前管理员信息
  308. * @param int $id
  309. * @param array $data
  310. * @return bool
  311. */
  312. public function updateAdmin(int $id, array $data)
  313. {
  314. $adminInfo = $this->dao->get($id);
  315. if (!$adminInfo)
  316. throw new AdminException('管理员信息未查到');
  317. if ($adminInfo->is_del) {
  318. throw new AdminException('管理员已经删除');
  319. }
  320. if ($data['head_pic'] != '') {
  321. $adminInfo->head_pic = $data['head_pic'];
  322. } elseif ($data['real_name'] != '') {
  323. $adminInfo->real_name = $data['real_name'];
  324. } elseif ($data['pwd'] != '') {
  325. if (!password_verify($data['pwd'], $adminInfo['pwd']))
  326. throw new AdminException('原始密码错误');
  327. if (!$data['new_pwd'])
  328. throw new AdminException('请输入新密码');
  329. if (!$data['conf_pwd'])
  330. throw new AdminException('请输入确认密码');
  331. if ($data['new_pwd'] != $data['conf_pwd'])
  332. throw new AdminException('两次输入的密码不一致');
  333. $adminInfo->pwd = $this->passwordHash($data['new_pwd']);
  334. } elseif ($data['phone'] != '') {
  335. $verifyCode = CacheService::get('code_' . $data['phone']);
  336. if (!$verifyCode)
  337. throw new AdminException('请先获取验证码');
  338. $verifyCode = substr($verifyCode, 0, 6);
  339. if ($verifyCode != $data['code']) {
  340. CacheService::delete('code_' . $data['phone']);
  341. throw new AdminException('验证码错误');
  342. }
  343. $adminInfo->phone = $data['phone'];
  344. }
  345. if ($adminInfo->save()) {
  346. CacheService::delete('code_' . $data['phone']);
  347. return true;
  348. } else {
  349. return false;
  350. }
  351. }
  352. /**
  353. * 后台订单下单,评论,支付成功,后台消息提醒
  354. */
  355. public function adminNewPush()
  356. {
  357. try {
  358. /** @var StoreOrderServices $orderServices */
  359. $orderServices = app()->make(StoreOrderServices::class);
  360. $data['ordernum'] = $orderServices->count(['is_del' => 0, 'status' => 1, 'shipping_type' => 1]);
  361. /** @var StoreProductServices $productServices */
  362. $productServices = app()->make(StoreProductServices::class);
  363. $data['inventory'] = $productServices->count(['type' => 5]);
  364. /** @var StoreProductReplyServices $replyServices */
  365. $replyServices = app()->make(StoreProductReplyServices::class);
  366. $data['commentnum'] = $replyServices->count(['is_reply' => 0]);
  367. /** @var UserExtractServices $extractServices */
  368. $extractServices = app()->make(UserExtractServices::class);
  369. $data['reflectnum'] = $extractServices->getCount(['status' => 0]);//提现
  370. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  371. SocketPush::admin()->type('ADMIN_NEW_PUSH')->data($data)->push();
  372. } catch (\Exception $e) {
  373. }
  374. }
  375. /**
  376. * 短信修改密码
  377. * @param $phone
  378. * @param $newPwd
  379. * @return bool
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. */
  384. public function resetPwd($phone, $newPwd)
  385. {
  386. $adminInfo = $this->dao->phoneByAdmin($phone);
  387. if ($adminInfo) {
  388. $adminInfo->pwd = $this->passwordHash($newPwd);
  389. $adminInfo->save();
  390. return true;
  391. } else {
  392. throw new AdminException('管理员不存在,请检查手机号码');
  393. }
  394. }
  395. /**
  396. * 获取供应商接收通知管理员
  397. * @param int $supplier_id
  398. * @param string $field
  399. * @return array
  400. * @throws \think\db\exception\DataNotFoundException
  401. * @throws \think\db\exception\DbException
  402. * @throws \think\db\exception\ModelNotFoundException
  403. */
  404. public function getNotifySupplierList(int $supplier_id, string $field = '*')
  405. {
  406. $where = [
  407. 'relation_id' => $supplier_id,
  408. 'status' => 1,
  409. 'is_del' => 0
  410. ];
  411. $list = $this->dao->getList($where, 0, 0, $field);
  412. return $list;
  413. }
  414. }