wxapp.account.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. load()->func('communication');
  8. class WxappAccount extends WeAccount {
  9. protected $tablename = 'account_wxapp';
  10. protected $menuFrame = 'wxapp';
  11. protected $type = ACCOUNT_TYPE_APP_NORMAL;
  12. protected $typeName = '微信小程序';
  13. protected $typeTempalte = '-wxapp';
  14. protected $typeSign = WXAPP_TYPE_SIGN;
  15. protected $supportVersion = STATUS_ON;
  16. protected function getAccountInfo($uniacid) {
  17. $account = table('account_wxapp')->getAccount($uniacid);
  18. $account['encrypt_key'] = $account['key'];
  19. return $account;
  20. }
  21. public function getOauthInfo($code = '') {
  22. global $_W, $_GPC;
  23. if (!empty($_GPC['code'])) {
  24. $code = $_GPC['code'];
  25. }
  26. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->account['key']}&secret={$this->account['secret']}&js_code={$code}&grant_type=authorization_code";
  27. return $response = $this->requestApi($url);
  28. }
  29. public function getOauthCodeUrl($callback, $state = '') {
  30. 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";
  31. }
  32. public function getOauthUserInfoUrl($callback, $state = '') {
  33. 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";
  34. }
  35. public function checkSign() {
  36. $token = $this->account['token'];
  37. $signkey = array($token, $_GET['timestamp'], $_GET['nonce']);
  38. sort($signkey, SORT_STRING);
  39. $signString = implode($signkey);
  40. $signString = sha1($signString);
  41. return $signString == $_GET['signature'];
  42. }
  43. public function pkcs7Encode($encrypt_data, $iv) {
  44. $key = base64_decode($_SESSION['session_key']);
  45. $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
  46. if (is_error($result)) {
  47. return error(1, '解密失败');
  48. }
  49. $result = json_decode($result, true);
  50. if (empty($result)) {
  51. return error(1, '解密失败');
  52. }
  53. if ($result['watermark']['appid'] != $this->account['key']) {
  54. return error(1, '解密失败');
  55. }
  56. unset($result['watermark']);
  57. return $result;
  58. }
  59. public function getAccessToken() {
  60. global $_W;
  61. $cachekey = cache_system_key('accesstoken_key', array('key' => $this->account['key']));
  62. $cache = cache_load($cachekey);
  63. if (!empty($cache) && !empty($cache['token'])) {
  64. $this->account['access_token'] = $cache;
  65. return $cache['token'];
  66. }
  67. if (!empty($this->account['link'])) {
  68. load()->library('sdk-module');
  69. $link_config = iunserializer($this->account['link_config']);
  70. try {
  71. $api = new \W7\Sdk\Module\Api($_W['setting']['site']['key'], $_W['setting']['site']['token'], $link_config['app_id'], 2, CLOUD_V3API_DOMAIN_PRE);
  72. $token = $api->app()->getAccessToken()->toArray();
  73. } catch (Exception $e) {
  74. return error('-1', $e->getMessage());
  75. }
  76. } else {
  77. if (empty($this->account['key']) || empty($this->account['secret'])) {
  78. return error('-1', '未填写小程序的 appid 或 appsecret!');
  79. }
  80. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->account['key']}&secret={$this->account['secret']}";
  81. $token = $this->requestApi($url);
  82. if (is_error($token)) {
  83. return $token;
  84. }
  85. }
  86. $record = array(
  87. 'token' => $token['access_token'],
  88. );
  89. $record_expire = intval($token['expires_in']) - 200;
  90. $this->account['access_token'] = $record;
  91. cache_write($cachekey, $record, $record_expire);
  92. return $record['token'];
  93. }
  94. public function getJssdkConfig($url = '') {
  95. return array();
  96. }
  97. public function getCodeLimit($path, $width = '430', $option = array()) {
  98. if (!preg_match('/[0-9a-zA-Z\&\/\:\=\?\-\.\_\~\@]{1,128}/', $path)) {
  99. return error(1, '路径值不合法');
  100. }
  101. $access_token = $this->getAccessToken();
  102. if (is_error($access_token)) {
  103. return $access_token;
  104. }
  105. $data = array(
  106. 'path' => $path,
  107. 'width' => intval($width),
  108. );
  109. if (!empty($option['auto_color'])) {
  110. $data['auto_color'] = intval($option['auto_color']);
  111. }
  112. if (!empty($option['line_color'])) {
  113. $data['line_color'] = array(
  114. 'r' => $option['line_color']['r'],
  115. 'g' => $option['line_color']['g'],
  116. 'b' => $option['line_color']['b'],
  117. );
  118. $data['auto_color'] = false;
  119. }
  120. $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' . $access_token;
  121. $response = $this->requestApi($url, json_encode($data));
  122. if (is_error($response)) {
  123. return $response;
  124. }
  125. return $response['content'];
  126. }
  127. public function getCodeUnlimit($scene, $page = '', $width = '430', $option = array()) {
  128. if (!preg_match('/[0-9a-zA-Z\!\#\$\&\'\(\)\*\+\,\/\:\;\=\?\@\-\.\_\~]{1,32}/', $scene)) {
  129. return error(1, '场景值不合法');
  130. }
  131. $access_token = $this->getAccessToken();
  132. if (is_error($access_token)) {
  133. return $access_token;
  134. }
  135. $data = array(
  136. 'scene' => $scene,
  137. 'width' => intval($width),
  138. );
  139. if (!empty($page)) {
  140. $data['page'] = $page;
  141. }
  142. if (!empty($option['auto_color'])) {
  143. $data['auto_color'] = intval($option['auto_color']);
  144. }
  145. if (!empty($option['line_color'])) {
  146. $data['line_color'] = array(
  147. 'r' => $option['line_color']['r'],
  148. 'g' => $option['line_color']['g'],
  149. 'b' => $option['line_color']['b'],
  150. );
  151. $data['auto_color'] = false;
  152. }
  153. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
  154. $response = $this->requestApi($url, json_encode($data));
  155. if (is_error($response)) {
  156. return $response;
  157. }
  158. return $response['content'];
  159. }
  160. public function getQrcode() {
  161. }
  162. protected function requestApi($url, $post = '') {
  163. $response = ihttp_request($url, $post);
  164. $result = @json_decode($response['content'], true);
  165. if (is_error($response)) {
  166. return error($result['errcode'], "访问公众平台接口失败, 错误详情: {$this->errorCode($result['errcode'])}");
  167. }
  168. if (empty($result)) {
  169. return $response;
  170. } elseif (!empty($result['errcode'])) {
  171. return error($result['errcode'], "访问公众平台接口失败, 错误: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  172. }
  173. return $result;
  174. }
  175. private function encryptErrorCode($code) {
  176. $errors = array(
  177. '40001' => '签名验证错误',
  178. '40002' => 'xml解析失败',
  179. '40003' => 'sha加密生成签名失败',
  180. '40004' => 'encodingAesKey 非法',
  181. '40005' => 'appid 校验错误',
  182. '40006' => 'aes 加密失败',
  183. '40007' => 'aes 解密失败',
  184. '40008' => '解密后得到的buffer非法',
  185. '40009' => 'base64加密失败',
  186. '40010' => 'base64解密失败',
  187. '40011' => '生成xml失败',
  188. );
  189. if ($errors[$code]) {
  190. return $errors[$code];
  191. } else {
  192. return '未知错误';
  193. }
  194. }
  195. public function checkSignature($encrypt_msg) {
  196. $str = $this->buildSignature($encrypt_msg);
  197. return $str == $_GET['msg_signature'];
  198. }
  199. public function buildSignature($encrypt_msg) {
  200. $token = $this->account['token'];
  201. $array = array($encrypt_msg, $token, $_GET['timestamp'], $_GET['nonce']);
  202. sort($array, SORT_STRING);
  203. $str = implode($array);
  204. $str = sha1($str);
  205. return $str;
  206. }
  207. public function encryptMsg($text) {
  208. $token = $this->account['token'];
  209. $encodingaeskey = $this->account['encodingaeskey'];
  210. $appid = $this->account['encrypt_key'];
  211. $key = base64_decode($encodingaeskey . '=');
  212. $text = random(16) . pack('N', strlen($text)) . $text . $appid;
  213. $iv = substr($key, 0, 16);
  214. $block_size = 32;
  215. $text_length = strlen($text);
  216. $amount_to_pad = $block_size - ($text_length % $block_size);
  217. if (0 == $amount_to_pad) {
  218. $amount_to_pad = $block_size;
  219. }
  220. $pad_chr = chr($amount_to_pad);
  221. $tmp = '';
  222. for ($index = 0; $index < $amount_to_pad; ++$index) {
  223. $tmp .= $pad_chr;
  224. }
  225. $text = $text . $tmp;
  226. $encrypted = openssl_encrypt($text, 'AES-256-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
  227. $encrypt_msg = base64_encode($encrypted);
  228. $signature = $this->buildSignature($encrypt_msg);
  229. return array($signature, $encrypt_msg);
  230. }
  231. public function xmlDetract($data) {
  232. $xml['Encrypt'] = $data[1];
  233. $xml['MsgSignature'] = $data[0];
  234. $xml['TimeStamp'] = $_GET['timestamp'];
  235. $xml['Nonce'] = $_GET['nonce'];
  236. return array2xml($xml);
  237. }
  238. public function xmlExtract($message) {
  239. $packet = array();
  240. if (!empty($message)) {
  241. $obj = isimplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA);
  242. if ($obj instanceof SimpleXMLElement) {
  243. $packet['encrypt'] = strval($obj->Encrypt);
  244. $packet['to'] = strval($obj->ToUserName);
  245. }
  246. }
  247. if (!empty($packet['encrypt'])) {
  248. return $packet;
  249. } else {
  250. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40002 \n,错误描述为: " . $this->encryptErrorCode('40002'));
  251. }
  252. }
  253. public function decryptMsg($postData) {
  254. $token = $this->account['token'];
  255. $encodingaeskey = $this->account['encodingaeskey'];
  256. $appid = $this->account['encrypt_key'];
  257. $key = base64_decode($encodingaeskey . '=');
  258. if (43 != strlen($encodingaeskey)) {
  259. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40004 \n,错误描述为: " . $this->encryptErrorCode('40004'));
  260. }
  261. $packet = $this->xmlExtract($postData);
  262. if (is_error($packet)) {
  263. return error(-1, $packet['message']);
  264. }
  265. $istrue = $this->checkSignature($packet['encrypt']);
  266. if (!$istrue) {
  267. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40001 \n,错误描述为: " . $this->encryptErrorCode('40001'));
  268. }
  269. $ciphertext_dec = base64_decode($packet['encrypt']);
  270. $iv = substr($key, 0, 16);
  271. $decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
  272. $pad = ord(substr($decrypted, -1));
  273. if ($pad < 1 || $pad > 32) {
  274. $pad = 0;
  275. }
  276. $result = substr($decrypted, 0, (strlen($decrypted) - $pad));
  277. if (strlen($result) < 16) {
  278. return '';
  279. }
  280. $content = substr($result, 16, strlen($result));
  281. $len_list = unpack('N', substr($content, 0, 4));
  282. $xml_len = $len_list[1];
  283. $xml_content = substr($content, 4, $xml_len);
  284. $from_appid = substr($content, $xml_len + 4);
  285. if ($from_appid != $appid) {
  286. return error(-1, "微信公众平台返回接口错误. \n错误代码为: 40005 \n,错误描述为: " . $this->encryptErrorCode('40005'));
  287. }
  288. return $xml_content;
  289. }
  290. public function result($errno, $message = '', $data = '') {
  291. exit(json_encode(array(
  292. 'errno' => $errno,
  293. 'message' => $message,
  294. 'data' => $data,
  295. )));
  296. }
  297. public function getDailyVisitTrend($date) {
  298. global $_W;
  299. $token = $this->getAccessToken();
  300. if (is_error($token)) {
  301. return $token;
  302. }
  303. $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token={$token}";
  304. $data = array(
  305. 'begin_date' => $date,
  306. 'end_date' => $date,
  307. );
  308. $response = $this->requestApi($url, json_encode($data));
  309. if (is_error($response)) {
  310. return $response;
  311. }
  312. return $response['list'][0];
  313. }
  314. }