xzapp.account.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. <?php
  2. defined('IN_IA') or exit('Access Denied');
  3. class XzappAccount extends WeAccount {
  4. protected $tablename = 'account_xzapp';
  5. protected $menuFrame = 'account';
  6. protected $type = ACCOUNT_TYPE_XZAPP_NORMAL;
  7. protected $typeName = '熊掌号';
  8. protected $typeSign = XZAPP_TYPE_SIGN;
  9. protected $typeTempalte = '-xzapp';
  10. protected function getAccountInfo($acid) {
  11. return table('account_xzapp')->getByAcid($acid);
  12. }
  13. public function checkSign() {
  14. $arrParams = array(
  15. $token = $this->account['token'],
  16. $intTimeStamp = $_GET['timestamp'],
  17. $strNonce = $_GET['nonce'],
  18. );
  19. sort($arrParams, SORT_STRING);
  20. $strParam = implode($arrParams);
  21. $strSignature = sha1($strParam);
  22. return $strSignature == $_GET['signature'];
  23. }
  24. public function getAccessToken() {
  25. $cachekey = cache_system_key('accesstoken', array('uniacid' => $this->account['uniacid']));
  26. $cache = cache_load($cachekey);
  27. if (!empty($cache) && !empty($cache['token']) && $cache['expire'] > TIMESTAMP) {
  28. $this->account['access_token'] = $cache;
  29. return $cache['token'];
  30. }
  31. if (empty($this->account['key']) || empty($this->account['secret'])) {
  32. return error('-1', '未填写熊掌号的 appid 或者 appsecret!');
  33. }
  34. $url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={$this->account['key']}&client_secret={$this->account['secret']}";
  35. $content = ihttp_get($url);
  36. $token = @json_decode($content['content'], true);
  37. $record = array();
  38. $record['token'] = $token['access_token'];
  39. $record['expire'] = TIMESTAMP + $token['expires_in'] - 200;
  40. $this->account['access_token'] = $record;
  41. cache_write($cachekey, $record);
  42. return $record['token'];
  43. }
  44. public function buildSignature($encrypt_msg) {
  45. $token = $this->account['token'];
  46. $array = array($encrypt_msg, $token, $_GET['timestamp'], $_GET['nonce']);
  47. sort($array, SORT_STRING);
  48. $str = implode($array);
  49. $str = sha1($str);
  50. return $str;
  51. }
  52. public function checkSignature($encrypt_msg) {
  53. $str = $this->buildSignature($encrypt_msg);
  54. return $str == $_GET['msg_signature'];
  55. }
  56. public function encryptMsg($text) {
  57. $appid = $this->account['key'];
  58. $encodingaeskey = $this->account['encodingaeskey'];
  59. $key = base64_decode($encodingaeskey . '=');
  60. static $blockSize = 32;
  61. $text = substr(md5(time()), 0, 16) . pack('N', strlen($text)) . $text . $appid;
  62. $padLen = $blockSize - (strlen($text) % $blockSize);
  63. $text .= str_repeat(chr($padLen), $padLen == 0 ? $blockSize : $padLen);
  64. $iv = substr($key, 0, 16);
  65. $encoded = openssl_encrypt($text, 'AES-256-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
  66. $encrypt_msg = base64_encode($encoded);
  67. $signature = $this->buildSignature($encrypt_msg);
  68. return array($signature, $encrypt_msg);
  69. }
  70. public function decryptMsg($postData) {
  71. $appid = $this->account['key'];
  72. $encodingaeskey = $this->account['encodingaeskey'];
  73. $key = base64_decode($encodingaeskey . '=');
  74. $packet = $this->xmlExtract($postData);
  75. if (is_error($packet)) {
  76. return error(-1, $packet['message']);
  77. }
  78. $encrypt = base64_decode($packet['encrypt']);
  79. $istrue = $this->checkSignature($packet['encrypt']);
  80. if(!$istrue) {
  81. return error(-1, "熊掌号签名错误!");
  82. }
  83. $iv = substr($key, 0, 16);
  84. $decoded = openssl_decrypt($encrypt, 'AES-256-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
  85. $pad = ord(substr($decoded, -1));
  86. $pad = ($pad < 1 || $pad > 32) ? 0 : $pad;
  87. $decoded = substr($decoded, 0, strlen($decoded) - $pad);
  88. $text = substr($decoded, 16, strlen($decoded));
  89. $unpack = unpack('Nlen/', substr($text, 0, 4));
  90. $content = substr($text, 4, $unpack['len']);
  91. $clientId = substr($text, $unpack['len'] + 4);
  92. if ($clientId != $appid) {
  93. return error(-1, 'ERR: decode clientId is ' . $clientId . ', need client is ' . $appid);
  94. }
  95. return $content;
  96. }
  97. public function xmlExtract($message) {
  98. $packet = array();
  99. if (!empty($message)){
  100. $obj = isimplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA);
  101. if($obj instanceof SimpleXMLElement) {
  102. $packet['encrypt'] = strval($obj->Encrypt);
  103. $packet['to'] = strval($obj->ToUserName);
  104. }
  105. }
  106. if(!empty($packet['encrypt'])) {
  107. return $packet;
  108. } else {
  109. return error(-1, "熊掌号返回接口错误");
  110. }
  111. }
  112. function xmlDetract($data) {
  113. $xml['Encrypt'] = $data[1];
  114. $xml['MsgSignature'] = $data[0];
  115. $xml['TimeStamp'] = $_GET['timestamp'];
  116. $xml['Nonce'] = $_GET['nonce'];
  117. return array2xml($xml);
  118. }
  119. protected function requestApi($url, $post = '') {
  120. $response = ihttp_request($url, $post);
  121. $result = @json_decode($response['content'], true);
  122. if ($result['error_code']) {
  123. return error(-1, "访问熊掌号接口失败, 错误代码:【{$result['error_code']}】, 错误信息:【{$result['error_msg']}】");
  124. }
  125. return $result;
  126. }
  127. public function checkIntoManage() {
  128. if (empty($this->account) || (!empty($this->uniaccount['account']) && $this->uniaccount['type'] != ACCOUNT_TYPE_XZAPP_NORMAL && !defined('IN_MODULE'))) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. public function getOauthCodeUrl($callback, $state = '') {
  134. $this->account['callbackurl'] = $callback;
  135. return "https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id={$this->account['key']}&redirect_uri={$callback}&scope=snsapi_base&state={$state}";
  136. }
  137. public function getOauthUserInfoUrl($callback, $state = '') {
  138. $this->account['callbackurl'] = $callback;
  139. return "https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id={$this->account['key']}&redirect_uri={$callback}&scope=snsapi_userinfo&state={$state}";
  140. }
  141. public function getOauthInfo($code = '') {
  142. global $_W,$_GPC;
  143. if (!empty($_GPC['code'])) {
  144. $code = $_GPC['code'];
  145. }
  146. if (empty($code)) {
  147. $oauth_url = uni_account_oauth_host();
  148. $url = urlencode($oauth_url . "app/index.php?{$_SERVER['QUERY_STRING']}");
  149. $forward = $this->getOauthCodeUrl($url);
  150. header('Location: ' . $forward);
  151. exit;
  152. }
  153. $str = '';
  154. if(uni_is_multi_acid()) {
  155. $str = "&j={$_W['acid']}";
  156. }
  157. $oauth_type = $_GPC['scope'];
  158. $oauth_url = uni_account_oauth_host();
  159. $url = $oauth_url . "app/index.php?i={$_W['uniacid']}{$str}&c=auth&a=oauth&scope=" . $oauth_type;
  160. $callback = urlencode($url);
  161. $oauth_info = $this->getOauthAccessToken($code, $callback);
  162. $user_info_url = "https://openapi.baidu.com/rest/2.0/cambrian/sns/userinfo?access_token={$oauth_info['token']}&openid={$oauth_info['openid']}";
  163. $response = $this->requestApi($user_info_url);
  164. return $response;
  165. }
  166. public function getOauthAccessToken($code, $callback) {
  167. $cachekey = cache_system_key('oauthaccesstoken', array('acid' => $this->account['acid']));
  168. $cache = cache_load($cachekey);
  169. if (!empty($cache) && !empty($cache['token']) && $cache['expire'] > TIMESTAMP) {
  170. return $cache;
  171. }
  172. $url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code={$code}&client_id={$this->account['key']}&client_secret={$this->account['secret']}&redirect_uri={$callback}";
  173. $oauth_info = $this->requestApi($url);
  174. $record = array();
  175. $record['token'] = $oauth_info['access_token'];
  176. $record['openid'] = $oauth_info['openid'];
  177. $record['expire'] = TIMESTAMP + $oauth_info['expires_in'] - 200;
  178. cache_write($cachekey, $record);
  179. return $record;
  180. }
  181. public function isTagSupported() {
  182. if (!empty($this->account['key']) && !empty($this->account['secret'])) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. }
  188. public function fansTagFetchAll() {
  189. $token = $this->getAccessToken();
  190. if (is_error($token)) {
  191. return $token;
  192. }
  193. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/get?access_token={$token}";
  194. $result = $this->requestApi($url);
  195. return $result;
  196. }
  197. public function fansAll($startopenid = '') {
  198. global $_W;
  199. $token = $this->getAccessToken();
  200. if (is_error($token)) {
  201. return $token;
  202. }
  203. $url = "https://openapi.baidu.com/rest/2.0/cambrian/user/get?start_index=0&access_token={$token}";
  204. if (!empty($_GPC['next_openid'])) {
  205. $url .= '&start_index=' . $_GPC['next_openid'];
  206. }
  207. $res = ihttp_get($url);
  208. $content = json_decode($res['content'], true);
  209. if ($content['error_code']) {
  210. return error(-1, '访问熊掌号接口失败, 错误代码: 【' . $content['error_code'] . '】, 错误信息:【' . $content['error_msg'] . '】');
  211. }
  212. $return = array();
  213. $return['total'] = $content['total'];
  214. $return['fans'] = $content['data'];
  215. $return['next'] = $content['start_index'];
  216. return $return;
  217. }
  218. public function fansQueryInfo($uniid, $isOpen = true) {
  219. if ($isOpen) {
  220. $openid = $uniid;
  221. } else {
  222. exit('error');
  223. }
  224. $token = $this->getAccessToken();
  225. if(is_error($token)){
  226. return $token;
  227. }
  228. $data = array(
  229. 'user_list' => array(
  230. array(
  231. 'openid' => $uniid,
  232. )
  233. ),
  234. );
  235. $url = "https://openapi.baidu.com/rest/2.0/cambrian/user/info?access_token={$token}";
  236. $result = $this->requestApi($url, json_encode($data));
  237. return $result['user_info_list'][0];
  238. }
  239. public function fansBatchQueryInfo($data) {
  240. if (empty($data)) {
  241. return error(-1, '粉丝 openid 错误');
  242. }
  243. $token = $this->getAccessToken();
  244. if (is_error($token)) {
  245. return $token;
  246. }
  247. $list['user_list'] = array();
  248. foreach ($data as $da) {
  249. $list['user_list'][] = array('openid' => $da);
  250. }
  251. $url = "https://openapi.baidu.com/rest/2.0/cambrian/user/info?access_token={$token}";
  252. $result = $this->requestApi($url, json_encode($list));
  253. return $result['user_info_list'];
  254. }
  255. public function fansTagAdd($tagname) {
  256. if(empty($tagname)) {
  257. return error(-1, '请填写标签名称');
  258. }
  259. $token = $this->getAccessToken();
  260. if(is_error($token)){
  261. return $token;
  262. }
  263. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/create?access_token={$token}";
  264. $data = stripslashes(ijson_encode(array('tag' => array('name' => $tagname)), JSON_UNESCAPED_UNICODE));
  265. $result = $this->requestApi($url, $data);
  266. return $result;
  267. }
  268. public function fansTagTagging($openid, $tagids) {
  269. $openid = (string) $openid;
  270. $tagids = (array) $tagids;
  271. if (empty($openid)) {
  272. return error(-1, '没有填写用户openid');
  273. }
  274. if (empty($tagids)) {
  275. return error(-1, '没有填写标签');
  276. }
  277. if (count($tagids) > 3) {
  278. return error(-1, '最多3个标签');
  279. }
  280. $token = $this->getAccessToken();
  281. if (is_error($token)) {
  282. return $token;
  283. }
  284. $fetch_result = $this->fansTagFetchOwnTags($openid);
  285. if (is_error($fetch_result)) {
  286. return $fetch_result;
  287. }
  288. foreach ($fetch_result['tagid_list'] as $del_tagid) {
  289. $this->fansTagBatchUntagging($openid, $del_tagid);
  290. }
  291. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/batchtagging?access_token={$token}";
  292. foreach ($tagids as $tagid) {
  293. $data = array(
  294. 'openid_list' => array($openid),
  295. 'tagid' => $tagid
  296. );
  297. $data = json_encode($data);
  298. $result = $this->requestApi($url, $data);
  299. if (is_error($result)) {
  300. return $result;
  301. }
  302. }
  303. return true;
  304. }
  305. public function fansTagFetchOwnTags($openid) {
  306. $openid = (string)$openid;
  307. if (empty($openid)) {
  308. return error(-1, '没有填写用户openid');
  309. }
  310. $token = $this->getAccessToken();
  311. if (is_error($token)) {
  312. return $token;
  313. }
  314. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/getidlist?access_token={$token}";
  315. $data = json_encode(array('openid' => $openid));
  316. $result = $this->requestApi($url, $data);
  317. return $result;
  318. }
  319. public function fansTagBatchUntagging($openid_list, $tagid) {
  320. $openid_list = (array)$openid_list;
  321. $tagid = (int)$tagid;
  322. if (empty($openid_list)) {
  323. return error(-1, '缺少openid参数');
  324. }
  325. if (empty($tagid)) {
  326. return error(-1, '没有填写tagid');
  327. }
  328. $token = $this->getAccessToken();
  329. if (is_error($token)) {
  330. return $token;
  331. }
  332. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/batchuntagging?access_token={$token}";
  333. $data = array(
  334. 'openid_list' => $openid_list,
  335. 'tagid' => $tagid
  336. );
  337. $data = json_encode($data);
  338. $result = $this->requestApi($url, $data);
  339. if (is_error($result)) {
  340. return $result;
  341. }
  342. return true;
  343. }
  344. public function fansTagBatchTagging($openid_list, $tagid) {
  345. $openid_list = (array)$openid_list;
  346. $tagid = (int)$tagid;
  347. if(empty($openid_list)){
  348. return error(-1, '没有填写用户openid列表');
  349. }
  350. if(empty($tagid)) {
  351. return error(-1, '没有填写tagid');
  352. }
  353. $token = $this->getAccessToken();
  354. if(is_error($token)){
  355. return $token;
  356. }
  357. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/batchtagging?access_token={$token}";
  358. $data = array(
  359. 'openid_list' => $openid_list,
  360. 'tagid' => $tagid
  361. );
  362. $result = $this->requestApi($url, json_encode($data));
  363. if (is_error($result)) {
  364. return $result;
  365. }
  366. return true;
  367. }
  368. public function menuCurrentQuery() {
  369. $token = $this->getAccessToken();
  370. if (is_error($token)) {
  371. return $token;
  372. }
  373. $url = "https://openapi.baidu.com/rest/2.0/cambrian/menu/get?access_token={$token}";
  374. $res = $this->requestApi($url);
  375. return $res;
  376. }
  377. public function menuCreate($menu) {
  378. global $_W;
  379. $token = $this->getAccessToken();
  380. if(is_error($token)){
  381. return $token;
  382. }
  383. $data['menues'] = json_encode($menu);
  384. $url = "https://openapi.baidu.com/rest/2.0/cambrian/menu/create?access_token={$token}";
  385. $res = $this->requestApi($url, $data);
  386. if (is_error($res)) {
  387. return $res;
  388. } else {
  389. return 0;
  390. }
  391. }
  392. public function menuBuild($post, $is_conditional = false) {
  393. $menu = array();
  394. foreach ($post['button'] as $button) {
  395. $temp = array();
  396. $temp['name'] = $button['name'];
  397. if (empty($button['sub_button'])) {
  398. $temp['type'] = $button['type'];
  399. if ($button['type'] == 'click') {
  400. if (!empty($button['media_id']) && empty($button['key'])) {
  401. $temp['key'] = $button['media_id'];
  402. $temp['msg'] = array(
  403. 'text' => '',
  404. 'type' => 'view_limited',
  405. 'materialId' => $button['media_id']
  406. );
  407. }
  408. if (!empty($button['key']) && $button['key'] == $button['msg']['materialId']) {
  409. $temp['msg'] = $button['msg'];
  410. $temp['key'] = $button['key'];
  411. }
  412. } elseif ($button['type'] == 'view') {
  413. $temp['url'] = $button['url'];
  414. }
  415. } else {
  416. foreach ($button['sub_button'] as $sub_button) {
  417. $sub_temp = array();
  418. $sub_temp['name'] = $sub_button['name'];
  419. $sub_temp['type'] = $sub_button['type'];
  420. if ($sub_button['type'] == 'click') {
  421. if (!empty($sub_button['media_id']) && empty($sub_button['key'])) {
  422. $sub_temp['key'] = $sub_button['media_id'];
  423. $sub_temp['msg'] = array(
  424. 'text' => '',
  425. 'type' => 'view_limited',
  426. 'materialId' => $sub_button['media_id']
  427. );
  428. }
  429. if (!empty($sub_button['key']) && $sub_button['key'] == $sub_button['msg']['materialId']) {
  430. $sub_temp['msg'] = $sub_button['msg'];
  431. $sub_temp['key'] = $sub_button['key'];
  432. }
  433. } elseif ($sub_button['type'] == 'view') {
  434. $sub_temp['url'] = $sub_button['url'];
  435. }
  436. $temp['sub_button'][] = $sub_temp;
  437. }
  438. }
  439. $menu['button'][] = $temp;
  440. }
  441. return $menu;
  442. }
  443. public function batchGetMaterial($type = 'news', $offset = 0, $count = 20) {
  444. global $_W;
  445. $token = $this->getAccessToken();
  446. if (is_error($token)) {
  447. return $token;
  448. }
  449. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/batchget_material?access_token={$token}&type={$type}&offset={$offset}&count={$count}";
  450. $response = $this->requestApi($url);
  451. if (!is_error($response)) {
  452. foreach ($response['item'] as $key => &$item) {
  453. foreach ($item['content']['news_item'] as $news_key => &$news_item) {
  454. $content = json_decode($news_item['content'], true);
  455. if (!empty($content) && is_array($content) && !empty($content['orihtml'])){
  456. $news_item['content'] = $content['orihtml'];
  457. }
  458. $news_info = $this->getMaterial($news_item['thumb_media_id']);
  459. $news_item['thumb_url'] = $news_info['url'];
  460. }
  461. }
  462. }
  463. return $response;
  464. }
  465. public function delMaterial($media_id) {
  466. $media_id = trim($media_id);
  467. if (empty($media_id)) {
  468. return error(-1, '素材media_id错误');
  469. }
  470. $token = $this->getAccessToken();
  471. if (is_error($token)) {
  472. return $token;
  473. }
  474. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/del_material?access_token=" . $token . "&media_id=" . $media_id;
  475. $response = $this->requestApi($url);
  476. return $response;
  477. }
  478. public function addMatrialNews($data) {
  479. $token = $this->getAccessToken();
  480. if(is_error($token)){
  481. return $token;
  482. }
  483. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/add_news?access_token={$token}";
  484. $data = stripslashes(urldecode(ijson_encode($data, JSON_UNESCAPED_UNICODE)));
  485. $response = $this->requestApi($url, $data);
  486. return $response['media_id'];
  487. }
  488. public function editMaterialNews($data) {
  489. $token = $this->getAccessToken();
  490. if(is_error($token)){
  491. return $token;
  492. }
  493. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/update_news?access_token={$token}";
  494. $response = $this->requestApi($url, stripslashes(ijson_encode($data, JSON_UNESCAPED_UNICODE)));
  495. return $response;
  496. }
  497. public function getMaterial($media_id) {
  498. $token = $this->getAccessToken();
  499. if (is_error($token)) {
  500. return $token;
  501. }
  502. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/get_material?access_token={$token}&media_id={$media_id}";
  503. $response = $this->requestApi($url);
  504. return $response;
  505. }
  506. public function uploadNewsThumb($thumb) {
  507. $token = $this->getAccessToken();
  508. if (is_error($token)) {
  509. return $token;
  510. }
  511. if (!file_exists($thumb)) {
  512. return error(1, '文件不存在');
  513. }
  514. $data = array(
  515. 'media' => '@' . $thumb,
  516. );
  517. $url = "https://openapi.baidu.com/rest/2.0/cambrian/media/uploadimg?access_token={$token}";
  518. $response = $this->requestApi($url, $data);
  519. return $response['url'];
  520. }
  521. public function uploadMediaFixed($path, $type = 'images') {
  522. if (empty($path)) {
  523. return error(-1, '参数错误');
  524. }
  525. if (in_array(substr(ltrim($path, '/'), 0, 6), array('images', 'videos', 'audios', 'thumb', 'voices'))) {
  526. $path = ATTACHMENT_ROOT . ltrim($path, '/');
  527. }
  528. if (!file_exists($path)) {
  529. return error(1, '文件不存在');
  530. }
  531. $token = $this->getAccessToken();
  532. if (is_error($token)){
  533. return $token;
  534. }
  535. $data = array(
  536. 'media' => '@' . $path
  537. );
  538. $url = "https://openapi.baidu.com/rest/2.0/cambrian/media/add_material?access_token={$token}";
  539. $response = $this->requestApi($url, $data);
  540. return $response;
  541. }
  542. public function sendCustomNotice($data) {
  543. if(empty($data)) {
  544. return error(-1, '参数错误');
  545. }
  546. $token = $this->getAccessToken();
  547. if(is_error($token)){
  548. return $token;
  549. }
  550. $url = "https://openapi.baidu.com/rest/2.0/cambrian/message/custom_send?access_token={$token}";
  551. $response = $this->requestApi($url, urldecode(json_encode($data)));
  552. WeUtility::logging('$resonse', var_export($response, true));
  553. if (is_error($response)) {
  554. return $response;
  555. }
  556. return true;
  557. }
  558. public function sendTplNotice($touser, $template_id, $postdata, $url = '') {
  559. if(empty($touser)) {
  560. return error(-1, '参数错误,粉丝openid不能为空');
  561. }
  562. if(empty($template_id)) {
  563. return error(-1, '参数错误,模板标示不能为空');
  564. }
  565. if(empty($postdata) || !is_array($postdata)) {
  566. return error(-1, '参数错误,请根据模板规则完善消息内容');
  567. }
  568. $token = $this->getAccessToken();
  569. if (is_error($token)) {
  570. return $token;
  571. }
  572. $data = array();
  573. $data['touser'] = $touser;
  574. $data['template_id'] = trim($template_id);
  575. $data['url'] = trim($url);
  576. $data['data'] = $postdata;
  577. $data = json_encode($data);
  578. $post_url = "https://openapi.baidu.com/rest/2.0/cambrian/template/send?access_token={$token}";
  579. $response = $this->requestApi($post_url, $data);
  580. if(is_error($response)) {
  581. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  582. }
  583. return true;
  584. }
  585. public function fansSendAll($group, $msgtype, $media_id) {
  586. $types = array('basic' => 'text', 'image' => 'image', 'news' => 'mpnews', 'voice' => 'voice');
  587. if (empty($types[$msgtype])) {
  588. return error(-1, '消息类型不合法');
  589. }
  590. $send_conent = ($types[$msgtype] == 'text') ? array('content' => $media_id) : array('media_id' => $media_id);
  591. if ($group == -1) {
  592. $data = array(
  593. 'filter' => array(
  594. 'is_to_all' => true,
  595. 'group_id' => $group
  596. ),
  597. 'msgtype' => $types[$msgtype],
  598. $types[$msgtype] => $send_conent,
  599. );
  600. } else {
  601. $openids = $this->getFansByTag($group);
  602. $data = array(
  603. 'touser' => $openids,
  604. 'msgtype' => $types[$msgtype],
  605. $types[$msgtype] => $send_conent,
  606. );
  607. }
  608. $token = $this->getAccessToken();
  609. if(is_error($token)){
  610. return $token;
  611. }
  612. $url = "https://openapi.baidu.com/rest/2.0/cambrian/message/sendall?access_token={$token}";
  613. $response = $this->requestApi($url, json_encode($data));
  614. return $response;
  615. }
  616. public function getFansByTag($tagid){
  617. $token = $this->getAccessToken();
  618. if(is_error($token)){
  619. return $token;
  620. }
  621. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tag/get?access_token={$token}";
  622. $data = array('tagid' => $tagid);
  623. $response = $this->requestApi($url, json_encode($data));
  624. return $response['data']['openid'];
  625. }
  626. public function getJsApiTicket() {
  627. $cachekey = cache_system_key('jsticket', array('acid' => $this->account['acid']));
  628. $cache = cache_load($cachekey);
  629. if(!empty($cache) && !empty($cache['ticket']) && $cache['expire'] > TIMESTAMP) {
  630. return $cache['ticket'];
  631. }
  632. $access_token = $this->getAccessToken();
  633. if(is_error($access_token)){
  634. return $access_token;
  635. }
  636. $url = "https://openapi.baidu.com/rest/2.0/cambrian/jssdk/getticket?access_token={$access_token}";
  637. $response = $this->requestApi($url);
  638. if (is_error($response)) {
  639. return $response;
  640. }
  641. $record = array();
  642. $record['ticket'] = $response['ticket'];
  643. $record['expire'] = TIMESTAMP + $response['expires_in'] - 200;
  644. $this->account['jsapi_ticket'] = $record;
  645. cache_write($cachekey, $record);
  646. return $record['ticket'];
  647. }
  648. public function getJssdkConfig($url = '') {
  649. global $_W;
  650. $jsapiTicket = $this->getJsApiTicket();
  651. if (is_error($jsapiTicket)) {
  652. $jsapiTicket = $jsapiTicket['message'];
  653. }
  654. $nonceStr = random(25);
  655. $timestamp = TIMESTAMP;
  656. $url = empty($url) ? $_W['siteurl'] : $url;
  657. $arr = array(
  658. "jsapi_ticket" => $jsapiTicket,
  659. "nonce_str" => $nonceStr,
  660. "timestamp" => $timestamp,
  661. "url" => urlencode($url)
  662. );
  663. ksort($arr);
  664. $string1 = http_build_query($arr);
  665. $signature = sha1($string1);
  666. $config = array(
  667. "appId" => $this->account['original'],
  668. "nonceStr" => $nonceStr,
  669. "timestamp" => "$timestamp",
  670. "signature" => $signature,
  671. "url" => urlencode($url),
  672. );
  673. return $config;
  674. }
  675. public function getMaterialSupport() {
  676. return array(
  677. 'mass' => array('news'=> false, 'image'=> false,'voice'=> false,'basic'=> false),
  678. 'chats' => array('basic'=> false,'news'=> false,'image'=> false,'music'=> true,'voice'=> false,'video'=> true)
  679. );
  680. }
  681. }