2023-12-09 16:50:36 +08:00

68 lines
2.0 KiB
PHP

<?php
namespace app\controller\api\dataview;
use app\common\repositories\user\UserRepository;
use crmeb\basic\BaseController;
use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\exception\ValidateException;
class Login extends BaseController
{
public function __construct(App $app)
{
parent::__construct($app);
}
public function login()
{
$account = $this->request->post('account', '');
$password = $this->request->post('password', '');
if (!$account){
return app('json')->fail('请输入账号');
}
$user = Db::name('dataview_account')->where('account', $account)->find();
if (!$user) {
throw new ValidateException("账号不存在");
}
if (!md5($password) === $user['password']) {
$msg = '账号或密码错误';
throw new ValidateException($msg);
}
$expire = time()+ 3600 * 24;
$token = md5($expire);
// 缓存token
Cache::set($token.'_dataview_'.$user['id'], $expire);
return app('json')->success(compact('user','token', 'expire'));
}
public function mechantLogin(UserRepository $repository)
{
$account = $this->request->param('account');
$password= $this->request->param('password');
if (!$account) return app('json')->fail('请输入账号');
$user = $repository->accountByUser($account);
if (!$user)return app('json')->fail('账号不存在');;
if (!password_verify($password, $user['pwd'])) {
$msg = '账号或密码错误';
throw new ValidateException($msg);
}
$user['merchant'] = Db::name('merchant')->where('uid', $user['uid'])->find();
$expire = time()+ 3600 * 24;
$token = md5($expire);
// 缓存token
Cache::set($token.'_merchant_'.$user['uid'], $expire);
return app('json')->success(compact('user','token', 'expire'));
}
}