Work.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace wx\work;
  3. use think\facade\Cache;
  4. class Work
  5. {
  6. public $accesstoken;
  7. public $corpid;
  8. public $corpsecret;
  9. public function __construct($corpid, $corpsecret)
  10. {
  11. $this->corpid = $this->corpid;
  12. $this->corpsecret = $this->corpsecret;
  13. $this->accessToken = $this->setAccessToken($corpid, $corpsecret);
  14. }
  15. /**
  16. * 获取普通token
  17. * @param $corpid 企业ID
  18. * @param $corpsecret 应用的凭证密钥
  19. * @return mixed
  20. */
  21. public function setAccessToken($corpid, $corpsecret)
  22. {
  23. if (Cache::has($corpid . '_' . $corpsecret)) {
  24. return Cache::get($corpid . '_' . $corpsecret);
  25. }
  26. $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $corpid . '&corpsecret=' . $corpsecret;
  27. $res = $this->curl($url);
  28. $res = json_decode($res, true);
  29. if (isset($res['errcode']) && $res['errcode'] != 0) {
  30. trace('access_token获取失败'.json_encode($res), 'error');
  31. return null;
  32. }
  33. Cache::set($corpid . '_' . $corpsecret, $res['access_token'], '7200');
  34. return $res['access_token'];
  35. }
  36. /**
  37. * 获取企业应用的jsapi_ticket
  38. */
  39. public function getAgentJsapiTicket()
  40. {
  41. $url = 'https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=' . $this->accessToken . '&type=agent_config';
  42. $res = $this->curl($url);
  43. $res = json_decode($res, true);
  44. if (isset($res['errcode']) && $res['errcode'] != 0) {
  45. trace('jsapi_ticket获取失败:'.json_encode($res), 'error');
  46. return null;
  47. }
  48. Cache::set($this->corpid . '_' . $this->corpsecret . '_agent_jsapi_ticket', $res['ticket']);
  49. return $res['ticket'];
  50. }
  51. /**
  52. * 获取企业的jsapi_ticket
  53. */
  54. public function getJsapiTicket()
  55. {
  56. $url = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=' . $this->accessToken;
  57. $res = $this->curl($url);
  58. $res = json_decode($res, true);
  59. if (isset($res['errcode']) && $res['errcode'] != 0) {
  60. return null;
  61. }
  62. Cache::set($this->corpid . '_' . $this->corpsecret . '_jsapi_ticket', $res['ticket']);
  63. return $res['ticket'];
  64. }
  65. /**
  66. * 签名
  67. */
  68. public function sign($data)
  69. {
  70. $str = 'jsapi_ticket='.$data['jsapi_ticket'].'&noncestr='.$data['noncestr'].'&timestamp='.$data['timestamp'].'&url='.$data['url'];
  71. return sha1($str);
  72. }
  73. /*
  74. * 获取企业微信用户UserId
  75. */
  76. public function getUserId($code)
  77. {
  78. $getUserId = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$this->accessToken."&code=".$code;
  79. $res = $this->curl($getUserId);
  80. $res = json_decode($res, true);
  81. if (isset($res['errcode']) && $res['errcode'] != 0) {
  82. trace('UserId获取失败'.json_encode($res), 'error');
  83. return null;
  84. }
  85. if (!isset($res['UserId'])) {
  86. trace('非企业成员获取失败'.json_encode($res), 'error');
  87. return null;
  88. }
  89. return $res['UserId'];
  90. }
  91. /*
  92. * 获取企业微信用户信息
  93. */
  94. public function getUserInfo($UserId)
  95. {
  96. $getUserInfo = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=".$this->accessToken."&userid=".$UserId;
  97. $res = $this->curl($getUserInfo);
  98. $res = json_decode($res, true);
  99. if (isset($res['errcode']) && $res['errcode'] != 0) {
  100. trace('企业微信信息获取失败'.json_encode($res), 'error');
  101. return null;
  102. }
  103. return $res;
  104. }
  105. /**
  106. * 请求接口
  107. * @param $url
  108. * @param null $data
  109. * @return mixed|null
  110. */
  111. protected function curl($url, $data = null)
  112. {
  113. $curl = curl_init();
  114. curl_setopt($curl, CURLOPT_URL, $url);
  115. curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type: application/json; encoding=utf-8']);
  116. if (!empty($data)) {
  117. curl_setopt($curl, CURLOPT_POST, 1);
  118. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  119. }
  120. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  121. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  122. curl_setopt($curl, CURLOPT_HEADER, false);
  123. $data = curl_exec($curl);
  124. curl_close($curl);
  125. return $data;
  126. }
  127. }