GetOpenIdService.php 935 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2019/10/16
  6. * Time: 17:30
  7. */
  8. namespace app\common\services\wechat;
  9. use app\common\services\wechat\lib\WxPayApi;
  10. use app\common\services\wechat\lib\WxPayConfig;
  11. use app\common\services\wechat\lib\WxPayOpenId;
  12. class GetOpenIdService
  13. {
  14. /**
  15. * @param $auth_code
  16. * @return array|mixed|\stdClass
  17. * @throws lib\WxPayException
  18. */
  19. public static function getOpenid($auth_code)
  20. {
  21. $config = new WxPayConfig();
  22. $config->SetSignType('MD5');
  23. $request = new WxPayOpenId($config);
  24. $request->SetAuth_code($auth_code);
  25. $response = WxPayApi::authcodetoopenid($config, $request);
  26. $openid = $response['openid'];
  27. $set = \Setting::get('shop.wechat_set');
  28. if (!$set['is_independent'] && $set['sub_mchid']) {
  29. $openid = $response['sub_openid'];
  30. }
  31. return $openid;
  32. }
  33. }