AdminRepository.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\system\admin;
  11. //附件
  12. use app\common\dao\system\admin\AdminDao;
  13. use app\common\model\system\admin\Admin;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\repositories\system\auth\RoleRepository;
  16. use crmeb\exceptions\AuthException;
  17. use crmeb\services\JwtTokenService;
  18. use FormBuilder\Exception\FormBuilderException;
  19. use FormBuilder\Factory\Elm;
  20. use FormBuilder\Form;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. use think\exception\ValidateException;
  25. use think\facade\Cache;
  26. use think\facade\Config;
  27. use think\facade\Route;
  28. use think\Model;
  29. /**
  30. * Class BaseRepository
  31. * @package common\repositories
  32. * @mixin AdminDao
  33. */
  34. class AdminRepository extends BaseRepository
  35. {
  36. public function __construct(AdminDao $dao)
  37. {
  38. /**
  39. * @var AdminDao
  40. */
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * @param array $where
  45. * @param $page
  46. * @param $limit
  47. * @return array
  48. * @throws DataNotFoundException
  49. * @throws DbException
  50. * @throws ModelNotFoundException
  51. * @author xaboy
  52. * @day 2020-04-09
  53. */
  54. public function getList(array $where, $page, $limit)
  55. {
  56. $query = $this->dao->search($where);
  57. $count = $query->count();
  58. $list = $query->page($page, $limit)->hidden(['pwd', 'is_del', 'update_time'])->select();
  59. foreach ($list as $k => $role) {
  60. $list[$k]['rule_name'] = $role->roleNames();
  61. $list[$k]['status'] = (int)$role['status'];
  62. }
  63. return compact('list', 'count');
  64. }
  65. public function passwordEncode($password)
  66. {
  67. return password_hash($password, PASSWORD_BCRYPT);
  68. }
  69. /**
  70. * 更新
  71. * @param int $id id
  72. * @param array $data 数组
  73. * @return int
  74. * @throws DbException
  75. * @author 张先生
  76. * @date 2020-03-26
  77. */
  78. public function update(int $id, array $data)
  79. {
  80. if (isset($data['roles']))
  81. $data['roles'] = implode(',', $data['roles']);
  82. return $this->dao->update($id, $data);
  83. }
  84. /**
  85. * @param int $id
  86. * @param $isSelf
  87. * @return Form
  88. * @throws FormBuilderException
  89. * @author xaboy
  90. * @day 2020-04-20
  91. */
  92. public function passwordForm(int $id, $isSelf = false)
  93. {
  94. $form = Elm::createForm(Route::buildUrl($isSelf ? 'systemAdminEditPassword' : 'systemAdminPassword', $isSelf ? [] : compact('id'))->build(), [
  95. $rules[] = Elm::password('pwd', '密码')->required(),
  96. $rules[] = Elm::password('againPassword', '确认密码')->required(),
  97. ]);
  98. return $form->setTitle('修改密码');
  99. }
  100. /**
  101. * @param array $formData
  102. * @return Form
  103. * @throws FormBuilderException
  104. * @author xaboy
  105. * @day 2020-04-20
  106. */
  107. public function editForm(array $formData)
  108. {
  109. $form = Elm::createForm(Route::buildUrl('systemAdminEdit')->build());
  110. $form->setRule([
  111. Elm::input('real_name', '管理员姓名')->required(),
  112. Elm::input('phone', '联系电话')
  113. ]);
  114. return $form->setTitle('修改信息')->formData($formData);
  115. }
  116. /**
  117. * @param int|null $id
  118. * @param array $formData
  119. * @return Form
  120. * @throws FormBuilderException
  121. * @author xaboy
  122. * @day 2020-04-08
  123. */
  124. public function form(?int $id = null, array $formData = []): Form
  125. {
  126. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemAdminCreate')->build() : Route::buildUrl('systemAdminUpdate', ['id' => $id])->build());
  127. if(isset($formData['status'])){
  128. $formData['status'] = (int)$formData['status'];
  129. }
  130. $rules = [
  131. Elm::select('roles', '身份', [])->options(function () {
  132. $data = app()->make(RoleRepository::class)->getAllOptions(0);
  133. $options = [];
  134. foreach ($data as $value => $label) {
  135. $options[] = compact('value', 'label');
  136. }
  137. return $options;
  138. })->multiple(true),
  139. Elm::input('real_name', '管理员姓名'),
  140. Elm::input('account', '账号')->required(),
  141. Elm::input('phone', ' 联系电话'),
  142. ];
  143. if (!$id) {
  144. $rules[] = Elm::password('pwd', '密码')->required();
  145. $rules[] = Elm::password('againPassword', '确认密码')->required();
  146. }
  147. $rules[] = Elm::switches('status', '是否开启', 1)->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启');
  148. $form->setRule($rules);
  149. return $form->setTitle(is_null($id) ? '添加管理员' : '编辑管理员')->formData($formData);
  150. }
  151. /**
  152. * @param int $id
  153. * @return Form
  154. * @throws DataNotFoundException
  155. * @throws DbException
  156. * @throws FormBuilderException
  157. * @throws ModelNotFoundException
  158. * @author xaboy
  159. * @day 2020-04-09
  160. */
  161. public function updateForm(int $id)
  162. {
  163. return $this->form($id, $this->dao->get($id)->toArray());
  164. }
  165. /**
  166. * @param string $account
  167. * @param string $password
  168. * @return array|Model|null
  169. * @throws DataNotFoundException
  170. * @throws DbException
  171. * @throws ModelNotFoundException
  172. * @author xaboy
  173. * @day 2020-04-10
  174. */
  175. public function login(string $account, string $password)
  176. {
  177. $adminInfo = $this->dao->accountByAdmin($account);
  178. if (!$adminInfo)
  179. throw new ValidateException('账号不存在');
  180. if ($adminInfo['status'] != 1)
  181. throw new ValidateException('账号已关闭');
  182. if (!password_verify($password, $adminInfo->pwd))
  183. throw new ValidateException('账号或密码错误');
  184. $adminInfo->last_time = date('Y-m-d H:i:s');
  185. $adminInfo->last_ip = app('request')->ip();
  186. $adminInfo->login_count++;
  187. $adminInfo->save();
  188. return $adminInfo;
  189. }
  190. /**
  191. * @param string $token
  192. * @param int $exp
  193. * @author xaboy
  194. * @day 2020-04-10
  195. */
  196. public function cacheToken(string $token, int $exp)
  197. {
  198. Cache::set('admin_' . $token, time() + $exp, $exp);
  199. }
  200. public function checkToken(string $token)
  201. {
  202. $has = Cache::has('admin_' . $token);
  203. if (!$has)
  204. throw new AuthException('无效的token');
  205. $lastTime = Cache::get('admin_' . $token);
  206. if (($lastTime + intval(Config::get('admin.token_valid_exp', 15)) * 60) > time())
  207. throw new AuthException('token 已过期');
  208. }
  209. public function updateToken(string $token)
  210. {
  211. Cache::set('admin_' . $token, time(), intval(Config::get('admin.token_valid_exp', 15)) * 60);
  212. }
  213. public function clearToken(string $token)
  214. {
  215. Cache::delete('admin_' . $token);
  216. }
  217. /**
  218. * @param Admin $admin
  219. * @return array
  220. * @author xaboy
  221. * @day 2020-04-09
  222. */
  223. public function createToken(Admin $admin)
  224. {
  225. $service = new JwtTokenService();
  226. $token = $service->createToken($admin->admin_id, 'admin');
  227. $this->cacheToken($token['token'], $token['out']);
  228. return $token;
  229. }
  230. /**
  231. * 检测验证码
  232. * @param string $key key
  233. * @param string $code 验证码
  234. * @author 张先生
  235. * @date 2020-03-26
  236. */
  237. public function checkCode(string $key, string $code)
  238. {
  239. $_code = Cache::get('am_captcha' . $key);
  240. if (!$_code) {
  241. throw new ValidateException('验证码过期');
  242. }
  243. if (strtolower($_code) != strtolower($code)) {
  244. throw new ValidateException('验证码错误');
  245. }
  246. //删除code
  247. Cache::delete('am_captcha' . $key);
  248. }
  249. /**
  250. * @param string $code
  251. * @return string
  252. * @author xaboy
  253. * @day 2020-04-09
  254. */
  255. public function createLoginKey(string $code)
  256. {
  257. $key = uniqid(microtime(true), true);
  258. Cache::set('am_captcha' . $key, $code, Config::get('admin.captcha_exp', 5) * 60);
  259. return $key;
  260. }
  261. }