人人商城

weixin.account.class.php 55KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. class WeiXinAccount extends WeAccount {
  8. public $tablename = 'account_wechats';
  9. public $types = array(
  10. 'view', 'click', 'scancode_push',
  11. 'scancode_waitmsg', 'pic_sysphoto', 'pic_photo_or_album',
  12. 'pic_weixin', 'location_select', 'media_id', 'view_limited'
  13. );
  14. public function __construct($account = array()) {
  15. $this->menuFrame = 'account';
  16. $this->type = ACCOUNT_TYPE_OFFCIAL_NORMAL;
  17. $this->typeName = '公众号';
  18. $this->typeSign = ACCOUNT_TYPE_SIGN;
  19. }
  20. public function accountDisplayUrl() {
  21. return url('account/display');
  22. }
  23. public function fetchAccountInfo() {
  24. $account_table = table('account');
  25. $account = $account_table->getWechatappAccount($this->uniaccount['acid']);
  26. $account['encrypt_key'] = $account['key'];
  27. return $account;
  28. }
  29. public function checkSign() {
  30. $token = $this->account['token'];
  31. $signkey = array($token, $_GET['timestamp'], $_GET['nonce']);
  32. sort($signkey, SORT_STRING);
  33. $signString = implode($signkey);
  34. $signString = sha1($signString);
  35. return $signString == $_GET['signature'];
  36. }
  37. public function checkSignature($encrypt_msg) {
  38. $str = $this->buildSignature($encrypt_msg);
  39. return $str == $_GET['msg_signature'];
  40. }
  41. public function checkIntoManage() {
  42. if (empty($this->uniaccount) || (!empty($this->uniaccount['account']) && !in_array($this->uniaccount['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH)) && !defined('IN_MODULE'))) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. public function local_checkSignature($packet) {
  48. $token = $this->account['token'];
  49. $array = array($packet['Encrypt'], $token, $packet['TimeStamp'], $packet['Nonce']);
  50. sort($array, SORT_STRING);
  51. $str = implode($array);
  52. $str = sha1($str);
  53. return $str == $packet['MsgSignature'];
  54. }
  55. public function local_decryptMsg($postData) {
  56. $token = $this->account['token'];
  57. $encodingaeskey = $this->account['encodingaeskey'];
  58. $appid = $this->account['encrypt_key'];
  59. if(strlen($encodingaeskey) != 43) {
  60. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40004 \n,错误描述为: " . $this->encryptErrorCode('40004'));
  61. }
  62. $key = base64_decode($encodingaeskey . '=');
  63. $packet = $this->local_xmlExtract($postData);
  64. if(is_error($packet)) {
  65. return error(-1, $packet['message']);
  66. }
  67. $istrue = $this->local_checkSignature($packet);
  68. if(!$istrue) {
  69. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40001 \n,错误描述为: " . $this->encryptErrorCode('40001'));
  70. }
  71. $ciphertext_dec = base64_decode($packet['Encrypt']);
  72. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  73. $iv = substr($key, 0, 16);
  74. mcrypt_generic_init($module, $key, $iv);
  75. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  76. mcrypt_generic_deinit($module);
  77. mcrypt_module_close($module);
  78. $block_size = 32;
  79. $pad = ord(substr($decrypted, -1));
  80. if ($pad < 1 || $pad > 32) {
  81. $pad = 0;
  82. }
  83. $result = substr($decrypted, 0, (strlen($decrypted) - $pad));
  84. if (strlen($result) < 16) {
  85. return '';
  86. }
  87. $content = substr($result, 16, strlen($result));
  88. $len_list = unpack("N", substr($content, 0, 4));
  89. $xml_len = $len_list[1];
  90. $xml_content = substr($content, 4, $xml_len);
  91. $from_appid = substr($content, $xml_len + 4);
  92. if ($from_appid != $appid) {
  93. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40005 \n,错误描述为: " . $this->encryptErrorCode('40005'));
  94. }
  95. return $xml_content;
  96. }
  97. public function buildSignature($encrypt_msg) {
  98. $token = $this->account['token'];
  99. $array = array($encrypt_msg, $token, $_GET['timestamp'], $_GET['nonce']);
  100. sort($array, SORT_STRING);
  101. $str = implode($array);
  102. $str = sha1($str);
  103. return $str;
  104. }
  105. public function encryptMsg($text) {
  106. $token = $this->account['token'];
  107. $encodingaeskey = $this->account['encodingaeskey'];
  108. $appid = $this->account['encrypt_key'];
  109. $key = base64_decode($encodingaeskey . '=');
  110. $text = random(16) . pack("N", strlen($text)) . $text . $appid;
  111. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  112. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  113. $iv = substr($key, 0, 16);
  114. $block_size = 32;
  115. $text_length = strlen($text);
  116. $amount_to_pad = $block_size - ($text_length % $block_size);
  117. if ($amount_to_pad == 0) {
  118. $amount_to_pad = $block_size;
  119. }
  120. $pad_chr = chr($amount_to_pad);
  121. $tmp = '';
  122. for ($index = 0; $index < $amount_to_pad; $index++) {
  123. $tmp .= $pad_chr;
  124. }
  125. $text = $text . $tmp;
  126. mcrypt_generic_init($module, $key, $iv);
  127. $encrypted = mcrypt_generic($module, $text);
  128. mcrypt_generic_deinit($module);
  129. mcrypt_module_close($module);
  130. $encrypt_msg = base64_encode($encrypted);
  131. $signature = $this->buildSignature($encrypt_msg);
  132. return array($signature, $encrypt_msg);
  133. }
  134. public function decryptMsg($postData) {
  135. $token = $this->account['token'];
  136. $encodingaeskey = $this->account['encodingaeskey'];
  137. $appid = $this->account['encrypt_key'];
  138. $key = base64_decode($encodingaeskey . '=');
  139. if(strlen($encodingaeskey) != 43) {
  140. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40004 \n,错误描述为: " . $this->encryptErrorCode('40004'));
  141. }
  142. $packet = $this->xmlExtract($postData);
  143. if(is_error($packet)) {
  144. return error(-1, $packet['message']);
  145. }
  146. $istrue = $this->checkSignature($packet['encrypt']);
  147. if(!$istrue) {
  148. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40001 \n,错误描述为: " . $this->encryptErrorCode('40001'));
  149. }
  150. $ciphertext_dec = base64_decode($packet['encrypt']);
  151. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  152. $iv = substr($key, 0, 16);
  153. mcrypt_generic_init($module, $key, $iv);
  154. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  155. mcrypt_generic_deinit($module);
  156. mcrypt_module_close($module);
  157. $block_size = 32;
  158. $pad = ord(substr($decrypted, -1));
  159. if ($pad < 1 || $pad > 32) {
  160. $pad = 0;
  161. }
  162. $result = substr($decrypted, 0, (strlen($decrypted) - $pad));
  163. if (strlen($result) < 16) {
  164. return '';
  165. }
  166. $content = substr($result, 16, strlen($result));
  167. $len_list = unpack("N", substr($content, 0, 4));
  168. $xml_len = $len_list[1];
  169. $xml_content = substr($content, 4, $xml_len);
  170. $from_appid = substr($content, $xml_len + 4);
  171. if ($from_appid != $appid) {
  172. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40005 \n,错误描述为: " . $this->encryptErrorCode('40005'));
  173. }
  174. return $xml_content;
  175. }
  176. function xmlDetract($data) {
  177. $xml['Encrypt'] = $data[1];
  178. $xml['MsgSignature'] = $data[0];
  179. $xml['TimeStamp'] = $_GET['timestamp'];
  180. $xml['Nonce'] = $_GET['nonce'];
  181. return array2xml($xml);
  182. }
  183. public function xmlExtract($message) {
  184. $packet = array();
  185. if (!empty($message)){
  186. $obj = isimplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA);
  187. if($obj instanceof SimpleXMLElement) {
  188. $packet['encrypt'] = strval($obj->Encrypt);
  189. $packet['to'] = strval($obj->ToUserName);
  190. }
  191. }
  192. if(!empty($packet['encrypt'])) {
  193. return $packet;
  194. } else {
  195. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40002 \n,错误描述为: " . $this->encryptErrorCode('40002'));
  196. }
  197. }
  198. public function local_xmlExtract($message) {
  199. $packet = array();
  200. if (!empty($message)){
  201. $obj = isimplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA);
  202. if($obj instanceof SimpleXMLElement) {
  203. $packet['Encrypt'] = strval($obj->Encrypt);
  204. $packet['MsgSignature'] = strval($obj->MsgSignature);
  205. $packet['TimeStamp'] = strval($obj->TimeStamp);
  206. $packet['Nonce'] = strval($obj->Nonce);
  207. }
  208. }
  209. if(!empty($packet)) {
  210. return $packet;
  211. } else {
  212. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40002 \n,错误描述为: " . $this->encryptErrorCode('40002'));
  213. }
  214. }
  215. public function queryAvailableMessages() {
  216. $messages = array('text', 'image', 'voice', 'video', 'location', 'link', 'subscribe', 'unsubscribe');
  217. if(!empty($this->account['key']) && !empty($this->account['secret'])) {
  218. $level = intval($this->account['level']);
  219. if($level > 1){
  220. $messages[] = 'click';
  221. $messages[] = 'view';
  222. }
  223. if ($level > 2) {
  224. $messages[] = 'qr';
  225. $messages[] = 'trace';
  226. }
  227. }
  228. return $messages;
  229. }
  230. public function queryAvailablePackets() {
  231. $packets = array('text', 'music', 'news');
  232. if(!empty($this->account['key']) && !empty($this->account['secret'])) {
  233. if (intval($this->account['level']) > 1) {
  234. $packets[] = 'image';
  235. $packets[] = 'voice';
  236. $packets[] = 'video';
  237. }
  238. }
  239. return $packets;
  240. }
  241. public function isMenuSupported() {
  242. return !empty($this->account['key']) &&
  243. !empty($this->account['secret']) &&
  244. (intval($this->account['level']) > 1);
  245. }
  246. public function menuCreate($menu) {
  247. global $_W;
  248. $token = $this->getAccessToken();
  249. if(is_error($token)){
  250. return $token;
  251. }
  252. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$token}";
  253. if(!empty($menu['matchrule'])) {
  254. $url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token={$token}";
  255. }
  256. $data = urldecode(json_encode($menu));
  257. $response = ihttp_post($url, $data);
  258. if(is_error($response)) {
  259. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  260. }
  261. $result = @json_decode($response['content'], true);
  262. if(!empty($result['errcode'])) {
  263. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  264. }
  265. return $result['menuid'];
  266. }
  267. public function menuBuild($data_array, $is_conditional = false) {
  268. $menu = array();
  269. if (empty($data_array) || empty($data_array['button']) || !is_array($data_array)) {
  270. return $menu;
  271. }
  272. foreach ($data_array['button'] as $button) {
  273. $temp = array();
  274. $temp['name'] = preg_replace_callback('/\:\:([0-9a-zA-Z_-]+)\:\:/', create_function('$matches', 'return utf8_bytes(hexdec($matches[1]));'), $button['name']);
  275. $temp['name'] = urlencode($temp['name']);
  276. if (empty($button['sub_button'])) {
  277. $temp['type'] = $button['type'];
  278. if ($button['type'] == 'view') {
  279. $temp['url'] = urlencode($button['url']);
  280. } elseif ($button['type'] == 'click') {
  281. if (!empty($button['media_id']) && empty($button['key'])) {
  282. $temp['media_id'] = urlencode($button['media_id']);
  283. $temp['type'] = 'media_id';
  284. } elseif (empty($button['media_id']) && !empty($button['key'])) {
  285. $temp['type'] = 'click';
  286. $temp['key'] = urlencode($button['key']);
  287. }
  288. } elseif ($button['type'] == 'media_id' || $button['type'] == 'view_limited') {
  289. $temp['media_id'] = urlencode($button['media_id']);
  290. } elseif ($button['type'] == 'miniprogram') {
  291. $temp['appid'] = trim($button['appid']);
  292. $temp['pagepath'] = urlencode($button['pagepath']);
  293. $temp['url'] = urlencode($button['url']);
  294. } else {
  295. $temp['key'] = urlencode($button['key']);
  296. }
  297. } else {
  298. foreach ($button['sub_button'] as $sub_button) {
  299. $sub_temp = array();
  300. $sub_temp['name'] = preg_replace_callback('/\:\:([0-9a-zA-Z_-]+)\:\:/', create_function('$matches', 'return utf8_bytes(hexdec($matches[1]));'), $sub_button['name']);
  301. $sub_temp['name'] = urlencode($sub_temp['name']);
  302. $sub_temp['type'] = $sub_button['type'];
  303. if ($sub_button['type'] == 'view') {
  304. $sub_temp['url'] = urlencode($sub_button['url']);
  305. } elseif ($sub_button['type'] == 'click') {
  306. if (!empty($sub_button['media_id']) && empty($sub_button['key'])) {
  307. $sub_temp['media_id'] = urlencode($sub_button['media_id']);
  308. $sub_temp['type'] = 'media_id';
  309. } elseif (empty($sub_button['media_id']) && !empty($sub_button['key'])) {
  310. $sub_temp['type'] = 'click';
  311. $sub_temp['key'] = urlencode($sub_button['key']);
  312. }
  313. } elseif ($sub_button['type'] == 'media_id' || $sub_button['type'] == 'view_limited') {
  314. $sub_temp['media_id'] = urlencode($sub_button['media_id']);
  315. } elseif ($sub_button['type'] == 'miniprogram') {
  316. $sub_temp['appid'] = trim($sub_button['appid']);
  317. $sub_temp['pagepath'] = urlencode($sub_button['pagepath']);
  318. $sub_temp['url'] = urlencode($sub_button['url']);
  319. } else {
  320. $sub_temp['key'] = urlencode($sub_button['key']);
  321. }
  322. $temp['sub_button'][] = $sub_temp;
  323. }
  324. }
  325. $menu['button'][] = $temp;
  326. }
  327. if (empty($is_conditional) || empty($data_array['matchrule']) || !is_array($data_array['matchrule'])) {
  328. return $menu;
  329. }
  330. if($data_array['matchrule']['sex'] > 0) {
  331. $menu['matchrule']['sex'] = $data_array['matchrule']['sex'];
  332. }
  333. if($data_array['matchrule']['group_id'] != -1) {
  334. $menu['matchrule']['tag_id'] = $data_array['matchrule']['group_id'];
  335. }
  336. if($data_array['matchrule']['client_platform_type'] > 0) {
  337. $menu['matchrule']['client_platform_type'] = $data_array['matchrule']['client_platform_type'];
  338. }
  339. if(!empty($data_array['matchrule']['province'])) {
  340. $menu['matchrule']['country'] = urlencode('中国');
  341. $menu['matchrule']['province'] = urlencode($data_array['matchrule']['province']);
  342. if(!empty($data_array['matchrule']['city'])) {
  343. $menu['matchrule']['city'] = urlencode($data_array['matchrule']['city']);
  344. }
  345. }
  346. if(!empty($data_array['matchrule']['language'])) {
  347. $inarray = 0;
  348. $languages = menu_languages();
  349. foreach ($languages as $key => $value) {
  350. if(in_array($data_array['matchrule']['language'], $value, true)) {
  351. $inarray = 1;
  352. break;
  353. }
  354. }
  355. if($inarray === 1) {
  356. $menu['matchrule']['language'] = $data_array['matchrule']['language'];
  357. }
  358. }
  359. return $menu;
  360. }
  361. public function menuDelete($menuid = 0) {
  362. $token = $this->getAccessToken();
  363. if(is_error($token)){
  364. return $token;
  365. }
  366. if($menuid > 0) {
  367. $url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token={$token}";
  368. $data = array(
  369. 'menuid' => $menuid
  370. );
  371. $response = ihttp_post($url, json_encode($data));
  372. } else {
  373. $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={$token}";
  374. $response = ihttp_get($url);
  375. }
  376. if(is_error($response)) {
  377. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  378. }
  379. $result = @json_decode($response['content'], true);
  380. if(!empty($result['errcode'])) {
  381. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  382. }
  383. return true;
  384. }
  385. public function menuModify($menu) {
  386. return $this->menuCreate($menu);
  387. }
  388. public function menuCurrentQuery() {
  389. $token = $this->getAccessToken();
  390. if(is_error($token)){
  391. return $token;
  392. }
  393. $url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={$token}";
  394. $result = $this->requestApi($url);
  395. return $result;
  396. }
  397. public function menuQuery() {
  398. $token = $this->getAccessToken();
  399. if(is_error($token)){
  400. return $token;
  401. }
  402. $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={$token}";
  403. $response = ihttp_get($url);
  404. if(is_error($response)) {
  405. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  406. }
  407. $result = @json_decode($response['content'], true);
  408. if(!empty($result['errcode']) && $result['errcode'] != '46003') {
  409. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  410. }
  411. return $result;
  412. }
  413. public function fansQueryInfo($uniid, $isOpen = true) {
  414. if($isOpen) {
  415. $openid = $uniid;
  416. } else {
  417. exit('error');
  418. }
  419. $token = $this->getAccessToken();
  420. if(is_error($token)){
  421. return $token;
  422. }
  423. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$openid}&lang=zh_CN";
  424. $response = ihttp_get($url);
  425. if(is_error($response)) {
  426. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  427. }
  428. preg_match('/city":"(.*)","province":"(.*)","country":"(.*)"/U', $response['content'], $reg_arr);
  429. $city = htmlentities(bin2hex($reg_arr[1]));
  430. $province = htmlentities(bin2hex($reg_arr[2]));
  431. $country = htmlentities(bin2hex($reg_arr[3]));
  432. $response['content'] = str_replace('"city":"'.$reg_arr[1].'","province":"'.$reg_arr[2].'","country":"'.$reg_arr[3].'"', '"city":"'.$city.'","province":"'.$province.'","country":"'.$country.'"', $response['content']);
  433. $result = @json_decode($response['content'], true);
  434. $result['city'] = hex2bin(html_entity_decode($result['city']));
  435. $result['province'] = hex2bin(html_entity_decode($result['province']));
  436. $result['country'] = hex2bin(html_entity_decode($result['country']));
  437. unset($result['remark'], $result['subscribe_scene'], $result['qr_scene'], $result['qr_scene_str']);
  438. if(empty($result)) {
  439. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  440. } elseif(!empty($result['errcode'])) {
  441. return error($result['errcode'], "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  442. }
  443. return $result;
  444. }
  445. public function fansBatchQueryInfo($data) {
  446. if(empty($data)) {
  447. return error(-1, '粉丝openid错误');
  448. }
  449. foreach($data as $da) {
  450. $post[] = array(
  451. 'openid' => trim($da),
  452. 'lang' => 'zh-CN'
  453. );
  454. }
  455. $data = array();
  456. $data['user_list'] = $post;
  457. $token = $this->getAccessToken();
  458. if(is_error($token)){
  459. return $token;
  460. }
  461. $url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={$token}";
  462. $response = ihttp_post($url, json_encode($data));
  463. if(is_error($response)) {
  464. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  465. }
  466. $result = @json_decode($response['content'], true);
  467. if(empty($result)) {
  468. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  469. } elseif(!empty($result['errcode'])) {
  470. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  471. }
  472. return $result['user_info_list'];
  473. }
  474. public function fansAll($startopenid = '') {
  475. global $_GPC;
  476. $token = $this->getAccessToken();
  477. if(is_error($token)){
  478. return $token;
  479. }
  480. $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . $token;
  481. if(!empty($_GPC['next_openid'])) {
  482. $startopenid = $_GPC['next_openid'];
  483. }
  484. if (!empty($startopenid)) {
  485. $url .= '&next_openid=' . $startopenid;
  486. }
  487. $response = ihttp_get($url);
  488. if(is_error($response)) {
  489. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  490. }
  491. $result = @json_decode($response['content'], true);
  492. if(empty($result)) {
  493. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  494. } elseif(!empty($result['errcode'])) {
  495. return error(-1, "访问公众平台接口失败, 错误: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  496. }
  497. $return = array();
  498. $return['total'] = $result['total'];
  499. $return['fans'] = $result['data']['openid'];
  500. $return['next'] = $result['next_openid'];
  501. return $return;
  502. }
  503. public function queryBarCodeActions() {
  504. return array('barCodeCreateDisposable', 'barCodeCreateFixed');
  505. }
  506. public function barCodeCreateDisposable($barcode) {
  507. $barcode['expire_seconds'] = empty($barcode['expire_seconds']) ? 2592000 : $barcode['expire_seconds'];
  508. if (empty($barcode['action_info']['scene']['scene_id']) || empty($barcode['action_name'])) {
  509. return error('1', 'Invalid params');
  510. }
  511. $token = $this->getAccessToken();
  512. $response = ihttp_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$token, json_encode($barcode));
  513. if (is_error($response)) {
  514. return $response;
  515. }
  516. $content = @json_decode($response['content'], true);
  517. if(empty($content)) {
  518. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  519. }
  520. if (!empty($content['errcode'])) {
  521. return error(-1, "访问微信接口错误, 错误代码: {$content['errcode']}, 错误信息: {$content['errmsg']},错误详情:{$this->errorCode($content['errcode'])}");
  522. }
  523. return $content;
  524. }
  525. public function barCodeCreateFixed($barcode) {
  526. if($barcode['action_name'] == 'QR_LIMIT_SCENE' && empty($barcode['action_info']['scene']['scene_id'])) {
  527. return error('1', '场景值错误');
  528. }
  529. if($barcode['action_name'] == 'QR_LIMIT_STR_SCENE' && empty($barcode['action_info']['scene']['scene_str'])) {
  530. return error('1', '场景字符串错误');
  531. }
  532. $token = $this->getAccessToken();
  533. $response = ihttp_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$token, json_encode($barcode));
  534. if (is_error($response)) {
  535. return $response;
  536. }
  537. $content = @json_decode($response['content'], true);
  538. if(empty($content)) {
  539. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  540. }
  541. if(!empty($content['errcode'])) {
  542. return error(-1, "访问微信接口错误, 错误代码: {$content['errcode']}, 错误信息: {$content['errmsg']},错误详情:{$this->errorCode($content['errcode'])}");
  543. }
  544. return $content;
  545. }
  546. private function encryptErrorCode($code) {
  547. $errors = array(
  548. '40001' => '签名验证错误',
  549. '40002' => 'xml解析失败',
  550. '40003' => 'sha加密生成签名失败',
  551. '40004' => 'encodingAesKey 非法',
  552. '40005' => 'appid 校验错误',
  553. '40006' => 'aes 加密失败',
  554. '40007' => 'aes 解密失败',
  555. '40008' => '解密后得到的buffer非法',
  556. '40009' => 'base64加密失败',
  557. '40010' => 'base64解密失败',
  558. '40011' => '生成xml失败',
  559. );
  560. if($errors[$code]) {
  561. return $errors[$code];
  562. } else {
  563. return '未知错误';
  564. }
  565. }
  566. public function changeSend($send) {
  567. if (empty($send)) {
  568. return error(-1, 'Invalid params');
  569. }
  570. $token = $this->getAccessToken();
  571. if(is_error($token)){
  572. return $token;
  573. }
  574. $sendapi = 'https://api.weixin.qq.com/pay/delivernotify?access_token='.$token;
  575. $response = ihttp_request($sendapi, json_encode($send));
  576. $response = json_decode($response['content'], true);
  577. if (empty($response)) {
  578. return error(-1, '发货失败,请检查您的公众号权限或是公众号AppId和公众号AppSecret!');
  579. }
  580. if (!empty($response['errcode'])) {
  581. return error(-1, $response['errmsg']);
  582. }
  583. return true;
  584. }
  585. public function getAccessToken() {
  586. $cachekey = cache_system_key('accesstoken', array('acid' => $this->account['acid']));
  587. $cache = cache_load($cachekey);
  588. if (!empty($cache) && !empty($cache['token']) && $cache['expire'] > TIMESTAMP) {
  589. $this->account['access_token'] = $cache;
  590. return $cache['token'];
  591. }
  592. if (empty($this->account['key']) || empty($this->account['secret'])) {
  593. return error('-1', '未填写公众号的 appid 或 appsecret!');
  594. }
  595. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->account['key']}&secret={$this->account['secret']}";
  596. $content = ihttp_get($url);
  597. if(is_error($content)) {
  598. return error('-1', '获取微信公众号授权失败, 请稍后重试!错误详情: ' . $content['message']);
  599. }
  600. if (empty($content['content'])) {
  601. return error('-1', 'AccessToken获取失败,请检查appid和appsecret的值是否与微信公众平台一致!');
  602. }
  603. $token = @json_decode($content['content'], true);
  604. if(empty($token) || !is_array($token) || empty($token['access_token']) || empty($token['expires_in'])) {
  605. $errorinfo = substr($content['meta'], strpos($content['meta'], '{'));
  606. $errorinfo = @json_decode($errorinfo, true);
  607. return error('-1', '获取微信公众号授权失败, 请稍后重试! 公众平台返回原始数据为: 错误代码-' . $errorinfo['errcode'] . ',错误信息-' . $errorinfo['errmsg']);
  608. }
  609. $record = array();
  610. $record['token'] = $token['access_token'];
  611. $record['expire'] = TIMESTAMP + $token['expires_in'] - 200;
  612. $this->account['access_token'] = $record;
  613. cache_write($cachekey, $record);
  614. return $record['token'];
  615. }
  616. public function getVailableAccessToken() {
  617. $accounts = pdo_fetchall("SELECT `key`, `secret`, `acid` FROM ".tablename('account_wechats')." WHERE uniacid = :uniacid ORDER BY `level` DESC ", array(':uniacid' => $GLOBALS['_W']['uniacid']));
  618. if (empty($accounts)) {
  619. return error(-1, 'no permission');
  620. }
  621. foreach ($accounts as $account) {
  622. if (empty($account['key']) || empty($account['secret'])) {
  623. continue;
  624. }
  625. $acid = $account['acid'];
  626. break;
  627. }
  628. $account = WeAccount::create($acid);
  629. return $account->getAccessToken();
  630. }
  631. public function fetch_token() {
  632. return $this->getAccessToken();
  633. }
  634. public function fetch_available_token() {
  635. return $this->getVailableAccessToken();
  636. }
  637. public function clearAccessToken() {
  638. $access_token = $this->getAccessToken();
  639. if(is_error($access_token)){
  640. return $access_token;
  641. }
  642. $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=' . $access_token;
  643. $response = $this->requestApi($url);
  644. if (is_error($response) && $response['errno'] == '40001') {
  645. cache_delete(cache_system_key('accesstoken', array('acid' => $this->account['acid'])));
  646. }
  647. return true;
  648. }
  649. public function getJsApiTicket(){
  650. $cachekey = cache_system_key('jsticket', array('acid' => $this->account['acid']));
  651. $cache = cache_load($cachekey);
  652. if(!empty($cache) && !empty($cache['ticket']) && $cache['expire'] > TIMESTAMP) {
  653. return $cache['ticket'];
  654. }
  655. $access_token = $this->getAccessToken();
  656. if(is_error($access_token)){
  657. return $access_token;
  658. }
  659. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi";
  660. $content = ihttp_get($url);
  661. if(is_error($content)) {
  662. return error(-1, '调用接口获取微信公众号 jsapi_ticket 失败, 错误信息: ' . $content['message']);
  663. }
  664. $result = @json_decode($content['content'], true);
  665. if(empty($result) || intval(($result['errcode'])) != 0 || $result['errmsg'] != 'ok') {
  666. return error(-1, '获取微信公众号 jsapi_ticket 结果错误, 错误信息: ' . $result['errmsg']);
  667. }
  668. $record = array();
  669. $record['ticket'] = $result['ticket'];
  670. $record['expire'] = TIMESTAMP + $result['expires_in'] - 200;
  671. $this->account['jsapi_ticket'] = $record;
  672. cache_write($cachekey, $record);
  673. return $record['ticket'];
  674. }
  675. public function getJssdkConfig($url = ''){
  676. global $_W;
  677. $jsapiTicket = $this->getJsApiTicket();
  678. if(is_error($jsapiTicket)){
  679. $jsapiTicket = $jsapiTicket['message'];
  680. }
  681. $nonceStr = random(16);
  682. $timestamp = TIMESTAMP;
  683. $url = empty($url) ? $_W['siteurl'] : $url;
  684. $string1 = "jsapi_ticket={$jsapiTicket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";
  685. $signature = sha1($string1);
  686. $config = array(
  687. "appId" => $this->account['key'],
  688. "nonceStr" => $nonceStr,
  689. "timestamp" => "$timestamp",
  690. "signature" => $signature,
  691. );
  692. if(DEVELOPMENT) {
  693. $config['url'] = $url;
  694. $config['string1'] = $string1;
  695. $config['name'] = $this->account['name'];
  696. }
  697. return $config;
  698. }
  699. public function long2short($longurl) {
  700. $token = $this->getAccessToken();
  701. if(is_error($token)){
  702. return $token;
  703. }
  704. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token={$token}";
  705. $send = array();
  706. $send['action'] = 'long2short';
  707. $send['long_url'] = $longurl;
  708. $response = ihttp_request($url, json_encode($send));
  709. if(is_error($response)) {
  710. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  711. }
  712. $result = @json_decode($response['content'], true);
  713. if(empty($result)) {
  714. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  715. } elseif(!empty($result['errcode'])) {
  716. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  717. }
  718. return $result;
  719. }
  720. public function fetchChatLog($params = array()) {
  721. if(empty($params['starttime']) || empty($params['endtime'])) {
  722. return error(-1, '没有要查询的时间段');
  723. }
  724. $starttmp = date('Y-m-d', $params['starttime']);
  725. $endtmp = date('Y-m-d', $params['endtime']);
  726. if($starttmp != $endtmp) {
  727. return error(-1, '时间范围有误,微信公众平台不支持跨日查询');
  728. }
  729. if(empty($params['openid'])) {
  730. return error(-1, '没有要查询的openid');
  731. }
  732. if(empty($params['pagesize'])) {
  733. $params['pagesize'] = 50;
  734. }
  735. if(empty($params['pageindex'])) {
  736. $params['pageindex'] = 1;
  737. }
  738. $token = $this->getAccessToken();
  739. if(is_error($token)){
  740. return $token;
  741. }
  742. $url = "https://api.weixin.qq.com/customservice/msgrecord/getrecord?access_token={$token}";
  743. $response = ihttp_request($url, json_encode($params));
  744. if(is_error($response)) {
  745. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  746. }
  747. $result = @json_decode($response['content'], true);
  748. if(empty($result)) {
  749. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  750. } elseif(!empty($result['errcode'])) {
  751. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  752. }
  753. return $result;
  754. }
  755. public function isTagSupported() {
  756. return (!empty($this->account['key']) &&
  757. !empty($this->account['secret']) || $this->account['type'] == ACCOUNT_OAUTH_LOGIN) &&
  758. (intval($this->account['level']) > ACCOUNT_SERVICE);
  759. }
  760. public function fansTagAdd($tagname) {
  761. if(empty($tagname)) {
  762. return error(-1, '请填写标签名称');
  763. }
  764. $token = $this->getAccessToken();
  765. if(is_error($token)){
  766. return $token;
  767. }
  768. $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token={$token}";
  769. $data = stripslashes(ijson_encode(array('tag' => array('name' => $tagname)), JSON_UNESCAPED_UNICODE));
  770. $result = $this->requestApi($url, $data);
  771. return $result;
  772. }
  773. public function fansTagFetchAll() {
  774. $token = $this->getAccessToken();
  775. if(is_error($token)){
  776. return $token;
  777. }
  778. $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={$token}";
  779. $result = $this->requestApi($url);
  780. return $result;
  781. }
  782. public function fansTagEdit($tagid, $tagname) {
  783. if(empty($tagid) || empty($tagname)) {
  784. return error(-1, '标签信息错误');
  785. }
  786. if(in_array($tagid, array(1, 2))) {
  787. return error(-1, '微信平台默认标签,不能修改');
  788. }
  789. $token = $this->getAccessToken();
  790. if(is_error($token)){
  791. return $token;
  792. }
  793. $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token={$token}";
  794. $data = stripslashes(ijson_encode(array('tag' => array('id' => $tagid, 'name' => $tagname)), JSON_UNESCAPED_UNICODE));
  795. $result = $this->requestApi($url, $data);
  796. if (is_error($result)) {
  797. return $result;
  798. }
  799. return true;
  800. }
  801. public function fansTagDelete($tagid) {
  802. $tagid = intval($tagid);
  803. if(empty($tagid)) {
  804. return error(-1, '标签id错误');
  805. }
  806. $token = $this->getAccessToken();
  807. if(is_error($token)){
  808. return $token;
  809. }
  810. $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={$token}";
  811. $data = json_encode(array('tag' => array('id' => $tagid)));
  812. $result = $this->requestApi($url, $data);
  813. if (is_error($result)) {
  814. return $result;
  815. }
  816. return true;
  817. }
  818. public function fansTagGetUserlist($tagid, $next_openid = '') {
  819. $tagid = intval($tagid);
  820. $next_openid = (string) $next_openid;
  821. if(empty($tagid)) {
  822. return error(-1, '标签id错误');
  823. }
  824. $token = $this->getAccessToken();
  825. if(is_error($token)){
  826. return $token;
  827. }
  828. $url = 'https://api.weixin.qq.com/cgi-bin/tag/get?access_token=' . $token;
  829. $data = array(
  830. 'tagid' => $tagid
  831. );
  832. if( ! empty($next_openid)){
  833. $data['next_openid'] = $next_openid;
  834. }
  835. $data = json_encode($data);
  836. $result = $this->requestApi($url, $data);
  837. return $result;
  838. }
  839. public function fansTagTagging($openid, $tagids) {
  840. $openid = (string) $openid;
  841. $tagids = (array) $tagids;
  842. if(empty($openid)){
  843. return error(-1, '没有填写用户openid');
  844. }
  845. if(empty($tagids)) {
  846. return error(-1, '没有填写标签');
  847. }
  848. if(count($tagids) > 3) {
  849. return error(-1, '最多3个标签');
  850. }
  851. $token = $this->getAccessToken();
  852. if (is_error($token)) {
  853. return $token;
  854. }
  855. $fetch_result = $this->fansTagFetchOwnTags($openid);
  856. if(is_error($fetch_result)) {
  857. return $fetch_result;
  858. }
  859. foreach($fetch_result['tagid_list'] as $del_tagid) {
  860. $this->fansTagBatchUntagging($openid, $del_tagid);
  861. }
  862. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$token}";
  863. foreach($tagids as $tagid) {
  864. $data = array(
  865. 'openid_list' => $openid,
  866. 'tagid' => $tagid
  867. );
  868. $data = json_encode($data);
  869. $result = $this->requestApi($url, $data);
  870. if(is_error($result)) {
  871. return $result;
  872. }
  873. }
  874. return true;
  875. }
  876. public function fansTagBatchTagging($openid_list, $tagid) {
  877. $openid_list = (array) $openid_list;
  878. $tagid = (int) $tagid;
  879. if(empty($openid_list)){
  880. return error(-1, '没有填写用户openid列表');
  881. }
  882. if(empty($tagid)) {
  883. return error(-1, '没有填写tagid');
  884. }
  885. $token = $this->getAccessToken();
  886. if(is_error($token)){
  887. return $token;
  888. }
  889. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$token}";
  890. $data = array(
  891. 'openid_list' => $openid_list,
  892. 'tagid' => $tagid
  893. );
  894. $data = json_encode($data);
  895. $result = $this->requestApi($url, $data);
  896. if(is_error($result)) {
  897. return $result;
  898. }
  899. return true;
  900. }
  901. public function fansTagBatchUntagging($openid_list, $tagid) {
  902. $openid_list = (array) $openid_list;
  903. $tagid = (int) $tagid;
  904. if(empty($openid_list)){
  905. return error(-1, '没有填写用户openid列表');
  906. }
  907. if(empty($tagid)) {
  908. return error(-1, '没有填写tagid');
  909. }
  910. $token = $this->getAccessToken();
  911. if(is_error($token)){
  912. return $token;
  913. }
  914. $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={$token}";
  915. $data = array(
  916. 'openid_list' => $openid_list,
  917. 'tagid' => $tagid
  918. );
  919. $data = json_encode($data);
  920. $result = $this->requestApi($url, $data);
  921. if(is_error($result)) {
  922. return $result;
  923. }
  924. return true;
  925. }
  926. public function fansTagFetchOwnTags($openid) {
  927. $openid = (string) $openid;
  928. if(empty($openid)){
  929. return error(-1, '没有填写用户openid');
  930. }
  931. $token = $this->getAccessToken();
  932. if(is_error($token)){
  933. return $token;
  934. }
  935. $url = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token={$token}";
  936. $data = json_encode(array('openid' => $openid));
  937. $result = $this->requestApi($url, $data);
  938. return $result;
  939. }
  940. public function fansSendAll($group, $msgtype, $media_id) {
  941. $types = array('text' => 'text', 'image' => 'image', 'news' => 'mpnews', 'voice' => 'voice', 'video' => 'mpvideo', 'wxcard' => 'wxcard');
  942. if(empty($types[$msgtype])) {
  943. return error(-1, '消息类型不合法');
  944. }
  945. $is_to_all = false;
  946. if($group == - 1) {
  947. $is_to_all = true;
  948. }
  949. $data = array(
  950. 'filter' => array(
  951. 'is_to_all' => $is_to_all,
  952. 'group_id' => $group
  953. ),
  954. 'msgtype' => $types[$msgtype],
  955. $types[$msgtype] => array(
  956. 'media_id' => $media_id
  957. )
  958. );
  959. if($msgtype == 'wxcard') {
  960. unset($data['wxcard']['media_id']);
  961. $data['wxcard']['card_id'] = $media_id;
  962. }
  963. $token = $this->getAccessToken();
  964. if(is_error($token)){
  965. return $token;
  966. }
  967. $url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={$token}";
  968. $data = urldecode(json_encode($data));
  969. $response = ihttp_request($url, $data);
  970. if(is_error($response)) {
  971. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  972. }
  973. $result = @json_decode($response['content'], true);
  974. if(empty($result)) {
  975. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  976. } elseif(!empty($result['errcode'])) {
  977. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  978. }
  979. return $result;
  980. }
  981. public function fansSendPreview($wxname, $content, $msgtype) {
  982. $types = array('text' => 'text', 'image' => 'image', 'news' => 'mpnews', 'voice' => 'voice', 'video' => 'mpvideo', 'wxcard' => 'wxcard');
  983. if(empty($types[$msgtype])) {
  984. return error(-1, '群发类型不合法');
  985. }
  986. $msgtype = $types[$msgtype];
  987. $token = $this->getAccessToken();
  988. if(is_error($token)){
  989. return $token;
  990. }
  991. $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=' . $token;
  992. $send = array(
  993. 'towxname' => $wxname,
  994. 'msgtype' => $msgtype,
  995. );
  996. if($msgtype == 'text') {
  997. $send[$msgtype] = array(
  998. 'content' => $content
  999. );
  1000. } elseif($msgtype == 'wxcard') {
  1001. $send[$msgtype] = array(
  1002. 'card_id' => $content
  1003. );
  1004. } else {
  1005. $send[$msgtype] = array(
  1006. 'media_id' => $content
  1007. );
  1008. }
  1009. $response = ihttp_request($url, json_encode($send));
  1010. if(is_error($response)) {
  1011. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  1012. }
  1013. $result = @json_decode($response['content'], true);
  1014. if(empty($result)) {
  1015. } elseif(!empty($result['errcode'])) {
  1016. return error(-1, "访问公众平台接口失败, 错误: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  1017. }
  1018. return $result;
  1019. }
  1020. public function sendCustomNotice($data) {
  1021. if(empty($data)) {
  1022. return error(-1, '参数错误');
  1023. }
  1024. $token = $this->getAccessToken();
  1025. if(is_error($token)){
  1026. return $token;
  1027. }
  1028. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  1029. $response = ihttp_request($url, urldecode(json_encode($data)));
  1030. if(is_error($response)) {
  1031. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  1032. }
  1033. $result = @json_decode($response['content'], true);
  1034. if(empty($result)) {
  1035. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  1036. } elseif(!empty($result['errcode'])) {
  1037. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  1038. }
  1039. return true;
  1040. }
  1041. public function sendTplNotice($touser, $template_id, $postdata, $url = '', $topcolor = '#FF683F', $appid = '', $pagepath = '') {
  1042. if(empty($this->account['key']) || $this->account['level'] != ACCOUNT_SERVICE_VERIFY) {
  1043. return error(-1, '你的公众号没有发送模板消息的权限');
  1044. }
  1045. if(empty($touser)) {
  1046. return error(-1, '参数错误,粉丝openid不能为空');
  1047. }
  1048. if(empty($template_id)) {
  1049. return error(-1, '参数错误,模板标示不能为空');
  1050. }
  1051. if(empty($postdata) || !is_array($postdata)) {
  1052. return error(-1, '参数错误,请根据模板规则完善消息内容');
  1053. }
  1054. $token = $this->getAccessToken();
  1055. if (is_error($token)) {
  1056. return $token;
  1057. }
  1058. if (!empty($appid) && !empty($pagepath)) {
  1059. $url = 'http://weixin.qq.com/download';
  1060. $data['miniprogram'] = array('appid' => $appid, 'pagepath' => $pagepath);
  1061. }
  1062. $data = array();
  1063. $data['touser'] = $touser;
  1064. $data['template_id'] = trim($template_id);
  1065. $data['url'] = trim($url);
  1066. $data['topcolor'] = trim($topcolor);
  1067. $data['data'] = $postdata;
  1068. $data = json_encode($data);
  1069. $post_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$token}";
  1070. $response = ihttp_request($post_url, $data);
  1071. if(is_error($response)) {
  1072. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  1073. }
  1074. $result = @json_decode($response['content'], true);
  1075. if(empty($result)) {
  1076. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  1077. } elseif(!empty($result['errcode'])) {
  1078. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},信息详情:{$this->errorCode($result['errcode'])}");
  1079. }
  1080. return true;
  1081. }
  1082. public function uploadMedia($path, $type = 'image') {
  1083. if (empty($path)) {
  1084. return error(-1, '参数错误');
  1085. }
  1086. if (in_array(substr(ltrim($path, '/'), 0, 6), array('images', 'videos', 'audios', 'thumb'))) {
  1087. $path = ATTACHMENT_ROOT . ltrim($path, '/');
  1088. }
  1089. if (!file_exists($path)) {
  1090. return error(1, '文件不存在');
  1091. }
  1092. $token = $this->getAccessToken();
  1093. if (is_error($token)){
  1094. return $token;
  1095. }
  1096. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
  1097. $data = array(
  1098. 'media' => '@' . $path,
  1099. );
  1100. return $this->requestApi($url, $data);
  1101. }
  1102. public function uploadMediaFixed($path, $type = 'images') {
  1103. if (empty($path)) {
  1104. return error(-1, '参数错误');
  1105. }
  1106. if (in_array(substr(ltrim($path, '/'), 0, 6), array('images', 'videos', 'audios', 'thumb', 'voices'))) {
  1107. $path = ATTACHMENT_ROOT . ltrim($path, '/');
  1108. }
  1109. if (!file_exists($path)) {
  1110. return error(1, '文件不存在');
  1111. }
  1112. $token = $this->getAccessToken();
  1113. if (is_error($token)){
  1114. return $token;
  1115. }
  1116. $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}";
  1117. $data = array(
  1118. 'media' => '@' . $path
  1119. );
  1120. $filename = pathinfo($path, PATHINFO_FILENAME);
  1121. $description = array(
  1122. 'title' => $filename,
  1123. 'introduction' => $filename,
  1124. );
  1125. $data['description'] = urldecode(json_encode($description));
  1126. return $this->requestApi($url, $data);
  1127. }
  1128. public function editMaterialNews($data) {
  1129. $token = $this->getAccessToken();
  1130. if(is_error($token)){
  1131. return $token;
  1132. }
  1133. $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token={$token}";
  1134. $response = $this->requestApi($url, stripslashes(ijson_encode($data, JSON_UNESCAPED_UNICODE)));
  1135. if (is_error($response)) {
  1136. return $response;
  1137. }
  1138. return true;
  1139. }
  1140. public function uploadNewsThumb($thumb) {
  1141. $token = $this->getAccessToken();
  1142. if(is_error($token)){
  1143. return $token;
  1144. }
  1145. if (!file_exists($thumb)) {
  1146. return error(1, '文件不存在');
  1147. }
  1148. $data = array(
  1149. 'media' => '@'. $thumb,
  1150. );
  1151. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$token}";
  1152. $response = $this->requestApi($url, $data);
  1153. if (is_error($response)) {
  1154. return $response;
  1155. } else {
  1156. return $response['url'];
  1157. }
  1158. }
  1159. public function uploadVideoFixed($title, $description, $path) {
  1160. if (empty($path) || empty($title) || empty($description)) {
  1161. return error(-1, '参数错误');
  1162. }
  1163. if (in_array(substr(ltrim($path, '/'), 0, 6), array('images', 'videos', 'audios'))) {
  1164. $path = ATTACHMENT_ROOT . ltrim($path, '/');
  1165. }
  1166. if (!file_exists($path)) {
  1167. return error(1, '文件不存在');
  1168. }
  1169. $token = $this->getAccessToken();
  1170. if (is_error($token)){
  1171. return $token;
  1172. }
  1173. $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type=videos";
  1174. $data = array(
  1175. 'media' => '@' . $path,
  1176. 'description' => stripslashes(ijson_encode(array('title' => $title, 'introduction' => $description), JSON_UNESCAPED_UNICODE)),
  1177. );
  1178. $response = $this->requestApi($url, $data);
  1179. return $response;
  1180. }
  1181. public function uploadVideo($data) {
  1182. if(empty($data)) {
  1183. return error(-1, '参数错误');
  1184. }
  1185. $token = $this->getAccessToken();
  1186. if(is_error($token)){
  1187. return $token;
  1188. }
  1189. $url = "https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token={$token}";
  1190. $response = ihttp_request($url, urldecode(json_encode($data)));
  1191. if(is_error($response)) {
  1192. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  1193. }
  1194. $result = @json_decode($response['content'], true);
  1195. if(empty($result)) {
  1196. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  1197. } elseif(!empty($result['errcode'])) {
  1198. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']}, 错误详情:{$this->errorCode($result['errcode'])}");
  1199. }
  1200. return $result;
  1201. }
  1202. public function uploadNews($data) {
  1203. if(empty($data)) {
  1204. return error(-1, '参数错误');
  1205. }
  1206. $token = $this->getAccessToken();
  1207. if(is_error($token)){
  1208. return $token;
  1209. }
  1210. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token={$token}";
  1211. $response = ihttp_request($url, urldecode(json_encode($data)));
  1212. if(is_error($response)) {
  1213. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  1214. }
  1215. $result = @json_decode($response['content'], true);
  1216. if(empty($result)) {
  1217. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  1218. } elseif(!empty($result['errcode'])) {
  1219. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  1220. }
  1221. return $result;
  1222. }
  1223. public function addMatrialNews($data) {
  1224. $token = $this->getAccessToken();
  1225. if(is_error($token)){
  1226. return $token;
  1227. }
  1228. $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={$token}";
  1229. $data = stripslashes(urldecode(ijson_encode($data, JSON_UNESCAPED_UNICODE)));
  1230. $response = $this->requestApi($url, $data);
  1231. if (is_error($response)) {
  1232. return $response;
  1233. }
  1234. return $response['media_id'];
  1235. }
  1236. public function batchGetMaterial($type = 'news', $offset = 0, $count = 20) {
  1237. global $_W;
  1238. $token = $this->getAccessToken();
  1239. if(is_error($token)){
  1240. return $token;
  1241. }
  1242. $url = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=' . $token;
  1243. $data = array(
  1244. 'type' => $type,
  1245. 'offset' => intval($offset),
  1246. 'count' => $count,
  1247. );
  1248. $response = $this->requestApi($url, json_encode($data));
  1249. return $response;
  1250. }
  1251. public function getMaterial($media_id, $savefile = true) {
  1252. $token = $this->getAccessToken();
  1253. if(is_error($token)){
  1254. return $token;
  1255. }
  1256. $url = 'https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=' . $token;
  1257. $data = array(
  1258. 'media_id' => trim($media_id),
  1259. );
  1260. $response = ihttp_request($url, json_encode($data));
  1261. if(is_error($response)) {
  1262. return error(-1, "访问公平台接口失败, 错误: {$response['message']}");
  1263. }
  1264. $result = @json_decode($response['content'], true);
  1265. if(!empty($result['errcode'])) {
  1266. return error(-1, "访问公众平台接口失败, 错误: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  1267. }
  1268. if (empty($response['headers']['Content-disposition'])) {
  1269. $response = json_decode($response['content'], true);
  1270. if (!empty($response['down_url'])) {
  1271. if (empty($savefile)) {
  1272. return $response;
  1273. }
  1274. $response = ihttp_get($response['down_url']);
  1275. $response['headers']['Content-disposition'] = $response['headers']['Content-Disposition'];
  1276. } elseif (!empty($response['news_item'])) {
  1277. return $response;
  1278. }
  1279. }
  1280. if($savefile && !empty($response['headers']['Content-disposition']) && strexists($response['headers']['Content-disposition'], 'filename=')){
  1281. global $_W;
  1282. preg_match('/filename=\"?([^"]*)/', $response['headers']['Content-disposition'], $match);
  1283. $pathinfo = pathinfo($match[1]);
  1284. $filename = $_W['uniacid'].'/'.date('Y/m/');
  1285. if (in_array(strtolower($pathinfo['extension']), array('mp4'))) {
  1286. $filename = 'videos/' . $filename;
  1287. } elseif (in_array(strtolower($pathinfo['extension']), array('amr', 'mp3', 'wma', 'wmv'))) {
  1288. $filename = 'audios/' . $filename;
  1289. } else {
  1290. $filename = 'images/' . $filename;
  1291. }
  1292. $filename .= file_random_name($filename, $pathinfo['extension']);
  1293. load()->func('file');
  1294. file_write($filename, $response['content']);
  1295. file_remote_upload($filename);
  1296. return $filename;
  1297. } else {
  1298. return $response['content'];
  1299. }
  1300. return $result;
  1301. }
  1302. public function downloadMedia($media_id, $savefile = true) {
  1303. $mediatypes = array('image', 'voice', 'thumb');
  1304. $media_id = is_array($media_id) ? $media_id['media_id'] : $media_id;
  1305. if (empty($media_id)) {
  1306. return error(-1, '微信下载媒体资源参数错误');
  1307. }
  1308. $token = $this->getAccessToken();
  1309. if(is_error($token)){
  1310. return $token;
  1311. }
  1312. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={$token}&media_id={$media_id}";
  1313. $response = ihttp_get($url);
  1314. if (empty($response['headers']['Content-disposition'])) {
  1315. $response = json_decode($response['content'], true);
  1316. if (!empty($response['video_url'])) {
  1317. $response = ihttp_get($response['video_url']);
  1318. $response['headers']['Content-disposition'] = $response['headers']['Content-Disposition'];
  1319. }
  1320. }
  1321. if($savefile && !empty($response['headers']['Content-disposition']) && strexists($response['headers']['Content-disposition'], 'filename=')){
  1322. global $_W;
  1323. preg_match('/filename=\"?([^"]*)/', $response['headers']['Content-disposition'], $match);
  1324. $filename = $_W['uniacid'].'/'.date('Y/m/') . $match[1];
  1325. $pathinfo = pathinfo($filename);
  1326. if (in_array(strtolower($pathinfo['extension']), array('mp4'))) {
  1327. $filename = 'videos/' . $filename;
  1328. } elseif (in_array(strtolower($pathinfo['extension']), array('amr', 'mp3', 'wma', 'wmv'))) {
  1329. $filename = 'audios/' . $filename;
  1330. } else {
  1331. $filename = 'images/' . $filename;
  1332. }
  1333. load()->func('file');
  1334. file_write($filename, $response['content']);
  1335. file_remote_upload($filename);
  1336. return $filename;
  1337. } else {
  1338. return $response['content'];
  1339. }
  1340. }
  1341. public function getMaterialCount() {
  1342. $token = $this->getAccessToken();
  1343. if(is_error($token)){
  1344. return $token;
  1345. }
  1346. $url = 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=' . $token;
  1347. $response = $this->requestApi($url);
  1348. return $response;
  1349. }
  1350. public function delMaterial($media_id) {
  1351. $media_id = trim($media_id);
  1352. if(empty($media_id)) {
  1353. return error(-1, '素材media_id错误');
  1354. }
  1355. $token = $this->getAccessToken();
  1356. if(is_error($token)){
  1357. return $token;
  1358. }
  1359. $url = 'https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=' . $token;
  1360. $data = array(
  1361. 'media_id' => trim($media_id),
  1362. );
  1363. $response = $this->requestApi($url, json_encode($data));
  1364. if (is_error($response)) {
  1365. return $response;
  1366. }
  1367. return true;
  1368. }
  1369. public function changeOrderStatus($send) {
  1370. if (empty($send)) {
  1371. return error(-1, '参数错误');
  1372. }
  1373. $token = $this->getAccessToken();
  1374. if(is_error($token)){
  1375. return $token;
  1376. }
  1377. $sendapi = 'https://api.weixin.qq.com/pay/delivernotify?access_token=' . $token;
  1378. $response = ihttp_request($sendapi, json_encode($send));
  1379. $response = json_decode($response['content'], true);
  1380. if (empty($response)) {
  1381. return error(-1, '发货失败,请检查您的公众号权限或是公众号AppId和公众号AppSecret!');
  1382. }
  1383. if (!empty($response['errcode'])) {
  1384. return error(-1, $response['errmsg']);
  1385. }
  1386. return $response;
  1387. }
  1388. public function getOauthUserInfo($accesstoken, $openid) {
  1389. $apiurl = "https://api.weixin.qq.com/sns/userinfo?access_token={$accesstoken}&openid={$openid}&lang=zh_CN";
  1390. $response = $this->requestApi($apiurl);
  1391. unset($response['remark'], $response['subscribe_scene'], $response['qr_scene'], $response['qr_scene_str']);
  1392. return $response;
  1393. }
  1394. public function getOauthInfo($code = '') {
  1395. global $_W, $_GPC;
  1396. if (!empty($_GPC['code'])) {
  1397. $code = $_GPC['code'];
  1398. }
  1399. if (empty($code)) {
  1400. $oauth_url = uni_account_oauth_host();
  1401. $url = $oauth_url . "app/index.php?{$_SERVER['QUERY_STRING']}";
  1402. $forward = $this->getOauthCodeUrl(urlencode($url));
  1403. header('Location: ' . $forward);
  1404. exit;
  1405. }
  1406. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->account['key']}&secret={$this->account['secret']}&code={$code}&grant_type=authorization_code";
  1407. $response = $this->requestApi($url);
  1408. return $response;
  1409. }
  1410. public function getOauthAccessToken() {
  1411. $cachekey = cache_system_key('oauthaccesstoken', array('acid' => $this->account['acid']));
  1412. $cache = cache_load($cachekey);
  1413. if (!empty($cache) && !empty($cache['token']) && $cache['expire'] > TIMESTAMP) {
  1414. return $cache['token'];
  1415. }
  1416. $token = $this->getOauthInfo();
  1417. if (is_error($token)) {
  1418. return error(1);
  1419. }
  1420. $record = array();
  1421. $record['token'] = $token['access_token'];
  1422. $record['expire'] = TIMESTAMP + $token['expires_in'] - 200;
  1423. cache_write($cachekey, $record);
  1424. return $token['access_token'];
  1425. }
  1426. public function getShareAddressConfig() {
  1427. global $_W;
  1428. static $current_url;
  1429. if (empty($current_url)) {
  1430. $current_url = $_W['siteurl'];
  1431. }
  1432. $token = $this->getOauthAccessToken();
  1433. if (is_error($token)) {
  1434. return false;
  1435. }
  1436. $package = array(
  1437. 'appid' => $this->account['key'],
  1438. 'url' => $current_url,
  1439. 'timestamp' => strval(TIMESTAMP),
  1440. 'noncestr' => strval(random(8, true)),
  1441. 'accesstoken' => $token
  1442. );
  1443. ksort($package, SORT_STRING);
  1444. $signstring = array();
  1445. foreach ($package as $k => $v) {
  1446. $signstring[] = "{$k}={$v}";
  1447. }
  1448. $signstring = strtolower(sha1(trim(implode('&', $signstring))));
  1449. $shareaddress_config = array(
  1450. 'appId' => $this->account['key'],
  1451. 'scope' => 'jsapi_address',
  1452. 'signType' => 'sha1',
  1453. 'addrSign' => $signstring,
  1454. 'timeStamp' => $package['timestamp'],
  1455. 'nonceStr' => $package['noncestr']
  1456. );
  1457. return $shareaddress_config;
  1458. }
  1459. public function getOauthCodeUrl($callback, $state = '') {
  1460. return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->account['key']}&redirect_uri={$callback}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect";
  1461. }
  1462. public function getOauthUserInfoUrl($callback, $state = '') {
  1463. return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->account['key']}&redirect_uri={$callback}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
  1464. }
  1465. public function getFansStat() {
  1466. global $_W;
  1467. $token = $this->getAccessToken();
  1468. if (is_error($token)) {
  1469. return $token;
  1470. }
  1471. $url = "https://api.weixin.qq.com/datacube/getusersummary?access_token={$token}";
  1472. $data = array(
  1473. 'begin_date' => date('Y-m-d', strtotime('-7 days')),
  1474. 'end_date' => date('Y-m-d', strtotime('-1 days'))
  1475. );
  1476. $summary_response = $this->requestApi($url, json_encode($data));
  1477. if (is_error($summary_response)) {
  1478. return $summary_response;
  1479. }
  1480. $url = "https://api.weixin.qq.com/datacube/getusercumulate?access_token={$token}";
  1481. $cumulate_response = $this->requestApi($url, json_encode($data));
  1482. if(is_error($cumulate_response)) {
  1483. return $cumulate_response;
  1484. }
  1485. $result = array();
  1486. if (!empty($summary_response['list'])) {
  1487. foreach ($summary_response['list'] as $row) {
  1488. $key = str_replace('-', '', $row['ref_date']);
  1489. $result[$key]['new'] = intval($result[$key]['new']) + $row['new_user'];
  1490. $result[$key]['cancel'] = intval($result[$key]['cancel']) + $row['cancel_user'];
  1491. }
  1492. }
  1493. if (!empty($cumulate_response['list'])) {
  1494. foreach ($cumulate_response['list'] as $row) {
  1495. $key = str_replace('-', '', $row['ref_date']);
  1496. $result[$key]['cumulate'] = $row['cumulate_user'];
  1497. }
  1498. }
  1499. return $result;
  1500. }
  1501. protected function requestApi($url, $post = '') {
  1502. $response = ihttp_request($url, $post);
  1503. $result = @json_decode($response['content'], true);
  1504. if(is_error($response)) {
  1505. return error($result['errcode'], "访问公众平台接口失败, 错误详情: {$this->errorCode($result['errcode'])}");
  1506. }
  1507. if(empty($result)) {
  1508. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  1509. } elseif(!empty($result['errcode'])) {
  1510. return error($result['errcode'], "访问公众平台接口失败, 错误: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  1511. }
  1512. return $result;
  1513. }
  1514. public function getMaterialSupport() {
  1515. return array(
  1516. 'mass' => array('news'=> false, 'image'=> false,'voice'=> false,'video'=> false),
  1517. 'chats' => array('basic'=> false,'news'=> false,'image'=> false,'music'=> false,'voice'=> false,'video'=> false)
  1518. );
  1519. }
  1520. }