Wechat.php 9.6 KB

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