Wechat.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | core.php 微信平台核心类
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Main\Lib;
  8. use Dever;
  9. class Wechat
  10. {
  11. /**
  12. * config
  13. *
  14. * @var array
  15. */
  16. private $config;
  17. /**
  18. * project
  19. *
  20. * @var string
  21. */
  22. private $project;
  23. /**
  24. * 构造函数 初始化
  25. *
  26. * @return mixed
  27. */
  28. public function __construct($project = false, $type = '')
  29. {
  30. $this->config = Dever::config('wechat', $type)->cAll;
  31. $type = $this->config['type'];
  32. if (!$project) {
  33. $appid = Dever::input('appid');
  34. if ($appid) {
  35. $project = Dever::db('main/project')->one(array('option_type' => $type, 'option_appid' => $appid));
  36. }
  37. /*
  38. if (!$project) {
  39. $project = Dever::db('main/project')->one(array('option_type' => $type));
  40. }
  41. */
  42. if (!$project) {
  43. $project = Dever::input('project', 1);
  44. }
  45. }
  46. if (is_numeric($project)) {
  47. $project = Dever::db('main/project')->one(array('option_type' => $type, 'option_id' => $project));
  48. }
  49. if (!$project) {
  50. Dever::alert('project is not exits!');
  51. }
  52. $this->project = $project;
  53. }
  54. /**
  55. * 获取当前站点的配置
  56. *
  57. * @return mixed
  58. */
  59. public function project()
  60. {
  61. return $this->project;
  62. }
  63. /**
  64. * 获取当前基本的配置
  65. *
  66. * @return mixed
  67. */
  68. public function config()
  69. {
  70. return $this->config;
  71. }
  72. /**
  73. * 更新数据库
  74. * state true为强制更新数据库中的token数据
  75. *
  76. * @return mixed
  77. */
  78. private function save($type = 'ticket', $value, $expires = false, $interval = 2000, $data = false, $state = false)
  79. {
  80. $refresh = false;
  81. if (strpos($type, '.')) {
  82. $temp = explode('.', $type);
  83. if ($temp[1] == 'refresh') {
  84. $refresh = true;
  85. $temp[1] = 'oauth';
  86. }
  87. $table = 'main/' . $temp[1];
  88. } else {
  89. $table = 'main/' . $type;
  90. }
  91. $db = Dever::db($table);
  92. if ($data) {
  93. if (isset($data['id'])) {
  94. $where['option_id'] = $data['id'];
  95. unset($data['id']);
  96. }
  97. if (isset($data['openid'])) {
  98. $where['option_openid'] = $data['openid'];
  99. }
  100. if (isset($data['unionid'])) {
  101. $where['option_unionid'] = $data['unionid'];
  102. }
  103. }
  104. $where['option_project_id'] = $this->project['id'];
  105. $info = $db->one($where);
  106. $update = false;
  107. if ($state == true) {
  108. $update = true;
  109. } elseif ($info && time() - $info['mdate'] >= $info['expires']) {
  110. $update = true;
  111. } elseif($info) {
  112. return $info;
  113. }
  114. if (!$info) {
  115. $update = false;
  116. }
  117. if (!$value) {
  118. $value = $this->param($type, $info);
  119. }
  120. if (is_array($value) || (is_string($value) && strstr($value, 'http://'))) {
  121. if ($refresh) {
  122. $result = $this->curl(false, $value, false, $type);
  123. } else {
  124. $result = $this->curl(false, $value, true, $type);
  125. }
  126. $key = $type;
  127. if ($result && isset($result[$key])) {
  128. $data = $result;
  129. $data['value'] = $result[$key];
  130. $data['expires'] = $result['expires_in'];
  131. }
  132. } else {
  133. $data['value'] = $value;
  134. $data['expires'] = $expires;
  135. }
  136. if ($data && isset($data['value']) && $data['value']) {
  137. $data['project_id'] = $this->project['id'];
  138. $expires = $data['expires'] - $interval;
  139. if ($expires <= 0) {
  140. $data['expires'] = $data['expires'] - 300;
  141. } else {
  142. $data['expires'] = $expires;
  143. }
  144. if ($update == true) {
  145. $data['where_id'] = $info['id'];
  146. $id = $info['id'];
  147. $db->update($data);
  148. } else {
  149. $id = $db->insert($data);
  150. }
  151. $data['id'] = $id;
  152. if ($id > 0 && isset($result['callback'])) {
  153. foreach ($result['callback'] as $v) {
  154. Dever::load($v[0], $id, $v[1], $this->project['id']);
  155. }
  156. }
  157. } elseif($info && $info['value']) {
  158. $data = $info;
  159. $data['value'] = $info['value'];
  160. }
  161. return $data;
  162. }
  163. /**
  164. * 获取最新的token
  165. *
  166. * @return mixed
  167. */
  168. public function token($value = false, $expires = false, $interval = 2000, $state = false)
  169. {
  170. $result = $this->save('token', $value, $expires, $interval, false, $state);
  171. return $result['value'];
  172. }
  173. /**
  174. * 获取最新的ticket
  175. *
  176. * @return mixed
  177. */
  178. public function ticket($value = false, $expires = false, $interval = 200, $state = false)
  179. {
  180. $result = $this->save('ticket', $value, $expires, $interval, false, $state);
  181. return $result['value'];
  182. }
  183. /**
  184. * 获取最新的oauth token
  185. *
  186. * @return mixed
  187. */
  188. public function oauth($param, $interval = 2000, $state = false)
  189. {
  190. if (is_array($param) && isset($param['auth_code'])) {
  191. $result = $this->curl('oauth.oauth', $param);
  192. if (isset($result['oauth.oauth'])) {
  193. return $this->save('oauth.oauth', $result['oauth.oauth'], $result['expires_in'], $interval, $result);
  194. } else {
  195. Dever::alert('oauth error');
  196. }
  197. } else {
  198. if (is_numeric($param)) {
  199. $id = $param;
  200. $param = array();
  201. $param['id'] = $id;
  202. }
  203. return $this->save('oauth.refresh', false, false, $interval, $param, $state);
  204. }
  205. }
  206. /**
  207. * 获取最新的code
  208. *
  209. * @return mixed
  210. */
  211. public function code($value = false, $expires = false, $interval = 200)
  212. {
  213. $result = $this->save('oauth.code', $value, $expires, $interval, false, true);
  214. return $result['value'];
  215. }
  216. /**
  217. * 获取最新的openid
  218. *
  219. * @return mixed
  220. */
  221. public function openid($id, $key = 'openid')
  222. {
  223. $info = Dever::db('main/oauth')->one($id);
  224. return $info[$key];
  225. }
  226. /**
  227. * 获取最新的oauth login地址
  228. *
  229. * @return mixed
  230. */
  231. public function login($callback = false, $code = false, $location = true)
  232. {
  233. if (!$code) {
  234. $code = $this->code();
  235. }
  236. $callback = Dever::url($callback);
  237. $config = $this->param('oauth.login', array('code' => $code, 'redirect' => $callback));
  238. $url = $config['url'] . http_build_query($config['param']);
  239. if ($location == true) {
  240. Dever::location($url);
  241. }
  242. return $url;
  243. }
  244. /**
  245. * 从wechat获取数据
  246. *
  247. * @return mixed
  248. */
  249. public function curl($method, $param = array(), $alert = true, $type = false)
  250. {
  251. //return array();
  252. if (is_string($param)) {
  253. $result = json_decode(Dever::curl($param), true);
  254. } else {
  255. if ($method) {
  256. $param = $this->param($method, $param);
  257. }
  258. $result = json_decode(Dever::curl($param['url'], $param['param'], $param['method'], $param['json']), true);
  259. $this->log($result, $param['name'], $param['url'], $param['param']);
  260. }
  261. if (isset($result['errcode']) && $result['errcode'] != 0) {
  262. /*
  263. if ($result['errcode'] == 40001) {
  264. $token = $this->token(false, false, 2000, true);
  265. return $this->curl($type, array(), $alert);
  266. }
  267. */
  268. $result = $param + $result;
  269. Dever::log($result);
  270. if ($alert) {
  271. Dever::alert(json_encode($result, JSON_UNESCAPED_UNICODE));
  272. }
  273. }
  274. if (isset($param['response'])) {
  275. foreach ($param['response'] as $k => $v) {
  276. if (strpos($k, '.')) {
  277. $temp = explode('.', $k);
  278. if (isset($result[$temp[0]][$temp[1]])) {
  279. $result[$v] = $result[$temp[0]][$temp[1]];
  280. }
  281. } elseif (isset($result[$k])) {
  282. $result[$v] = $result[$k];
  283. }
  284. if (strpos($v, 'callback.') !== false) {
  285. $temp = explode('callback.', $v);
  286. $result['callback'][] = array($temp[1], $result[$v]);
  287. }
  288. }
  289. }
  290. return $result;
  291. }
  292. public function log($result, $name, $url, $param)
  293. {
  294. $insert['name'] = $name;
  295. $insert['url'] = $url;
  296. $insert['result'] = json_encode($result, JSON_UNESCAPED_UNICODE);
  297. $insert['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);
  298. Dever::db('main/log')->insert($insert);
  299. }
  300. /**
  301. * 拼装wechat需要的参数
  302. *
  303. * @return mixed
  304. */
  305. public function param($method, $param = array())
  306. {
  307. if (strpos($method, '.')) {
  308. $temp = explode('.', $method);
  309. $config = $this->config[$temp[0]][$temp[1]];
  310. } else {
  311. if (!isset($this->config[$method])) {
  312. return false;
  313. }
  314. $config = $this->config[$method];
  315. }
  316. if (!$param) {
  317. $param = array();
  318. }
  319. $param = $this->project + $param;
  320. //print_r($param);die;
  321. foreach ($config['param'] as $k => $v) {
  322. if ($v == 'token') {
  323. $config['url'] .= $k . '=' . $this->token();
  324. unset($config['param'][$k]);
  325. } elseif ($v == 'ticket') {
  326. $config['param'][$k] = $this->ticket();
  327. } elseif ($v == 'code') {
  328. $config['param'][$k] = $this->code();
  329. } elseif ($v == 'oauth') {
  330. if (!isset($param['oauth'])) {
  331. Dever::alert('oauth erorr');
  332. } elseif (is_numeric($param['oauth'])) {
  333. $oauth = $this->oauth($param['oauth']);
  334. $param['oauth'] = $oauth['value'];
  335. }
  336. $config['url'] .= $k . '=' . $param['oauth'];
  337. unset($config['param'][$k]);
  338. } elseif (!is_array($v) && isset($param[$v])) {
  339. $config['param'][$k] = $param[$v];
  340. } elseif($v) {
  341. $config['param'][$k] = $this->replace($v, $param);
  342. }
  343. }
  344. $config['json'] = isset($config['json']) && $config['json'] ? true : false;
  345. return $config;
  346. }
  347. /**
  348. * 替换{}
  349. *
  350. * @return mixed
  351. */
  352. public function replace($value, $param)
  353. {
  354. if (isset($param['domain']) && strpos($value, '{domain}') !== false) {
  355. foreach ($param['domain'] as $k => $v) {
  356. $param['domain'][$k] = str_replace('{domain}', $v, $value);
  357. }
  358. return $param['domain'];
  359. }
  360. return $value;
  361. }
  362. /**
  363. * 消息解密
  364. *
  365. * @return mixed
  366. */
  367. public function decode($sign, $timestamp, $nonce, $encrypt)
  368. {
  369. include(DEVER_PROJECT_PATH . 'example/wxBizMsgCrypt.php');
  370. $crypt = new \WXBizMsgCrypt($this->project['token'], $this->project['key'], $this->project['appid']);
  371. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  372. $xml = sprintf($format, $encrypt);
  373. $result = '';
  374. $code = $crypt->decryptMsg($sign, $timestamp, $nonce, $xml, $result);
  375. if ($code == 0) {
  376. libxml_disable_entity_loader(true);
  377. $result = (array) simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
  378. }
  379. return $result;
  380. }
  381. /**
  382. * 获取nonce
  383. *
  384. * @return mixed
  385. */
  386. public function nonce()
  387. {
  388. return substr(md5(microtime()), rand(10, 15));
  389. }
  390. /**
  391. * 获取signature
  392. *
  393. * @return mixed
  394. */
  395. public function signature($ticket, $url, $timestamp, $noncestr)
  396. {
  397. /*
  398. $info = array();
  399. $info['jsapi_ticket'] = $ticket;
  400. $info['url'] = $url;
  401. $info['timestamp'] = $timestamp;
  402. $info['noncestr'] = $noncestr;
  403. ksort($info);
  404. */
  405. $signature_string = "jsapi_ticket=$ticket&noncestr=$noncestr&timestamp=$timestamp&url=$url";
  406. //$signature_string = substr(http_build_query($info), 0, -1);
  407. return sha1($signature_string);
  408. }
  409. /**
  410. * 获取sign签名数据包
  411. *
  412. * @return mixed
  413. */
  414. public function sign($url)
  415. {
  416. $ticket = $this->ticket();
  417. if (!$url) {
  418. $url = Dever::url();
  419. } else {
  420. $url = htmlspecialchars_decode($url);
  421. }
  422. $timestamp = time();
  423. $noncestr = $this->nonce();
  424. $signature = $this->signature($ticket, $url, $timestamp, $noncestr);
  425. $sign = array();
  426. $sign['appId'] = $this->project['appid'];
  427. $sign['nonceStr'] = $noncestr;
  428. $sign['timestamp'] = $timestamp;
  429. $sign['url'] = $url;
  430. $sign['signature'] = $signature;
  431. return $sign;
  432. }
  433. }