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.

128 lines
4.0 KiB

4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 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\Employees;
  7. use App\Model\v3\User;
  8. use App\Model\v3\ServicePersonnel;
  9. use App\Model\v3\Store;
  10. use App\Service\v3\Interfaces\UserInfoServiceInterface;
  11. use App\TaskWorker\SSDBTask;
  12. use EasyWeChat\Factory;
  13. use EasyWeChat\Kernel\Exceptions\DecryptException;
  14. use Hyperf\Guzzle\CoroutineHandler;
  15. use Hyperf\Utils\ApplicationContext;
  16. class UserInfoService
  17. {
  18. /**
  19. * 更新
  20. * @param $data
  21. * @return array
  22. * @throws DecryptException
  23. */
  24. public function do($data)
  25. {
  26. try {
  27. $userId = $data['userId'];
  28. $iv = $data['iv'];
  29. $encryptedData = $data['encryptedData'];
  30. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  31. $user = $ssdb->exec('hgetall', SsdbKeys::USER_INFO.$userId);
  32. if(empty($user)) {
  33. return ['找不到缓存的用户信息'];
  34. }
  35. // 微信登录
  36. $config = config('applet');
  37. $app = Factory::miniProgram($config);
  38. $app['guzzle_handler'] = CoroutineHandler::class;
  39. $decryptedData = $app->encryptor->decryptData($user['session_key'], $iv, $encryptedData);
  40. $data = [
  41. 'nick_name' => $decryptedData['nickName'],
  42. 'avatar' => $decryptedData['avatarUrl'],
  43. // 'openid' => $decryptedData['openId'],
  44. // 'unionid' => $decryptedData['unionId'],
  45. 'country' => $decryptedData['country'],
  46. 'province' => $decryptedData['province'],
  47. 'city' => $decryptedData['city'],
  48. 'gender' => $decryptedData['gender'],
  49. 'language' => $decryptedData['language'],
  50. ];
  51. $user = User::query()->find($userId);
  52. $res = $user->fill($data)->save();
  53. if (!$res) {
  54. $logArr = [
  55. 'userId' => $userId,
  56. 'iv' => $iv,
  57. 'encryptedData' => $encryptedData,
  58. '$decryptedData' => $decryptedData
  59. ];
  60. throw new ErrorCodeException(ErrorCode::USER_INFO_UPDATE_ERROR,'更新用户信息失败',$logArr);
  61. }
  62. return $res;
  63. } catch (\Exception $e) {
  64. throw new ErrorCodeException(ErrorCode::USER_INFO_UPDATE_ERROR,'更新用户信息失败',['error_msg' => $e->getMessage()]);
  65. }
  66. }
  67. public function check($userId)
  68. {
  69. return User::query()->where(['status' => 1, 'id' => $userId])->exists();
  70. }
  71. public function undo($userId)
  72. {
  73. // TODO: Implement undo() method.
  74. }
  75. public function detail($userId)
  76. {
  77. $result = User::query()
  78. ->select(['id', 'nick_name', 'avatar', 'openid', 'unionid', 'total_score', 'real_name'])
  79. ->where(['status' => 1, 'id' => $userId])
  80. ->first();
  81. if(!$result) {
  82. return [];
  83. }else{
  84. return $result;
  85. }
  86. }
  87. public function getStoreByUID($userId)
  88. {
  89. $store = Store::where('user_id',$userId)->select(['id','name','user_id','market_id'])->first();
  90. return $store;
  91. }
  92. public function getEmployeesByUID($userId)
  93. {
  94. $employees = Employees::where('user_id',$userId)->where(function ($query){
  95. $query->whereJsonContains('position', '29')->orWhereJsonContains('position', '30');
  96. })->first();
  97. return $employees;
  98. }
  99. public function getPaidUnionId($openid, $options = [])
  100. {
  101. $config = config('applet');
  102. $app = Factory::miniProgram($config);
  103. $app['guzzle_handler'] = CoroutineHandler::class;
  104. $res = $app->base->getPaidUnionid($openid, $options);
  105. if (isset($res['unionid']) && $res['unionid']) {
  106. return $res['unionid'];
  107. } else {
  108. return false;
  109. }
  110. }
  111. }