From 15d822a93484704a4a5b88cead6eb15a1ce0e39d Mon Sep 17 00:00:00 2001 From: koogua Date: Fri, 17 Sep 2021 16:53:11 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=9B=B4=E6=92=AD=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Home/Views/course/show_catalog.volt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Home/Views/course/show_catalog.volt b/app/Http/Home/Views/course/show_catalog.volt index d6502be6..e7ed78b3 100644 --- a/app/Http/Home/Views/course/show_catalog.volt +++ b/app/Http/Home/Views/course/show_catalog.volt @@ -66,7 +66,7 @@ {%- endmacro %} {%- macro live_status_info(lesson) %} - {% if lesson.attrs.start_time < time() and lesson.attrs.end_time > time() %} + {% if lesson.attrs.stream.status == 'active' %} {{ date('m月d日 H:i',lesson.attrs.start_time) }} 直播中 {% elseif lesson.attrs.start_time > time() %} {{ date('m月d日 H:i',lesson.attrs.start_time) }} 倒计时 From b65c66d99c2e10c6145dd3af90a688ffc5fea168 Mon Sep 17 00:00:00 2001 From: koogua Date: Mon, 20 Sep 2021 17:50:27 +0800 Subject: [PATCH 02/21] v1.4.5 beta1 --- .../Api/Controllers/ConnectController.php | 52 +++++ app/Http/Api/Controllers/TradeController.php | 14 +- app/Http/Api/Services/Connect.php | 190 ++++++++++++++++++ app/Http/Api/Services/Trade.php | 41 +++- app/Library/AppInfo.php | 2 +- app/Library/Helper.php | 12 +- app/Models/Connect.php | 33 ++- app/Services/OAuth/QQ.php | 1 + app/Services/OAuth/WeChat.php | 94 +++++++++ app/Services/OAuth/WeiBo.php | 1 + app/Services/OAuth/WeiXin.php | 1 + app/Services/Pay/Wxpay.php | 31 +++ db/migrations/20210917093354.php | 56 ++++++ 13 files changed, 522 insertions(+), 6 deletions(-) create mode 100644 app/Http/Api/Controllers/ConnectController.php create mode 100644 app/Http/Api/Services/Connect.php create mode 100644 app/Services/OAuth/WeChat.php create mode 100644 db/migrations/20210917093354.php diff --git a/app/Http/Api/Controllers/ConnectController.php b/app/Http/Api/Controllers/ConnectController.php new file mode 100644 index 00000000..cf34611c --- /dev/null +++ b/app/Http/Api/Controllers/ConnectController.php @@ -0,0 +1,52 @@ +getAuthorizeUrl(ConnectModel::PROVIDER_WECHAT); + + return $this->response->redirect($url, true); + } + + /** + * @Get("/wechat/callback", name="api.oauth.wechat_callback") + */ + public function wechatCallbackAction() + { + $service = new ConnectService(); + + $data = $service->handleCallback(ConnectModel::PROVIDER_WECHAT); + + $location = kg_h5_index_url() . '?' . http_build_query($data); + + return $this->response->redirect($location, true); + } + + /** + * @Get("/wechat/info", name="api.oauth.wechat_info") + */ + public function wechatInfoAction() + { + $service = new ConnectService(); + + $wechat = $service->getWechatInfo(); + + return $this->jsonSuccess(['wechat' => $wechat]); + } + +} diff --git a/app/Http/Api/Controllers/TradeController.php b/app/Http/Api/Controllers/TradeController.php index 4e772559..bb03665d 100644 --- a/app/Http/Api/Controllers/TradeController.php +++ b/app/Http/Api/Controllers/TradeController.php @@ -54,6 +54,18 @@ class TradeController extends Controller return $this->jsonSuccess($content); } + /** + * @Post("/mini/create", name="api.trade.mini_create") + */ + public function createMiniTradeAction() + { + $service = new TradeService(); + + $content = $service->createMiniTrade(); + + return $this->jsonSuccess($content); + } + /** * @Post("/app/create", name="api.trade.app_create") */ @@ -61,7 +73,7 @@ class TradeController extends Controller { $service = new TradeService(); - $content = $service->createMpTrade(); + $content = $service->createAppTrade(); return $this->jsonSuccess($content); } diff --git a/app/Http/Api/Services/Connect.php b/app/Http/Api/Services/Connect.php new file mode 100644 index 00000000..f64b01e5 --- /dev/null +++ b/app/Http/Api/Services/Connect.php @@ -0,0 +1,190 @@ + kg_full_url(['for' => 'api.oauth.wechat']), + 'auto_login' => 1, + ]; + } + + public function handleCallback($provider) + { + $code = $this->request->getQuery('code'); + $state = $this->request->getQuery('state'); + + $openUser = $this->getOpenUserInfo($code, $state, $provider); + $relation = $this->getConnectRelation($openUser['id'], $provider); + + $token = null; + + if ($relation) { + + $userRepo = new UserRepo(); + + $user = $userRepo->findById($relation->user_id); + + $auth = new ApiAuthService(); + + $token = $auth->saveAuthInfo($user); + + $this->handleLoginNotice($user); + } + + return [ + 'openid' => $openUser['id'], + 'token' => $token, + ]; + } + + public function getAuthorizeUrl($provider) + { + $auth = $this->getConnectAuth($provider); + + return $auth->getAuthorizeUrl(); + } + + public function getOpenUserInfo($code, $state, $provider) + { + $auth = $this->getConnectAuth($provider); + + $auth->checkState($state); + + $token = $auth->getAccessToken($code); + + $openId = $auth->getOpenId($token); + + return $auth->getUserInfo($token, $openId); + } + + public function getConnectRelation($openId, $provider) + { + $connectRepo = new ConnectRepo(); + + return $connectRepo->findByOpenId($openId, $provider); + } + + public function getConnectAuth($provider) + { + $auth = null; + + switch ($provider) { + case ConnectModel::PROVIDER_QQ: + $auth = $this->getQQAuth(); + break; + case ConnectModel::PROVIDER_WEIBO: + $auth = $this->getWeiBoAuth(); + break; + case ConnectModel::PROVIDER_WECHAT: + $auth = $this->getWeChatAuth(); + break; + } + + if (!$auth) { + throw new Exception('Invalid OAuth Provider'); + } + + return $auth; + } + + protected function getQQAuth() + { + $settings = $this->getSettings('oauth.qq'); + + $settings['redirect_uri'] = kg_full_url(['for' => 'api.oauth.qq_callback']); + + return new QQAuth( + $settings['client_id'], + $settings['client_secret'], + $settings['redirect_uri'] + ); + } + + protected function getWeChatAuth() + { + /** + * 使用的是微信公众号网页授权登录功能 + */ + $settings = $this->getSettings('wechat.oa'); + + $settings['redirect_uri'] = kg_full_url(['for' => 'api.oauth.wechat_callback']); + + return new WeChatAuth( + $settings['app_id'], + $settings['app_secret'], + $settings['redirect_uri'] + ); + } + + protected function getWeiBoAuth() + { + $settings = $this->getSettings('oauth.weibo'); + + $settings['redirect_uri'] = kg_full_url(['for' => 'api.oauth.weibo_callback']); + + return new WeiBoAuth( + $settings['client_id'], + $settings['client_secret'], + $settings['redirect_uri'] + ); + } + + protected function handleConnectRelation(UserModel $user, array $openUser) + { + $connectRepo = new ConnectRepo(); + + $connect = $connectRepo->findByOpenId($openUser['id'], $openUser['provider']); + + if ($connect) { + + $connect->open_name = $openUser['name']; + $connect->open_avatar = $openUser['avatar']; + + if ($connect->user_id != $user->id) { + $connect->user_id = $user->id; + } + + if ($connect->deleted == 1) { + $connect->deleted = 0; + } + + $connect->update(); + + } else { + + $connect = new ConnectModel(); + + $connect->user_id = $user->id; + $connect->open_id = $openUser['id']; + $connect->open_name = $openUser['name']; + $connect->open_avatar = $openUser['avatar']; + $connect->provider = $openUser['provider']; + + $connect->create(); + } + } + + protected function handleLoginNotice(UserModel $user) + { + $service = new AccountLoginNoticeService(); + + $service->createTask($user); + } + +} diff --git a/app/Http/Api/Services/Trade.php b/app/Http/Api/Services/Trade.php index 933aae6e..36946153 100644 --- a/app/Http/Api/Services/Trade.php +++ b/app/Http/Api/Services/Trade.php @@ -75,6 +75,45 @@ class Trade extends Service { $post = $this->request->getPost(); + $order = $this->checkOrderBySn($post['order_sn']); + + $user = $this->getLoginUser(); + + $channel = TradeModel::CHANNEL_WXPAY; + + $trade = new TradeModel(); + + $trade->subject = $order->subject; + $trade->amount = $order->amount; + $trade->channel = $channel; + $trade->order_id = $order->id; + $trade->owner_id = $user->id; + + $trade->create(); + + $wxpay = new Wxpay(); + + $response = $wxpay->mp($trade, $post['open_id']); + + $payment = [ + 'appId' => $response->appId, + 'timeStamp' => $response->timeStamp, + 'nonceStr' => $response->nonceStr, + 'package' => $response->package, + 'signType' => $response->signType, + 'paySign' => $response->paySign, + ]; + + return [ + 'trade' => $this->handleTradeInfo($trade->sn), + 'payment' => $payment, + ]; + } + + public function createMiniTrade() + { + $post = $this->request->getPost(); + $validator = new ClientValidator(); $platform = $this->getPlatform(); @@ -120,7 +159,7 @@ class Trade extends Service public function createAppTrade() { - + return []; } protected function getPlatform() diff --git a/app/Library/AppInfo.php b/app/Library/AppInfo.php index 2e94c37e..55c22de9 100644 --- a/app/Library/AppInfo.php +++ b/app/Library/AppInfo.php @@ -16,7 +16,7 @@ class AppInfo protected $link = 'https://koogua.com'; - protected $version = '1.4.4'; + protected $version = '1.4.5'; public function __get($name) { diff --git a/app/Library/Helper.php b/app/Library/Helper.php index fcc32506..3288ff70 100644 --- a/app/Library/Helper.php +++ b/app/Library/Helper.php @@ -707,4 +707,14 @@ function kg_full_url($uri, $args = null) $baseUrl = kg_site_url(); return $baseUrl . $url->get($uri, $args); -} \ No newline at end of file +} + +/** + * 获取H5首页地址 + * + * @return string + */ +function kg_h5_index_url() +{ + return kg_site_url() . '/h5/#/pages/index/index'; +} diff --git a/app/Models/Connect.php b/app/Models/Connect.php index b4700dd8..9f7d17e7 100644 --- a/app/Models/Connect.php +++ b/app/Models/Connect.php @@ -7,12 +7,15 @@ namespace App\Models; +use Phalcon\Mvc\Model\Behavior\SoftDelete; + class Connect extends Model { const PROVIDER_QQ = 1; // QQ - const PROVIDER_WEIXIN = 2; // 微信 - const PROVIDER_WEIBO = 3; // 微博 + const PROVIDER_WEIXIN = 2; // 微信扫码 + const PROVIDER_WEIBO = 3; // 新浪微博 + const PROVIDER_WECHAT = 4; // 公众号网页 /** * 主键编号 @@ -28,6 +31,13 @@ class Connect extends Model */ public $user_id = 0; + /** + * 联合ID + * + * @var string + */ + public $union_id = ''; + /** * 开放ID * @@ -56,6 +66,13 @@ class Connect extends Model */ public $provider = 0; + /** + * 删除标识 + * + * @var int + */ + public $deleted = 0; + /** * 创建时间 * @@ -75,6 +92,18 @@ class Connect extends Model return 'kg_connect'; } + public function initialize() + { + parent::initialize(); + + $this->addBehavior( + new SoftDelete([ + 'field' => 'deleted', + 'value' => 1, + ]) + ); + } + public function beforeCreate() { $this->create_time = time(); diff --git a/app/Services/OAuth/QQ.php b/app/Services/OAuth/QQ.php index 444f5d66..dd6dac68 100644 --- a/app/Services/OAuth/QQ.php +++ b/app/Services/OAuth/QQ.php @@ -113,6 +113,7 @@ class QQ extends OAuth $userInfo['name'] = $data['nickname']; $userInfo['avatar'] = $data['figureurl']; $userInfo['provider'] = ConnectModel::PROVIDER_QQ; + $userInfo['unionid'] = ''; return $userInfo; } diff --git a/app/Services/OAuth/WeChat.php b/app/Services/OAuth/WeChat.php new file mode 100644 index 00000000..29e541a3 --- /dev/null +++ b/app/Services/OAuth/WeChat.php @@ -0,0 +1,94 @@ + $this->clientId, + 'redirect_uri' => $this->redirectUri, + 'response_type' => 'code', + 'scope' => 'snsapi_userinfo', + 'state' => $this->getState(), + ]; + + return self::AUTHORIZE_URL . '?' . http_build_query($params) . '#wechat_redirect'; + } + + public function getAccessToken($code) + { + $params = [ + 'code' => $code, + 'appid' => $this->clientId, + 'secret' => $this->clientSecret, + 'grant_type' => 'authorization_code', + ]; + + $response = $this->httpPost(self::ACCESS_TOKEN_URL, $params); + + $this->accessToken = $this->parseAccessToken($response); + + return $this->accessToken; + } + + public function getOpenId($accessToken = null) + { + return $this->openId; + } + + public function getUserInfo($accessToken, $openId) + { + $params = [ + 'access_token' => $accessToken, + 'openid' => $openId, + ]; + + $response = $this->httpGet(self::USER_INFO_URL, $params); + + return $this->parseUserInfo($response); + } + + private function parseAccessToken($response) + { + $data = json_decode($response, true); + + if (isset($data['errcode']) && $data['errcode'] != 0) { + throw new \Exception("Fetch Access Token Failed:{$response}"); + } + + $this->openId = $data['openid']; + + return $data['access_token']; + } + + private function parseUserInfo($response) + { + $data = json_decode($response, true); + + if (isset($data['errcode']) && $data['errcode'] != 0) { + throw new \Exception("Fetch User Info Failed:{$response}"); + } + + $userInfo['id'] = $data['openid']; + $userInfo['name'] = $data['nickname']; + $userInfo['avatar'] = $data['headimgurl']; + $userInfo['unionid'] = $data['unionid'] ?? ''; + $userInfo['provider'] = ConnectModel::PROVIDER_WECHAT; + + return $userInfo; + } + +} diff --git a/app/Services/OAuth/WeiBo.php b/app/Services/OAuth/WeiBo.php index 698ec0a6..ba98eb74 100644 --- a/app/Services/OAuth/WeiBo.php +++ b/app/Services/OAuth/WeiBo.php @@ -88,6 +88,7 @@ class WeiBo extends OAuth $userInfo['name'] = $data['name']; $userInfo['avatar'] = $data['profile_image_url']; $userInfo['provider'] = ConnectModel::PROVIDER_WEIBO; + $userInfo['unionid'] = ''; return $userInfo; } diff --git a/app/Services/OAuth/WeiXin.php b/app/Services/OAuth/WeiXin.php index c5d22282..52703c16 100644 --- a/app/Services/OAuth/WeiXin.php +++ b/app/Services/OAuth/WeiXin.php @@ -87,6 +87,7 @@ class WeiXin extends OAuth $userInfo['id'] = $data['openid']; $userInfo['name'] = $data['nickname']; $userInfo['avatar'] = $data['headimgurl']; + $userInfo['unionid'] = $data['unionid'] ?? ''; $userInfo['provider'] = ConnectModel::PROVIDER_WEIXIN; return $userInfo; diff --git a/app/Services/Pay/Wxpay.php b/app/Services/Pay/Wxpay.php index 86c8493e..c72a4182 100644 --- a/app/Services/Pay/Wxpay.php +++ b/app/Services/Pay/Wxpay.php @@ -121,6 +121,37 @@ class Wxpay extends PayService return $result; } + /** + * 公众号支付 + * + * @param TradeModel $trade + * @param string $openId + * @return Collection|bool + */ + public function mp(TradeModel $trade, $openId) + { + try { + + $result = $this->gateway->mp([ + 'out_trade_no' => $trade->sn, + 'total_fee' => 100 * $trade->amount, + 'body' => $trade->subject, + 'openid' => $openId, + ]); + + } catch (\Exception $e) { + + Log::error('Wxpay MP Exception', [ + 'code' => $e->getCode(), + 'message' => $e->getMessage(), + ]); + + $result = false; + } + + return $result; + } + /** * 小程序支付 * diff --git a/db/migrations/20210917093354.php b/db/migrations/20210917093354.php new file mode 100644 index 00000000..f4ddbfe9 --- /dev/null +++ b/db/migrations/20210917093354.php @@ -0,0 +1,56 @@ +alterConnectTable(); + $this->alterWechatSubscribeTable(); + } + + protected function alterConnectTable() + { + $this->table('kg_connect') + ->addColumn('deleted', 'integer', [ + 'null' => false, + 'default' => '0', + 'limit' => MysqlAdapter::INT_REGULAR, + 'signed' => false, + 'comment' => '删除标识', + 'after' => 'provider', + ]) + ->save(); + } + + protected function alterWechatSubscribeTable() + { + $this->table('kg_wechat_subscribe') + ->addColumn('union_id', 'string', [ + 'null' => false, + 'default' => '', + 'limit' => 64, + 'collation' => 'utf8mb4_general_ci', + 'encoding' => 'utf8mb4', + 'comment' => '联合ID', + 'after' => 'open_id', + ]) + ->addColumn('deleted', 'integer', [ + 'null' => false, + 'default' => '0', + 'limit' => MysqlAdapter::INT_REGULAR, + 'signed' => false, + 'comment' => '删除标识', + 'after' => 'open_id', + ]) + ->save(); + } + +} From 616c7aa40d53a78e4a8980c268ac5a139cb83f4b Mon Sep 17 00:00:00 2001 From: koogua Date: Tue, 21 Sep 2021 08:14:06 +0800 Subject: [PATCH 03/21] v1.4.5 beta2 --- .../Api/Controllers/ConnectController.php | 24 +++++++- app/Http/Api/Services/Connect.php | 20 +++++++ .../Home/Controllers/ConnectController.php | 15 ++++- app/Http/Home/Services/Connect.php | 40 +++++++++++-- .../Home/Services/WeChatOfficialAccount.php | 32 ++++++++-- app/Repos/WeChatSubscribe.php | 12 ++++ app/Services/OAuth/WeChat.php | 5 ++ db/migrations/20210917093354.php | 60 +++++++++++++++---- 8 files changed, 187 insertions(+), 21 deletions(-) diff --git a/app/Http/Api/Controllers/ConnectController.php b/app/Http/Api/Controllers/ConnectController.php index cf34611c..9f06fbd2 100644 --- a/app/Http/Api/Controllers/ConnectController.php +++ b/app/Http/Api/Controllers/ConnectController.php @@ -1,4 +1,9 @@ request->getQuery('redirect', 'trim'); + $service = new ConnectService(); + if ($redirect) { + $service->setWechatRedirectCache($redirect); + } + $url = $service->getAuthorizeUrl(ConnectModel::PROVIDER_WECHAT); return $this->response->redirect($url, true); @@ -32,7 +43,18 @@ class ConnectController extends Controller $data = $service->handleCallback(ConnectModel::PROVIDER_WECHAT); - $location = kg_h5_index_url() . '?' . http_build_query($data); + $redirect = $service->getWechatRedirectCache(); + + if ($redirect) { + $service->removeWechatRedirectCache(); + if (strpos($redirect, '?')) { + $location = $redirect . '&' . http_build_query($data); + } else { + $location = $redirect . '?' . http_build_query($data); + } + } else { + $location = kg_h5_index_url() . '?' . http_build_query($data); + } return $this->response->redirect($location, true); } diff --git a/app/Http/Api/Services/Connect.php b/app/Http/Api/Services/Connect.php index f64b01e5..fde7af49 100644 --- a/app/Http/Api/Services/Connect.php +++ b/app/Http/Api/Services/Connect.php @@ -1,4 +1,9 @@ session->get('wechat_redirect'); + } + + public function setWechatRedirectCache($redirect) + { + $this->session->set('wechat_redirect', $redirect); + } + + public function removeWechatRedirectCache() + { + $this->session->remove('wechat_redirect'); + } + public function handleCallback($provider) { $code = $this->request->getQuery('code'); diff --git a/app/Http/Home/Controllers/ConnectController.php b/app/Http/Home/Controllers/ConnectController.php index f9bf060d..395556b3 100644 --- a/app/Http/Home/Controllers/ConnectController.php +++ b/app/Http/Home/Controllers/ConnectController.php @@ -119,6 +119,19 @@ class ConnectController extends Controller $service = new ConnectService(); + $openUser = $service->getOpenUserInfo($code, $state, $provider); + + /** + * 微信扫码登录检查是否关注过公众号,关注过直接登录 + */ + if ($provider == ConnectModel::PROVIDER_WEIXIN && !empty($openUser['unionid'])) { + $subscribe = $service->getWeChatSubscribe($openUser['unionid']); + if ($subscribe && $subscribe->deleted == 0) { + $service->authSubscribeLogin($subscribe); + return $this->response->redirect(['for' => 'home.index']); + } + } + $openUser = $service->getOpenUserInfo($code, $state, $provider); $connect = $service->getConnectRelation($openUser['id'], $openUser['provider']); @@ -129,7 +142,7 @@ class ConnectController extends Controller } } else { if ($connect) { - $service->authLogin($connect); + $service->authConnectLogin($connect); return $this->response->redirect(['for' => 'home.index']); } } diff --git a/app/Http/Home/Services/Connect.php b/app/Http/Home/Services/Connect.php index 3f598fab..df7b7c9f 100644 --- a/app/Http/Home/Services/Connect.php +++ b/app/Http/Home/Services/Connect.php @@ -9,11 +9,14 @@ namespace App\Http\Home\Services; use App\Models\Connect as ConnectModel; use App\Models\User as UserModel; +use App\Models\WeChatSubscribe as WeChatSubscribeModel; use App\Repos\Connect as ConnectRepo; use App\Repos\User as UserRepo; +use App\Repos\WeChatSubscribe as WeChatSubscribeRepo; use App\Services\Auth\Home as AuthService; +use App\Services\Auth\Home as HomeAuthService; use App\Services\Logic\Account\Register as RegisterService; -use App\Services\Logic\Notice\AccountLogin as AccountLoginNoticeService; +use App\Services\Logic\Notice\AccountLogin as AccountLoginNotice; use App\Services\OAuth\QQ as QQAuth; use App\Services\OAuth\WeiBo as WeiBoAuth; use App\Services\OAuth\WeiXin as WeiXinAuth; @@ -79,7 +82,20 @@ class Connect extends Service $this->handleConnectRelation($user, $openUser); } - public function authLogin(ConnectModel $connect) + public function authSubscribeLogin(WeChatSubscribeModel $subscribe) + { + $userRepo = new UserRepo(); + + $user = $userRepo->findById($subscribe->user_id); + + $this->handleLoginNotice($user); + + $auth = new HomeAuthService(); + + $auth->saveAuthInfo($user); + } + + public function authConnectLogin(ConnectModel $connect) { $userRepo = new UserRepo(); @@ -112,6 +128,13 @@ class Connect extends Service return $auth->getUserInfo($token, $openId); } + public function getWeChatSubscribe($unionId) + { + $subscribeRepo = new WeChatSubscribeRepo(); + + return $subscribeRepo->findByUnionId($unionId); + } + public function getConnectRelation($openId, $provider) { $connectRepo = new ConnectRepo(); @@ -200,6 +223,14 @@ class Connect extends Service $connect->user_id = $user->id; } + if (empty($connect->union_id) && !empty($openUser['unionid'])) { + $connect->union_id = $openUser['unionid']; + } + + if ($connect->deleted == 1) { + $connect->deleted = 0; + } + $connect->update(); } else { @@ -207,6 +238,7 @@ class Connect extends Service $connect = new ConnectModel(); $connect->user_id = $user->id; + $connect->union_id = $openUser['unionid']; $connect->open_id = $openUser['id']; $connect->open_name = $openUser['name']; $connect->open_avatar = $openUser['avatar']; @@ -218,9 +250,9 @@ class Connect extends Service protected function handleLoginNotice(UserModel $user) { - $service = new AccountLoginNoticeService(); + $notice = new AccountLoginNotice(); - $service->createTask($user); + $notice->createTask($user); } } diff --git a/app/Http/Home/Services/WeChatOfficialAccount.php b/app/Http/Home/Services/WeChatOfficialAccount.php index d04ec722..038581dd 100644 --- a/app/Http/Home/Services/WeChatOfficialAccount.php +++ b/app/Http/Home/Services/WeChatOfficialAccount.php @@ -114,7 +114,8 @@ class WeChatOfficialAccount extends Service $subscribe = $subscribeRepo->findByOpenId($openId); if ($subscribe) { - $subscribe->delete(); + $subscribe->deleted = 1; + $subscribe->update(); } return new TextMessage('伤心呀,我们又少了一个小伙伴!'); @@ -128,7 +129,9 @@ class WeChatOfficialAccount extends Service $userId = str_replace('qrscene_', '', $eventKey); if ($userId && $openId) { - $this->saveWechatSubscribe($userId, $openId); + $userInfo = $this->getUserInfo($openId); + $unionId = $userInfo['unionid'] ?: ''; + $this->saveWechatSubscribe($userId, $openId, $unionId); } return $this->emptyReply(); @@ -194,7 +197,7 @@ class WeChatOfficialAccount extends Service return new TextMessage('没有匹配的服务哦!'); } - protected function saveWechatSubscribe($userId, $openId) + protected function saveWechatSubscribe($userId, $openId, $unionId = '') { if (!$userId || !$openId) return; @@ -211,14 +214,35 @@ class WeChatOfficialAccount extends Service if ($subscribe) { if ($subscribe->user_id != $userId) { $subscribe->user_id = $userId; - $subscribe->update(); } + if (empty($subscribe->union_id) && !empty($unionId)) { + $subscribe->union_id = $unionId; + } + if ($subscribe->deleted == 1) { + $subscribe->deleted = 0; + } + $subscribe->update(); } else { $subscribe = new WeChatSubscribeModel(); $subscribe->user_id = $userId; $subscribe->open_id = $openId; + $subscribe->union_id = $unionId; $subscribe->create(); } } + protected function getUserInfo($openId) + { + $app = $this->getOfficialAccount(); + + return $app->user->get($openId); + } + + protected function getWechatLogger() + { + $service = new WeChatService(); + + return $service->logger; + } + } diff --git a/app/Repos/WeChatSubscribe.php b/app/Repos/WeChatSubscribe.php index f00ce785..bec577d5 100644 --- a/app/Repos/WeChatSubscribe.php +++ b/app/Repos/WeChatSubscribe.php @@ -62,4 +62,16 @@ class WeChatSubscribe extends Repository ]); } + /** + * @param string $unionId + * @return WeChatSubscribeModel|Model|bool + */ + public function findByUnionId($unionId) + { + return WeChatSubscribeModel::findFirst([ + 'conditions' => 'union_id = :union_id:', + 'bind' => ['union_id' => $unionId], + ]); + } + } diff --git a/app/Services/OAuth/WeChat.php b/app/Services/OAuth/WeChat.php index 29e541a3..98a1390e 100644 --- a/app/Services/OAuth/WeChat.php +++ b/app/Services/OAuth/WeChat.php @@ -1,4 +1,9 @@ table('kg_connect') - ->addColumn('deleted', 'integer', [ + $table = $this->table('kg_connect'); + + if (!$table->hasColumn('deleted')) { + $table->addColumn('deleted', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '删除标识', 'after' => 'provider', - ]) - ->save(); + ]); + } + if (!$table->hasIndexByName('user_id')) { + $table->addIndex(['user_id'], [ + 'name' => 'user_id', + 'unique' => false, + ]); + } + if (!$table->hasIndexByName('open_id')) { + $table->addIndex(['open_id'], [ + 'name' => 'open_id', + 'unique' => false, + ]); + } + $table->save(); } protected function alterWechatSubscribeTable() { - $this->table('kg_wechat_subscribe') - ->addColumn('union_id', 'string', [ + $table = $this->table('kg_wechat_subscribe'); + + if (!$table->hasColumn('union_id')) { + $table->addColumn('union_id', 'string', [ 'null' => false, 'default' => '', 'limit' => 64, @@ -41,16 +58,37 @@ class V20210917093354 extends Phinx\Migration\AbstractMigration 'encoding' => 'utf8mb4', 'comment' => '联合ID', 'after' => 'open_id', - ]) - ->addColumn('deleted', 'integer', [ + ]); + } + if (!$table->hasColumn('deleted')) { + $table->addColumn('deleted', 'integer', [ 'null' => false, 'default' => '0', 'limit' => MysqlAdapter::INT_REGULAR, 'signed' => false, 'comment' => '删除标识', - 'after' => 'open_id', - ]) - ->save(); + 'after' => 'union_id', + ]); + } + if (!$table->hasIndexByName('user_id')) { + $table->addIndex(['user_id'], [ + 'name' => 'user_id', + 'unique' => false, + ]); + } + if (!$table->hasIndexByName('open_id')) { + $table->addIndex(['open_id'], [ + 'name' => 'open_id', + 'unique' => false, + ]); + } + if (!$table->hasIndexByName('union_id')) { + $table->addIndex(['union_id'], [ + 'name' => 'union_id', + 'unique' => false, + ]); + } + $table->save(); } } From c7c9ab4f59ec238e5382ba1e1ba59b1199a7dba8 Mon Sep 17 00:00:00 2001 From: koogua Date: Wed, 22 Sep 2021 21:48:49 +0800 Subject: [PATCH 04/21] v1.4.5 beta3 --- CHANGELOG.md | 9 +++++++ app/Console/Tasks/DeliverTask.php | 4 +-- app/Console/Tasks/NoticeTask.php | 2 +- app/Http/Admin/Services/AuthNode.php | 2 +- app/Http/Admin/Views/setting/oauth.volt | 2 +- app/Http/Admin/Views/setting/oauth_local.volt | 12 ++++----- .../Home/Controllers/AccountController.php | 5 ++++ app/Http/Home/Views/account/login.volt | 3 --- .../Home/Views/account/login_by_password.volt | 20 +++----------- app/Http/Home/Views/account/register.volt | 23 +++++++++++++--- app/Http/Home/Views/article/show.volt | 6 ++--- app/Http/Home/Views/chapter/live/active.volt | 6 ++--- app/Http/Home/Views/chapter/read.volt | 6 ++--- app/Http/Home/Views/chapter/vod.volt | 6 ++--- app/Http/Home/Views/course/show.volt | 6 ++--- app/Http/Home/Views/order/info.volt | 13 +++++---- app/Http/Home/Views/question/show.volt | 6 ++--- app/Http/Home/Views/user/show.volt | 6 ++--- app/Services/Logic/Account/OAuthProvider.php | 4 +-- app/Services/Logic/Order/OrderInfo.php | 2 ++ config/errors.php | 1 + db/migrations/20210917093354.php | 19 +++++++++++++ public/static/home/js/article.share.js | 6 ++--- public/static/home/js/course.share.js | 6 ++--- public/static/home/js/order.info.js | 27 +++++++++++++++++++ public/static/home/js/question.share.js | 6 ++--- public/static/home/js/user.share.js | 6 ++--- public/static/lib/layui/extends/helper.js | 9 ------- scheduler.php | 16 +++++------ 29 files changed, 146 insertions(+), 93 deletions(-) create mode 100644 public/static/home/js/order.info.js diff --git a/CHANGELOG.md b/CHANGELOG.md index a884b950..9914dcf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +### [v1.4.5](https://gitee.com/koogua/course-tencent-cloud/releases/v1.4.5)(2021-09-27) + +- 修正点击内容分享到微信会额外出现公众号二维码的问题 +- 调整登录限制(邮箱|手机)为注册限制 +- 调整订单发货为每一分钟执行一次 +- 增加微信公众号支付处理 +- 增加取消订单功能 +- 优化计划任务 + ### [v1.4.4](https://gitee.com/koogua/course-tencent-cloud/releases/v1.4.4)(2021-09-17) - 后台增加邮件手机登录选择配置 diff --git a/app/Console/Tasks/DeliverTask.php b/app/Console/Tasks/DeliverTask.php index fd868471..dd741203 100644 --- a/app/Console/Tasks/DeliverTask.php +++ b/app/Console/Tasks/DeliverTask.php @@ -293,11 +293,11 @@ class DeliverTask extends Task * @param int $limit * @return ResultsetInterface|Resultset|TaskModel[] */ - protected function findTasks($limit = 30) + protected function findTasks($limit = 100) { $itemType = TaskModel::TYPE_DELIVER; $status = TaskModel::STATUS_PENDING; - $createTime = strtotime('-3 days'); + $createTime = strtotime('-1 days'); return TaskModel::query() ->where('item_type = :item_type:', ['item_type' => $itemType]) diff --git a/app/Console/Tasks/NoticeTask.php b/app/Console/Tasks/NoticeTask.php index bfe774d5..1f8eac73 100644 --- a/app/Console/Tasks/NoticeTask.php +++ b/app/Console/Tasks/NoticeTask.php @@ -84,7 +84,7 @@ class NoticeTask extends Task $task->update(); - $logger->info('Notice Process Exception ' . kg_json_encode([ + $logger->error('Notice Process Exception ' . kg_json_encode([ 'file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $e->getMessage(), diff --git a/app/Http/Admin/Services/AuthNode.php b/app/Http/Admin/Services/AuthNode.php index 2ff1ef67..69450293 100644 --- a/app/Http/Admin/Services/AuthNode.php +++ b/app/Http/Admin/Services/AuthNode.php @@ -1205,7 +1205,7 @@ class AuthNode extends Service ], [ 'id' => '5-1-12', - 'title' => '登录设置', + 'title' => '注册登录', 'type' => 'menu', 'route' => 'admin.setting.oauth', ], diff --git a/app/Http/Admin/Views/setting/oauth.volt b/app/Http/Admin/Views/setting/oauth.volt index a7116e16..22b7627b 100644 --- a/app/Http/Admin/Views/setting/oauth.volt +++ b/app/Http/Admin/Views/setting/oauth.volt @@ -4,7 +4,7 @@
    -
  • 本地登录
  • +
  • 注册设置
  • QQ登录
  • 微信登录
  • 微博登录
  • diff --git a/app/Http/Admin/Views/setting/oauth_local.volt b/app/Http/Admin/Views/setting/oauth_local.volt index f258e782..28cf337d 100644 --- a/app/Http/Admin/Views/setting/oauth_local.volt +++ b/app/Http/Admin/Views/setting/oauth_local.volt @@ -1,16 +1,16 @@
    - +
    - - + +
    - +
    - - + +
    diff --git a/app/Http/Home/Controllers/AccountController.php b/app/Http/Home/Controllers/AccountController.php index a7a18363..a7f86564 100644 --- a/app/Http/Home/Controllers/AccountController.php +++ b/app/Http/Home/Controllers/AccountController.php @@ -28,6 +28,10 @@ class AccountController extends Controller $returnUrl = $this->request->getHTTPReferer(); + $service = new OAuthProviderService(); + + $oauthProvider = $service->handle(); + $service = new AccountService(); $captcha = $service->getSettings('captcha'); @@ -35,6 +39,7 @@ class AccountController extends Controller $this->seo->prependTitle('注册'); $this->view->setVar('return_url', $returnUrl); + $this->view->setVar('local_oauth', $oauthProvider['local']); $this->view->setVar('captcha', $captcha); } diff --git a/app/Http/Home/Views/account/login.volt b/app/Http/Home/Views/account/login.volt index 59254fef..34641c28 100644 --- a/app/Http/Home/Views/account/login.volt +++ b/app/Http/Home/Views/account/login.volt @@ -2,9 +2,6 @@ {% block content %} - {% set login_with_phone = oauth_provider.local.login_with_phone == 1 %} - {% set login_with_email = oauth_provider.local.login_with_email == 1 %} - diff --git a/app/Http/Home/Views/order/info.volt b/app/Http/Home/Views/order/info.volt index 4f5cdf9b..40878719 100644 --- a/app/Http/Home/Views/order/info.volt +++ b/app/Http/Home/Views/order/info.volt @@ -4,6 +4,7 @@ {{ partial('macros/order') }} + {% set order_cancel_url = url({'for':'home.order.cancel'}) %} {% set order_pay_url = url({'for':'home.order.pay'},{'sn':order.sn}) %} {% set refund_confirm_url = url({'for':'home.refund.confirm'},{'sn':order.sn}) %} @@ -24,6 +25,9 @@ {% if order.me.allow_pay == 1 %} 立即支付 {% endif %} + {% if order.me.allow_cancel == 1 %} + 立即取消 + {% endif %} {% if order.me.allow_refund == 1 %} 申请退款 {% endif %} @@ -31,13 +35,8 @@ {% endblock %} -{% block inline_js %} +{% block include_js %} - + {{ js_include('home/js/order.info.js') }} {% endblock %} \ No newline at end of file diff --git a/app/Http/Home/Views/question/show.volt b/app/Http/Home/Views/question/show.volt index 91dca280..edd6de95 100644 --- a/app/Http/Home/Views/question/show.volt +++ b/app/Http/Home/Views/question/show.volt @@ -22,9 +22,9 @@ 详情
    diff --git a/app/Http/Home/Views/user/show.volt b/app/Http/Home/Views/user/show.volt index ebebe70c..f39aa5ff 100644 --- a/app/Http/Home/Views/user/show.volt +++ b/app/Http/Home/Views/user/show.volt @@ -14,9 +14,9 @@
diff --git a/app/Services/Logic/Account/OAuthProvider.php b/app/Services/Logic/Account/OAuthProvider.php index 20b25662..6b63763d 100644 --- a/app/Services/Logic/Account/OAuthProvider.php +++ b/app/Services/Logic/Account/OAuthProvider.php @@ -21,8 +21,8 @@ class OAuthProvider extends LogicService return [ 'local' => [ - 'login_with_phone' => $local['login_with_phone'], - 'login_with_email' => $local['login_with_email'], + 'register_with_phone' => $local['register_with_phone'], + 'register_with_email' => $local['register_with_email'], ], 'weixin' => ['enabled' => $weixin['enabled']], 'weibo' => ['enabled' => $weibo['enabled']], diff --git a/app/Services/Logic/Order/OrderInfo.php b/app/Services/Logic/Order/OrderInfo.php index e8ee03f1..9bc159e9 100644 --- a/app/Services/Logic/Order/OrderInfo.php +++ b/app/Services/Logic/Order/OrderInfo.php @@ -75,11 +75,13 @@ class OrderInfo extends LogicService { $result = [ 'allow_pay' => 0, + 'allow_cancel' => 0, 'allow_refund' => 0, ]; if ($order->status == OrderModel::STATUS_PENDING) { $result['allow_pay'] = 1; + $result['allow_cancel'] = 1; } if ($order->status == OrderModel::STATUS_FINISHED) { diff --git a/config/errors.php b/config/errors.php index 7b48669d..6b283efb 100644 --- a/config/errors.php +++ b/config/errors.php @@ -369,6 +369,7 @@ $error['order.item_not_found'] = '商品不存在'; $error['order.trade_expired'] = '交易已过期'; $error['order.has_bought_course'] = '已经购买过该课程'; $error['order.has_bought_package'] = '已经购买过该套餐'; +$error['order.cancel_not_allowed'] = '当前不允许取消订单'; $error['order.close_not_allowed'] = '当前不允许关闭订单'; $error['order.refund_not_allowed'] = '当前不允许申请退款'; $error['order.refund_item_unsupported'] = '该品类不支持退款'; diff --git a/db/migrations/20210917093354.php b/db/migrations/20210917093354.php index fd513a96..08a64015 100644 --- a/db/migrations/20210917093354.php +++ b/db/migrations/20210917093354.php @@ -14,6 +14,7 @@ class V20210917093354 extends Phinx\Migration\AbstractMigration { $this->alterConnectTable(); $this->alterWechatSubscribeTable(); + $this->handleLocalAuthSetting(); } protected function alterConnectTable() @@ -91,4 +92,22 @@ class V20210917093354 extends Phinx\Migration\AbstractMigration $table->save(); } + protected function handleLocalAuthSetting() + { + $rows = [ + [ + 'section' => 'oauth.local', + 'item_key' => 'register_with_phone', + 'item_value' => '1', + ], + [ + 'section' => 'oauth.local', + 'item_key' => 'register_with_email', + 'item_value' => '1', + ] + ]; + + $this->table('kg_setting')->insert($rows)->save(); + } + } diff --git a/public/static/home/js/article.share.js b/public/static/home/js/article.share.js index 8fc93236..1845f6ad 100644 --- a/public/static/home/js/article.share.js +++ b/public/static/home/js/article.share.js @@ -10,16 +10,16 @@ layui.use(['jquery', 'helper'], function () { qrcode: $('input[name="share.qrcode"]').val() }; - $('.icon-wechat').on('click', function () { + $('.share-wechat').on('click', function () { helper.wechatShare(myShare.qrcode); }); - $('.icon-qq').on('click', function () { + $('.share-qq').on('click', function () { var title = '推荐一篇好文章:' + myShare.title + ',快来和我一起学习吧!'; helper.qqShare(title, myShare.url, myShare.pic); }); - $('.icon-weibo').on('click', function () { + $('.share-weibo').on('click', function () { var title = '推荐一篇好文章:' + myShare.title + ',快来和我一起学习吧!'; helper.weiboShare(title, myShare.url, myShare.pic); }); diff --git a/public/static/home/js/course.share.js b/public/static/home/js/course.share.js index 3ca96c2b..a2e69bfa 100644 --- a/public/static/home/js/course.share.js +++ b/public/static/home/js/course.share.js @@ -10,16 +10,16 @@ layui.use(['jquery', 'helper'], function () { qrcode: $('input[name="share.qrcode"]').val() }; - $('.icon-wechat').on('click', function () { + $('.share-wechat').on('click', function () { helper.wechatShare(myShare.qrcode); }); - $('.icon-qq').on('click', function () { + $('.share-qq').on('click', function () { var title = '推荐一门好课:' + myShare.title + ',快来和我一起学习吧!'; helper.qqShare(title, myShare.url, myShare.pic); }); - $('.icon-weibo').on('click', function () { + $('.share-weibo').on('click', function () { var title = '推荐一门好课:' + myShare.title + ',快来和我一起学习吧!'; helper.weiboShare(title, myShare.url, myShare.pic); }); diff --git a/public/static/home/js/order.info.js b/public/static/home/js/order.info.js new file mode 100644 index 00000000..8dfb9e4e --- /dev/null +++ b/public/static/home/js/order.info.js @@ -0,0 +1,27 @@ +layui.use(['jquery', 'layer', 'helper'], function () { + + var $ = layui.jquery; + var layer = layui.layer; + + var index = parent.layer.getFrameIndex(window.name); + + parent.layer.iframeAuto(index); + + $('.order-cancel').on('click', function () { + var url = $(this).data('url'); + var data = {sn: $(this).data('sn')}; + layer.confirm('确定要取消订单吗?', function () { + $.ajax({ + type: 'POST', + url: url, + data: data, + success: function () { + layer.msg('取消订单成功', {icon: 1}); + parent.layer.close(index); + top.location.href = '/uc/orders'; + } + }); + }); + }); + +}); \ No newline at end of file diff --git a/public/static/home/js/question.share.js b/public/static/home/js/question.share.js index 5096f92e..83ed9580 100644 --- a/public/static/home/js/question.share.js +++ b/public/static/home/js/question.share.js @@ -10,16 +10,16 @@ layui.use(['jquery', 'helper'], function () { qrcode: $('input[name="share.qrcode"]').val() }; - $('.icon-wechat').on('click', function () { + $('.share-wechat').on('click', function () { helper.wechatShare(myShare.qrcode); }); - $('.icon-qq').on('click', function () { + $('.share-qq').on('click', function () { var title = '分享一个好问题:' + myShare.title + ',快来和我一起学习吧!'; helper.qqShare(title, myShare.url, myShare.pic); }); - $('.icon-weibo').on('click', function () { + $('.share-weibo').on('click', function () { var title = '分享一个好问题:' + myShare.title + ',快来和我一起学习吧!'; helper.weiboShare(title, myShare.url, myShare.pic); }); diff --git a/public/static/home/js/user.share.js b/public/static/home/js/user.share.js index e44e90e3..281bd947 100644 --- a/public/static/home/js/user.share.js +++ b/public/static/home/js/user.share.js @@ -10,16 +10,16 @@ layui.use(['jquery', 'helper'], function () { qrcode: $('input[name="share.qrcode"]').val() }; - $('.icon-wechat').on('click', function () { + $('.share-wechat').on('click', function () { helper.wechatShare(myShare.qrcode); }); - $('.icon-qq').on('click', function () { + $('.share-qq').on('click', function () { var title = '推荐一个有趣的朋友:' + myShare.title + ',快来和Ta一起学习吧!'; helper.qqShare(title, myShare.url, myShare.pic); }); - $('.icon-weibo').on('click', function () { + $('.share-weibo').on('click', function () { var title = '推荐一个有趣的朋友:' + myShare.title + ',快来和Ta一起学习吧!'; helper.weiboShare(title, myShare.url, myShare.pic); }); diff --git a/public/static/lib/layui/extends/helper.js b/public/static/lib/layui/extends/helper.js index 2b475f15..2c3c686b 100644 --- a/public/static/lib/layui/extends/helper.js +++ b/public/static/lib/layui/extends/helper.js @@ -29,15 +29,6 @@ layui.define(['jquery', 'layer'], function (exports) { callback(); }; - helper.cs = function () { - layer.open({ - type: 2, - title: '在线客服', - area: ['600px', '570px'], - content: ['/im/cs', 'no'] - }); - }; - helper.wechatShare = function (qrcode) { var content = '
分享到微信
'; layer.open({ diff --git a/scheduler.php b/scheduler.php index cf8d5ffe..70217971 100644 --- a/scheduler.php +++ b/scheduler.php @@ -16,28 +16,28 @@ $script = __DIR__ . '/console.php'; $bin = '/usr/local/bin/php'; $scheduler->php($script, $bin, ['--task' => 'deliver', '--action' => 'main']) - ->at('*/3 * * * *'); + ->everyMinute(); $scheduler->php($script, $bin, ['--task' => 'vod_event', '--action' => 'main']) - ->at('*/5 * * * *'); + ->everyMinute(5); $scheduler->php($script, $bin, ['--task' => 'sync_learning', '--action' => 'main']) - ->at('*/7 * * * *'); + ->everyMinute(7); $scheduler->php($script, $bin, ['--task' => 'teacher_live_notice', '--action' => 'consume']) - ->at('*/10 * * * *'); + ->everyMinute(9); $scheduler->php($script, $bin, ['--task' => 'point_gift_deliver', '--action' => 'main']) - ->at('*/11 * * * *'); + ->everyMinute(11); $scheduler->php($script, $bin, ['--task' => 'server_monitor', '--action' => 'main']) - ->at('*/12 * * * *'); + ->everyMinute(12); $scheduler->php($script, $bin, ['--task' => 'close_trade', '--action' => 'main']) - ->at('*/13 * * * *'); + ->everyMinute(13); $scheduler->php($script, $bin, ['--task' => 'close_flash_sale_order', '--action' => 'main']) - ->at('*/15 * * * *'); + ->everyMinute(15); $scheduler->php($script, $bin, ['--task' => 'notice', '--action' => 'main']) ->everyMinute(); From 5617a5f1c1362228f5491759835a79d147224fe5 Mon Sep 17 00:00:00 2001 From: koogua Date: Sun, 26 Sep 2021 12:25:38 +0800 Subject: [PATCH 05/21] v1.4.5 beta4 --- CHANGELOG.md | 2 + app/Caches/IndexArticleList.php | 2 +- app/Caches/IndexFlashSaleList.php | 36 ++++++++++ app/Caches/IndexQuestionList.php | 2 +- app/Caches/IndexTeacherList.php | 68 ++++++++++++++++++ app/Caches/SaleTrend.php | 70 ------------------- app/Caches/SiteGlobalStat.php | 6 ++ .../Admin/Views/index/main_global_stat.volt | 4 +- app/Http/Api/Controllers/IndexController.php | 26 +++++++ app/Validators/UserLimit.php | 2 +- 10 files changed, 143 insertions(+), 75 deletions(-) create mode 100644 app/Caches/IndexFlashSaleList.php create mode 100644 app/Caches/IndexTeacherList.php delete mode 100644 app/Caches/SaleTrend.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9914dcf3..3fe34773 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ ### [v1.4.5](https://gitee.com/koogua/course-tencent-cloud/releases/v1.4.5)(2021-09-27) - 修正点击内容分享到微信会额外出现公众号二维码的问题 +- 修正后台首页提问和回答的数量统计 - 调整登录限制(邮箱|手机)为注册限制 - 调整订单发货为每一分钟执行一次 +- 增加首页推荐教师接口 - 增加微信公众号支付处理 - 增加取消订单功能 - 优化计划任务 diff --git a/app/Caches/IndexArticleList.php b/app/Caches/IndexArticleList.php index 0348b0a4..0f03cc0f 100644 --- a/app/Caches/IndexArticleList.php +++ b/app/Caches/IndexArticleList.php @@ -14,7 +14,7 @@ use App\Services\Logic\Article\ArticleList as ArticleListService; class IndexArticleList extends Cache { - protected $lifetime = 1 * 86400; + protected $lifetime = 15 * 60; public function getLifetime() { diff --git a/app/Caches/IndexFlashSaleList.php b/app/Caches/IndexFlashSaleList.php new file mode 100644 index 00000000..587da335 --- /dev/null +++ b/app/Caches/IndexFlashSaleList.php @@ -0,0 +1,36 @@ +handle(); + + return $sales[0]['items'] ?? []; + } + +} diff --git a/app/Caches/IndexQuestionList.php b/app/Caches/IndexQuestionList.php index db028c91..9c301b1a 100644 --- a/app/Caches/IndexQuestionList.php +++ b/app/Caches/IndexQuestionList.php @@ -14,7 +14,7 @@ use App\Services\Logic\Question\QuestionList as QuestionListService; class IndexQuestionList extends Cache { - protected $lifetime = 1 * 86400; + protected $lifetime = 15 * 60; public function getLifetime() { diff --git a/app/Caches/IndexTeacherList.php b/app/Caches/IndexTeacherList.php new file mode 100644 index 00000000..2decb09d --- /dev/null +++ b/app/Caches/IndexTeacherList.php @@ -0,0 +1,68 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_teacher_list'; + } + + public function getContent($id = null) + { + $teachers = $this->findTeachers(); + + if ($teachers->count() == 0) return []; + + $result = []; + + $baseUrl = kg_cos_url(); + + foreach ($teachers->toArray() as $teacher) { + + $teacher['avatar'] = $baseUrl . $teacher['avatar']; + + $result[] = [ + 'id' => $teacher['id'], + 'name' => $teacher['name'], + 'title' => $teacher['title'], + 'avatar' => $teacher['avatar'], + 'about' => $teacher['about'], + ]; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|UserModel[] + */ + protected function findTeachers($limit = 8) + { + return UserModel::query() + ->where('edu_role = :edu_role:', ['edu_role' => UserModel::EDU_ROLE_TEACHER]) + ->orderBy('RAND()') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/SaleTrend.php b/app/Caches/SaleTrend.php deleted file mode 100644 index c5c7454d..00000000 --- a/app/Caches/SaleTrend.php +++ /dev/null @@ -1,70 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return 'sale_trend'; - } - - public function getContent($id = null) - { - - } - - /** - * @param OrderModel[] $sales - * @param int $days - * @return array - */ - protected function handleSales($sales, $days = 7) - { - $result = []; - - foreach (array_reverse(range(1, $days)) as $num) { - $date = date('Y-m-d', strtotime("-{$num} days")); - $result[$date] = 0; - } - - foreach ($sales as $sale) { - $date = date('Y-m-d', $sale->create_time); - $result[$date] += $sale->amount; - } - - return $result; - } - - /** - * @param int $days - * @return ResultsetInterface|Resultset|OrderModel[] - */ - protected function findSales($days = 7) - { - $time = strtotime("-{$days} days"); - - return OrderModel::query() - ->where('status = :status:', ['status' => OrderModel::STATUS_FINISHED]) - ->andWhere('create_time > :time:', ['time' => $time]) - ->execute(); - } - -} diff --git a/app/Caches/SiteGlobalStat.php b/app/Caches/SiteGlobalStat.php index 99cb45d3..2e93c8a4 100644 --- a/app/Caches/SiteGlobalStat.php +++ b/app/Caches/SiteGlobalStat.php @@ -7,12 +7,14 @@ namespace App\Caches; +use App\Repos\Answer as AnswerRepo; use App\Repos\Article as ArticleRepo; use App\Repos\Comment as CommentRepo; use App\Repos\Consult as ConsultRepo; use App\Repos\Course as CourseRepo; use App\Repos\ImGroup as GroupRepo; use App\Repos\Package as PackageRepo; +use App\Repos\Question as QuestionRepo; use App\Repos\Review as ReviewRepo; use App\Repos\Topic as TopicRepo; use App\Repos\User as UserRepo; @@ -36,6 +38,8 @@ class SiteGlobalStat extends Cache { $courseRepo = new CourseRepo(); $articleRepo = new ArticleRepo(); + $questionRepo = new QuestionRepo(); + $answerRepo = new AnswerRepo(); $commentRepo = new CommentRepo(); $consultRepo = new ConsultRepo(); $groupRepo = new GroupRepo(); @@ -47,6 +51,8 @@ class SiteGlobalStat extends Cache return [ 'course_count' => $courseRepo->countCourses(), 'article_count' => $articleRepo->countArticles(), + 'question_count' => $questionRepo->countQuestions(), + 'answer_count' => $answerRepo->countAnswers(), 'comment_count' => $commentRepo->countComments(), 'consult_count' => $consultRepo->countConsults(), 'group_count' => $groupRepo->countGroups(), diff --git a/app/Http/Admin/Views/index/main_global_stat.volt b/app/Http/Admin/Views/index/main_global_stat.volt index f4198c5a..1e9738d2 100644 --- a/app/Http/Admin/Views/index/main_global_stat.volt +++ b/app/Http/Admin/Views/index/main_global_stat.volt @@ -59,13 +59,13 @@
提问数
-
0
+
{{ global_stat.question_count }}
回答数
-
0
+
{{ global_stat.answer_count }}
diff --git a/app/Http/Api/Controllers/IndexController.php b/app/Http/Api/Controllers/IndexController.php index eb9557d9..d6efa30c 100644 --- a/app/Http/Api/Controllers/IndexController.php +++ b/app/Http/Api/Controllers/IndexController.php @@ -8,6 +8,7 @@ namespace App\Http\Api\Controllers; use App\Caches\IndexArticleList; +use App\Caches\IndexFlashSaleList; use App\Caches\IndexLiveList; use App\Caches\IndexQuestionList; use App\Caches\IndexSimpleFeaturedCourseList; @@ -15,6 +16,7 @@ use App\Caches\IndexSimpleFreeCourseList; use App\Caches\IndexSimpleNewCourseList; use App\Caches\IndexSimpleVipCourseList; use App\Caches\IndexSlideList; +use App\Caches\IndexTeacherList; /** * @RoutePrefix("/api/index") @@ -70,6 +72,30 @@ class IndexController extends Controller return $this->jsonSuccess(['lives' => $lives]); } + /** + * @Get("/teachers", name="api.index.teachers") + */ + public function teachersAction() + { + $cache = new IndexTeacherList(); + + $teachers = $cache->get(); + + return $this->jsonSuccess(['teachers' => $teachers]); + } + + /** + * @Get("/flash/sales", name="api.index.flash_sales") + */ + public function flashSalesAction() + { + $cache = new IndexFlashSaleList(); + + $sales = $cache->get(); + + return $this->jsonSuccess(['sales' => $sales]); + } + /** * @Get("/courses/featured", name="api.index.featured_courses") */ diff --git a/app/Validators/UserLimit.php b/app/Validators/UserLimit.php index 854b7294..95fbcc9e 100644 --- a/app/Validators/UserLimit.php +++ b/app/Validators/UserLimit.php @@ -111,7 +111,7 @@ class UserLimit extends Validator { $count = $this->counter->hGet($user->id, 'order_count'); - if ($count > 10) { + if ($count > 50) { throw new BadRequestException('user_limit.reach_daily_order_limit'); } } From 53295ed9809ac9c51d727842c371f268b5849c4a Mon Sep 17 00:00:00 2001 From: koogua Date: Tue, 28 Sep 2021 15:07:34 +0800 Subject: [PATCH 06/21] =?UTF-8?q?=EF=BC=91=EF=BC=8E=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=20?= =?UTF-8?q?=EF=BC=92=EF=BC=8E=E6=97=A0=E6=9D=83=E9=99=90=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E8=AF=BE=E6=97=B6=E4=B8=8D=E8=BF=94=E5=9B=9E=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 + app/Builders/OrderList.php | 51 ++++++++++++++++++- app/Services/Logic/Chapter/ChapterInfo.php | 13 +++++ app/Services/Logic/Order/OrderInfo.php | 18 +++++-- app/Services/Logic/User/Console/OrderList.php | 5 +- 5 files changed, 81 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fe34773..e66998d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,11 @@ - 修正后台首页提问和回答的数量统计 - 调整登录限制(邮箱|手机)为注册限制 - 调整订单发货为每一分钟执行一次 +- 增强课时安全性,无权限时不返回播放地址或内容 - 增加首页推荐教师接口 - 增加微信公众号支付处理 - 增加取消订单功能 +- 优化订单API结构 - 优化计划任务 ### [v1.4.4](https://gitee.com/koogua/course-tencent-cloud/releases/v1.4.4)(2021-09-17) diff --git a/app/Builders/OrderList.php b/app/Builders/OrderList.php index d46f0ae6..cfcdae2d 100644 --- a/app/Builders/OrderList.php +++ b/app/Builders/OrderList.php @@ -7,6 +7,7 @@ namespace App\Builders; +use App\Models\Course as CourseModel; use App\Models\Order as OrderModel; use App\Repos\User as UserRepo; @@ -42,7 +43,7 @@ class OrderList extends Builder public function handleItems(array $orders) { foreach ($orders as $key => $order) { - $itemInfo = $this->handleItem($order); + $itemInfo = $this->handleItemInfo($order); $orders[$key]['item_info'] = $itemInfo; } @@ -53,7 +54,7 @@ class OrderList extends Builder * @param array $order * @return array|mixed */ - public function handleItem(array $order) + public function handleItemInfo(array $order) { $itemInfo = []; @@ -72,6 +73,52 @@ class OrderList extends Builder return $itemInfo; } + /** + * @param array $order + * @return array|mixed + */ + public function handleMeInfo(array $order) + { + $me = [ + 'allow_pay' => 0, + 'allow_cancel' => 0, + 'allow_refund' => 0, + ]; + + $payStatusOk = $order['status'] == OrderModel::STATUS_PENDING ? 1 : 0; + $cancelStatusOk = $order['status'] == OrderModel::STATUS_PENDING ? 1 : 0; + $refundStatusOk = $order['status'] == OrderModel::STATUS_FINISHED ? 1 : 0; + + if ($order['item_type'] == OrderModel::ITEM_COURSE) { + + $course = $order['item_info']['course']; + + $courseModelOk = $course['model'] != CourseModel::MODEL_OFFLINE; + $refundTimeOk = $course['refund_expiry_time'] > time(); + + $me['allow_refund'] = $courseModelOk && $refundStatusOk && $refundTimeOk ? 1 : 0; + + } elseif ($order['item_type'] == OrderModel::ITEM_PACKAGE) { + + $courses = $order['item_info']['courses']; + + $refundTimeOk = false; + + foreach ($courses as $course) { + if ($course['refund_expiry_time'] > time()) { + $refundTimeOk = true; + } + } + + $me['allow_refund'] = $refundStatusOk && $refundTimeOk ? 1 : 0; + } + + $me['allow_pay'] = $payStatusOk; + $me['allow_cancel'] = $cancelStatusOk; + + return $me; + } + /** * @param string $itemInfo * @return mixed diff --git a/app/Services/Logic/Chapter/ChapterInfo.php b/app/Services/Logic/Chapter/ChapterInfo.php index 16ddc456..345e8fb5 100644 --- a/app/Services/Logic/Chapter/ChapterInfo.php +++ b/app/Services/Logic/Chapter/ChapterInfo.php @@ -64,6 +64,19 @@ class ChapterInfo extends LogicService $result = $service->handleBasicInfo($chapter); + /** + * 无内容查看权限,过滤掉相关内容 + */ + if (!$this->ownedChapter) { + if ($chapter->model == CourseModel::MODEL_VOD) { + $result['play_urls'] = []; + } elseif ($chapter->model == CourseModel::MODEL_LIVE) { + $result['play_urls'] = []; + } elseif ($chapter->model == CourseModel::MODEL_READ) { + $result['content'] = ''; + } + } + $result['course'] = $service->handleCourseInfo($this->course); $me = [ diff --git a/app/Services/Logic/Order/OrderInfo.php b/app/Services/Logic/Order/OrderInfo.php index 9bc159e9..6e63de78 100644 --- a/app/Services/Logic/Order/OrderInfo.php +++ b/app/Services/Logic/Order/OrderInfo.php @@ -86,16 +86,24 @@ class OrderInfo extends LogicService if ($order->status == OrderModel::STATUS_FINISHED) { /** - * 只允许线上课程退款,因为线下课程无法进行退款计算 + * 只允许线上课程退款 */ if ($order->item_type == OrderModel::ITEM_COURSE) { - $result['allow_refund'] = 1; $course = $order->item_info['course']; - if (isset($course['model']) && $course['model'] == CourseModel::MODEL_OFFLINE) { - $result['allow_refund'] = 0; + $refundTimeOk = $course['refund_expiry_time'] > time(); + $courseModelOk = $course['model'] != CourseModel::MODEL_OFFLINE; + if ($refundTimeOk && $courseModelOk) { + $result['allow_refund'] = 1; } } elseif ($order->item_type == OrderModel::ITEM_PACKAGE) { - $result['allow_refund'] = $order->status == OrderModel::STATUS_FINISHED ? 1 : 0; + $courses = $order->item_info['courses']; + foreach ($courses as $course) { + $refundTimeOk = $course['refund_expiry_time'] > time(); + $courseModelOk = $course['model'] != CourseModel::MODEL_OFFLINE; + if ($refundTimeOk && $courseModelOk) { + $result['allow_refund'] = 1; + } + } } } diff --git a/app/Services/Logic/User/Console/OrderList.php b/app/Services/Logic/User/Console/OrderList.php index 74f19445..7ab24fa1 100644 --- a/app/Services/Logic/User/Console/OrderList.php +++ b/app/Services/Logic/User/Console/OrderList.php @@ -61,7 +61,9 @@ class OrderList extends LogicService foreach ($orders as $order) { - $order['item_info'] = $builder->handleItem($order); + $order['item_info'] = $builder->handleItemInfo($order); + + $me = $builder->handleMeInfo($order); $items[] = [ 'sn' => $order['sn'], @@ -76,6 +78,7 @@ class OrderList extends LogicService 'promotion_info' => $order['promotion_info'], 'create_time' => $order['create_time'], 'update_time' => $order['update_time'], + 'me' => $me, ]; } From 124b28856f700c8c5f438c657c33b5191229c9f7 Mon Sep 17 00:00:00 2001 From: koogua Date: Tue, 28 Sep 2021 17:33:52 +0800 Subject: [PATCH 07/21] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=92=8C=E6=8F=90=E9=97=AE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + app/Http/Api/Controllers/AnswerController.php | 2 +- app/Http/Api/Controllers/ArticleController.php | 13 +++++++++++++ app/Http/Api/Controllers/CommentController.php | 2 +- app/Http/Api/Controllers/QuestionController.php | 13 +++++++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e66998d5..e5c1c0ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - 调整登录限制(邮箱|手机)为注册限制 - 调整订单发货为每一分钟执行一次 - 增强课时安全性,无权限时不返回播放地址或内容 +- 增加删除文章和提问接口 - 增加首页推荐教师接口 - 增加微信公众号支付处理 - 增加取消订单功能 diff --git a/app/Http/Api/Controllers/AnswerController.php b/app/Http/Api/Controllers/AnswerController.php index 79c1c416..979f5827 100644 --- a/app/Http/Api/Controllers/AnswerController.php +++ b/app/Http/Api/Controllers/AnswerController.php @@ -82,7 +82,7 @@ class AnswerController extends Controller $service->handle($id); - return $this->jsonSuccess(['msg' => '删除回答成功']); + return $this->jsonSuccess(); } /** diff --git a/app/Http/Api/Controllers/ArticleController.php b/app/Http/Api/Controllers/ArticleController.php index fa636c57..10db6ded 100644 --- a/app/Http/Api/Controllers/ArticleController.php +++ b/app/Http/Api/Controllers/ArticleController.php @@ -7,6 +7,7 @@ namespace App\Http\Api\Controllers; +use App\Services\Logic\Article\ArticleDelete as ArticleDeleteService; use App\Services\Logic\Article\ArticleFavorite as ArticleFavoriteService; use App\Services\Logic\Article\ArticleInfo as ArticleInfoService; use App\Services\Logic\Article\ArticleLike as ArticleLikeService; @@ -68,6 +69,18 @@ class ArticleController extends Controller return $this->jsonPaginate($pager); } + /** + * @Post("/{id:[0-9]+}/delete", name="api.article.delete") + */ + public function deleteAction($id) + { + $service = new ArticleDeleteService(); + + $service->handle($id); + + return $this->jsonSuccess(); + } + /** * @Post("/{id:[0-9]+}/favorite", name="api.article.favorite") */ diff --git a/app/Http/Api/Controllers/CommentController.php b/app/Http/Api/Controllers/CommentController.php index 4e9031c2..55e8a9a2 100644 --- a/app/Http/Api/Controllers/CommentController.php +++ b/app/Http/Api/Controllers/CommentController.php @@ -85,7 +85,7 @@ class CommentController extends Controller $service->handle($id); - return $this->jsonSuccess(['msg' => '删除评论成功']); + return $this->jsonSuccess(); } /** diff --git a/app/Http/Api/Controllers/QuestionController.php b/app/Http/Api/Controllers/QuestionController.php index d76c82bb..3ba26c90 100644 --- a/app/Http/Api/Controllers/QuestionController.php +++ b/app/Http/Api/Controllers/QuestionController.php @@ -10,6 +10,7 @@ namespace App\Http\Api\Controllers; use App\Services\Logic\Question\AnswerList as AnswerListService; use App\Services\Logic\Question\CategoryList as CategoryListService; use App\Services\Logic\Question\CommentList as CommentListService; +use App\Services\Logic\Question\QuestionDelete as QuestionDeleteService; use App\Services\Logic\Question\QuestionFavorite as QuestionFavoriteService; use App\Services\Logic\Question\QuestionInfo as QuestionInfoService; use App\Services\Logic\Question\QuestionLike as QuestionLikeService; @@ -81,6 +82,18 @@ class QuestionController extends Controller return $this->jsonPaginate($pager); } + /** + * @Post("/{id:[0-9]+}/delete", name="api.question.delete") + */ + public function deleteAction($id) + { + $service = new QuestionDeleteService(); + + $service->handle($id); + + return $this->jsonSuccess(); + } + /** * @Post("/{id:[0-9]+}/favorite", name="api.question.favorite") */ From 5009ae36a0f2bb36ff42eae080ae2f1862df31a4 Mon Sep 17 00:00:00 2001 From: koogua Date: Tue, 28 Sep 2021 21:14:15 +0800 Subject: [PATCH 08/21] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=9F=A5=E8=AF=A2=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Logic/Article/CommentList.php | 1 + app/Services/Logic/Comment/ListTrait.php | 1 + app/Services/Logic/Question/AnswerList.php | 1 + 3 files changed, 3 insertions(+) diff --git a/app/Services/Logic/Article/CommentList.php b/app/Services/Logic/Article/CommentList.php index 266ad84b..be2cb373 100644 --- a/app/Services/Logic/Article/CommentList.php +++ b/app/Services/Logic/Article/CommentList.php @@ -31,6 +31,7 @@ class CommentList extends LogicService $params['item_id'] = $article->id; $params['item_type'] = CommentModel::ITEM_ARTICLE; $params['published'] = CommentModel::PUBLISH_APPROVED; + $params['deleted'] = 0; $sort = $pagerQuery->getSort(); $page = $pagerQuery->getPage(); diff --git a/app/Services/Logic/Comment/ListTrait.php b/app/Services/Logic/Comment/ListTrait.php index 89df1416..68534643 100644 --- a/app/Services/Logic/Comment/ListTrait.php +++ b/app/Services/Logic/Comment/ListTrait.php @@ -77,6 +77,7 @@ trait ListTrait foreach ($comments as $comment) { $result[$comment['id']] = [ 'liked' => in_array($comment['id'], $likedIds) ? 1 : 0, + 'owned' => $comment['owner_id'] == $user->id ? 1 : 0, ]; } diff --git a/app/Services/Logic/Question/AnswerList.php b/app/Services/Logic/Question/AnswerList.php index 8f5bff83..71a79a3d 100644 --- a/app/Services/Logic/Question/AnswerList.php +++ b/app/Services/Logic/Question/AnswerList.php @@ -108,6 +108,7 @@ class AnswerList extends LogicService foreach ($answers as $answer) { $result[$answer['id']] = [ 'liked' => in_array($answer['id'], $likedIds) ? 1 : 0, + 'owned' => $answer['owner_id'] == $user->id ? 1 : 0, ]; } From acc25bb5dc80af60cdca4beb47b0d1a90368999b Mon Sep 17 00:00:00 2001 From: koogua Date: Wed, 29 Sep 2021 21:01:01 +0800 Subject: [PATCH 09/21] =?UTF-8?q?=E6=8A=BD=E7=A6=BB=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E5=92=8C=E7=A7=81=E6=9C=89=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../Api/Controllers/ArticleController.php | 30 +++++++++++++++ .../Home/Controllers/ArticleController.php | 38 +++++++++++++++++++ app/Http/Home/Views/article/show.volt | 12 ++++++ app/Services/Logic/Article/ArticleClose.php | 36 ++++++++++++++++++ app/Services/Logic/Article/ArticlePrivate.php | 36 ++++++++++++++++++ public/static/home/js/article.show.js | 25 +++++++++++- 7 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 app/Services/Logic/Article/ArticleClose.php create mode 100644 app/Services/Logic/Article/ArticlePrivate.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e5c1c0ed..0cec2ad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - 调整登录限制(邮箱|手机)为注册限制 - 调整订单发货为每一分钟执行一次 - 增强课时安全性,无权限时不返回播放地址或内容 +- 抽离出文章关闭,仅我可见操作 - 增加删除文章和提问接口 - 增加首页推荐教师接口 - 增加微信公众号支付处理 diff --git a/app/Http/Api/Controllers/ArticleController.php b/app/Http/Api/Controllers/ArticleController.php index 10db6ded..0ad2e498 100644 --- a/app/Http/Api/Controllers/ArticleController.php +++ b/app/Http/Api/Controllers/ArticleController.php @@ -7,11 +7,13 @@ namespace App\Http\Api\Controllers; +use App\Services\Logic\Article\ArticleClose as ArticleCloseService; use App\Services\Logic\Article\ArticleDelete as ArticleDeleteService; use App\Services\Logic\Article\ArticleFavorite as ArticleFavoriteService; use App\Services\Logic\Article\ArticleInfo as ArticleInfoService; use App\Services\Logic\Article\ArticleLike as ArticleLikeService; use App\Services\Logic\Article\ArticleList as ArticleListService; +use App\Services\Logic\Article\ArticlePrivate as ArticlePrivateService; use App\Services\Logic\Article\CategoryList as CategoryListService; use App\Services\Logic\Article\CommentList as CommentListService; @@ -81,6 +83,34 @@ class ArticleController extends Controller return $this->jsonSuccess(); } + /** + * @Post("/{id:[0-9]+}/close", name="home.article.close") + */ + public function closeAction($id) + { + $service = new ArticleCloseService(); + + $article = $service->handle($id); + + $msg = $article->closed == 1 ? '关闭评论成功' : '开启评论成功'; + + return $this->jsonSuccess(['msg' => $msg]); + } + + /** + * @Post("/{id:[0-9]+}/private", name="home.article.private") + */ + public function privateAction($id) + { + $service = new ArticlePrivateService(); + + $article = $service->handle($id); + + $msg = $article->private == 1 ? '开启仅我可见成功' : '关闭仅我可见成功'; + + return $this->jsonSuccess(['msg' => $msg]); + } + /** * @Post("/{id:[0-9]+}/favorite", name="api.article.favorite") */ diff --git a/app/Http/Home/Controllers/ArticleController.php b/app/Http/Home/Controllers/ArticleController.php index b43d0278..9f199c3c 100644 --- a/app/Http/Home/Controllers/ArticleController.php +++ b/app/Http/Home/Controllers/ArticleController.php @@ -10,12 +10,14 @@ namespace App\Http\Home\Controllers; use App\Http\Home\Services\Article as ArticleService; use App\Http\Home\Services\ArticleQuery as ArticleQueryService; use App\Models\Article as ArticleModel; +use App\Services\Logic\Article\ArticleClose as ArticleCloseService; use App\Services\Logic\Article\ArticleCreate as ArticleCreateService; use App\Services\Logic\Article\ArticleDelete as ArticleDeleteService; use App\Services\Logic\Article\ArticleFavorite as ArticleFavoriteService; use App\Services\Logic\Article\ArticleInfo as ArticleInfoService; use App\Services\Logic\Article\ArticleLike as ArticleLikeService; use App\Services\Logic\Article\ArticleList as ArticleListService; +use App\Services\Logic\Article\ArticlePrivate as ArticlePrivateService; use App\Services\Logic\Article\ArticleUpdate as ArticleUpdateService; use App\Services\Logic\Article\RelatedArticleList as RelatedArticleListService; use Phalcon\Mvc\View; @@ -107,6 +109,14 @@ class ArticleController extends Controller return $this->notFound(); } + $private = $article['private'] == 1; + + $owned = $this->authUser->id == $article['owner']['id']; + + if ($private && !$owned) { + return $this->forbidden(); + } + $this->seo->prependTitle(['专栏', $article['title']]); $this->seo->setDescription($article['summary']); @@ -195,6 +205,34 @@ class ArticleController extends Controller return $this->jsonSuccess($content); } + /** + * @Post("/{id:[0-9]+}/close", name="home.article.close") + */ + public function closeAction($id) + { + $service = new ArticleCloseService(); + + $article = $service->handle($id); + + $msg = $article->closed == 1 ? '关闭评论成功' : '开启评论成功'; + + return $this->jsonSuccess(['msg' => $msg]); + } + + /** + * @Post("/{id:[0-9]+}/private", name="home.article.private") + */ + public function privateAction($id) + { + $service = new ArticlePrivateService(); + + $article = $service->handle($id); + + $msg = $article->private == 1 ? '开启仅我可见成功' : '关闭仅我可见成功'; + + return $this->jsonSuccess(['msg' => $msg]); + } + /** * @Post("/{id:[0-9]+}/favorite", name="home.article.favorite") */ diff --git a/app/Http/Home/Views/article/show.volt b/app/Http/Home/Views/article/show.volt index 06a57cb1..62fa935f 100644 --- a/app/Http/Home/Views/article/show.volt +++ b/app/Http/Home/Views/article/show.volt @@ -6,6 +6,8 @@ {% set article_edit_url = url({'for':'home.article.edit','id':article.id}) %} {% set article_delete_url = url({'for':'home.article.delete','id':article.id}) %} + {% set article_private_url = url({'for':'home.article.private','id':article.id}) %} + {% set article_close_url = url({'for':'home.article.close','id':article.id}) %} {% set article_owner_url = url({'for':'home.user.show','id':article.owner.id}) %} {% set article_related_url = url({'for':'home.article.related','id':article.id}) %} {% set article_report_url = url({'for':'home.report.add'},{'item_id':article.id,'item_type':106}) %} @@ -44,6 +46,16 @@
举报 {% if auth_user.id == article.owner.id %} + {% if article.closed == 0 %} + 关闭 + {% else %} + 打开 + {% endif %} + {% if article.private == 0 %} + 私密 + {% else %} + 共享 + {% endif %} 编辑 删除 {% endif %} diff --git a/app/Services/Logic/Article/ArticleClose.php b/app/Services/Logic/Article/ArticleClose.php new file mode 100644 index 00000000..2dc6b63d --- /dev/null +++ b/app/Services/Logic/Article/ArticleClose.php @@ -0,0 +1,36 @@ +checkArticle($id); + + $user = $this->getLoginUser(); + + $validator = new AppValidator(); + + $validator->checkOwner($user->id, $article->owner_id); + + $article->closed = $article->closed == 1 ? 0 : 1; + + $article->update(); + + return $article; + } + +} diff --git a/app/Services/Logic/Article/ArticlePrivate.php b/app/Services/Logic/Article/ArticlePrivate.php new file mode 100644 index 00000000..e9e0d59a --- /dev/null +++ b/app/Services/Logic/Article/ArticlePrivate.php @@ -0,0 +1,36 @@ +checkArticle($id); + + $user = $this->getLoginUser(); + + $validator = new AppValidator(); + + $validator->checkOwner($user->id, $article->owner_id); + + $article->private = $article->private == 1 ? 0 : 1; + + $article->update(); + + return $article; + } + +} diff --git a/public/static/home/js/article.show.js b/public/static/home/js/article.show.js index ac64e257..bf5acfbd 100644 --- a/public/static/home/js/article.show.js +++ b/public/static/home/js/article.show.js @@ -27,9 +27,30 @@ layui.use(['jquery', 'layer', 'helper'], function () { }); $('.article-edit').on('click', function () { + window.location.href = $(this).data('url'); + }); + + $('.article-close').on('click', function () { var url = $(this).data('url'); - helper.checkLogin(function () { - window.location.href = url; + $.post(url, function (res) { + if (res.msg) { + layer.msg(res.msg, {icon: 1}); + } + setTimeout(function () { + window.location.reload() + }, 1500); + }); + }); + + $('.article-private').on('click', function () { + var url = $(this).data('url'); + $.post(url, function (res) { + if (res.msg) { + layer.msg(res.msg, {icon: 1}); + } + setTimeout(function () { + window.location.reload() + }, 1500); }); }); From cebd2f09e6dcc2b296a48ab8237d6440d489f159 Mon Sep 17 00:00:00 2001 From: koogua Date: Thu, 30 Sep 2021 11:07:48 +0800 Subject: [PATCH 10/21] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=AF=BE?= =?UTF-8?q?=E7=A8=8B=E8=AF=84=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Logic/Course/BasicInfo.php | 8 ++++---- app/Services/Logic/Course/CourseList.php | 2 +- app/Services/Logic/Search/Course.php | 1 + public/static/home/js/article.show.js | 11 ----------- public/static/home/js/question.show.js | 10 ++-------- 5 files changed, 8 insertions(+), 24 deletions(-) diff --git a/app/Services/Logic/Course/BasicInfo.php b/app/Services/Logic/Course/BasicInfo.php index 9f527719..658b1aad 100644 --- a/app/Services/Logic/Course/BasicInfo.php +++ b/app/Services/Logic/Course/BasicInfo.php @@ -70,10 +70,10 @@ class BasicInfo extends LogicService $rating = $repo->findCourseRating($course->id); return [ - 'rating' => $rating->rating, - 'rating1' => $rating->rating1, - 'rating2' => $rating->rating2, - 'rating3' => $rating->rating3, + 'rating' => round($rating->rating, 1), + 'rating1' => round($rating->rating1, 1), + 'rating2' => round($rating->rating2, 1), + 'rating3' => round($rating->rating3, 1), ]; } diff --git a/app/Services/Logic/Course/CourseList.php b/app/Services/Logic/Course/CourseList.php index e7f0cf5f..4d29d144 100644 --- a/app/Services/Logic/Course/CourseList.php +++ b/app/Services/Logic/Course/CourseList.php @@ -80,7 +80,7 @@ class CourseList extends LogicService 'cover' => $course['cover'], 'market_price' => (float)$course['market_price'], 'vip_price' => (float)$course['vip_price'], - 'rating' => (float)$course['rating'], + 'rating' => round($course['rating'], 1), 'model' => $course['model'], 'level' => $course['level'], 'user_count' => $course['user_count'], diff --git a/app/Services/Logic/Search/Course.php b/app/Services/Logic/Search/Course.php index 92fa1338..0926573f 100644 --- a/app/Services/Logic/Search/Course.php +++ b/app/Services/Logic/Search/Course.php @@ -90,6 +90,7 @@ class Course extends Handler 'summary' => (string)$item['summary'], 'model' => (int)$item['model'], 'level' => (int)$item['level'], + 'rating' => round($item['rating'], 1), 'market_price' => (float)$item['market_price'], 'vip_price' => (float)$item['vip_price'], 'user_count' => (int)$item['user_count'], diff --git a/public/static/home/js/article.show.js b/public/static/home/js/article.show.js index bf5acfbd..5e52fa33 100644 --- a/public/static/home/js/article.show.js +++ b/public/static/home/js/article.show.js @@ -15,17 +15,6 @@ layui.use(['jquery', 'layer', 'helper'], function () { helper.ajaxLoadHtml($commentList.data('url'), $commentList.attr('id')); } - $('.article-report').on('click', function () { - var url = $(this).data('url'); - layer.open({ - type: 2, - title: '内容举报', - shadeClose: true, - content: [url, 'no'], - area: ['640px', '480px'], - }); - }); - $('.article-edit').on('click', function () { window.location.href = $(this).data('url'); }); diff --git a/public/static/home/js/question.show.js b/public/static/home/js/question.show.js index 7b3e9412..4bc30dc1 100644 --- a/public/static/home/js/question.show.js +++ b/public/static/home/js/question.show.js @@ -24,17 +24,11 @@ layui.use(['jquery', 'helper'], function () { }); $('.question-edit').on('click', function () { - var url = $(this).data('url'); - helper.checkLogin(function () { - window.location.href = url; - }); + window.location.href = $(this).data('url'); }); $('.btn-answer').on('click', function () { - var url = $(this).data('url'); - helper.checkLogin(function () { - window.location.href = url; - }); + window.location.href = $(this).data('url'); }); $('.icon-star').on('click', function () { From b64168120a759cfdb6b69d3393f3c78e8e242404 Mon Sep 17 00:00:00 2001 From: koogua Date: Thu, 30 Sep 2021 21:00:03 +0800 Subject: [PATCH 11/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=96=87=E7=AB=A0?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E9=97=AE=EF=BC=8C=E5=9B=9E=E7=AD=94,?= =?UTF-8?q?=E8=AF=BE=E6=97=B6=E8=AF=84=E8=AE=BA=E6=9F=A5=E8=AF=A2=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Logic/Answer/CommentList.php | 2 ++ app/Services/Logic/Article/CommentList.php | 1 + app/Services/Logic/Chapter/CommentList.php | 2 ++ app/Services/Logic/Question/CommentList.php | 2 ++ 4 files changed, 7 insertions(+) diff --git a/app/Services/Logic/Answer/CommentList.php b/app/Services/Logic/Answer/CommentList.php index 959e9ab7..8c32e903 100644 --- a/app/Services/Logic/Answer/CommentList.php +++ b/app/Services/Logic/Answer/CommentList.php @@ -31,6 +31,8 @@ class CommentList extends LogicService $params['item_id'] = $answer->id; $params['item_type'] = CommentModel::ITEM_ANSWER; $params['published'] = CommentModel::PUBLISH_APPROVED; + $params['parent_id'] = 0; + $params['deleted'] = 0; $sort = $pagerQuery->getSort(); $page = $pagerQuery->getPage(); diff --git a/app/Services/Logic/Article/CommentList.php b/app/Services/Logic/Article/CommentList.php index be2cb373..ed2f034f 100644 --- a/app/Services/Logic/Article/CommentList.php +++ b/app/Services/Logic/Article/CommentList.php @@ -31,6 +31,7 @@ class CommentList extends LogicService $params['item_id'] = $article->id; $params['item_type'] = CommentModel::ITEM_ARTICLE; $params['published'] = CommentModel::PUBLISH_APPROVED; + $params['parent_id'] = 0; $params['deleted'] = 0; $sort = $pagerQuery->getSort(); diff --git a/app/Services/Logic/Chapter/CommentList.php b/app/Services/Logic/Chapter/CommentList.php index ca0881ea..b1d64521 100644 --- a/app/Services/Logic/Chapter/CommentList.php +++ b/app/Services/Logic/Chapter/CommentList.php @@ -31,6 +31,8 @@ class CommentList extends LogicService $params['item_id'] = $chapter->id; $params['item_type'] = CommentModel::ITEM_CHAPTER; $params['published'] = CommentModel::PUBLISH_APPROVED; + $params['parent_id'] = 0; + $params['deleted'] = 0; $sort = $pagerQuery->getSort(); $page = $pagerQuery->getPage(); diff --git a/app/Services/Logic/Question/CommentList.php b/app/Services/Logic/Question/CommentList.php index 14add9ef..aed22e64 100644 --- a/app/Services/Logic/Question/CommentList.php +++ b/app/Services/Logic/Question/CommentList.php @@ -31,6 +31,8 @@ class CommentList extends LogicService $params['item_id'] = $question->id; $params['item_type'] = CommentModel::ITEM_QUESTION; $params['published'] = CommentModel::PUBLISH_APPROVED; + $params['parent_id'] = 0; + $params['deleted'] = 0; $sort = $pagerQuery->getSort(); $page = $pagerQuery->getPage(); From 1f46f35a4227cb6ad448699dfcf0845a68ed19e9 Mon Sep 17 00:00:00 2001 From: koogua Date: Sun, 3 Oct 2021 09:47:23 +0800 Subject: [PATCH 12/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3websocket=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Api/Controllers/Controller.php | 5 +++++ app/Http/Home/Controllers/Controller.php | 2 +- app/Validators/Validator.php | 14 ++++++++++++++ config/config.default.php | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/Http/Api/Controllers/Controller.php b/app/Http/Api/Controllers/Controller.php index 60c3dac3..f9677c20 100644 --- a/app/Http/Api/Controllers/Controller.php +++ b/app/Http/Api/Controllers/Controller.php @@ -11,6 +11,7 @@ use App\Models\User as UserModel; use App\Services\Auth\Api as ApiAuth; use App\Traits\Response as ResponseTrait; use App\Traits\Security as SecurityTrait; +use App\Validators\Validator as AppValidator; use Phalcon\Mvc\Dispatcher; class Controller extends \Phalcon\Mvc\Controller @@ -30,6 +31,10 @@ class Controller extends \Phalcon\Mvc\Controller $this->setCors(); } + $validator = new AppValidator(); + + $validator->checkSiteStatus(); + $this->checkRateLimit(); return true; diff --git a/app/Http/Home/Controllers/Controller.php b/app/Http/Home/Controllers/Controller.php index f295e6c8..96d1a664 100644 --- a/app/Http/Home/Controllers/Controller.php +++ b/app/Http/Home/Controllers/Controller.php @@ -147,7 +147,7 @@ class Controller extends \Phalcon\Mvc\Controller * ssl通过nginx转发实现 */ if ($this->request->isSecure()) { - $websocket->connect_url = sprintf('wss://%s/wss', $this->request->getHttpHost()); + $websocket->connect_url = sprintf('wss://%s/wss', $websocket->connect_address); } else { $websocket->connect_url = sprintf('ws://%s', $websocket->connect_address); } diff --git a/app/Validators/Validator.php b/app/Validators/Validator.php index ea2d7ad5..d395a851 100644 --- a/app/Validators/Validator.php +++ b/app/Validators/Validator.php @@ -8,12 +8,26 @@ namespace App\Validators; use App\Exceptions\Forbidden as ForbiddenException; +use App\Exceptions\ServiceUnavailable; +use App\Exceptions\ServiceUnavailable as ServiceUnavailableException; use App\Exceptions\Unauthorized as UnauthorizedException; +use App\Services\Service as AppService; use Phalcon\Di\Injectable; class Validator extends Injectable { + public function checkSiteStatus() + { + $service = new AppService(); + + $siteInfo = $service->getSettings('site'); + + if ($siteInfo['status'] == 'closed') { + throw new ServiceUnavailableException('sys.service_unavailable'); + } + } + public function checkAuthUser($userId) { if (empty($userId)) { diff --git a/config/config.default.php b/config/config.default.php index 889f30da..2a1e5a53 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -150,7 +150,7 @@ $config['throttle']['rate_limit'] = 60; /** * 客户端ping服务端间隔(秒) */ -$config['websocket']['ping_interval'] = 50; +$config['websocket']['ping_interval'] = 30; /** * 客户端连接地址(外部可访问的域名或ip),带端口号 From 6495cbfbe0788f098997caff8930d27925f7e253 Mon Sep 17 00:00:00 2001 From: koogua Date: Sun, 3 Oct 2021 11:55:22 +0800 Subject: [PATCH 13/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3websocket=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Api/Controllers/PublicController.php | 6 +++++- app/Http/Home/Controllers/Controller.php | 3 ++- app/Http/Home/Controllers/ImController.php | 2 +- public/static/home/js/chapter.live.chat.js | 1 + public/static/home/js/im.cs.js | 1 + public/static/home/js/im.msgbox.js | 2 +- 6 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/Http/Api/Controllers/PublicController.php b/app/Http/Api/Controllers/PublicController.php index bc4058e2..aade7fd4 100644 --- a/app/Http/Api/Controllers/PublicController.php +++ b/app/Http/Api/Controllers/PublicController.php @@ -49,8 +49,12 @@ class PublicController extends Controller $content = []; + /** + * ssl通过nginx转发实现 + */ if ($this->request->isSecure()) { - $content['connect_url'] = sprintf('wss://%s/wss', $this->request->getHttpHost()); + list($domain) = explode(':', $websocket->connect_address); + $websocket['connect_url'] = sprintf('wss://%s/wss', $domain); } else { $content['connect_url'] = sprintf('ws://%s', $websocket->connect_address); } diff --git a/app/Http/Home/Controllers/Controller.php b/app/Http/Home/Controllers/Controller.php index 96d1a664..3cd93081 100644 --- a/app/Http/Home/Controllers/Controller.php +++ b/app/Http/Home/Controllers/Controller.php @@ -147,7 +147,8 @@ class Controller extends \Phalcon\Mvc\Controller * ssl通过nginx转发实现 */ if ($this->request->isSecure()) { - $websocket->connect_url = sprintf('wss://%s/wss', $websocket->connect_address); + list($domain) = explode(':', $websocket->connect_address); + $websocket->connect_url = sprintf('wss://%s/wss', $domain); } else { $websocket->connect_url = sprintf('ws://%s', $websocket->connect_address); } diff --git a/app/Http/Home/Controllers/ImController.php b/app/Http/Home/Controllers/ImController.php index 13df71d5..bb530c2d 100644 --- a/app/Http/Home/Controllers/ImController.php +++ b/app/Http/Home/Controllers/ImController.php @@ -145,7 +145,7 @@ class ImController extends Controller } /** - * @Post("/notice/read", name="home.im.read_notice") + * @Get("/notice/read", name="home.im.read_notice") */ public function readNoticeAction() { diff --git a/public/static/home/js/chapter.live.chat.js b/public/static/home/js/chapter.live.chat.js index edb92926..8f2e2662 100644 --- a/public/static/home/js/chapter.live.chat.js +++ b/public/static/home/js/chapter.live.chat.js @@ -13,6 +13,7 @@ layui.use(['jquery', 'form', 'helper'], function () { console.log('socket connect success'); setInterval(function () { socket.send('ping'); + console.log('ping...'); }, 1000 * parseInt(window.im.ws.ping_interval)); }; diff --git a/public/static/home/js/im.cs.js b/public/static/home/js/im.cs.js index f86c831f..17a22417 100644 --- a/public/static/home/js/im.cs.js +++ b/public/static/home/js/im.cs.js @@ -22,6 +22,7 @@ layui.use(['jquery', 'layim'], function () { console.log('socket connect success'); setInterval(function () { socket.send('ping'); + console.log('ping...'); }, 1000 * parseInt(window.im.ws.ping_interval)); }; diff --git a/public/static/home/js/im.msgbox.js b/public/static/home/js/im.msgbox.js index a1420e77..3fb891f6 100644 --- a/public/static/home/js/im.msgbox.js +++ b/public/static/home/js/im.msgbox.js @@ -42,7 +42,7 @@ layui.use(['jquery', 'layer', 'laypage'], function () { } function readNotices() { - $.post('/im/notice/read'); + $.get('/im/notice/read'); } var action = { From 1fe8c289a5a3316fc2f988e6f2cbcd01c7ec9b70 Mon Sep 17 00:00:00 2001 From: koogua Date: Sun, 3 Oct 2021 14:53:17 +0800 Subject: [PATCH 14/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3websocket=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Api/Controllers/PublicController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Api/Controllers/PublicController.php b/app/Http/Api/Controllers/PublicController.php index aade7fd4..c350a69c 100644 --- a/app/Http/Api/Controllers/PublicController.php +++ b/app/Http/Api/Controllers/PublicController.php @@ -54,7 +54,7 @@ class PublicController extends Controller */ if ($this->request->isSecure()) { list($domain) = explode(':', $websocket->connect_address); - $websocket['connect_url'] = sprintf('wss://%s/wss', $domain); + $content['connect_url'] = sprintf('wss://%s/wss', $domain); } else { $content['connect_url'] = sprintf('ws://%s', $websocket->connect_address); } From 8cfcc74b2af5a534dcfb53d840e03e3b05b91a2b Mon Sep 17 00:00:00 2001 From: koogua Date: Mon, 4 Oct 2021 18:56:08 +0800 Subject: [PATCH 15/21] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=80=E5=87=BA?= =?UTF-8?q?=E7=BE=A4=E7=BB=84=E5=92=8C=E8=A7=A3=E9=99=A4=E5=A5=BD=E5=8F=8B?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../Api/Controllers/ImFriendController.php | 30 ++++++++++ .../Api/Controllers/ImGroupController.php | 13 ++++ app/Services/Logic/Im/FriendQuit.php | 45 ++++++++++++++ app/Services/Logic/Im/GroupInfo.php | 2 +- app/Services/Logic/Im/GroupQuit.php | 60 +++++++++++++++++++ app/Services/Logic/Im/GroupUserList.php | 2 +- app/Services/Logic/ImGroupTrait.php | 2 +- app/Services/Logic/ImUserTrait.php | 22 +++++++ app/Validators/ImGroupUser.php | 9 +++ config/errors.php | 1 + 11 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 app/Http/Api/Controllers/ImFriendController.php create mode 100644 app/Services/Logic/Im/FriendQuit.php create mode 100644 app/Services/Logic/Im/GroupQuit.php create mode 100644 app/Services/Logic/ImUserTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cec2ad5..903b5edc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - 调整订单发货为每一分钟执行一次 - 增强课时安全性,无权限时不返回播放地址或内容 - 抽离出文章关闭,仅我可见操作 +- 增加退出群组和解除好友接口 - 增加删除文章和提问接口 - 增加首页推荐教师接口 - 增加微信公众号支付处理 diff --git a/app/Http/Api/Controllers/ImFriendController.php b/app/Http/Api/Controllers/ImFriendController.php new file mode 100644 index 00000000..958bcf7c --- /dev/null +++ b/app/Http/Api/Controllers/ImFriendController.php @@ -0,0 +1,30 @@ +handle($id); + + return $this->jsonSuccess(); + } + +} diff --git a/app/Http/Api/Controllers/ImGroupController.php b/app/Http/Api/Controllers/ImGroupController.php index 928cb225..e8d53e2f 100644 --- a/app/Http/Api/Controllers/ImGroupController.php +++ b/app/Http/Api/Controllers/ImGroupController.php @@ -9,6 +9,7 @@ namespace App\Http\Api\Controllers; use App\Services\Logic\Im\GroupInfo as GroupInfoService; use App\Services\Logic\Im\GroupList as GroupListService; +use App\Services\Logic\Im\GroupQuit as GroupQuitService; use App\Services\Logic\Im\GroupUserList as GroupUserListService; /** @@ -53,4 +54,16 @@ class ImGroupController extends Controller return $this->jsonPaginate($pager); } + /** + * @Post("/{id:[0-9]+}/quit", name="api.im_group.quit") + */ + public function quitAction($id) + { + $service = new GroupQuitService(); + + $service->handle($id); + + return $this->jsonSuccess(); + } + } diff --git a/app/Services/Logic/Im/FriendQuit.php b/app/Services/Logic/Im/FriendQuit.php new file mode 100644 index 00000000..b022ef85 --- /dev/null +++ b/app/Services/Logic/Im/FriendQuit.php @@ -0,0 +1,45 @@ +getLoginUser(); + + $user = $this->checkImUser($loginUser->id); + + $validator = new ImFriendUserValidator(); + + $friend = $validator->checkFriend($id); + + $friendUser = $validator->checkFriendUser($user->id, $friend->id); + + $friendUser->delete(); + + $this->decrUserFriendCount($user); + } + + protected function decrUserFriendCount(ImUserModel $user) + { + if ($user->friend_count > 0) { + $user->friend_count -= 1; + $user->update(); + } + } + +} diff --git a/app/Services/Logic/Im/GroupInfo.php b/app/Services/Logic/Im/GroupInfo.php index 490bf3a1..4074ec42 100644 --- a/app/Services/Logic/Im/GroupInfo.php +++ b/app/Services/Logic/Im/GroupInfo.php @@ -18,7 +18,7 @@ class GroupInfo extends LogicService public function handle($id) { - $group = $this->checkGroup($id); + $group = $this->checkImGroup($id); $userRepo = new UserRepo(); diff --git a/app/Services/Logic/Im/GroupQuit.php b/app/Services/Logic/Im/GroupQuit.php new file mode 100644 index 00000000..cdf00832 --- /dev/null +++ b/app/Services/Logic/Im/GroupQuit.php @@ -0,0 +1,60 @@ +getLoginUser(); + + $group = $this->checkImGroup($id); + + $user = $this->checkImUser($loginUser->id); + + $validator = new ImGroupUserValidator(); + + $groupUser = $validator->checkGroupUser($group->id, $user->id); + + $validator->checkIfAllowQuit($group->id, $user->id); + + $groupUser->delete(); + + $this->decrGroupUserCount($group); + + $this->decrUserGroupCount($user); + } + + protected function decrUserGroupCount(ImUserModel $user) + { + if ($user->group_count > 0) { + $user->group_count -= 1; + $user->update(); + } + } + + protected function decrGroupUserCount(ImGroupModel $group) + { + if ($group->user_count > 0) { + $group->user_count -= 1; + $group->update(); + } + } + +} diff --git a/app/Services/Logic/Im/GroupUserList.php b/app/Services/Logic/Im/GroupUserList.php index 9a674b8f..b77acb79 100644 --- a/app/Services/Logic/Im/GroupUserList.php +++ b/app/Services/Logic/Im/GroupUserList.php @@ -20,7 +20,7 @@ class GroupUserList extends LogicService public function handle($id) { - $group = $this->checkGroup($id); + $group = $this->checkImGroup($id); $pagerQuery = new PagerQuery(); diff --git a/app/Services/Logic/ImGroupTrait.php b/app/Services/Logic/ImGroupTrait.php index 739b4fb6..b3a11a73 100644 --- a/app/Services/Logic/ImGroupTrait.php +++ b/app/Services/Logic/ImGroupTrait.php @@ -12,7 +12,7 @@ use App\Validators\ImGroup as ImGroupValidator; trait ImGroupTrait { - public function checkGroup($id) + public function checkImGroup($id) { $validator = new ImGroupValidator(); diff --git a/app/Services/Logic/ImUserTrait.php b/app/Services/Logic/ImUserTrait.php new file mode 100644 index 00000000..3cc33145 --- /dev/null +++ b/app/Services/Logic/ImUserTrait.php @@ -0,0 +1,22 @@ +checkUser($id); + } + +} diff --git a/app/Validators/ImGroupUser.php b/app/Validators/ImGroupUser.php index 19e3fd5c..b02892fc 100644 --- a/app/Validators/ImGroupUser.php +++ b/app/Validators/ImGroupUser.php @@ -114,4 +114,13 @@ class ImGroupUser extends Validator } } + public function checkIfAllowQuit($groupId, $userId) + { + $group = $this->checkGroup($groupId); + + if ($group->owner_id == $userId) { + throw new BadRequestException('im_group_user.owner_quit_not_allowed'); + } + } + } diff --git a/config/errors.php b/config/errors.php index 6b283efb..90855cbe 100644 --- a/config/errors.php +++ b/config/errors.php @@ -464,6 +464,7 @@ $error['im_group_user.not_found'] = '群组关系不存在'; $error['im_group_user.remark_too_long'] = '验证信息太长(超过30字符)'; $error['im_group_user.has_joined'] = '已经加入过群组'; $error['im_group_user.join_not_allowed'] = '当前不允许加入群组'; +$error['im_group_user.owner_quit_not_allowed'] = '当前不允许群主退群'; $error['im_group_user.delete_owner_not_allowed'] = '当前不允许删除群主'; $error['im_friend_user.not_found'] = '好友关系不存在'; From d94001a4a412cd89806505a9f7297f18262b53c7 Mon Sep 17 00:00:00 2001 From: koogua Date: Wed, 6 Oct 2021 17:01:49 +0800 Subject: [PATCH 16/21] =?UTF-8?q?=EF=BC=91=EF=BC=8E=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E4=B8=AD=E5=88=A0=E9=99=A4=E5=85=B3=E8=81=94?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E6=93=8D=E4=BD=9C=20=EF=BC=92=EF=BC=8E?= =?UTF-8?q?=E6=95=B4=E7=90=86=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Builders/AnswerList.php | 2 +- app/Builders/ArticleList.php | 2 +- app/Builders/CategoryTreeList.php | 2 + app/Builders/CommentList.php | 2 +- app/Builders/ConsultList.php | 2 +- app/Builders/CourseList.php | 2 +- app/Builders/DanmuList.php | 2 +- app/Builders/ImMessageList.php | 2 +- app/Builders/LiveList.php | 2 +- app/Builders/NavTreeList.php | 5 +- app/Builders/NotificationList.php | 2 +- app/Builders/OrderList.php | 2 +- app/Builders/QuestionFavoriteList.php | 2 +- app/Builders/QuestionList.php | 2 +- app/Builders/RefundList.php | 5 +- app/Builders/ReportList.php | 2 +- app/Builders/ReviewList.php | 2 +- app/Builders/TradeList.php | 5 +- app/Caches/CategoryList.php | 1 + app/Caches/CourseRecommendedList.php | 4 +- app/Caches/CourseTeacherList.php | 1 + app/Caches/HelpList.php | 5 +- app/Caches/HotQuestionList.php | 3 +- app/Caches/ImActiveGroupList.php | 2 + app/Caches/ImNewGroupList.php | 1 + app/Caches/IndexArticleList.php | 5 +- app/Caches/IndexFeaturedCourseList.php | 7 +- app/Caches/IndexFreeCourseList.php | 7 +- app/Caches/IndexLiveList.php | 2 + app/Caches/IndexNewCourseList.php | 5 +- app/Caches/IndexQuestionList.php | 5 +- app/Caches/IndexSimpleFeaturedCourseList.php | 5 +- app/Caches/IndexSimpleFreeCourseList.php | 5 +- app/Caches/IndexSimpleNewCourseList.php | 1 + app/Caches/IndexSimpleVipCourseList.php | 5 +- app/Caches/IndexSlideList.php | 1 + app/Caches/IndexTeacherList.php | 1 + app/Caches/IndexVipCourseList.php | 7 +- app/Caches/PointHotGiftList.php | 1 + app/Caches/TaggedArticleList.php | 1 + app/Caches/TaggedQuestionList.php | 1 + app/Console/Tasks/ArticleIndexTask.php | 1 + app/Console/Tasks/CourseIndexTask.php | 1 + app/Console/Tasks/GroupIndexTask.php | 1 + app/Console/Tasks/QuestionIndexTask.php | 1 + app/Console/Tasks/UserIndexTask.php | 1 + app/Http/Api/Controllers/AnswerController.php | 12 +++ .../Api/Controllers/ArticleController.php | 17 ++++ .../Api/Controllers/ChapterController.php | 10 ++- .../Api/Controllers/ConsultController.php | 12 +++ app/Http/Api/Controllers/CourseController.php | 8 ++ app/Http/Api/Controllers/HelpController.php | 8 ++ .../Api/Controllers/ImGroupController.php | 8 ++ app/Http/Api/Controllers/OrderController.php | 4 + app/Http/Api/Controllers/PageController.php | 8 ++ .../Api/Controllers/QuestionController.php | 12 +++ app/Http/Api/Controllers/RefundController.php | 8 ++ app/Http/Api/Controllers/ReviewController.php | 12 +++ app/Http/Api/Controllers/TradeController.php | 8 ++ app/Http/Api/Controllers/UserController.php | 4 + .../Home/Controllers/AnswerController.php | 11 ++- .../Home/Controllers/ArticleController.php | 12 ++- .../Home/Controllers/ChapterController.php | 22 ++--- .../Home/Controllers/ConsultController.php | 12 +++ .../Home/Controllers/CourseController.php | 6 +- app/Http/Home/Controllers/HelpController.php | 6 +- .../Home/Controllers/ImGroupController.php | 6 +- app/Http/Home/Controllers/OrderController.php | 8 ++ .../Home/Controllers/PackageController.php | 2 +- app/Http/Home/Controllers/PageController.php | 6 +- .../Home/Controllers/PointGiftController.php | 6 +- .../Home/Controllers/QuestionController.php | 11 ++- .../Home/Controllers/RefundController.php | 8 ++ .../Home/Controllers/ReviewController.php | 12 +++ app/Http/Home/Controllers/TopicController.php | 2 +- app/Http/Home/Controllers/UserController.php | 2 +- app/Http/Home/Views/refund/info.volt | 2 +- app/Models/Category.php | 4 - app/Models/Chapter.php | 4 - app/Models/Consult.php | 4 - app/Models/Course.php | 4 - app/Models/Danmu.php | 4 - app/Models/FlashSale.php | 4 - app/Models/Help.php | 4 - app/Models/ImGroup.php | 4 - app/Models/Nav.php | 4 - app/Models/Package.php | 4 - app/Models/Page.php | 4 - app/Models/PointGift.php | 4 - app/Models/Review.php | 4 - app/Models/Slide.php | 4 - app/Models/Tag.php | 4 - app/Models/Topic.php | 4 - app/Models/WeChatSubscribe.php | 7 ++ app/Repos/Comment.php | 5 +- app/Repos/Consult.php | 5 +- app/Repos/ImGroup.php | 4 +- app/Repos/Package.php | 5 +- app/Repos/Review.php | 5 +- app/Repos/Topic.php | 5 +- app/Repos/User.php | 33 +++++++- app/Services/Logic/Answer/AnswerInfo.php | 44 ++++------ app/Services/Logic/Article/ArticleInfo.php | 70 +++++----------- app/Services/Logic/Comment/CommentInfo.php | 56 +++++++------ app/Services/Logic/Consult/ConsultInfo.php | 80 ++++++++++--------- app/Services/Logic/Danmu/DanmuInfo.php | 20 ++--- app/Services/Logic/Order/OrderInfo.php | 32 +++++--- app/Services/Logic/Question/AnswerList.php | 7 +- app/Services/Logic/Question/QuestionInfo.php | 31 ++----- app/Services/Logic/Refund/RefundInfo.php | 46 +++++++++-- app/Services/Logic/Review/ReviewInfo.php | 61 +++++++++----- app/Services/Logic/Trade/TradeInfo.php | 74 ++++++++++++++++- app/Services/Logic/UserTrait.php | 14 ++++ app/Traits/Response.php | 29 +------ 114 files changed, 708 insertions(+), 380 deletions(-) diff --git a/app/Builders/AnswerList.php b/app/Builders/AnswerList.php index 7e47e5b4..0c3b0b99 100644 --- a/app/Builders/AnswerList.php +++ b/app/Builders/AnswerList.php @@ -58,7 +58,7 @@ class AnswerList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/ArticleList.php b/app/Builders/ArticleList.php index 7edf86f8..7ef8ca46 100644 --- a/app/Builders/ArticleList.php +++ b/app/Builders/ArticleList.php @@ -71,7 +71,7 @@ class ArticleList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/CategoryTreeList.php b/app/Builders/CategoryTreeList.php index ab23e991..6944aea8 100644 --- a/app/Builders/CategoryTreeList.php +++ b/app/Builders/CategoryTreeList.php @@ -69,6 +69,7 @@ class CategoryTreeList extends Builder $query->where('parent_id = 0'); $query->andWhere('published = 1'); + $query->andWhere('deleted = 0'); $query->andWhere('type = :type:', ['type' => $type]); $query->orderBy('priority ASC'); @@ -84,6 +85,7 @@ class CategoryTreeList extends Builder $query = CategoryModel::query(); $query->where('published = 1'); + $query->where('deleted = 0'); $query->andWhere('parent_id = :parent_id:', ['parent_id' => $parentId]); $query->orderBy('priority ASC'); diff --git a/app/Builders/CommentList.php b/app/Builders/CommentList.php index f300ecda..e0eb0059 100644 --- a/app/Builders/CommentList.php +++ b/app/Builders/CommentList.php @@ -32,7 +32,7 @@ class CommentList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/ConsultList.php b/app/Builders/ConsultList.php index b356276b..a06e667c 100644 --- a/app/Builders/ConsultList.php +++ b/app/Builders/ConsultList.php @@ -79,7 +79,7 @@ class ConsultList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/CourseList.php b/app/Builders/CourseList.php index 25c821e4..e5a3c383 100644 --- a/app/Builders/CourseList.php +++ b/app/Builders/CourseList.php @@ -62,7 +62,7 @@ class CourseList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/DanmuList.php b/app/Builders/DanmuList.php index 8adf3c09..2ee29569 100644 --- a/app/Builders/DanmuList.php +++ b/app/Builders/DanmuList.php @@ -87,7 +87,7 @@ class DanmuList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/ImMessageList.php b/app/Builders/ImMessageList.php index 0026bfbc..03fce797 100644 --- a/app/Builders/ImMessageList.php +++ b/app/Builders/ImMessageList.php @@ -29,7 +29,7 @@ class ImMessageList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/LiveList.php b/app/Builders/LiveList.php index 734e4d2d..8f8102e2 100644 --- a/app/Builders/LiveList.php +++ b/app/Builders/LiveList.php @@ -48,7 +48,7 @@ class LiveList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($teacherIds, ['id', 'name', 'title', 'avatar', 'about']); + $users = $userRepo->findShallowUserByIds($teacherIds); $baseUrl = kg_cos_url(); diff --git a/app/Builders/NavTreeList.php b/app/Builders/NavTreeList.php index 185e3fa7..6801c2a5 100644 --- a/app/Builders/NavTreeList.php +++ b/app/Builders/NavTreeList.php @@ -68,6 +68,7 @@ class NavTreeList extends Builder return NavModel::query() ->where('parent_id = :parent_id:', ['parent_id' => $navId]) ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->execute(); } @@ -80,7 +81,9 @@ class NavTreeList extends Builder { return NavModel::query() ->where('position = :position:', ['position' => $position]) - ->andWhere('level = 1 AND published = 1') + ->andWhere('level = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->execute(); } diff --git a/app/Builders/NotificationList.php b/app/Builders/NotificationList.php index 73067c4a..6670553d 100644 --- a/app/Builders/NotificationList.php +++ b/app/Builders/NotificationList.php @@ -32,7 +32,7 @@ class NotificationList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/OrderList.php b/app/Builders/OrderList.php index cfcdae2d..d317d604 100644 --- a/app/Builders/OrderList.php +++ b/app/Builders/OrderList.php @@ -186,7 +186,7 @@ class OrderList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name']); + $users = $userRepo->findShallowUserByIds($ids); $result = []; diff --git a/app/Builders/QuestionFavoriteList.php b/app/Builders/QuestionFavoriteList.php index c7675389..7d5185de 100644 --- a/app/Builders/QuestionFavoriteList.php +++ b/app/Builders/QuestionFavoriteList.php @@ -72,7 +72,7 @@ class QuestionFavoriteList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/QuestionList.php b/app/Builders/QuestionList.php index 3cb04d76..10a40a3c 100644 --- a/app/Builders/QuestionList.php +++ b/app/Builders/QuestionList.php @@ -74,7 +74,7 @@ class QuestionList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/RefundList.php b/app/Builders/RefundList.php index 4328431d..396bdd44 100644 --- a/app/Builders/RefundList.php +++ b/app/Builders/RefundList.php @@ -58,11 +58,14 @@ class RefundList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name']); + $users = $userRepo->findShallowUserByIds($ids); + + $baseUrl = kg_cos_url(); $result = []; foreach ($users->toArray() as $user) { + $user['avatar'] = $baseUrl . $user['avatar']; $result[$user['id']] = $user; } diff --git a/app/Builders/ReportList.php b/app/Builders/ReportList.php index f858ede8..89c67218 100644 --- a/app/Builders/ReportList.php +++ b/app/Builders/ReportList.php @@ -29,7 +29,7 @@ class ReportList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/ReviewList.php b/app/Builders/ReviewList.php index 2cbb354a..5a931902 100644 --- a/app/Builders/ReviewList.php +++ b/app/Builders/ReviewList.php @@ -58,7 +58,7 @@ class ReviewList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findShallowUserByIds($ids); $baseUrl = kg_cos_url(); diff --git a/app/Builders/TradeList.php b/app/Builders/TradeList.php index aa62d73d..40d17a92 100644 --- a/app/Builders/TradeList.php +++ b/app/Builders/TradeList.php @@ -58,11 +58,14 @@ class TradeList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name']); + $users = $userRepo->findShallowUserByIds($ids); + + $baseUrl = kg_cos_url(); $result = []; foreach ($users->toArray() as $user) { + $user['avatar'] = $baseUrl . $user['avatar']; $result[$user['id']] = $user; } diff --git a/app/Caches/CategoryList.php b/app/Caches/CategoryList.php index 8f7b6b59..5d436aa8 100644 --- a/app/Caches/CategoryList.php +++ b/app/Caches/CategoryList.php @@ -38,6 +38,7 @@ class CategoryList extends Cache ->columns(['id', 'parent_id', 'name', 'priority', 'level', 'path']) ->where('type = :type:', ['type' => $type]) ->andWhere('published = 1') + ->andWhere('deleted = 0') ->execute(); if ($categories->count() == 0) { diff --git a/app/Caches/CourseRecommendedList.php b/app/Caches/CourseRecommendedList.php index b9c27f20..51ed746d 100644 --- a/app/Caches/CourseRecommendedList.php +++ b/app/Caches/CourseRecommendedList.php @@ -70,7 +70,9 @@ class CourseRecommendedList extends Cache public function findCourses($limit = 5) { return CourseModel::query() - ->where('published = 1 AND market_price > 0') + ->where('market_price > 0') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('RAND()') ->limit($limit) ->execute(); diff --git a/app/Caches/CourseTeacherList.php b/app/Caches/CourseTeacherList.php index f6a3c583..70314edc 100644 --- a/app/Caches/CourseTeacherList.php +++ b/app/Caches/CourseTeacherList.php @@ -51,6 +51,7 @@ class CourseTeacherList extends Cache 'id' => $user->id, 'name' => $user->name, 'avatar' => $user->avatar, + 'vip' => $user->vip, 'title' => $user->title, 'about' => $user->about, ]; diff --git a/app/Caches/HelpList.php b/app/Caches/HelpList.php index a253c3d5..2899bbd8 100644 --- a/app/Caches/HelpList.php +++ b/app/Caches/HelpList.php @@ -73,7 +73,9 @@ class HelpList extends Cache { return CategoryModel::query() ->where('type = :type:', ['type' => CategoryModel::TYPE_HELP]) - ->andWhere('level = 1 AND published = 1') + ->andWhere('level = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->execute(); } @@ -87,6 +89,7 @@ class HelpList extends Cache return HelpModel::query() ->where('category_id = :category_id:', ['category_id' => $categoryId]) ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->execute(); } diff --git a/app/Caches/HotQuestionList.php b/app/Caches/HotQuestionList.php index e09b517d..dea5c8a4 100644 --- a/app/Caches/HotQuestionList.php +++ b/app/Caches/HotQuestionList.php @@ -111,7 +111,8 @@ class HotQuestionList extends Cache { return QuestionModel::query() ->where('create_time > :create_time:', ['create_time' => $createTime]) - ->andWhere('published = 1 AND deleted = 0') + ->andWhere('published = :published:', ['published' => QuestionModel::PUBLISH_APPROVED]) + ->andWhere('deleted = 0') ->orderBy('score DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/ImActiveGroupList.php b/app/Caches/ImActiveGroupList.php index 24a0759d..581d3044 100644 --- a/app/Caches/ImActiveGroupList.php +++ b/app/Caches/ImActiveGroupList.php @@ -72,6 +72,8 @@ class ImActiveGroupList extends Cache ->orderBy('total_count DESC') ->where('receiver_type = :type:', ['type' => ImMessageModel::TYPE_GROUP]) ->betweenWhere('create_time', $startTime, $endTime) + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->limit($limit) ->execute(); diff --git a/app/Caches/ImNewGroupList.php b/app/Caches/ImNewGroupList.php index 03cb1f5f..b76e0495 100644 --- a/app/Caches/ImNewGroupList.php +++ b/app/Caches/ImNewGroupList.php @@ -70,6 +70,7 @@ class ImNewGroupList extends Cache { return ImGroupModel::query() ->where('published = 1') + ->andWhere('deleted = 0') ->orderBy('id DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexArticleList.php b/app/Caches/IndexArticleList.php index 0f03cc0f..6b6f20cc 100644 --- a/app/Caches/IndexArticleList.php +++ b/app/Caches/IndexArticleList.php @@ -30,7 +30,10 @@ class IndexArticleList extends Cache { $articleRepo = new ArticleRepo(); - $where = ['published' => ArticleModel::PUBLISH_APPROVED]; + $where = [ + 'published' => ArticleModel::PUBLISH_APPROVED, + 'deleted' => 0, + ]; $pager = $articleRepo->paginate($where, 'latest', 1, 10); diff --git a/app/Caches/IndexFeaturedCourseList.php b/app/Caches/IndexFeaturedCourseList.php index 7fe31e94..9ef38676 100644 --- a/app/Caches/IndexFeaturedCourseList.php +++ b/app/Caches/IndexFeaturedCourseList.php @@ -94,7 +94,9 @@ class IndexFeaturedCourseList extends Cache { return CategoryModel::query() ->where('type = :type:', ['type' => CategoryModel::TYPE_COURSE]) - ->andWhere('level = 1 AND published = 1') + ->andWhere('level = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->limit($limit) ->execute(); @@ -113,8 +115,9 @@ class IndexFeaturedCourseList extends Cache return CourseModel::query() ->inWhere('category_id', $categoryIds) - ->andWhere('published = 1') ->andWhere('featured = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('id DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexFreeCourseList.php b/app/Caches/IndexFreeCourseList.php index 071ba74c..ecd23215 100644 --- a/app/Caches/IndexFreeCourseList.php +++ b/app/Caches/IndexFreeCourseList.php @@ -94,7 +94,9 @@ class IndexFreeCourseList extends Cache { return CategoryModel::query() ->where('type = :type:', ['type' => CategoryModel::TYPE_COURSE]) - ->andWhere('level = 1 AND published = 1') + ->andWhere('level = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->limit($limit) ->execute(); @@ -113,8 +115,9 @@ class IndexFreeCourseList extends Cache return CourseModel::query() ->inWhere('category_id', $categoryIds) - ->andWhere('published = 1') ->andWhere('market_price = 0') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('score DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexLiveList.php b/app/Caches/IndexLiveList.php index 6eaab7bf..6d5d0836 100644 --- a/app/Caches/IndexLiveList.php +++ b/app/Caches/IndexLiveList.php @@ -132,6 +132,8 @@ class IndexLiveList extends Cache ->addFrom(ChapterLiveModel::class, 'cl') ->join(ChapterModel::class, 'cl.chapter_id = c.id', 'c') ->betweenWhere('start_time', $startTime, $endTime) + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('start_time ASC') ->getQuery() ->execute(); diff --git a/app/Caches/IndexNewCourseList.php b/app/Caches/IndexNewCourseList.php index 6f4c9f50..1049d2c2 100644 --- a/app/Caches/IndexNewCourseList.php +++ b/app/Caches/IndexNewCourseList.php @@ -94,7 +94,9 @@ class IndexNewCourseList extends Cache { return CategoryModel::query() ->where('type = :type:', ['type' => CategoryModel::TYPE_COURSE]) - ->andWhere('level = 1 AND published = 1') + ->andWhere('level = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->limit($limit) ->execute(); @@ -114,6 +116,7 @@ class IndexNewCourseList extends Cache return CourseModel::query() ->inWhere('category_id', $categoryIds) ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('id DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexQuestionList.php b/app/Caches/IndexQuestionList.php index 9c301b1a..b79db98b 100644 --- a/app/Caches/IndexQuestionList.php +++ b/app/Caches/IndexQuestionList.php @@ -30,7 +30,10 @@ class IndexQuestionList extends Cache { $questionRepo = new QuestionRepo(); - $where = ['published' => QuestionModel::PUBLISH_APPROVED]; + $where = [ + 'published' => QuestionModel::PUBLISH_APPROVED, + 'deleted' => 0, + ]; $pager = $questionRepo->paginate($where, 'latest', 1, 10); diff --git a/app/Caches/IndexSimpleFeaturedCourseList.php b/app/Caches/IndexSimpleFeaturedCourseList.php index a933b7cc..f3588fed 100644 --- a/app/Caches/IndexSimpleFeaturedCourseList.php +++ b/app/Caches/IndexSimpleFeaturedCourseList.php @@ -65,8 +65,9 @@ class IndexSimpleFeaturedCourseList extends Cache protected function findCourses($limit = 8) { return CourseModel::query() - ->where('published = 1') - ->andWhere('featured = 1') + ->where('featured = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('id DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexSimpleFreeCourseList.php b/app/Caches/IndexSimpleFreeCourseList.php index b7f187ea..a37e8779 100644 --- a/app/Caches/IndexSimpleFreeCourseList.php +++ b/app/Caches/IndexSimpleFreeCourseList.php @@ -65,8 +65,9 @@ class IndexSimpleFreeCourseList extends Cache protected function findCourses($limit = 8) { return CourseModel::query() - ->where('published = 1') - ->andWhere('market_price = 0') + ->where('market_price = 0') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('score DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexSimpleNewCourseList.php b/app/Caches/IndexSimpleNewCourseList.php index 94c0b5e2..b43166a9 100644 --- a/app/Caches/IndexSimpleNewCourseList.php +++ b/app/Caches/IndexSimpleNewCourseList.php @@ -66,6 +66,7 @@ class IndexSimpleNewCourseList extends Cache { return CourseModel::query() ->where('published = 1') + ->andWhere('deleted = 0') ->orderBy('id DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexSimpleVipCourseList.php b/app/Caches/IndexSimpleVipCourseList.php index cda3478f..d4474287 100644 --- a/app/Caches/IndexSimpleVipCourseList.php +++ b/app/Caches/IndexSimpleVipCourseList.php @@ -65,9 +65,10 @@ class IndexSimpleVipCourseList extends Cache protected function findCourses($limit = 8) { return CourseModel::query() - ->where('published = 1') - ->andWhere('market_price > vip_price') + ->where('market_price > vip_price') ->andWhere('vip_price >= 0') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('score DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexSlideList.php b/app/Caches/IndexSlideList.php index e8ca5e71..abc7d28c 100644 --- a/app/Caches/IndexSlideList.php +++ b/app/Caches/IndexSlideList.php @@ -68,6 +68,7 @@ class IndexSlideList extends Cache { return SlideModel::query() ->where('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexTeacherList.php b/app/Caches/IndexTeacherList.php index 2decb09d..df9d2056 100644 --- a/app/Caches/IndexTeacherList.php +++ b/app/Caches/IndexTeacherList.php @@ -60,6 +60,7 @@ class IndexTeacherList extends Cache { return UserModel::query() ->where('edu_role = :edu_role:', ['edu_role' => UserModel::EDU_ROLE_TEACHER]) + ->andWhere('deleted = 0') ->orderBy('RAND()') ->limit($limit) ->execute(); diff --git a/app/Caches/IndexVipCourseList.php b/app/Caches/IndexVipCourseList.php index 201edb71..163bff1f 100644 --- a/app/Caches/IndexVipCourseList.php +++ b/app/Caches/IndexVipCourseList.php @@ -94,7 +94,9 @@ class IndexVipCourseList extends Cache { return CategoryModel::query() ->where('type = :type:', ['type' => CategoryModel::TYPE_COURSE]) - ->andWhere('level = 1 AND published = 1') + ->andWhere('level = 1') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('priority ASC') ->limit($limit) ->execute(); @@ -113,9 +115,10 @@ class IndexVipCourseList extends Cache return CourseModel::query() ->inWhere('category_id', $categoryIds) - ->andWhere('published = 1') ->andWhere('market_price > vip_price') ->andWhere('vip_price >= 0') + ->andWhere('published = 1') + ->andWhere('deleted = 0') ->orderBy('score DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/PointHotGiftList.php b/app/Caches/PointHotGiftList.php index 7b89f3cf..5fe9213a 100644 --- a/app/Caches/PointHotGiftList.php +++ b/app/Caches/PointHotGiftList.php @@ -76,6 +76,7 @@ class PointHotGiftList extends Cache { return PointGiftModel::query() ->where('published = 1') + ->andWhere('deleted = 0') ->orderBy('redeem_count DESC') ->limit($limit) ->execute(); diff --git a/app/Caches/TaggedArticleList.php b/app/Caches/TaggedArticleList.php index 88ad3f5d..8113b4f8 100644 --- a/app/Caches/TaggedArticleList.php +++ b/app/Caches/TaggedArticleList.php @@ -34,6 +34,7 @@ class TaggedArticleList extends Cache $where = [ 'tag_id' => $id, 'published' => ArticleModel::PUBLISH_APPROVED, + 'deleted' => 0, ]; $pager = $articleRepo->paginate($where); diff --git a/app/Caches/TaggedQuestionList.php b/app/Caches/TaggedQuestionList.php index ee7ef9c9..d563dabe 100644 --- a/app/Caches/TaggedQuestionList.php +++ b/app/Caches/TaggedQuestionList.php @@ -35,6 +35,7 @@ class TaggedQuestionList extends Cache $where = [ 'tag_id' => $id, 'published' => QuestionModel::PUBLISH_APPROVED, + 'deleted' => 0, ]; $pager = $questionRepo->paginate($where); diff --git a/app/Console/Tasks/ArticleIndexTask.php b/app/Console/Tasks/ArticleIndexTask.php index 77b5ff2d..017a2577 100644 --- a/app/Console/Tasks/ArticleIndexTask.php +++ b/app/Console/Tasks/ArticleIndexTask.php @@ -124,6 +124,7 @@ class ArticleIndexTask extends Task { return ArticleModel::query() ->where('published = :published:', ['published' => ArticleModel::PUBLISH_APPROVED]) + ->andWhere('deleted = 0') ->execute(); } diff --git a/app/Console/Tasks/CourseIndexTask.php b/app/Console/Tasks/CourseIndexTask.php index f757883b..ed568faf 100644 --- a/app/Console/Tasks/CourseIndexTask.php +++ b/app/Console/Tasks/CourseIndexTask.php @@ -124,6 +124,7 @@ class CourseIndexTask extends Task { return CourseModel::query() ->where('published = 1') + ->where('deleted = 0') ->execute(); } diff --git a/app/Console/Tasks/GroupIndexTask.php b/app/Console/Tasks/GroupIndexTask.php index 8147ef37..cad23dbf 100644 --- a/app/Console/Tasks/GroupIndexTask.php +++ b/app/Console/Tasks/GroupIndexTask.php @@ -124,6 +124,7 @@ class GroupIndexTask extends Task { return GroupModel::query() ->where('published = 1') + ->andWhere('deleted = 0') ->execute(); } diff --git a/app/Console/Tasks/QuestionIndexTask.php b/app/Console/Tasks/QuestionIndexTask.php index 6594881c..886d0aea 100644 --- a/app/Console/Tasks/QuestionIndexTask.php +++ b/app/Console/Tasks/QuestionIndexTask.php @@ -124,6 +124,7 @@ class QuestionIndexTask extends Task { return QuestionModel::query() ->where('published = :published:', ['published' => QuestionModel::PUBLISH_APPROVED]) + ->andWhere('deleted = 0') ->execute(); } diff --git a/app/Console/Tasks/UserIndexTask.php b/app/Console/Tasks/UserIndexTask.php index 674d12ee..77b0db5d 100644 --- a/app/Console/Tasks/UserIndexTask.php +++ b/app/Console/Tasks/UserIndexTask.php @@ -139,6 +139,7 @@ class UserIndexTask extends Task protected function findUsers($limit, $offset) { return UserModel::query() + ->where('deleted = 0') ->limit($limit, $offset) ->execute(); } diff --git a/app/Http/Api/Controllers/AnswerController.php b/app/Http/Api/Controllers/AnswerController.php index 979f5827..d49ed3ae 100644 --- a/app/Http/Api/Controllers/AnswerController.php +++ b/app/Http/Api/Controllers/AnswerController.php @@ -7,6 +7,7 @@ namespace App\Http\Api\Controllers; +use App\Models\Answer as AnswerModel; use App\Services\Logic\Answer\AnswerAccept as AnswerAcceptService; use App\Services\Logic\Answer\AnswerCreate as AnswerCreateService; use App\Services\Logic\Answer\AnswerDelete as AnswerDeleteService; @@ -30,6 +31,17 @@ class AnswerController extends Controller $answer = $service->handle($id); + if ($answer['deleted'] == 1) { + $this->notFound(); + } + + $approved = $answer['published'] != AnswerModel::PUBLISH_APPROVED; + $owned = $answer['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); + } + return $this->jsonSuccess(['answer' => $answer]); } diff --git a/app/Http/Api/Controllers/ArticleController.php b/app/Http/Api/Controllers/ArticleController.php index 0ad2e498..c61ac491 100644 --- a/app/Http/Api/Controllers/ArticleController.php +++ b/app/Http/Api/Controllers/ArticleController.php @@ -7,6 +7,7 @@ namespace App\Http\Api\Controllers; +use App\Models\Article as ArticleModel; use App\Services\Logic\Article\ArticleClose as ArticleCloseService; use App\Services\Logic\Article\ArticleDelete as ArticleDeleteService; use App\Services\Logic\Article\ArticleFavorite as ArticleFavoriteService; @@ -56,6 +57,22 @@ class ArticleController extends Controller $article = $service->handle($id); + if ($article['deleted'] == 1) { + $this->notFound(); + } + + $approved = $article['published'] == ArticleModel::PUBLISH_APPROVED; + $owned = $article['me']['owned'] == 1; + $private = $article['private'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); + } + + if ($private && !$owned) { + $this->forbidden(); + } + return $this->jsonSuccess(['article' => $article]); } diff --git a/app/Http/Api/Controllers/ChapterController.php b/app/Http/Api/Controllers/ChapterController.php index 63ca974f..1b96f7c5 100644 --- a/app/Http/Api/Controllers/ChapterController.php +++ b/app/Http/Api/Controllers/ChapterController.php @@ -65,8 +65,16 @@ class ChapterController extends Controller $chapter = $service->handle($id); + if ($chapter['deleted'] == 1) { + $this->notFound(); + } + + if ($chapter['published'] == 0) { + $this->notFound(); + } + if ($chapter['me']['owned'] == 0) { - return $this->jsonError(['msg' => '没有访问章节权限']); + $this->forbidden(); } return $this->jsonSuccess(['chapter' => $chapter]); diff --git a/app/Http/Api/Controllers/ConsultController.php b/app/Http/Api/Controllers/ConsultController.php index 4a9b1386..45db1c0e 100644 --- a/app/Http/Api/Controllers/ConsultController.php +++ b/app/Http/Api/Controllers/ConsultController.php @@ -7,6 +7,7 @@ namespace App\Http\Api\Controllers; +use App\Models\Consult as ConsultModel; use App\Services\Logic\Consult\ConsultCreate as ConsultCreateService; use App\Services\Logic\Consult\ConsultDelete as ConsultDeleteService; use App\Services\Logic\Consult\ConsultInfo as ConsultInfoService; @@ -28,6 +29,17 @@ class ConsultController extends Controller $consult = $service->handle($id); + if ($consult['deleted'] == 1) { + $this->notFound(); + } + + $approved = $consult['published'] == ConsultModel::PUBLISH_APPROVED; + $owned = $consult['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); + } + return $this->jsonSuccess(['consult' => $consult]); } diff --git a/app/Http/Api/Controllers/CourseController.php b/app/Http/Api/Controllers/CourseController.php index 5c449652..62c8596b 100644 --- a/app/Http/Api/Controllers/CourseController.php +++ b/app/Http/Api/Controllers/CourseController.php @@ -55,6 +55,14 @@ class CourseController extends Controller $course = $service->handle($id); + if ($course['deleted'] == 1) { + $this->notFound(); + } + + if ($course['published'] == 0) { + $this->notFound(); + } + return $this->jsonSuccess(['course' => $course]); } diff --git a/app/Http/Api/Controllers/HelpController.php b/app/Http/Api/Controllers/HelpController.php index e944dc47..06dc00c1 100644 --- a/app/Http/Api/Controllers/HelpController.php +++ b/app/Http/Api/Controllers/HelpController.php @@ -37,6 +37,14 @@ class HelpController extends Controller $help = $service->handle($id); + if ($help['deleted'] == 1) { + $this->notFound(); + } + + if ($help['published'] == 0) { + $this->notFound(); + } + return $this->jsonSuccess(['help' => $help]); } diff --git a/app/Http/Api/Controllers/ImGroupController.php b/app/Http/Api/Controllers/ImGroupController.php index e8d53e2f..5e199b08 100644 --- a/app/Http/Api/Controllers/ImGroupController.php +++ b/app/Http/Api/Controllers/ImGroupController.php @@ -39,6 +39,14 @@ class ImGroupController extends Controller $group = $service->handle($id); + if ($group['deleted'] == 1) { + $this->notFound(); + } + + if ($group['published'] == 0) { + $this->notFound(); + } + return $this->jsonSuccess(['group' => $group]); } diff --git a/app/Http/Api/Controllers/OrderController.php b/app/Http/Api/Controllers/OrderController.php index dbd9dc47..cd5fb1f3 100644 --- a/app/Http/Api/Controllers/OrderController.php +++ b/app/Http/Api/Controllers/OrderController.php @@ -29,6 +29,10 @@ class OrderController extends Controller $order = $service->handle($sn); + if ($order['deleted'] == 1) { + $this->notFound(); + } + return $this->jsonSuccess(['order' => $order]); } diff --git a/app/Http/Api/Controllers/PageController.php b/app/Http/Api/Controllers/PageController.php index 7ef38905..e8094151 100644 --- a/app/Http/Api/Controllers/PageController.php +++ b/app/Http/Api/Controllers/PageController.php @@ -24,6 +24,14 @@ class PageController extends Controller $page = $service->handle($id); + if ($page['deleted'] == 1) { + $this->notFound(); + } + + if ($page['published'] == 0) { + $this->notFound(); + } + return $this->jsonSuccess(['page' => $page]); } diff --git a/app/Http/Api/Controllers/QuestionController.php b/app/Http/Api/Controllers/QuestionController.php index 3ba26c90..a3e402d3 100644 --- a/app/Http/Api/Controllers/QuestionController.php +++ b/app/Http/Api/Controllers/QuestionController.php @@ -7,6 +7,7 @@ namespace App\Http\Api\Controllers; +use App\Models\Question as QuestionModel; use App\Services\Logic\Question\AnswerList as AnswerListService; use App\Services\Logic\Question\CategoryList as CategoryListService; use App\Services\Logic\Question\CommentList as CommentListService; @@ -55,6 +56,17 @@ class QuestionController extends Controller $question = $service->handle($id); + if ($question['deleted'] == 1) { + $this->notFound(); + } + + $approved = $question['published'] == QuestionModel::PUBLISH_APPROVED; + $owned = $question['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); + } + return $this->jsonSuccess(['question' => $question]); } diff --git a/app/Http/Api/Controllers/RefundController.php b/app/Http/Api/Controllers/RefundController.php index 8f5249d3..17c8e692 100644 --- a/app/Http/Api/Controllers/RefundController.php +++ b/app/Http/Api/Controllers/RefundController.php @@ -43,6 +43,14 @@ class RefundController extends Controller $refund = $service->handle($sn); + if ($refund['deleted'] == 1) { + $this->notFound(); + } + + if ($refund['me']['owned'] == 0) { + $this->forbidden(); + } + return $this->jsonSuccess(['refund' => $refund]); } diff --git a/app/Http/Api/Controllers/ReviewController.php b/app/Http/Api/Controllers/ReviewController.php index 1c88a173..2f6d0e9d 100644 --- a/app/Http/Api/Controllers/ReviewController.php +++ b/app/Http/Api/Controllers/ReviewController.php @@ -7,6 +7,7 @@ namespace App\Http\Api\Controllers; +use App\Models\Review as ReviewModel; use App\Services\Logic\Review\ReviewCreate as ReviewCreateService; use App\Services\Logic\Review\ReviewDelete as ReviewDeleteService; use App\Services\Logic\Review\ReviewInfo as ReviewInfoService; @@ -28,6 +29,17 @@ class ReviewController extends Controller $review = $service->handle($id); + if ($review['deleted'] == 1) { + $this->notFound(); + } + + $approved = $review['published'] == ReviewModel::PUBLISH_APPROVED; + $owned = $review['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); + } + return $this->jsonSuccess(['review' => $review]); } diff --git a/app/Http/Api/Controllers/TradeController.php b/app/Http/Api/Controllers/TradeController.php index bb03665d..99cd5b13 100644 --- a/app/Http/Api/Controllers/TradeController.php +++ b/app/Http/Api/Controllers/TradeController.php @@ -27,6 +27,14 @@ class TradeController extends Controller $trade = $service->handle($sn); + if ($trade['deleted'] == 1) { + $this->notFound(); + } + + if ($trade['me']['owned'] == 0) { + $this->forbidden(); + } + return $this->jsonSuccess(['trade' => $trade]); } diff --git a/app/Http/Api/Controllers/UserController.php b/app/Http/Api/Controllers/UserController.php index 6bb5ecfa..13d3a078 100644 --- a/app/Http/Api/Controllers/UserController.php +++ b/app/Http/Api/Controllers/UserController.php @@ -30,6 +30,10 @@ class UserController extends Controller $user = $service->handle($id); + if ($user['deleted'] == 1) { + $this->notFound(); + } + return $this->jsonSuccess(['user' => $user]); } diff --git a/app/Http/Home/Controllers/AnswerController.php b/app/Http/Home/Controllers/AnswerController.php index 0a09e4d7..a833382b 100644 --- a/app/Http/Home/Controllers/AnswerController.php +++ b/app/Http/Home/Controllers/AnswerController.php @@ -57,8 +57,15 @@ class AnswerController extends Controller $answer = $service->handle($id); - if ($answer['published'] != AnswerModel::PUBLISH_APPROVED) { - return $this->notFound(); + if ($answer['deleted'] == 1) { + $this->notFound(); + } + + $approved = $answer['published'] != AnswerModel::PUBLISH_APPROVED; + $owned = $answer['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); } $questionId = $answer['question']['id']; diff --git a/app/Http/Home/Controllers/ArticleController.php b/app/Http/Home/Controllers/ArticleController.php index 9f199c3c..cbd48b8f 100644 --- a/app/Http/Home/Controllers/ArticleController.php +++ b/app/Http/Home/Controllers/ArticleController.php @@ -105,16 +105,20 @@ class ArticleController extends Controller $article = $service->handle($id); - if ($article['published'] != ArticleModel::PUBLISH_APPROVED) { - return $this->notFound(); + if ($article['deleted'] == 1) { + $this->notFound(); } + $approved = $article['published'] == ArticleModel::PUBLISH_APPROVED; + $owned = $article['me']['owned'] == 1; $private = $article['private'] == 1; - $owned = $this->authUser->id == $article['owner']['id']; + if (!$approved && !$owned) { + $this->notFound(); + } if ($private && !$owned) { - return $this->forbidden(); + $this->forbidden(); } $this->seo->prependTitle(['专栏', $article['title']]); diff --git a/app/Http/Home/Controllers/ChapterController.php b/app/Http/Home/Controllers/ChapterController.php index c1a75bff..6e2ba588 100644 --- a/app/Http/Home/Controllers/ChapterController.php +++ b/app/Http/Home/Controllers/ChapterController.php @@ -39,20 +39,24 @@ class ChapterController extends Controller */ public function showAction($id) { - $service = new ChapterInfoService(); - - $chapter = $service->handle($id); - - if ($chapter['published'] == 0) { - return $this->notFound(); - } - if ($this->authUser->id == 0) { return $this->response->redirect(['for' => 'home.account.login']); } + $service = new ChapterInfoService(); + + $chapter = $service->handle($id); + + if ($chapter['deleted'] == 1) { + $this->notFound(); + } + + if ($chapter['published'] == 0) { + $this->notFound(); + } + if ($chapter['me']['owned'] == 0) { - return $this->forbidden(); + $this->forbidden(); } $service = new CourseInfoService(); diff --git a/app/Http/Home/Controllers/ConsultController.php b/app/Http/Home/Controllers/ConsultController.php index d1d78730..dfacf179 100644 --- a/app/Http/Home/Controllers/ConsultController.php +++ b/app/Http/Home/Controllers/ConsultController.php @@ -7,6 +7,7 @@ namespace App\Http\Home\Controllers; +use App\Models\Consult as ConsultModel; use App\Services\Logic\Consult\ConsultCreate as ConsultCreateService; use App\Services\Logic\Consult\ConsultDelete as ConsultDeleteService; use App\Services\Logic\Consult\ConsultInfo as ConsultInfoService; @@ -37,6 +38,17 @@ class ConsultController extends Controller $consult = $service->handle($id); + if ($consult['deleted'] == 1) { + $this->notFound(); + } + + $approved = $consult['published'] == ConsultModel::PUBLISH_APPROVED; + $owned = $consult['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); + } + $this->view->setVar('consult', $consult); } diff --git a/app/Http/Home/Controllers/CourseController.php b/app/Http/Home/Controllers/CourseController.php index ec180bb5..bc1ae450 100644 --- a/app/Http/Home/Controllers/CourseController.php +++ b/app/Http/Home/Controllers/CourseController.php @@ -77,8 +77,12 @@ class CourseController extends Controller $course = $service->handle($id); + if ($course['deleted'] == 1) { + $this->notFound(); + } + if ($course['published'] == 0) { - return $this->notFound(); + $this->notFound(); } $service = new CourseChapterListService(); diff --git a/app/Http/Home/Controllers/HelpController.php b/app/Http/Home/Controllers/HelpController.php index db5cbe88..e41ccb9a 100644 --- a/app/Http/Home/Controllers/HelpController.php +++ b/app/Http/Home/Controllers/HelpController.php @@ -40,8 +40,12 @@ class HelpController extends Controller $help = $service->handle($id); + if ($help['deleted'] == 1) { + $this->notFound(); + } + if ($help['published'] == 0) { - return $this->notFound(); + $this->notFound(); } $featuredCourses = $this->getFeaturedCourses(); diff --git a/app/Http/Home/Controllers/ImGroupController.php b/app/Http/Home/Controllers/ImGroupController.php index 858c9374..fdabf072 100644 --- a/app/Http/Home/Controllers/ImGroupController.php +++ b/app/Http/Home/Controllers/ImGroupController.php @@ -51,8 +51,12 @@ class ImGroupController extends Controller $group = $service->getGroup($id); + if ($group['deleted'] == 1) { + $this->notFound(); + } + if ($group['published'] == 0) { - return $this->notFound(); + $this->notFound(); } $this->seo->prependTitle(['群组', $group['name']]); diff --git a/app/Http/Home/Controllers/OrderController.php b/app/Http/Home/Controllers/OrderController.php index 70a74c56..98310103 100644 --- a/app/Http/Home/Controllers/OrderController.php +++ b/app/Http/Home/Controllers/OrderController.php @@ -45,6 +45,14 @@ class OrderController extends Controller $order = $service->handle($sn); + if ($order['deleted'] == 1) { + $this->notFound(); + } + + if ($order['me']['owned'] == 0) { + $this->forbidden(); + } + $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW); $this->view->setVar('order', $order); } diff --git a/app/Http/Home/Controllers/PackageController.php b/app/Http/Home/Controllers/PackageController.php index 67e739f1..0baf6299 100644 --- a/app/Http/Home/Controllers/PackageController.php +++ b/app/Http/Home/Controllers/PackageController.php @@ -27,7 +27,7 @@ class PackageController extends Controller $package = $service->handle($id); if ($package['published'] == 0) { - return $this->notFound(); + $this->notFound(); } $this->seo->prependTitle(['套餐', $package['title']]); diff --git a/app/Http/Home/Controllers/PageController.php b/app/Http/Home/Controllers/PageController.php index 286d63fe..5689b2c9 100644 --- a/app/Http/Home/Controllers/PageController.php +++ b/app/Http/Home/Controllers/PageController.php @@ -25,8 +25,12 @@ class PageController extends Controller $page = $service->handle($id); + if ($page['deleted'] == 1) { + $this->notFound(); + } + if ($page['published'] == 0) { - return $this->notFound(); + $this->notFound(); } $featuredCourses = $this->getFeaturedCourses(); diff --git a/app/Http/Home/Controllers/PointGiftController.php b/app/Http/Home/Controllers/PointGiftController.php index 2023605b..f6c50caa 100644 --- a/app/Http/Home/Controllers/PointGiftController.php +++ b/app/Http/Home/Controllers/PointGiftController.php @@ -68,8 +68,12 @@ class PointGiftController extends Controller $gift = $service->handle($id); + if ($gift['deleted'] == 1) { + $this->notFound(); + } + if ($gift['published'] == 0) { - return $this->notFound(); + $this->notFound(); } $hotGifts = $this->getHotGifts(); diff --git a/app/Http/Home/Controllers/QuestionController.php b/app/Http/Home/Controllers/QuestionController.php index a2f406fb..7c92df29 100644 --- a/app/Http/Home/Controllers/QuestionController.php +++ b/app/Http/Home/Controllers/QuestionController.php @@ -102,8 +102,15 @@ class QuestionController extends Controller $question = $service->handle($id); - if ($question['published'] != QuestionModel::PUBLISH_APPROVED) { - return $this->notFound(); + if ($question['deleted'] == 1) { + $this->notFound(); + } + + $approved = $question['published'] == QuestionModel::PUBLISH_APPROVED; + $owned = $question['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); } $this->seo->prependTitle(['问答', $question['title']]); diff --git a/app/Http/Home/Controllers/RefundController.php b/app/Http/Home/Controllers/RefundController.php index 591f579e..ba01b011 100644 --- a/app/Http/Home/Controllers/RefundController.php +++ b/app/Http/Home/Controllers/RefundController.php @@ -63,6 +63,14 @@ class RefundController extends Controller $refund = $service->handle($sn); + if ($refund['deleted'] == 1) { + $this->notFound(); + } + + if ($refund['me']['owned'] == 0) { + $this->forbidden(); + } + $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW); $this->view->setVar('refund', $refund); } diff --git a/app/Http/Home/Controllers/ReviewController.php b/app/Http/Home/Controllers/ReviewController.php index 5b3db66c..2b6ab9f6 100644 --- a/app/Http/Home/Controllers/ReviewController.php +++ b/app/Http/Home/Controllers/ReviewController.php @@ -7,6 +7,7 @@ namespace App\Http\Home\Controllers; +use App\Models\Review as ReviewModel; use App\Services\Logic\Review\ReviewCreate as ReviewCreateService; use App\Services\Logic\Review\ReviewDelete as ReviewDeleteService; use App\Services\Logic\Review\ReviewInfo as ReviewInfoService; @@ -50,6 +51,17 @@ class ReviewController extends Controller $review = $service->handle($id); + if ($review['deleted'] == 1) { + $this->notFound(); + } + + $approved = $review['published'] == ReviewModel::PUBLISH_APPROVED; + $owned = $review['me']['owned'] == 1; + + if (!$approved && !$owned) { + $this->notFound(); + } + return $this->jsonSuccess(['review' => $review]); } diff --git a/app/Http/Home/Controllers/TopicController.php b/app/Http/Home/Controllers/TopicController.php index c6098c64..33ff9ad7 100644 --- a/app/Http/Home/Controllers/TopicController.php +++ b/app/Http/Home/Controllers/TopicController.php @@ -27,7 +27,7 @@ class TopicController extends Controller $topic = $service->handle($id); if ($topic['published'] == 0) { - return $this->notFound(); + $this->notFound(); } $this->seo->prependTitle(['专题', $topic['title']]); diff --git a/app/Http/Home/Controllers/UserController.php b/app/Http/Home/Controllers/UserController.php index 80a09280..6870b2db 100644 --- a/app/Http/Home/Controllers/UserController.php +++ b/app/Http/Home/Controllers/UserController.php @@ -32,7 +32,7 @@ class UserController extends Controller $user = $service->handle($id); if ($user['deleted'] == 1) { - return $this->notFound(); + $this->notFound(); } $this->seo->prependTitle(['空间', $user['name']]); diff --git a/app/Http/Home/Views/refund/info.volt b/app/Http/Home/Views/refund/info.volt index d2365231..400c00c5 100644 --- a/app/Http/Home/Views/refund/info.volt +++ b/app/Http/Home/Views/refund/info.volt @@ -21,7 +21,7 @@
- {% if refund.status == 3 %} + {% if refund.me.allow_cancel == 1 %} {% endif %}
diff --git a/app/Models/Category.php b/app/Models/Category.php index b3f125c4..8b182ef5 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -144,10 +144,6 @@ class Category extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index 870c4a7d..4cbfaac0 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -253,10 +253,6 @@ class Chapter extends Model $this->attrs = kg_json_encode($this->attrs); } - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Consult.php b/app/Models/Consult.php index 2281e6a5..469cb8eb 100644 --- a/app/Models/Consult.php +++ b/app/Models/Consult.php @@ -183,10 +183,6 @@ class Consult extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Course.php b/app/Models/Course.php index 72ee81a6..957451f1 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -342,10 +342,6 @@ class Course extends Model $this->attrs = kg_json_encode($this->attrs); } - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Danmu.php b/app/Models/Danmu.php index 76449a17..7e0e0bb1 100644 --- a/app/Models/Danmu.php +++ b/app/Models/Danmu.php @@ -149,10 +149,6 @@ class Danmu extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/FlashSale.php b/app/Models/FlashSale.php index b43cd117..e82eeab0 100644 --- a/app/Models/FlashSale.php +++ b/app/Models/FlashSale.php @@ -135,10 +135,6 @@ class FlashSale extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Help.php b/app/Models/Help.php index a9c2f574..75226fde 100644 --- a/app/Models/Help.php +++ b/app/Models/Help.php @@ -100,10 +100,6 @@ class Help extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/ImGroup.php b/app/Models/ImGroup.php index cac13b16..f9aeab4a 100644 --- a/app/Models/ImGroup.php +++ b/app/Models/ImGroup.php @@ -142,10 +142,6 @@ class ImGroup extends Model $sync->addItem($this->id); } - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Nav.php b/app/Models/Nav.php index 5041d9d4..34cb6c57 100644 --- a/app/Models/Nav.php +++ b/app/Models/Nav.php @@ -146,10 +146,6 @@ class Nav extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Package.php b/app/Models/Package.php index c609d8a7..0432529d 100644 --- a/app/Models/Package.php +++ b/app/Models/Package.php @@ -115,10 +115,6 @@ class Package extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Page.php b/app/Models/Page.php index eff9ad4c..4318e8f8 100644 --- a/app/Models/Page.php +++ b/app/Models/Page.php @@ -93,10 +93,6 @@ class Page extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/PointGift.php b/app/Models/PointGift.php index 19a27098..7efcce29 100644 --- a/app/Models/PointGift.php +++ b/app/Models/PointGift.php @@ -180,10 +180,6 @@ class PointGift extends Model $this->attrs = kg_json_encode($this->attrs); } - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Review.php b/app/Models/Review.php index 46c30a9f..57564009 100644 --- a/app/Models/Review.php +++ b/app/Models/Review.php @@ -169,10 +169,6 @@ class Review extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Slide.php b/app/Models/Slide.php index cf8f703e..b7dffd17 100644 --- a/app/Models/Slide.php +++ b/app/Models/Slide.php @@ -135,10 +135,6 @@ class Slide extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Tag.php b/app/Models/Tag.php index bbcc6a7f..c33d8b56 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -143,10 +143,6 @@ class Tag extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/Topic.php b/app/Models/Topic.php index 114afed6..8b672d1f 100644 --- a/app/Models/Topic.php +++ b/app/Models/Topic.php @@ -93,10 +93,6 @@ class Topic extends Model public function beforeUpdate() { - if ($this->deleted == 1) { - $this->published = 0; - } - $this->update_time = time(); } diff --git a/app/Models/WeChatSubscribe.php b/app/Models/WeChatSubscribe.php index ae51cc9a..b6b0234a 100644 --- a/app/Models/WeChatSubscribe.php +++ b/app/Models/WeChatSubscribe.php @@ -31,6 +31,13 @@ class WeChatSubscribe extends Model */ public $open_id = ''; + /** + * 删除标识 + * + * @var int + */ + public $deleted = 0; + /** * 创建时间 * diff --git a/app/Repos/Comment.php b/app/Repos/Comment.php index d70b31d9..aaec7d90 100644 --- a/app/Repos/Comment.php +++ b/app/Repos/Comment.php @@ -103,7 +103,10 @@ class Comment extends Repository public function countComments() { - return (int)CommentModel::count(['conditions' => 'deleted = 0']); + return (int)CommentModel::count([ + 'conditions' => 'published = :published: AND deleted = 0', + 'bind' => ['published' => CommentModel::PUBLISH_APPROVED], + ]); } } diff --git a/app/Repos/Consult.php b/app/Repos/Consult.php index 45f71fb6..6ceb5fac 100644 --- a/app/Repos/Consult.php +++ b/app/Repos/Consult.php @@ -118,7 +118,10 @@ class Consult extends Repository public function countConsults() { - return (int)ConsultModel::count(['conditions' => 'deleted = 0']); + return (int)ConsultModel::count([ + 'conditions' => 'published = :published: AND deleted = 0', + 'bind' => ['published' => ConsultModel::PUBLISH_APPROVED], + ]); } public function countLikes($consultId) diff --git a/app/Repos/ImGroup.php b/app/Repos/ImGroup.php index b0985645..d3ef2b96 100644 --- a/app/Repos/ImGroup.php +++ b/app/Repos/ImGroup.php @@ -128,7 +128,9 @@ class ImGroup extends Repository public function countGroups() { - return (int)ImGroupModel::count(['conditions' => 'published = 1']); + return (int)ImGroupModel::count([ + 'conditions' => 'published = 1 AND deleted = 0', + ]); } public function countUsers($groupId) diff --git a/app/Repos/Package.php b/app/Repos/Package.php index 81105f63..cac9bf52 100644 --- a/app/Repos/Package.php +++ b/app/Repos/Package.php @@ -111,13 +111,16 @@ class Package extends Repository ->join(CoursePackageModel::class, 'c.id = cp.course_id', 'cp') ->where('cp.package_id = :package_id:', ['package_id' => $packageId]) ->andWhere('c.published = 1') + ->andWhere('c.deleted = 0') ->getQuery() ->execute(); } public function countPackages() { - return (int)PackageModel::count(['conditions' => 'deleted = 0']); + return (int)PackageModel::count([ + 'conditions' => 'published = 1 AND deleted = 0', + ]); } public function countCourses($packageId) diff --git a/app/Repos/Review.php b/app/Repos/Review.php index 49aee2d4..3abc4e53 100644 --- a/app/Repos/Review.php +++ b/app/Repos/Review.php @@ -116,7 +116,10 @@ class Review extends Repository public function countReviews() { - return (int)ReviewModel::count(['conditions' => 'deleted = 0']); + return (int)ReviewModel::count([ + 'conditions' => 'published = :published: AND deleted = 0', + 'bind' => ['published' => ReviewModel::PUBLISH_APPROVED], + ]); } public function countLikes($reviewId) diff --git a/app/Repos/Topic.php b/app/Repos/Topic.php index 771768cf..c6e80a91 100644 --- a/app/Repos/Topic.php +++ b/app/Repos/Topic.php @@ -96,12 +96,15 @@ class Topic extends Repository ->join(CourseTopicModel::class, 'c.id = ct.course_id', 'ct') ->where('ct.topic_id = :topic_id:', ['topic_id' => $topicId]) ->andWhere('c.published = 1') + ->andWhere('c.deleted = 0') ->getQuery()->execute(); } public function countTopics() { - return (int)TopicModel::count(['conditions' => 'deleted = 0']); + return (int)TopicModel::count([ + 'conditions' => 'published = 1 AND deleted = 0', + ]); } public function countCourses($topicId) diff --git a/app/Repos/User.php b/app/Repos/User.php index 2858e4e8..917c18ff 100644 --- a/app/Repos/User.php +++ b/app/Repos/User.php @@ -102,6 +102,19 @@ class User extends Repository ]); } + /** + * @param int $id + * @return UserModel|Model|bool + */ + public function findShallowUserById($id) + { + return UserModel::findFirst([ + 'conditions' => 'id = :id:', + 'columns' => ['id', 'name', 'avatar', 'vip', 'title', 'about'], + 'bind' => ['id' => $id], + ]); + } + /** * @param array $ids * @param array|string $columns @@ -115,6 +128,18 @@ class User extends Repository ->execute(); } + /** + * @param array $ids + * @return ResultsetInterface|Resultset|UserModel[] + */ + public function findShallowUserByIds($ids) + { + return UserModel::query() + ->columns(['id', 'name', 'avatar', 'vip', 'title', 'about']) + ->inWhere('id', $ids) + ->execute(); + } + /** * @param int $userId * @return UserBalanceModel|Model @@ -163,12 +188,16 @@ class User extends Repository public function countUsers() { - return (int)UserModel::count(); + return (int)UserModel::count([ + 'conditions' => 'deleted = 0', + ]); } public function countVipUsers() { - return (int)UserModel::count(['conditions' => 'vip = 1']); + return (int)UserModel::count([ + 'conditions' => 'vip = 1 AND deleted = 0', + ]); } public function countCourses($userId) diff --git a/app/Services/Logic/Answer/AnswerInfo.php b/app/Services/Logic/Answer/AnswerInfo.php index 98e52b24..959ea1a3 100644 --- a/app/Services/Logic/Answer/AnswerInfo.php +++ b/app/Services/Logic/Answer/AnswerInfo.php @@ -11,14 +11,15 @@ use App\Models\Answer as AnswerModel; use App\Models\User as UserModel; use App\Repos\AnswerLike as AnswerLikeRepo; use App\Repos\Question as QuestionRepo; -use App\Repos\User as UserRepo; use App\Services\Logic\AnswerTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class AnswerInfo extends LogicService { use AnswerTrait; + use UserTrait; public function handle($id) { @@ -31,11 +32,15 @@ class AnswerInfo extends LogicService protected function handleAnswer(AnswerModel $answer, UserModel $user) { - $answer->content = kg_parse_markdown($answer->content); + $content = kg_parse_markdown($answer->content); - $result = [ + $question = $this->handleQuestionInfo($answer->question_id); + $owner = $this->handleShallowUserInfo($answer->owner_id); + $me = $this->handleMeInfo($answer, $user); + + return [ 'id' => $answer->id, - 'content' => $answer->content, + 'content' => $content, 'anonymous' => $answer->anonymous, 'accepted' => $answer->accepted, 'published' => $answer->published, @@ -44,20 +49,17 @@ class AnswerInfo extends LogicService 'like_count' => $answer->like_count, 'create_time' => $answer->create_time, 'update_time' => $answer->update_time, + 'question' => $question, + 'owner' => $owner, + 'me' => $me, ]; - - $result['question'] = $this->handleQuestionInfo($answer); - $result['owner'] = $this->handleOwnerInfo($answer); - $result['me'] = $this->handleMeInfo($answer, $user); - - return $result; } - protected function handleQuestionInfo(AnswerModel $answer) + protected function handleQuestionInfo($questionId) { $questionRepo = new QuestionRepo(); - $question = $questionRepo->findById($answer->question_id); + $question = $questionRepo->findById($questionId); return [ 'id' => $question->id, @@ -65,19 +67,6 @@ class AnswerInfo extends LogicService ]; } - protected function handleOwnerInfo(AnswerModel $answer) - { - $userRepo = new UserRepo(); - - $owner = $userRepo->findById($answer->owner_id); - - return [ - 'id' => $owner->id, - 'name' => $owner->name, - 'avatar' => $owner->avatar, - ]; - } - protected function handleMeInfo(AnswerModel $answer, UserModel $user) { $me = [ @@ -85,10 +74,7 @@ class AnswerInfo extends LogicService 'owned' => 0, ]; - $isOwner = $user->id == $answer->owner_id; - $approved = $answer->published = AnswerModel::PUBLISH_APPROVED; - - if ($isOwner || $approved) { + if ($user->id == $answer->owner_id) { $me['owned'] = 1; } diff --git a/app/Services/Logic/Article/ArticleInfo.php b/app/Services/Logic/Article/ArticleInfo.php index 3bd32656..dfe8fdf8 100644 --- a/app/Services/Logic/Article/ArticleInfo.php +++ b/app/Services/Logic/Article/ArticleInfo.php @@ -8,7 +8,6 @@ namespace App\Services\Logic\Article; use App\Caches\Category as CategoryCache; -use App\Caches\User as UserCache; use App\Models\Article as ArticleModel; use App\Models\Category as CategoryModel; use App\Models\User as UserModel; @@ -16,11 +15,13 @@ use App\Repos\ArticleFavorite as ArticleFavoriteRepo; use App\Repos\ArticleLike as ArticleLikeRepo; use App\Services\Logic\ArticleTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class ArticleInfo extends LogicService { use ArticleTrait; + use UserTrait; public function handle($id) { @@ -41,8 +42,8 @@ class ArticleInfo extends LogicService { $content = kg_parse_markdown($article->content); - $category = $this->handleCategoryInfo($article); - $owner = $this->handleOwnerInfo($article); + $category = $this->handleCategoryInfo($article->category_id); + $owner = $this->handleShallowUserInfo($article->owner_id); $me = $this->handleMeInfo($article, $user); return [ @@ -71,6 +72,23 @@ class ArticleInfo extends LogicService ]; } + protected function handleCategoryInfo($categoryId) + { + $cache = new CategoryCache(); + + /** + * @var CategoryModel $category + */ + $category = $cache->get($categoryId); + + if (!$category) return new \stdClass(); + + return [ + 'id' => $category->id, + 'name' => $category->name, + ]; + } + protected function handleMeInfo(ArticleModel $article, UserModel $user) { $me = [ @@ -79,14 +97,8 @@ class ArticleInfo extends LogicService 'owned' => 0, ]; - $isOwner = $user->id == $article->owner_id; - $approved = $article->published == ArticleModel::PUBLISH_APPROVED; - $public = $article->private == 0; - - if ($approved && $public) { + if ($user->id == $article->owner_id) { $me['owned'] = 1; - } else { - $me['owned'] = $isOwner ? 1 : 0; } if ($user->id > 0) { @@ -111,44 +123,6 @@ class ArticleInfo extends LogicService return $me; } - protected function handleCategoryInfo(ArticleModel $article) - { - $cache = new CategoryCache(); - - /** - * @var CategoryModel $category - */ - $category = $cache->get($article->category_id); - - if (!$category) return new \stdClass(); - - return [ - 'id' => $category->id, - 'name' => $category->name, - ]; - } - - protected function handleOwnerInfo(ArticleModel $article) - { - $cache = new UserCache(); - - /** - * @var UserModel $owner - */ - $owner = $cache->get($article->owner_id); - - if (!$owner) return new \stdClass(); - - return [ - 'id' => $owner->id, - 'name' => $owner->name, - 'avatar' => $owner->avatar, - 'title' => $owner->title, - 'about' => $owner->about, - 'vip' => $owner->vip, - ]; - } - protected function incrArticleViewCount(ArticleModel $article) { $article->view_count += 1; diff --git a/app/Services/Logic/Comment/CommentInfo.php b/app/Services/Logic/Comment/CommentInfo.php index 848c305d..b7887b5a 100644 --- a/app/Services/Logic/Comment/CommentInfo.php +++ b/app/Services/Logic/Comment/CommentInfo.php @@ -8,26 +8,32 @@ namespace App\Services\Logic\Comment; use App\Models\Comment as CommentModel; -use App\Repos\User as UserRepo; +use App\Models\User as UserModel; +use App\Repos\AnswerLike as AnswerLikeRepo; use App\Services\Logic\CommentTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class CommentInfo extends LogicService { use CommentTrait; + use UserTrait; public function handle($id) { $comment = $this->checkComment($id); - return $this->handleComment($comment); + $user = $this->getCurrentUser(true); + + return $this->handleComment($comment, $user); } - protected function handleComment(CommentModel $comment) + protected function handleComment(CommentModel $comment, UserModel $user) { - $owner = $comment->owner_id > 0 ? $this->handleOwnerInfo($comment) : new \stdClass(); - $toUser = $comment->to_user_id > 0 ? $this->handleToUserInfo($comment) : new \stdClass(); + $toUser = $this->handleShallowUserInfo($comment->to_user_id); + $owner = $this->handleShallowUserInfo($comment->owner_id); + $me = $this->handleMeInfo($comment, $user); return [ 'id' => $comment->id, @@ -39,35 +45,35 @@ class CommentInfo extends LogicService 'reply_count' => $comment->reply_count, 'create_time' => $comment->create_time, 'update_time' => $comment->update_time, - 'owner' => $owner, 'to_user' => $toUser, + 'owner' => $owner, + 'me' => $me, ]; } - protected function handleOwnerInfo(CommentModel $comment) + protected function handleMeInfo(CommentModel $comment, UserModel $user) { - $userRepo = new UserRepo(); - - $user = $userRepo->findById($comment->owner_id); - - return [ - 'id' => $user->id, - 'name' => $user->name, - 'avatar' => $user->avatar, + $me = [ + 'liked' => 0, + 'owned' => 0, ]; - } - protected function handleToUserInfo(CommentModel $comment) - { - $userRepo = new UserRepo(); + if ($user->id == $comment->owner_id) { + $me['owned'] = 1; + } - $user = $userRepo->findById($comment->to_user_id); + if ($user->id > 0) { - return [ - 'id' => $user->id, - 'name' => $user->name, - 'avatar' => $user->avatar, - ]; + $likeRepo = new AnswerLikeRepo(); + + $like = $likeRepo->findAnswerLike($comment->id, $user->id); + + if ($like && $like->deleted == 0) { + $me['liked'] = 1; + } + } + + return $me; } } diff --git a/app/Services/Logic/Consult/ConsultInfo.php b/app/Services/Logic/Consult/ConsultInfo.php index 15dc4e0f..cae47fc1 100644 --- a/app/Services/Logic/Consult/ConsultInfo.php +++ b/app/Services/Logic/Consult/ConsultInfo.php @@ -8,30 +8,41 @@ namespace App\Services\Logic\Consult; use App\Models\Consult as ConsultModel; +use App\Models\User as UserModel; use App\Repos\Chapter as ChapterRepo; +use App\Repos\ConsultLike as ConsultLikeRepo; use App\Repos\Course as CourseRepo; -use App\Repos\User as UserRepo; use App\Services\Logic\ConsultTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class ConsultInfo extends LogicService { use ConsultTrait; + use UserTrait; public function handle($id) { $consult = $this->checkConsult($id); - return $this->handleConsult($consult); + $user = $this->getCurrentUser(true); + + return $this->handleConsult($consult, $user); } - protected function handleConsult(ConsultModel $consult) + protected function handleConsult(ConsultModel $consult, UserModel $user) { - $result = [ + $course = $this->handleCourseInfo($consult->course_id); + $chapter = $this->handleChapterInfo($consult->chapter_id); + $replier = $this->handleShallowUserInfo($consult->replier_id); + $owner = $this->handleShallowUserInfo($consult->owner_id); + $me = $this->handleMeInfo($consult, $user); + + return [ 'id' => $consult->id, 'question' => $consult->question, - 'answer' => $consult->answer, + 'consult' => $consult->consult, 'rating' => $consult->rating, 'private' => $consult->private, 'published' => $consult->published, @@ -39,21 +50,19 @@ class ConsultInfo extends LogicService 'like_count' => $consult->like_count, 'create_time' => $consult->create_time, 'update_time' => $consult->update_time, + 'course' => $course, + 'chapter' => $chapter, + 'replier' => $replier, + 'owner' => $owner, + 'me' => $me, ]; - - $result['course'] = $this->handleCourseInfo($consult); - $result['chapter'] = $this->handleChapterInfo($consult); - $result['owner'] = $this->handleOwnerInfo($consult); - $result['replier'] = $this->handleReplierInfo($consult); - - return $result; } - protected function handleCourseInfo(ConsultModel $consult) + protected function handleCourseInfo($courseId) { $courseRepo = new CourseRepo(); - $course = $courseRepo->findById($consult->course_id); + $course = $courseRepo->findById($courseId); if (!$course) return new \stdClass(); @@ -64,11 +73,11 @@ class ConsultInfo extends LogicService ]; } - protected function handleChapterInfo(ConsultModel $consult) + protected function handleChapterInfo($chapterId) { $chapterRepo = new ChapterRepo(); - $chapter = $chapterRepo->findById($consult->chapter_id); + $chapter = $chapterRepo->findById($chapterId); if (!$chapter) return new \stdClass(); @@ -78,34 +87,29 @@ class ConsultInfo extends LogicService ]; } - protected function handleOwnerInfo(ConsultModel $consult) + protected function handleMeInfo(ConsultModel $consult, UserModel $user) { - $userRepo = new UserRepo(); - - $owner = $userRepo->findById($consult->owner_id); - - if (!$owner) return new \stdClass(); - - return [ - 'id' => $owner->id, - 'name' => $owner->name, - 'avatar' => $owner->avatar, + $me = [ + 'liked' => 0, + 'owned' => 0, ]; - } - protected function handleReplierInfo(ConsultModel $consult) - { - $userRepo = new UserRepo(); + if ($user->id == $consult->owner_id) { + $me['owned'] = 1; + } - $replier = $userRepo->findById($consult->replier_id); + if ($user->id > 0) { - if (!$replier) return new \stdClass(); + $likeRepo = new ConsultLikeRepo(); - return [ - 'id' => $replier->id, - 'name' => $replier->name, - 'avatar' => $replier->avatar, - ]; + $like = $likeRepo->findConsultLike($consult->id, $user->id); + + if ($like && $like->deleted == 0) { + $me['liked'] = 1; + } + } + + return $me; } } diff --git a/app/Services/Logic/Danmu/DanmuInfo.php b/app/Services/Logic/Danmu/DanmuInfo.php index 54ec09a4..9095e23a 100644 --- a/app/Services/Logic/Danmu/DanmuInfo.php +++ b/app/Services/Logic/Danmu/DanmuInfo.php @@ -8,14 +8,15 @@ namespace App\Services\Logic\Danmu; use App\Models\Danmu as DanmuModel; -use App\Repos\User as UserRepo; use App\Services\Logic\DanmuTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class DanmuInfo extends LogicService { use DanmuTrait; + use UserTrait; public function handle($id) { @@ -26,26 +27,17 @@ class DanmuInfo extends LogicService protected function handleDanmu(DanmuModel $danmu) { - $result = [ + $owner = $this->handleShallowUserInfo($danmu->owner_id); + + return [ 'id' => $danmu->id, 'text' => $danmu->text, 'color' => $danmu->color, 'size' => $danmu->size, 'position' => $danmu->position, 'time' => $danmu->time, + 'owner' => $owner, ]; - - $userRepo = new UserRepo(); - - $owner = $userRepo->findById($danmu->user_id); - - $result['owner'] = [ - 'id' => $owner->id, - 'name' => $owner->name, - 'avatar' => $owner->avatar, - ]; - - return $result; } } diff --git a/app/Services/Logic/Order/OrderInfo.php b/app/Services/Logic/Order/OrderInfo.php index 6e63de78..60d59789 100644 --- a/app/Services/Logic/Order/OrderInfo.php +++ b/app/Services/Logic/Order/OrderInfo.php @@ -9,37 +9,38 @@ namespace App\Services\Logic\Order; use App\Models\Course as CourseModel; use App\Models\Order as OrderModel; +use App\Models\User as UserModel; use App\Repos\Order as OrderRepo; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; use App\Validators\Order as OrderValidator; class OrderInfo extends LogicService { + use UserTrait; + public function handle($sn) { $validator = new OrderValidator(); $order = $validator->checkOrderBySn($sn); - return $this->handleOrder($order); + $user = $this->getLoginUser(); + + return $this->handleOrder($order, $user); } - protected function handleOrder(OrderModel $order) + protected function handleOrder(OrderModel $order, UserModel $user) { $order->item_info = $this->handleItemInfo($order); - $statusHistory = $this->handleStatusHistory($order->id); - - $me = $this->handleMeInfo($order); - - return [ - 'me' => $me, + $result = [ 'sn' => $order->sn, 'subject' => $order->subject, 'amount' => $order->amount, 'status' => $order->status, - 'status_history' => $statusHistory, + 'deleted' => $order->deleted, 'item_id' => $order->item_id, 'item_type' => $order->item_type, 'item_info' => $order->item_info, @@ -49,6 +50,12 @@ class OrderInfo extends LogicService 'create_time' => $order->create_time, 'update_time' => $order->update_time, ]; + + $result['status_history'] = $this->handleStatusHistory($order->id); + $result['owner'] = $this->handleShallowUserInfo($order->owner_id); + $result['me'] = $this->handleMeInfo($order, $user); + + return $result; } protected function handleStatusHistory($orderId) @@ -71,14 +78,19 @@ class OrderInfo extends LogicService return $result; } - protected function handleMeInfo(OrderModel $order) + protected function handleMeInfo(OrderModel $order, UserModel $user) { $result = [ + 'owned' => 0, 'allow_pay' => 0, 'allow_cancel' => 0, 'allow_refund' => 0, ]; + if ($user->id == $order->owner_id) { + $result['owned'] = 1; + } + if ($order->status == OrderModel::STATUS_PENDING) { $result['allow_pay'] = 1; $result['allow_cancel'] = 1; diff --git a/app/Services/Logic/Question/AnswerList.php b/app/Services/Logic/Question/AnswerList.php index 71a79a3d..7e614348 100644 --- a/app/Services/Logic/Question/AnswerList.php +++ b/app/Services/Logic/Question/AnswerList.php @@ -9,7 +9,7 @@ namespace App\Services\Logic\Question; use App\Builders\AnswerList as AnswerListBuilder; use App\Library\Paginator\Query as PagerQuery; -use App\Models\Question as QuestionModel; +use App\Models\Answer as AnswerModel; use App\Repos\Answer as AnswerRepo; use App\Repos\AnswerLike as AnswerLikeRepo; use App\Services\Logic\QuestionTrait; @@ -29,7 +29,7 @@ class AnswerList extends LogicService $params = $pagerQuery->getParams(); $params['question_id'] = $question->id; - $params['published'] = QuestionModel::PUBLISH_APPROVED; + $params['published'] = AnswerModel::PUBLISH_APPROVED; $params['deleted'] = 0; $sort = $pagerQuery->getSort(); @@ -63,7 +63,8 @@ class AnswerList extends LogicService $answer['content'] = kg_parse_markdown($answer['content']); - $owner = $users[$answer['owner_id']] ?? new \stdClass(); + $owner = $users[$answer['owner_id']]; + $me = $meMappings[$answer['id']]; $items[] = [ diff --git a/app/Services/Logic/Question/QuestionInfo.php b/app/Services/Logic/Question/QuestionInfo.php index 07c8ba47..37ea2e06 100644 --- a/app/Services/Logic/Question/QuestionInfo.php +++ b/app/Services/Logic/Question/QuestionInfo.php @@ -14,14 +14,15 @@ use App\Models\User as UserModel; use App\Repos\Question as QuestionRepo; use App\Repos\QuestionFavorite as QuestionFavoriteRepo; use App\Repos\QuestionLike as QuestionLikeRepo; -use App\Repos\User as UserRepo; use App\Services\Logic\QuestionTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class QuestionInfo extends LogicService { use QuestionTrait; + use UserTrait; public function handle($id) { @@ -42,9 +43,9 @@ class QuestionInfo extends LogicService { $content = kg_parse_markdown($question->content); - $lastReplier = $this->handleUserInfo($question->last_replier_id); - $category = $this->handleCategoryInfo($question); - $owner = $this->handleUserInfo($question->owner_id); + $lastReplier = $this->handleShallowUserInfo($question->last_replier_id); + $category = $this->handleCategoryInfo($question->category_id); + $owner = $this->handleShallowUserInfo($question->owner_id); $me = $this->handleMeInfo($question, $user); return [ @@ -74,14 +75,14 @@ class QuestionInfo extends LogicService ]; } - protected function handleCategoryInfo(QuestionModel $question) + protected function handleCategoryInfo($categoryId) { $cache = new CategoryCache(); /** * @var CategoryModel $category */ - $category = $cache->get($question->category_id); + $category = $cache->get($categoryId); if (!$category) return new \stdClass(); @@ -138,24 +139,6 @@ class QuestionInfo extends LogicService return $me; } - protected function handleUserInfo($userId) - { - $userRepo = new UserRepo(); - - $user = $userRepo->findById($userId); - - if (!$user) return new \stdClass(); - - return [ - 'id' => $user->id, - 'name' => $user->name, - 'avatar' => $user->avatar, - 'title' => $user->title, - 'about' => $user->about, - 'vip' => $user->vip, - ]; - } - protected function incrQuestionViewCount(QuestionModel $question) { $question->view_count += 1; diff --git a/app/Services/Logic/Refund/RefundInfo.php b/app/Services/Logic/Refund/RefundInfo.php index d1343b69..f5f3c7cb 100644 --- a/app/Services/Logic/Refund/RefundInfo.php +++ b/app/Services/Logic/Refund/RefundInfo.php @@ -8,38 +8,49 @@ namespace App\Services\Logic\Refund; use App\Models\Refund as RefundModel; +use App\Models\User as UserModel; use App\Repos\Order as OrderRepo; use App\Repos\Refund as RefundRepo; use App\Services\Logic\RefundTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class RefundInfo extends LogicService { use RefundTrait; + use UserTrait; public function handle($sn) { $refund = $this->checkRefundBySn($sn); - return $this->handleRefund($refund); + $user = $this->getLoginUser(); + + return $this->handleRefund($refund, $user); } - protected function handleRefund(RefundModel $refund) + protected function handleRefund(RefundModel $refund, UserModel $user) { - $order = $this->handleOrderInfo($refund->order_id); - $statusHistory = $this->handleStatusHistory($refund->id); + $order = $this->handleOrderInfo($refund->order_id); + $owner = $this->handleShallowUserInfo($refund->owner_id); + $me = $this->handleMeInfo($refund, $user); return [ - 'order' => $order, 'sn' => $refund->sn, 'subject' => $refund->subject, 'amount' => $refund->amount, 'status' => $refund->status, - 'status_history' => $statusHistory, + 'deleted' => $refund->deleted, 'apply_note' => $refund->apply_note, 'review_note' => $refund->review_note, + 'create_time' => $refund->create_time, + 'update_time' => $refund->update_time, + 'status_history' => $statusHistory, + 'order' => $order, + 'owner' => $owner, + 'me' => $me, ]; } @@ -79,4 +90,27 @@ class RefundInfo extends LogicService return $result; } + protected function handleMeInfo(RefundModel $refund, UserModel $user) + { + $result = [ + 'owned' => 0, + 'allow_cancel' => 0, + ]; + + if ($user->id == $refund->owner_id) { + $result['owned'] = 1; + } + + $statusTypes = [ + RefundModel::STATUS_PENDING, + RefundModel::STATUS_APPROVED, + ]; + + if (in_array($refund->status, $statusTypes)) { + $result['allow_cancel'] = 1; + } + + return $result; + } + } diff --git a/app/Services/Logic/Review/ReviewInfo.php b/app/Services/Logic/Review/ReviewInfo.php index 08f717aa..7a2ac9c9 100644 --- a/app/Services/Logic/Review/ReviewInfo.php +++ b/app/Services/Logic/Review/ReviewInfo.php @@ -8,26 +8,35 @@ namespace App\Services\Logic\Review; use App\Models\Review as ReviewModel; +use App\Models\User as UserModel; use App\Repos\Course as CourseRepo; -use App\Repos\User as UserRepo; +use App\Repos\ReviewLike as ReviewLikeRepo; use App\Services\Logic\ReviewTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class ReviewInfo extends LogicService { use ReviewTrait; + use UserTrait; public function handle($id) { $review = $this->checkReview($id); - return $this->handleReview($review); + $user = $this->getCurrentUser(); + + return $this->handleReview($review, $user); } - protected function handleReview(ReviewModel $review) + protected function handleReview(ReviewModel $review, UserModel $user) { - $result = [ + $course = $this->handleCourseInfo($review->course_id); + $owner = $this->handleShallowUserInfo($review->owner_id); + $me = $this->handleMeInfo($review, $user); + + return [ 'id' => $review->id, 'content' => $review->content, 'reply' => $review->reply, @@ -40,19 +49,17 @@ class ReviewInfo extends LogicService 'like_count' => $review->like_count, 'create_time' => $review->create_time, 'update_time' => $review->update_time, + 'course' => $course, + 'owner' => $owner, + 'me' => $me, ]; - - $result['course'] = $this->handleCourseInfo($review); - $result['owner'] = $this->handleOwnerInfo($review); - - return $result; } - protected function handleCourseInfo(ReviewModel $review) + protected function handleCourseInfo($courseId) { $courseRepo = new CourseRepo(); - $course = $courseRepo->findById($review->course_id); + $course = $courseRepo->findById($courseId); if (!$course) return new \stdClass(); @@ -63,19 +70,29 @@ class ReviewInfo extends LogicService ]; } - protected function handleOwnerInfo(ReviewModel $review) + protected function handleMeInfo(ReviewModel $review, UserModel $user) { - $userRepo = new UserRepo(); - - $owner = $userRepo->findById($review->owner_id); - - if (!$owner) return new \stdClass(); - - return [ - 'id' => $owner->id, - 'name' => $owner->name, - 'avatar' => $owner->avatar, + $me = [ + 'liked' => 0, + 'owned' => 0, ]; + + if ($user->id == $review->owner_id) { + $me['owned'] = 1; + } + + if ($user->id > 0) { + + $likeRepo = new ReviewLikeRepo(); + + $like = $likeRepo->findReviewLike($review->id, $user->id); + + if ($like && $like->deleted == 0) { + $me['liked'] = 1; + } + } + + return $me; } } diff --git a/app/Services/Logic/Trade/TradeInfo.php b/app/Services/Logic/Trade/TradeInfo.php index 873ae91f..d6dc3986 100644 --- a/app/Services/Logic/Trade/TradeInfo.php +++ b/app/Services/Logic/Trade/TradeInfo.php @@ -8,31 +8,101 @@ namespace App\Services\Logic\Trade; use App\Models\Trade as TradeModel; +use App\Models\User as UserModel; +use App\Repos\Order as OrderRepo; +use App\Repos\Trade as TradeRepo; use App\Services\Logic\Service as LogicService; use App\Services\Logic\TradeTrait; +use App\Services\Logic\UserTrait; class TradeInfo extends LogicService { use TradeTrait; + use UserTrait; public function handle($sn) { $trade = $this->checkTradeBySn($sn); - return $this->handleTrade($trade); + $user = $this->getCurrentUser(true); + + return $this->handleTrade($trade, $user); } - protected function handleTrade(TradeModel $trade) + protected function handleTrade(TradeModel $trade, UserModel $user) { + $statusHistory = $this->handleStatusHistory($trade->id); + $owner = $this->handleShallowUserInfo($trade->owner_id); + $me = $this->handleMeInfo($trade, $user); + return [ 'sn' => $trade->sn, 'subject' => $trade->subject, 'amount' => $trade->amount, 'channel' => $trade->channel, 'status' => $trade->status, + 'deleted' => $trade->deleted, 'create_time' => $trade->create_time, + 'update_time' => $trade->update_time, + 'status_history' => $statusHistory, + 'owner' => $owner, + 'me' => $me, ]; } + protected function handleOrderInfo($orderId) + { + $orderRepo = new OrderRepo(); + + $order = $orderRepo->findById($orderId); + + return [ + 'id' => $order->id, + 'sn' => $order->sn, + 'subject' => $order->subject, + 'amount' => $order->amount, + ]; + } + + protected function handleStatusHistory($tradeId) + { + $tradeRepo = new TradeRepo(); + + $records = $tradeRepo->findStatusHistory($tradeId); + + if ($records->count() == 0) { + return []; + } + + $result = []; + + foreach ($records as $record) { + $result[] = [ + 'status' => $record->status, + 'create_time' => $record->create_time, + ]; + } + + return $result; + } + + protected function handleMeInfo(TradeModel $trade, UserModel $user) + { + $result = [ + 'owned' => 0, + 'allow_cancel' => 0, + ]; + + if ($user->id == $trade->owner_id) { + $result['owned'] = 1; + } + + if ($trade->status == TradeModel::STATUS_PENDING) { + $result['allow_cancel'] = 1; + } + + return $result; + } + } diff --git a/app/Services/Logic/UserTrait.php b/app/Services/Logic/UserTrait.php index 6631cd2b..68dbf048 100644 --- a/app/Services/Logic/UserTrait.php +++ b/app/Services/Logic/UserTrait.php @@ -7,6 +7,7 @@ namespace App\Services\Logic; +use App\Repos\User as UserRepo; use App\Validators\User as UserValidator; trait UserTrait @@ -26,4 +27,17 @@ trait UserTrait return $validator->checkUserCache($id); } + public function handleShallowUserInfo($id) + { + $userRepo = new UserRepo(); + + $user = $userRepo->findShallowUserById($id); + + $result = $user->toArray(); + + $result['avatar'] = kg_cos_user_avatar_url($user->avatar); + + return $result; + } + } diff --git a/app/Traits/Response.php b/app/Traits/Response.php index 77f7363c..9ba736c2 100644 --- a/app/Traits/Response.php +++ b/app/Traits/Response.php @@ -7,45 +7,24 @@ namespace App\Traits; +use App\Exceptions\Forbidden as ForbiddenException; +use App\Exceptions\NotFound as NotFoundException; use Phalcon\Config; use Phalcon\Di; use Phalcon\Http\Request as HttpRequest; use Phalcon\Http\Response as HttpResponse; -use Phalcon\Mvc\Dispatcher; trait Response { public function forbidden() { - /** - * @var Dispatcher $dispatcher - */ - $dispatcher = Di::getDefault()->getShared('dispatcher'); - - $dispatcher->forward([ - 'module' => 'home', - 'controller' => 'error', - 'action' => 'show403', - ]); - - return false; + throw new ForbiddenException('sys.forbidden'); } public function notFound() { - /** - * @var Dispatcher $dispatcher - */ - $dispatcher = Di::getDefault()->getShared('dispatcher'); - - $dispatcher->forward([ - 'module' => 'home', - 'controller' => 'error', - 'action' => 'show404', - ]); - - return false; + throw new NotFoundException('sys.not_found'); } public function setCors() From 93279a53e25f4185f64de64abd40552688d9e5f1 Mon Sep 17 00:00:00 2001 From: koogua Date: Wed, 6 Oct 2021 18:39:50 +0800 Subject: [PATCH 17/21] =?UTF-8?q?=E5=AE=8C=E5=96=84shallowUserInfo?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Logic/UserTrait.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Services/Logic/UserTrait.php b/app/Services/Logic/UserTrait.php index 68dbf048..2f379c30 100644 --- a/app/Services/Logic/UserTrait.php +++ b/app/Services/Logic/UserTrait.php @@ -29,10 +29,14 @@ trait UserTrait public function handleShallowUserInfo($id) { + if (empty($id)) return new \stdClass(); + $userRepo = new UserRepo(); $user = $userRepo->findShallowUserById($id); + if (!$user) return new \stdClass(); + $result = $user->toArray(); $result['avatar'] = kg_cos_user_avatar_url($user->avatar); From febf7d9982ceca0b2d0c1d0598bd3222a8d5566c Mon Sep 17 00:00:00 2001 From: koogua Date: Wed, 6 Oct 2021 20:28:13 +0800 Subject: [PATCH 18/21] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootstrap/HttpErrorHandler.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bootstrap/HttpErrorHandler.php b/bootstrap/HttpErrorHandler.php index dd8fd8af..4d5cf821 100644 --- a/bootstrap/HttpErrorHandler.php +++ b/bootstrap/HttpErrorHandler.php @@ -127,6 +127,13 @@ class HttpErrorHandler extends Injectable { $errors = require config_path('errors.php'); + /** + * 向外隐藏具体的500异常信息 + */ + if ($this->response->getStatusCode() == 500) { + $code = 'sys.internal_server_error'; + } + return [ 'code' => $code, 'msg' => $errors[$code] ?? $code, From 2a305897162efc833ad1f7b1ac933b910ecfa443 Mon Sep 17 00:00:00 2001 From: koogua Date: Thu, 7 Oct 2021 13:16:56 +0800 Subject: [PATCH 19/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=BE=A4=E7=BB=84?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Logic/Consult/ConsultInfo.php | 2 +- app/Services/Logic/Im/GroupInfo.php | 53 +++++++++++++++++----- bootstrap/HttpErrorHandler.php | 7 --- config/errors.php | 2 +- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/app/Services/Logic/Consult/ConsultInfo.php b/app/Services/Logic/Consult/ConsultInfo.php index cae47fc1..784ccece 100644 --- a/app/Services/Logic/Consult/ConsultInfo.php +++ b/app/Services/Logic/Consult/ConsultInfo.php @@ -42,7 +42,7 @@ class ConsultInfo extends LogicService return [ 'id' => $consult->id, 'question' => $consult->question, - 'consult' => $consult->consult, + 'answer' => $consult->answer, 'rating' => $consult->rating, 'private' => $consult->private, 'published' => $consult->published, diff --git a/app/Services/Logic/Im/GroupInfo.php b/app/Services/Logic/Im/GroupInfo.php index 4074ec42..abc22a52 100644 --- a/app/Services/Logic/Im/GroupInfo.php +++ b/app/Services/Logic/Im/GroupInfo.php @@ -7,22 +7,32 @@ namespace App\Services\Logic\Im; -use App\Repos\User as UserRepo; +use App\Models\ImGroup as ImGroupModel; +use App\Models\User as UserModel; +use App\Repos\ImGroupUser as ImGroupUserRepo; use App\Services\Logic\ImGroupTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\UserTrait; class GroupInfo extends LogicService { use ImGroupTrait; + use UserTrait; public function handle($id) { $group = $this->checkImGroup($id); - $userRepo = new UserRepo(); + $user = $this->getCurrentUser(true); - $owner = $userRepo->findById($group->owner_id); + return $this->handleGroup($group, $user); + } + + protected function handleGroup(ImGroupModel $group, UserModel $user) + { + $owner = $this->handleShallowUserInfo($group->owner_id); + $me = $this->handleMeInfo($group, $user); return [ 'id' => $group->id, @@ -30,17 +40,38 @@ class GroupInfo extends LogicService 'name' => $group->name, 'avatar' => $group->avatar, 'about' => $group->about, + 'published' => $group->published, + 'deleted' => $group->deleted, 'user_count' => $group->user_count, 'msg_count' => $group->msg_count, - 'owner' => [ - 'id' => $owner->id, - 'name' => $owner->name, - 'avatar' => $owner->avatar, - 'title' => $owner->title, - 'about' => $owner->about, - 'vip' => $owner->vip, - ], + 'owner' => $owner, + 'me' => $me, ]; } + protected function handleMeInfo(ImGroupModel $group, UserModel $user) + { + $me = [ + 'joined' => 0, + 'owned' => 0, + ]; + + if ($user->id == $group->owner_id) { + $me['owned'] = 1; + } + + if ($user->id > 0) { + + $repo = new ImGroupUserRepo(); + + $groupUser = $repo->findGroupUser($group->id, $user->id); + + if ($groupUser) { + $me['joined'] = 1; + } + } + + return $me; + } + } diff --git a/bootstrap/HttpErrorHandler.php b/bootstrap/HttpErrorHandler.php index 4d5cf821..dd8fd8af 100644 --- a/bootstrap/HttpErrorHandler.php +++ b/bootstrap/HttpErrorHandler.php @@ -127,13 +127,6 @@ class HttpErrorHandler extends Injectable { $errors = require config_path('errors.php'); - /** - * 向外隐藏具体的500异常信息 - */ - if ($this->response->getStatusCode() == 500) { - $code = 'sys.internal_server_error'; - } - return [ 'code' => $code, 'msg' => $errors[$code] ?? $code, diff --git a/config/errors.php b/config/errors.php index 90855cbe..5289cc99 100644 --- a/config/errors.php +++ b/config/errors.php @@ -14,7 +14,7 @@ $error['sys.unauthorized'] = '认证失败'; $error['sys.forbidden'] = '拒绝访问'; $error['sys.bad_request'] = '无效的请求'; $error['sys.not_found'] = '资源不存在'; -$error['sys.internal_server_error'] = '内部错误'; +$error['sys.server_error'] = '服务器内部错误'; $error['sys.service_unavailable'] = '服务不可用'; $error['sys.trans_rollback'] = '事务回滚'; $error['sys.unknown_error'] = '未知错误'; From e2e3479851b210651f6da31f8082771e66c12a7b Mon Sep 17 00:00:00 2001 From: koogua Date: Thu, 7 Oct 2021 14:47:27 +0800 Subject: [PATCH 20/21] =?UTF-8?q?=E7=94=A8=E4=BC=9A=E9=80=80=E6=AC=BE?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E4=B8=AD=E5=A2=9E=E5=8A=A0me=E5=B1=9E?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Builders/RefundList.php | 19 +++++++++++++++++++ .../Logic/User/Console/RefundList.php | 5 ++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/Builders/RefundList.php b/app/Builders/RefundList.php index 396bdd44..6452288d 100644 --- a/app/Builders/RefundList.php +++ b/app/Builders/RefundList.php @@ -7,6 +7,7 @@ namespace App\Builders; +use App\Models\Refund as RefundModel; use App\Repos\Order as OrderRepo; use App\Repos\User as UserRepo; @@ -35,6 +36,24 @@ class RefundList extends Builder return $refunds; } + public function handleMeInfo(array $refund) + { + $me = [ + 'allow_cancel' => 0, + ]; + + $statusTypes = [ + RefundModel::STATUS_PENDING, + RefundModel::STATUS_APPROVED, + ]; + + if (in_array($refund['status'], $statusTypes)) { + $me['allow_cancel'] = 1; + } + + return $me; + } + public function getOrders(array $trades) { $ids = kg_array_column($trades, 'order_id'); diff --git a/app/Services/Logic/User/Console/RefundList.php b/app/Services/Logic/User/Console/RefundList.php index 09704ef9..dbfb4ae9 100644 --- a/app/Services/Logic/User/Console/RefundList.php +++ b/app/Services/Logic/User/Console/RefundList.php @@ -65,8 +65,9 @@ class RefundList extends LogicService $order = $orders[$refund['order_id']] ?? new \stdClass(); + $me = $builder->handleMeInfo($refund); + $items[] = [ - 'order' => $order, 'sn' => $refund['sn'], 'subject' => $refund['subject'], 'amount' => (float)$refund['amount'], @@ -75,6 +76,8 @@ class RefundList extends LogicService 'review_note' => $refund['review_note'], 'create_time' => $refund['create_time'], 'update_time' => $refund['update_time'], + 'order' => $order, + 'me' => $me, ]; } From 8abe4d1e8e7e2e10a4adaf20084333dafc223dc8 Mon Sep 17 00:00:00 2001 From: koogua Date: Thu, 7 Oct 2021 15:58:06 +0800 Subject: [PATCH 21/21] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=97=AE=E9=A2=98me?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Home/Views/question/show.volt | 7 ++++++- app/Services/Logic/Question/QuestionInfo.php | 15 +++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/Http/Home/Views/question/show.volt b/app/Http/Home/Views/question/show.volt index edd6de95..e7d9f36b 100644 --- a/app/Http/Home/Views/question/show.volt +++ b/app/Http/Home/Views/question/show.volt @@ -64,11 +64,16 @@ {% endif %}
- {% if question.me.answered == 0 and question.published == 2 %} + {% if question.me.allow_answer == 1 %}
{% endif %} + {% if question.closed == 1 %} +
+ +
+ {% endif %} {% if answer_id > 0 %}
diff --git a/app/Services/Logic/Question/QuestionInfo.php b/app/Services/Logic/Question/QuestionInfo.php index 37ea2e06..ac11829a 100644 --- a/app/Services/Logic/Question/QuestionInfo.php +++ b/app/Services/Logic/Question/QuestionInfo.php @@ -95,18 +95,19 @@ class QuestionInfo extends LogicService protected function handleMeInfo(QuestionModel $question, UserModel $user) { $me = [ + 'allow_answer' => 0, 'liked' => 0, 'favorited' => 0, 'answered' => 0, 'owned' => 0, ]; - $isOwner = $user->id == $question->owner_id; + $approved = $question->published == QuestionModel::PUBLISH_APPROVED; + $closed = $question->closed == 1; + $solved = $question->solved == 1; - if ($question->published == QuestionModel::PUBLISH_APPROVED) { + if ($user->id == $question->owner_id) { $me['owned'] = 1; - } else { - $me['owned'] = $isOwner ? 1 : 0; } if ($user->id > 0) { @@ -134,6 +135,12 @@ class QuestionInfo extends LogicService if ($userAnswers->count() > 0) { $me['answered'] = 1; } + + $answered = $me['answered'] == 1; + + if ($approved && !$closed && !$solved && !$answered) { + $me['allow_answer'] = 1; + } } return $me;