pref: 增加服务类优化UI版面

This commit is contained in:
Ying
2023-06-19 14:32:30 +08:00
parent 27eda6f37f
commit 2b8f874450
148 changed files with 3933 additions and 9286 deletions

View File

@@ -4,11 +4,10 @@ declare (strict_types=1);
namespace app\api\controller;
use app\ApiController;
use app\common\library\Email;
use app\common\library\Sms;
use app\common\model\system\User;
use PHPMailer\PHPMailer\Exception;
use app\common\exception\OperateException;
use app\common\service\notice\EmailService;
use app\common\service\notice\SmsService;
use Psr\SimpleCache\InvalidArgumentException;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
@@ -19,87 +18,47 @@ use think\db\exception\ModelNotFoundException;
*/
class Ajax extends ApiController
{
public bool $needLogin = true;
/**
* 发送短信
* @return Response|void
* @throws DataNotFoundException
* @throws DbException
* 首页
*/
public function smsSend()
public function index(): Response
{
if (request()->isPost()) {
$mobile = input('mobile');
$event = input('event', 'register');
if (!is_mobile($mobile)) {
return $this->error('手机号码不正确');
}
$sms = Sms::instance();
$data = $sms->getLast($mobile);
if ($data && (time() - strtotime($data['create_time'])) < 60) {
return $this->error(__('发送频繁'));
}
$user = User::getByMobile($mobile);
if (in_array($event, ['register', 'changer']) && $user) {
return $this->error('当前手机号已被占用');
} else if ($event == 'forgot' && !$user) {
return $this->error('当前手机号未注册');
}
if ($sms->send($mobile, $event)) {
return $this->success("验证码发送成功!");
} else {
return $this->error($sms->getError());
}
}
return json(['msg' => 'success', 'data' => 'Hello']);
return response('Hello swiftadmin!');
}
/**
* 发送邮件
* @return Response|void
* 发送短信验证码
* @return Response
* @throws InvalidArgumentException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws Exception
*/
public function emailSend()
public function smsSend(): Response
{
if (request()->isPost()) {
$email = input('email');
$event = input('event', 'register');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return $this->error('邮件格式不正确');
}
$Ems = Email::instance();
$data = $Ems->getLast($email);
if ($data && (time() - strtotime($data['create_time'])) < 60) {
return $this->error(__('发送频繁'));
}
$user = User::getByEmail($email);
if (in_array($event, ['register', 'changer']) && $user) {
return $this->error('当前邮箱已被注册');
} else if ($event == 'forgot' && !$user) {
return $this->error('当前邮箱不存在');
}
if ($Ems->captcha($email, $event)->send()) {
return $this->success("验证码发送成功!");
} else {
return $this->error($Ems->getError());
}
$mobile = input('mobile', '');
$event = input('event', 'register');
if (!SmsService::filterMobile($mobile)) {
return $this->error('手机号码不正确');
}
SmsService::send($mobile, $event);
return $this->success("验证码发送成功!");
}
/**
* 发送邮件验证码
* @return Response
* @throws InvalidArgumentException
* @throws OperateException
*/
public function emailSend(): Response
{
$email = input('email');
$event = input('event', 'register');
if (!EmailService::filterEmail($email)) {
return $this->error('邮件格式不正确');
}
EmailService::captcha($email, $event);
return $this->success("验证码发送成功!");
}
}

View File

@@ -4,10 +4,18 @@
namespace app\api\controller;
use app\ApiController;
use app\common\exception\OperateException;
use app\common\exception\user\UserException;
use app\common\library\ResultCode;
use app\common\library\Sms;
use app\common\library\Upload;
use app\common\model\system\User as UserModel;
use app\common\validate\system\User as UserValidate;
use app\common\service\user\UserService;
use PHPMailer\PHPMailer\Exception;
use Psr\SimpleCache\InvalidArgumentException;
use support\Request;
use support\Response;
use system\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
@@ -21,82 +29,267 @@ class User extends ApiController
* 需要登录
* @var bool
*/
public bool $needLogin = true;
public bool $needLogin = true;
/**
* 非鉴权方法
* @var array
*/
public array $noNeedAuth = ['register', 'login'];
public array $noNeedLogin = ['register', 'login', 'mobileLogin', 'mnpLogin', 'forgot'];
/**
* 构造方法
*/
public function __construct()
{
parent::__construct();
$this->model = new UserModel();
}
/**
* 用户中心
* @param Request $request
* @return Response
*/
public function center(Request $request): Response
{
$fields = $this->model->getVisibleFields();
$userInfo = array_intersect_key($request->userInfo, array_flip($fields));
return $this->success('获取成功', '', $userInfo);
}
/**
* 修改用户资料
* @param Request $request
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function profile(Request $request): Response
{
$post = request()->post();
validate(UserValidate::class)->scene('nickname')->check($post);
UserService::editProfile($post, $request->userId);
return $this->success('修改成功', '/');
}
/**
* 用户注册
* @return mixed|void
* @throws DataNotFoundException
* @return Response
* @throws DbException
* @throws ModelNotFoundException|InvalidArgumentException
* @throws InvalidArgumentException
* @throws OperateException
*/
public function register()
public function register(): Response
{
if (request()->isPost()) {
// 获取参数
$post = input('post.');
// 获取注册方式
$registerType = saenv('user_register');
if ($registerType == 'mobile') {
$mobile = input('mobile');
$captcha = input('captcha');
// 校验手机验证码
if (!Sms::instance()->check($mobile, $captcha, 'register')) {
return $this->error(Sms::instance()->getError());
}
}
$response = $this->auth->register($post);
if (!$response) {
return $this->error($this->auth->getError());
}
return $response->withBody(json_encode(ResultCode::REGISTERSUCCESS));
}
$post = request()->post();
validate(UserValidate::class)->scene('register')->check($post);
$result = UserService::register($post);
return $this->success('注册成功', '/', ['token' => $result['token']]);
}
/**
* 用户登录
* @return mixed|void
* @return Response
* @throws InvalidArgumentException
* @throws OperateException
*/
public function login() {
if (request()->isPost()) {
// 获取参数
$nickname = input('nickname');
$password = input('pwd');
$response = $this->auth->login($nickname, $password);
if (!$response) {
return $this->error($this->auth->getError());
}
$response->withBody(json_encode(array_merge(ResultCode::LOGINSUCCESS, ['token' => $this->auth->token])));
return $response;
}
public function login(): Response
{
$nickname = input('nickname');
$password = input('pwd');
if (!$nickname || !$password) {
return $this->error('请输入用户名或密码');
}
$result = UserService::accountLogin($nickname, $password);
return $this->success('登录成功', '/', ['token' => $result['token']]);
}
/**
* 文件上传
* 手机号登录
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws InvalidArgumentException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function upload()
public function mobileLogin(): Response
{
$mobile = input('mobile');
$captcha = input('captcha');
$result = UserService::mobileLogin($mobile, $captcha);
return $this->success('登录成功', '/', ['token' => $result['token']]);
}
/**
* 修改密码
* @param Request $request
* @return Response
* @throws OperateException
*/
public function changePwd(Request $request): Response
{
$post = request()->post();
UserService::changePwd($post, $request->userId);
return $this->success('修改密码成功!');
}
/**
* 找回密码
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function forgot(): Response
{
$post = request()->post();
validate(UserValidate::class)->check($post);
UserService::forgotPwd($post);
return $this->success('修改密码成功!');
}
/**
* 获取消息列表
* @param Request $request
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function message(Request $request): Response
{
$page = input('page/d', 1);
$limit = input('limit/d', 1);
$status = input('status', 'all');
$where[] = ['user_id', '=', $request->userId];
if ($status !== 'all') {
$where[] = ['status', '=', $status];
}
list($list, $count) = UserService::listMessage($limit, $page, $where);
return $this->success('查询成功', "/", $list, $count);
}
/**
* 查看消息
* @param Request $request
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public function viewMessage(Request $request): Response
{
$id = input('id/d', 0);
$result = UserService::viewMessage($id, $request->userId);
return $this->success('查询成功', "/", $result);
}
/**
* 批量操作消息
* @param Request $request
* @return Response
*/
public function batchMessage(Request $request): Response
{
$ids = input('id');
$type = input('type', 'del');
try {
UserService::batchMessage($ids, $type, $request->userId);
} catch (UserException $e) {
return $this->error($e->getMessage());
}
return $this->success('操作成功');
}
/**
* 申请APP_KEY
* @param Request $request
* @return Response
*/
public function appid(Request $request): Response
{
$data['id'] = $request->userId;
$data['app_id'] = 10000 + $request->userId;
$data['app_secret'] = Random::alpha(22);
if ($this->model->update($data)) {
return $this->success('申请成功!', '/', $data);
}
return $this->error('申请失败!');
}
/**
* 修改邮箱地址
* @param Request $request
* @return Response
* @throws Exception|UserException|OperateException
*/
public function changeEmail(Request $request): Response
{
$email = input('email');
$captcha = input('captcha');
$event = input('event');
UserService::changeEmail($email, $captcha, $event, $request->userId);
return $this->success('修改邮箱成功!');
}
/**
* 修改手机号
* @param Request $request
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
* @throws UserException
*/
public function changeMobile(Request $request): Response
{
$mobile = input('mobile');
$captcha = input('captcha');
$event = input('event');
UserService::changeMobile($mobile, $captcha, $event, $request->userId);
return $this->success('修改手机号成功!');
}
/**
* 意见反馈
* @return Response
*/
public function feedback(): Response
{
$type = input('type', '');
$content = input('content');
if (empty($type) || empty($content)) {
return $this->error('参数错误');
}
return $this->success('反馈成功');
}
/**
* 文件上传函数
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws InvalidArgumentException
* @throws ModelNotFoundException
*/
public function upload(): Response
{
if (request()->isPost()) {
$file = Upload::instance()->upload();
if (!$file) {
$response = Upload::instance()->upload();
if (empty($response)) {
return $this->error(Upload::instance()->getError());
}
return json($file);
return json($response);
}
return json(ResultCode::SUCCESS);
}
}