123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- <?php
- /**
- * [WeEngine System] Copyright (c) 2014 WE7.CC
- * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
- */
- defined('IN_IA') or exit('Access Denied');
- load()->classs('weixin.platform');
- class WxappPlatform extends WeixinPlatform {
- const JSCODEURL = 'https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s';
- const FAST_REGISTER_WEAPP_CREATE = 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create&component_access_token=%s';
- const FAST_REGISTER_WEAPP_SEARCH = 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=search&component_access_token=%s';
-
- public $appid;
- protected $appsecret;
- public $encodingaeskey;
- public $token;
- protected $refreshtoken;
- protected $tablename = 'account_wxapp';
- protected $menuFrame = 'wxapp';
- protected $type = ACCOUNT_TYPE_APP_AUTH;
- protected $typeName = '微信小程序';
- protected $typeSign = WXAPP_TYPE_SIGN;
- protected $supportVersion = STATUS_ON;
- public function __construct($uniaccount = array()) {
- $setting = setting_load('platform');
- $this->appid = $setting['platform']['appid'];
- $this->appsecret = $setting['platform']['appsecret'];
- $this->token = $setting['platform']['token'];
- $this->encodingaeskey = $setting['platform']['encodingaeskey'];
- parent::__construct($uniaccount);
- }
- protected function getAccountInfo($uniacid) {
- if ('wxd101a85aa106f53e' == $this->account['key']) {
- $this->account['key'] = $this->appid;
- $this->openPlatformTestCase();
- }
- $account = table('account_wxapp')->getAccount($uniacid);
- $account['encrypt_key'] = $this->appid;
- return $account;
- }
- public function getOauthInfo($code = '') {
- $component_accesstoken = $this->getComponentAccesstoken();
- if (is_error($component_accesstoken)) {
- return $component_accesstoken;
- }
- $apiurl = sprintf(self::JSCODEURL, $this->account['key'], $code, $this->appid, $component_accesstoken);
- $response = $this->request($apiurl);
- if (is_error($response)) {
- return $response;
- }
- cache_write('account_oauth_refreshtoken' . $this->account['key'], $response['refresh_token']);
- return $response;
- }
- protected function setAuthRefreshToken($token) {
- $tablename = 'account_wxapp';
- pdo_update($tablename, array('auth_refresh_token' => $token), array('uniacid' => $this->account['uniacid']));
- cache_write(cache_system_key('account_auth_refreshtoken', array('uniacid' => $this->account['uniacid'])), $token);
- }
-
- public function pkcs7Encode($encrypt_data, $iv) {
- $key = base64_decode($_SESSION['session_key']);
- $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
- if (is_error($result)) {
- return error(1, '解密失败');
- }
- $result = json_decode($result, true);
- if (empty($result)) {
- return error(1, '解密失败');
- }
- if ($result['watermark']['appid'] != $this->account['key']) {
- return error(1, '解密失败');
- }
- unset($result['watermark']);
- return $result;
- }
- public function result($errno, $message = '', $data = '') {
- exit(json_encode(array(
- 'errno' => $errno,
- 'message' => $message,
- 'data' => $data,
- )));
- }
- public function getDailyVisitTrend($date) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token={$token}";
- $response = $this->requestApi($url, json_encode(array('begin_date' => $date, 'end_date' => $date)));
- if (is_error($response)) {
- return $response;
- }
- return $response['list'][0];
- }
- public function fastRegisterWxapp($data) {
- $component_accesstoken = $this->getComponentAccesstoken();
- if (is_error($component_accesstoken)) {
- return $component_accesstoken;
- }
- $apiurl = sprintf(self::FAST_REGISTER_WEAPP_CREATE, $component_accesstoken);
- $post = array(
- 'name' => $data['name'],
- 'code' => $data['code'],
- 'code_type' => $data['code_type'],
- 'legal_persona_wechat' => $data['legal_persona_wechat'],
- 'legal_persona_name' => $data['legal_persona_name'],
- 'component_phone' => $data['component_phone'],
- );
- return $this->request($apiurl, $post);
- }
- public function fastRegisterWxappSearch($data) {
- $component_accesstoken = $this->getComponentAccesstoken();
- if (is_error($component_accesstoken)) {
- return $component_accesstoken;
- }
- $apiurl = sprintf(self::FAST_REGISTER_WEAPP_SEARCH, $component_accesstoken);
- $post = array(
- 'name' => $data['name'],
- 'legal_persona_wechat' => $data['legal_persona_wechat'],
- 'legal_persona_name' => $data['legal_persona_name'],
- );
- return $this->request($apiurl, $post);
- }
-
- public function bindTester($wechatid) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('wechatid' => $wechatid);
- $url = "https://api.weixin.qq.com/wxa/bind_tester?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function getDomain() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array(
- 'action' => 'get',
- );
- $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function setWebViewDomain($data) {
- $webviewdomain = array();
- if ('get' == $data['action']) {
- $cachekey = cache_system_key('account_web_view_domain', array('uniacid' => $this->account['uniacid']));
- $webviewdomain = cache_load($cachekey);
- }
- if (empty($webviewdomain)) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token={$token}";
- $webviewdomain = $this->request($url, $data);
- if (is_error($webviewdomain)) {
- return error($webviewdomain['errno'], $this->errorCode($webviewdomain['errno']));
- }
- if ('get' == $data['action']) {
- $webviewdomain = $webviewdomain['webviewdomain'];
- cache_write($cachekey, $webviewdomain, CACHE_EXPIRE_LONG);
- }
- }
- return $webviewdomain;
- }
-
- public function modifyDomain($domains) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array(
- 'action' => 'set',
- 'requestdomain' => $domains['requestdomain'],
- 'wsrequestdomain' => $domains['wsrequestdomain'],
- 'uploaddomain' => $domains['uploaddomain'],
- 'downloaddomain' => $domains['downloaddomain'],
- );
- $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function getAccountBasicInfo() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo?access_token={$token}";
- return $this->request($url);
- }
-
- public function setNickname($name) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('nick_name' => $name);
- $url = "https://api.weixin.qq.com/wxa/setnickname?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function queryNickname($audit_id) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('audit_id' => $audit_id);
- $url = "https://api.weixin.qq.com/wxa/api_wxa_querynickname?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function checkwxVerifyNickname($nick_name) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('nick_name' => $nick_name);
- $url = "https://api.weixin.qq.com/cgi-bin/wxverify/checkwxverifynickname?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function modifyHeadImage($path) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $upload_to_temporary = $this->uploadMedia($path);
- if (is_error($upload_to_temporary)) {
- return $upload_to_temporary;
- }
- $media_id = $upload_to_temporary['media_id'];
- $data = array(
- 'head_img_media_id' => $media_id,
- 'x1' => 0,
- 'y1' => 0,
- 'x2' => 1,
- 'y2' => 1,
- );
- $url = "https://api.weixin.qq.com/cgi-bin/account/modifyheadimage?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function modifySignature($signature) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('signature' => $signature);
- $url = "https://api.weixin.qq.com/cgi-bin/account/modifysignature?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function getAllCategories() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/cgi-bin/wxopen/getallcategories?access_token={$token}";
- return $this->request($url);
- }
-
- public function addCategory($category) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- if (!empty($category['certicates']) && !empty($category['certicates']['value'])) {
- $upload_to_temporary = $this->uploadMedia($category['certicates']['value']);
- if (is_error($upload_to_temporary)) {
- return $upload_to_temporary;
- }
- $category['certicates']['value'] = $upload_to_temporary['media_id'];
- }
- $data = array('categories' => $category);
- $url = "https://api.weixin.qq.com/cgi-bin/wxopen/addcategory?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function deleteCategory($data) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/cgi-bin/wxopen/deletecategory?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function getCategory() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/cgi-bin/wxopen/getcategory?access_token={$token}";
- return $this->request($url);
- }
-
- public function modifyCategory($data) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/cgi-bin/wxopen/modifycategory?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function getTemplateDraftList() {
- $token = $this->getComponentAccesstoken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token={$token}";
- return $this->request($url);
- }
-
- public function getTemplatelist() {
- $token = $this->getComponentAccesstoken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/gettemplatelist?access_token={$token}";
- return $this->request($url);
- }
-
- public function addToTemplate($draft_id) {
- $token = $this->getComponentAccesstoken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('draft_id' => $draft_id);
- $url = "https://api.weixin.qq.com/wxa/addtotemplate?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function deleteTemplate($template_id) {
- $token = $this->getComponentAccesstoken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('template_id' => $template_id);
- $url = "https://api.weixin.qq.com/wxa/deletetemplate?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function commit($template_id, $ext_json, $version, $desc = '') {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array(
- 'template_id' => $template_id,
- 'ext_json' => !empty($ext_json) ? json_encode($ext_json) : '',
- 'user_version' => $version,
- 'user_desc' => $desc,
- );
- $url = "https://api.weixin.qq.com/wxa/commit?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function getQrcode($path = '') {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$token}";
- if (!empty($path)) {
- $url .= '&path=' . urlencode($path);
- }
- return ihttp_request($url);
- }
-
- public function getWxappCategory() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/get_category?access_token={$token}";
- return $this->request($url);
- }
-
- public function getWxappPage() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/get_page?access_token={$token}";
- return $this->request($url);
- }
-
- public function submitAudit($item_list = array()) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('item_list' => $item_list);
- $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token={$token}";
- return $this->request($url, $data);
- }
-
- public function getLatestAuditStatus() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token={$token}";
- return $this->request($url);
- }
-
-
- public function getAuditStatus($auditid) {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $data = array('auditid' => $auditid);
- $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token={$token}";
-
- return $this->request($url, $data);
- }
-
-
- public function release() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/release?access_token={$token}";
- $response = ihttp_request($url, '{}');
- $response = json_decode($response['content'], true);
- if (empty($response) || !empty($response['errcode'])) {
- return error($response['errcode'], $this->errorCode($response['errcode'], $response['errmsg']));
- }
- return $response;
- }
-
- public function undoCodeAudit() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token={$token}";
- return $this->request($url);
- }
-
- public function revertCodeRelease() {
- $token = $this->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
- $url = "https://api.weixin.qq.com/wxa/revertcoderelease?access_token={$token}";
- return $this->request($url);
- }
- }
|