Wx.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\helper;
  10. use fecshop\services\Service;
  11. use Yii;
  12. /**
  13. * Format services.
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. // use \fecshop\services\helper\Format;
  18. class Wx extends Service
  19. {
  20. public $wxApiBaseUrl = 'https://api.weixin.qq.com';
  21. public $configFile;
  22. // APPID:绑定支付的APPID
  23. public $microProgramAppId ;
  24. // 小程序secert
  25. public $microProgramSecret;
  26. public function init()
  27. {
  28. parent::init();
  29. $wxpayConfigFile = Yii::getAlias($this->configFile);
  30. if (!is_file($wxpayConfigFile)) {
  31. throw new InvalidConfigException('wxpay config file:['.$wxpayConfigFile.'] is not exist');
  32. }
  33. require_once($wxpayConfigFile);
  34. // 通过上面的小程序,设置配置信息
  35. $this->microProgramAppId = \WxPayConfig::APPID;
  36. $this->microProgramSecret = \WxPayConfig::APPSECRET;
  37. }
  38. /**
  39. * @param $code | string, 微信登陆的code
  40. * @return array , example: ['session_key' => '', 'openid' => '']
  41. */
  42. public function getUserInfoByCode($code)
  43. {
  44. $urlKey = '/sns/jscode2session';
  45. $apiId = $this->microProgramAppId;
  46. $secret = $this->microProgramSecret;
  47. $grant_type = 'authorization_code';
  48. $url = $this->wxApiBaseUrl . $urlKey . "?appid=$apiId&secret=$secret&js_code=$code&grant_type=$grant_type";
  49. // echo $url;
  50. $returnStr = \fec\helpers\CApi::getCurlData($url);
  51. $wxUserInfo = json_decode($returnStr, true);
  52. if (!isset($wxUserInfo['session_key']) || !isset($wxUserInfo['openid']) ) {
  53. return null;
  54. }
  55. // 保存到session
  56. Yii::$service->helper->wx->setWxSessionKeyAndOpenid($wxUserInfo['session_key'], $wxUserInfo['openid']);
  57. return $wxUserInfo;
  58. }
  59. /**
  60. * @param $session_key | string, 微信登陆返回的session_key
  61. * @param $openid | string, 微信登陆返回的openid
  62. * @return bolean,将值保存到session中
  63. */
  64. public function setWxSessionKeyAndOpenid($session_key, $openid)
  65. {
  66. $openidStatus = Yii::$service->session->set('wx_openid', $openid);
  67. $sessionKeyStatus = Yii::$service->session->set('wx_session_key', $session_key);
  68. //var_dump([Yii::$service->session->get('wx_session_key'), Yii::$service->session->get('wx_openid')]);
  69. //exit;
  70. return $openidStatus && $sessionKeyStatus;
  71. }
  72. /**
  73. * @return string, 从session中取出来session_key
  74. */
  75. public function getWxSessionKey()
  76. {
  77. return Yii::$service->session->get('wx_session_key');
  78. }
  79. /**
  80. * @return string, 从session中取出来 openid
  81. */
  82. public function getWxOpenid()
  83. {
  84. return Yii::$service->session->get('wx_openid');
  85. }
  86. /*
  87. public function createQRCode()
  88. {
  89. /cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
  90. $urlKey = '/sns/jscode2session';
  91. $apiId = $this->microProgramAppId;
  92. $secret = $this->microProgramSecret;
  93. $grant_type = 'authorization_code';
  94. $url = $this->wxApiBaseUrl . $urlKey . "?appid=$apiId&secret=$secret&js_code=$code&grant_type=$grant_type";
  95. // echo $url;
  96. $returnStr = \fec\helpers\CApi::getCurlData($url);
  97. $wxUserInfo = json_decode($returnStr, true);
  98. if (!isset($wxUserInfo['session_key']) || !isset($wxUserInfo['openid']) ) {
  99. return null;
  100. }
  101. // 保存到session
  102. Yii::$service->helper->wx->setWxSessionKeyAndOpenid($wxUserInfo['session_key'], $wxUserInfo['openid']);
  103. return $wxUserInfo;
  104. }
  105. */
  106. }