更新内核

增加自定义表单(未完成)
This commit is contained in:
2017-07-04 12:13:24 +08:00
parent 8af311aa07
commit aef819d417
82 changed files with 12328 additions and 59 deletions

View File

@@ -0,0 +1,3 @@
[2017/07/03 16:00:58] MSG - Get New AccessToken Success.
[2017/07/03 18:02:14] MSG - Get New AccessToken Success.
[2017/07/03 22:47:05] MSG - Get New AccessToken Success.

View File

@@ -0,0 +1 @@
a:2:{s:5:"value";s:138:"hX91IMjz-VD6-uRvXuhwuQb47YPpPKVZ0LQu_ToussTNxywjwyzt26ROt5_wFsRRoGfE9kdLffZmZFgiGf_xRaFHfka8aYQvknDUWXKr5g5lGg84tCKMjCifMBiRjSTkPYYdAGAUDD";s:7:"expired";i:1499098225;}

View File

@@ -0,0 +1 @@
a:2:{s:5:"value";s:86:"sM4AOVdWfPE4DxkXGEs8VIMcGjJAQX643GrxVbHHaF1crt_AsDafYIdQTn-FW5wCE3l-EU6K8o2ylaud4-1BAA";s:7:"expired";i:1499100325;}

View File

@@ -0,0 +1,93 @@
<?php
namespace Wechat\Lib;
use Wechat\Loader;
/**
* 微信SDK基础缓存类
*
* @author Anyon <zoujingli@qq.com>
* @date 2016-08-20 17:50
*/
class Cache {
/**
* 缓存位置
* @var string
*/
static public $cachepath;
/**
* 设置缓存
* @param string $name
* @param string $value
* @param int $expired
* @return mixed
*/
static public function set($name, $value, $expired = 0) {
if (isset(Loader::$callback['CacheSet'])) {
return call_user_func_array(Loader::$callback['CacheSet'], func_get_args());
}
$data = serialize(array('value' => $value, 'expired' => $expired > 0 ? time() + $expired : 0));
return self::check() && file_put_contents(self::$cachepath . $name, $data);
}
/**
* 读取缓存
* @param string $name
* @return mixed
*/
static public function get($name) {
if (isset(Loader::$callback['CacheGet'])) {
return call_user_func_array(Loader::$callback['CacheGet'], func_get_args());
}
if (self::check() && ($file = self::$cachepath . $name) && file_exists($file) && ($data = file_get_contents($file)) && !empty($data)) {
$data = unserialize($data);
if (isset($data['expired']) && ($data['expired'] > time() || $data['expired'] === 0)) {
return isset($data['value']) ? $data['value'] : null;
}
}
return null;
}
/**
* 删除缓存
* @param string $name
* @return mixed
*/
static public function del($name) {
if (isset(Loader::$callback['CacheDel'])) {
return call_user_func_array(Loader::$callback['CacheDel'], func_get_args());
}
return self::check() && @unlink(self::$cachepath . $name);
}
/**
* 输出内容到日志
* @param string $line
* @param string $filename
* @return mixed
*/
static public function put($line, $filename = '') {
if (isset(Loader::$callback['CachePut'])) {
return call_user_func_array(Loader::$callback['CachePut'], func_get_args());
}
empty($filename) && $filename = date('Ymd') . '.log';
return self::check() && file_put_contents(self::$cachepath . $filename, '[' . date('Y/m/d H:i:s') . "] {$line}\n", FILE_APPEND);
}
/**
* 检查缓存目录
* @return bool
*/
static protected function check() {
empty(self::$cachepath) && self::$cachepath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;
self::$cachepath = rtrim(self::$cachepath, '/\\') . DIRECTORY_SEPARATOR;
if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, TRUE)) {
return FALSE;
}
return TRUE;
}
}

View File

@@ -0,0 +1,179 @@
<?php
namespace Wechat\Lib;
use Prpcrypt;
use Wechat\Loader;
/**
* 微信SDK基础类
*
* @category WechatSDK
* @subpackage library
* @author Anyon <zoujingli@qq.com>
* @date 2016/05/28 11:55
*/
class Common {
/** API接口URL需要使用此前缀 */
const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com';
const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
const GET_TICKET_URL = '/ticket/getticket?';
const AUTH_URL = '/token?grant_type=client_credential&';
public $token;
public $encodingAesKey;
public $encrypt_type;
public $appid;
public $appsecret;
public $access_token;
public $postxml;
public $_msg;
public $errCode = 0;
public $errMsg = "";
public $config = array();
private $_retry = FALSE;
/**
* 构造方法
* @param array $options
*/
public function __construct($options = array()) {
$config = Loader::config($options);
$this->token = isset($config['token']) ? $config['token'] : '';
$this->appid = isset($config['appid']) ? $config['appid'] : '';
$this->appsecret = isset($config['appsecret']) ? $config['appsecret'] : '';
$this->encodingAesKey = isset($config['encodingaeskey']) ? $config['encodingaeskey'] : '';
$this->config = $config;
}
/**
* 接口验证
* @return bool
*/
public function valid() {
$encryptStr = "";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$postStr = file_get_contents("php://input");
$array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"] : '';
if ($this->encrypt_type == 'aes') {
$encryptStr = $array['Encrypt'];
!class_exists('Prpcrypt', FALSE) && require __DIR__ . '/Prpcrypt.php';
$pc = new Prpcrypt($this->encodingAesKey);
$array = $pc->decrypt($encryptStr, $this->appid);
if (!isset($array[0]) || intval($array[0]) > 0) {
$this->errCode = $array[0];
$this->errMsg = $array[1];
Tools::log("Interface Authentication Failed. {$this->errMsg}[{$this->errCode}]", 'ERR');
return false;
}
$this->postxml = $array[1];
empty($this->appid) && $this->appid = $array[2];
} else {
$this->postxml = $postStr;
}
} elseif (isset($_GET["echostr"])) {
if ($this->checkSignature()) {
exit($_GET["echostr"]);
} else {
return false;
}
}
if (!$this->checkSignature($encryptStr)) {
$this->errMsg = 'Interface authentication failed, please use the correct method to call.';
return false;
}
return true;
}
/**
* 验证来自微信服务器
* @param string $str
* @return bool
*/
private function checkSignature($str = '') {
// 如果存在加密验证则用加密验证段
$signature = isset($_GET["msg_signature"]) ? $_GET["msg_signature"] : (isset($_GET["signature"]) ? $_GET["signature"] : '');
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
$nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : '';
$tmpArr = array($this->token, $timestamp, $nonce, $str);
sort($tmpArr, SORT_STRING);
if (sha1(implode($tmpArr)) == $signature) {
return true;
} else {
return false;
}
}
/**
* 获取公众号访问 access_token
* @param string $appid 如在类初始化时已提供,则可为空
* @param string $appsecret 如在类初始化时已提供,则可为空
* @param string $token 手动指定access_token非必要情况不建议用
* @return bool|string
*/
public function getAccessToken($appid = '', $appsecret = '', $token = '') {
if (!$appid || !$appsecret) {
$appid = $this->appid;
$appsecret = $this->appsecret;
}
if ($token) {
return $this->access_token = $token;
}
$cache = 'wechat_access_token_' . $appid;
if (($access_token = Tools::getCache($cache)) && !empty($access_token)) {
return $this->access_token = $access_token;
}
# 检测事件注册
if (isset(Loader::$callback[__FUNCTION__])) {
return $this->access_token = call_user_func_array(Loader::$callback[__FUNCTION__], array(&$this, &$cache));
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::AUTH_URL . 'appid=' . $appid . '&secret=' . $appsecret);
if ($result) {
$json = json_decode($result, true);
if (!$json || isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
Tools::log("Get New AccessToken Error. {$this->errMsg}[{$this->errCode}]", 'ERR');
return false;
}
$this->access_token = $json['access_token'];
Tools::log("Get New AccessToken Success.");
Tools::setCache($cache, $this->access_token, 5000);
return $this->access_token;
}
return false;
}
/**
* 接口失败重试
* @param $method SDK方法名称
* @param array $arguments SDK方法参数
* @return bool|mixed
*/
protected function checkRetry($method, $arguments = array()) {
if (!$this->_retry && in_array($this->errCode, array('40014', '40001', '41001', '42001'))) {
Tools::log("Run {$method} Faild. {$this->errMsg}[{$this->errCode}]", 'ERR');
($this->_retry = true) && $this->resetAuth();
$this->errCode = 40001;
$this->errMsg = 'no access';
Tools::log("Retry Run {$method} ...");
return call_user_func_array(array($this, $method), $arguments);
}
return false;
}
/**
* 删除验证数据
* @param string $appid 如在类初始化时已提供,则可为空
* @return bool
*/
public function resetAuth($appid = '') {
$authname = 'wechat_access_token_' . (empty($appid) ? $this->appid : $appid);
Tools::log("Reset Auth And Remove Old AccessToken.");
$this->access_token = '';
Tools::removeCache($authname);
return true;
}
}

View File

@@ -0,0 +1,176 @@
<?php
/**
* PKCS7算法 加解密
* @category WechatSDK
* @subpackage library
* @date 2016/06/28 11:59
*/
class PKCS7Encoder {
public static $block_size = 32;
/**
* 对需要加密的明文进行填充补位
* @param string $text 需要进行填充补位操作的明文
* @return string 补齐明文字符串
*/
function encode($text) {
$amount_to_pad = PKCS7Encoder::$block_size - (strlen($text) % PKCS7Encoder::$block_size);
if ($amount_to_pad == 0) {
$amount_to_pad = PKCS7Encoder::$block_size;
}
$pad_chr = chr($amount_to_pad);
$tmp = "";
for ($index = 0; $index < $amount_to_pad; $index++) {
$tmp .= $pad_chr;
}
return $text . $tmp;
}
/**
* 对解密后的明文进行补位删除
* @param string $text 解密后的明文
* @return string 删除填充补位后的明文
*/
function decode($text) {
$pad = ord(substr($text, -1));
if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
$pad = 0;
}
return substr($text, 0, (strlen($text) - $pad));
}
}
/**
* 接收和推送给公众平台消息的加解密
* @category WechatSDK
* @subpackage library
* @date 2016/06/28 11:59
*/
class Prpcrypt {
public $key;
function __construct($k) {
$this->key = base64_decode($k . "=");
}
/**
* 对明文进行加密
* @param string $text 需要加密的明文
* @param string $appid 公众号APPID
* @return string 加密后的密文
*/
public function encrypt($text, $appid) {
try {
//获得16位随机字符串填充到明文之前
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid;
$iv = substr($this->key, 0, 16);
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
$encrypted = openssl_encrypt($text, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
return array(ErrorCode::$OK, $encrypted);
} catch (Exception $e) {
return array(ErrorCode::$EncryptAESError, null);
}
}
/**
* 对密文进行解密
* @param string $encrypted 需要解密的密文
* @param string $appid 公众号APPID
* @return string 解密得到的明文
*/
public function decrypt($encrypted, $appid) {
try {
$iv = substr($this->key, 0, 16);
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
} catch (Exception $e) {
return array(ErrorCode::$DecryptAESError, null);
}
try {
$pkc_encoder = new PKCS7Encoder;
$result = $pkc_encoder->decode($decrypted);
if (strlen($result) < 16) {
return "";
}
$content = substr($result, 16, strlen($result));
$len_list = unpack("N", substr($content, 0, 4));
$xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len);
$from_appid = substr($content, $xml_len + 4);
if (!$appid) {
$appid = $from_appid;
}
} catch (Exception $e) {
return array(ErrorCode::$IllegalBuffer, null);
}
return array(0, $xml_content, $from_appid);
}
/**
* 随机生成16位字符串
* @return string 生成的字符串
*/
function getRandomStr() {
$str = "";
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($str_pol) - 1;
for ($i = 0; $i < 16; $i++) {
$str .= $str_pol[mt_rand(0, $max)];
}
return $str;
}
}
/**
* 仅用作类内部使用
* 不用于官方API接口的errCode码
* Class ErrorCode
*/
class ErrorCode {
public static $OK = 0;
public static $ValidateSignatureError = 40001;
public static $ParseXmlError = 40002;
public static $ComputeSignatureError = 40003;
public static $IllegalAesKey = 40004;
public static $ValidateAppidError = 40005;
public static $EncryptAESError = 40006;
public static $DecryptAESError = 40007;
public static $IllegalBuffer = 40008;
public static $EncodeBase64Error = 40009;
public static $DecodeBase64Error = 40010;
public static $GenReturnXmlError = 40011;
public static $errCode = array(
'0' => '处理成功',
'40001' => '校验签名失败',
'40002' => '解析xml失败',
'40003' => '计算签名失败',
'40004' => '不合法的AESKey',
'40005' => '校验AppID失败',
'40006' => 'AES加密失败',
'40007' => 'AES解密失败',
'40008' => '公众平台发送的xml不合法',
'40009' => 'Base64编码失败',
'40010' => 'Base64解码失败',
'40011' => '公众帐号生成回包xml失败'
);
/**
* 获取错误消息内容
* @param string $err
* @return bool
*/
public static function getErrText($err) {
if (isset(self::$errCode[$err])) {
return self::$errCode[$err];
}
return false;
}
}

View File

@@ -0,0 +1,268 @@
<?php
namespace Wechat\Lib;
use CURLFile;
/**
* 微信接口通用类
*
* @category WechatSDK
* @subpackage library
* @author Anyon <zoujingli@qq.com>
* @date 2016/05/28 11:55
*/
class Tools {
/**
* 产生随机字符串
* @param int $length
* @param string $str
* @return string
*/
static public function createNoncestr($length = 32, $str = "") {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 获取签名
* @param array $arrdata 签名数组
* @param string $method 签名方法
* @return bool|string 签名值
*/
static public function getSignature($arrdata, $method = "sha1") {
if (!function_exists($method)) {
return false;
}
ksort($arrdata);
$params = array();
foreach ($arrdata as $key => $value) {
$params[] = "{$key}={$value}";
}
return $method(join('&', $params));
}
/**
* 生成支付签名
* @param array $option
* @param string $partnerKey
* @return string
*/
static public function getPaySign($option, $partnerKey) {
ksort($option);
$buff = '';
foreach ($option as $k => $v) {
$buff .= "{$k}={$v}&";
}
return strtoupper(md5("{$buff}key={$partnerKey}"));
}
/**
* XML编码
* @param mixed $data 数据
* @param string $root 根节点名
* @param string $item 数字索引的子节点名
* @param string $id 数字索引子节点key转换的属性名
* @return string
*/
static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id') {
return "<{$root}>" . self::_data_to_xml($data, $item, $id) . "</{$root}>";
}
static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '') {
foreach ($data as $key => $val) {
is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
$content .= "<{$key}>";
if (is_array($val) || is_object($val)) {
$content .= self::_data_to_xml($val);
} elseif (is_numeric($val)) {
$content .= $val;
} else {
$content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
}
list($_key,) = explode(' ', $key . ' ');
$content .= "</$_key>";
}
return $content;
}
/**
* 将xml转为array
* @param string $xml
* @return array
*/
static public function xml2arr($xml) {
return json_decode(Tools::json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
/**
* 生成安全JSON数据
* @param array $array
* @return string
*/
static public function json_encode($array) {
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), json_encode($array));
}
/**
* 以get方式提交请求
* @param $url
* @return bool|mixed
*/
static public function httpGet($url) {
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
/**
* 以post方式提交请求
* @param string $url
* @param array|string $data
* @return bool|mixed
*/
static public function httpPost($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
if (is_array($data)) {
foreach ($data as &$value) {
if (is_string($value) && stripos($value, '@') === 0 && class_exists('CURLFile', FALSE)) {
$value = new CURLFile(realpath(trim($value, '@')));
}
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
return $data;
}
return false;
}
/**
* 使用证书以post方式提交xml到对应的接口url
* @param string $url POST提交的内容
* @param array $postdata 请求的地址
* @param string $ssl_cer 证书Cer路径 | 证书内容
* @param string $ssl_key 证书Key路径 | 证书内容
* @param int $second 设置请求超时时间
* @return bool|mixed
*/
static public function httpsPost($url, $postdata, $ssl_cer = null, $ssl_key = null, $second = 30) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
/* 要求结果为字符串且输出到屏幕上 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
/* 设置证书 */
if (!is_null($ssl_cer) && file_exists($ssl_cer) && is_file($ssl_cer)) {
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $ssl_cer);
}
if (!is_null($ssl_key) && file_exists($ssl_key) && is_file($ssl_key)) {
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $ssl_key);
}
curl_setopt($ch, CURLOPT_POST, true);
if (is_array($postdata)) {
foreach ($postdata as &$data) {
if (is_string($data) && stripos($data, '@') === 0 && class_exists('CURLFile', FALSE)) {
$data = new CURLFile(realpath(trim($data, '@')));
}
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
return $result;
} else {
return false;
}
}
/**
* 读取微信客户端IP
* @return null|string
*/
static public function getAddress() {
foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP', 'REMOTE_ADDR') as $header) {
if (!isset($_SERVER[$header]) || ($spoof = $_SERVER[$header]) === NULL) {
continue;
}
sscanf($spoof, '%[^,]', $spoof);
if (!filter_var($spoof, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$spoof = NULL;
} else {
return $spoof;
}
}
return '0.0.0.0';
}
/**
* 设置缓存,按需重载
* @param string $cachename
* @param mixed $value
* @param int $expired
* @return bool
*/
static public function setCache($cachename, $value, $expired = 0) {
return Cache::set($cachename, $value, $expired);
}
/**
* 获取缓存,按需重载
* @param string $cachename
* @return mixed
*/
static public function getCache($cachename) {
return Cache::get($cachename);
}
/**
* 清除缓存,按需重载
* @param string $cachename
* @return bool
*/
static public function removeCache($cachename) {
return Cache::del($cachename);
}
/**
* SDK日志处理方法
* @param string $msg 日志行内容
* @param string $type 日志级别
*/
static public function log($msg, $type = 'MSG') {
Cache::put($type . ' - ' . $msg);
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace Wechat;
use Wechat\Lib\Cache;
/**
* 注册SDK自动加载机制
* @author Anyon <zoujingli@qq.com>
* @date 2016/10/26 10:21
*/
spl_autoload_register(function ($class) {
if (0 === stripos($class, 'Wechat\\')) {
$filename = dirname(__DIR__) . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
file_exists($filename) && require($filename);
}
});
/**
* 微信SDK加载器
* @author Anyon <zoujingli@qq.com>
* @date 2016-08-21 11:06
*/
class Loader {
/**
* 事件注册函数
* @var array
*/
static public $callback = array();
/**
* 配置参数
* @var array
*/
static protected $config = array();
/**
* 对象缓存
* @var array
*/
static protected $cache = array();
/**
* 动态注册SDK事件处理函数
* @param string $event 事件名称getAccessToken|getJsTicket
* @param string $method 处理方法(可以是普通方法或者类中的方法)
* @param string|null $class 处理对象(可以直接使用的类实例)
*/
static public function register($event, $method, $class = NULL) {
if (!empty($class) && class_exists($class, FALSE) && method_exists($class, $method)) {
self::$callback[$event] = array($class, $method);
} else {
self::$callback[$event] = $method;
}
}
/**
* 获取微信SDK接口对象(别名函数)
* @param string $type 接口类型(Card|Custom|Device|Extends|Media|Menu|Oauth|Pay|Receive|Script|User|Poi)
* @param array $config SDK配置(token,appid,appsecret,encodingaeskey,mch_id,partnerkey,ssl_cer,ssl_key,qrc_img)
* @return WechatCard|WechatCustom|WechatDevice|WechatExtends|WechatMedia|WechatMenu|WechatOauth|WechatPay|WechatPoi|WechatReceive|WechatScript|WechatService|WechatUser
*/
static public function & get_instance($type, $config = array()) {
return self::get($type, $config);
}
/**
* 获取微信SDK接口对象
* @param string $type 接口类型(Card|Custom|Device|Extends|Media|Menu|Oauth|Pay|Receive|Script|User|Poi)
* @param array $config SDK配置(token,appid,appsecret,encodingaeskey,mch_id,partnerkey,ssl_cer,ssl_key,qrc_img)
* @return WechatCard|WechatCustom|WechatDevice|WechatExtends|WechatMedia|WechatMenu|WechatOauth|WechatPay|WechatPoi|WechatReceive|WechatScript|WechatService|WechatUser
*/
static public function & get($type, $config = array()) {
$index = md5(strtolower($type) . md5(json_encode(self::$config)));
if (!isset(self::$cache[$index])) {
$basicName = 'Wechat' . ucfirst(strtolower($type));
$className = "\\Wechat\\{$basicName}";
// 注册类的无命名空间别名兼容未带命名空间的老版本SDK
!class_exists($basicName, FALSE) && class_alias($className, $basicName);
self::$cache[$index] = new $className(self::config($config));
}
return self::$cache[$index];
}
/**
* 设置配置参数
* @param array $config
* @return array
*/
static public function config($config = array()) {
!empty($config) && self::$config = array_merge(self::$config, $config);
if (!empty(self::$config['cachepath'])) {
Cache::$cachepath = self::$config['cachepath'];
}
if (empty(self::$config['component_verify_ticket'])) {
self::$config['component_verify_ticket'] = Cache::get('component_verify_ticket');
}
if (empty(self::$config['token']) && !empty(self::$config['component_token'])) {
self::$config['token'] = self::$config['component_token'];
}
if (empty(self::$config['appsecret']) && !empty(self::$config['component_appsecret'])) {
self::$config['appsecret'] = self::$config['component_appsecret'];
}
if (empty(self::$config['encodingaeskey']) && !empty(self::$config['component_encodingaeskey'])) {
self::$config['encodingaeskey'] = self::$config['component_encodingaeskey'];
}
return self::$config;
}
}

View File

@@ -0,0 +1,776 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信卡卷
*/
class WechatCard extends Common {
/** 卡券相关地址 */
const CARD_CREATE = '/card/create?';
// 删除卡卷
const CARD_DELETE = '/card/delete?';
// 更新卡卷信息
const CARD_UPDATE = '/card/update?';
// 获取卡卷详细信息
const CARD_GET = '/card/get?';
// 读取粉丝拥有的卡卷列表
const CARD_USER_GET_LIST = '/card/user/getcardlist?';
// 卡卷核查接口
const CARD_CHECKCODE = '/card/code/checkcode?';
// 卡卷图文群发获取HTML
const CARD_SET_SELFCONSUMECELL = '/card/selfconsumecell/set?';
const CARD_SEND_HTML = '/card/mpnews/gethtml?';
const CARD_BATCHGET = '/card/batchget?';
const CARD_MODIFY_STOCK = '/card/modifystock?';
const CARD_GETCOLORS = '/card/getcolors?';
const CARD_QRCODE_CREATE = '/card/qrcode/create?';
const CARD_CODE_CONSUME = '/card/code/consume?';
const CARD_CODE_DECRYPT = '/card/code/decrypt?';
const CARD_CODE_GET = '/card/code/get?';
const CARD_CODE_UPDATE = '/card/code/update?';
const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?';
const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?';
const CARD_MEETINGCARD_UPDATEUSER = '/card/meetingticket/updateuser?'; //更新会议门票
const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?'; //激活会员卡
const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?'; //更新会员卡
const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?'; //更新电影票(未加方法)
const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?'; //飞机票-在线选座(未加方法)
/** 更新红包金额 */
const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?';
/*买单接口*/
const CARD_PAYCELL_SET = '/card/paycell/set?';
/*设置开卡字段接口*/
const CARD_MEMBERCARD_ACTIVATEUSERFORM_SET = '/card/membercard/activateuserform/set?';
/**
* 获取微信卡券 api_ticket
* @param string $appid
* @param string $jsapi_ticket
* @return bool|string
*/
public function getJsCardTicket($appid = '', $jsapi_ticket = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$appid = empty($appid) ? $this->appid : $appid;
if ($jsapi_ticket) {
return $jsapi_ticket;
}
$authname = 'wechat_jsapi_ticket_wxcard_' . $appid;
if (($jsapi_ticket = Tools::getCache($authname))) {
return $jsapi_ticket;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::GET_TICKET_URL . "access_token={$this->access_token}" . '&type=wx_card');
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
$expire = $json['expires_in'] ? intval($json['expires_in']) - 100 : 3600;
Tools::setCache($authname, $json['ticket'], $expire);
return $json['ticket'];
}
return false;
}
/**
* 生成选择卡卷JS签名包
* @param string $cardid 卡券Id
* @param string $cardtype 卡券类型
* @param string $shopid 门店Id
* @return array
*/
public function createChooseCardJsPackage($cardid = NULL, $cardtype = NULL, $shopid = NULL) {
$data = array();
$data['api_ticket'] = $this->getJsCardTicket();
$data['app_id'] = $this->appid;
$data['timestamp'] = time();
$data['nonceStr'] = Tools::createNoncestr();
!empty($cardid) && $data['cardId'] = $cardid;
!empty($cardtype) && $data['cardType'] = $cardtype;
!empty($shopid) && $data['shopId'] = $shopid;
$data['cardSign'] = $this->getTicketSignature($data);
$data['signType'] = 'SHA1';
unset($data['api_ticket'], $data['app_id']);
return $data;
}
/**
* 生成添加卡卷JS签名包
* @param string|null $cardid 卡卷ID
* @param array $data 其它限定参数
* @return array
*/
public function createAddCardJsPackage($cardid = NULL, $data = array()) {
function _sign($cardid = NULL, $attr = array(), $self) {
unset($attr['outer_id']);
$attr['cardId'] = $cardid;
$attr['timestamp'] = time();
$attr['api_ticket'] = $self->getJsCardTicket();
$attr['nonce_str'] = Tools::createNoncestr();
$attr['signature'] = $self->getTicketSignature($attr);
unset($attr['api_ticket']);
return $attr;
}
$cardList = array();
if (is_array($cardid)) {
foreach ($cardid as $id) {
$cardList[] = array('cardId' => $id, 'cardExt' => json_encode(_sign($id, $data, $this)));
}
} else {
$cardList[] = array('cardId' => $cardid, 'cardExt' => json_encode(_sign($cardid, $data, $this)));
}
return array('cardList' => $cardList);
}
/**
* 获取微信卡券签名
* @param array $arrdata 签名数组
* @param string $method 签名方法
* @return bool|string 签名值
*/
public function getTicketSignature($arrdata, $method = "sha1") {
if (!function_exists($method)) {
return false;
}
$newArray = array();
foreach ($arrdata as $value) {
array_push($newArray, (string)$value);
}
sort($newArray, SORT_STRING);
return $method(implode($newArray));
}
/**
* 创建卡券
* @param array $data 卡券数据
* @return bool|array 返回数组中card_id为卡券ID
*/
public function createCard($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CREATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 更改卡券信息
* 调用该接口更新信息后会重新送审,卡券状态变更为待审核。已被用户领取的卡券会实时更新票面信息。
* @param string $data
* @return bool
*/
public function updateCard($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 删除卡券
* 允许商户删除任意一类卡券。删除卡券后,该卡券对应已生成的领取用二维码、添加到卡包 JS API 均会失效。
* 注意:删除卡券不能删除已被用户领取,保存在微信客户端中的卡券,已领取的卡券依旧有效。
* @param string $card_id 卡券ID
* @return bool
*/
public function delCard($card_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('card_id' => $card_id);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_DELETE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 获取粉丝下所有卡卷列表
* @param $openid 粉丝openid
* @param string $card_id 卡卷ID可不给
* @return bool|array
*/
public function getCardList($openid, $card_id = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid' => $openid);
!empty($card_id) && $data['card_id'] = $card_id;
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_USER_GET_LIST . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode']) || empty($json['card_list'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取图文消息群发卡券HTML
* @param string $card_id 卡卷ID
* @return bool|array
*/
public function getCardMpHtml($card_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('card_id' => $card_id);
!empty($card_id) && $data['card_id'] = $card_id;
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_SEND_HTML . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode']) || empty($json['card_list'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 卡卷code核查
* @param string $card_id 卡卷ID
* @param array $code_list 卡卷code列表一维数组
* @return bool|array
*/
public function checkCardCodeList($card_id, $code_list) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('card_id' => $card_id, 'code' => $code_list);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CHECKCODE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode']) || empty($json['card_list'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 查询卡券详情
* @param string $card_id 卡卷ID
* @return bool|array
*/
public function getCardInfo($card_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('card_id' => $card_id);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_GET . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取颜色列表
* 获得卡券的最新颜色列表,用于创建卡券
* @return bool|array
*/
public function getCardColors() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::CARD_GETCOLORS . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 生成卡券二维码
* 成功则直接返回ticket值可以用 getQRUrl($ticket) 换取二维码url
* @param string $card_id 卡券ID 必须
* @param string $code 指定卡券 code 码只能被领一次。use_custom_code 字段为 true 的卡券必须填写,非自定义 code 不必填写。
* @param string $openid 指定领取者的 openid只有该用户能领取。bind_openid 字段为 true 的卡券必须填写,非自定义 openid 不必填写。
* @param int $expire_seconds 指定二维码的有效时间,范围是 60 ~ 1800 秒。不填默认为永久有效。
* @param bool $is_unique_code 指定下发二维码,生成的二维码随机分配一个 code领取后不可再次扫描。填写 true 或 false。默认 false。
* @param string $balance 红包余额以分为单位。红包类型必填LUCKY_MONEY其他卡券类型不填。
* @return bool|string
*/
public function createCardQrcode($card_id, $code = '', $openid = '', $expire_seconds = 0, $is_unique_code = false, $balance = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$card = array('card_id' => $card_id);
!empty($code) && $card['code'] = $code;
!empty($openid) && $card['openid'] = $openid;
!empty($is_unique_code) && $card['is_unique_code'] = $is_unique_code;
!empty($balance) && $card['balance'] = $balance;
$data = array('action_name' => "QR_CARD");
!empty($expire_seconds) && $data['expire_seconds'] = $expire_seconds;
$data['action_info'] = array('card' => $card);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_QRCODE_CREATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 消耗 code
* 自定义 codeuse_custom_code 为 true的优惠券在 code 被核销时,必须调用此接口。
* @param string $code 要消耗的序列号
* @param string $card_id 要消耗序列号所述的 card_id创建卡券时use_custom_code 填写 true 时必填。
* @return bool|array
* {
* "errcode":0,
* "errmsg":"ok",
* "card":{"card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"},
* "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA"
* }
*/
public function consumeCardCode($code, $card_id = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('code' => $code);
!empty($card_id) && $data['card_id'] = $card_id;
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_CONSUME . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* code 解码
* @param string $encrypt_code 通过 choose_card_info 获取的加密字符串
* @return bool|array
* {
* "errcode":0,
* "errmsg":"ok",
* "code":"751234212312"
* }
*/
public function decryptCardCode($encrypt_code) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('encrypt_code' => $encrypt_code,);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_DECRYPT . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 查询 code 的有效性(非自定义 code
* @param string $code
* @return bool|array
* {
* "errcode":0,
* "errmsg":"ok",
* "openid":"oFS7Fjl0WsZ9AMZqrI80nbIq8xrA", //用户 openid
* "card":{
* "card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
* "begin_time": 1404205036, //起始使用时间
* "end_time": 1404205036, //结束时间
* }
* }
*/
public function checkCardCode($code) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('code' => $code);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_GET . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 批量查询卡列表
* @param int $offset 开始拉取的偏移默认为0从头开始
* @param int $count 需要查询的卡片的数量数量最大50,默认50
* @return bool|array
* {
* "errcode":0,
* "errmsg":"ok",
* "card_id_list":["ph_gmt7cUVrlRk8swPwx7aDyF-pg"], //卡 id 列表
* "total_num":1 //该商户名下 card_id 总数
* }
*/
public function getCardIdList($offset = 0, $count = 50) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$count > 50 && $count = 50;
$data = array('offset' => $offset, 'count' => $count);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_BATCHGET . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 更改 code
* 为确保转赠后的安全性微信允许自定义code的商户对已下发的code进行更改。
* 注为避免用户疑惑建议仅在发生转赠行为后发生转赠后微信会通过事件推送的方式告知商户被转赠的卡券code对用户的code进行更改。
* @param string $code 卡券的 code 编码
* @param string $card_id 卡券 ID
* @param string $new_code 新的卡券 code 编码
* @return bool
*/
public function updateCardCode($code, $card_id, $new_code) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('code' => $code, 'card_id' => $card_id, 'new_code' => $new_code);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 设置卡券失效
* 设置卡券失效的操作不可逆
* @param string $code 需要设置为失效的 code
* @param string $card_id 自定义 code 的卡券必填。非自定义 code 的卡券不填。
* @return bool
*/
public function unavailableCardCode($code, $card_id = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('code' => $code);
!empty($card_id) && $data['card_id'] = $card_id;
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_CODE_UNAVAILABLE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 库存修改
* @param string $data
* @return bool
*/
public function modifyCardStock($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MODIFY_STOCK . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 更新门票
* @param string $data
* @return bool
*/
public function updateMeetingCard($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEETINGCARD_UPDATEUSER . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 激活/绑定会员卡
* @param string $data 具体结构请参看卡券开发文档(6.1.1 激活/绑定会员卡)章节
* @return bool
*/
public function activateMemberCard($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 会员卡交易
* 会员卡交易后每次积分及余额变更需通过接口通知微信,便于后续消息通知及其他扩展功能。
* @param string $data 具体结构请参看卡券开发文档(6.1.2 会员卡交易)章节
* @return bool|array
*/
public function updateMemberCard($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_UPDATEUSER . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 设置卡券测试白名单
* @param array $openid 测试的 openid 列表
* @param array $user 测试的微信号列表
* @return bool
*/
public function setCardTestWhiteList($openid = array(), $user = array()) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array();
count($openid) > 0 && $data['openid'] = $openid;
count($user) > 0 && $data['username'] = $user;
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_TESTWHILELIST_SET . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 更新红包金额
* @param string $code 红包的序列号
* @param int $balance 红包余额
* @param string $card_id 自定义 code 的卡券必填。非自定义 code 可不填。
* @return bool|array
*/
public function updateLuckyMoney($code, $balance, $card_id = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('code' => $code, 'balance' => $balance);
!empty($card_id) && $data['card_id'] = $card_id;
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_LUCKYMONEY_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 设置自助核销接口
* @param string $card_id 卡券ID
* @param bool $is_openid 是否开启自助核销功能填true/false默认为false
* @param bool $need_verify_cod 用户核销时是否需要输入验证码填true/false默认为false
* @param bool $need_remark_amount 用户核销时是否需要备注核销金额填true/false默认为false
* @return bool|array
*/
public function setSelfconsumecell($card_id, $is_openid = false, $need_verify_cod = false, $need_remark_amount = false) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array(
'card_id' => $card_id,
'is_open' => $is_openid,
'need_verify_cod' => $need_verify_cod,
'need_remark_amount' => $need_remark_amount,
);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_SET_SELFCONSUMECELL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 设置买单接口
* @param string $card_id
* @param bool $is_openid
* @return bool|mixed
*/
public function setPaycell($card_id, $is_openid = true) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array(
'card_id' => $card_id,
'is_open' => $is_openid,
);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_PAYCELL_SET . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 设置开卡字段信息接口
* @param array $data
* @return bool|array
*/
public function setMembercardActivateuserform($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_MEMBERCARD_ACTIVATEUSERFORM_SET . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}

View File

@@ -0,0 +1,338 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
class WechatCustom extends Common {
/** 多客服相关地址 */
const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?';
const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
const CUSTOM_SESSION_CREATE = '/customservice/kfsession/create?';
const CUSTOM_SESSION_CLOSE = '/customservice/kfsession/close?';
const CUSTOM_SESSION_SWITCH = '/customservice/kfsession/switch?';
const CUSTOM_SESSION_GET = '/customservice/kfsession/getsession?';
const CUSTOM_SESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
const CUSTOM_SESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?';
const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?';
const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?';
const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?';
const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?';
/**
* 获取多客服会话记录
* @param array $data 数据结构 {"starttime":123456789,"endtime":987654321,"openid":"OPENID","pagesize":10,"pageindex":1,}
* @return bool|array
*/
public function getCustomServiceMessage($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_RECORD . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 获取多客服客服基本信息
*
* @return bool|array
*/
public function getCustomServiceKFlist() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_KFLIST . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取多客服在线客服接待信息
*
* @return bool|array
*/
public function getCustomServiceOnlineKFlist() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_ONLINEKFLIST . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 创建指定多客服会话
* @tutorial 当用户已被其他客服接待或指定客服不在线则会失败
* @param string $openid //用户openid
* @param string $kf_account //客服账号
* @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
* @return bool|array
*/
public function createKFSession($openid, $kf_account, $text = '') {
$data = array("openid" => $openid, "kf_account" => $kf_account);
if ($text) {
$data["text"] = $text;
}
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_CREATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 关闭指定多客服会话
* @tutorial 当用户被其他客服接待时则会失败
* @param string $openid //用户openid
* @param string $kf_account //客服账号
* @param string $text //附加信息,文本会展示在客服人员的多客服客户端,可为空
* @return bool | array //成功返回json数组
* {
* "errcode": 0,
* "errmsg": "ok",
* }
*/
public function closeKFSession($openid, $kf_account, $text = '') {
$data = array("openid" => $openid, "kf_account" => $kf_account);
if ($text) {
$data["text"] = $text;
}
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_CLOSE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取用户会话状态
* @param string $openid //用户openid
* @return bool | array //成功返回json数组
* {
* "errcode" : 0,
* "errmsg" : "ok",
* "kf_account" : "test1@test", //正在接待的客服
* "createtime": 123456789, //会话接入时间
* }
*/
public function getKFSession($openid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_GET . "access_token={$this->access_token}" . '&openid=' . $openid);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取指定客服的会话列表
* @param string $kf_account //用户openid
* @return bool | array //成功返回json数组
* array(
* 'sessionlist' => array (
* array (
* 'openid'=>'OPENID', //客户 openid
* 'createtime'=>123456789, //会话创建时间UNIX 时间戳
* ),
* array (
* 'openid'=>'OPENID', //客户 openid
* 'createtime'=>123456789, //会话创建时间UNIX 时间戳
* ),
* )
* )
*/
public function getKFSessionlist($kf_account) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_GET_LIST . "access_token={$this->access_token}" . '&kf_account=' . $kf_account);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取未接入会话列表
* @return bool|array
*/
public function getKFSessionWait() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::CUSTOM_SESSION_GET_WAIT . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 添加客服账号
*
* @param string $account 完整客服账号(账号前缀@公众号微信号账号前缀最多10个字符)
* @param string $nickname 客服昵称最长6个汉字或12个英文字符
* @param string $password 客服账号明文登录密码,会自动加密
* @return bool|array
*/
public function addKFAccount($account, $nickname, $password) {
$data = array("kf_account" => $account, "nickname" => $nickname, "password" => md5($password));
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_ADD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 修改客服账号信息
*
* @param string $account //完整客服账号,格式为:账号前缀@公众号微信号账号前缀最多10个字符必须是英文或者数字字符
* @param string $nickname //客服昵称最长6个汉字或12个英文字符
* @param string $password //客服账号明文登录密码,会自动加密
* @return bool|array
* 成功返回结果
* {
* "errcode": 0,
* "errmsg": "ok",
* }
*/
public function updateKFAccount($account, $nickname, $password) {
$data = array("kf_account" => $account, "nickname" => $nickname, "password" => md5($password));
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_UPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 删除客服账号
* @param string $account 完整客服账号(账号前缀@公众号微信号账号前缀最多10个字符)
* @return bool|array
*/
public function deleteKFAccount($account) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_DEL_URL . "access_token={$this->access_token}" . '&kf_account=' . $account);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 上传客服头像
* @param string $account 完整客服账号(账号前缀@公众号微信号账号前缀最多10个字符)
* @param string $imgfile 头像文件完整路径,如:'D:\user.jpg'。头像文件必须JPG格式像素建议640*640
* @return bool|array
*/
public function setKFHeadImg($account, $imgfile) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL . "access_token={$this->access_token}" . '&kf_account=' . $account, array('media' => '@' . $imgfile), true);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}

View File

@@ -0,0 +1,441 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信设备相关SDK
* @author Anyon <zoujingli@qq.com>
* @date 2016-08-22 10:35
*/
class WechatDevice extends Common {
const SHAKEAROUND_DEVICE_APPLYID = '/shakearound/device/applyid?'; //申请设备ID
const SHAKEAROUND_DEVICE_APPLYSTATUS = '/shakearound/device/applystatus?'; //查询设备ID申请审核状态
const SHAKEAROUND_DEVICE_UPDATE = '/shakearound/device/update?'; //编辑设备信息
const SHAKEAROUND_DEVICE_SEARCH = '/shakearound/device/search?'; //查询设备列表
const SHAKEAROUND_DEVICE_BINDLOCATION = '/shakearound/device/bindlocation?'; //配置设备与门店ID的关系
const SHAKEAROUND_DEVICE_BINDPAGE = '/shakearound/device/bindpage?'; //配置设备与页面的绑定关系
const SHAKEAROUND_MATERIAL_ADD = '/shakearound/material/add?'; //上传摇一摇图片素材
const SHAKEAROUND_PAGE_ADD = '/shakearound/page/add?'; //增加页面
const SHAKEAROUND_PAGE_UPDATE = '/shakearound/page/update?'; //编辑页面
const SHAKEAROUND_PAGE_SEARCH = '/shakearound/page/search?'; //查询页面列表
const SHAKEAROUND_PAGE_DELETE = '/shakearound/page/delete?'; //删除页面
const SHAKEAROUND_USER_GETSHAKEINFO = '/shakearound/user/getshakeinfo?'; //获取摇周边的设备及用户信息
const SHAKEAROUND_STATISTICS_DEVICE = '/shakearound/statistics/device?'; //以设备为维度的数据统计接口
const SHAKEAROUND_STATISTICS_PAGE = '/shakearound/statistics/page?'; //以页面为维度的数据统计接口
/**
* 申请设备ID
* @param array $data
* @return bool|array
*/
public function applyShakeAroundDevice($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_APPLYID . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 查询设备ID申请审核状态
* @param int $apply_id
* @return bool|array
*/
public function applyStatusShakeAroundDevice($apply_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array("apply_id" => $apply_id);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_APPLYSTATUS . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 编辑设备信息
* @param array $data
* @return bool
*/
public function updateShakeAroundDevice($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 查询设备列表
* @param $data
* @return bool|array
*/
public function searchShakeAroundDevice($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_SEARCH . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 配置设备与门店的关联关系
* @param string $device_id 设备编号若填了UUID、major、minor则可不填设备编号若二者都填则以设备编号为优先
* @param int $poi_id 待关联的门店ID
* @param string $uuid UUID、major、minor三个信息需填写完整若填了设备编号则可不填此信息
* @param int $major
* @param int $minor
* @return bool|array
*/
public function bindLocationShakeAroundDevice($device_id, $poi_id, $uuid = '', $major = 0, $minor = 0) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if (!$device_id) {
if (!$uuid || !$major || !$minor) {
return false;
}
$device_identifier = array('uuid' => $uuid, 'major' => $major, 'minor' => $minor);
} else {
$device_identifier = array(
'device_id' => $device_id
);
}
$data = array('device_identifier' => $device_identifier, 'poi_id' => $poi_id);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDLOCATION . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json; //这个可以更改为返回true
}
return false;
}
/**
* 配置设备与其他公众账号门店的关联关系
* @param type $device_identifier 设备信息
* @param type $poi_id 待关联的门店ID
* @param type $poi_appid 目标微信appid
* @return boolean
*/
public function bindLocationOtherShakeAroundDevice($device_identifier, $poi_id, $poi_appid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('device_identifier' => $device_identifier, 'poi_id' => $poi_id, "type" => 2, "poi_appid" => $poi_appid);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDLOCATION . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json; //这个可以更改为返回true
}
return false;
}
/**
* 配置设备与页面的关联关系
* @param string $device_id 设备编号若填了UUID、major、minor则可不填设备编号若二者都填则以设备编号为优先
* @param array $page_ids 待关联的页面列表
* @param int $bind 关联操作标志位, 0 为解除关联关系1 为建立关联关系
* @param int $append 新增操作标志位, 0 为覆盖1 为新增
* @param string $uuid UUID、major、minor三个信息需填写完整若填了设备编号则可不填此信息
* @param int $major
* @param int $minor
* @return bool|array
*/
public function bindPageShakeAroundDevice($device_id, $page_ids = array(), $bind = 1, $append = 1, $uuid = '', $major = 0, $minor = 0) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if (!$device_id) {
if (!$uuid || !$major || !$minor) {
return false;
}
$device_identifier = array('uuid' => $uuid, 'major' => $major, 'minor' => $minor);
} else {
$device_identifier = array('device_id' => $device_id);
}
$data = array('device_identifier' => $device_identifier, 'page_ids' => $page_ids, 'bind' => $bind, 'append' => $append);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_DEVICE_BINDPAGE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 上传在摇一摇页面展示的图片素材
* @param array $data {"media":'@Path\filename.jpg'}
* @return bool|array
*/
public function uploadShakeAroundMedia($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_MATERIAL_ADD . "access_token={$this->access_token}", $data, true);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 增加摇一摇出来的页面信息
* @param string $title 在摇一摇页面展示的主标题不超过6 个字
* @param string $description 在摇一摇页面展示的副标题不超过7 个字
* @param string $icon_url 在摇一摇页面展示的图片, 格式限定为jpg,jpeg,png,gif; 建议120*120 限制不超过200*200
* @param string $page_url 跳转链接
* @param string $comment 页面的备注信息不超过15 个字,可不填
* @return bool|array
*/
public function addShakeAroundPage($title, $description, $icon_url, $page_url, $comment = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array("title" => $title, "description" => $description, "icon_url" => $icon_url, "page_url" => $page_url, "comment" => $comment);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_ADD . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 编辑摇一摇出来的页面信息
* @param int $page_id
* @param string $title 在摇一摇页面展示的主标题不超过6 个字
* @param string $description 在摇一摇页面展示的副标题不超过7 个字
* @param string $icon_url 在摇一摇页面展示的图片, 格式限定为jpg,jpeg,png,gif; 建议120*120 限制不超过200*200
* @param string $page_url 跳转链接
* @param string $comment 页面的备注信息不超过15 个字,可不填
* @return bool|array
*/
public function updateShakeAroundPage($page_id, $title, $description, $icon_url, $page_url, $comment = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array("page_id" => $page_id, "title" => $title, "description" => $description, "icon_url" => $icon_url, "page_url" => $page_url, "comment" => $comment);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 查询已有的页面
* @param array $page_ids
* @param int $begin
* @param int $count
* @return bool|mixed
*/
public function searchShakeAroundPage($page_ids = array(), $begin = 0, $count = 1) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if (!empty($page_ids)) {
$data = array('page_ids' => $page_ids);
} else {
$data = array('begin' => $begin, 'count' => $count);
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_SEARCH . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 删除已有的页面
* @param array $page_ids
* @return bool|array
*/
public function deleteShakeAroundPage($page_ids = array()) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('page_ids' => $page_ids);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_PAGE_DELETE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取设备信息
* @param string $ticket 摇周边业务的ticket(可在摇到的URL中得到ticket生效时间为30 分钟)
* @return bool|array
*/
public function getShakeInfoShakeAroundUser($ticket) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('ticket' => $ticket);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_USER_GETSHAKEINFO . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 以设备为维度的数据统计接口
* @param int $device_id 设备编号若填了UUID、major、minor即可不填设备编号二者选其一
* @param int $begin_date 起始日期时间戳最长时间跨度为30 天
* @param int $end_date 结束日期时间戳最长时间跨度为30 天
* @param string $uuid UUID、major、minor三个信息需填写完成若填了设备编辑即可不填此信息二者选其一
* @param int $major
* @param int $minor
* @return bool|array
*/
public function deviceShakeAroundStatistics($device_id, $begin_date, $end_date, $uuid = '', $major = 0, $minor = 0) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if (!$device_id) {
if (!$uuid || !$major || !$minor) {
return false;
}
$device_identifier = array('uuid' => $uuid, 'major' => $major, 'minor' => $minor);
} else {
$device_identifier = array('device_id' => $device_id);
}
$data = array('device_identifier' => $device_identifier, 'begin_date' => $begin_date, 'end_date' => $end_date);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 以页面为维度的数据统计接口
* @param int $page_id 指定页面的ID
* @param int $begin_date 起始日期时间戳最长时间跨度为30 天
* @param int $end_date 结束日期时间戳最长时间跨度为30 天
* @return bool|array
*/
public function pageShakeAroundStatistics($page_id, $begin_date, $end_date) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('page_id' => $page_id, 'begin_date' => $begin_date, 'end_date' => $end_date);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SHAKEAROUND_STATISTICS_DEVICE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}

View File

@@ -0,0 +1,197 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信扩展功能
*
* @author Anyon <zoujingli@qq.com>
* @date 2016-08-22 10:32
*/
class WechatExtends extends Common {
const QR_LIMIT_SCENE = 1;
/** 语义理解 */
const SEMANTIC_API_URL = '/semantic/semproxy/search?';
const QRCODE_IMG_URL = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';
const QRCODE_CREATE_URL = '/qrcode/create?';
const SHORT_URL = '/shorturl?';
const QR_SCENE = 0;
/** 数据分析接口 */
static $DATACUBE_URL_ARR = array(//用户分析
'user' => array(
'summary' => '/datacube/getusersummary?', //获取用户增减数据getusersummary
'cumulate' => '/datacube/getusercumulate?', //获取累计用户数据getusercumulate
),
'article' => array(//图文分析
'summary' => '/datacube/getarticlesummary?', //获取图文群发每日数据getarticlesummary
'total' => '/datacube/getarticletotal?', //获取图文群发总数据getarticletotal
'read' => '/datacube/getuserread?', //获取图文统计数据getuserread
'readhour' => '/datacube/getuserreadhour?', //获取图文统计分时数据getuserreadhour
'share' => '/datacube/getusershare?', //获取图文分享转发数据getusershare
'sharehour' => '/datacube/getusersharehour?', //获取图文分享转发分时数据getusersharehour
),
'upstreammsg' => array(//消息分析
'summary' => '/datacube/getupstreammsg?', //获取消息发送概况数据getupstreammsg
'hour' => '/datacube/getupstreammsghour?', //获取消息分送分时数据getupstreammsghour
'week' => '/datacube/getupstreammsgweek?', //获取消息发送周数据getupstreammsgweek
'month' => '/datacube/getupstreammsgmonth?', //获取消息发送月数据getupstreammsgmonth
'dist' => '/datacube/getupstreammsgdist?', //获取消息发送分布数据getupstreammsgdist
'distweek' => '/datacube/getupstreammsgdistweek?', //获取消息发送分布周数据getupstreammsgdistweek
'distmonth' => '/datacube/getupstreammsgdistmonth?', //获取消息发送分布月数据getupstreammsgdistmonth
),
'interface' => array(//接口分析
'summary' => '/datacube/getinterfacesummary?', //获取接口分析数据getinterfacesummary
'summaryhour' => '/datacube/getinterfacesummaryhour?', //获取接口分析分时数据getinterfacesummaryhour
)
);
/**
* 获取二维码图片
* @param string $ticket 传入由getQRCode方法生成的ticket参数
* @return string url 返回http地址
*/
public function getQRUrl($ticket) {
return self::QRCODE_IMG_URL . urlencode($ticket);
}
/**
* 长链接转短链接接口
* @param string $long_url 传入要转换的长url
* @return bool|string url 成功则返回转换后的短url
*/
public function getShortUrl($long_url) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array(
'action' => 'long2short',
'long_url' => $long_url
);
$result = Tools::httpPost(self::API_URL_PREFIX . self::SHORT_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json['short_url'];
}
return false;
}
/**
* 创建二维码ticket
* @param int|string $scene_id 自定义追踪id,临时二维码只能用数值型
* @param int $type 0:临时二维码1:永久二维码(此时expire参数无效)2:永久二维码(此时expire参数无效)
* @param int $expire 临时二维码有效期最大为2592000秒(30天)
* @return bool|array ('ticket'=>'qrcode字串','expire_seconds'=>2592000,'url'=>'二维码图片解析后的地址')
*/
public function getQRCode($scene_id, $type = 0, $expire = 2592000) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$type = ($type && is_string($scene_id)) ? 2 : $type;
$data = array(
'action_name' => $type ? ($type == 2 ? "QR_LIMIT_STR_SCENE" : "QR_LIMIT_SCENE") : "QR_SCENE",
'expire_seconds' => $expire,
'action_info' => array('scene' => ($type == 2 ? array('scene_str' => $scene_id) : array('scene_id' => $scene_id)))
);
if ($type == 1) {
unset($data['expire_seconds']);
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::QRCODE_CREATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 语义理解接口
* @param string $uid 用户唯一id非开发者id用户区分公众号下的不同用户建议填入用户openid
* @param string $query 输入文本串
* @param string $category 需要使用的服务类型,多个用“,”隔开,不能为空
* @param float $latitude 纬度坐标,与经度同时传入;与城市二选一传入
* @param float $longitude 经度坐标,与纬度同时传入;与城市二选一传入
* @param string $city 城市名称,与经纬度二选一传入
* @param string $region 区域名称,在城市存在的情况下可省略;与经纬度二选一传入
* @return bool|array
*/
public function querySemantic($uid, $query, $category, $latitude = 0.00, $longitude = 0.00, $city = "", $region = "") {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array(
'query' => $query,
'category' => $category,
'appid' => $this->appid,
'uid' => ''
);
//地理坐标或城市名称二选一
if ($latitude) {
$data['latitude'] = $latitude;
$data['longitude'] = $longitude;
} elseif ($city) {
$data['city'] = $city;
} elseif ($region) {
$data['region'] = $region;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::SEMANTIC_API_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取统计数据
* @param string $type 数据分类(user|article|upstreammsg|interface)分别为(用户分析|图文分析|消息分析|接口分析)
* @param string $subtype 数据子分类,参考 DATACUBE_URL_ARR 常量定义部分 或者README.md说明文档
* @param string $begin_date 开始时间
* @param string $end_date 结束时间
* @return bool|array 成功返回查询结果数组,其定义请看官方文档
*/
public function getDatacube($type, $subtype, $begin_date, $end_date = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if (!isset(self::$DATACUBE_URL_ARR[$type]) || !isset(self::$DATACUBE_URL_ARR[$type][$subtype])) {
return false;
}
$data = array(
'begin_date' => $begin_date,
'end_date' => $end_date ? $end_date : $begin_date
);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::$DATACUBE_URL_ARR[$type][$subtype] . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return isset($json['list']) ? $json['list'] : $json;
}
return false;
}
}

View File

@@ -0,0 +1,142 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
class WechatHardware extends Common {
const DEVICE_AUTHORIZE_DEVICE = '/device/authorize_device?'; //设备设全
const DEVICE_GETQRCODE = '/device/getqrcode?'; //设备授权新接口
const DEVICE_CREATE_QRCODE = '/device/create_qrcode?'; //获取设备二维码
const DEVICE_GET_STAT = '/device/get_stat?'; //获取设备状态
const DEVICE_TRANSMSG = '/device/transmsg?'; //主动发送消息给设备
const DEVICE_COMPEL_UNBINDHTTPS = '/device/compel_unbind?'; //强制解绑用户和设备
/**
* 强制解绑用户和设备
* @param $data
* @return bool|mixed
*/
public function deviceCompelUnbindhttps($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::DEVICE_COMPEL_UNBINDHTTPS . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
public function transmsg($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::DEVICE_TRANSMSG . "access_token={$this->access_token}", Tools::json_encode($data));
//dump($result);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
public function getQrcode($product_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::DEVICE_GETQRCODE . "access_token={$this->access_token}&product_id=$product_id");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 设备授权
* @param $data
* @return bool|mixed
*/
public function deviceAuthorize($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::DEVICE_AUTHORIZE_DEVICE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取设备二维码
* @param $data
* @return bool|mixed
*/
public function getDeviceQrcode($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::DEVICE_CREATE_QRCODE . "access_token={$this->access_token}", Tools::json_encode($data));
//dump($result);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取设备状态
* @param $device_id
* @return bool|mixed
*/
public function getDeviceStat($device_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::DEVICE_GET_STAT . "access_token={$this->access_token}&device_id=$device_id");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}

View File

@@ -0,0 +1,417 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信媒体素材管理类
*
* @author Anyon <zoujingli@qq.com>
* @date 2016/10/26 14:47
*/
class WechatMedia extends Common {
const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin';
const MEDIA_UPLOAD_URL = '/media/upload?';
const MEDIA_UPLOADIMG_URL = '/media/uploadimg?'; //图片上传接口
const MEDIA_GET_URL = '/media/get?';
const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?';
const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
const MEDIA_FOREVER_GET_URL = '/material/get_material?';
const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?';
/**
* 上传临时素材有效期为3天(认证后的订阅号可用)
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* 注意临时素材的media_id是可复用的
* @param array $data {"media":'@Path\filename.jpg'}
* @param string $type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @return bool|array
*/
public function uploadMedia($data, $type) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_UPLOAD_URL . "access_token={$this->access_token}" . '&type=' . $type, $data, true);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取临时素材(认证后的订阅号可用)
* @param string $media_id 媒体文件id
* @param bool $is_video 是否为视频文件,默认为否
* @return bool|array
*/
public function getMedia($media_id, $is_video = false) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
//如果要获取的素材是视频文件时不能使用https协议必须更换成http协议
$url_prefix = $is_video ? str_replace('https', 'http', self::API_URL_PREFIX) : self::API_URL_PREFIX;
$result = Tools::httpGet($url_prefix . self::MEDIA_GET_URL . "access_token={$this->access_token}" . '&media_id=' . $media_id);
if ($result) {
if (is_string($result)) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
}
return $result;
}
return false;
}
/**
* 获取临时素材(认证后的订阅号可用) 包含返回的http头信息
* @param string $media_id 媒体文件id
* @param bool $is_video 是否为视频文件,默认为否
* @return bool|array
*/
public function getMediaWithHttpInfo($media_id, $is_video = false) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
//如果要获取的素材是视频文件时不能使用https协议必须更换成http协议
$url_prefix = $is_video ? str_replace('https', 'http', self::API_URL_PREFIX) : self::API_URL_PREFIX;
$url = $url_prefix . self::MEDIA_GET_URL . "access_token={$this->access_token}" . '&media_id=' . $media_id;
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
$result = [];
if (intval($aStatus["http_code"]) !== 200) {
return false;
}
if ($sContent) {
if (is_string($sContent)) {
$json = json_decode($sContent, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
}
$result['content'] = $sContent;
$result['info'] = $aStatus;
return $result;
}
return false;
}
/**
* 上传图片本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式大小必须在1MB以下。 (认证后的订阅号可用)
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* @param array $data {"media":'@Path\filename.jpg'}
* @return bool|array
*/
public function uploadImg($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
/* 原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀 */
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_UPLOADIMG_URL . "access_token={$this->access_token}", $data, true);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 上传永久素材(认证后的订阅号可用)
* 新增的永久素材也可以在公众平台官网素材管理模块中看到
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* @param array $data {"media":'@Path\filename.jpg'}
* @param string $type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @param bool $is_video 是否为视频文件,默认为否
* @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'=>'视频标题','introduction'=>'描述')
* @return bool|array
*/
public function uploadForeverMedia($data, $type, $is_video = false, $video_info = array()) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if ($is_video) {
$data['description'] = Tools::json_encode($video_info);
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_UPLOAD_URL . "access_token={$this->access_token}" . '&type=' . $type, $data, true);
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 上传永久图文素材(认证后的订阅号可用)
* 新增的永久素材也可以在公众平台官网素材管理模块中看到
* @param array $data 消息结构{"articles":[{...}]}
* @return bool|array
*/
public function uploadForeverArticles($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_NEWS_UPLOAD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 修改永久图文素材(认证后的订阅号可用)
* 永久素材也可以在公众平台官网素材管理模块中看到
* @param string $media_id 图文素材id
* @param array $data 消息结构{"articles":[{...}]}
* @param int $index 更新的文章在图文素材的位置第一篇为0仅多图文使用
* @return bool|array
*/
public function updateForeverArticles($media_id, $data, $index = 0) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
if (!isset($data['media_id'])) {
$data['media_id'] = $media_id;
}
if (!isset($data['index'])) {
$data['index'] = $index;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_NEWS_UPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取永久素材(认证后的订阅号可用)
* 返回图文消息数组或二进制数据失败返回false
* @param string $media_id 媒体文件id
* @param bool $is_video 是否为视频文件,默认为否
* @return bool|array|raw data
*/
public function getForeverMedia($media_id, $is_video = false) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('media_id' => $media_id);
//#TODO 暂不确定此接口是否需要让视频文件走http协议
//如果要获取的素材是视频文件时不能使用https协议必须更换成http协议
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_GET_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
if (is_string($result)) {
$json = json_decode($result, true);
if ($json) {
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
} else {
return $result;
}
}
return $result;
}
return false;
}
/**
* 删除永久素材(认证后的订阅号可用)
* @param string $media_id 媒体文件id
* @return bool
*/
public function delForeverMedia($media_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('media_id' => $media_id);
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_DEL_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 获取永久素材列表(认证后的订阅号可用)
* @param string $type 素材的类型,图片image、视频video、语音 voice、图文news
* @param int $offset 全部素材的偏移位置0表示从第一个素材
* @param int $count 返回素材的数量取值在1到20之间
* @return bool|array
* 返回数组格式:
* array(
* 'total_count'=>0, //该类型的素材的总数
* 'item_count'=>0, //本次调用获取的素材的数量
* 'item'=>array() //素材列表数组,内容定义请参考官方文档
* )
*/
public function getForeverList($type, $offset, $count) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array(
'type' => $type,
'offset' => $offset,
'count' => $count,
);
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_FOREVER_BATCHGET_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取永久素材总数(认证后的订阅号可用)
* @return bool|array
* 返回数组格式:
* array(
* 'voice_count'=>0, //语音总数量
* 'video_count'=>0, //视频总数量
* 'image_count'=>0, //图片总数量
* 'news_count'=>0 //图文总数量
* )
*/
public function getForeverCount() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::MEDIA_FOREVER_COUNT_URL . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 上传图文消息素材,用于群发(认证后的订阅号可用)
* @param array $data 消息结构{"articles":[{...}]}
* @return bool|array
*/
public function uploadArticles($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MEDIA_UPLOADNEWS_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 上传视频素材(认证后的订阅号可用)
* @param array $data 消息结构
* {
* "media_id"=>"", //通过上传媒体接口得到的MediaId
* "title"=>"TITLE", //视频标题
* "description"=>"Description" //视频描述
* }
* @return bool|array
* {
* "type":"video",
* "media_id":"mediaid",
* "created_at":1398848981
* }
*/
public function uploadMpVideo($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::UPLOAD_MEDIA_URL . self::MEDIA_VIDEO_UPLOAD . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}

View File

@@ -0,0 +1,164 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信菜单操作SDK
*
* @author Anyon <zoujingli@qq.com>
* @date 2016/06/28 11:52
*/
class WechatMenu extends Common {
/** 创建自定义菜单 */
const MENU_ADD_URL = '/menu/create?';
/* 获取自定义菜单 */
const MENU_GET_URL = '/menu/get?';
/* 删除自定义菜单 */
const MENU_DEL_URL = '/menu/delete?';
/** 添加个性菜单 */
const COND_MENU_ADD_URL = '/menu/addconditional?';
/* 删除个性菜单 */
const COND_MENU_DEL_URL = '/menu/delconditional?';
/* 测试个性菜单 */
const COND_MENU_TRY_URL = '/menu/trymatch?';
/**
* 创建自定义菜单
* @param array $data 菜单数组数据
* @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN 文档
* @return bool
*/
public function createMenu($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MENU_ADD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 获取所有菜单
* @return bool|array
*/
public function getMenu() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::MENU_GET_URL . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 删除所有菜单
* @return bool
*/
public function deleteMenu() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::MENU_DEL_URL . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 创建个性菜单
* @param array $data 菜单数组数据
* @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN 文档
* @return bool|string
*/
public function createCondMenu($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::COND_MENU_ADD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode']) || empty($json['menuid'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json['menuid'];
}
return false;
}
/**
* 删除个性菜单
* @param string $menuid 菜单ID
* @return bool
*/
public function deleteCondMenu($menuid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('menuid' => $menuid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::COND_MENU_DEL_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 测试并返回个性化菜单
* @param string $openid 粉丝openid
* @return bool
*/
public function tryCondMenu($openid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('user_id' => $openid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::COND_MENU_TRY_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}

View File

@@ -0,0 +1,120 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信网页授权
*/
class WechatOauth extends Common {
const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
const OAUTH_AUTHORIZE_URL = '/authorize?';
const OAUTH_TOKEN_URL = '/sns/oauth2/access_token?';
const OAUTH_REFRESH_URL = '/sns/oauth2/refresh_token?';
const OAUTH_USERINFO_URL = '/sns/userinfo?';
const OAUTH_AUTH_URL = '/sns/auth?';
/**
* Oauth 授权跳转接口
* @param string $callback 授权回跳地址
* @param string $state 为重定向后会带上state参数填写a-zA-Z0-9的参数值最多128字节
* @param string $scope 授权类类型(可选值snsapi_base|snsapi_userinfo)
* @return string
*/
public function getOauthRedirect($callback, $state = '', $scope = 'snsapi_base') {
$redirect_uri = urlencode($callback);
return self::OAUTH_PREFIX . self::OAUTH_AUTHORIZE_URL . "appid={$this->appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
}
/**
* 通过 code 获取 AccessToken 和 openid
* @return bool|array
*/
public function getOauthAccessToken() {
$code = isset($_GET['code']) ? $_GET['code'] : '';
if (empty($code)) {
Tools::log("getOauthAccessToken Fail, Because there is no access to the code value in get.");
return false;
}
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::OAUTH_TOKEN_URL . "appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
Tools::log("WechatOauth::getOauthAccessToken Fail.{$this->errMsg} [{$this->errCode}]", 'ERR');
return false;
}
return $json;
}
return false;
}
/**
* 刷新access token并续期
* @param string $refresh_token
* @return bool|array
*/
public function getOauthRefreshToken($refresh_token) {
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::OAUTH_REFRESH_URL . "appid={$this->appid}&grant_type=refresh_token&refresh_token={$refresh_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
Tools::log("WechatOauth::getOauthRefreshToken Fail.{$this->errMsg} [{$this->errCode}]", 'ERR');
return false;
}
return $json;
}
return false;
}
/**
* 获取授权后的用户资料
* @param string $access_token
* @param string $openid
* @return bool|array {openid,nickname,sex,province,city,country,headimgurl,privilege,[unionid]}
* 注意unionid字段 只有在用户将公众号绑定到微信开放平台账号后才会出现。建议调用前用isset()检测一下
*/
public function getOauthUserInfo($access_token, $openid) {
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::OAUTH_USERINFO_URL . "access_token={$access_token}&openid={$openid}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
Tools::log("WechatOauth::getOauthUserInfo Fail.{$this->errMsg} [{$this->errCode}]", 'ERR');
return false;
}
return $json;
}
return false;
}
/**
* 检验授权凭证是否有效
* @param string $access_token
* @param string $openid
* @return bool 是否有效
*/
public function getOauthAuth($access_token, $openid) {
$result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::OAUTH_AUTH_URL . "access_token={$access_token}&openid={$openid}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
Tools::log("WechatOauth::getOauthAuth Fail.{$this->errMsg} [{$this->errCode}]", 'ERR');
return false;
} else if ($json['errcode'] == 0) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,532 @@
<?php
namespace Wechat;
use Wechat\Lib\Tools;
/**
* 微信支付SDK
* @author zoujingli <zoujingli@qq.com>
* @date 2015/05/13 12:12:00
*/
class WechatPay {
/** 支付接口基础地址 */
const MCH_BASE_URL = 'https://api.mch.weixin.qq.com';
/** 公众号appid */
public $appid;
/** 商户身份ID */
public $mch_id;
/** 商户支付密钥Key */
public $partnerKey;
/** 证书路径 */
public $ssl_cer;
public $ssl_key;
/** 执行错误消息及代码 */
public $errMsg;
public $errCode;
/**
* WechatPay constructor.
* @param array $options
*/
public function __construct($options = array()) {
$config = Loader::config($options);
$this->appid = isset($config['appid']) ? $config['appid'] : '';
$this->mch_id = isset($config['mch_id']) ? $config['mch_id'] : '';
$this->partnerKey = isset($config['partnerkey']) ? $config['partnerkey'] : '';
$this->ssl_cer = isset($config['ssl_cer']) ? $config['ssl_cer'] : '';
$this->ssl_key = isset($config['ssl_key']) ? $config['ssl_key'] : '';
}
/**
* 设置标配的请求参数生成签名生成接口参数xml
* @param array $data
* @return string
*/
protected function createXml($data) {
if (!isset($data['wxappid']) && !isset($data['mch_appid']) && !isset($data['appid'])) {
$data['appid'] = $this->appid;
}
if (!isset($data['mchid']) && !isset($data['mch_id'])) {
$data['mch_id'] = $this->mch_id;
}
isset($data['nonce_str']) || $data['nonce_str'] = Tools::createNoncestr();
$data["sign"] = Tools::getPaySign($data, $this->partnerKey);
return Tools::arr2xml($data);
}
/**
* POST提交XML
* @param array $data
* @param string $url
* @return mixed
*/
public function postXml($data, $url) {
return Tools::httpPost($url, $this->createXml($data));
}
/**
* 使用证书post请求XML
* @param array $data
* @param string $url
* @return mixed
*/
function postXmlSSL($data, $url) {
return Tools::httpsPost($url, $this->createXml($data), $this->ssl_cer, $this->ssl_key);
}
/**
* POST提交获取Array结果
* @param array $data 需要提交的数据
* @param string $url
* @param string $method
* @return array
*/
public function getArrayResult($data, $url, $method = 'postXml') {
return Tools::xml2arr($this->$method($data, $url));
}
/**
* 解析返回的结果
* @param array $result
* @return bool|array
*/
protected function _parseResult($result) {
if (empty($result)) {
$this->errCode = 'result error';
$this->errMsg = '解析返回结果失败';
return false;
}
if ($result['return_code'] !== 'SUCCESS') {
$this->errCode = $result['return_code'];
$this->errMsg = $result['return_msg'];
return false;
}
if (isset($result['err_code']) && $result['err_code'] !== 'SUCCESS') {
$this->errMsg = $result['err_code_des'];
$this->errCode = $result['err_code'];
return false;
}
return $result;
}
/**
* 创建刷卡支付参数包
* @param string $auth_code 授权Code号
* @param string $out_trade_no 商户订单号
* @param int $total_fee 支付费用
* @param string $body 订单标识
* @param null $goods_tag 商品标签
* @return array|bool
*/
public function createMicroPay($auth_code, $out_trade_no, $total_fee, $body, $goods_tag = null) {
$data = array(
"appid" => $this->appid,
"mch_id" => $this->mch_id,
"body" => $body,
"out_trade_no" => $out_trade_no,
"total_fee" => $total_fee,
"auth_code" => $auth_code,
"spbill_create_ip" => Tools::getAddress()
);
empty($goods_tag) || $data['goods_tag'] = $goods_tag;
$json = Tools::xml2arr($this->postXml($data, self::MCH_BASE_URL . '/pay/micropay'));
if (!empty($json) && false === $this->_parseResult($json)) {
return false;
}
return $json;
}
/**
* 支付通知验证处理
* @return bool|array
*/
public function getNotify() {
$notifyInfo = (array)simplexml_load_string(file_get_contents("php://input"), 'SimpleXMLElement', LIBXML_NOCDATA);
if (empty($notifyInfo)) {
Tools::log('Payment notification forbidden access.', 'ERR');
$this->errCode = '404';
$this->errMsg = 'Payment notification forbidden access.';
return false;
}
if (empty($notifyInfo['sign'])) {
Tools::log('Payment notification signature is missing.' . var_export($notifyInfo, true), 'ERR');
$this->errCode = '403';
$this->errMsg = 'Payment notification signature is missing.';
return false;
}
$data = $notifyInfo;
unset($data['sign']);
if ($notifyInfo['sign'] !== Tools::getPaySign($data, $this->partnerKey)) {
Tools::log('Payment notification signature verification failed.' . var_export($notifyInfo, true), 'ERR');
$this->errCode = '403';
$this->errMsg = 'Payment signature verification failed.';
return false;
}
Tools::log('Payment notification signature verification success.' . var_export($notifyInfo, true), 'MSG');
$this->errCode = '0';
$this->errMsg = '';
return $notifyInfo;
}
/**
* 支付XML统一回复
* @param array $data 需要回复的XML内容数组
* @param bool $isReturn 是否返回XML内容默认不返回
* @return string
*/
public function replyXml(array $data, $isReturn = false) {
$xml = Tools::arr2xml($data);
if ($isReturn) {
return $xml;
}
ob_clean();
exit($xml);
}
/**
* 获取预支付ID
* @param string $openid 用户openidJSAPI必填
* @param string $body 商品标题
* @param string $out_trade_no 第三方订单号
* @param int $total_fee 订单总价
* @param string $notify_url 支付成功回调地址
* @param string $trade_type 支付类型JSAPI|NATIVE|APP
* @param string $goods_tag 商品标记,代金券或立减优惠功能的参数
* @return bool|string
*/
public function getPrepayId($openid, $body, $out_trade_no, $total_fee, $notify_url, $trade_type = "JSAPI", $goods_tag = null) {
$postdata = array(
"body" => $body,
"out_trade_no" => $out_trade_no,
"total_fee" => $total_fee,
"notify_url" => $notify_url,
"trade_type" => $trade_type,
"spbill_create_ip" => Tools::getAddress()
);
empty($goods_tag) || $postdata['goods_tag'] = $goods_tag;
empty($openid) || $postdata['openid'] = $openid;
$result = $this->getArrayResult($postdata, self::MCH_BASE_URL . '/pay/unifiedorder');
if (false === $this->_parseResult($result)) {
return false;
}
return in_array($trade_type, array('JSAPI', 'APP')) ? $result['prepay_id'] : $result['code_url'];
}
/**
* 获取二维码预支付ID
* @param string $openid 用户openidJSAPI必填
* @param string $body 商品标题
* @param string $out_trade_no 第三方订单号
* @param int $total_fee 订单总价
* @param string $notify_url 支付成功回调地址
* @param string $goods_tag 商品标记,代金券或立减优惠功能的参数
* @return bool|string
*/
public function getQrcPrepayId($openid, $body, $out_trade_no, $total_fee, $notify_url, $goods_tag = null) {
$postdata = array(
"body" => $body,
"out_trade_no" => $out_trade_no,
"total_fee" => $total_fee,
"notify_url" => $notify_url,
"trade_type" => 'NATIVE',
"spbill_create_ip" => Tools::getAddress()
);
empty($goods_tag) || $postdata['goods_tag'] = $goods_tag;
empty($openid) || $postdata['openid'] = $openid;
$result = $this->getArrayResult($postdata, self::MCH_BASE_URL . '/pay/unifiedorder');
if (false === $this->_parseResult($result) || empty($result['prepay_id'])) {
return false;
}
return $result['prepay_id'];
}
/**
* 获取支付规二维码
* @param string $product_id 商户定义的商品id 或者订单号
* @return string
*/
public function getQrcPayUrl($product_id) {
$data = array(
'appid' => $this->appid,
'mch_id' => $this->mch_id,
'time_stamp' => (string)time(),
'nonce_str' => Tools::createNoncestr(),
'product_id' => (string)$product_id,
);
$data['sign'] = Tools::getPaySign($data, $this->partnerKey);
return "weixin://wxpay/bizpayurl?" . http_build_query($data);
}
/**
* 创建JSAPI支付参数包
* @param string $prepay_id
* @return array
*/
public function createMchPay($prepay_id) {
$option = array();
$option["appId"] = $this->appid;
$option["timeStamp"] = (string)time();
$option["nonceStr"] = Tools::createNoncestr();
$option["package"] = "prepay_id={$prepay_id}";
$option["signType"] = "MD5";
$option["paySign"] = Tools::getPaySign($option, $this->partnerKey);
$option['timestamp'] = $option['timeStamp'];
return $option;
}
/**
* 关闭订单
* @param string $out_trade_no
* @return bool
*/
public function closeOrder($out_trade_no) {
$data = array('out_trade_no' => $out_trade_no);
$result = $this->getArrayResult($data, self::MCH_BASE_URL . '/pay/closeorder');
if (false === $this->_parseResult($result)) {
return false;
}
return ($result['return_code'] === 'SUCCESS');
}
/**
* 查询订单详情
* @param $out_trade_no
* @return bool|array
*/
public function queryOrder($out_trade_no) {
$data = array('out_trade_no' => $out_trade_no);
$result = $this->getArrayResult($data, self::MCH_BASE_URL . '/pay/orderquery');
if (false === $this->_parseResult($result)) {
return false;
}
return $result;
}
/**
* 订单退款接口
* @param string $out_trade_no 商户订单号
* @param string $transaction_id 微信订单号
* @param string $out_refund_no 商户退款订单号
* @param int $total_fee 商户订单总金额
* @param int $refund_fee 退款金额
* @param int|null $op_user_id 操作员ID默认商户ID
* @param string $refund_account 退款资金来源
* 仅针对老资金流商户使用
* REFUND_SOURCE_UNSETTLED_FUNDS --- 未结算资金退款(默认使用未结算资金退款)
* REFUND_SOURCE_RECHARGE_FUNDS --- 可用余额退款
* @return bool
*/
public function refund($out_trade_no, $transaction_id, $out_refund_no, $total_fee, $refund_fee, $op_user_id = null, $refund_account = '') {
$data = array();
$data['out_trade_no'] = $out_trade_no;
$data['transaction_id'] = $transaction_id;
$data['out_refund_no'] = $out_refund_no;
$data['total_fee'] = $total_fee;
$data['refund_fee'] = $refund_fee;
$data['op_user_id'] = empty($op_user_id) ? $this->mch_id : $op_user_id;
!empty($refund_account) && $data['refund_account'] = $refund_account;
$result = $this->getArrayResult($data, self::MCH_BASE_URL . '/secapi/pay/refund', 'postXmlSSL');
if (false === $this->_parseResult($result)) {
return false;
}
return ($result['return_code'] === 'SUCCESS');
}
/**
* 退款查询接口
* @param string $out_trade_no
* @return bool|array
*/
public function refundQuery($out_trade_no) {
$data = array();
$data['out_trade_no'] = $out_trade_no;
$result = $this->getArrayResult($data, self::MCH_BASE_URL . '/pay/refundquery');
if (false === $this->_parseResult($result)) {
return false;
}
return $result;
}
/**
* 获取对账单
* @param string $bill_date 账单日期,如 20141110
* @param string $bill_type ALL|SUCCESS|REFUND|REVOKED
* @return bool|array
*/
public function getBill($bill_date, $bill_type = 'ALL') {
$data = array();
$data['bill_date'] = $bill_date;
$data['bill_type'] = $bill_type;
$result = $this->postXml($data, self::MCH_BASE_URL . '/pay/downloadbill');
$json = Tools::xml2arr($result);
if (!empty($json) && false === $this->_parseResult($json)) {
return false;
}
return $json;
}
/**
* 发送现金红包
* @param string $openid 红包接收者OPENID
* @param int $total_amount 红包总金额
* @param string $mch_billno 商户订单号
* @param string $sendname 商户名称
* @param string $wishing 红包祝福语
* @param string $act_name 活动名称
* @param string $remark 备注信息
* @param null|int $total_num 红包发放总人数大于1为裂变红包
* @param null|string $scene_id 场景id
* @param string $risk_info 活动信息
* @param null|string $consume_mch_id 资金授权商户号
* @return array|bool
* @link https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5
*/
public function sendRedPack($openid, $total_amount, $mch_billno, $sendname, $wishing, $act_name, $remark, $total_num = 1, $scene_id = null, $risk_info = '', $consume_mch_id = null) {
$data = array();
$data['mch_billno'] = $mch_billno; // 商户订单号 mch_id+yyyymmdd+10位一天内不能重复的数字
$data['wxappid'] = $this->appid;
$data['send_name'] = $sendname; //商户名称
$data['re_openid'] = $openid; //红包接收者
$data['total_amount'] = $total_amount; //红包总金额
$data['total_num'] = '1'; //发放人数据
$data['wishing'] = $wishing; //红包祝福语
$data['client_ip'] = Tools::getAddress(); //调用接口的机器Ip地址
$data['act_name'] = $act_name; //活动名称
$data['remark'] = $remark; //备注信息
$data['total_num'] = $total_num;
!empty($scene_id) && $data['scene_id'] = $scene_id;
!empty($risk_info) && $data['risk_info'] = $risk_info;
!empty($consume_mch_id) && $data['consume_mch_id'] = $consume_mch_id;
if ($total_num > 1) {
$data['amt_type'] = 'ALL_RAND';
$api = self::MCH_BASE_URL . '/mmpaymkttransfers/sendgroupredpack';
} else {
$api = self::MCH_BASE_URL . '/mmpaymkttransfers/sendredpack';
}
$result = $this->postXmlSSL($data, $api);
$json = Tools::xml2arr($result);
if (!empty($json) && false === $this->_parseResult($json)) {
return false;
}
return $json;
}
/**
* 现金红包状态查询
* @param string $billno
* @return bool|array
* @link https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_7&index=6
*/
public function queryRedPack($billno) {
$data['mch_billno'] = $billno;
$data['bill_type'] = 'MCHT';
$result = $this->postXmlSSL($data, self::MCH_BASE_URL . '/mmpaymkttransfers/gethbinfo');
$json = Tools::xml2arr($result);
if (!empty($json) && false === $this->_parseResult($json)) {
return false;
}
return $json;
}
/**
* 企业付款
* @param string $openid 红包接收者OPENID
* @param int $amount 红包总金额
* @param string $billno 商户订单号
* @param string $desc 备注信息
* @return bool|array
* @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
*/
public function transfers($openid, $amount, $billno, $desc) {
$data = array();
$data['mchid'] = $this->mch_id;
$data['mch_appid'] = $this->appid;
$data['partner_trade_no'] = $billno;
$data['openid'] = $openid;
$data['amount'] = $amount;
$data['check_name'] = 'NO_CHECK'; #不验证姓名
$data['spbill_create_ip'] = Tools::getAddress(); //调用接口的机器Ip地址
$data['desc'] = $desc; //备注信息
$result = $this->postXmlSSL($data, self::MCH_BASE_URL . '/mmpaymkttransfers/promotion/transfers');
$json = Tools::xml2arr($result);
if (!empty($json) && false === $this->_parseResult($json)) {
return false;
}
return $json;
}
/**
* 企业付款查询
* @param string $billno
* @return bool|array
* @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3
*/
public function queryTransfers($billno) {
$data['appid'] = $this->appid;
$data['mch_id'] = $this->mch_id;
$data['partner_trade_no'] = $billno;
$result = $this->postXmlSSL($data, self::MCH_BASE_URL . '/mmpaymkttransfers/gettransferinfo');
$json = Tools::xml2arr($result);
if (!empty($json) && false === $this->_parseResult($json)) {
return false;
}
return $json;
}
/**
* 二维码链接转成短链接
* @param string $url 需要处理的长链接
* @return bool|string
*/
public function shortUrl($url) {
$data = array();
$data['long_url'] = $url;
$result = $this->getArrayResult($data, self::MCH_BASE_URL . '/tools/shorturl');
if (!$result || $result['return_code'] !== 'SUCCESS') {
$this->errCode = $result['return_code'];
$this->errMsg = $result['return_msg'];
return false;
}
if (isset($result['err_code']) && $result['err_code'] !== 'SUCCESS') {
$this->errMsg = $result['err_code_des'];
$this->errCode = $result['err_code'];
return false;
}
return $result['short_url'];
}
/**
* 发放代金券
* @param int $coupon_stock_id 代金券批次id
* @param string $partner_trade_no 商户此次发放凭据号格式商户id+日期+流水号),商户侧需保持唯一性
* @param string $openid Openid信息
* @param string $op_user_id 操作员帐号, 默认为商户号 可在商户平台配置操作员对应的api权限
* @return bool|array
* @link https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_3
*/
public function sendCoupon($coupon_stock_id, $partner_trade_no, $openid, $op_user_id = null) {
$data = array();
$data['appid'] = $this->appid;
$data['coupon_stock_id'] = $coupon_stock_id;
$data['openid_count'] = 1;
$data['partner_trade_no'] = $partner_trade_no;
$data['openid'] = $openid;
$data['op_user_id'] = empty($op_user_id) ? $this->mch_id : $op_user_id;
$result = $this->postXmlSSL($data, self::MCH_BASE_URL . '/mmpaymkttransfers/send_coupon');
$json = Tools::xml2arr($result);
if (!empty($json) && false === $this->_parseResult($json)) {
return false;
}
return $json;
}
}

View File

@@ -0,0 +1,174 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信门店接口
* @author Anyon <zoujingli@qq.com>
* @date 2016/10/26 15:43
*/
class WechatPoi extends Common {
/** 创建门店 */
const POI_ADD = '/cgi-bin/poi/addpoi?';
/** 查询门店信息 */
const POI_GET = '/cgi-bin/poi/getpoi?';
/** 获取门店列表 */
const POI_GET_LIST = '/cgi-bin/poi/getpoilist?';
/** 修改门店信息 */
const POI_UPDATE = '/cgi-bin/poi/updatepoi?';
/** 删除门店 */
const POI_DELETE = '/cgi-bin/poi/delpoi?';
/** 获取门店类目表 */
const POI_CATEGORY = '/cgi-bin/poi/getwxcategory?';
/**
* 创建门店
* @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN
* @param array $data
* @return bool
*/
public function addPoi($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::POI_ADD . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 删除门店
* @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN
* @param string $poi_id JSON数据格式
* @return bool|array
*/
public function delPoi($poi_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('poi_id' => $poi_id);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::POI_DELETE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 修改门店服务信息
* @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN
* @param array $data
* @return bool
*/
public function updatePoi($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::POI_UPDATE . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 查询门店信息
* @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN
* @param string $poi_id
* @return bool
*/
public function getPoi($poi_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('poi_id' => $poi_id);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::POI_GET . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 查询门店列表
* @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444378120&token=&lang=zh_CN
* @param int $begin 开始位置0 即为从第一条开始查询
* @param int $limit 返回数据条数最大允许50默认为20
* @return bool|array
*/
public function getPoiList($begin = 0, $limit = 50) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$limit > 50 && $limit = 50;
$data = array('begin' => $begin, 'limit' => $limit);
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::POI_GET_LIST . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取商家门店类目表
* @return bool|string
*/
public function getCategory() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::POI_CATEGORY . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}

View File

@@ -0,0 +1,993 @@
<?php
namespace Wechat;
use Prpcrypt;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信消息对象解析SDK
*
* @author Anyon <zoujingli@qq.com>
* @date 2016/06/28 11:29
*/
class WechatReceive extends Common {
/** 消息推送地址 */
const CUSTOM_SEND_URL = '/message/custom/send?';
const MASS_SEND_URL = '/message/mass/send?';
const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?';
const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?';
const TEMPLATE_SEND_URL = '/message/template/send?';
const MASS_SEND_GROUP_URL = '/message/mass/sendall?';
const MASS_DELETE_URL = '/message/mass/delete?';
const MASS_PREVIEW_URL = '/message/mass/preview?';
const MASS_QUERY_URL = '/message/mass/get?';
/** 消息回复类型 */
const MSGTYPE_TEXT = 'text';
const MSGTYPE_IMAGE = 'image';
const MSGTYPE_LOCATION = 'location';
const MSGTYPE_LINK = 'link';
const MSGTYPE_EVENT = 'event';
const MSGTYPE_MUSIC = 'music';
const MSGTYPE_NEWS = 'news';
const MSGTYPE_VOICE = 'voice';
const MSGTYPE_VIDEO = 'video';
/** 文件过滤 */
protected $_text_filter = true;
/** 消息对象 */
private $_receive;
/**
* 获取微信服务器发来的内容
* @return $this
*/
public function getRev() {
if ($this->_receive) {
return $this;
}
$postStr = !empty($this->postxml) ? $this->postxml : file_get_contents("php://input");
!empty($postStr) && $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
return $this;
}
/**
* 获取微信服务器发来的信息数据
* @return array
*/
public function getRevData() {
return $this->_receive;
}
/**
* 获取消息发送者
* @return bool|string
*/
public function getRevFrom() {
if (isset($this->_receive['FromUserName'])) {
return $this->_receive['FromUserName'];
}
return false;
}
/**
* 获取消息接受者
* @return bool|string
*/
public function getRevTo() {
if (isset($this->_receive['ToUserName'])) {
return $this->_receive['ToUserName'];
}
return false;
}
/**
* 获取接收消息的类型
* @return bool|string
*/
public function getRevType() {
if (isset($this->_receive['MsgType'])) {
return $this->_receive['MsgType'];
}
return false;
}
/**
* 获取消息ID
* @return bool|string
*/
public function getRevID() {
if (isset($this->_receive['MsgId'])) {
return $this->_receive['MsgId'];
}
return false;
}
/**
* 获取消息发送时间
* @return bool|string
*/
public function getRevCtime() {
if (isset($this->_receive['CreateTime'])) {
return $this->_receive['CreateTime'];
}
return false;
}
/**
* 获取卡券事件推送 - 卡卷审核是否通过
* 当Event为 card_pass_check(审核通过) 或 card_not_pass_check(未通过)
* @return bool|string 返回卡券ID
*/
public function getRevCardPass() {
if (isset($this->_receive['CardId'])) {
return $this->_receive['CardId'];
}
return false;
}
/**
* 获取卡券事件推送 - 领取卡券
* 当Event为 user_get_card(用户领取卡券)
* @return bool|array
*/
public function getRevCardGet() {
$array = array();
if (isset($this->_receive['CardId'])) {
$array['CardId'] = $this->_receive['CardId'];
}
if (isset($this->_receive['IsGiveByFriend'])) {
$array['IsGiveByFriend'] = $this->_receive['IsGiveByFriend'];
}
$array['OldUserCardCode'] = $this->_receive['OldUserCardCode'];
if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) {
$array['UserCardCode'] = $this->_receive['UserCardCode'];
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 获取卡券事件推送 - 删除卡券
* 当Event为 user_del_card(用户删除卡券)
* @return bool|array
*/
public function getRevCardDel() {
if (isset($this->_receive['CardId'])) { //卡券 ID
$array['CardId'] = $this->_receive['CardId'];
}
if (isset($this->_receive['UserCardCode']) && !empty($this->_receive['UserCardCode'])) {
$array['UserCardCode'] = $this->_receive['UserCardCode'];
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 获取接收消息内容正文
* @return bool
*/
public function getRevContent() {
if (isset($this->_receive['Content'])) {
return $this->_receive['Content'];
} else if (isset($this->_receive['Recognition'])) { //获取语音识别文字内容,需申请开通
return $this->_receive['Recognition'];
}
return false;
}
/**
* 获取接收消息图片
* @return array|bool
*/
public function getRevPic() {
if (isset($this->_receive['PicUrl'])) {
return array(
'mediaid' => $this->_receive['MediaId'],
'picurl' => (string)$this->_receive['PicUrl'], //防止picurl为空导致解析出错
);
}
return false;
}
/**
* 获取接收消息链接
* @return bool|array
*/
public function getRevLink() {
if (isset($this->_receive['Url'])) {
return array(
'url' => $this->_receive['Url'],
'title' => $this->_receive['Title'],
'description' => $this->_receive['Description']
);
}
return false;
}
/**
* 获取接收地理位置
* @return bool|array
*/
public function getRevGeo() {
if (isset($this->_receive['Location_X'])) {
return array(
'x' => $this->_receive['Location_X'],
'y' => $this->_receive['Location_Y'],
'scale' => $this->_receive['Scale'],
'label' => $this->_receive['Label']
);
}
return false;
}
/**
* 获取上报地理位置事件
* @return bool|array
*/
public function getRevEventGeo() {
if (isset($this->_receive['Latitude'])) {
return array(
'x' => $this->_receive['Latitude'],
'y' => $this->_receive['Longitude'],
'precision' => $this->_receive['Precision'],
);
}
return false;
}
/**
* 获取接收事件推送
* @return bool|array
*/
public function getRevEvent() {
if (isset($this->_receive['Event'])) {
$array['event'] = $this->_receive['Event'];
}
if (isset($this->_receive['EventKey'])) {
$array['key'] = $this->_receive['EventKey'];
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 获取自定义菜单的扫码推事件信息
*
* 事件类型为以下两种时则调用此方法有效
* Event 事件类型scancode_push
* Event 事件类型scancode_waitmsg
* @return bool|array
*/
public function getRevScanInfo() {
if (isset($this->_receive['ScanCodeInfo'])) {
if (!is_array($this->_receive['ScanCodeInfo'])) {
$array = (array)$this->_receive['ScanCodeInfo'];
$this->_receive['ScanCodeInfo'] = $array;
} else {
$array = $this->_receive['ScanCodeInfo'];
}
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 获取自定义菜单的图片发送事件信息
*
* 事件类型为以下三种时则调用此方法有效
* Event 事件类型pic_sysphoto 弹出系统拍照发图的事件推送
* Event 事件类型pic_photo_or_album 弹出拍照或者相册发图的事件推送
* Event 事件类型pic_weixin 弹出微信相册发图器的事件推送
*
* @return bool|array
* array (
* 'Count' => '2',
* 'PicList' =>array (
* 'item' =>array (
* 0 =>array ('PicMd5Sum' => 'aaae42617cf2a14342d96005af53624c'),
* 1 =>array ('PicMd5Sum' => '149bd39e296860a2adc2f1bb81616ff8'),
* ),
* ),
* )
*
*/
public function getRevSendPicsInfo() {
if (isset($this->_receive['SendPicsInfo'])) {
if (!is_array($this->_receive['SendPicsInfo'])) {
$array = (array)$this->_receive['SendPicsInfo'];
if (isset($array['PicList'])) {
$array['PicList'] = (array)$array['PicList'];
$item = $array['PicList']['item'];
$array['PicList']['item'] = array();
foreach ($item as $key => $value) {
$array['PicList']['item'][$key] = (array)$value;
}
}
$this->_receive['SendPicsInfo'] = $array;
} else {
$array = $this->_receive['SendPicsInfo'];
}
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 获取自定义菜单的地理位置选择器事件推送
*
* 事件类型为以下时则可以调用此方法有效
* Event 事件类型location_select 弹出地理位置选择器的事件推送
*
* @return bool|array
* array (
* 'Location_X' => '33.731655000061',
* 'Location_Y' => '113.29955200008047',
* 'Scale' => '16',
* 'Label' => '某某市某某区某某路',
* 'Poiname' => '',
* )
*
*/
public function getRevSendGeoInfo() {
if (isset($this->_receive['SendLocationInfo'])) {
if (!is_array($this->_receive['SendLocationInfo'])) {
$array = (array)$this->_receive['SendLocationInfo'];
if (empty($array['Poiname'])) {
$array['Poiname'] = "";
}
if (empty($array['Label'])) {
$array['Label'] = "";
}
$this->_receive['SendLocationInfo'] = $array;
} else {
$array = $this->_receive['SendLocationInfo'];
}
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 获取接收语音推送
* @return bool|array
*/
public function getRevVoice() {
if (isset($this->_receive['MediaId'])) {
return array(
'mediaid' => $this->_receive['MediaId'],
'format' => $this->_receive['Format'],
);
}
return false;
}
/**
* 获取接收视频推送
* @return array|bool
*/
public function getRevVideo() {
if (isset($this->_receive['MediaId'])) {
return array(
'mediaid' => $this->_receive['MediaId'],
'thumbmediaid' => $this->_receive['ThumbMediaId']
);
}
return false;
}
/**
* 获取接收TICKET
* @return bool|string
*/
public function getRevTicket() {
if (isset($this->_receive['Ticket'])) {
return $this->_receive['Ticket'];
}
return false;
}
/**
* 获取二维码的场景值
* @return bool|string
*/
public function getRevSceneId() {
if (isset($this->_receive['EventKey'])) {
return str_replace('qrscene_', '', $this->_receive['EventKey']);
}
return false;
}
/**
* 获取主动推送的消息ID
* 经过验证这个和普通的消息MsgId不一样
* 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH
* @return bool|string
*/
public function getRevTplMsgID() {
if (isset($this->_receive['MsgID'])) {
return $this->_receive['MsgID'];
}
return false;
}
/**
* 获取模板消息发送状态
* @return bool|string
*/
public function getRevStatus() {
if (isset($this->_receive['Status'])) {
return $this->_receive['Status'];
}
return false;
}
/**
* 获取群发或模板消息发送结果
* 当Event为 MASSSENDJOBFINISH 或 TEMPLATESENDJOBFINISH即高级群发/模板消息
* @return bool|array
*/
public function getRevResult() {
if (isset($this->_receive['Status'])) { //发送是否成功,具体的返回值请参考 高级群发/模板消息 的事件推送说明
$array['Status'] = $this->_receive['Status'];
}
if (isset($this->_receive['MsgID'])) { //发送的消息id
$array['MsgID'] = $this->_receive['MsgID'];
}
//以下仅当群发消息时才会有的事件内容
if (isset($this->_receive['TotalCount'])) { //分组或openid列表内粉丝数量
$array['TotalCount'] = $this->_receive['TotalCount'];
}
if (isset($this->_receive['FilterCount'])) { //过滤过滤是指特定地区、性别的过滤、用户设置拒收的过滤用户接收已超4条的过滤准备发送的粉丝数
$array['FilterCount'] = $this->_receive['FilterCount'];
}
if (isset($this->_receive['SentCount'])) { //发送成功的粉丝数
$array['SentCount'] = $this->_receive['SentCount'];
}
if (isset($this->_receive['ErrorCount'])) { //发送失败的粉丝数
$array['ErrorCount'] = $this->_receive['ErrorCount'];
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 获取多客服会话状态推送事件 - 接入会话
* 当Event为 kfcreatesession 即接入会话
* @return bool|string
*/
public function getRevKFCreate() {
if (isset($this->_receive['KfAccount'])) {
return $this->_receive['KfAccount'];
}
return false;
}
/**
* 获取多客服会话状态推送事件 - 关闭会话
* 当Event为 kfclosesession 即关闭会话
* @return bool|string
*/
public function getRevKFClose() {
if (isset($this->_receive['KfAccount'])) {
return $this->_receive['KfAccount'];
}
return false;
}
/**
* 获取多客服会话状态推送事件 - 转接会话
* 当Event为 kfswitchsession 即转接会话
* @return bool|array
*/
public function getRevKFSwitch() {
if (isset($this->_receive['FromKfAccount'])) { //原接入客服
$array['FromKfAccount'] = $this->_receive['FromKfAccount'];
}
if (isset($this->_receive['ToKfAccount'])) { //转接到客服
$array['ToKfAccount'] = $this->_receive['ToKfAccount'];
}
if (isset($array) && count($array) > 0) {
return $array;
}
return false;
}
/**
* 发送客服消息
* @param array $data 消息结构{"touser":"OPENID","msgtype":"news","news":{...}}
* @return bool|array
*/
public function sendCustomMessage($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::CUSTOM_SEND_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 模板消息 设置所属行业
* @param string $id1 公众号模板消息所属行业编号,参看官方开发文档 行业代码
* @param string $id2 同$id1。但如果只有一个行业此参数可省略
* @return bool|mixed
*/
public function setTMIndustry($id1, $id2 = '') {
if ($id1) {
$data['industry_id1'] = $id1;
}
if ($id2) {
$data['industry_id2'] = $id2;
}
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::TEMPLATE_SET_INDUSTRY_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 模板消息 添加消息模板
* 成功返回消息模板的调用id
* @param string $tpl_id 模板库中模板的编号有“TM**”和“OPENTMTM**”等形式
* @return bool|string
*/
public function addTemplateMessage($tpl_id) {
$data = array('template_id_short' => $tpl_id);
if (!$this->access_token && !$this->getAccessToken())
return false;
$result = Tools::httpPost(self::API_URL_PREFIX . self::TEMPLATE_ADD_TPL_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json['template_id'];
}
return false;
}
/**
* 发送模板消息
* @param array $data 消息结构
* {
* "touser":"OPENID",
* "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
* "url":"http://weixin.qq.com/download",
* "topcolor":"#FF0000",
* "data":{
* "参数名1": {
* "value":"参数",
* "color":"#173177" //参数颜色
* },
* "Date":{
* "value":"06月07日 19时24分",
* "color":"#173177"
* },
* "CardNumber":{
* "value":"0426",
* "color":"#173177"
* },
* "Type":{
* "value":"消费",
* "color":"#173177"
* }
* }
* }
* @return bool|array
*/
public function sendTemplateMessage($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::TEMPLATE_SEND_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 转发多客服消息
* @param string $customer_account
* @return $this
*/
public function transfer_customer_service($customer_account = '') {
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName' => $this->getRevTo(),
'CreateTime' => time(),
'MsgType' => 'transfer_customer_service',
);
if ($customer_account) {
$msg['TransInfo'] = array('KfAccount' => $customer_account);
}
$this->Message($msg);
return $this;
}
/**
* 高级群发消息, 根据OpenID列表群发图文消息(订阅号不可用)
* 注意视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
* 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
* @param array $data 消息结构
* {
* "touser"=>array(
* "OPENID1",
* "OPENID2"
* ),
* "msgtype"=>"mpvideo",
* // 在下面5种类型中选择对应的参数内容
* // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
* // text => array ( "content" => "hello")
* }
* @return bool|array
*/
public function sendMassMessage($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MASS_SEND_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 高级群发消息, 根据群组id群发图文消息(认证后的订阅号可用)
* 注意视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
* 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
* @param array $data 消息结构
* {
* "filter"=>array(
* "is_to_all"=>False, //是否群发给所有用户.True不用分组idFalse需填写分组id
* "group_id"=>"2" //群发的分组id
* ),
* "msgtype"=>"mpvideo",
* // 在下面5种类型中选择对应的参数内容
* // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
* // text => array ( "content" => "hello")
* }
* @return bool|array
*/
public function sendGroupMassMessage($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MASS_SEND_GROUP_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 高级群发消息, 删除群发图文消息(认证后的订阅号可用)
* @param string $msg_id 消息ID
* @return bool
*/
public function deleteMassMessage($msg_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MASS_DELETE_URL . "access_token={$this->access_token}", Tools::json_encode(array('msg_id' => $msg_id)));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
/**
* 高级群发消息, 预览群发消息(认证后的订阅号可用)
* 注意视频需要在调用uploadMedia()方法后,再使用 uploadMpVideo() 方法生成,
* 然后获得的 mediaid 才能用于群发,且消息类型为 mpvideo 类型。
* @param type $data
* @消息结构
* {
* "touser"=>"OPENID",
* "msgtype"=>"mpvideo",
* // 在下面5种类型中选择对应的参数内容
* // mpnews | voice | image | mpvideo => array( "media_id"=>"MediaId")
* // text => array ( "content" => "hello")
* }
* @return bool|array
*/
public function previewMassMessage($data) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MASS_PREVIEW_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 高级群发消息, 查询群发消息发送状态(认证后的订阅号可用)
* @param string $msg_id 消息ID
* @return bool|array
* {
* "msg_id":201053012, //群发消息后返回的消息id
* "msg_status":"SEND_SUCCESS" //消息发送后的状态SENDING表示正在发送 SEND_SUCCESS表示发送成功
* }
*/
public function queryMassMessage($msg_id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MASS_QUERY_URL . "access_token={$this->access_token}", Tools::json_encode(array('msg_id' => $msg_id)));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 设置发送消息
* @param string|array $msg 消息数组
* @param bool $append 是否在原消息数组追加
* @return array
*/
public function Message($msg = '', $append = false) {
if (is_null($msg)) {
$this->_msg = array();
} elseif (is_array($msg)) {
if ($append) {
$this->_msg = array_merge($this->_msg, $msg);
} else {
$this->_msg = $msg;
}
return $this->_msg;
}
return $this->_msg;
}
/**
* 设置文本消息
* @param string $text 文本内容
* @return $this
*/
public function text($text = '') {
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName' => $this->getRevTo(),
'MsgType' => self::MSGTYPE_TEXT,
'Content' => $this->_auto_text_filter($text),
'CreateTime' => time(),
);
$this->Message($msg);
return $this;
}
/**
* 设置图片消息
* @param string $mediaid 图片媒体ID
* @return $this
*/
public function image($mediaid = '') {
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName' => $this->getRevTo(),
'MsgType' => self::MSGTYPE_IMAGE,
'Image' => array('MediaId' => $mediaid),
'CreateTime' => time(),
);
$this->Message($msg);
return $this;
}
/**
* 设置语音回复消息
* @param string $mediaid 语音媒体ID
* @return $this
*/
public function voice($mediaid = '') {
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName' => $this->getRevTo(),
'MsgType' => self::MSGTYPE_VOICE,
'Voice' => array('MediaId' => $mediaid),
'CreateTime' => time(),
);
$this->Message($msg);
return $this;
}
/**
* 设置视频回复消息
* @param string $mediaid 视频媒体ID
* @param string $title 视频标题
* @param string $description 视频描述
* @return $this
*/
public function video($mediaid = '', $title = '', $description = '') {
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName' => $this->getRevTo(),
'MsgType' => self::MSGTYPE_VIDEO,
'Video' => array(
'MediaId' => $mediaid,
'Title' => $title,
'Description' => $description
),
'CreateTime' => time(),
);
$this->Message($msg);
return $this;
}
/**
* 设置音乐回复消息
* @param string $title 音乐标题
* @param string $desc 音乐描述
* @param string $musicurl 音乐地址
* @param string $hgmusicurl 高清音乐地址
* @param string $thumbmediaid 音乐图片缩略图的媒体id可选
* @return $this
*/
public function music($title, $desc, $musicurl, $hgmusicurl = '', $thumbmediaid = '') {
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName' => $this->getRevTo(),
'CreateTime' => time(),
'MsgType' => self::MSGTYPE_MUSIC,
'Music' => array(
'Title' => $title,
'Description' => $desc,
'MusicUrl' => $musicurl,
'HQMusicUrl' => $hgmusicurl
),
);
if ($thumbmediaid) {
$msg['Music']['ThumbMediaId'] = $thumbmediaid;
}
$this->Message($msg);
return $this;
}
/**
* 设置回复图文
* @param array $newsData
* @return $this
*/
public function news($newsData = array()) {
$msg = array(
'ToUserName' => $this->getRevFrom(),
'FromUserName' => $this->getRevTo(),
'CreateTime' => time(),
'MsgType' => self::MSGTYPE_NEWS,
'ArticleCount' => count($newsData),
'Articles' => $newsData,
);
$this->Message($msg);
return $this;
}
/**
* 回复微信服务器
* @param array $msg 要发送的信息(默认取$this->_msg
* @param bool $return 是否返回信息而不抛出到浏览器(默认:否)
* @return bool|string
*/
public function reply($msg = array(), $return = false) {
if (empty($msg)) {
if (empty($this->_msg)) { //防止不先设置回复内容直接调用reply方法导致异常
return false;
}
$msg = $this->_msg;
}
$xmldata = Tools::arr2xml($msg);
if ($this->encrypt_type == 'aes') { //如果来源消息为加密方式
!class_exists('Prpcrypt', FALSE) && require __DIR__ . '/Lib/Prpcrypt.php';
$pc = new Prpcrypt($this->encodingAesKey);
// 如果是第三方平台,加密得使用 component_appid
$array = $pc->encrypt($xmldata, empty($this->config['component_appid']) ? $this->appid : $this->config['component_appid']);
$ret = $array[0];
if ($ret != 0) {
Tools::log('encrypt err!');
return false;
}
$timestamp = time();
$nonce = rand(77, 999) * rand(605, 888) * rand(11, 99);
$encrypt = $array[1];
$tmpArr = array($this->token, $timestamp, $nonce, $encrypt); //比普通公众平台多了一个加密的密文
sort($tmpArr, SORT_STRING);
$signature = sha1(implode($tmpArr));
$format = "<xml><Encrypt><![CDATA[%s]]></Encrypt><MsgSignature><![CDATA[%s]]></MsgSignature><TimeStamp>%s</TimeStamp><Nonce><![CDATA[%s]]></Nonce></xml>";
$xmldata = sprintf($format, $encrypt, $signature, $timestamp, $nonce);
}
if ($return) {
return $xmldata;
}
echo $xmldata;
}
/**
* 过滤文字回复\r\n换行符
* @param string $text
* @return string
*/
private function _auto_text_filter($text) {
if (!$this->_text_filter) {
return $text;
}
return str_replace("\r\n", "\n", $text);
}
}

View File

@@ -0,0 +1,122 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信前端 JavaScript 签名SDK
*
* @author Anyon <zoujingli@qq.com>
* @date 2016/06/28 11:24
*/
class WechatScript extends Common {
/**
* JSAPI授权TICKET
* @var string
*/
public $jsapi_ticket;
/**
* 删除JSAPI授权TICKET
* @param string $appid
* @return bool
*/
public function resetJsTicket($appid = '') {
$this->jsapi_ticket = '';
$authname = 'wechat_jsapi_ticket_' . empty($appid) ? $this->appid : $appid;
Tools::removeCache($authname);
return true;
}
/**
* 获取JSAPI授权TICKET
* @param string $appid 用于多个appid时使用,可空
* @param string $jsapi_ticket 手动指定jsapi_ticket非必要情况不建议用
* @param string $access_token 获取 jsapi_ticket 指定 access_token
* @return bool|string
*/
public function getJsTicket($appid = '', $jsapi_ticket = '', $access_token = '') {
if (empty($access_token)) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$access_token = $this->access_token;
}
if (empty($appid)) {
$appid = $this->appid;
}
# 手动指定token优先使用
if ($jsapi_ticket) {
$this->jsapi_ticket = $jsapi_ticket;
return $this->jsapi_ticket;
}
# 尝试从缓存中读取
$cache = 'wechat_jsapi_ticket_' . $appid;
$jt = Tools::getCache($cache);
if ($jt) {
return $this->jsapi_ticket = $jt;
}
# 检测事件注册
if (isset(Loader::$callback[__FUNCTION__])) {
return $this->jsapi_ticket = call_user_func_array(Loader::$callback[__FUNCTION__], array(&$this, &$cache));
}
# 调接口获取
$result = Tools::httpGet(self::API_URL_PREFIX . self::GET_TICKET_URL . "access_token={$access_token}" . '&type=jsapi');
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
$this->jsapi_ticket = $json['ticket'];
Tools::setCache($cache, $this->jsapi_ticket, $json['expires_in'] ? intval($json['expires_in']) - 100 : 3600);
return $this->jsapi_ticket;
}
return false;
}
/**
* 获取JsApi使用签名
* @param string $url 网页的URL自动处理#及其后面部分
* @param int $timestamp 当前时间戳 (为空则自动生成)
* @param string $noncestr 随机串 (为空则自动生成)
* @param string $appid 用于多个appid时使用,可空
* @param string $access_token 获取 jsapi_ticket 指定 access_token
* @return array|bool 返回签名字串
*/
public function getJsSign($url, $timestamp = 0, $noncestr = '', $appid = '', $access_token = '') {
if (!$this->jsapi_ticket && !$this->getJsTicket($appid, '', $access_token) || empty($url)) {
return false;
}
$data = array(
"jsapi_ticket" => $this->jsapi_ticket,
"timestamp" => empty($timestamp) ? time() : $timestamp,
"noncestr" => '' . empty($noncestr) ? Tools::createNoncestr(16) : $noncestr,
"url" => trim($url),
);
return array(
"url" => $url,
'debug' => false,
"appId" => empty($appid) ? $this->appid : $appid,
"nonceStr" => $data['noncestr'],
"timestamp" => $data['timestamp'],
"signature" => Tools::getSignature($data, 'sha1'),
'jsApiList' => array(
'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone',
'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem',
'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'closeWindow', 'scanQRCode', 'chooseWXPay',
'translateVoice', 'getNetworkType', 'openLocation', 'getLocation',
'openProductSpecificView', 'addCard', 'chooseCard', 'openCard',
'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice',
'openWXDeviceLib', 'closeWXDeviceLib', 'getWXDeviceInfos', 'sendDataToWXDevice', 'disconnectWXDevice', 'getWXDeviceTicket', 'connectWXDevice',
'startScanWXDevice', 'stopScanWXDevice', 'onWXDeviceBindStateChange', 'onScanWXDeviceResult', 'onReceiveDataFromWXDevice',
'onWXDeviceBluetoothStateChange', 'onWXDeviceStateChange'
)
);
}
}

View File

@@ -0,0 +1,382 @@
<?php
namespace Wechat;
use Wechat\Lib\Cache;
use Wechat\Lib\Tools;
/**
* 公众号第三方平台SDK
*
* @version 1.0
* @author Anyon <zoujingli@qq.com>
* @date 2016/10/18 00:35:55
*/
class WechatService {
const URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin/component';
// 获取服务access_token
const COMPONENT_TOKEN_URL = '/api_component_token';
// 获取(刷新)授权公众号的令牌
const REFRESH_ACCESS_TOKEN = '/api_authorizer_token';
// 获取预授权码
const PREAUTH_CODE_URL = '/api_create_preauthcode';
// 获取公众号的授权信息
const QUERY_AUTH_URL = '/api_query_auth';
// 获取授权方的账户信息
const GET_AUTHORIZER_INFO_URL = '/api_get_authorizer_info';
// 获取授权方的选项设置信息
const GET_AUTHORIZER_OPTION_URL = '/api_get_authorizer_option';
// 设置授权方的选项信息
const SET_AUTHORIZER_OPTION_URL = '/api_set_authorizer_option';
// 微信后台推送的ticket 每十分钟更新一次
public $errCode;
// 服务appid
public $errMsg;
// 服务appsecret
protected $component_verify_ticket;
// 公众号消息校验Token
protected $component_appid;
// 公众号消息加解密Key
protected $component_appsecret;
// 服务令牌
protected $component_token;
// 授权方appid
protected $component_encodingaeskey;
// 授权方令牌
protected $component_access_token;
// 刷新令牌
protected $authorizer_appid;
// JSON数据
protected $pre_auth_code;
// 错误消息
protected $data;
/**
* WechatService constructor.
* @param array $options
*/
public function __construct($options = array()) {
$options = Loader::config($options);
$this->component_encodingaeskey = !empty($options['component_encodingaeskey']) ? $options['component_encodingaeskey'] : '';
$this->component_verify_ticket = !empty($options['component_verify_ticket']) ? $options['component_verify_ticket'] : '';
$this->component_appsecret = !empty($options['component_appsecret']) ? $options['component_appsecret'] : '';
$this->component_token = !empty($options['component_token']) ? $options['component_token'] : '';
$this->component_appid = !empty($options['component_appid']) ? $options['component_appid'] : '';
}
/**
* 接收公众平台推送的 Ticket
* @return bool|array
*/
public function getComonentTicket() {
$receive = new WechatReceive(array(
'appid' => $this->component_appid,
'appsecret' => $this->component_appsecret,
'encodingaeskey' => $this->component_encodingaeskey,
'token' => $this->component_token,
'cachepath' => Cache::$cachepath
));
# 会话内容解密状态判断
if (false === $receive->valid()) {
$this->errCode = $receive->errCode;
$this->errMsg = $receive->errMsg;
Tools::log("Get Wechat Push ComponentVerifyTicket Faild. {$this->errMsg} [$this->errCode]", 'Err');
return false;
}
$data = $receive->getRev()->getRevData();
if ($data['InfoType'] === 'component_verify_ticket' && !empty($data['ComponentVerifyTicket'])) {
# 记录推送日志到微信SDK
Tools::log("Get Wechat Push ComponentVerifyTicket Success. ");
Tools::setCache('component_verify_ticket', $data['ComponentVerifyTicket']);
}
return $data;
}
/**
* 获取(刷新)授权公众号的令牌
* @注意1. 授权公众号访问access token2小时有效
* @注意2. 一定保存好新的刷新令牌
* @param string $authorizer_appid 授权方APPID
* @param string $authorizer_refresh_token 授权方刷新令牌
* @return bool|string
*/
public function refreshAccessToken($authorizer_appid, $authorizer_refresh_token) {
empty($this->component_access_token) && $this->getComponentAccessToken();
if (empty($this->component_access_token)) {
return false;
}
$data = array();
$data['component_appid'] = $this->component_appid;
$data['authorizer_appid'] = $authorizer_appid;
$data['authorizer_refresh_token'] = $authorizer_refresh_token;
$url = self::URL_PREFIX . self::REFRESH_ACCESS_TOKEN . "?component_access_token={$this->component_access_token}";
$result = Tools::httpPost($url, Tools::json_encode($data));
if (($result = $this->_decode($result)) === false) {
Tools::log("Get getAuthorizerOption Faild. {$this->errMsg} [$this->errCode]", 'ERR');
}
return $result;
}
/**
* 获取或刷新服务 AccessToken
* @return bool|string
*/
public function getComponentAccessToken() {
$cacheKey = 'wechat_component_access_token';
$this->component_access_token = Tools::getCache($cacheKey);
if (empty($this->component_access_token)) {
$data = array();
$data['component_appid'] = $this->component_appid;
$data['component_appsecret'] = $this->component_appsecret;
$data['component_verify_ticket'] = $this->component_verify_ticket;
$url = self::URL_PREFIX . self::COMPONENT_TOKEN_URL;
$result = Tools::httpPost($url, Tools::json_encode($data));
if (($this->component_access_token = $this->_decode($result, 'component_access_token')) === false) {
Tools::log("Get getComponentAccessToken Faild. {$this->errMsg} [$this->errCode]", 'ERR');
return false;
}
Tools::setCache($cacheKey, $this->component_access_token, 7200);
}
return $this->component_access_token;
}
/**
* 解析JSON数据
* @param string $result
* @param string|null $field
* @return bool|array
*/
private function _decode($result, $field = null) {
$this->data = json_decode($result, true);
if (!empty($this->data['errcode'])) {
$this->errCode = $this->data['errcode'];
$this->errMsg = $this->data['errmsg'];
return false;
}
if ($this->data && !is_null($field)) {
if (isset($this->data[$field])) {
return $this->data[$field];
} else {
return false;
}
}
return $this->data;
}
/**
* 获取公众号的授权信息
*
* @param string $authorization_code
* @return bool|array
*/
public function getAuthorizationInfo($authorization_code) {
empty($this->component_access_token) && $this->getComponentAccessToken();
if (empty($this->component_access_token)) {
return false;
}
$data = array();
$data['component_appid'] = $this->component_appid;
$data['authorization_code'] = $authorization_code;
$url = self::URL_PREFIX . self::QUERY_AUTH_URL . "?component_access_token={$this->component_access_token}";
$result = Tools::httpPost($url, Tools::json_encode($data));
$authorization_info = $this->_decode($result, 'authorization_info');
if (empty($authorization_info)) {
Tools::log("Get getAuthorizationInfo Faild. {$this->errMsg} [$this->errCode]", 'ERR');
return false;
}
$authorization_info['func_info'] = $this->_parseFuncInfo($authorization_info['func_info']);
return $authorization_info;
}
/**
* 解析授权信息,返回以逗号分割的数据
* @param array $func_info
* @return string
*/
private function _parseFuncInfo($func_info) {
$authorization_list = array();
foreach ($func_info as $func) {
foreach ($func as $f) {
$authorization_list[] = $f['id'];
}
}
return join($authorization_list, ',');
}
/**
* 获取授权方的账户信息
* @param string $authorizer_appid
* @return bool
*/
public function getWechatInfo($authorizer_appid) {
empty($this->component_access_token) && $this->getComponentAccessToken();
$data = array();
$data['component_access_token'] = $this->component_access_token;
$data['component_appid'] = $this->component_appid;
$data['authorizer_appid'] = $authorizer_appid;
$url = self::URL_PREFIX . self::GET_AUTHORIZER_INFO_URL . "?component_access_token={$this->component_access_token}";
$result = Tools::httpPost($url, Tools::json_encode($data));
$authorizer_info = $this->_decode($result, 'authorizer_info');
if (empty($authorizer_info)) {
Tools::log("Get WechatInfo Faild. {$this->errMsg} [$this->errCode]", 'ERR');
return false;
}
$author_data = array_merge($authorizer_info, $this->data['authorization_info']);
$author_data['service_type_info'] = $author_data['service_type_info']['id'];
$author_data['verify_type_info'] = $author_data['verify_type_info']['id'];
$author_data['func_info'] = $this->_parseFuncInfo($author_data['func_info']);
$author_data['business_info'] = json_encode($author_data['business_info']);
return $author_data;
}
/**
* 获取授权方的选项设置信息
* @param string $authorizer_appid
* @param string $option_name
* @return bool
*/
public function getAuthorizerOption($authorizer_appid, $option_name) {
empty($this->component_access_token) && $this->getComponentAccessToken();
if (empty($this->authorizer_appid)) {
return false;
}
$data = array();
$data['component_appid'] = $this->component_appid;
$data['authorizer_appid'] = $authorizer_appid;
$data['option_name'] = $option_name;
$url = self::URL_PREFIX . self::GET_AUTHORIZER_OPTION_URL . "?component_access_token={$this->component_access_token}";
$result = Tools::httpPost($url, Tools::json_encode($data));
if (($result = $this->_decode($result)) === false) {
Tools::log("Get getAuthorizerOption Faild. {$this->errMsg} [$this->errCode]", 'ERR');
}
return $result;
}
/**
* 设置授权方的选项信息
* @param string $authorizer_appid
* @param string $option_name
* @param string $option_value
* @return bool
*/
public function setAuthorizerOption($authorizer_appid, $option_name, $option_value) {
empty($this->component_access_token) && $this->getComponentAccessToken();
if (empty($this->authorizer_appid)) {
return false;
}
$data = array();
$data['component_appid'] = $this->component_appid;
$data['authorizer_appid'] = $authorizer_appid;
$data['option_name'] = $option_name;
$data['option_value'] = $option_value;
$url = self::URL_PREFIX . self::SET_AUTHORIZER_OPTION_URL . "?component_access_token={$this->component_access_token}";
$result = Tools::httpPost($url, Tools::json_encode($data));
if (($result = $this->_decode($result)) === false) {
Tools::log("Get setAuthorizerOption Faild. {$this->errMsg} [$this->errCode]", 'ERR');
}
return $result;
}
/**
* 获取授权回跳地址
* @param string $redirect_uri
* @return bool
*/
public function getAuthRedirect($redirect_uri) {
empty($this->pre_auth_code) && $this->getPreauthCode();
if (empty($this->pre_auth_code)) {
return false;
}
return "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$this->component_appid}&pre_auth_code={$this->pre_auth_code}&redirect_uri={$redirect_uri}";
}
/**
* 获取预授权码
*
* @return bool|string
*/
public function getPreauthCode() {
empty($this->component_access_token) && $this->getComponentAccessToken();
if (empty($this->component_access_token)) {
return false;
}
$data = array();
$data['component_appid'] = $this->component_appid;
$url = self::URL_PREFIX . self::PREAUTH_CODE_URL . "?component_access_token={$this->component_access_token}";
$result = Tools::httpPost($url, Tools::json_encode($data));
$this->pre_auth_code = $this->_decode($result, 'pre_auth_code');
if (empty($this->pre_auth_code)) {
Tools::log("Get getPreauthCode Faild. {$this->errMsg} [$this->errCode]", 'ERR');
}
return $this->pre_auth_code;
}
/**
* oauth 授权跳转接口
* @param string $appid
* @param string $redirect_uri
* @param string $scope snsapi_userinfo|snsapi_base
* @return string
*/
public function getOauthRedirect($appid, $redirect_uri, $scope = 'snsapi_userinfo') {
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri=" . urlencode($redirect_uri)
. "&response_type=code&scope={$scope}&state={$appid}&component_appid={$this->component_appid}#wechat_redirect";
}
/**
* 通过code获取Access Token
* @param string $appid
* @return bool|array
*/
public function getOauthAccessToken($appid) {
$code = isset($_GET['code']) ? $_GET['code'] : '';
if (empty($code)) {
return false;
}
empty($this->component_access_token) && $this->getComponentAccessToken();
if (empty($this->component_access_token)) {
return false;
}
$url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?"
. "appid={$appid}&code={$code}&"
. "grant_type=authorization_code&"
. "component_appid={$this->component_appid}&"
. "component_access_token={$this->component_access_token}";
$json = $this->parseJson(Tools::httpGet($url));
if ($json !== false) {
return $json;
}
return false;
}
/**
* 解析JSON数据
* @param string $result
* @return bool
*/
private function parseJson($result) {
$json = json_decode($result, true);
if (!empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
/**
* 获取关注者详细信息
* @param string $openid
* @param string $oauthAccessToken
* @return bool|array {subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
* 注意unionid字段 只有在用户将公众号绑定到公众号第三方平台账号后才会出现。建议调用前用isset()检测一下
*/
public function getOauthUserInfo($openid, $oauthAccessToken) {
$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$oauthAccessToken}&openid={$openid}&lang=zh_CN";
return $this->parseJson(Tools::httpGet($url));
}
}

View File

@@ -0,0 +1,578 @@
<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信粉丝操作SDK
*
* @author Anyon <zoujingli@qq.com>
* @date 2016/06/28 11:20
*/
class WechatUser extends Common {
/** 获取粉丝列表 */
const USER_GET_URL = '/user/get?';
/* 获取粉丝信息 */
const USER_INFO_URL = '/user/info?';
/* 批量获取粉丝信息 */
const USER_BATCH_INFO_URL = '/user/info/batchget?';
/* 更新粉丝标注 */
const USER_UPDATEREMARK_URL = '/user/info/updateremark?';
/** 创建标签 */
const TAGS_CREATE_URL = '/tags/create?';
/* 获取标签列表 */
const TAGS_GET_URL = '/tags/get?';
/* 更新标签 */
const TAGS_UPDATE_URL = '/tags/update?';
/* 删除标签 */
const TAGS_DELETE_URL = '/tags/delete?';
/* 获取标签下的粉丝列表 */
const TAGS_GET_USER_URL = '/user/tag/get?';
/* 批量为粉丝打标签 */
const TAGS_MEMBER_BATCHTAGGING = '/tags/members/batchtagging?';
/* 批量为粉丝取消标签 */
const TAGS_MEMBER_BATCHUNTAGGING = '/tags/members/batchuntagging?';
/* 获取粉丝的标签列表 */
const TAGS_LIST = '/tags/getidlist?';
/** 获取分组列表 */
const GROUP_GET_URL = '/groups/get?';
/* 获取粉丝所在的分组 */
const USER_GROUP_URL = '/groups/getid?';
/* 创建分组 */
const GROUP_CREATE_URL = '/groups/create?';
/* 更新分组 */
const GROUP_UPDATE_URL = '/groups/update?';
/* 删除分组 */
const GROUP_DELETE_URL = '/groups/delete?';
/* 修改粉丝所在分组 */
const GROUP_MEMBER_UPDATE_URL = '/groups/members/update?';
/* 批量修改粉丝所在分组 */
const GROUP_MEMBER_BATCHUPDATE_URL = '/groups/members/batchupdate?';
/** 获取黑名单列表 */
const BACKLIST_GET_URL = '/tags/members/getblacklist?';
/* 批量拉黑粉丝 */
const BACKLIST_ADD_URL = '/tags/members/batchblacklist?';
/* 批量取消拉黑粉丝 */
const BACKLIST_DEL_URL = '/tags/members/batchunblacklist?';
/**
* 批量获取关注粉丝列表
* @param string $next_openid
* @return bool|array
*/
public function getUserList($next_openid = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::USER_GET_URL . "access_token={$this->access_token}" . '&next_openid=' . $next_openid);
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取关注者详细信息
* @param string $openid
* @return bool|array {subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
* @注意unionid字段 只有在粉丝将公众号绑定到微信开放平台账号后才会出现。建议调用前用isset()检测一下
*/
public function getUserInfo($openid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::USER_INFO_URL . "access_token={$this->access_token}&openid={$openid}");
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 批量获取用户基本信息
* @param array $openids 用户oepnid列表(最多支持100个openid)
* @param string $lang 指定返回语言
* @return bool|mixed
*/
public function getUserBatchInfo(array $openids, $lang = 'zh_CN') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('user_list' => array());
foreach (array_unique($openids) as $openid) {
$data['user_list'][] = array('openid' => $openid, 'lang' => $lang);
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::USER_BATCH_INFO_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode']) && !isset($json['user_info_list'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json['user_info_list'];
}
return false;
}
/**
* 设置粉丝备注名
* @param string $openid
* @param string $remark 备注名
* @return bool|array
*/
public function updateUserRemark($openid, $remark) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid' => $openid, 'remark' => $remark);
$result = Tools::httpPost(self::API_URL_PREFIX . self::USER_UPDATEREMARK_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取粉丝分组列表
* @return bool|array
*/
public function getGroup() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::GROUP_GET_URL . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 删除粉丝分组
* @param type $id
* @return bool
*/
public function delGroup($id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('group' => array('id' => $id));
$result = Tools::httpPost(self::API_URL_PREFIX . self::GROUP_DELETE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取粉丝所在分组
* @param string $openid
* @return bool|int 成功则返回粉丝分组id
*/
public function getUserGroup($openid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid' => $openid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::USER_GROUP_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
} else if (isset($json['groupid'])) {
return $json['groupid'];
}
}
return false;
}
/**
* 新增自定分组
* @param string $name 分组名称
* @return bool|array
*/
public function createGroup($name) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('group' => array('name' => $name));
$result = Tools::httpPost(self::API_URL_PREFIX . self::GROUP_CREATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 更改分组名称
* @param int $groupid 分组id
* @param string $name 分组名称
* @return bool|array
*/
public function updateGroup($groupid, $name) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('group' => array('id' => $groupid, 'name' => $name));
$result = Tools::httpPost(self::API_URL_PREFIX . self::GROUP_UPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 移动粉丝分组
* @param int $groupid 分组id
* @param string $openid 粉丝openid
* @return bool|array
*/
public function updateGroupMembers($groupid, $openid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid' => $openid, 'to_groupid' => $groupid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::GROUP_MEMBER_UPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 批量移动粉丝分组
* @param string $groupid 分组ID
* @param string $openid_list 粉丝openid数组(一次不能超过50个)
* @return bool|array
*/
public function batchUpdateGroupMembers($groupid, $openid_list) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid_list' => $openid_list, 'to_groupid' => $groupid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::GROUP_MEMBER_BATCHUPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 新增自定标签
* @param string $name 标签名称
* @return bool|array
*/
public function createTags($name) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('tag' => array('name' => $name));
$result = Tools::httpPost(self::API_URL_PREFIX . self::TAGS_CREATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 更新标签
* @param string $id 标签id
* @param string $name 标签名称
* @return bool|array
*/
public function updateTag($id, $name) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('tag' => array('id' => $id, 'name' => $name));
$result = Tools::httpPost(self::API_URL_PREFIX . self::TAGS_UPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取粉丝标签列表
* @return bool|array
*/
public function getTags() {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::TAGS_GET_URL . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 删除粉丝标签
* @param string $id
* @return bool
*/
public function delTag($id) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('tag' => array('id' => $id));
$result = Tools::httpPost(self::API_URL_PREFIX . self::TAGS_DELETE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取标签下的粉丝列表
* @param string $tagid
* @param string $next_openid
* @return bool
*/
public function getTagUsers($tagid, $next_openid = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('tagid' => $tagid, 'next_openid' => $next_openid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::TAGS_GET_USER_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 批量为粉丝打标签
* @param string $tagid 标签ID
* @param array $openid_list 粉丝openid数组一次不能超过50个
* @return bool|array
*/
public function batchAddUserTag($tagid, $openid_list) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid_list' => $openid_list, 'tagid' => $tagid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::TAGS_MEMBER_BATCHTAGGING . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 批量为粉丝取消标签
* @param string $tagid 标签ID
* @param array $openid_list 粉丝openid数组一次不能超过50个
* @return bool|array
*/
public function batchDeleteUserTag($tagid, $openid_list) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid_list' => $openid_list, 'tagid' => $tagid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::TAGS_MEMBER_BATCHUNTAGGING . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 获取粉丝的标签列表
* @param string $openid 粉丝openid
* @return bool|array
*/
public function getUserTags($openid) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid' => $openid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::TAGS_LIST . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !isset($json['tagid_list']) || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json['tagid_list'];
}
return false;
}
/**
* 批量获取黑名单粉丝
* @param string $begin_openid
* @return bool|array
*/
public function getBacklist($begin_openid = '') {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = empty($begin_openid) ? array() : array('begin_openid' => $begin_openid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::BACKLIST_GET_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 批量拉黑粉丝
* @param string $openids
* @return bool|array
*/
public function addBacklist($openids) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid_list' => $openids);
$result = Tools::httpPost(self::API_URL_PREFIX . self::BACKLIST_ADD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
/**
* 批量取消拉黑粉丝
* @param string $openids
* @return bool|array
*/
public function delBacklist($openids) {
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('openid_list' => $openids);
$result = Tools::httpPost(self::API_URL_PREFIX . self::BACKLIST_DEL_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}