UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * 用户api.
  4. *
  5. * @author zhangjunbao <junbaoz1314@gmail.com>
  6. * @date 2020-01-10 21:12:58
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Controllers;
  10. use App\Jobs\RecordSearchJob;
  11. use App\Constants\CommonConstant;
  12. use App\Model\Mongo\UserRelationModel;
  13. use App\Model\UserModel;
  14. use App\Repositories\ConfigRepoistory;
  15. use App\Repositories\CreatorRepository;
  16. use App\Repositories\CreatorRuleRepository;
  17. use App\Repositories\UserRepository;
  18. use App\Repositories\Utils\M3u8CacheRepository;
  19. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  20. use Psr\SimpleCache\CacheInterface;
  21. class UserController extends BaseController
  22. {
  23. /**
  24. * 用户详情数据
  25. * @return mixed
  26. */
  27. public function info()
  28. {
  29. //当前用户邀请码
  30. $userId = $this->getUserId();
  31. $result = make(UserRepository::class)->getUserInfoData($userId);
  32. $creatorRepo = make(CreatorRepository::class);
  33. $creatorTotalCoin = $creatorTotalIncome = $creatorIncome = $creatorCoin = 0;
  34. $creatorObj = $creatorRepo->detail($userId);
  35. $dayAddonNum = 0;
  36. $dayVideo = 0;
  37. if (!empty($creatorObj)) {
  38. $creatorTotalCoin = $creatorObj->total_coin;
  39. $creatorTotalIncome = $creatorObj->total_income;
  40. $dayAddonNum = intval($creatorObj->day_addon_num);
  41. $dayVideo = intval($creatorObj->day_video);
  42. }
  43. $result['creator_total_coin'] = $creatorTotalCoin;
  44. $result['creator_total_income'] = $creatorTotalIncome;
  45. $ruleRepo = make(CreatorRuleRepository::class);
  46. $ruleLevel = $ruleRepo->getRuleByLevel($result['creator_level']);
  47. $dayMaxVideo = intval($ruleLevel->day_max_video) + $dayAddonNum;
  48. $todayVideo = $dayVideo;
  49. $isCanUpload = true;
  50. if ($todayVideo >= $dayMaxVideo) {
  51. $isCanUpload = false;
  52. }
  53. $result['creator_day_max_video'] = $dayMaxVideo;
  54. $result['creator_today_video'] = $todayVideo;
  55. $result['creator_is_can_upload'] = $isCanUpload;
  56. $result['upload_is_maintenance'] = false;
  57. $result['upload_maintenance_desc'] = '';
  58. $configRepo = make(ConfigRepoistory::class);
  59. $arrConfig = $configRepo->gets(['upload_maintenance_status', 'upload_maintenance_desc'], 'sys', []);
  60. if (isset($arrConfig['upload_maintenance_status']) && intval($arrConfig['upload_maintenance_status']) == 1) {
  61. $result['upload_is_maintenance'] = true;
  62. $result['upload_maintenance_desc'] = $arrConfig['upload_maintenance_desc'] ?? '上传维护中,请稍后恢复再试';
  63. }
  64. return json_success($result);
  65. }
  66. /**
  67. * 用户资料数据
  68. * @return mixed
  69. */
  70. public function userData()
  71. {
  72. //当前用户邀请码
  73. $userId = $this->getUserId();
  74. $inputData = $this->getJsonData();
  75. $rules = [
  76. 'code' => 'required|string',
  77. ];
  78. $validator = make(ValidatorFactoryInterface::class)
  79. ->make($inputData, $rules);
  80. if ($validator->fails()) {
  81. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  82. return json_fail($validator->errors()->first());
  83. }
  84. $userInfo = make(UserRepository::class)
  85. ->getUserInfoFromCache($inputData['code']);
  86. if (!$userInfo) {
  87. return json_fail('账号异常');
  88. }
  89. $favoriteKey = md5('favorite_' . $userId . '_' . $userInfo['code']);
  90. $favoriteData = [
  91. '_id' => $favoriteKey
  92. ];
  93. $attention = make(UserRelationModel::class)->db()
  94. ->countDocuments($favoriteData);
  95. if ($attention) {
  96. $is_attention = 1;
  97. } else {
  98. $is_attention = 0;
  99. }
  100. $result = array(
  101. 'nick' => $userInfo['nick'].'',
  102. 'avatar'=> M3u8CacheRepository::getImageUrl($userInfo['avatar']),
  103. 'intro' => $userInfo['intro'].'',
  104. 'birth' => $userInfo['birth'].'',
  105. 'gender'=> intval($userInfo['gender']),
  106. 'flow' => $userInfo['flow'],
  107. 'fans' => $userInfo['fans'],
  108. 'like' => $userInfo['like'],
  109. 'invite'=> $userInfo['invite'],
  110. 'is_attention' => $is_attention
  111. );
  112. return json_success($result);
  113. }
  114. /**
  115. * 根据帐户二维码找回账号
  116. * @return mixed
  117. */
  118. public function findByQrCode()
  119. {
  120. //当前用户邀请码
  121. $userId = $this->getUserId();
  122. $inputData = $this->getJsonData();
  123. $rules = [
  124. 'qrcode' => 'required|string',
  125. ];
  126. $validator = make(ValidatorFactoryInterface::class)
  127. ->make($inputData, $rules);
  128. if ($validator->fails()) {
  129. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  130. return json_fail($validator->errors()->first());
  131. }
  132. $result = make(UserRepository::class)
  133. ->findByQrcode($userId, $inputData['qrcode']);
  134. return json_success($result);
  135. }
  136. /**
  137. * 绑定手机号
  138. * @return mixed
  139. */
  140. public function bindPhone()
  141. {
  142. //当前用户邀请码
  143. $userId = $this->getUserId();
  144. $inputData = $this->getJsonData();
  145. $rules = [
  146. 'phone' => 'required|string|regex:/^1\d{10}$/',
  147. 'code' => 'required|string',
  148. 'type' => 'required|in:1,2', //1,手机登陆;2,绑定手机号
  149. ];
  150. $validator = make(ValidatorFactoryInterface::class)
  151. ->make($inputData, $rules);
  152. if ($validator->fails()) {
  153. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  154. return json_fail($validator->errors()->first());
  155. }
  156. // 验证code
  157. $cache = make(CacheInterface::class);
  158. $cache_code = $cache->get('phone:code:' . $inputData['phone']);
  159. if (config('app_env') == 'prod') {
  160. // 生产环境
  161. if ($inputData['code'] != '887462') {
  162. if ($cache_code != $inputData['code'] || empty($cache_code)) {
  163. return json_fail('验证码不正确');
  164. }
  165. }
  166. } else {
  167. // 非生产环境.
  168. if (strval($inputData['code']) != '666666') {
  169. if ($cache_code != $inputData['code'] || empty($cache_code)) {
  170. return json_fail('验证码不正确');
  171. }
  172. }
  173. }
  174. $result = make(UserRepository::class)
  175. ->bindPhone($userId, $inputData['phone'], $inputData['type']);
  176. return json_success($result);
  177. }
  178. /**
  179. * 更新个人信息
  180. * @return mixed
  181. */
  182. public function update()
  183. {
  184. $userId = $this->getUserId();
  185. $inputData = $this->getJsonData();
  186. $rules = [
  187. 'nick' => 'nullable|string',
  188. 'intro' => 'nullable|string',
  189. 'gender' => 'nullable|string',
  190. 'birth' => 'nullable|date',
  191. 'avatar' => 'nullable|string',
  192. 'live_code' => 'nullable|max:16',
  193. ];
  194. $validator = make(ValidatorFactoryInterface::class)
  195. ->make($inputData, $rules);
  196. if ($validator->fails()) {
  197. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  198. return json_fail($validator->errors()->first());
  199. }
  200. $result = make(UserRepository::class)->updateUserData($userId, $inputData);
  201. return json_success($result);
  202. }
  203. /**
  204. *
  205. * @return mixed
  206. */
  207. public function bindCode()
  208. {
  209. //当前用户邀请码
  210. $userId = $this->getUserId();
  211. $inputData = $this->getJsonData();
  212. $rules = [
  213. 'code' => 'required|string',
  214. ];
  215. $validator = make(ValidatorFactoryInterface::class)
  216. ->make($inputData, $rules);
  217. if ($validator->fails()) {
  218. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  219. return json_fail($validator->errors()->first());
  220. }
  221. $result = make(UserRepository::class)->bindCode($userId, $inputData['code']);
  222. return json_success($result);
  223. }
  224. /**
  225. * 解绑手机号
  226. * @return mixed
  227. */
  228. public function unbindPhone()
  229. {
  230. //当前用户邀请码
  231. $userId = $this->getUserId();
  232. $inputData = $this->getJsonData();
  233. $rules = [
  234. 'phone' => 'required|string|regex:/^1\d{10}$/',
  235. 'code' => 'required|string',
  236. ];
  237. $validator = make(ValidatorFactoryInterface::class)
  238. ->make($inputData, $rules);
  239. if ($validator->fails()) {
  240. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  241. return json_fail($validator->errors()->first());
  242. }
  243. // 验证code
  244. $cache = make(CacheInterface::class);
  245. $cache_code = $cache->get('phone:code:' . $inputData['phone']);
  246. if ($cache_code != $inputData['code'] || empty($cache_code)) {
  247. return json_fail('验证码不正确');
  248. }
  249. $result = make(UserRepository::class)
  250. ->bindPhone($userId, $inputData['phone'], $inputData['code']);
  251. return json_success($result);
  252. }
  253. /**
  254. * 获取默认头像.
  255. * @return mixed
  256. */
  257. public function defaultAvatars()
  258. {
  259. $arrData = [];
  260. $filePrefix = 'f2|/'. env('STATIC_DIR_NAME', 'doukui') . '/avatars/a_';
  261. $maxNum = 11;
  262. for ($i = 1; $i <= $maxNum; $i++) {
  263. $tmpNum = str_pad(strval($i), 3, '0', STR_PAD_LEFT);
  264. $groupUrl = $filePrefix . $tmpNum . '.ceb';
  265. $fullUrl = M3u8CacheRepository::getImageUrl($groupUrl);
  266. $arrData[] = [
  267. 'group_url' => $groupUrl,
  268. 'full_url' => $fullUrl,
  269. ];
  270. }
  271. return json_success($arrData);
  272. }
  273. /**
  274. * 搜索用户.
  275. * @return mixed
  276. */
  277. public function searchPages()
  278. {
  279. $inputData = $this->getJsonData();
  280. $rules = [
  281. 'nick' => 'required|string|max:64',
  282. 'page' => 'nullable|int',
  283. 'page_size' => 'nullable|int',
  284. ];
  285. $validator = make(ValidatorFactoryInterface::class)
  286. ->make($inputData, $rules);
  287. if ($validator->fails()) {
  288. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  289. return json_fail($validator->errors()->first());
  290. }
  291. $userCode = $this->getUserId();
  292. $defaultPageSize = 10;
  293. $page = !empty($inputData['page']) ? intval($inputData['page']) : 1;
  294. $pageSize = !empty($inputData['page_size']) ? intval($inputData['page_size']) : $defaultPageSize;
  295. if ($pageSize > 100) {
  296. return json_fail('参数错误');
  297. }
  298. $page = format_page($page);
  299. $pageSize = intval($pageSize);
  300. if ($pageSize <= 0) {
  301. $pageSize = CommonConstant::PAGE_SIZE;
  302. }
  303. $columns = ['id', 'nick', 'intro', 'avatar', 'fans', 'flow', 'like', 'gender', 'code'];
  304. $items = UserModel::query()->where(['nick' => $inputData['nick'], 'status' => 1])->orderBy('id', 'DESC')->paginate($pageSize, $columns, 'page', $page);
  305. $arrData = [];
  306. if ($items->isNotEmpty()) {
  307. $arrData = $items->toArray();
  308. foreach ($arrData['data'] as $k => &$v) {
  309. $v['avatar'] = M3u8CacheRepository::getImageUrl($v['avatar']);
  310. if ($v['gender'] == 3) {
  311. $v['gender'] = 2;
  312. }
  313. unset($v['id']);
  314. }
  315. }
  316. // 异步记录搜索记录.
  317. $jobParams = [
  318. 'event_name' => 'user',
  319. 'user_code' => $userCode,
  320. 'keywords' => data_get($inputData, 'nick', ''),
  321. 'module' => 'user',
  322. ];
  323. dispatch_job(new RecordSearchJob($jobParams));
  324. return json_success($arrData);
  325. }
  326. }