wxapp.platform.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->classs('weixin.platform');
  8. class WxappPlatform extends WeixinPlatform {
  9. 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';
  10. const FAST_REGISTER_WEAPP_CREATE = 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create&component_access_token=%s';
  11. const FAST_REGISTER_WEAPP_SEARCH = 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=search&component_access_token=%s';
  12. public $appid;
  13. protected $appsecret;
  14. public $encodingaeskey;
  15. public $token;
  16. protected $refreshtoken;
  17. protected $tablename = 'account_wxapp';
  18. protected $menuFrame = 'wxapp';
  19. protected $type = ACCOUNT_TYPE_APP_AUTH;
  20. protected $typeName = '微信小程序';
  21. protected $typeSign = WXAPP_TYPE_SIGN;
  22. protected $supportVersion = STATUS_ON;
  23. public function __construct($uniaccount = array()) {
  24. $setting = setting_load('platform');
  25. $this->appid = $setting['platform']['appid'];
  26. $this->appsecret = $setting['platform']['appsecret'];
  27. $this->token = $setting['platform']['token'];
  28. $this->encodingaeskey = $setting['platform']['encodingaeskey'];
  29. parent::__construct($uniaccount);
  30. }
  31. protected function getAccountInfo($uniacid) {
  32. if ('wxd101a85aa106f53e' == $this->account['key']) {
  33. $this->account['key'] = $this->appid;
  34. $this->openPlatformTestCase();
  35. }
  36. $account = table('account_wxapp')->getAccount($uniacid);
  37. $account['encrypt_key'] = $this->appid;
  38. return $account;
  39. }
  40. public function getOauthInfo($code = '') {
  41. $component_accesstoken = $this->getComponentAccesstoken();
  42. if (is_error($component_accesstoken)) {
  43. return $component_accesstoken;
  44. }
  45. $apiurl = sprintf(self::JSCODEURL, $this->account['key'], $code, $this->appid, $component_accesstoken);
  46. $response = $this->request($apiurl);
  47. if (is_error($response)) {
  48. return $response;
  49. }
  50. cache_write('account_oauth_refreshtoken' . $this->account['key'], $response['refresh_token']);
  51. return $response;
  52. }
  53. protected function setAuthRefreshToken($token) {
  54. $tablename = 'account_wxapp';
  55. pdo_update($tablename, array('auth_refresh_token' => $token), array('uniacid' => $this->account['uniacid']));
  56. cache_write(cache_system_key('account_auth_refreshtoken', array('uniacid' => $this->account['uniacid'])), $token);
  57. }
  58. public function pkcs7Encode($encrypt_data, $iv) {
  59. $key = base64_decode($_SESSION['session_key']);
  60. $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
  61. if (is_error($result)) {
  62. return error(1, '解密失败');
  63. }
  64. $result = json_decode($result, true);
  65. if (empty($result)) {
  66. return error(1, '解密失败');
  67. }
  68. if ($result['watermark']['appid'] != $this->account['key']) {
  69. return error(1, '解密失败');
  70. }
  71. unset($result['watermark']);
  72. return $result;
  73. }
  74. public function result($errno, $message = '', $data = '') {
  75. exit(json_encode(array(
  76. 'errno' => $errno,
  77. 'message' => $message,
  78. 'data' => $data,
  79. )));
  80. }
  81. public function getDailyVisitTrend($date) {
  82. $token = $this->getAccessToken();
  83. if (is_error($token)) {
  84. return $token;
  85. }
  86. $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token={$token}";
  87. $response = $this->requestApi($url, json_encode(array('begin_date' => $date, 'end_date' => $date)));
  88. if (is_error($response)) {
  89. return $response;
  90. }
  91. return $response['list'][0];
  92. }
  93. public function fastRegisterWxapp($data) {
  94. $component_accesstoken = $this->getComponentAccesstoken();
  95. if (is_error($component_accesstoken)) {
  96. return $component_accesstoken;
  97. }
  98. $apiurl = sprintf(self::FAST_REGISTER_WEAPP_CREATE, $component_accesstoken);
  99. $post = array(
  100. 'name' => $data['name'],
  101. 'code' => $data['code'],
  102. 'code_type' => $data['code_type'],
  103. 'legal_persona_wechat' => $data['legal_persona_wechat'],
  104. 'legal_persona_name' => $data['legal_persona_name'],
  105. 'component_phone' => $data['component_phone'],
  106. );
  107. return $this->request($apiurl, $post);
  108. }
  109. public function fastRegisterWxappSearch($data) {
  110. $component_accesstoken = $this->getComponentAccesstoken();
  111. if (is_error($component_accesstoken)) {
  112. return $component_accesstoken;
  113. }
  114. $apiurl = sprintf(self::FAST_REGISTER_WEAPP_SEARCH, $component_accesstoken);
  115. $post = array(
  116. 'name' => $data['name'],
  117. 'legal_persona_wechat' => $data['legal_persona_wechat'],
  118. 'legal_persona_name' => $data['legal_persona_name'],
  119. );
  120. return $this->request($apiurl, $post);
  121. }
  122. public function bindTester($wechatid) {
  123. $token = $this->getAccessToken();
  124. if (is_error($token)) {
  125. return $token;
  126. }
  127. $data = array('wechatid' => $wechatid);
  128. $url = "https://api.weixin.qq.com/wxa/bind_tester?access_token={$token}";
  129. return $this->request($url, $data);
  130. }
  131. public function getDomain() {
  132. $token = $this->getAccessToken();
  133. if (is_error($token)) {
  134. return $token;
  135. }
  136. $data = array(
  137. 'action' => 'get',
  138. );
  139. $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token={$token}";
  140. return $this->request($url, $data);
  141. }
  142. public function setWebViewDomain($data) {
  143. $webviewdomain = array();
  144. if ('get' == $data['action']) {
  145. $cachekey = cache_system_key('account_web_view_domain', array('uniacid' => $this->account['uniacid']));
  146. $webviewdomain = cache_load($cachekey);
  147. }
  148. if (empty($webviewdomain)) {
  149. $token = $this->getAccessToken();
  150. if (is_error($token)) {
  151. return $token;
  152. }
  153. $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token={$token}";
  154. $webviewdomain = $this->request($url, $data);
  155. if (is_error($webviewdomain)) {
  156. return error($webviewdomain['errno'], $this->errorCode($webviewdomain['errno']));
  157. }
  158. if ('get' == $data['action']) {
  159. $webviewdomain = $webviewdomain['webviewdomain'];
  160. cache_write($cachekey, $webviewdomain, CACHE_EXPIRE_LONG);
  161. }
  162. }
  163. return $webviewdomain;
  164. }
  165. public function modifyDomain($domains) {
  166. $token = $this->getAccessToken();
  167. if (is_error($token)) {
  168. return $token;
  169. }
  170. $data = array(
  171. 'action' => 'set',
  172. 'requestdomain' => $domains['requestdomain'],
  173. 'wsrequestdomain' => $domains['wsrequestdomain'],
  174. 'uploaddomain' => $domains['uploaddomain'],
  175. 'downloaddomain' => $domains['downloaddomain'],
  176. );
  177. $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token={$token}";
  178. return $this->request($url, $data);
  179. }
  180. public function getAccountBasicInfo() {
  181. $token = $this->getAccessToken();
  182. if (is_error($token)) {
  183. return $token;
  184. }
  185. $url = "https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo?access_token={$token}";
  186. return $this->request($url);
  187. }
  188. public function setNickname($name) {
  189. $token = $this->getAccessToken();
  190. if (is_error($token)) {
  191. return $token;
  192. }
  193. $data = array('nick_name' => $name);
  194. $url = "https://api.weixin.qq.com/wxa/setnickname?access_token={$token}";
  195. return $this->request($url, $data);
  196. }
  197. public function queryNickname($audit_id) {
  198. $token = $this->getAccessToken();
  199. if (is_error($token)) {
  200. return $token;
  201. }
  202. $data = array('audit_id' => $audit_id);
  203. $url = "https://api.weixin.qq.com/wxa/api_wxa_querynickname?access_token={$token}";
  204. return $this->request($url, $data);
  205. }
  206. public function checkwxVerifyNickname($nick_name) {
  207. $token = $this->getAccessToken();
  208. if (is_error($token)) {
  209. return $token;
  210. }
  211. $data = array('nick_name' => $nick_name);
  212. $url = "https://api.weixin.qq.com/cgi-bin/wxverify/checkwxverifynickname?access_token={$token}";
  213. return $this->request($url, $data);
  214. }
  215. public function modifyHeadImage($path) {
  216. $token = $this->getAccessToken();
  217. if (is_error($token)) {
  218. return $token;
  219. }
  220. $upload_to_temporary = $this->uploadMedia($path);
  221. if (is_error($upload_to_temporary)) {
  222. return $upload_to_temporary;
  223. }
  224. $media_id = $upload_to_temporary['media_id'];
  225. $data = array(
  226. 'head_img_media_id' => $media_id,
  227. 'x1' => 0,
  228. 'y1' => 0,
  229. 'x2' => 1,
  230. 'y2' => 1,
  231. );
  232. $url = "https://api.weixin.qq.com/cgi-bin/account/modifyheadimage?access_token={$token}";
  233. return $this->request($url, $data);
  234. }
  235. public function modifySignature($signature) {
  236. $token = $this->getAccessToken();
  237. if (is_error($token)) {
  238. return $token;
  239. }
  240. $data = array('signature' => $signature);
  241. $url = "https://api.weixin.qq.com/cgi-bin/account/modifysignature?access_token={$token}";
  242. return $this->request($url, $data);
  243. }
  244. public function getAllCategories() {
  245. $token = $this->getAccessToken();
  246. if (is_error($token)) {
  247. return $token;
  248. }
  249. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/getallcategories?access_token={$token}";
  250. return $this->request($url);
  251. }
  252. public function addCategory($category) {
  253. $token = $this->getAccessToken();
  254. if (is_error($token)) {
  255. return $token;
  256. }
  257. if (!empty($category['certicates']) && !empty($category['certicates']['value'])) {
  258. $upload_to_temporary = $this->uploadMedia($category['certicates']['value']);
  259. if (is_error($upload_to_temporary)) {
  260. return $upload_to_temporary;
  261. }
  262. $category['certicates']['value'] = $upload_to_temporary['media_id'];
  263. }
  264. $data = array('categories' => $category);
  265. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/addcategory?access_token={$token}";
  266. return $this->request($url, $data);
  267. }
  268. public function deleteCategory($data) {
  269. $token = $this->getAccessToken();
  270. if (is_error($token)) {
  271. return $token;
  272. }
  273. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/deletecategory?access_token={$token}";
  274. return $this->request($url, $data);
  275. }
  276. public function getCategory() {
  277. $token = $this->getAccessToken();
  278. if (is_error($token)) {
  279. return $token;
  280. }
  281. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/getcategory?access_token={$token}";
  282. return $this->request($url);
  283. }
  284. public function modifyCategory($data) {
  285. $token = $this->getAccessToken();
  286. if (is_error($token)) {
  287. return $token;
  288. }
  289. $url = "https://api.weixin.qq.com/cgi-bin/wxopen/modifycategory?access_token={$token}";
  290. return $this->request($url, $data);
  291. }
  292. public function getTemplateDraftList() {
  293. $token = $this->getComponentAccesstoken();
  294. if (is_error($token)) {
  295. return $token;
  296. }
  297. $url = "https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token={$token}";
  298. return $this->request($url);
  299. }
  300. public function getTemplatelist() {
  301. $token = $this->getComponentAccesstoken();
  302. if (is_error($token)) {
  303. return $token;
  304. }
  305. $url = "https://api.weixin.qq.com/wxa/gettemplatelist?access_token={$token}";
  306. return $this->request($url);
  307. }
  308. public function addToTemplate($draft_id) {
  309. $token = $this->getComponentAccesstoken();
  310. if (is_error($token)) {
  311. return $token;
  312. }
  313. $data = array('draft_id' => $draft_id);
  314. $url = "https://api.weixin.qq.com/wxa/addtotemplate?access_token={$token}";
  315. return $this->request($url, $data);
  316. }
  317. public function deleteTemplate($template_id) {
  318. $token = $this->getComponentAccesstoken();
  319. if (is_error($token)) {
  320. return $token;
  321. }
  322. $data = array('template_id' => $template_id);
  323. $url = "https://api.weixin.qq.com/wxa/deletetemplate?access_token={$token}";
  324. return $this->request($url, $data);
  325. }
  326. public function commit($template_id, $ext_json, $version, $desc = '') {
  327. $token = $this->getAccessToken();
  328. if (is_error($token)) {
  329. return $token;
  330. }
  331. $data = array(
  332. 'template_id' => $template_id,
  333. 'ext_json' => !empty($ext_json) ? json_encode($ext_json) : '',
  334. 'user_version' => $version,
  335. 'user_desc' => $desc,
  336. );
  337. $url = "https://api.weixin.qq.com/wxa/commit?access_token={$token}";
  338. return $this->request($url, $data);
  339. }
  340. public function getQrcode($path = '') {
  341. $token = $this->getAccessToken();
  342. if (is_error($token)) {
  343. return $token;
  344. }
  345. $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token={$token}";
  346. if (!empty($path)) {
  347. $url .= '&path=' . urlencode($path);
  348. }
  349. return ihttp_request($url);
  350. }
  351. public function getWxappCategory() {
  352. $token = $this->getAccessToken();
  353. if (is_error($token)) {
  354. return $token;
  355. }
  356. $url = "https://api.weixin.qq.com/wxa/get_category?access_token={$token}";
  357. return $this->request($url);
  358. }
  359. public function getWxappPage() {
  360. $token = $this->getAccessToken();
  361. if (is_error($token)) {
  362. return $token;
  363. }
  364. $url = "https://api.weixin.qq.com/wxa/get_page?access_token={$token}";
  365. return $this->request($url);
  366. }
  367. public function submitAudit($item_list = array()) {
  368. $token = $this->getAccessToken();
  369. if (is_error($token)) {
  370. return $token;
  371. }
  372. $data = array('item_list' => $item_list);
  373. $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token={$token}";
  374. return $this->request($url, $data);
  375. }
  376. public function getLatestAuditStatus() {
  377. $token = $this->getAccessToken();
  378. if (is_error($token)) {
  379. return $token;
  380. }
  381. $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token={$token}";
  382. return $this->request($url);
  383. }
  384. public function getAuditStatus($auditid) {
  385. $token = $this->getAccessToken();
  386. if (is_error($token)) {
  387. return $token;
  388. }
  389. $data = array('auditid' => $auditid);
  390. $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token={$token}";
  391. return $this->request($url, $data);
  392. }
  393. public function release() {
  394. $token = $this->getAccessToken();
  395. if (is_error($token)) {
  396. return $token;
  397. }
  398. $url = "https://api.weixin.qq.com/wxa/release?access_token={$token}";
  399. $response = ihttp_request($url, '{}');
  400. $response = json_decode($response['content'], true);
  401. if (empty($response) || !empty($response['errcode'])) {
  402. return error($response['errcode'], $this->errorCode($response['errcode'], $response['errmsg']));
  403. }
  404. return $response;
  405. }
  406. public function undoCodeAudit() {
  407. $token = $this->getAccessToken();
  408. if (is_error($token)) {
  409. return $token;
  410. }
  411. $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token={$token}";
  412. return $this->request($url);
  413. }
  414. public function revertCodeRelease() {
  415. $token = $this->getAccessToken();
  416. if (is_error($token)) {
  417. return $token;
  418. }
  419. $url = "https://api.weixin.qq.com/wxa/revertcoderelease?access_token={$token}";
  420. return $this->request($url);
  421. }
  422. }