Skip to content

Commit

Permalink
Merge pull request #106 from Qsnh/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Qsnh authored Mar 27, 2020
2 parents eec06f3 + 9aff6ea commit f99c403
Show file tree
Hide file tree
Showing 50 changed files with 1,027 additions and 496 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ ALI_OSS_DOMAIN=

# 微信小程序
WECHAT_MINI_APP_ID=
WECHAT_MINI_APP_SECRET=
WECHAT_MINI_APP_SECRET=

# MeEduCloud
MEEDUCLOUD_DOMAIN=https://meedu.vip
MEEDUCLOUD_USER_ID=
MEEDUCLOUD_PASSWORD=
7 changes: 6 additions & 1 deletion .env.install
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ ALI_OSS_DOMAIN=

# 微信小程序
WECHAT_MINI_APP_ID=
WECHAT_MINI_APP_SECRET=
WECHAT_MINI_APP_SECRET=

# MeEduCloud
MEEDUCLOUD_DOMAIN=https://meedu.vip
MEEDUCLOUD_USER_ID=
MEEDUCLOUD_PASSWORD=
14 changes: 12 additions & 2 deletions app/Businesses/BusinessState.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BusinessState
* @param array $course
* @param array $video
* @return bool
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function canSeeVideo(array $user, array $course, array $video): bool
{
Expand Down Expand Up @@ -98,6 +99,13 @@ public function isRole(array $user): bool
return true;
}

/**
* 是否可以生成邀请码
*
* @param array $user
* @return bool
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function canGenerateInviteCode(array $user): bool
{
/**
Expand Down Expand Up @@ -125,11 +133,12 @@ public function canGenerateInviteCode(array $user): bool
/**
* @param array $promoCode
* @return bool
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function promoCodeCanUse(array $promoCode): bool
{
// 自己不能使用自己的优惠码
if ($promoCode['user_id'] == Auth::id()) {
if ($promoCode['user_id'] === Auth::id()) {
return false;
}
if ($promoCode['use_times'] > 0 && $promoCode['use_times'] - $promoCode['used_times'] <= 0) {
Expand Down Expand Up @@ -167,12 +176,13 @@ public function promoCodeCanUse(array $promoCode): bool
*/
public function isUserInvitePromoCode(string $code): bool
{
return strtolower(substr($code, 0, 1)) == 'u';
return strtolower($code[0]) === 'u';
}

/**
* @param array $order
* @return int
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function calculateOrderNeedPaidSum(array $order): int
{
Expand Down
26 changes: 23 additions & 3 deletions app/Http/Controllers/Api/V2/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Constant\ApiV2Constant;
use Illuminate\Support\Facades\Auth;
use App\Services\Member\Services\RoleService;
use App\Services\Member\Services\UserService;
use App\Services\Order\Services\OrderService;
use App\Services\Course\Services\VideoService;
use App\Services\Course\Services\CourseService;
Expand Down Expand Up @@ -53,18 +54,25 @@ class OrderController extends BaseController
*/
protected $promoCodeService;

/**
* @var UserService
*/
protected $userService;

public function __construct(
CourseServiceInterface $courseService,
OrderServiceInterface $orderService,
RoleServiceInterface $roleService,
VideoServiceInterface $videoService,
PromoCodeServiceInterface $promoCodeService
PromoCodeServiceInterface $promoCodeService,
UserService $userService
) {
$this->courseService = $courseService;
$this->orderService = $orderService;
$this->roleService = $roleService;
$this->videoService = $videoService;
$this->promoCodeService = $promoCodeService;
$this->userService = $userService;
}

/**
Expand Down Expand Up @@ -93,10 +101,16 @@ public function __construct(
public function createCourseOrder(Request $request)
{
$courseId = $request->input('course_id');
$course = $this->courseService->find($courseId);
if ($course['charge'] === 0) {
return $this->error(__('course cant buy'));
}
if ($this->userService->hasCourse(Auth::id(), $course['id'])) {
return $this->error(__('course purchased'));
}
$code = $request->input('promo_code');
$promoCode = [];
$code && $promoCode = $this->promoCodeService->findCode($code);
$course = $this->courseService->find($courseId);
$order = $this->orderService->createCourseOrder(Auth::id(), $course, $promoCode['id'] ?? 0);
$order = arr1_clear($order, ApiV2Constant::MODEL_ORDER_FIELD);
return $this->data($order);
Expand Down Expand Up @@ -163,10 +177,16 @@ public function createRoleOrder(Request $request)
public function createVideoOrder(Request $request)
{
$videoId = $request->input('video_id');
$video = $this->videoService->find($videoId);
if ($video['charge'] === 0) {
return $this->error(__('video cant buy'));
}
if ($this->userService->hasVideo(Auth::id(), $video['id'])) {
return $this->error(__('video purchased'));
}
$code = $request->input('promo_code');
$promoCode = [];
$code && $promoCode = $this->promoCodeService->findCode($code);
$video = $this->videoService->find($videoId);
$order = $this->orderService->createVideoOrder(Auth::id(), $video, $promoCode['id'] ?? 0);
$order = arr1_clear($order, ApiV2Constant::MODEL_ORDER_FIELD);
return $this->data($order);
Expand Down
24 changes: 12 additions & 12 deletions app/Http/Controllers/Backend/Api/V1/AdFromController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ public function destroy($id)
public function number(Request $request, $id)
{
$ad = AdFrom::findOrFail($id);
$startDate = $request->input('start_date', date('Y-m-d', Carbon::now()->subDays(30)->timestamp));
$endDate = $request->input('end_date', date('Y-m-d', Carbon::now()->timestamp));
$records = $ad->numbers()->whereBetween('day', [$startDate, $endDate])->get();
$labels = [];
$dataset = [];
foreach ($records as $item) {
$labels[] = $item->day;
$dataset[] = $item->num;
$startDate = Carbon::parse($request->input('start_date', Carbon::now()->subMonths(1)));
$endDate = Carbon::parse($request->input('end_date', Carbon::now()));
$records = $ad->numbers()->select(['day', 'num'])->whereBetween('day', [$startDate->timestamp, $endDate->timestamp])->get();
$data = [];
while ($startDate->lt($endDate)) {
$data[$startDate->format('Y-m-d')] = 0;
$startDate->addDays(1);
}
foreach ($records as $record) {
$data[$record->day] += $record->num;
}

return $this->successData([
'ad' => $ad,
'labels' => $labels,
'dataset' => $dataset,
'labels' => array_keys($data),
'dataset' => array_values($data),
]);
}
}
168 changes: 166 additions & 2 deletions app/Http/Controllers/Backend/Api/V1/AddonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace App\Http\Controllers\Backend\Api\V1;

use App\Meedu\Addons;
use GuzzleHttp\Client;
use App\Meedu\MeEduCloud;
use Illuminate\Http\Request;
use Chumper\Zipper\Facades\Zipper;

class AddonsController extends BaseController
{
Expand Down Expand Up @@ -51,14 +54,175 @@ public function switchHandler(Request $request, Addons $lib)
$sign = $request->input('sign');
$action = $request->input('action');
try {
if ($action == 'enabled') {
if ($action === 'enabled') {
$lib->enabled($sign);
} elseif ($action == 'disabled') {
$lib->install($sign);
} elseif ($action === 'disabled') {
$lib->uninstall($sign);
$lib->disabled($sign);
}
return $this->success();
} catch (\Exception $exception) {
return $this->error($exception->getMessage());
}
}

/**
* @param Addons $lib
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function repository(Request $request, Addons $lib)
{
$mc = new MeEduCloud(
config('meedu.meeducloud.domain'),
config('meedu.meeducloud.user_id'),
config('meedu.meeducloud.password')
);
$addons = $mc->addons($request->input('page'), $request->input('size'));
$addonsData = $addons['data'];

// 读取已购买的插件
$buyAddons = [];
try {
$buyAddons = $mc->userAddons();
$buyAddons = array_column($buyAddons, null, 'sign');
} catch (\Exception $e) {
}

// 读取本地安装的插件
$installAddons = $lib->addons();
$installAddons = array_column($installAddons, null, 'sign');

foreach ($addonsData as $key => $item) {
$addonsData[$key]['is_buy'] = isset($buyAddons[$item['sign']]);
$isInstall = isset($installAddons[$item['sign']]);
$addonsData[$key]['is_install'] = $isInstall;
$addonsData[$key]['is_upgrade'] = false;
if ($isInstall) {
$addonsData[$key]['is_upgrade'] = version_compare($item['version'], $installAddons[$item['sign']]['version'], '>');
$addonsData[$key]['local_version'] = $installAddons[$item['sign']]['version'];
}
}

$addons['data'] = $addonsData;

return $this->successData($addons);
}

/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function buyAddons(Request $request)
{
$addonsId = $request->input('addons_id');
$mc = new MeEduCloud(
config('meedu.meeducloud.domain'),
config('meedu.meeducloud.user_id'),
config('meedu.meeducloud.password')
);
try {
$mc->addonsBuy($addonsId);

return $this->success();
} catch (\Exception $exception) {
return $this->error($exception->getMessage());
}
}

/**
* 安装插件
* @param Request $request
* @param Addons $lib
* @return \Illuminate\Http\JsonResponse
*/
public function installAddons(Request $request, Addons $lib)
{
$addonsId = $request->input('addons_id');
$addonsSign = $request->input('addons_sign');
$mc = new MeEduCloud(
config('meedu.meeducloud.domain'),
config('meedu.meeducloud.user_id'),
config('meedu.meeducloud.password')
);
try {
// 获取下载url
$url = $mc->addonsDownloadUrl($addonsId, 0);

// 下载插件
$storagePath = storage_path('app/templates/' . $addonsSign . '_' . time() . random_int(0, 100) . '.zip');
$http = new Client();
$http->get($url, [
'sink' => $storagePath,
]);

// 解压
Zipper::make($storagePath)->extractTo(base_path('addons'));

// 安装命令
$lib->install($addonsSign);

// 删除缓存文件
@unlink($storagePath);

return $this->success();
} catch (\Exception $exception) {
return $this->error($exception->getMessage());
}
}

public function upgradeAddons(Request $request, Addons $lib)
{
$addonsId = $request->input('addons_id');
$addonsSign = $request->input('addons_sign');
$mc = new MeEduCloud(
config('meedu.meeducloud.domain'),
config('meedu.meeducloud.user_id'),
config('meedu.meeducloud.password')
);
try {
// 获取下载url
$url = $mc->addonsDownloadUrl($addonsId, 0);

// 下载插件
$storagePath = storage_path('app/templates/' . time() . random_int(0, 100) . '.zip');
$http = new Client();
$http->get($url, [
'sink' => $storagePath,
]);

// 解压
Zipper::make($storagePath)->extractTo(base_path('addons'));

// 执行升级命令
$lib->upgrade($addonsSign);

// 删除缓存文件
@unlink($storagePath);

return $this->success();
} catch (\Exception $exception) {
return $this->error($exception->getMessage());
}
}

/**
* @return \Illuminate\Http\JsonResponse
*/
public function user()
{
$mc = new MeEduCloud(
config('meedu.meeducloud.domain'),
config('meedu.meeducloud.user_id'),
config('meedu.meeducloud.password')
);
try {
$user = $mc->user();

return $this->successData($user);
} catch (\Exception $exception) {
return $this->error($exception->getMessage());
}
}
}
Loading

0 comments on commit f99c403

Please sign in to comment.