1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 11:41:27 +08:00

整理代码

This commit is contained in:
xiaochong0302 2020-05-15 21:56:51 +08:00
parent 4c57496aa3
commit 1edf6310c0
21 changed files with 480 additions and 191 deletions

View File

@ -21,6 +21,7 @@
<th>退款原因</th>
<th>退款状态</th>
<th>创建时间</th>
<th></th>
</tr>
{% for item in refunds %}
<tr>
@ -29,6 +30,7 @@
<td><a href="#" title="{{ item.apply_note }}">{{ substr(item.apply_note,0,15) }}</td>
<td>{{ refund_status(item) }}</td>
<td>{{ date('Y-m-d H:i:s',item.create_time) }}</td>
<td><a class="layui-btn layui-btn-sm" href="{{ url({'for':'admin.refund.show','id':item.id}) }}">详情</a></td>
</tr>
{% endfor %}
</table>
@ -41,12 +43,12 @@
</fieldset>
<table class="kg-table layui-table">
<tr>
<th>交易号</th>
<th>交易号</th>
<th>交易金额</th>
<th>交易平台</th>
<th>交易状态</th>
<th>创建时间</th>
<th>操作</th>
<th></th>
</tr>
{% for item in trades %}
<tr>
@ -55,7 +57,7 @@
<td>{{ channel_type(item.channel) }}</td>
<td>{{ trade_status(item.status) }}</td>
<td>{{ date('Y-m-d H:i:s',item.create_time) }}</td>
<td><a class="kg-close layui-btn layui-btn-sm" href="{{ url({'for':'admin.trade.show','id':item.id}) }}">详情</a></td>
<td><a class="layui-btn layui-btn-sm" href="{{ url({'for':'admin.trade.show','id':item.id}) }}">详情</a></td>
</tr>
{% endfor %}
</table>

View File

@ -24,6 +24,7 @@
<th>退款原因</th>
<th>退款状态</th>
<th>创建时间</th>
<th></th>
</tr>
{% for item in refunds %}
<tr>
@ -32,6 +33,7 @@
<td><a href="#" title="{{ item.apply_note }}">{{ substr(item.apply_note,0,15) }}</td>
<td>{{ refund_status(item) }}</td>
<td>{{ date('Y-m-d H:i:s',item.create_time) }}</td>
<td><a class="layui-btn layui-btn-sm" href="{{ url({'for':'admin.refund.show','id':item.id}) }}">详情</a></td>
</tr>
{% endfor %}
</table>

View File

@ -15,24 +15,23 @@ class AccountController extends Controller
{
/**
* @Route("/login", name="web.account.login")
* @Get("/register", name="web.account.register")
*/
public function loginAction()
public function registerAction()
{
if ($this->request->isPost()) {
$service = new AccountService();
$service->login();
$location = $this->request->getHTTPReferer();
return $this->jsonSuccess(['location' => $location]);
}
}
/**
* @Route("/logout", name="web.account.logout")
* @Get("/login", name="web.account.login")
*/
public function loginAction()
{
}
/**
* @Get("/logout", name="web.account.logout")
*/
public function logoutAction()
{
@ -44,27 +43,7 @@ class AccountController extends Controller
}
/**
* @Route("/register", name="web.account.register")
*/
public function registerAction()
{
if ($this->request->isPost()) {
$service = new AccountService();
$service->register();
$content = [
'location' => $this->request->getHTTPReferer(),
'msg' => '注册账户成功',
];
return $this->jsonSuccess($content);
}
}
/**
* @Route("/password/reset", name="web.account.reset_password")
* @Get("/password/reset", name="web.account.reset_password")
*/
public function resetPasswordAction()
{
@ -79,48 +58,138 @@ class AccountController extends Controller
}
/**
* @Route("/phone/update", name="web.account.update_phone")
* @Post("/register_by_phone", name="web.account.register_by_phone")
*/
public function registerByPhoneAction()
{
if ($this->request->isPost()) {
$service = new AccountService();
$service->registerByPhone();
$content = [
'location' => $this->request->getHTTPReferer(),
'msg' => '注册账户成功',
];
return $this->jsonSuccess($content);
}
}
/**
* @Post("/register_by_email", name="web.account.register_by_email")
*/
public function registerByEmailAction()
{
if ($this->request->isPost()) {
$service = new AccountService();
$service->registerByPhone();
$content = [
'msg' => '注册账户成功',
];
return $this->jsonSuccess($content);
}
}
/**
* @Post("/login_by_pwd", name="web.account.login_by_pwd")
*/
public function loginByPasswordAction()
{
if ($this->request->isPost()) {
$service = new AccountService();
$service->loginByPassword();
return $this->jsonSuccess();
}
}
/**
* @Post("/login_by_verify", name="web.account.login_by_verify")
*/
public function loginByVerifyAction()
{
if ($this->request->isPost()) {
$service = new AccountService();
$service->loginByVerify();
return $this->jsonSuccess();
}
}
/**
* @Post("/password/reset_by_email", name="web.account.reset_pwd_by_email")
*/
public function resetPasswordByEmailAction()
{
if ($this->request->isPost()) {
$service = new PasswordResetService();
$service->handle();
return $this->jsonSuccess(['msg' => '重置密码成功']);
}
}
/**
* @Post("/password/reset_by_phone", name="web.account.reset_pwd_by_phone")
*/
public function resetPasswordByPhoneAction()
{
if ($this->request->isPost()) {
$service = new PasswordResetService();
$service->handle();
return $this->jsonSuccess(['msg' => '重置密码成功']);
}
}
/**
* @Post("/phone/update", name="web.account.update_phone")
*/
public function updatePhoneAction()
{
if ($this->request->isPost()) {
$service = new PhoneUpdateService();
$service = new PhoneUpdateService();
$service->handle();
$service->handle();
return $this->jsonSuccess(['msg' => '更新手机成功']);
}
return $this->jsonSuccess(['msg' => '更新手机成功']);
}
/**
* @Route("/email/update", name="web.account.update_email")
* @Post("/email/update", name="web.account.update_email")
*/
public function updateEmailAction()
{
if ($this->request->isPost()) {
$service = new EmailUpdateService();
$service = new EmailUpdateService();
$service->handle();
$service->handle();
return $this->jsonSuccess(['msg' => '更新邮箱成功']);
}
return $this->jsonSuccess(['msg' => '更新邮箱成功']);
}
/**
* @Route("/password/update", name="web.account.update_password")
* @Post("/password/update", name="web.account.update_password")
*/
public function updatePasswordAction()
{
if ($this->request->isPost()) {
$service = new PasswordUpdateService();
$service = new PasswordUpdateService();
$service->handle();
$service->handle();
return $this->jsonSuccess(['msg' => '更新密码成功']);
}
return $this->jsonSuccess(['msg' => '更新密码成功']);
}
}

View File

@ -4,7 +4,6 @@ namespace App\Http\Web\Controllers;
use App\Models\ContentImage as ContentImageModel;
use App\Services\Storage as StorageService;
use App\Services\Verification as VerifyService;
use App\Traits\Response as ResponseTrait;
use PHPQRCode\QRcode as PHPQRCode;
@ -52,44 +51,4 @@ class PublicController extends \Phalcon\Mvc\Controller
exit;
}
/**
* @Post("/sms/code", name="web.sms_code")
*/
public function smsCodeAction()
{
$phone = $this->request->getPost('phone', 'trim');
$service = new VerifyService();
$success = $service->sendSmsCode($phone);
if ($success) {
return $this->jsonSuccess();
} else {
return $this->jsonError([
'code' => 'verify.send_sms_failed',
]);
}
}
/**
* @Post("/email/code", name="web.email_code")
*/
public function emailCodeAction()
{
$email = $this->request->getPost('email', 'trim');
$service = new VerifyService();
$success = $service->sendEmailCode($email);
if ($success) {
return $this->jsonSuccess();
} else {
return $this->jsonError([
'code' => 'verify.send_email_failed',
]);
}
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Http\Web\Controllers;
use App\Services\Frontend\Verify\EmailCode as EmailCodeService;
use App\Services\Frontend\Verify\SmsCode as SmsCodeService;
use App\Traits\Response as ResponseTrait;
/**
* @RoutePrefix("/verify")
*/
class VerifyController extends \Phalcon\Mvc\Controller
{
use ResponseTrait;
/**
* @Post("/sms/code", name="verify.sms_code")
*/
public function smsCodeAction()
{
$service = new SmsCodeService();
$service->handle();
return $this->jsonSuccess();
}
/**
* @Post("/email/code", name="verify.email_code")
*/
public function emailCodeAction()
{
$service = new EmailCodeService();
$service->handle();
return $this->jsonSuccess();
}
}

View File

@ -3,7 +3,8 @@
namespace App\Http\Web\Services;
use App\Services\Auth as AuthService;
use App\Services\Frontend\Account\Register as RegisterService;
use App\Services\Frontend\Account\RegisterByEmail as RegisterByEmailService;
use App\Services\Frontend\Account\RegisterByPhone as RegisterByPhoneService;
use App\Validators\Account as AccountValidator;
use App\Validators\Captcha as CaptchaValidator;
@ -20,25 +21,46 @@ class Account extends Service
$this->auth = $this->getDI()->get('auth');
}
public function login()
public function registerByEmail()
{
$service = new RegisterByEmailService();
$user = $service->handle();
$this->auth->saveAuthInfo($user);
}
public function registerByPhone()
{
$service = new RegisterByPhoneService();
$user = $service->handle();
$this->auth->saveAuthInfo($user);
}
public function loginByPassword()
{
$post = $this->request->getPost();
$captchaValidator = new CaptchaValidator();
$captchaValidator->checkCode($post['ticket'], $post['rand']);
$accountValidator = new AccountValidator();
$user = $accountValidator->checkUserLogin($post['account'], $post['password']);
$captchaSettings = $this->getSectionSettings('captcha');
$this->auth->saveAuthInfo($user);
}
/**
* 验证码是一次性的,放到最后检查,减少第三方调用
*/
if ($captchaSettings['enabled'] == 1) {
public function loginByVerify()
{
$post = $this->request->getPost();
$captchaValidator = new CaptchaValidator();
$accountValidator = new AccountValidator();
$captchaValidator->checkCode($post['ticket'], $post['rand']);
}
$user = $accountValidator->checkVerifyLogin($post['account'], $post['verify_code']);
$this->auth->saveAuthInfo($user);
}
@ -48,13 +70,4 @@ class Account extends Service
$this->auth->clearAuthInfo();
}
public function register()
{
$service = new RegisterService();
$user = $service->handle();
$this->auth->saveAuthInfo($user);
}
}

View File

@ -19,9 +19,9 @@ class Order extends Model
/**
* 来源类型
*/
const SOURCE_DESKTOP = 'desktop';
const SOURCE_ANDROID = 'android';
const SOURCE_IOS = 'ios';
const SOURCE_WEB = 'web';
const SOURCE_WAP = 'wap';
const SOURCE_APP = 'app';
/**
* 状态类型
@ -138,6 +138,8 @@ class Order extends Model
{
parent::initialize();
$this->keepSnapshots(true);
$this->addBehavior(
new SoftDelete([
'field' => 'deleted',
@ -149,9 +151,7 @@ class Order extends Model
public function beforeCreate()
{
$this->status = self::STATUS_PENDING;
$this->sn = date('YmdHis') . rand(1000, 9999);
$this->create_time = time();
if (is_array($this->item_info) && !empty($this->item_info)) {
@ -170,6 +170,16 @@ class Order extends Model
}
}
public function afterSave()
{
if ($this->hasUpdated('status')) {
$orderStatus = new OrderStatus();
$orderStatus->order_id = $this->id;
$orderStatus->status = $this->getSnapshotData()['status'];
$orderStatus->create();
}
}
public function afterFetch()
{
$this->amount = (float)$this->amount;
@ -193,9 +203,9 @@ class Order extends Model
public static function sourceTypes()
{
return [
self::SOURCE_DESKTOP => 'desktop',
self::SOURCE_ANDROID => 'android',
self::SOURCE_IOS => 'ios',
self::SOURCE_WEB => 'web',
self::SOURCE_WAP => 'wap',
self::SOURCE_APP => 'app',
];
}

View File

@ -117,6 +117,8 @@ class Refund extends Model
{
parent::initialize();
$this->keepSnapshots(true);
$this->addBehavior(
new SoftDelete([
'field' => 'deleted',
@ -128,9 +130,7 @@ class Refund extends Model
public function beforeCreate()
{
$this->status = self::STATUS_PENDING;
$this->sn = date('YmdHis') . rand(1000, 9999);
$this->create_time = time();
}
@ -139,6 +139,16 @@ class Refund extends Model
$this->update_time = time();
}
public function afterSave()
{
if ($this->hasUpdated('status')) {
$refundStatus = new RefundStatus();
$refundStatus->refund_id = $this->id;
$refundStatus->status = $this->getSnapshotData()['status'];
$refundStatus->create();
}
}
public function afterFetch()
{
$this->amount = (float)$this->amount;

View File

@ -114,6 +114,8 @@ class Trade extends Model
{
parent::initialize();
$this->keepSnapshots(true);
$this->addBehavior(
new SoftDelete([
'field' => 'deleted',
@ -125,9 +127,7 @@ class Trade extends Model
public function beforeCreate()
{
$this->status = self::STATUS_PENDING;
$this->sn = date('YmdHis') . rand(1000, 9999);
$this->create_time = time();
}
@ -136,6 +136,16 @@ class Trade extends Model
$this->update_time = time();
}
public function afterSave()
{
if ($this->hasUpdated('status')) {
$tradeStatus = new TradeStatus();
$tradeStatus->trade_id = $this->id;
$tradeStatus->status = $this->getSnapshotData()['status'];
$tradeStatus->create();
}
}
public function afterFetch()
{
$this->amount = (float)$this->amount;

View File

@ -0,0 +1,33 @@
<?php
namespace App\Services\Frontend\Account;
use App\Services\Frontend\Service;
use App\Validators\Account as AccountValidator;
use App\Validators\Verify as VerifyValidator;
class PasswordResetByEmail extends Service
{
public function handle()
{
$post = $this->request->getPost();
$accountValidator = new AccountValidator();
$account = $accountValidator->checkAccountByEmail($post['email']);
$accountValidator->checkPassword($post['new_password']);
$verifyValidator = new VerifyValidator();
$verifyValidator->checkEmailCode($post['email'], $post['verify_code']);
$account->password = $post['new_password'];
$account->update();
return $account;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Services\Frontend\Account;
use App\Services\Frontend\Service;
use App\Validators\Account as AccountValidator;
use App\Validators\Verify as VerifyValidator;
class PasswordResetByPhone extends Service
{
public function handle()
{
$post = $this->request->getPost();
$accountValidator = new AccountValidator();
$account = $accountValidator->checkAccountByPhone($post['phone']);
$accountValidator->checkPassword($post['new_password']);
$verifyValidator = new VerifyValidator();
$verifyValidator->checkSmsCode($post['phone'], $post['verify_code']);
$account->password = $post['new_password'];
$account->update();
return $account;
}
}

View File

@ -2,14 +2,13 @@
namespace App\Services\Frontend\Account;
use App\Library\Validators\Common as CommonValidator;
use App\Models\Account as AccountModel;
use App\Repos\User as UserRepo;
use App\Services\Frontend\Service;
use App\Validators\Account as AccountValidator;
use App\Validators\Verify as VerifyValidator;
class Register extends Service
class RegisterByEmail extends Service
{
public function handle()
@ -18,24 +17,15 @@ class Register extends Service
$verifyValidator = new VerifyValidator();
$verifyValidator->checkCode($post['account'], $post['verify_code']);
$verifyValidator->checkEmailCode($post['email'], $post['verify_code']);
$accountValidator = new AccountValidator();
$data = [];
if (CommonValidator::email($post['account'])) {
$data['email'] = $accountValidator->checkEmail($post['email']);
$data['email'] = $accountValidator->checkEmail($post['account']);
$accountValidator->checkIfEmailTaken($post['account']);
} elseif (CommonValidator::phone($post['account'])) {
$data['phone'] = $accountValidator->checkPhone($post['account']);
$accountValidator->checkIfPhoneTaken($post['account']);
}
$accountValidator->checkIfEmailTaken($post['email']);
$data['password'] = $accountValidator->checkPassword($post['password']);

View File

@ -0,0 +1,41 @@
<?php
namespace App\Services\Frontend\Account;
use App\Models\Account as AccountModel;
use App\Repos\User as UserRepo;
use App\Services\Frontend\Service;
use App\Validators\Account as AccountValidator;
use App\Validators\Verify as VerifyValidator;
class RegisterByPhone extends Service
{
public function handle()
{
$post = $this->request->getPost();
$verifyValidator = new VerifyValidator();
$verifyValidator->checkSmsCode($post['phone'], $post['verify_code']);
$accountValidator = new AccountValidator();
$data = [];
$data['phone'] = $accountValidator->checkPhone($post['phone']);
$accountValidator->checkIfPhoneTaken($post['phone']);
$data['password'] = $accountValidator->checkPassword($post['password']);
$account = new AccountModel();
$account->create($data);
$userRepo = new UserRepo();
return $userRepo->findById($account->id);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Services\Frontend\Verify;
use App\Services\Frontend\Service;
use App\Services\Mailer\Verify as VerifyMailerService;
use App\Validators\Captcha as CaptchaValidator;
use App\Validators\Verify as VerifyValidator;
class EmailCode extends Service
{
public function handle()
{
$post = $this->request->getPost();
$validator = new VerifyValidator();
$post['email'] = $validator->checkEmail($post['email']);
$validator = new CaptchaValidator();
$validator->checkCode($post['ticket'], $post['rand']);
$service = new VerifyMailerService();
$service->handle($post['email']);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Services\Frontend\Verify;
use App\Services\Frontend\Service;
use App\Services\Smser\Verify as VerifySmserService;
use App\Validators\Captcha as CaptchaValidator;
use App\Validators\Verify as VerifyValidator;
class SmsCode extends Service
{
public function handle()
{
$post = $this->request->getPost();
$validator = new VerifyValidator();
$post['phone'] = $validator->checkPhone($post['phone']);
$validator = new CaptchaValidator();
$validator->checkCode($post['ticket'], $post['rand']);
$service = new VerifySmserService();
$service->handle($post['phone']);
}
}

View File

@ -2,10 +2,10 @@
namespace App\Services\Mailer;
use App\Services\Mailer;
use App\Services\Verification;
use App\Services\Mailer as MailerService;
use App\Services\Verify as VerifyService;
class Verify extends Mailer
class Verify extends MailerService
{
/**
@ -18,11 +18,11 @@ class Verify extends Mailer
$message = $this->manager->createMessage();
$verification = new Verification();
$verify = new VerifyService();
$minutes = 5;
$code = $verification->getSmsCode($email, 60 * $minutes);
$code = $verify->getSmsCode($email, 60 * $minutes);
$subject = '邮件验证码';

View File

@ -2,10 +2,10 @@
namespace App\Services\Smser;
use App\Services\Smser;
use App\Services\Verification;
use App\Services\Smser as SmserService;
use App\Services\Verify as VerifyService;
class Verify extends Smser
class Verify extends SmserService
{
protected $templateCode = 'verify';
@ -16,11 +16,11 @@ class Verify extends Smser
*/
public function handle($phone)
{
$verification = new Verification();
$verify = new VerifyService();
$minutes = 5;
$code = $verification->getSmsCode($phone, 60 * $minutes);
$code = $verify->getSmsCode($phone, 60 * $minutes);
$templateId = $this->getTemplateId($this->templateCode);

View File

@ -3,12 +3,9 @@
namespace App\Services;
use App\Library\Cache\Backend\Redis as RedisCache;
use App\Services\Mailer\Verify as VerifyMailer;
use App\Services\Smser\Verify as VerifySmser;
use App\Validators\Verify as VerifyValidator;
use Phalcon\Text;
class Verification extends Service
class Verify extends Service
{
/**
@ -21,28 +18,6 @@ class Verification extends Service
$this->cache = $this->getDI()->get('cache');
}
public function sendSmsCode($phone)
{
$validator = new VerifyValidator();
$validator->checkPhone($phone);
$smser = new VerifySmser();
return $smser->handle($phone);
}
public function sendEmailCode($email)
{
$validator = new VerifyValidator();
$validator->checkEmail($email);
$mailer = new VerifyMailer();
return $mailer->handle($email);
}
public function getSmsCode($phone, $lifetime = 300)
{
$key = $this->getSmsCacheKey($phone);

View File

@ -26,6 +26,32 @@ class Account extends Validator
return $account;
}
public function checkAccountByEmail($email)
{
$accountRepo = new AccountRepo();
$account = $accountRepo->findByEmail($email);
if (!$account) {
throw new BadRequestException('account.not_found');
}
return $account;
}
public function checkAccountByPhone($phone)
{
$accountRepo = new AccountRepo();
$account = $accountRepo->findByPhone($phone);
if (!$account) {
throw new BadRequestException('account.not_found');
}
return $account;
}
public function checkPhone($phone)
{
if (!CommonValidator::phone($phone)) {

View File

@ -4,7 +4,7 @@ namespace App\Validators;
use App\Exceptions\BadRequest as BadRequestException;
use App\Library\Validators\Common as CommonValidator;
use App\Services\Verification as VerifyService;
use App\Services\Verify as VerifyService;
class Verify extends Validator
{
@ -27,20 +27,34 @@ class Verify extends Validator
return $email;
}
public function checkCode($key, $code)
public function checkCode($name, $code)
{
if (CommonValidator::email($name)) {
$this->checkEmailCode($name, $code);
} elseif (CommonValidator::phone($name)) {
$this->checkSmsCode($name, $code);
}
}
public function checkSmsCode($phone, $code)
{
$service = new VerifyService();
$result = false;
if (CommonValidator::email($key)) {
$result = $service->checkEmailCode($key, $code);
} elseif (CommonValidator::phone($key)) {
$result = $service->checkSmsCode($key, $code);
}
$result = $service->checkSmsCode($phone, $code);
if (!$result) {
throw new BadRequestException('verify.invalid_code');
throw new BadRequestException('verify.invalid_sms_code');
}
}
public function checkEmailCode($email, $code)
{
$service = new VerifyService();
$result = $service->checkEmailCode($email, $code);
if (!$result) {
throw new BadRequestException('verify.invalid_email_code');
}
}

View File

@ -23,9 +23,10 @@ $error['security.invalid_http_referer'] = '无效请求来源';
/**
* 验证相关
*/
$error['verify.invalid_email'] = '无效的邮箱';
$error['verify.invalid_phone'] = '无效手机号';
$error['verify.invalid_code'] = '无效的验证码';
$error['verify.invalid_email'] = '无效的邮箱';
$error['verify.invalid_sms_code'] = '无效的短信验证码';
$error['verify.invalid_email_code'] = '无效的邮件验证码';
$error['verify.send_sms_failed'] = '发送短信验证码失败';
$error['verify.send_email_failed'] = '发送邮件验证码失败';