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.

102 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
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. try {
  28. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  29. $user = $ssdb->exec('hgetall', SsdbKeys::USER_INFO.$userId);
  30. // 微信登录
  31. $config = config('applet');
  32. $app = Factory::miniProgram($config);
  33. $app['guzzle_handler'] = CoroutineHandler::class;
  34. $decryptedData = $app->encryptor->decryptData($user['session_key'], $iv, $encryptedData);
  35. $data = [
  36. 'nick_name' => $decryptedData['nickName'],
  37. 'avatar' => $decryptedData['avatarUrl'],
  38. 'openid' => $decryptedData['openId'],
  39. 'unionid' => $decryptedData['unionId'],
  40. 'country' => $decryptedData['country'],
  41. 'province' => $decryptedData['province'],
  42. 'city' => $decryptedData['city'],
  43. 'gender' => $decryptedData['gender'],
  44. 'language' => $decryptedData['language'],
  45. ];
  46. $user = User::query()->find($userId);
  47. $res = $user->fill($data)->save();
  48. if (!$res) {
  49. $logArr = [
  50. 'userId' => $userId,
  51. 'iv' => $iv,
  52. 'encryptedData' => $encryptedData,
  53. '$decryptedData' => $decryptedData
  54. ];
  55. throw new ErrorCodeException(ErrorCode::USER_INFO_UPDATE_ERROR,'更新用户信息失败',$logArr);
  56. }
  57. return $res;
  58. } catch (\Exception $e) {
  59. throw new ErrorCodeException(ErrorCode::USER_INFO_UPDATE_ERROR,'更新用户信息失败',['error_msg' => $e->getMessage()]);
  60. }
  61. }
  62. public function check($userId)
  63. {
  64. return User::query()->where(['status' => 1, 'id' => $userId])->exists();
  65. }
  66. public function undo($userId)
  67. {
  68. // TODO: Implement undo() method.
  69. }
  70. public function detail($userId)
  71. {
  72. return User::query()
  73. ->select(['id', 'nick_name', 'avatar', 'openid', 'total_score', 'real_name'])
  74. ->where(['status' => 1, 'id' => $userId])
  75. ->first()->toArray();
  76. }
  77. public function getStoreByUID($userId)
  78. {
  79. $store = Store::where('user_id',$userId)->select(['id','name','user_id','market_id'])->first();
  80. return $store;
  81. }
  82. public function getServicePersonnelByUID($userId)
  83. {
  84. $sp = ServicePersonnel::where('user_id',$userId)->select('id','user_id')->first();
  85. return $sp;
  86. }
  87. }