Wechat.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 ($result && isset($result[$key])) {
  105. $data = $result;
  106. $data['value'] = $result[$key];
  107. $data['expires'] = $result['expires_in'];
  108. }
  109. } else {
  110. $data['value'] = $value;
  111. $data['expires'] = $expires;
  112. }
  113. if ($data && isset($data['value']) && $data['value']) {
  114. $data['project_id'] = $this->project['id'];
  115. $expires = $data['expires'] - $interval;
  116. if ($expires <= 0) {
  117. $data['expires'] = $data['expires'] - 300;
  118. } else {
  119. $data['expires'] = $expires;
  120. }
  121. if ($update == true) {
  122. $data['where_id'] = $info['id'];
  123. $db->update($data);
  124. } else {
  125. $db->insert($data);
  126. }
  127. } elseif($info && $info['value']) {
  128. $data['value'] = $info['value'];
  129. }
  130. return $data['value'];
  131. }
  132. /**
  133. * 获取最新的token
  134. *
  135. * @return mixed
  136. */
  137. public function token($value = false, $expires = false, $interval = 2000)
  138. {
  139. return $this->save('token', $value, $expires, $interval);
  140. }
  141. /**
  142. * 获取最新的ticket
  143. *
  144. * @return mixed
  145. */
  146. public function ticket($value = false, $expires = false, $interval = 200)
  147. {
  148. return $this->save('ticket', $value, $expires, $interval);
  149. }
  150. /**
  151. * 获取最新的oauth token
  152. *
  153. * @return mixed
  154. */
  155. public function oauth($value = false, $expires = false, $interval = 200)
  156. {
  157. return $this->save('oauth.oauth', $value, $expires, $interval);
  158. }
  159. /**
  160. * 获取最新的code
  161. *
  162. * @return mixed
  163. */
  164. public function code($value = false, $expires = false, $interval = 200)
  165. {
  166. return $this->save('oauth.code', $value, $expires, $interval);
  167. }
  168. /**
  169. * 获取最新的oauth login地址
  170. *
  171. * @return mixed
  172. */
  173. public function login($callback = false, $code = false, $location = true)
  174. {
  175. if (!$code) {
  176. $code = $this->code();
  177. }
  178. $callback = Dever::url($callback);
  179. $config = $this->param('oauth.login', array('code' => $code, 'redirect' => $callback));
  180. $url = $config['url'] . http_build_query($config['param']);
  181. if ($location == true) {
  182. Dever::location($url);
  183. }
  184. return $url;
  185. }
  186. /**
  187. * 从wechat获取数据
  188. *
  189. * @return mixed
  190. */
  191. public function curl($method, $param = array())
  192. {
  193. if (is_string($param)) {
  194. $result = json_decode(Dever::curl($param), true);
  195. } else {
  196. if ($method) {
  197. $param = $this->param($method, $param);
  198. }
  199. $result = json_decode(Dever::curl($param['url'], $param['param'], $param['method'], $param['json']), true);
  200. }
  201. if (isset($result['errcode']) && $result['errcode'] != 0) {
  202. $result = $param + $result;
  203. Dever::log($result);
  204. Dever::alert(json_encode($result));
  205. }
  206. if (isset($param['response'])) {
  207. foreach ($param['response'] as $k => $v) {
  208. if (strpos($k, '.')) {
  209. $temp = explode('.', $k);
  210. if (isset($result[$temp[0]][$temp[1]])) {
  211. $result[$v] = $result[$temp[0]][$temp[1]];
  212. }
  213. } elseif (isset($result[$k])) {
  214. $result[$v] = $result[$k];
  215. }
  216. }
  217. }
  218. return $result;
  219. }
  220. /**
  221. * 拼装wechat需要的参数
  222. *
  223. * @return mixed
  224. */
  225. public function param($method, $param = array())
  226. {
  227. if (strpos($method, '.')) {
  228. $temp = explode('.', $method);
  229. $config = $this->config[$temp[0]][$temp[1]];
  230. } else {
  231. if (!isset($this->config[$method])) {
  232. return false;
  233. }
  234. $config = $this->config[$method];
  235. }
  236. $param = $this->project + $param;
  237. foreach ($config['param'] as $k => $v) {
  238. if ($v == 'token') {
  239. $config['url'] .= $k . '=' . $this->token();
  240. unset($config['param'][$k]);
  241. } elseif ($v == 'ticket') {
  242. $config['param'][$k] = $this->ticket();
  243. } elseif ($v == 'code') {
  244. $config['param'][$k] = $this->code();
  245. }elseif ($v == 'oauth') {
  246. $config['param'][$k] = $this->oauth();
  247. } elseif (isset($param[$v]) && $param[$v]) {
  248. $config['param'][$k] = $param[$v];
  249. } elseif($v) {
  250. $config['param'][$k] = $v;
  251. }
  252. }
  253. $config['json'] = isset($config['json']) && $config['json'] ? true : false;
  254. return $config;
  255. }
  256. /**
  257. * 消息解密
  258. *
  259. * @return mixed
  260. */
  261. public function decode($sign, $timestamp, $nonce, $encrypt)
  262. {
  263. include(DEVER_PROJECT_PATH . 'example/wxBizMsgCrypt.php');
  264. $crypt = new \WXBizMsgCrypt($this->project['token'], $this->project['key'], $this->project['appid']);
  265. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  266. $xml = sprintf($format, $encrypt);
  267. $result = '';
  268. $code = $crypt->decryptMsg($sign, $timestamp, $nonce, $xml, $result);
  269. if ($code == 0) {
  270. libxml_disable_entity_loader(true);
  271. $result = (array) simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
  272. }
  273. return $result;
  274. }
  275. /**
  276. * 获取nonce
  277. *
  278. * @return mixed
  279. */
  280. public function nonce()
  281. {
  282. return substr(md5(microtime()), rand(10, 15));
  283. }
  284. /**
  285. * 获取signature
  286. *
  287. * @return mixed
  288. */
  289. public function signature($ticket, $url, $timestamp, $noncestr)
  290. {
  291. $info = array();
  292. $info['jsapi_ticket'] = $ticket;
  293. $info['url'] = $url;
  294. $info['timestamp'] = $timestamp;
  295. $info['noncestr'] = $noncestr;
  296. ksort($info);
  297. $signature_string = substr(http_build_query($info), 0, -1);
  298. return sha1($signature_string);
  299. }
  300. }