Wechat.php 9.4 KB

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