MerchantAdminRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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\common\repositories\system\merchant;
  12. use app\common\dao\BaseDao;
  13. use app\common\dao\system\merchant\MerchantAdminDao;
  14. use app\common\model\system\merchant\Merchant;
  15. use app\common\model\system\merchant\MerchantAdmin;
  16. use app\common\repositories\BaseRepository;
  17. use app\common\repositories\system\auth\RoleRepository;
  18. use crmeb\exceptions\AuthException;
  19. use crmeb\services\JwtTokenService;
  20. use FormBuilder\Exception\FormBuilderException;
  21. use FormBuilder\Factory\Elm;
  22. use FormBuilder\Form;
  23. use think\db\exception\DataNotFoundException;
  24. use think\db\exception\DbException;
  25. use think\db\exception\ModelNotFoundException;
  26. use think\exception\ValidateException;
  27. use think\facade\Cache;
  28. use think\facade\Config;
  29. use think\facade\Route;
  30. use think\Model;
  31. /**
  32. * Class MerchantRepository
  33. * @package app\common\repositories\system\merchant
  34. * @mixin MerchantAdminDao
  35. * @author xaboy
  36. * @day 2020-04-16
  37. */
  38. class MerchantAdminRepository extends BaseRepository
  39. {
  40. const PASSWORD_TYPE_ADMIN = 1;
  41. const PASSWORD_TYPE_MERCHANT = 2;
  42. const PASSWORD_TYPE_SELF = 3;
  43. /**
  44. * MerchantAdminRepository constructor.
  45. * @param MerchantAdminDao $dao
  46. */
  47. public function __construct(MerchantAdminDao $dao)
  48. {
  49. $this->dao = $dao;
  50. }
  51. /**
  52. * @param $merId
  53. * @param array $where
  54. * @param $page
  55. * @param $limit
  56. * @return array
  57. * @throws DataNotFoundException
  58. * @throws DbException
  59. * @throws ModelNotFoundException
  60. * @author xaboy
  61. * @day 2020-04-18
  62. */
  63. public function getList($merId, array $where, $page, $limit)
  64. {
  65. $query = $this->dao->search($merId, $where, 1);
  66. $count = $query->count();
  67. $list = $query->page($page, $limit)->hidden(['pwd', 'is_del'])->select();
  68. $topAccount = $this->dao->merIdByAccount($merId);
  69. foreach ($list as $k => $role) {
  70. if ($topAccount)
  71. $list[$k]['account'] = $topAccount . '@' . $role['account'];
  72. $list[$k]['rule_name'] = $role->roleNames();
  73. }
  74. return compact('list', 'count');
  75. }
  76. /**
  77. * @param int $merId
  78. * @param int|null $id
  79. * @param array $formData
  80. * @return Form
  81. * @throws FormBuilderException
  82. * @author xaboy
  83. * @day 2020-04-18
  84. */
  85. public function form(int $merId, ?int $id = null, array $formData = []): Form
  86. {
  87. $form = Elm::createForm(is_null($id) ? Route::buildUrl('merchantAdminCreate')->build() : Route::buildUrl('merchantAdminUpdate', ['id' => $id])->build());
  88. $rules = [
  89. Elm::select('roles', '身份', [])->options(function () use ($merId) {
  90. $data = app()->make(RoleRepository::class)->getAllOptions($merId);
  91. $options = [];
  92. foreach ($data as $value => $label) {
  93. $options[] = compact('value', 'label');
  94. }
  95. return $options;
  96. })->multiple(true),
  97. Elm::input('real_name', '管理员姓名'),
  98. Elm::input('account', '账号')->required(),
  99. Elm::input('phone', ' 联系电话'),
  100. ];
  101. if (!$id) {
  102. $rules[] = Elm::password('pwd', '密码')->required();
  103. $rules[] = Elm::password('againPassword', '确认密码')->required();
  104. }
  105. $rules[] = Elm::switches('status', '是否开启', 1)->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启');
  106. $form->setRule($rules);
  107. return $form->setTitle(is_null($id) ? '添加管理员' : '编辑管理员')->formData($formData);
  108. }
  109. /**
  110. * @param int $merId
  111. * @param int $id
  112. * @return Form
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws FormBuilderException
  116. * @throws ModelNotFoundException
  117. * @author xaboy
  118. * @day 2020-04-18
  119. */
  120. public function updateForm(int $merId, int $id)
  121. {
  122. return $this->form($merId, $id, $this->dao->get($id)->toArray());
  123. }
  124. /**
  125. * @param Merchant $merchant
  126. * @param $account
  127. * @param $pwd
  128. * @return BaseDao|Model
  129. * @author xaboy
  130. * @day 2020-04-17
  131. */
  132. public function createMerchantAccount(Merchant $merchant, $account, $pwd)
  133. {
  134. $pwd = $this->passwordEncode($pwd);
  135. $data = compact('pwd', 'account') + [
  136. 'mer_id' => $merchant->mer_id,
  137. 'real_name' => $merchant->real_name,
  138. 'phone' => $merchant->mer_phone,
  139. 'level' => 0
  140. ];
  141. return $this->create($data);
  142. }
  143. /**
  144. * @param $password
  145. * @return bool|string
  146. * @author xaboy
  147. * @day 2020-04-17
  148. */
  149. public function passwordEncode($password)
  150. {
  151. return password_hash($password, PASSWORD_BCRYPT);
  152. }
  153. /**
  154. * @param string $account
  155. * @param string $password
  156. * @return array|Model|null
  157. * @throws DataNotFoundException
  158. * @throws DbException
  159. * @throws ModelNotFoundException
  160. * @author xaboy
  161. * @day 2020-04-17
  162. */
  163. public function login(string $account, string $password)
  164. {
  165. $accountInfo = explode('@', $account, 2);
  166. if (count($accountInfo) === 1) {
  167. $adminInfo = $this->dao->accountByTopAdmin($accountInfo[0]);
  168. } else {
  169. $merId = $this->dao->accountByMerchantId($accountInfo[0]);
  170. if (!$merId)
  171. throw new ValidateException('账号不存在');
  172. $adminInfo = $this->dao->accountByAdmin($accountInfo[1], $merId);
  173. }
  174. if (!$adminInfo)
  175. throw new ValidateException('账号不存在');
  176. if ($adminInfo['status'] != 1)
  177. throw new ValidateException('账号已关闭');
  178. if (!password_verify($password, $adminInfo->pwd))
  179. throw new ValidateException('账号或密码错误');
  180. /**
  181. * @var MerchantRepository $merchantRepository
  182. */
  183. $merchantRepository = app()->make(MerchantRepository::class);
  184. $merchant = $merchantRepository->get($adminInfo->mer_id);
  185. if (!$merchant)
  186. throw new ValidateException('商户不存在');
  187. if (!$merchant['status'])
  188. throw new ValidateException('商户已被锁定');
  189. $adminInfo->last_time = date('Y-m-d H:i:s');
  190. $adminInfo->last_ip = app('request')->ip();
  191. $adminInfo->login_count++;
  192. $adminInfo->save();
  193. return $adminInfo;
  194. }
  195. /**
  196. * @param string $token
  197. * @param int $exp
  198. * @author xaboy
  199. * @day 2020-04-10
  200. */
  201. public function cacheToken(string $token, int $exp)
  202. {
  203. Cache::set('mer_' . $token, time() + $exp, $exp);
  204. }
  205. /**
  206. * @param string $token
  207. * @author xaboy
  208. * @day 2020-04-17
  209. */
  210. public function checkToken(string $token)
  211. {
  212. $has = Cache::has('mer_' . $token);
  213. if (!$has)
  214. throw new AuthException('无效的token');
  215. $lastTime = Cache::get('mer_' . $token);
  216. if (($lastTime + (intval(Config::get('admin.token_valid_exp', 15))) * 60) < time())
  217. throw new AuthException('token 已过期');
  218. }
  219. /**
  220. * @param string $token
  221. * @author xaboy
  222. * @day 2020-04-17
  223. */
  224. public function updateToken(string $token)
  225. {
  226. Cache::set('mer_' . $token, time(), intval(Config::get('admin.token_valid_exp', 15)) * 60);
  227. }
  228. /**
  229. * @param string $token
  230. * @author xaboy
  231. * @day 2020-04-17
  232. */
  233. public function clearToken(string $token)
  234. {
  235. Cache::delete('mer_' . $token);
  236. }
  237. /**
  238. * @param MerchantAdmin $admin
  239. * @return array
  240. * @author xaboy
  241. * @day 2020-04-17
  242. */
  243. public function createToken(MerchantAdmin $admin)
  244. {
  245. $service = new JwtTokenService();
  246. $exp = intval(Config::get('admin.token_exp', 3));
  247. $token = $service->createToken($admin->merchant_admin_id, 'mer', strtotime("+ {$exp}hour"));
  248. $this->cacheToken($token['token'], $token['out']);
  249. return $token;
  250. }
  251. /**
  252. * @param string $key
  253. * @param string $code
  254. * @author xaboy
  255. * @day 2020-04-17
  256. */
  257. public function checkCode(string $key, string $code)
  258. {
  259. $_code = Cache::get('am_captcha' . $key);
  260. if (!$_code) {
  261. throw new ValidateException('验证码过期');
  262. }
  263. if (strtolower($_code) != strtolower($code)) {
  264. throw new ValidateException('验证码错误');
  265. }
  266. //删除code
  267. Cache::delete('am_captcha' . $key);
  268. }
  269. /**
  270. * @param string $code
  271. * @return string
  272. * @author xaboy
  273. * @day 2020-04-17
  274. */
  275. public function createLoginKey(string $code)
  276. {
  277. $key = uniqid(microtime(true), true);
  278. Cache::set('am_captcha' . $key, $code, Config::get('admin.captcha_exp', 5) * 60);
  279. return $key;
  280. }
  281. /**
  282. * @param int $id
  283. * @param int $userType
  284. * @return Form
  285. * @throws FormBuilderException
  286. * @author xaboy
  287. * @day 2020-04-20
  288. */
  289. public function passwordForm(int $id, $userType = 2)
  290. {
  291. $action = 'merchantAdminPassword';
  292. if ($userType == self::PASSWORD_TYPE_ADMIN)
  293. $action = 'systemMerchantAdminPassword';
  294. else if ($userType == self::PASSWORD_TYPE_SELF)
  295. $action = 'merchantAdminEditPassword';
  296. $form = Elm::createForm(Route::buildUrl($action, $userType == self::PASSWORD_TYPE_SELF ? [] : compact('id'))->build(), [
  297. $rules[] = Elm::password('pwd', '密码')->required(),
  298. $rules[] = Elm::password('againPassword', '确认密码')->required(),
  299. ]);
  300. return $form->setTitle('修改密码');
  301. }
  302. /**
  303. * @param array $formData
  304. * @return Form
  305. * @throws FormBuilderException
  306. * @author xaboy
  307. * @day 2020-04-20
  308. */
  309. public function editForm(array $formData)
  310. {
  311. $form = Elm::createForm(Route::buildUrl('merchantAdminEdit')->build());
  312. $form->setRule([
  313. Elm::input('real_name', '管理员姓名')->required(),
  314. Elm::input('phone', '联系电话')
  315. ]);
  316. return $form->setTitle('修改信息')->formData($formData);
  317. }
  318. /**
  319. * @param int $id
  320. * @param array $data
  321. * @return int
  322. * @throws DbException
  323. * @author xaboy
  324. * @day 2020-04-18
  325. */
  326. public function update(int $id, array $data)
  327. {
  328. if (isset($data['roles']))
  329. $data['roles'] = implode(',', $data['roles']);
  330. return $this->dao->update($id, $data);
  331. }
  332. }