Modules_Api_Controllers_UserController.proxy.php 11 KB

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