xzapp.account.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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), 0 == $padLen ? $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. public 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 getOauthCodeUrl($callback, $state = '') {
  128. $this->account['callbackurl'] = $callback;
  129. 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}";
  130. }
  131. public function getOauthUserInfoUrl($callback, $state = '') {
  132. $this->account['callbackurl'] = $callback;
  133. 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}";
  134. }
  135. public function getOauthInfo($code = '') {
  136. global $_W,$_GPC;
  137. if (!empty($_GPC['code'])) {
  138. $code = $_GPC['code'];
  139. }
  140. if (empty($code)) {
  141. $oauth_url = uni_account_oauth_host();
  142. $url = urlencode($oauth_url . "app/index.php?{$_SERVER['QUERY_STRING']}");
  143. $forward = $this->getOauthCodeUrl($url);
  144. header('Location: ' . $forward);
  145. exit;
  146. }
  147. $str = '';
  148. if (uni_is_multi_acid()) {
  149. $str = "&j={$_W['acid']}";
  150. }
  151. $oauth_type = $_GPC['scope'];
  152. $oauth_url = uni_account_oauth_host();
  153. $url = $oauth_url . "app/index.php?i={$_W['uniacid']}{$str}&c=auth&a=oauth&scope=" . $oauth_type;
  154. $callback = urlencode($url);
  155. $oauth_info = $this->getOauthAccessToken($code, $callback);
  156. $user_info_url = "https://openapi.baidu.com/rest/2.0/cambrian/sns/userinfo?access_token={$oauth_info['token']}&openid={$oauth_info['openid']}";
  157. $response = $this->requestApi($user_info_url);
  158. return $response;
  159. }
  160. public function getOauthAccessToken($code, $callback) {
  161. $cachekey = cache_system_key('oauthaccesstoken', array('acid' => $this->account['acid']));
  162. $cache = cache_load($cachekey);
  163. if (!empty($cache) && !empty($cache['token']) && $cache['expire'] > TIMESTAMP) {
  164. return $cache;
  165. }
  166. $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}";
  167. $oauth_info = $this->requestApi($url);
  168. $record = array();
  169. $record['token'] = $oauth_info['access_token'];
  170. $record['openid'] = $oauth_info['openid'];
  171. $record['expire'] = TIMESTAMP + $oauth_info['expires_in'] - 200;
  172. cache_write($cachekey, $record);
  173. return $record;
  174. }
  175. public function isTagSupported() {
  176. if (!empty($this->account['key']) && !empty($this->account['secret'])) {
  177. return true;
  178. } else {
  179. return false;
  180. }
  181. }
  182. public function fansTagFetchAll() {
  183. $token = $this->getAccessToken();
  184. if (is_error($token)) {
  185. return $token;
  186. }
  187. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/get?access_token={$token}";
  188. $result = $this->requestApi($url);
  189. return $result;
  190. }
  191. public function fansAll($startopenid = '') {
  192. global $_W;
  193. $token = $this->getAccessToken();
  194. if (is_error($token)) {
  195. return $token;
  196. }
  197. $url = "https://openapi.baidu.com/rest/2.0/cambrian/user/get?start_index=0&access_token={$token}";
  198. if (!empty($_GPC['next_openid'])) {
  199. $url .= '&start_index=' . $_GPC['next_openid'];
  200. }
  201. $res = ihttp_get($url);
  202. $content = json_decode($res['content'], true);
  203. if ($content['error_code']) {
  204. return error(-1, '访问熊掌号接口失败, 错误代码: 【' . $content['error_code'] . '】, 错误信息:【' . $content['error_msg'] . '】');
  205. }
  206. $return = array();
  207. $return['total'] = $content['total'];
  208. $return['fans'] = $content['data'];
  209. $return['next'] = $content['start_index'];
  210. return $return;
  211. }
  212. public function fansQueryInfo($uniid, $isOpen = true) {
  213. if ($isOpen) {
  214. $openid = $uniid;
  215. } else {
  216. exit('error');
  217. }
  218. $token = $this->getAccessToken();
  219. if (is_error($token)) {
  220. return $token;
  221. }
  222. $data = array(
  223. 'user_list' => array(
  224. array(
  225. 'openid' => $uniid,
  226. ),
  227. ),
  228. );
  229. $url = "https://openapi.baidu.com/rest/2.0/cambrian/user/info?access_token={$token}";
  230. $result = $this->requestApi($url, json_encode($data));
  231. return $result['user_info_list'][0];
  232. }
  233. public function fansBatchQueryInfo($data) {
  234. if (empty($data)) {
  235. return error(-1, '粉丝 openid 错误');
  236. }
  237. $token = $this->getAccessToken();
  238. if (is_error($token)) {
  239. return $token;
  240. }
  241. $list['user_list'] = array();
  242. foreach ($data as $da) {
  243. $list['user_list'][] = array('openid' => $da);
  244. }
  245. $url = "https://openapi.baidu.com/rest/2.0/cambrian/user/info?access_token={$token}";
  246. $result = $this->requestApi($url, json_encode($list));
  247. return $result['user_info_list'];
  248. }
  249. public function fansTagAdd($tagname) {
  250. if (empty($tagname)) {
  251. return error(-1, '请填写标签名称');
  252. }
  253. $token = $this->getAccessToken();
  254. if (is_error($token)) {
  255. return $token;
  256. }
  257. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/create?access_token={$token}";
  258. $data = stripslashes(ijson_encode(array('tag' => array('name' => $tagname)), JSON_UNESCAPED_UNICODE));
  259. $result = $this->requestApi($url, $data);
  260. return $result;
  261. }
  262. public function fansTagTagging($openid, $tagids) {
  263. $openid = (string) $openid;
  264. $tagids = (array) $tagids;
  265. if (empty($openid)) {
  266. return error(-1, '没有填写用户openid');
  267. }
  268. if (empty($tagids)) {
  269. return error(-1, '没有填写标签');
  270. }
  271. if (count($tagids) > 3) {
  272. return error(-1, '最多3个标签');
  273. }
  274. $token = $this->getAccessToken();
  275. if (is_error($token)) {
  276. return $token;
  277. }
  278. $fetch_result = $this->fansTagFetchOwnTags($openid);
  279. if (is_error($fetch_result)) {
  280. return $fetch_result;
  281. }
  282. foreach ($fetch_result['tagid_list'] as $del_tagid) {
  283. $this->fansTagBatchUntagging($openid, $del_tagid);
  284. }
  285. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/batchtagging?access_token={$token}";
  286. foreach ($tagids as $tagid) {
  287. $data = array(
  288. 'openid_list' => array($openid),
  289. 'tagid' => $tagid,
  290. );
  291. $data = json_encode($data);
  292. $result = $this->requestApi($url, $data);
  293. if (is_error($result)) {
  294. return $result;
  295. }
  296. }
  297. return true;
  298. }
  299. public function fansTagFetchOwnTags($openid) {
  300. $openid = (string) $openid;
  301. if (empty($openid)) {
  302. return error(-1, '没有填写用户openid');
  303. }
  304. $token = $this->getAccessToken();
  305. if (is_error($token)) {
  306. return $token;
  307. }
  308. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/getidlist?access_token={$token}";
  309. $data = json_encode(array('openid' => $openid));
  310. $result = $this->requestApi($url, $data);
  311. return $result;
  312. }
  313. public function fansTagBatchUntagging($openid_list, $tagid) {
  314. $openid_list = (array) $openid_list;
  315. $tagid = (int) $tagid;
  316. if (empty($openid_list)) {
  317. return error(-1, '缺少openid参数');
  318. }
  319. if (empty($tagid)) {
  320. return error(-1, '没有填写tagid');
  321. }
  322. $token = $this->getAccessToken();
  323. if (is_error($token)) {
  324. return $token;
  325. }
  326. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/batchuntagging?access_token={$token}";
  327. $data = array(
  328. 'openid_list' => $openid_list,
  329. 'tagid' => $tagid,
  330. );
  331. $data = json_encode($data);
  332. $result = $this->requestApi($url, $data);
  333. if (is_error($result)) {
  334. return $result;
  335. }
  336. return true;
  337. }
  338. public function fansTagBatchTagging($openid_list, $tagid) {
  339. $openid_list = (array) $openid_list;
  340. $tagid = (int) $tagid;
  341. if (empty($openid_list)) {
  342. return error(-1, '没有填写用户openid列表');
  343. }
  344. if (empty($tagid)) {
  345. return error(-1, '没有填写tagid');
  346. }
  347. $token = $this->getAccessToken();
  348. if (is_error($token)) {
  349. return $token;
  350. }
  351. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tags/batchtagging?access_token={$token}";
  352. $data = array(
  353. 'openid_list' => $openid_list,
  354. 'tagid' => $tagid,
  355. );
  356. $result = $this->requestApi($url, json_encode($data));
  357. if (is_error($result)) {
  358. return $result;
  359. }
  360. return true;
  361. }
  362. public function menuCurrentQuery() {
  363. $token = $this->getAccessToken();
  364. if (is_error($token)) {
  365. return $token;
  366. }
  367. $url = "https://openapi.baidu.com/rest/2.0/cambrian/menu/get?access_token={$token}";
  368. $res = $this->requestApi($url);
  369. return $res;
  370. }
  371. public function menuCreate($menu) {
  372. global $_W;
  373. $token = $this->getAccessToken();
  374. if (is_error($token)) {
  375. return $token;
  376. }
  377. $data['menues'] = json_encode($menu);
  378. $url = "https://openapi.baidu.com/rest/2.0/cambrian/menu/create?access_token={$token}";
  379. $res = $this->requestApi($url, $data);
  380. if (is_error($res)) {
  381. return $res;
  382. } else {
  383. return 0;
  384. }
  385. }
  386. public function menuBuild($post, $is_conditional = false) {
  387. $menu = array();
  388. foreach ($post['button'] as $button) {
  389. $temp = array();
  390. $temp['name'] = $button['name'];
  391. if (empty($button['sub_button'])) {
  392. $temp['type'] = $button['type'];
  393. if ('click' == $button['type']) {
  394. if (!empty($button['media_id']) && empty($button['key'])) {
  395. $temp['key'] = $button['media_id'];
  396. $temp['msg'] = array(
  397. 'text' => '',
  398. 'type' => 'view_limited',
  399. 'materialId' => $button['media_id'],
  400. );
  401. }
  402. if (!empty($button['key']) && $button['msg']['materialId'] == $button['key']) {
  403. $temp['msg'] = $button['msg'];
  404. $temp['key'] = $button['key'];
  405. }
  406. } elseif ('view' == $button['type']) {
  407. $temp['url'] = $button['url'];
  408. }
  409. } else {
  410. foreach ($button['sub_button'] as $sub_button) {
  411. $sub_temp = array();
  412. $sub_temp['name'] = $sub_button['name'];
  413. $sub_temp['type'] = $sub_button['type'];
  414. if ('click' == $sub_button['type']) {
  415. if (!empty($sub_button['media_id']) && empty($sub_button['key'])) {
  416. $sub_temp['key'] = $sub_button['media_id'];
  417. $sub_temp['msg'] = array(
  418. 'text' => '',
  419. 'type' => 'view_limited',
  420. 'materialId' => $sub_button['media_id'],
  421. );
  422. }
  423. if (!empty($sub_button['key']) && $sub_button['msg']['materialId'] == $sub_button['key']) {
  424. $sub_temp['msg'] = $sub_button['msg'];
  425. $sub_temp['key'] = $sub_button['key'];
  426. }
  427. } elseif ('view' == $sub_button['type']) {
  428. $sub_temp['url'] = $sub_button['url'];
  429. }
  430. $temp['sub_button'][] = $sub_temp;
  431. }
  432. }
  433. $menu['button'][] = $temp;
  434. }
  435. return $menu;
  436. }
  437. public function batchGetMaterial($type = 'news', $offset = 0, $count = 20) {
  438. global $_W;
  439. $token = $this->getAccessToken();
  440. if (is_error($token)) {
  441. return $token;
  442. }
  443. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/batchget_material?access_token={$token}&type={$type}&offset={$offset}&count={$count}";
  444. $response = $this->requestApi($url);
  445. if (!is_error($response)) {
  446. foreach ($response['item'] as $key => &$item) {
  447. foreach ($item['content']['news_item'] as $news_key => &$news_item) {
  448. $content = json_decode($news_item['content'], true);
  449. if (!empty($content) && is_array($content) && !empty($content['orihtml'])) {
  450. $news_item['content'] = $content['orihtml'];
  451. }
  452. $news_info = $this->getMaterial($news_item['thumb_media_id']);
  453. $news_item['thumb_url'] = $news_info['url'];
  454. }
  455. }
  456. }
  457. return $response;
  458. }
  459. public function delMaterial($media_id) {
  460. $media_id = trim($media_id);
  461. if (empty($media_id)) {
  462. return error(-1, '素材media_id错误');
  463. }
  464. $token = $this->getAccessToken();
  465. if (is_error($token)) {
  466. return $token;
  467. }
  468. $url = 'https://openapi.baidu.com/rest/2.0/cambrian/material/del_material?access_token=' . $token . '&media_id=' . $media_id;
  469. $response = $this->requestApi($url);
  470. return $response;
  471. }
  472. public function addMatrialNews($data) {
  473. $token = $this->getAccessToken();
  474. if (is_error($token)) {
  475. return $token;
  476. }
  477. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/add_news?access_token={$token}";
  478. $data = stripslashes(urldecode(ijson_encode($data, JSON_UNESCAPED_UNICODE)));
  479. $response = $this->requestApi($url, $data);
  480. return $response['media_id'];
  481. }
  482. public function editMaterialNews($data) {
  483. $token = $this->getAccessToken();
  484. if (is_error($token)) {
  485. return $token;
  486. }
  487. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/update_news?access_token={$token}";
  488. $response = $this->requestApi($url, stripslashes(ijson_encode($data, JSON_UNESCAPED_UNICODE)));
  489. return $response;
  490. }
  491. public function getMaterial($media_id) {
  492. $token = $this->getAccessToken();
  493. if (is_error($token)) {
  494. return $token;
  495. }
  496. $url = "https://openapi.baidu.com/rest/2.0/cambrian/material/get_material?access_token={$token}&media_id={$media_id}";
  497. $response = $this->requestApi($url);
  498. return $response;
  499. }
  500. public function uploadNewsThumb($thumb) {
  501. $token = $this->getAccessToken();
  502. if (is_error($token)) {
  503. return $token;
  504. }
  505. if (!file_exists($thumb)) {
  506. return error(1, '文件不存在');
  507. }
  508. $data = array(
  509. 'media' => '@' . $thumb,
  510. );
  511. $url = "https://openapi.baidu.com/rest/2.0/cambrian/media/uploadimg?access_token={$token}";
  512. $response = $this->requestApi($url, $data);
  513. return $response['url'];
  514. }
  515. public function uploadMediaFixed($path, $type = 'images') {
  516. if (empty($path)) {
  517. return error(-1, '参数错误');
  518. }
  519. if (in_array(substr(ltrim($path, '/'), 0, 6), array('images', 'videos', 'audios', 'thumb', 'voices'))) {
  520. $path = ATTACHMENT_ROOT . ltrim($path, '/');
  521. }
  522. if (!file_exists($path)) {
  523. return error(1, '文件不存在');
  524. }
  525. $token = $this->getAccessToken();
  526. if (is_error($token)) {
  527. return $token;
  528. }
  529. $data = array(
  530. 'media' => '@' . $path,
  531. );
  532. $url = "https://openapi.baidu.com/rest/2.0/cambrian/media/add_material?access_token={$token}";
  533. $response = $this->requestApi($url, $data);
  534. return $response;
  535. }
  536. public function sendCustomNotice($data) {
  537. if (empty($data)) {
  538. return error(-1, '参数错误');
  539. }
  540. $token = $this->getAccessToken();
  541. if (is_error($token)) {
  542. return $token;
  543. }
  544. $url = "https://openapi.baidu.com/rest/2.0/cambrian/message/custom_send?access_token={$token}";
  545. $response = $this->requestApi($url, urldecode(json_encode($data)));
  546. WeUtility::logging('$resonse', var_export($response, true));
  547. if (is_error($response)) {
  548. return $response;
  549. }
  550. return true;
  551. }
  552. public function sendTplNotice($touser, $template_id, $postdata, $url = '') {
  553. if (empty($touser)) {
  554. return error(-1, '参数错误,粉丝openid不能为空');
  555. }
  556. if (empty($template_id)) {
  557. return error(-1, '参数错误,模板标示不能为空');
  558. }
  559. if (empty($postdata) || !is_array($postdata)) {
  560. return error(-1, '参数错误,请根据模板规则完善消息内容');
  561. }
  562. $token = $this->getAccessToken();
  563. if (is_error($token)) {
  564. return $token;
  565. }
  566. $data = array();
  567. $data['touser'] = $touser;
  568. $data['template_id'] = trim($template_id);
  569. $data['url'] = trim($url);
  570. $data['data'] = $postdata;
  571. $data = json_encode($data);
  572. $post_url = "https://openapi.baidu.com/rest/2.0/cambrian/template/send?access_token={$token}";
  573. $response = $this->requestApi($post_url, $data);
  574. if (is_error($response)) {
  575. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  576. }
  577. return true;
  578. }
  579. public function fansSendAll($group, $msgtype, $media_id) {
  580. $types = array('basic' => 'text', 'image' => 'image', 'news' => 'mpnews', 'voice' => 'voice');
  581. if (empty($types[$msgtype])) {
  582. return error(-1, '消息类型不合法');
  583. }
  584. $send_conent = ('text' == $types[$msgtype]) ? array('content' => $media_id) : array('media_id' => $media_id);
  585. if ($group == -1) {
  586. $data = array(
  587. 'filter' => array(
  588. 'is_to_all' => true,
  589. 'group_id' => $group,
  590. ),
  591. 'msgtype' => $types[$msgtype],
  592. $types[$msgtype] => $send_conent,
  593. );
  594. } else {
  595. $openids = $this->getFansByTag($group);
  596. $data = array(
  597. 'touser' => $openids,
  598. 'msgtype' => $types[$msgtype],
  599. $types[$msgtype] => $send_conent,
  600. );
  601. }
  602. $token = $this->getAccessToken();
  603. if (is_error($token)) {
  604. return $token;
  605. }
  606. $url = "https://openapi.baidu.com/rest/2.0/cambrian/message/sendall?access_token={$token}";
  607. $response = $this->requestApi($url, json_encode($data));
  608. return $response;
  609. }
  610. public function getFansByTag($tagid) {
  611. $token = $this->getAccessToken();
  612. if (is_error($token)) {
  613. return $token;
  614. }
  615. $url = "https://openapi.baidu.com/rest/2.0/cambrian/tag/get?access_token={$token}";
  616. $data = array('tagid' => $tagid);
  617. $response = $this->requestApi($url, json_encode($data));
  618. return $response['data']['openid'];
  619. }
  620. public function getJsApiTicket() {
  621. $cachekey = cache_system_key('jsticket', array('uniacid' => $this->account['uniacid']));
  622. $cache = cache_load($cachekey);
  623. if (!empty($cache) && !empty($cache['ticket']) && $cache['expire'] > TIMESTAMP) {
  624. return $cache['ticket'];
  625. }
  626. $access_token = $this->getAccessToken();
  627. if (is_error($access_token)) {
  628. return $access_token;
  629. }
  630. $url = "https://openapi.baidu.com/rest/2.0/cambrian/jssdk/getticket?access_token={$access_token}";
  631. $response = $this->requestApi($url);
  632. if (is_error($response)) {
  633. return $response;
  634. }
  635. $record = array();
  636. $record['ticket'] = $response['ticket'];
  637. $record['expire'] = TIMESTAMP + $response['expires_in'] - 200;
  638. $this->account['jsapi_ticket'] = $record;
  639. cache_write($cachekey, $record);
  640. return $record['ticket'];
  641. }
  642. public function getJssdkConfig($url = '') {
  643. global $_W;
  644. $jsapiTicket = $this->getJsApiTicket();
  645. if (is_error($jsapiTicket)) {
  646. $jsapiTicket = $jsapiTicket['message'];
  647. }
  648. $nonceStr = random(25);
  649. $timestamp = TIMESTAMP;
  650. $url = empty($url) ? $_W['siteurl'] : $url;
  651. $arr = array(
  652. 'jsapi_ticket' => $jsapiTicket,
  653. 'nonce_str' => $nonceStr,
  654. 'timestamp' => $timestamp,
  655. 'url' => urlencode($url),
  656. );
  657. ksort($arr);
  658. $string1 = http_build_query($arr);
  659. $signature = sha1($string1);
  660. $config = array(
  661. 'appId' => $this->account['original'],
  662. 'nonceStr' => $nonceStr,
  663. 'timestamp' => "$timestamp",
  664. 'signature' => $signature,
  665. 'url' => urlencode($url),
  666. );
  667. return $config;
  668. }
  669. public function getMaterialSupport() {
  670. return array(
  671. 'mass' => array('news' => false, 'image' => false, 'voice' => false, 'basic' => false),
  672. 'chats' => array('basic' => false, 'news' => false, 'image' => false, 'music' => true, 'voice' => false, 'video' => true),
  673. );
  674. }
  675. }