123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- /**
- * WxProgram.php
- *
- * 微信小程序开发API接口
- * - 小程序码
- * - 安全内容
- * - 微信支付
- *
- */
- namespace echowx;
- use think\Exception;
- use echowx\utils\HttpCurl;
- use echowx\utils\Error;
- use echowx\utils\SHA1;
- use echowx\utils\Xml;
- class WxProgram
- {
- // 微信API域名
- const API_DOMAIN = 'https://api.weixin.qq.com/';
- // 开发者中心-配置项-AppID(应用ID)
- protected $appId;
- // 开发者中心-配置项-AppSecret(应用密钥)
- protected $appSecret;
- // 微信支付商户号,商户申请微信支付后,由微信支付分配的商户收款账号
- protected $payMchId;
- // API密钥,微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
- protected $payKey;
- /**
- * 设定配置项
- *
- * @param 微信小程序配置文件读取
- */
- public function __construct()
- {
- $this->appId = config('wxconfig.appId');
- $this->appSecret = config('wxconfig.appSecret');
- $this->payMchId = config('wxconfig.payMchId');
- $this->payKey = config('wxconfig.payKey');
- }
- /**
- * 校验access_token是否过期
- * @param string $token
- * @return bool
- */
- public function valid_access_token($token)
- {
- return $token && isset($token['expires_in']) && ($token['expires_in'] > time() + 1200);
- }
- /**
- * 生成新的access_token
- * @return mixed
- */
- public function new_access_token()
- {
- $url = self::API_DOMAIN . 'cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
- $res = HttpCurl::get($url, 'json');
- // 异常处理: 获取access_token网络错误
- if ($res === false) {
- @error_log('Http Get AccessToken Error.', 0);
- return false;
- }
- // 异常处理: access_token获取失败
- if (!isset($res['access_token'])) {
- @error_log('Get AccessToken Error: ' . json_encode($res), 0);
- return false;
- }
- $res['expires_in'] += time();
- $fp = fopen(dirname(__FILE__) . "/json/access_token.json", "w", 0777);
- fwrite($fp, json_encode($res));
- fclose($fp);
- return $res;
- }
- /**
- * 获取access_token
- * @return string
- */
- public function get_access_token()
- {
- $token = json_decode(file_get_contents(dirname(__FILE__) . "/json/access_token.json"), true);
- // 验证AccessToken是否有效
- if (!$this->valid_access_token($token)) {
- // 生成新的AccessToken
- $token = $this->new_access_token();
- if ($token === false) {
- return false;
- }
- }
- return $token['access_token'];
- }
- /**
- * MiniProgran - 登录
- * 登录凭证校验
- * @param code
- */
- public function auth_code2_session($js_code)
- {
- $url = self::API_DOMAIN . 'sns/jscode2session?appid=' . $this->appId . '&secret=' . $this->appSecret . '&js_code=' . $js_code . '&grant_type=authorization_code';
- $res = HttpCurl::get($url, 'json');
- return $res;
- }
- /**
- * MiniProgran - 内容安全
- * 检查一段文本是否含有违法违规内容
- * @param string
- * @return bool
- */
- public function security_msg_sec_check($content)
- {
- $access_token = $this->get_access_token();
- $url = self::API_DOMAIN . 'wxa/msg_sec_check?access_token=' . $access_token;
- $postarray = ['content' => $content];
- $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE);
- $res = HttpCurl::post($url, $postjson, 'json');
- if ($res['errcode'] == 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * MiniProgran - 小程序码
- * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。
- * @param string
- * @return bool
- */
- public function wxacode_get_unlimited($scene, $page = "", $width = 430, $filepath = '')
- {
- if (file_exists(root_path("/" . $filepath))) {
- return request()->domain() . "/" . $filepath;
- }
- $access_token = $this->get_access_token();
- $url = self::API_DOMAIN . 'wxa/getwxacodeunlimit?access_token=' . $access_token;
- $postarray = [
- 'scene' => $scene,
- 'page' => $page,
- 'width' => $width,
- ];
- $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE);
- $res = HttpCurl::post($url, $postjson, 'text');
- if (is_null(json_decode($res))) {
- $file = fopen($filepath, "w");
- fwrite($file, $res);
- fclose($file);
- return request()->domain() . "/" . $filepath;
- } else {
- return false;
- }
- }
- }
|