From bb07da202beb1bf57294c82585cb537d63db8355 Mon Sep 17 00:00:00 2001 From: Anyon Date: Wed, 18 Apr 2018 17:43:08 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeMini/Crypt.php | 44 ++++++++ WeMini/Plugs.php | 98 ++++++++++++++++++ WeMini/Poi.php | 91 +++++++++++++++++ WeMini/Qrcode.php | 82 +++++++++++++++ WeMini/Template.php | 110 ++++++++++++++++++++ WeMini/Total.php | 176 ++++++++++++++++++++++++++++++++ WeMini/crypt/errorCode.php | 20 ++++ WeMini/crypt/wxBizDataCrypt.php | 59 +++++++++++ composer.json | 3 +- include.php | 12 ++- 10 files changed, 691 insertions(+), 4 deletions(-) create mode 100644 WeMini/Crypt.php create mode 100644 WeMini/Plugs.php create mode 100644 WeMini/Poi.php create mode 100644 WeMini/Qrcode.php create mode 100644 WeMini/Template.php create mode 100644 WeMini/Total.php create mode 100644 WeMini/crypt/errorCode.php create mode 100644 WeMini/crypt/wxBizDataCrypt.php diff --git a/WeMini/Crypt.php b/WeMini/Crypt.php new file mode 100644 index 0000000..46fbad0 --- /dev/null +++ b/WeMini/Crypt.php @@ -0,0 +1,44 @@ +config->get('appid'), $sessionKey); + $errCode = $pc->decryptData($encryptedData, $iv, $data); + if ($errCode == 0) { + return $data; + } + return false; + } +} \ No newline at end of file diff --git a/WeMini/Plugs.php b/WeMini/Plugs.php new file mode 100644 index 0000000..8546fbf --- /dev/null +++ b/WeMini/Plugs.php @@ -0,0 +1,98 @@ +registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['action' => 'apply', 'plugin_appid' => $plugin_appid], true); + } + + /** + * 2.查询已添加的插件 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getList() + { + $url = 'https://api.weixin.qq.com/wxa/plugin?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['action' => 'list'], true); + } + + /** + * 3.删除已添加的插件 + * @param string $plugin_appid 插件appid + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function unbind($plugin_appid) + { + $url = 'https://api.weixin.qq.com/wxa/plugin?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['action' => 'list', 'plugin_appid' => $plugin_appid], true); + } + + + /** + * 4.获取当前所有插件使用方(供插件开发者调用) + * @param integer $page 拉取第page页的数据 + * @param integer $num 表示每页num条记录 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function devApplyList($page = 1, $num = 10) + { + $url = 'https://api.weixin.qq.com/wxa/plugin?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + $data = ['action' => 'dev_apply_list', 'page' => $page, 'num' => $num]; + return $this->callPostApi($url, $data, true); + } + + /** + * 5.修改插件使用申请的状态(供插件开发者调用) + * @param string $action dev_agree:同意申请;dev_refuse:拒绝申请;dev_delete:删除已拒绝的申请者 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function devAgree($action = 'dev_agree') + { + $url = 'https://api.weixin.qq.com/wxa/plugin?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['action' => $action], true); + } + +} \ No newline at end of file diff --git a/WeMini/Poi.php b/WeMini/Poi.php new file mode 100644 index 0000000..c81ef5d --- /dev/null +++ b/WeMini/Poi.php @@ -0,0 +1,91 @@ +registerApi($url, __FUNCTION__, func_get_args()); + $data = [ + 'related_name' => $related_name, 'related_credential' => $related_credential, + 'related_address' => $related_address, 'related_proof_material' => $related_proof_material, + ]; + return $this->callPostApi($url, $data, true); + } + + /** + * 查看地点列表 + * @param integer $page 起始页id(从1开始计数) + * @param integer $page_rows 每页展示个数(最多1000个) + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getNearByPoiList($page = 1, $page_rows = 1000) + { + $url = "https://api.weixin.qq.com/wxa/getnearbypoilist?page={$page}&page_rows={$page_rows}&access_token=ACCESS_TOKEN"; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callGetApi($url); + } + + /** + * 删除地点 + * @param string $poi_id 附近地点ID + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function delNearByPoiList($poi_id) + { + $url = "https://api.weixin.qq.com/wxa/delnearbypoi?access_token=ACCESS_TOKEN"; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['poi_id' => $poi_id], true); + } + + /** + * 展示/取消展示附近小程序 + * @param string $poi_id 附近地点ID + * @param string $status 0:取消展示;1:展示 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function setNearByPoiShowStatus($poi_id, $status) + { + $url = "https://api.weixin.qq.com/wxa/setnearbypoishowstatus?access_token=ACCESS_TOKEN"; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['poi_id' => $poi_id, 'status' => $status], true); + } + +} \ No newline at end of file diff --git a/WeMini/Qrcode.php b/WeMini/Qrcode.php new file mode 100644 index 0000000..217d4d5 --- /dev/null +++ b/WeMini/Qrcode.php @@ -0,0 +1,82 @@ + "0", "g" => "0", "b" => "0"]) + { + $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + $data = ['path' => $path, 'width' => $width, 'auto_color' => false, 'line_color' => $line_color]; + return $this->callPostApi($url, $data, true); + } + + /** + * 获取小程序码(永久有效) + * 接口B:适用于需要的码数量极多的业务场景 + * @param string $scene 最大32个可见字符,只支持数字 + * @param string $page 必须是已经发布的小程序存在的页面 + * @param integer $width 二维码的宽度 + * @param bool $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 + * @param array $line_color auto_color 为 false 时生效 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getCodeUnlimit($scene, $page, $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"]) + { + $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + $data = ['scene' => $scene, 'width' => $width, 'auto_color' => false, 'line_color' => $line_color]; + return $this->callPostApi($url, $data, true); + } + + /** + * 获取小程序二维码(永久有效) + * 接口C:适用于需要的码数量较少的业务场景 + * @param string $path 不能为空,最大长度 128 字节 + * @param integer $width 二维码的宽度 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function createQrcode($path, $width = 430) + { + $url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['path' => $path, 'width' => $width], true); + } + +} \ No newline at end of file diff --git a/WeMini/Template.php b/WeMini/Template.php new file mode 100644 index 0000000..516d407 --- /dev/null +++ b/WeMini/Template.php @@ -0,0 +1,110 @@ +registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['offset' => '0', 'count' => '20'], true); + } + + /** + * 获取模板库某个模板标题下关键词库 + * @param string $template_id 模板标题id,可通过接口获取,也可登录小程序后台查看获取 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getTemplateLibrary($template_id) + { + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['id' => $template_id], true); + } + + /** + * 组合模板并添加至帐号下的个人模板库 + * @param string $template_id 模板标题id,可通过接口获取,也可登录小程序后台查看获取 + * @param array $keyword_id_list 开发者自行组合好的模板关键词列表,关键词顺序可以自由搭配(例如[3,5,4]或[4,5,3]),最多支持10个关键词组合 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function addTemplate($template_id, array $keyword_id_list) + { + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['id' => $template_id, 'keyword_id_list' => $keyword_id_list], true); + } + + /** + * 获取帐号下已存在的模板列表 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getTemplateList() + { + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['offset' => '0', 'count' => '20'], true); + } + + /** + * 删除模板消息 + * @param string $template_id 要删除的模板id + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function delTemplate($template_id) + { + $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['template_id' => $template_id], true); + } + + /** + * 发送模板消息 + * @param array $data 发送的消息对象数组 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function send(array $data) + { + $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, $data, true); + } + +} \ No newline at end of file diff --git a/WeMini/Total.php b/WeMini/Total.php new file mode 100644 index 0000000..9d55a6b --- /dev/null +++ b/WeMini/Total.php @@ -0,0 +1,176 @@ +registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 访问分析 + * @param string $begin_date 开始日期 + * @param string $end_date 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidDailyVisittrend($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 周趋势 + * @param string $begin_date 开始日期,为周一日期 + * @param string $end_date 结束日期,为周日日期,限定查询一周数据 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidWeeklyVisittrend($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 月趋势 + * @param string $begin_date 开始日期,为自然月第一天 + * @param string $end_date 结束日期,为自然月最后一天,限定查询一个月数据 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidMonthlyVisittrend($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 访问分布 + * @param string $begin_date 开始日期 + * @param string $end_date 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidVisitdistribution($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappidvisitdistribution?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 日留存 + * @param string $begin_date 开始日期 + * @param string $end_date 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidDailyRetaininfo($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 周留存 + * @param string $begin_date 开始日期,为周一日期 + * @param string $end_date 结束日期,为周日日期,限定查询一周数据 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidWeeklyRetaininfo($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappidweeklyretaininfo?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 月留存 + * @param string $begin_date 开始日期,为自然月第一天 + * @param string $end_date 结束日期,为自然月最后一天,限定查询一个月数据 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidMonthlyRetaininfo($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyretaininfo?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 访问页面 + * @param string $begin_date 开始日期 + * @param string $end_date 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidVisitPage($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + + /** + * 用户画像 + * @param string $begin_date 开始日期 + * @param string $end_date 结束日期,开始日期与结束日期相差的天数限定为0/6/29,分别表示查询最近1/7/30天数据,end_date允许设置的最大值为昨日 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function getWeanalysisAppidUserportrait($begin_date, $end_date) + { + $url = 'https://api.weixin.qq.com/datacube/getweanalysisappiduserportrait?access_token=ACCESS_TOKEN'; + $this->registerApi($url, __FUNCTION__, func_get_args()); + return $this->callPostApi($url, ['begin_date' => $begin_date, 'end_date' => $end_date], true); + } + +} \ No newline at end of file diff --git a/WeMini/crypt/errorCode.php b/WeMini/crypt/errorCode.php new file mode 100644 index 0000000..a4f8e72 --- /dev/null +++ b/WeMini/crypt/errorCode.php @@ -0,0 +1,20 @@ + + *
  • -41001: encodingAesKey 非法
  • + *
  • -41003: aes 解密失败
  • + *
  • -41004: 解密后得到的buffer非法
  • + *
  • -41005: base64加密失败
  • + *
  • -41016: base64解密失败
  • + * + */ +class ErrorCode +{ + public static $OK = 0; + public static $IllegalAesKey = -41001; + public static $IllegalIv = -41002; + public static $IllegalBuffer = -41003; + public static $DecodeBase64Error = -41004; +} \ No newline at end of file diff --git a/WeMini/crypt/wxBizDataCrypt.php b/WeMini/crypt/wxBizDataCrypt.php new file mode 100644 index 0000000..d253e96 --- /dev/null +++ b/WeMini/crypt/wxBizDataCrypt.php @@ -0,0 +1,59 @@ +appid = $appid; + $this->sessionKey = $sessionKey; + } + + /** + * 检验数据的真实性,并且获取解密后的明文. + * @param $encryptedData string 加密的用户数据 + * @param $iv string 与用户数据一同返回的初始向量 + * @param $data string 解密后的原文 + * + * @return int 成功0,失败返回对应的错误码 + */ + public function decryptData($encryptedData, $iv, &$data) + { + if (strlen($this->sessionKey) != 24) { + return \ErrorCode::$IllegalAesKey; + } + $aesKey = base64_decode($this->sessionKey); + if (strlen($iv) != 24) { + return \ErrorCode::$IllegalIv; + } + $aesIV = base64_decode($iv); + $aesCipher = base64_decode($encryptedData); + $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); + $dataObj = json_decode($result); + if ($dataObj == null) { + return \ErrorCode::$IllegalBuffer; + } + if ($dataObj->watermark->appid != $this->appid) { + return \ErrorCode::$IllegalBuffer; + } + $data = $result; + return \ErrorCode::$OK; + } + +} + diff --git a/composer.json b/composer.json index c06a697..895e4e8 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ }, "autoload": { "psr-4": { - "WeChat\\": "WeChat" + "WeChat\\": "WeChat", + "WeMini\\": "WeMini" } } } \ No newline at end of file diff --git a/include.php b/include.php index b8f402c..889d8c6 100644 --- a/include.php +++ b/include.php @@ -14,8 +14,14 @@ // 动态注册SDK自动加载 spl_autoload_register(function ($classname) { - $filename = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $classname) . '.php'; - if (stripos($classname, 'WeChat') === 0 && file_exists($filename)) { - include $filename; + $separator = DIRECTORY_SEPARATOR; + $filename = __DIR__ . $separator . str_replace('\\', $separator, $classname) . '.php'; + if (file_exists($filename)) { + if (stripos($classname, 'WeChat') === 0) { + include $filename; + } + if (stripos($classname, 'WeMini') === 0) { + include $filename; + } } }); \ No newline at end of file