Wechat.php 9.7 KB

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