WechatService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace addons\HuifuPay\common\service;
  3. use yii\db\Exception;
  4. use \Yii;
  5. class WechatService
  6. {
  7. public function getOpenId($code)
  8. {
  9. $wechatModel = \Yii::$app->wechat;
  10. if (!$wechatModel->isWechat) throw new Exception('不是微信请求');
  11. $resultData = $wechatModel->miniProgram->auth->session($code);
  12. $openid = $resultData["openid"];
  13. return $openid;
  14. }
  15. public function getScheme($params)
  16. {
  17. $wechatModel = \Yii::$app->wechat;
  18. $accessToken= $wechatModel->miniProgram->access_token->getToken(true);
  19. $url1 = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$accessToken['access_token'];
  20. $ch = curl_init();
  21. $data1 = array("path"=>$params['path'], "query"=>$params['query'],"env_version"=>"release");
  22. // if(isset($params["env_version"]))
  23. // {
  24. // array_merge($data1,["env_version"=>"trial"]);
  25. // }
  26. $data = json_encode(array("expire_type"=>1,"expire_interval"=>5,"jump_wxa"=>$data1));
  27. curl_setopt($ch, CURLOPT_URL, $url1);
  28. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  29. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  30. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  31. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  33. $output = curl_exec($ch);
  34. curl_close($ch);
  35. return json_decode($output,true);
  36. }
  37. }