PcController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. namespace Modules\Web\Controllers;
  3. use App\Constants\CreatorConstant;
  4. use App\Jobs\SavePcVideoCoverCebJob;
  5. use App\Model\TypeModel;
  6. use App\Model\TypeVideoModel;
  7. use App\Model\VideoModel;
  8. use App\Repositories\ConfigRepoistory;
  9. use App\Repositories\CreatorRepository;
  10. use App\Repositories\CreatorRuleRepository;
  11. use App\Repositories\UserRepository;
  12. use App\Repositories\Utils\MediaCenterRepository;
  13. use App\Repositories\VideoRepository;
  14. use Carbon\Carbon;
  15. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  16. use Modules\Web\Services\AuthService;
  17. use Psr\SimpleCache\CacheInterface;
  18. class PcController extends BaseController
  19. {
  20. /**
  21. * 签名token/key
  22. * @var string
  23. */
  24. private $signToken = 'uwgnnDtzTZR1xzoW';
  25. /**
  26. * @var null|AuthService
  27. */
  28. private $authService = null;
  29. /**
  30. * @var null|UserRepository
  31. */
  32. private $userRepo = null;
  33. /**
  34. * @var null|CacheInterface
  35. */
  36. private $cacheRepo = null;
  37. public function index()
  38. {
  39. return 'pc-ok-pong';
  40. }
  41. /**
  42. * 轮询登陆是否成功.
  43. * @return mixed
  44. * @throws \Psr\SimpleCache\InvalidArgumentException
  45. */
  46. public function lookup()
  47. {
  48. $qrcodeContent = trim(request()->input('qc', ''));
  49. $qrFrom = request()->input('from', 'web');
  50. $qrInfo = $this->getUserCodeByQrCode($qrcodeContent);
  51. if (!empty($qrInfo['err_msg'])) {
  52. return $this->jsonFail($qrInfo['err_msg'], 5001);
  53. }
  54. $realUserCode = '';
  55. $userCode = $qrInfo['user_code'];
  56. $userInfo = $this->getAuthInfo();
  57. if (!empty($userInfo['code'])) {
  58. return $this->jsonFail('授权无效1', 5002);
  59. }
  60. if ($userInfo['code'] !== $userCode) {
  61. return $this->jsonFail('授权非法1', 5003);
  62. }
  63. $status = 2; // 状态,1=拿到token,2=未拿到,未登陆状态
  64. $cacheKey = $this->getAuthService()->getUserCacheKey($userCode);
  65. $token = $this->getCacheRepo()->get($cacheKey, '');
  66. if (empty($token)) {
  67. $status = 1;
  68. } else {
  69. $realUserCode = $userCode;
  70. }
  71. $result = [
  72. 'status' => $status,
  73. 'token' => $token,
  74. 'user_code' => $realUserCode,
  75. ];
  76. return $this->jsonSuccess($result);
  77. }
  78. /**
  79. * 检查是否合法登陆
  80. * @return mixed
  81. */
  82. public function checkLogin()
  83. {
  84. $userInfo = $this->getAuthInfo();
  85. if (empty($userInfo['code'])) {
  86. return $this->jsonFail('授权无效', 5091);
  87. }
  88. $result = [
  89. 'is_success' => true,
  90. ];
  91. return $this->jsonSuccess($result);
  92. }
  93. public function setting()
  94. {
  95. $userInfo = $this->getAuthInfo();
  96. if (empty($userInfo['code'])) {
  97. return $this->jsonFail('授权无效', 5091);
  98. }
  99. $userCode = $userInfo['code'];
  100. $configRepo = make(ConfigRepoistory::class);
  101. $creatorRepo = make(CreatorRepository::class);
  102. $ruleRepo = make(CreatorRuleRepository::class);
  103. $detail = $creatorRepo->detail($userCode);
  104. $todayCanUpload = false;
  105. $level = 0;
  106. $dayAddonNum = 0;
  107. $dayVideo = 0;
  108. if (!empty($detail)) {
  109. $level = intval($detail->level);
  110. $dayAddonNum = intval($detail->day_addon_num);
  111. $dayVideo = intval($detail->day_video);
  112. }
  113. $ruleLevel = $ruleRepo->getRuleByLevel($level);
  114. $dayMaxVideo = intval($ruleLevel->day_max_video) + $dayAddonNum;
  115. $todayVideo = !empty($detail) ? $dayVideo : 0;
  116. if ($todayVideo < $dayMaxVideo) {
  117. $todayCanUpload = true;
  118. }
  119. $fileMaxSizeMb = CreatorConstant::VIDEO_MAX_SIZE_MB; // 文件最大多少MB
  120. $maxCoin = CreatorConstant::VIDEO_SELL_MAX_COIN; // 最多设置金币个数
  121. $creatorMinVideoNum = CreatorConstant::FREE_VIDEO_NUM; // 创作者,level>0
  122. $normalMinVideoNum = CreatorConstant::LEVEL_0_FREE_VIDEO_NUM; // 普通人, level=0
  123. $maxTagNum = CreatorConstant::VIDEO_TAG_MAX_NUM; // 最多选择标签个数.
  124. $tags = $creatorRepo->canUseTags();
  125. $ppvodParams = $configRepo->gets(['ppvod_upload_category', 'ppvod_upload_tpl_id', 'ppvod_upload_tpl_name'], 'sys', []);
  126. $isCanUploadCoin = isset($detail->coin_uper) ? boolval($detail->coin_uper) : false; // 是否能传收费视频
  127. $needUploadFreeNum = 0; // 还需要上传免费视频个数.
  128. $countFreeVideos = $creatorRepo->countPublishedCoinVideo($userCode, true); // 统计免费视频个数
  129. $countNotFreeVideos = $creatorRepo->countPublishedCoinVideo($userCode, false); //统计收费视频个数
  130. if ($level <= 0) {
  131. if (CreatorConstant::LEVEL_0_FREE_VIDEO_NUM > 0) {
  132. // 金币视频 x 免费要求 = 最低历史上传免费个数
  133. $leastFreeNum = $countNotFreeVideos > 0 ? $countNotFreeVideos * CreatorConstant::LEVEL_0_FREE_VIDEO_NUM : CreatorConstant::LEVEL_0_FREE_VIDEO_NUM;
  134. if ($countFreeVideos >= $leastFreeNum) {
  135. $isCanUploadCoin = true;
  136. } else {
  137. $needUploadFreeNum = $leastFreeNum - $countFreeVideos;
  138. }
  139. } else {
  140. $isCanUploadCoin = true;
  141. }
  142. } else {
  143. if (CreatorConstant::FREE_VIDEO_NUM > 0) {
  144. // 金币视频 x 免费要求 = 最低限制上传免费个数
  145. $leastFreeNum = $countNotFreeVideos > 0 ? $countNotFreeVideos * CreatorConstant::FREE_VIDEO_NUM : CreatorConstant::FREE_VIDEO_NUM;
  146. if ($countFreeVideos >= $leastFreeNum) {
  147. $isCanUploadCoin = true;
  148. } else {
  149. $needUploadFreeNum = $leastFreeNum - $countFreeVideos;
  150. }
  151. } else {
  152. $isCanUploadCoin = true;
  153. }
  154. }
  155. $uploadIsMaintenance = false;
  156. $uploadMaintenanceDesc = '';
  157. $arrConfig = $configRepo->gets(['upload_maintenance_status', 'upload_maintenance_desc'], 'sys', []);
  158. if (isset($arrConfig['upload_maintenance_status']) && intval($arrConfig['upload_maintenance_status']) == 1) {
  159. $uploadIsMaintenance = true;
  160. $uploadMaintenanceDesc = $arrConfig['upload_maintenance_desc'] ?? '上传维护中,请稍后恢复再试';
  161. }
  162. $result = [
  163. 'user_id' => $userInfo['code'],
  164. 'nick' => $userInfo['nick'],
  165. 'level' => $level,
  166. 'today_can_upload' => $todayCanUpload,
  167. 'file_max_size_mb' => $fileMaxSizeMb,
  168. 'max_coin' => $maxCoin,
  169. 'creator_min_video_num' => $creatorMinVideoNum,
  170. 'normal_min_video_num' => $normalMinVideoNum,
  171. 'max_tag_num' => $maxTagNum,
  172. 'tags' => $tags,
  173. 'is_can_upload_coin' => $isCanUploadCoin,
  174. 'need_upload_free_num' => $needUploadFreeNum,
  175. 'count_free_videos' => $countFreeVideos,
  176. 'count_not_free_videos' => $countNotFreeVideos,
  177. 'upload_category' => !empty($ppvodParams['ppvod_upload_category']) ? $ppvodParams['ppvod_upload_category'] : '用户上传',
  178. 'upload_tpl_id' => !empty($ppvodParams['ppvod_upload_tpl_id']) ? $ppvodParams['ppvod_upload_tpl_id'] : '',
  179. 'upload_tpl_name' => !empty($ppvodParams['ppvod_upload_tpl_name']) ? $ppvodParams['ppvod_upload_tpl_name'] : '默认',
  180. 'upload_is_maintenance' => $uploadIsMaintenance,
  181. 'upload_maintenance_desc' => $uploadMaintenanceDesc,
  182. ];
  183. return $this->jsonSuccess($result);
  184. }
  185. public function createVideo()
  186. {
  187. $userInfo = $this->getAuthInfo();
  188. if (empty($userInfo['code'])) {
  189. return $this->jsonFail('授权无效', 5091);
  190. }
  191. $userCode = $userInfo['code'];
  192. $validatorFactory = make(ValidatorFactoryInterface::class);
  193. $inputData = request()->all();
  194. $rules = [
  195. 'title' => 'required',
  196. 'cover' => 'nullable|max:700',
  197. 'coins' => 'required|int',
  198. 'tags' => 'required', // 这个是ID
  199. 'file_hash' => 'required'
  200. ];
  201. $validator = $validatorFactory->make($inputData, $rules);
  202. if ($validator->fails()) {
  203. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  204. return json_fail($validator->errors()->first());
  205. }
  206. $videoRepo = make(VideoRepository::class);
  207. if ($videoRepo->checkExist($inputData['file_hash'])) {
  208. return $this->jsonFail('此视频已被其他人上传过,请重新更换', 6444);
  209. }
  210. $mediaCenterRepo = make(MediaCenterRepository::class);
  211. $hashVideoInfo = $mediaCenterRepo->getHashInfo($inputData['file_hash']);
  212. if (!empty($hashVideoInfo)) {
  213. return $this->jsonFail('此视频有人上传过了,请重新更换', 6444);
  214. }
  215. $ruleRepo = make(CreatorRuleRepository::class);
  216. $creatorRepo = make(CreatorRepository::class);
  217. $detail = $creatorRepo->detail($userCode);
  218. $level = 0;
  219. $dayAddonNum = 0;
  220. $dayVideo = 0;
  221. if (!empty($detail)) {
  222. $level = intval($detail->level);
  223. $dayAddonNum = intval($detail->day_addon_num);
  224. $dayVideo = intval($detail->day_video);
  225. } else {
  226. // 自动申请为原创者.
  227. $detail = $creatorRepo->applyToUncheck($userCode);
  228. $level = intval($detail->level);
  229. }
  230. $ruleLevel = $ruleRepo->getRuleByLevel($level);
  231. $dayMaxVideo = intval($ruleLevel->day_max_video) + $dayAddonNum;
  232. $todayVideo = !empty($detail) ? $dayVideo : 0;
  233. if ($todayVideo >= $dayMaxVideo) {
  234. return $this->jsonFail('今日已经超出上传数量限制');
  235. }
  236. $cover = trim(data_get($inputData, 'cover', ''));
  237. $cover = substr($cover, 0, 255);
  238. $coins = intval(data_get($inputData, 'coins', 0)); // 金币,单位数量个,10金币=1元人民币
  239. $uploadingUrl = ''; // TODO,这里可以做一个固定视频,等待用户上传转码中.
  240. if ($coins < 0) {
  241. $coins = 0;
  242. }
  243. if ($coins > CreatorConstant::VIDEO_SELL_MAX_COIN) {
  244. return $this->jsonFail('售卖金币价格太大,可以提前联系客服');
  245. }
  246. $arrTagIds = [];
  247. $tmpTagIds = data_get($inputData, 'tags', '');
  248. if (is_string($tmpTagIds)) {
  249. $arrTagIds = explode(',', $tmpTagIds);
  250. } else {
  251. $arrTagIds = $tmpTagIds;
  252. }
  253. if (!empty($arrTagIds)) {
  254. $arrTagIds = array_filter($arrTagIds);
  255. $arrTagIds = array_unique($arrTagIds);
  256. }
  257. if (empty($arrTagIds)) {
  258. return $this->jsonFail('请选择至少1个口味');
  259. }
  260. if (count($arrTagIds) > CreatorConstant::VIDEO_TAG_MAX_NUM) {
  261. return $this->jsonFail('最多选择'. strval(CreatorConstant::VIDEO_TAG_MAX_NUM) .'个口味');
  262. }
  263. // 处理关联标签.
  264. $tagIdsRaltion = [];
  265. $tagNames = [];
  266. if (!empty($arrTagIds)) {
  267. foreach ($arrTagIds as $v) {
  268. $tagObj = TypeModel::query()->where('id', $v)->first();
  269. if (!empty($tagObj->id)) {
  270. $tagIdsRaltion[] = $tagObj->id;
  271. $tagNames[] = $tagObj->title;
  272. }
  273. }
  274. }
  275. $isCanUploadCoin = isset($detail->coin_uper) ? boolval($detail->coin_uper) : false; // 是否能传收费视频
  276. $needUploadFreeNum = 0; // 还需要上传免费视频个数.
  277. $countFreeVideos = $creatorRepo->countPublishedCoinVideo($userCode, true); // 统计免费视频个数
  278. $countNotFreeVideos = $creatorRepo->countPublishedCoinVideo($userCode, false); //统计收费视频个数
  279. if ($level <= 0) {
  280. if (CreatorConstant::LEVEL_0_FREE_VIDEO_NUM > 0) {
  281. // 金币视频 x 免费要求 = 最低历史上传免费个数
  282. $leastFreeNum = $countNotFreeVideos > 0 ? $countNotFreeVideos * CreatorConstant::LEVEL_0_FREE_VIDEO_NUM : CreatorConstant::LEVEL_0_FREE_VIDEO_NUM;
  283. if ($countFreeVideos >= $leastFreeNum) {
  284. $isCanUploadCoin = true;
  285. } else {
  286. $needUploadFreeNum = $leastFreeNum - $countFreeVideos;
  287. }
  288. } else {
  289. $isCanUploadCoin = true;
  290. }
  291. } else {
  292. if (CreatorConstant::FREE_VIDEO_NUM > 0) {
  293. // 金币视频 x 免费要求 = 最低限制上传免费个数
  294. $leastFreeNum = $countNotFreeVideos > 0 ? $countNotFreeVideos * CreatorConstant::FREE_VIDEO_NUM : CreatorConstant::FREE_VIDEO_NUM;
  295. if ($countFreeVideos >= $leastFreeNum) {
  296. $isCanUploadCoin = true;
  297. } else {
  298. $needUploadFreeNum = $leastFreeNum - $countFreeVideos;
  299. }
  300. } else {
  301. $isCanUploadCoin = true;
  302. }
  303. }
  304. if ($coins > 0 && !$isCanUploadCoin) {
  305. return $this->jsonFail('还不能上传收费视频,请先再传'. strval($needUploadFreeNum) . '个免费视频');
  306. }
  307. $payType = 2; // 默认金币
  308. if ($coins == 0) {
  309. $payType = 1; // vip
  310. }
  311. $newStrTagNames = !empty($tagNames) ? implode(',', $tagNames) : '';
  312. $newStrTagIds = !empty($arrTagIds) ? implode(',', $arrTagIds) : '';
  313. $title = trim(data_get($inputData, 'title', ''));
  314. $title = mb_substr($title, 0, 24);
  315. $fileHash = trim(data_get($inputData, 'file_hash', ''));
  316. $fileHash = substr($fileHash, 0, 32);
  317. $setarr = [
  318. 'uid' => $userCode,
  319. 'status' => 3,
  320. 'check' => 0,
  321. 'pay_type' => $payType,
  322. 'type' => 1,
  323. 'mu' => $uploadingUrl,
  324. 'title' => $title,
  325. 'cover' => $cover,
  326. 'coins' => $coins,
  327. 'tags' => $newStrTagNames,
  328. 'file_hash' => $fileHash,
  329. 'custom_cover' => $cover, // 用户自定义封面.
  330. ];
  331. $videoObj = VideoModel::query()->create($setarr);
  332. if ($videoObj) {
  333. $videoId = $videoObj->id;
  334. if (!empty($tagIdsRaltion)) {
  335. foreach ($tagIdsRaltion as $tagId) {
  336. $setarr2 = [
  337. 'type_id' => $tagId,
  338. 'video_id' => $videoId,
  339. 'status' => 1,
  340. ];
  341. TypeVideoModel::query()->create($setarr2);
  342. }
  343. }
  344. // 累加今日上传视频数量.
  345. $detail->day_video++;
  346. $detail->save();
  347. if (!empty($cover)) {
  348. $coverFileName = basename($cover);
  349. $coverExt = $this->fileExt($coverFileName);
  350. if ($coverExt != 'ceb' && in_array($coverExt, ['jpg', 'jpeg', 'png', 'gif'])) {
  351. $jobParams = [
  352. 'video_id' => $videoId,
  353. 'cover' => $cover,
  354. ];
  355. dispatch_job(new SavePcVideoCoverCebJob($jobParams));
  356. }
  357. }
  358. } else {
  359. return $this->jsonFail('创建视频失败');
  360. }
  361. $result = [
  362. 'video' => [
  363. 'id' => $videoObj->id, // 给前端的。前端拿到这个, 拼接 app应用全称(媒资库里定义的)| 模块名 | 视频ID ,doukui|video|video_id
  364. // 'title' => $setarr['title'],
  365. // 'coins' => $setarr['coins'],
  366. // 'cover' => '', // 这里ppvod的是暴露的jpg路径,不能用,直接访问空字符串
  367. // 'tag_names' => $newStrTagNames,
  368. // 'tag_ids' => $newStrTagIds,
  369. ],
  370. ];
  371. return $this->jsonSuccess($result);
  372. }
  373. /**
  374. * 二维码登陆
  375. * @return mixed
  376. * @throws \Psr\SimpleCache\InvalidArgumentException
  377. */
  378. public function qrcodeLogin()
  379. {
  380. $qrcodeContent = trim(request()->input('qc', ''));
  381. $qrFrom = request()->input('from', 'web');
  382. $qrInfo = $this->getUserCodeByQrCode($qrcodeContent);
  383. if (!empty($qrInfo['err_msg'])) {
  384. return $this->jsonFail($qrInfo['err_msg']);
  385. }
  386. $userCode = $qrInfo['user_code'];
  387. // $cacheKey = $this->getAuthService()->getUserCacheKey($userCode);
  388. // $token = $this->getCacheRepo()->get($cacheKey);
  389. $tokenInfo = $this->getAuthService()->createSingleJwt($userCode);
  390. $token = $tokenInfo['token'];
  391. $result = [
  392. 'token' => $token,
  393. 'user_code' => $userCode,
  394. ];
  395. return $this->jsonSuccess($result);
  396. }
  397. public function upAuth()
  398. {
  399. $userInfo = $this->getAuthInfo();
  400. if (empty($userInfo['code'])) {
  401. logger('upAuth', ['msg' => '[4]握手失败']);
  402. return response('[4]握手失败', 500);
  403. }
  404. $queryUserCode = $userInfo['code'];
  405. $inputData = request()->all();
  406. $rules = [
  407. 'auth_event' => 'required',
  408. 'auth_timestamp' => 'required',
  409. 'app_name' => 'required',
  410. 'auth_user' => 'required',
  411. 'auth_sign' => 'required|string',
  412. 'file_hash' => 'nullable',
  413. 'file_origin_name' => 'nullable',
  414. 'file_ext' => 'nullable',
  415. ];
  416. $validator = make(ValidatorFactoryInterface::class)
  417. ->make($inputData, $rules);
  418. if ($validator->fails()) {
  419. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  420. logger('upAuth', ['msg' => $validator->errors()->first()]);
  421. return response($validator->errors()->first(), 500);
  422. }
  423. $authTimestamp = strval(data_get($inputData, 'auth_timestamp', ''));
  424. $appName = data_get($inputData, 'app_name', '');
  425. $authUserCode = data_get($inputData, 'auth_user', '');
  426. $authSign = data_get($inputData, 'auth_sign', '');
  427. $authEvent = data_get($inputData, 'auth_event', '');
  428. $fileOriginName = data_get($inputData, 'file_origin_name', '');
  429. $fileHash = data_get($inputData, 'file_hash', '');
  430. $fileExt = strtolower(data_get($inputData, 'file_ext', ''));
  431. $tmpTimestamp = substr($authTimestamp, 0, 10);
  432. if (abs(Carbon::createFromTimestamp($tmpTimestamp)->diffInMinutes(Carbon::now())) > 30) {
  433. logger('upAuth', ['msg' => '[5]握手失败']);
  434. return response('[5]握手失败', 500);
  435. }
  436. $inSign = $this->createSign($authTimestamp, $appName, $authUserCode, $authEvent);
  437. if ($inSign !== $authSign) {
  438. logger('upAuth', ['msg' => '[3]握手失败']);
  439. return response('[3]握手失败', 500);
  440. }
  441. if (empty($queryUserCode) || $queryUserCode !== $authUserCode) {
  442. logger('upAuth', ['msg' => '[6]握手失败']);
  443. return response('[6]握手失败', 500);
  444. }
  445. return response('success', 200);
  446. }
  447. /**
  448. * 获取认证里的用户信息.
  449. * @return array
  450. */
  451. private function getAuthInfo()
  452. {
  453. $authInfo = $this->getAuthService()->getTokenUserInfo();
  454. if (empty($authInfo['user_id'])) {
  455. return [];
  456. }
  457. $userInfo = $this->getAuthService()->checkValid($authInfo['user_id']);
  458. if (empty($userInfo)) {
  459. return [];
  460. }
  461. $userInfo['user_id'] = $userInfo['code']; // 兼容.
  462. return $userInfo;
  463. }
  464. /**
  465. * 获取服务.
  466. * @return mixed|AuthService
  467. */
  468. private function getAuthService()
  469. {
  470. if (!$this->authService instanceof AuthService) {
  471. $this->authService = make(AuthService::class);
  472. }
  473. return $this->authService;
  474. }
  475. /**
  476. * 获取用户仓库.
  477. * @return UserRepository|mixed
  478. */
  479. private function getUserRepo()
  480. {
  481. if (!$this->userRepo instanceof UserRepository) {
  482. $this->userRepo = make(UserRepository::class);
  483. }
  484. return $this->userRepo;
  485. }
  486. /**
  487. * 获取缓存仓库.
  488. * @return mixed|CacheInterface
  489. */
  490. private function getCacheRepo()
  491. {
  492. if (!$this->cacheRepo instanceof CacheInterface) {
  493. $this->cacheRepo = make(CacheInterface::class);
  494. }
  495. return $this->cacheRepo;
  496. }
  497. /**
  498. * 通过二维码内容获取用户code.
  499. * @param $qrcodeContent
  500. * @return string[]
  501. */
  502. private function getUserCodeByQrCode($qrcodeContent)
  503. {
  504. $result = [
  505. 'err_msg' => '',
  506. 'user_code' => '',
  507. ];
  508. if (empty($qrcodeContent)) {
  509. $result['err_msg'] = '无效的二维码1';
  510. return $result;
  511. }
  512. $txt = $qrcodeContent;
  513. if (empty($txt)) {
  514. $result['err_msg'] = '无效的二维码2';
  515. return $result;
  516. }
  517. $account = hand_decrypt($txt, $this->getUserRepo()->getAcKey(), $this->getUserRepo()->getAcIv());
  518. if (empty($account)) {
  519. $result['err_msg'] = '无效的二维码3';
  520. return $result;
  521. }
  522. $pattern = '/^user:\/\/([a-zA-Z0-9]{6,12})/';
  523. preg_match($pattern, $account, $matches);
  524. if (empty($matches[1])) {
  525. $result['err_msg'] = '无效的二维码4';
  526. return $result;
  527. }
  528. $userCode = $matches[1];
  529. $userInfo = $this->getUserRepo()->getUserInfoFromCache($userCode);
  530. if (empty($userInfo['code'])) {
  531. $result['err_msg'] = '无效的二维码5';
  532. return $result;
  533. }
  534. $result['user_code'] = $userCode;
  535. return $result;
  536. }
  537. /**
  538. * @param $timestamp
  539. * @param $appName
  540. * @param $userCode
  541. * @return string
  542. */
  543. private function createSign($timestamp, $appName, $userCode, $authEvent)
  544. {
  545. $strParams = $appName . '-' . $authEvent . '-' . $userCode. '-'. $timestamp;
  546. $strSign = md5(md5($strParams) . '-'. $this->signToken);
  547. return $strSign;
  548. }
  549. private function fileExt($filename) {
  550. return strtolower(trim(substr(strrchr($filename, '.'), 1)));
  551. }
  552. }