人人商城

xzapp.account.class.php 23KB

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