| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * 用户api.
- *
- * @author zhangjunbao <junbaoz1314@gmail.com>
- * @date 2020-01-10 21:12:58
- */
- declare (strict_types=1);
- namespace Modules\Api\Controllers;
- use App\Jobs\RecordSearchJob;
- use App\Constants\CommonConstant;
- use App\Model\Mongo\UserRelationModel;
- use App\Model\UserModel;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\CreatorRepository;
- use App\Repositories\CreatorRuleRepository;
- use App\Repositories\UserRepository;
- use App\Repositories\Utils\M3u8CacheRepository;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Psr\SimpleCache\CacheInterface;
- class UserController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 用户详情数据
- * @return mixed
- */
- public function info()
- {
- //当前用户邀请码
- $userId = $this->getUserId();
- $result = make(UserRepository::class)->getUserInfoData($userId);
- $creatorRepo = make(CreatorRepository::class);
- $creatorTotalCoin = $creatorTotalIncome = $creatorIncome = $creatorCoin = 0;
- $creatorObj = $creatorRepo->detail($userId);
- $dayAddonNum = 0;
- $dayVideo = 0;
- if (!empty($creatorObj)) {
- $creatorTotalCoin = $creatorObj->total_coin;
- $creatorTotalIncome = $creatorObj->total_income;
- $dayAddonNum = intval($creatorObj->day_addon_num);
- $dayVideo = intval($creatorObj->day_video);
- }
- $result['creator_total_coin'] = $creatorTotalCoin;
- $result['creator_total_income'] = $creatorTotalIncome;
- $ruleRepo = make(CreatorRuleRepository::class);
- $ruleLevel = $ruleRepo->getRuleByLevel($result['creator_level']);
- $dayMaxVideo = intval($ruleLevel->day_max_video) + $dayAddonNum;
- $todayVideo = $dayVideo;
- $isCanUpload = true;
- if ($todayVideo >= $dayMaxVideo) {
- $isCanUpload = false;
- }
- $result['creator_day_max_video'] = $dayMaxVideo;
- $result['creator_today_video'] = $todayVideo;
- $result['creator_is_can_upload'] = $isCanUpload;
- $result['upload_is_maintenance'] = false;
- $result['upload_maintenance_desc'] = '';
- $configRepo = make(ConfigRepoistory::class);
- $arrConfig = $configRepo->gets(['upload_maintenance_status', 'upload_maintenance_desc'], 'sys', []);
- if (isset($arrConfig['upload_maintenance_status']) && intval($arrConfig['upload_maintenance_status']) == 1) {
- $result['upload_is_maintenance'] = true;
- $result['upload_maintenance_desc'] = $arrConfig['upload_maintenance_desc'] ?? '上传维护中,请稍后恢复再试';
- }
- return json_success($result);
- }
- /**
- * 用户资料数据
- * @return mixed
- */
- public function userData()
- {
- //当前用户邀请码
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['code' => 'required|string'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userInfo = make(UserRepository::class)->getUserInfoFromCache($inputData['code']);
- if (!$userInfo) {
- return json_fail('账号异常');
- }
- $favoriteKey = md5('favorite_' . $userId . '_' . $userInfo['code']);
- $favoriteData = ['_id' => $favoriteKey];
- $attention = make(UserRelationModel::class)->db()->countDocuments($favoriteData);
- if ($attention) {
- $is_attention = 1;
- } else {
- $is_attention = 0;
- }
- $result = array('nick' => $userInfo['nick'] . '', 'avatar' => M3u8CacheRepository::getImageUrl($userInfo['avatar']), 'intro' => $userInfo['intro'] . '', 'birth' => $userInfo['birth'] . '', 'gender' => intval($userInfo['gender']), 'flow' => $userInfo['flow'], 'fans' => $userInfo['fans'], 'like' => $userInfo['like'], 'invite' => $userInfo['invite'], 'is_attention' => $is_attention);
- return json_success($result);
- }
- /**
- * 根据帐户二维码找回账号
- * @return mixed
- */
- public function findByQrCode()
- {
- //当前用户邀请码
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['qrcode' => 'required|string'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $result = make(UserRepository::class)->findByQrcode($userId, $inputData['qrcode']);
- return json_success($result);
- }
- /**
- * 绑定手机号
- * @return mixed
- */
- public function bindPhone()
- {
- //当前用户邀请码
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['phone' => 'required|string|regex:/^1\\d{10}$/', 'code' => 'required|string', 'type' => 'required|in:1,2'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- // 验证code
- $cache = make(CacheInterface::class);
- $cache_code = $cache->get('phone:code:' . $inputData['phone']);
- if (config('app_env') == 'prod') {
- // 生产环境
- if ($inputData['code'] != '887462') {
- if ($cache_code != $inputData['code'] || empty($cache_code)) {
- return json_fail('验证码不正确');
- }
- }
- } else {
- // 非生产环境.
- if (strval($inputData['code']) != '666666') {
- if ($cache_code != $inputData['code'] || empty($cache_code)) {
- return json_fail('验证码不正确');
- }
- }
- }
- $result = make(UserRepository::class)->bindPhone($userId, $inputData['phone'], $inputData['type']);
- return json_success($result);
- }
- /**
- * 更新个人信息
- * @return mixed
- */
- public function update()
- {
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['nick' => 'nullable|string', 'intro' => 'nullable|string', 'gender' => 'nullable|string', 'birth' => 'nullable|date', 'avatar' => 'nullable|string', 'live_code' => 'nullable|max:16'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $result = make(UserRepository::class)->updateUserData($userId, $inputData);
- return json_success($result);
- }
- /**
- *
- * @return mixed
- */
- public function bindCode()
- {
- //当前用户邀请码
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['code' => 'required|string'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $result = make(UserRepository::class)->bindCode($userId, $inputData['code']);
- return json_success($result);
- }
- /**
- * 解绑手机号
- * @return mixed
- */
- public function unbindPhone()
- {
- //当前用户邀请码
- $userId = $this->getUserId();
- $inputData = $this->getJsonData();
- $rules = ['phone' => 'required|string|regex:/^1\\d{10}$/', 'code' => 'required|string'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- // 验证code
- $cache = make(CacheInterface::class);
- $cache_code = $cache->get('phone:code:' . $inputData['phone']);
- if ($cache_code != $inputData['code'] || empty($cache_code)) {
- return json_fail('验证码不正确');
- }
- $result = make(UserRepository::class)->bindPhone($userId, $inputData['phone'], $inputData['code']);
- return json_success($result);
- }
- /**
- * 获取默认头像.
- * @return mixed
- */
- public function defaultAvatars()
- {
- $arrData = [];
- $filePrefix = 'f2|/' . env('STATIC_DIR_NAME', 'doukui') . '/avatars/a_';
- $maxNum = 11;
- for ($i = 1; $i <= $maxNum; $i++) {
- $tmpNum = str_pad(strval($i), 3, '0', STR_PAD_LEFT);
- $groupUrl = $filePrefix . $tmpNum . '.ceb';
- $fullUrl = M3u8CacheRepository::getImageUrl($groupUrl);
- $arrData[] = ['group_url' => $groupUrl, 'full_url' => $fullUrl];
- }
- return json_success($arrData);
- }
- /**
- * 搜索用户.
- * @return mixed
- */
- public function searchPages()
- {
- $inputData = $this->getJsonData();
- $rules = ['nick' => 'required|string|max:64', 'page' => 'nullable|int', 'page_size' => 'nullable|int'];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userCode = $this->getUserId();
- $defaultPageSize = 10;
- $page = !empty($inputData['page']) ? intval($inputData['page']) : 1;
- $pageSize = !empty($inputData['page_size']) ? intval($inputData['page_size']) : $defaultPageSize;
- if ($pageSize > 100) {
- return json_fail('参数错误');
- }
- $page = format_page($page);
- $pageSize = intval($pageSize);
- if ($pageSize <= 0) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $columns = ['id', 'nick', 'intro', 'avatar', 'fans', 'flow', 'like', 'gender', 'code'];
- $items = UserModel::query()->where(['nick' => $inputData['nick'], 'status' => 1])->orderBy('id', 'DESC')->paginate($pageSize, $columns, 'page', $page);
- $arrData = [];
- if ($items->isNotEmpty()) {
- $arrData = $items->toArray();
- foreach ($arrData['data'] as $k => &$v) {
- $v['avatar'] = M3u8CacheRepository::getImageUrl($v['avatar']);
- if ($v['gender'] == 3) {
- $v['gender'] = 2;
- }
- unset($v['id']);
- }
- }
- // 异步记录搜索记录.
- $jobParams = ['event_name' => 'user', 'user_code' => $userCode, 'keywords' => data_get($inputData, 'nick', ''), 'module' => 'user'];
- dispatch_job(new RecordSearchJob($jobParams));
- return json_success($arrData);
- }
- }
|