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.

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