You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.2 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Constants\v3\SsdbKeys;
  5. use App\Exception\ErrorCodeException;
  6. use App\Model\v3\User;
  7. use App\Service\v3\Interfaces\UserInfoServiceInterface;
  8. use App\TaskWorker\SSDBTask;
  9. use EasyWeChat\Factory;
  10. use EasyWeChat\Kernel\Exceptions\DecryptException;
  11. use Hyperf\Guzzle\CoroutineHandler;
  12. use Hyperf\Utils\ApplicationContext;
  13. class UserInfoService implements UserInfoServiceInterface
  14. {
  15. /**
  16. * 更新
  17. * @param $userId
  18. * @param $iv
  19. * @param $encryptedData
  20. * @return array
  21. * @throws DecryptException
  22. */
  23. public function do($userId, $iv, $encryptedData)
  24. {
  25. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  26. $user = $ssdb->exec('hgetall', SsdbKeys::USER_INFO.$userId);
  27. // 微信登录
  28. $config = config('applet');
  29. $app = Factory::miniProgram($config);
  30. $app['guzzle_handler'] = CoroutineHandler::class;
  31. $decryptedData = $app->encryptor->decryptData($user['session_key'], $iv, $encryptedData);
  32. $data = [
  33. 'nick_name' => $decryptedData['nickName'],
  34. 'avatar' => $decryptedData['avatarUrl'],
  35. 'openid' => $decryptedData['openId'],
  36. 'unionid' => $decryptedData['unionId'],
  37. 'country' => $decryptedData['country'],
  38. 'province' => $decryptedData['province'],
  39. 'city' => $decryptedData['city'],
  40. 'gender' => $decryptedData['gender'],
  41. 'language' => $decryptedData['nickName'],
  42. ];
  43. $user = User::query()->find($userId);
  44. $res = $user->fill($data)->save();
  45. if (!$res) {
  46. throw new ErrorCodeException(ErrorCode::USER_INFO_UPDATE_ERROR);
  47. }
  48. return $res;
  49. }
  50. public function check($userId)
  51. {
  52. return User::query()->where(['status' => 1, 'id' => $userId])->exists();
  53. }
  54. public function undo($userId)
  55. {
  56. // TODO: Implement undo() method.
  57. }
  58. public function detail($userId)
  59. {
  60. return User::query()
  61. ->select(['id', 'nick_name', 'avatar', 'openid', 'total_score', 'real_name'])
  62. ->where(['status' => 1, 'id' => $userId])
  63. ->first()->toArray();
  64. }
  65. }