1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-17 05:36:13 +08:00

增加第三方登录设计

整理代码
This commit is contained in:
xiaochong0302 2020-04-16 19:29:09 +08:00
parent 92d9395065
commit cf5e50f402
28 changed files with 181 additions and 180 deletions

View File

@ -2,9 +2,7 @@
namespace App\Http\Admin\Services;
use Phalcon\Mvc\User\Component;
class Service extends Component
class Service extends \App\Services\Service
{
}

View File

@ -25,8 +25,8 @@
{% set course = order.item_info['course'] %}
{% set reward = order.item_info['reward'] %}
<div class="kg-order-item">
<p>课程名称:{{ course['title'] }}</p>
<p>打赏金额:¥{{ reward['price'] }}</p>
<p>商品名称:{{ order.subject }}</p>
<p>商品价格:¥{{ order.amount }}</p>
</div>
{% elseif order.item_type == 'test' %}
<div class="kg-order-item">

View File

@ -2,12 +2,7 @@
namespace App\Http\Api\Services;
use App\Traits\Auth as AuthTrait;
use Phalcon\Mvc\User\Component;
class Service extends Component
class Service extends \App\Services\Service
{
use AuthTrait;
}

View File

@ -2,11 +2,7 @@
namespace App\Http\Html5\Services;
use App\Traits\Auth as AuthTrait;
use Phalcon\Mvc\User\Component;
class Service extends Component
class Service extends \App\Services\Service
{
use AuthTrait;
}

View File

@ -2,11 +2,7 @@
namespace App\Http\Web\Services;
use App\Traits\Auth as AuthTrait;
use Phalcon\Mvc\User\Component;
class Service extends Component
class Service extends \App\Services\Service
{
use AuthTrait;
}

View File

@ -0,0 +1,93 @@
<?php
namespace App\Models;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class AccountBind extends Model
{
/**
* 服务商类型
*/
const PROVIDER_QQ = 'qq';
const PROVIDER_WEIXIN = 'weixin';
const PROVIDER_WEIBO = 'weibo';
/**
* 主键编号
*
* @var int
*/
public $id;
/**
* 服务商
*
* @var string
*/
public $provider;
/**
* 外部用户编号
*
* @var string
*/
public $open_id;
/**
* 内部用户编号
*
* @var int
*/
public $user_id;
/**
* 删除标识
*
* @var int
*/
public $deleted;
/**
* 创建时间
*
* @var int
*/
public $create_time;
/**
* 更新时间
*
* @var int
*/
public $update_time;
public function getSource()
{
return 'kg_account_bind';
}
public function initialize()
{
parent::initialize();
$this->addBehavior(
new SoftDelete([
'field' => 'deleted',
'value' => 1,
])
);
}
public function beforeCreate()
{
$this->create_time = time();
}
public function beforeUpdate()
{
$this->update_time = time();
}
}

View File

@ -3,6 +3,7 @@
namespace App\Repos;
use App\Models\Account as AccountModel;
use App\Models\AccountBind as AccountBindModel;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Model\ResultsetInterface;
@ -43,6 +44,23 @@ class Account extends Repository
]);
}
/**
* @param string $provider
* @param string $openId
* @return AccountModel|Model|bool
*/
public function findByOpenId($provider, $openId)
{
$bind = AccountBindModel::findFirst([
'conditions' => 'provider = ?1 AND open_id = ?2',
'bind' => [1 => $provider, 2 => $openId],
]);
if (!$bind) return false;
return AccountModel::findFirst($bind->user_id);
}
/**
* @param array $ids
* @param array|string $columns

View File

@ -1,8 +1,9 @@
<?php
namespace App\Services\Frontend;
namespace App\Services\Frontend\Account;
use App\Repos\Account as AccountRepo;
use App\Services\Frontend\Service;
use App\Validators\Account as AccountValidator;
use App\Validators\Security as SecurityValidator;

View File

@ -45,41 +45,31 @@ class ChapterInfo extends Service
$this->user = $user;
$this->setCourseUser($course, $user);
$this->setChapterUser($chapter, $user);
$this->handleCourseUser($course, $user);
$this->handleChapterUser($chapter, $user);
return $this->handleChapter($chapter, $user);
}
/**
* @param ChapterModel $chapter
* @param UserModel $user
* @return array
*/
protected function handleChapter(ChapterModel $chapter, UserModel $user)
{
$result = $this->formatChapter($chapter);
$me = [
'agreed' => false,
'opposed' => false,
'agreed' => 0,
'opposed' => 0,
];
$me['owned'] = $this->ownedChapter;
if ($user->id > 0) {
$chapterVoteRepo = new ChapterVoteRepo();
$chapterVote = $chapterVoteRepo->findChapterVote($chapter->id, $user->id);
if ($chapterVote) {
$me['agreed'] = $chapterVote->type == ChapterVoteModel::TYPE_AGREE;
$me['opposed'] = $chapterVote->type == ChapterVoteModel::TYPE_OPPOSE;
$me['agreed'] = $chapterVote->type == ChapterVoteModel::TYPE_AGREE ? 1 : 0;
$me['opposed'] = $chapterVote->type == ChapterVoteModel::TYPE_OPPOSE ? 1 : 0;
}
}
@ -88,10 +78,6 @@ class ChapterInfo extends Service
return $result;
}
/**
* @param ChapterModel $chapter
* @return array
*/
protected function formatChapter(ChapterModel $chapter)
{
$item = [];
@ -111,10 +97,6 @@ class ChapterInfo extends Service
return $item;
}
/**
* @param ChapterModel $chapter
* @return array
*/
protected function formatChapterVod(ChapterModel $chapter)
{
$chapterVodService = new ChapterVodService();
@ -123,7 +105,7 @@ class ChapterInfo extends Service
$course = $this->formatCourse($this->course);
$item = [
return [
'id' => $chapter->id,
'title' => $chapter->title,
'summary' => $chapter->summary,
@ -134,14 +116,8 @@ class ChapterInfo extends Service
'comment_count' => $chapter->comment_count,
'user_count' => $chapter->user_count,
];
return $item;
}
/**
* @param ChapterModel $chapter
* @return array
*/
protected function formatChapterLive(ChapterModel $chapter)
{
$headers = getallheaders();
@ -162,7 +138,7 @@ class ChapterInfo extends Service
$live = $chapterRepo->findChapterLive($chapter->id);
$item = [
return [
'id' => $chapter->id,
'title' => $chapter->title,
'summary' => $chapter->summary,
@ -175,14 +151,8 @@ class ChapterInfo extends Service
'comment_count' => $chapter->comment_count,
'user_count' => $chapter->user_count,
];
return $item;
}
/**
* @param ChapterModel $chapter
* @return array
*/
protected function formatChapterRead(ChapterModel $chapter)
{
$chapterRepo = new ChapterRepo();
@ -191,7 +161,7 @@ class ChapterInfo extends Service
$course = $this->formatCourse($this->course);
$item = [
return [
'id' => $chapter->id,
'title' => $chapter->title,
'summary' => $chapter->summary,
@ -202,28 +172,16 @@ class ChapterInfo extends Service
'comment_count' => $chapter->comment_count,
'user_count' => $chapter->user_count,
];
return $item;
}
/**
* @param CourseModel $course
* @return array
*/
protected function formatCourse(CourseModel $course)
{
$result = [
return [
'id' => $course->id,
'title' => $course->title,
];
return $result;
}
/**
* @param CourseModel $course
* @param UserModel $user
*/
protected function handleCourseUser(CourseModel $course, UserModel $user)
{
if ($user->id == 0) return;
@ -247,10 +205,6 @@ class ChapterInfo extends Service
$course->update();
}
/**
* @param ChapterModel $chapter
* @param UserModel $user
*/
protected function handleChapterUser(ChapterModel $chapter, UserModel $user)
{
if ($user->id == 0) return;
@ -272,5 +226,4 @@ class ChapterInfo extends Service
$chapter->update();
}
}

View File

@ -24,9 +24,7 @@ trait ChapterTrait
{
$validator = new ChapterValidator();
$chapter = $validator->checkChapter($id);
return $chapter;
return $validator->checkChapter($id);
}
/**

View File

@ -1,6 +1,6 @@
<?php
namespace App\Services\Frontend\Chapter;
namespace App\Services\Frontend\Comment;
use App\Models\Comment as CommentModel;
use App\Models\User as UserModel;
@ -15,16 +15,10 @@ class CommentCreate extends Service
use ChapterTrait;
public function createComment($id)
public function createComment()
{
$post = $this->request->getPost();
$chapter = $this->checkChapter($id);
$courseRepo = new CourseRepo();
$course = $courseRepo->findById($chapter->course_id);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
@ -33,12 +27,19 @@ class CommentCreate extends Service
$validator = new CommentValidator();
$chapter = $validator->checkChapter($post['chapter_id']);
$courseRepo = new CourseRepo();
$course = $courseRepo->findById($chapter->course_id);
$data = [];
$data['content'] = $validator->checkContent($post['content']);
if (isset($post['parent_id'])) {
$data['parent_id'] = $validator->checkParentId($post['parent_id']);
$parent = $validator->checkParent($post['parent_id']);
$data['parent_id'] = $parent->id;
}
if (isset($post['mentions'])) {

View File

@ -11,9 +11,7 @@ trait CommentTrait
{
$validator = new CommentValidator();
$comment = $validator->checkComment($id);
return $comment;
return $validator->checkComment($id);
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Services\Frontend\Course;
namespace App\Services\Frontend\Consult;
use App\Models\Consult as ConsultModel;
use App\Models\User as UserModel;
@ -14,12 +14,10 @@ class ConsultCreate extends Service
use CourseTrait;
public function createConsult($id)
public function createConsult()
{
$post = $this->request->getPost();
$course = $this->checkCourse($id);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
@ -28,6 +26,7 @@ class ConsultCreate extends Service
$validator = new ConsultValidator();
$course = $validator->checkCourse($post['course_id']);
$question = $validator->checkQuestion($post['question']);
$consult = new ConsultModel();

View File

@ -11,9 +11,7 @@ trait ConsultTrait
{
$validator = new ConsultValidator();
$consult = $validator->checkConsult($id);
return $consult;
return $validator->checkConsult($id);
}
}

View File

@ -24,20 +24,15 @@ class CourseInfo extends Service
return $this->handleCourse($course, $user);
}
/**
* @param CourseModel $course
* @param UserModel $user
* @return array
*/
protected function handleCourse($course, $user)
protected function handleCourse(CourseModel $course, UserModel $user)
{
$result = $this->formatCourse($course);
$me = [
'joined' => false,
'owned' => false,
'reviewed' => false,
'favorited' => false,
'joined' => 0,
'owned' => 0,
'reviewed' => 0,
'favorited' => 0,
'progress' => 0,
];
@ -48,16 +43,16 @@ class CourseInfo extends Service
$favorite = $favoriteRepo->findCourseFavorite($course->id, $user->id);
if ($favorite && $favorite->deleted == 0) {
$me['favorited'] = true;
$me['favorited'] = 1;
}
if ($this->courseUser) {
$me['reviewed'] = $this->courseUser->reviewed;
$me['progress'] = $this->courseUser->progress;
$me['reviewed'] = $this->courseUser->reviewed ? 1 : 0;
$me['progress'] = $this->courseUser->progress ? 1 : 0;
}
$me['joined'] = $this->joinedCourse;
$me['owned'] = $this->ownedCourse;
$me['joined'] = $this->joinedCourse ? 1 : 0;
$me['owned'] = $this->ownedCourse ? 1 : 0;
}
$result['me'] = $me;
@ -65,13 +60,9 @@ class CourseInfo extends Service
return $result;
}
/**
* @param CourseModel $course
* @return array
*/
protected function formatCourse($course)
{
$result = [
return [
'id' => $course->id,
'title' => $course->title,
'cover' => kg_ci_img_url($course->cover),
@ -82,7 +73,8 @@ class CourseInfo extends Service
'vip_price' => (float)$course->vip_price,
'study_expiry' => $course->study_expiry,
'refund_expiry' => $course->refund_expiry,
'score' => $course->score,
'rating' => (float)$course->rating,
'score' => (float)$course->score,
'model' => $course->model,
'level' => $course->level,
'attrs' => $course->attrs,
@ -91,8 +83,6 @@ class CourseInfo extends Service
'review_count' => $course->review_count,
'favorite_count' => $course->favorite_count,
];
return $result;
}
}

View File

@ -30,9 +30,7 @@ trait CourseTrait
{
$validator = new CourseValidator();
$course = $validator->checkCourse($id);
return $course;
return $validator->checkCourse($id);
}
/**

View File

@ -11,9 +11,7 @@ trait OrderTrait
{
$validator = new OrderValidator();
$order = $validator->checkOrderBySn($sn);
return $order;
return $validator->checkOrderBySn($sn);
}
}

View File

@ -1,10 +1,9 @@
<?php
namespace App\Services\Frontend\Course;
namespace App\Services\Frontend\Review;
use App\Models\Review as ReviewModel;
use App\Models\User as UserModel;
use App\Services\Frontend\CourseTrait;
use App\Services\Frontend\Service;
use App\Validators\Review as ReviewValidator;
use App\Validators\UserDailyLimit as UserDailyLimitValidator;
@ -12,14 +11,10 @@ use App\Validators\UserDailyLimit as UserDailyLimitValidator;
class ReviewCreate extends Service
{
use CourseTrait;
public function createReview($id)
public function createReview()
{
$post = $this->request->getPost();
$course = $this->checkCourse($id);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
@ -28,11 +23,12 @@ class ReviewCreate extends Service
$validator = new ReviewValidator();
$validator->checkIfReviewed($course->id, $user->id);
$course = $validator->checkCourse($post['course_id']);
$content = $validator->checkContent($post['content']);
$rating = $validator->checkRating($post['rating']);
$validator->checkIfReviewed($course->id, $user->id);
$review = new ReviewModel();
$review->course_id = $course->id;

View File

@ -1,10 +1,12 @@
<?php
namespace App\Services\Frontend;
namespace App\Services\Frontend\Review;
use App\Models\ReviewVote as ReviewVoteModel;
use App\Models\User as UserModel;
use App\Repos\ReviewVote as ReviewVoteRepo;
use App\Services\Frontend\ReviewTrait;
use App\Services\Frontend\Service;
use App\Validators\UserDailyLimit as UserDailyLimitValidator;
class ReviewVote extends Service

View File

@ -11,9 +11,7 @@ trait ReviewTrait
{
$validator = new ReviewValidator();
$review = $validator->checkReview($id);
return $review;
return $validator->checkReview($id);
}
}

View File

@ -2,11 +2,7 @@
namespace App\Services\Frontend;
use App\Traits\Auth as AuthTrait;
use Phalcon\Mvc\User\Component;
class Service extends Component
class Service extends \App\Services\Service
{
use AuthTrait;
}

View File

@ -28,7 +28,7 @@ class TeacherInfo extends Service
$user->vip = $user->vip == 1;
$user->locked = $user->locked == 1;
$result = [
return [
'id' => $user->id,
'name' => $user->name,
'avatar' => $user->avatar,
@ -45,8 +45,6 @@ class TeacherInfo extends Service
'notice_count' => $user->notice_count,
'msg_count' => $user->msg_count,
];
return $result;
}
}

View File

@ -28,7 +28,7 @@ class UserInfo extends Service
$user->vip = $user->vip == 1;
$user->locked = $user->locked == 1;
$result = [
return [
'id' => $user->id,
'name' => $user->name,
'avatar' => $user->avatar,
@ -45,8 +45,6 @@ class UserInfo extends Service
'notice_count' => $user->notice_count,
'msg_count' => $user->msg_count,
];
return $result;
}
}

View File

@ -11,9 +11,7 @@ trait UserTrait
{
$validator = new UserValidator();
$user = $validator->checkUser($id);
return $user;
return $validator->checkUser($id);
}
}

View File

@ -2,7 +2,6 @@
namespace App\Traits;
use App\Exceptions\Unauthorized as UnauthorizedException;
use App\Models\User as UserModel;
use App\Repos\User as UserRepo;
use App\Services\Auth as AuthService;
@ -31,7 +30,6 @@ trait Auth
/**
* @return UserModel
* @throws UnauthorizedException
*/
public function getLoginUser()
{

View File

@ -23,20 +23,7 @@ class Comment extends Validator
return $comment;
}
public function checkCourseId($courseId)
{
$courseRepo = new CourseRepo();
$course = $courseRepo->findById($courseId);
if (!$course) {
throw new BadRequestException('comment.invalid_course_id');
}
return $course->id;
}
public function checkChapterId($chapterId)
public function checkChapter($chapterId)
{
$chapterRepo = new ChapterRepo();
@ -46,20 +33,20 @@ class Comment extends Validator
throw new BadRequestException('comment.invalid_chapter_id');
}
return $chapter->id;
return $chapter;
}
public function checkParentId($parentId)
public function checkParent($parentId)
{
$commentRepo = new CourseRepo();
$comment = $commentRepo->findById($parentId);
$parent = $commentRepo->findById($parentId);
if (!$comment) {
if (!$parent) {
throw new BadRequestException('comment.invalid_parent_id');
}
return $comment->id;
return $parent;
}
public function checkContent($content)

View File

@ -22,7 +22,7 @@ class Consult extends Validator
return $consult;
}
public function checkCourseId($courseId)
public function checkCourse($courseId)
{
$courseRepo = new CourseRepo();
@ -32,7 +32,7 @@ class Consult extends Validator
throw new BadRequestException('consult.invalid_course_id');
}
return $course->id;
return $course;
}
public function checkQuestion($question)

View File

@ -23,7 +23,7 @@ class Review extends Validator
return $review;
}
public function checkCourseId($courseId)
public function checkCourse($courseId)
{
$courseRepo = new CourseRepo();
@ -33,7 +33,7 @@ class Review extends Validator
throw new BadRequestException('review.course_not_found');
}
return $courseId;
return $course;
}
public function checkContent($content)