WechatService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Modules\Mini\Services;
  3. use App\Base\BaseService;
  4. use App\Exceptions\ClientException;
  5. use App\Models\User\User;
  6. use EasyWeChat\Kernel\HttpClient\Response;
  7. use EasyWeChat\MiniApp\Application;
  8. use Illuminate\Support\Arr;
  9. class WechatService extends BaseService
  10. {
  11. protected $config;
  12. /**
  13. * @param $config
  14. */
  15. public function __construct()
  16. {
  17. $this->config = config('account.wechat.mini_app');
  18. }
  19. public function decryptUserInfo($params)
  20. {
  21. $data = $this->wechat()->getUtils()->decryptSession($params['session_key'], $params['iv'], $params['encrypted_data']);
  22. //{
  23. // "openId": "oGZUI0egBJY1zhBYw2KhdUfwVJJE",
  24. // "nickName": "Band",
  25. // "gender": 1,
  26. // "language": "zh_CN",
  27. // "city": "Guangzhou",
  28. // "province": "Guangdong",
  29. // "country": "CN",
  30. // "avatarUrl": "http://wx.qlogo.cn/mmopen/vi_32/aSKcBBPpibyKNicHNTMM0qJVh8Kjgiak2AHWr8MHM4WgMEm7GFhsf8OYrySdbvAMvTsw3mo8ibKicsnfN5pRjl1p8HQ/0",
  31. // "unionId": "ocMvos6NjeKLIBqg5Mr9QjxrP1FA",
  32. // "watermark": {
  33. // "timestamp": 1477314187,
  34. // "appid": "wx4f4bc4dec97d474b"
  35. // }
  36. //}
  37. /** @var User $user */
  38. $user = \Auth::user();
  39. if (is_null($user)) {
  40. throw new ClientException("need Authentication");
  41. }
  42. $user->name = $data['nickName'];
  43. $user->extra = array_merge($user->extra, [
  44. "wechat" => $data,
  45. ]);
  46. $user->save();
  47. }
  48. public function decryptPhone($params)
  49. {
  50. $data = $this->wechat()->getUtils()->decryptSession($params['session_key'], $params['iv'], $params['encrypted_data']);
  51. logger()->info("decryptPhone", $data);
  52. /** @var User $user */
  53. $user = \Auth::user();
  54. if (is_null($user)) {
  55. throw new ClientException("need Authentication");
  56. }
  57. if ($phone = Arr::get($data, "purePhoneNumber")) {
  58. $user->phone = $phone;
  59. $user->save();
  60. }
  61. }
  62. /**
  63. * @return Application
  64. */
  65. private function wechat()
  66. {
  67. return new Application($this->config);
  68. }
  69. /**
  70. * @see https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
  71. */
  72. public function codeToSession($code)
  73. {
  74. $options = [
  75. 'query' => [
  76. 'appid' => $this->config['app_id'],
  77. 'js_code' => $code,
  78. 'secret' => $this->config['secret'],
  79. "grant_type" => "authorization_code",
  80. ],
  81. ];
  82. return $this->render($this->wechat()->getClient()->get("/sns/jscode2session", $options));
  83. }
  84. /**
  85. * @param Response $resp
  86. */
  87. private function render($resp)
  88. {
  89. if ($resp->isFailed()) {
  90. logger()->error("wechat render failer", [
  91. "resp" => $resp->getContent(),
  92. ]);
  93. throw new ClientException("调用微信接口出错,请稍后再试");
  94. }
  95. return json_decode($resp->getContent(), true);
  96. }
  97. }