|
@@ -20,11 +20,11 @@ use echowx\utils\Xml;
|
|
|
|
|
|
class WxProgram
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
// 微信API域名
|
|
|
const API_DOMAIN = 'https://api.weixin.qq.com/';
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
// 开发者中心-配置项-AppID(应用ID)
|
|
|
protected $appId;
|
|
|
// 开发者中心-配置项-AppSecret(应用密钥)
|
|
@@ -33,8 +33,8 @@ class WxProgram
|
|
|
protected $payMchId;
|
|
|
// API密钥,微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
|
|
|
protected $payKey;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 设定配置项
|
|
|
*
|
|
@@ -42,13 +42,13 @@ class WxProgram
|
|
|
*/
|
|
|
public function __construct()
|
|
|
{
|
|
|
- $this->appId = config('wxconfig.appId');
|
|
|
- $this->appSecret = config('wxconfig.appSecret');
|
|
|
- $this->payMchId = config('wxconfig.payMchId');
|
|
|
- $this->payKey = config('wxconfig.payKey');
|
|
|
+ $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
|
|
@@ -58,7 +58,7 @@ class WxProgram
|
|
|
{
|
|
|
return $token && isset($token['expires_in']) && ($token['expires_in'] > time() + 1200);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 生成新的access_token
|
|
|
* @return mixed
|
|
@@ -78,19 +78,19 @@ class WxProgram
|
|
|
return false;
|
|
|
}
|
|
|
$res['expires_in'] += time();
|
|
|
- $fp = fopen(dirname(__FILE__)."/json/access_token.json", "w",0777);
|
|
|
+ $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);
|
|
|
+ $token = json_decode(file_get_contents(dirname(__FILE__) . "/json/access_token.json"), true);
|
|
|
// 验证AccessToken是否有效
|
|
|
if (!$this->valid_access_token($token)) {
|
|
|
// 生成新的AccessToken
|
|
@@ -101,71 +101,69 @@ class WxProgram
|
|
|
}
|
|
|
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 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 - 内容安全
|
|
|
+ * MiniProgran - 内容安全
|
|
|
* 检查一段文本是否含有违法违规内容
|
|
|
- * @param string
|
|
|
+ * @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 = array('content'=>$content);
|
|
|
- $postjson = json_encode($postarray, JSON_UNESCAPED_UNICODE);
|
|
|
- $res = HttpCurl::post($url, $postjson, 'json');
|
|
|
- if ($res['errcode']==0){
|
|
|
- return true;
|
|
|
- }else{
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $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;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * MiniProgran - 小程序码
|
|
|
- * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。
|
|
|
- * @param string
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- public function wxacode_get_unlimited($scene, $page="", $width=430, $filepath)
|
|
|
- {
|
|
|
- $access_token = $this->get_access_token();
|
|
|
- $url = self::API_DOMAIN . 'wxa/getwxacodeunlimit?access_token=' . $access_token;
|
|
|
- $postarray = array(
|
|
|
- '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;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
}
|