人人商城

xzapp.account.class.php 21KB

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