123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace app\services\out;
- use app\dao\out\OutAccountDao;
- use crmeb\basic\BaseAuth;
- use app\services\BaseServices;
- use crmeb\exceptions\AuthException;
- use crmeb\utils\ApiErrorCode;
- use think\exception\ValidateException;
- class OutAccountServices extends BaseServices
- {
- const FEPAORPL = 'OSeCVa';
-
- public function __construct(OutAccountDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function authLogin(string $appid, string $appsecret = null)
- {
- $autInfo = $this->dao->get(['appid' => $appid, 'is_del' => 0]);
- if (!$autInfo) {
- throw new ValidateException('没有此用户');
- }
- if ($appsecret && !password_verify($appsecret, $autInfo->appsecret)) {
- throw new ValidateException('appid或appsecret错误');
- }
- if ($autInfo->status == 2) {
- throw new ValidateException('您已被禁止登录');
- }
- $token = $this->createToken($autInfo->id, 'out', $autInfo->appsecret);
- $data['last_time'] = time();
- $data['ip'] = request()->ip();
- $this->update($autInfo['id'], $data);
- return [
- 'token' => $token['token'],
- 'exp_time' => $token['params']['exp'],
- 'autInfo' => $autInfo->hidden(['appsecret', 'ip', 'is_del', 'add_time', 'status', 'last_time'])->toArray()
- ];
- }
-
- public function parseToken(string $token)
- {
-
- $services = app()->make(BaseAuth::class);
- $adminInfo = $services->parseToken($token, function ($id) {
- return $this->dao->get($id);
- });
- if (isset($adminInfo->auth) && $adminInfo->auth !== md5($adminInfo->appsecret)) {
- throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID);
- }
- return $adminInfo->hidden(['appsecret', 'ip', 'status']);
- }
-
- public function getOne($where = [])
- {
- $info = $this->dao->getOne($where);
- return $info ? $info->toArray() : [];
- }
-
- public function getList(array $where = [])
- {
- [$page, $limit] = $this->getPageValue();
- $where['is_del'] = 0;
- $list = $this->dao->getList((array)$where, (int)$page, (int)$limit);
- $count = $this->dao->count($where);
- if ($list) {
- foreach ($list as &$item) {
- $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
- $item['last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '暂无';
- }
- }
- return compact('count', 'list');
- }
- }
|