WechatService.php 2.9 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. "wechatdata" => $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. /** @var User $user */
  52. $user = \Auth::user();
  53. if (is_null($user)) {
  54. throw new ClientException("need Authentication");
  55. }
  56. if ($phone = Arr::get($data, "phone")) {
  57. $user->phone = $phone;
  58. $user->save();
  59. }
  60. }
  61. /**
  62. * @return Application
  63. */
  64. private function wechat()
  65. {
  66. return new Application($this->config);
  67. }
  68. /**
  69. * @see https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
  70. */
  71. public function codeToSession($code)
  72. {
  73. $options = [
  74. 'query' => [
  75. 'appid' => $this->config['app_id'],
  76. 'js_code' => $code,
  77. 'secret' => $this->config['secret'],
  78. "grant_type" => "authorization_code",
  79. ],
  80. ];
  81. return $this->render($this->wechat()->getClient()->get("/sns/jscode2session", $options));
  82. }
  83. /**
  84. * @param Response $resp
  85. */
  86. private function render($resp)
  87. {
  88. if ($resp->isFailed()) {
  89. logger()->error("wechat render failer", [
  90. "resp" => $resp->getContent(),
  91. ]);
  92. throw new ClientException("调用微信接口出错,请稍后再试");
  93. }
  94. return json_decode($resp->getContent(), true);
  95. }
  96. }