Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

157 wiersze
4.0 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2017/7/5
  6. * Time: 17:31
  7. */
  8. namespace common\models;
  9. use backend\common\Utils;
  10. class AccessToken
  11. {
  12. private $corpId = "";
  13. private $secret = "";
  14. private $agentId; //应用ID
  15. private $appConfigs;
  16. /**
  17. * AccessToken构造器
  18. * @param [Number] $agentId 两种情况:1是传入字符串“txl”表示获取通讯录应用的Secret;2是传入应用的agentId
  19. */
  20. public function __construct($agentId)
  21. {
  22. $this->appConfigs = $this->loadConfig();
  23. $this->corpId = $this->appConfigs->CorpId;
  24. $this->agentId = $agentId;
  25. //由于通讯录是特殊的应用,需要单独处理
  26. if ($agentId == "txl") {
  27. $this->secret = 'JV0C-B1T6e19y66EhAt_aU7t8iWTVtUUsfxbiQSRU-g';
  28. $this->corpId = 'wwb1c5a9f70b4faecb';
  29. } else { //其他应用
  30. $config = $this->getConfigByAgentId($agentId);
  31. if ($config) {
  32. $this->secret = $config->Secret;
  33. }
  34. }
  35. }
  36. /**
  37. * User:Steven
  38. * Desc:给URL地址追加参数
  39. * @param $url
  40. * @param $key
  41. * @param $value
  42. * @return string
  43. */
  44. public function appendParamter($url, $key, $value)
  45. {
  46. return strrpos($url, "?", 0) > -1 ? "$url&$key=$value" : "$url?$key=$value";
  47. }
  48. /**
  49. * User:Steven
  50. * Desc:生成指定长度的随机字符串
  51. * @param int $length
  52. * @return string
  53. */
  54. function createNonceStr($length = 16)
  55. {
  56. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  57. $str = "";
  58. for ($i = 0; $i < $length; $i++) {
  59. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  60. }
  61. return $str;
  62. }
  63. /**
  64. * User:Steven
  65. * Desc:读取本地文件
  66. * @param $filename
  67. * @return string
  68. */
  69. function get_php_file($filename)
  70. {
  71. if (file_exists($filename)) {
  72. return trim(substr(file_get_contents($filename), 15));
  73. } else {
  74. return '{"expire_time":0}';
  75. }
  76. }
  77. /**
  78. * User:Steven
  79. * Desc:写入本地文件
  80. * @param $filename
  81. * @param $content
  82. */
  83. function set_php_file($filename, $content)
  84. {
  85. $fp = fopen($filename, "w");
  86. fwrite($fp, "<?php exit();?>" . $content);
  87. fclose($fp);
  88. }
  89. /**
  90. * User:Steven
  91. * Desc:加载本地的应用配置文件
  92. * @return mixed
  93. */
  94. function loadConfig()
  95. {
  96. return json_decode($this->get_php_file(\Yii::getAlias('@backend') . "/config/wechat/config.php"));
  97. }
  98. /**
  99. * User:Steven
  100. * Desc:根据应用ID获取应用配置
  101. * @param $id
  102. * @return mixed
  103. */
  104. public function getConfigByAgentId($id)
  105. {
  106. $configs = $this->loadConfig();
  107. foreach ($configs->AppsConfig as $key => $value) {
  108. if ($value->AgentId == $id) {
  109. $config = $value;
  110. break;
  111. }
  112. }
  113. return $config;
  114. }
  115. /**
  116. * User:Steven
  117. * Desc:获取access_token
  118. * @return mixed
  119. */
  120. public function getAccessToken()
  121. {
  122. //由于实际使用过程中不同的应用会产生不同的token,所以示例按照agentId做为文件名进行存储
  123. $path = \Yii::getAlias('@backend') . "/config/wechat/$this->agentId.php";
  124. $data = json_decode($this->get_php_file($path));
  125. if ($data->expire_time < time()) {
  126. $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->corpId&corpsecret=$this->secret";
  127. $res = json_decode(Utils::http_get($url)["content"]);
  128. $access_token = $res->access_token;
  129. $data = (object)$data;
  130. if ($access_token) {
  131. $data->expire_time = time() + 7000;
  132. $data->access_token = $access_token;
  133. $this->set_php_file($path, json_encode($data));
  134. }
  135. } else {
  136. $access_token = $data->access_token;
  137. }
  138. return $access_token;
  139. }
  140. }