EduSoho 如何与第三方平台实现同步注册和开通课程
1、注册
使用方法
HTTP-POST > /api/users
对应文件与代码
api/src/Topxia/Api/Resource/Users.php
public function post(Application $app, Request $request)
{
$fields = $request->request->all();
if (!ArrayToolkit::requireds($fields, array('email', 'nickname', 'password'))) {
return array('message' => '缺少必填字段');
}
if (empty($fields['registeredWay']) || !in_array(strtolower($fields['registeredWay']), array('ios', 'android'))) {
$fields['registeredWay'] = $this->guessDeviceFromUserAgent($request->headers->get("user-agent"));
}
$ip = $request->getClientIp();
$fields['createdIp'] = $ip;
$authSettings = ServiceKernel::instance()->createService('System:SettingService')->get('auth', array());
if (isset($authSettings['register_protective'])) {
$type = $authSettings['register_protective'];
switch ($type) {
case 'middle':
$condition = array(
'startTime' => time() - 24 * 3600,
'createdIp' => $ip);
$registerCount = $this->getUserService()->countUsers($condition);
if ($registerCount > 30) {
goto failure;
}
goto register;
break;
case 'high':
$condition = array(
'startTime' => time() - 24 * 3600,
'createdIp' => $ip);
$registerCount = $this->getUserService()->countUsers($condition);
if ($registerCount > 10) {
goto failure;
}
$registerCount = $this->getUserService()->countUsers(array(
'startTime' => time() - 3600,
'createdIp' => $ip));
if ($registerCount >= 1) {
goto failure;
}
goto register;
break;
default:
goto register;
break;
}
}
register:
$user = $this->getUserService()->register($fields, array('mobile'));
$user['profile'] = $this->getUserService()->getUserProfile($user['id']);
return $this->callFilter('User', $user);
failure:
return array('message' => '已经超出用户注册次数限制,用户注册失败');
}
2、加入课程
使用方法
HTTP-POST /courses/{id}/members
官方文档
认证与授权 · EduSoho开发指南
http://developer.edusoho.com/api/auth.html
课程 · EduSoho开发指南
http://developer.edusoho.com/api/course.html#教学计划成员
对应文件与代码
src/ApiBundle/Api/Resource/Course/CourseMember.php
public function add(ApiRequest $request, $courseId)
{
$course = $this->getCourseService()->getCourse($courseId);
if (!$course) {
throw CourseException::NOTFOUND_COURSE();
}
$member = $this->getMemberService()->getCourseMember($courseId, $this->getCurrentUser()->getId());
if (!$member) {
$member = $this->tryJoin($course);
}
if ($member) {
$this->getOCUtil()->single($member, array('userId'));
return $member;
}
return null;
}