Wechat.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. * config
  19. *
  20. * @var array
  21. */
  22. private $token;
  23. /**
  24. * project
  25. *
  26. * @var string
  27. */
  28. private $project;
  29. /**
  30. * 构造函数 初始化
  31. *
  32. * @return mixed
  33. */
  34. public function __construct($type = 1, $project = false)
  35. {
  36. $this->config = Dever::config('wechat')->cAll;
  37. if (!$project) {
  38. $appid = Dever::input('appid');
  39. if ($appid) {
  40. $project = Dever::db('main/project')->one(array('option_type' => $type, 'option_appid' => $appid));
  41. }
  42. if (!$project) {
  43. $project = Dever::input('project', 1);
  44. }
  45. }
  46. if (!$project) {
  47. Dever::alert('project is not exits!');
  48. }
  49. if (is_numeric($project)) {
  50. $project = Dever::db('main/project')->one($project);
  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. *
  75. * @return mixed
  76. */
  77. private function save($type = 'ticket', $value, $expires = false, $interval = 2000)
  78. {
  79. if (strpos($type, '.')) {
  80. $temp = explode('.', $type);
  81. $table = 'main/' . $temp[1];
  82. } else {
  83. $table = 'main/' . $type;
  84. }
  85. $db = Dever::db($table);
  86. $where['option_project_id'] = $this->project['id'];
  87. $info = $db->one($where);
  88. $update = false;
  89. if ($info && time() - $info['mdate'] >= $info['expires']) {
  90. $update = true;
  91. } elseif($info) {
  92. return $info['value'];
  93. }
  94. if (!$info) {
  95. $update = false;
  96. }
  97. if (!$value) {
  98. $value = $this->param($type);
  99. }
  100. $data = array();
  101. if (is_array($value) || (is_string($value) && strstr($value, 'http://'))) {
  102. $result = $this->curl(false, $value);
  103. $key = $type;
  104. if ($type == 'token') {
  105. $key = 'access_token';
  106. }
  107. if ($result && isset($result[$key])) {
  108. $data = $result;
  109. $data['value'] = $result[$key];
  110. $data['expires'] = $result['expires_in'];
  111. }
  112. } else {
  113. $data['value'] = $value;
  114. $data['expires'] = $expires;
  115. }
  116. if ($data && isset($data['value']) && $data['value']) {
  117. $data['project_id'] = $this->project['id'];
  118. $expires = $data['expires'] - $interval;
  119. if ($expires <= 0) {
  120. $data['expires'] = $data['expires'] - 300;
  121. } else {
  122. $data['expires'] = $expires;
  123. }
  124. if ($update == true) {
  125. $data['where_id'] = $info['id'];
  126. $db->update($data);
  127. } else {
  128. $db->insert($data);
  129. }
  130. } elseif($info['value']) {
  131. $data['value'] = $info['value'];
  132. }
  133. return $data['value'];
  134. }
  135. /**
  136. * 获取最新的token
  137. *
  138. * @return mixed
  139. */
  140. public function token($value = false, $expires = false, $interval = 2000)
  141. {
  142. return $this->save('token', $value, $expires, $interval);
  143. }
  144. /**
  145. * 获取最新的ticket
  146. *
  147. * @return mixed
  148. */
  149. public function ticket($value = false, $expires = false, $interval = 200)
  150. {
  151. return $this->save('ticket', $value, $expires, $interval);
  152. }
  153. /**
  154. * 获取最新的oauth token
  155. *
  156. * @return mixed
  157. */
  158. public function oauth($value = false, $expires = false, $interval = 200)
  159. {
  160. return $this->save('oauth.oauth', $value, $expires, $interval);
  161. }
  162. /**
  163. * 获取最新的code
  164. *
  165. * @return mixed
  166. */
  167. public function code($value = false, $expires = false, $interval = 200)
  168. {
  169. return $this->save('oauth.code', $value, $expires, $interval);
  170. }
  171. /**
  172. * 获取最新的oauth login地址
  173. *
  174. * @return mixed
  175. */
  176. public function login($callback = false, $code = false, $location = true)
  177. {
  178. if (!$code) {
  179. $code = $this->code();
  180. }
  181. $callback = urlencode(Dever::url($callback));
  182. $config = $this->param('oauth.login', array('code' => $code, 'redirect' => $callback));
  183. $url = $config['url'] . http_build_query($config['param']);
  184. if ($location == true) {
  185. Dever::location($url);
  186. }
  187. return $url;
  188. }
  189. /**
  190. * 从wechat获取数据
  191. *
  192. * @return mixed
  193. */
  194. public function curl($method, $param = array())
  195. {
  196. if (is_string($param)) {
  197. $result = json_decode(Dever::curl($param), true);
  198. } else {
  199. if ($method) {
  200. $param = $this->param($method, $param);
  201. }
  202. $result = json_decode(Dever::curl($param['url'], $param['param'], $param['method'], $param['json']), true);
  203. }
  204. if (isset($result['errcode']) && $result['errcode'] != 0) {
  205. $result = $param + $result;
  206. Dever::log($result);
  207. Dever::alert(json_encode($result));
  208. }
  209. if (isset($param['response'])) {
  210. foreach ($param['response'] as $k => $v) {
  211. if (strpos($k, '.')) {
  212. $temp = explode('.', $k);
  213. if (isset($result[$temp[0]][$temp[1]])) {
  214. $result[$v] = $result[$temp[0]][$temp[1]];
  215. }
  216. } elseif (isset($result[$k])) {
  217. $result[$v] = $result[$k];
  218. }
  219. }
  220. }
  221. return $result;
  222. }
  223. /**
  224. * 拼装wechat需要的参数
  225. *
  226. * @return mixed
  227. */
  228. public function param($method, $param = array())
  229. {
  230. if (strpos($method, '.')) {
  231. $temp = explode('.', $method);
  232. $config = $this->config[$temp[0]][$temp[1]];
  233. } else {
  234. if (!isset($this->config[$method])) {
  235. return false;
  236. }
  237. $config = $this->config[$method];
  238. }
  239. $param = $this->project + $param;
  240. foreach ($config['param'] as $k => $v) {
  241. if ($v == 'token') {
  242. $config['url'] .= $k . '=' . $this->token();
  243. unset($config['param'][$k]);
  244. } elseif ($v == 'ticket') {
  245. $config['param'][$k] = $this->ticket();
  246. } elseif ($v == 'code') {
  247. $config['param'][$k] = $this->code();
  248. }elseif ($v == 'oauth') {
  249. $config['param'][$k] = $this->oauth();
  250. } elseif (isset($param[$v]) && $param[$v]) {
  251. $config['param'][$k] = $param[$v];
  252. } elseif($v) {
  253. $config['param'][$k] = $v;
  254. }
  255. }
  256. $config['json'] = isset($config['json']) && $config['json'] ? true : false;
  257. return $config;
  258. }
  259. /**
  260. * 消息解密
  261. *
  262. * @return mixed
  263. */
  264. public function decode($sign, $timestamp, $nonce, $encrypt)
  265. {
  266. include(DEVER_PROJECT_PATH . 'example/wxBizMsgCrypt.php');
  267. $crypt = new \WXBizMsgCrypt($this->project['token'], $this->project['key'], $this->project['appid']);
  268. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  269. $xml = sprintf($format, $encrypt);
  270. $result = '';
  271. $code = $crypt->decryptMsg($sign, $timestamp, $nonce, $xml, $result);
  272. if ($code == 0) {
  273. libxml_disable_entity_loader(true);
  274. $result = (array) simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
  275. }
  276. return $result;
  277. }
  278. /**
  279. * 获取nonce
  280. *
  281. * @return mixed
  282. */
  283. public function nonce()
  284. {
  285. return substr(md5(microtime()), rand(10, 15));
  286. }
  287. /**
  288. * 获取signature
  289. *
  290. * @return mixed
  291. */
  292. public function signature($ticket, $url, $timestamp, $noncestr)
  293. {
  294. $info = array();
  295. $info['jsapi_ticket'] = $ticket;
  296. $info['url'] = $url;
  297. $info['timestamp'] = $timestamp;
  298. $info['noncestr'] = $noncestr;
  299. ksort($info);
  300. $signature_string = substr(http_build_query($info), 0, -1);
  301. return sha1($signature_string);
  302. }
  303. }