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.

91 lines
2.6 KiB

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