Wx.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $appId = Yii::$app->store->get('payment_wxpay', 'wechat_micro_app_id' );
  34. $appSecret = Yii::$app->store->get('payment_wxpay', 'wechat_micro_app_secret');
  35. $mchKey = Yii::$app->store->get('payment_wxpay', 'merchant_key');
  36. $mchId = Yii::$app->store->get('payment_wxpay', 'merchant_mch_id');
  37. define('WX_APP_ID', $appId);
  38. define('WX_APP_SECRET', $appSecret);
  39. define('WX_MCH_KEY', $mchKey);
  40. define('WX_MCH_ID', $mchId);
  41. require_once($wxpayConfigFile);
  42. // 通过上面的小程序,设置配置信息
  43. $this->microProgramAppId = \WxPayConfig::APPID;
  44. $this->microProgramSecret = \WxPayConfig::APPSECRET;
  45. }
  46. /**
  47. * @param $code | string, 微信登陆的code
  48. * @return array , example: ['session_key' => '', 'openid' => '']
  49. */
  50. public function getUserInfoByCode($code)
  51. {
  52. $urlKey = '/sns/jscode2session';
  53. $apiId = $this->microProgramAppId;
  54. $secret = $this->microProgramSecret;
  55. $grant_type = 'authorization_code';
  56. $url = $this->wxApiBaseUrl . $urlKey . "?appid=$apiId&secret=$secret&js_code=$code&grant_type=$grant_type";
  57. //echo $url; exit;
  58. $returnStr = \fec\helpers\CApi::getCurlData($url);
  59. $wxUserInfo = json_decode($returnStr, true);
  60. if (!isset($wxUserInfo['session_key']) || !isset($wxUserInfo['openid']) ) {
  61. return null;
  62. }
  63. // 保存到session
  64. //Yii::$service->helper->wx->setWxSessionKeyAndOpenid($wxUserInfo['session_key'], $wxUserInfo['openid']);
  65. return $wxUserInfo;
  66. }
  67. /**
  68. * @param $session_key | string, 微信登陆返回的session_key
  69. * @param $openid | string, 微信登陆返回的openid
  70. * @return bolean,将值保存到session中
  71. */
  72. //public function setWxSessionKeyAndOpenid($session_key, $openid)
  73. //{
  74. // $openidStatus = Yii::$service->session->set('wx_openid', $openid);
  75. // $sessionKeyStatus = Yii::$service->session->set('wx_session_key', $session_key);
  76. //var_dump([Yii::$service->session->get('wx_session_key'), Yii::$service->session->get('wx_openid')]);
  77. //exit;
  78. // return $openidStatus && $sessionKeyStatus;
  79. //}
  80. /**
  81. * @return string, 从session中取出来session_key
  82. */
  83. //public function getWxSessionKey()
  84. //{
  85. // return Yii::$service->session->get('wx_session_key');
  86. //}
  87. /**
  88. * @return string, 从session中取出来 openid
  89. */
  90. //public function getWxOpenid()
  91. //{
  92. // return Yii::$service->session->get('wx_openid');
  93. //}
  94. /*
  95. public function createQRCode()
  96. {
  97. /cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
  98. $urlKey = '/sns/jscode2session';
  99. $apiId = $this->microProgramAppId;
  100. $secret = $this->microProgramSecret;
  101. $grant_type = 'authorization_code';
  102. $url = $this->wxApiBaseUrl . $urlKey . "?appid=$apiId&secret=$secret&js_code=$code&grant_type=$grant_type";
  103. // echo $url;
  104. $returnStr = \fec\helpers\CApi::getCurlData($url);
  105. $wxUserInfo = json_decode($returnStr, true);
  106. if (!isset($wxUserInfo['session_key']) || !isset($wxUserInfo['openid']) ) {
  107. return null;
  108. }
  109. // 保存到session
  110. Yii::$service->helper->wx->setWxSessionKeyAndOpenid($wxUserInfo['session_key'], $wxUserInfo['openid']);
  111. return $wxUserInfo;
  112. }
  113. */
  114. }