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.

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