WechatEnterprise.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace addons\qingdongams\library;
  3. use addons\qingdongams\model\AdminConfig;
  4. use addons\qingdongams\model\Staff;
  5. use EasyWeChat\Factory;
  6. use think\Cache;
  7. use think\Model;
  8. use fast\Http;
  9. /**
  10. * 企业微信
  11. */
  12. class WechatEnterprise
  13. {
  14. public $config;
  15. public $error;
  16. protected $app;
  17. public $corpid;
  18. public $corpsecret;
  19. public function __construct()
  20. {
  21. $this->corpid = AdminConfig::getConfigValue('corpid', 'wechat');
  22. $this->corpsecret = AdminConfig::getConfigValue('corpsecret', 'wechat');
  23. }
  24. //获取企业微信userid
  25. public function userid($mobile)
  26. {
  27. $user = array(
  28. 'mobile' => $mobile,
  29. );
  30. $access_token=$this->get_token();
  31. $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=' . $access_token;
  32. $customer = $this->http_post($url, json_encode($user));
  33. $result = json_decode($customer, true);
  34. if ($result['errcode'] == 0) {
  35. $staffinfo = Staff::where(['mobile' => $mobile])->find();
  36. if (!$staffinfo) {
  37. $this->error = '员工手机号不存在';
  38. return false;
  39. }
  40. Staff::where(['mobile' => $mobile])->update(['touser' => $result['userid']]);
  41. return $result['userid'];
  42. }
  43. return false;
  44. }
  45. //获取token
  46. public function get_token()
  47. {
  48. $access_token = Cache::get('enterprise_access_token');
  49. if (empty($access_token)) {
  50. $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $this->corpid . '&corpsecret=' . $this->corpsecret;
  51. $token = $this->http_get($url);
  52. $token = json_decode($token, true);
  53. if ($token['errcode'] != 0) {
  54. return false;
  55. }
  56. $access_token = $token['access_token'];
  57. $this->access_token = $token['access_token'];
  58. Cache::set('enterprise_access_token', $access_token, 6000);
  59. }
  60. return $access_token;
  61. }
  62. /**
  63. * curl请求
  64. */
  65. private function http_post($url, $param, $post_file = false)
  66. {
  67. $oCurl = curl_init();
  68. if (stripos($url, "https://") !== FALSE) {
  69. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  70. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  71. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  72. }
  73. if (is_string($param) || $post_file) {
  74. $strPOST = $param;
  75. } else {
  76. $aPOST = array();
  77. foreach ($param as $key => $val) {
  78. $aPOST[] = $key . "=" . urlencode($val);
  79. }
  80. $strPOST = join("&", $aPOST);
  81. }
  82. curl_setopt($oCurl, CURLOPT_URL, $url);
  83. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  84. curl_setopt($oCurl, CURLOPT_POST, true);
  85. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
  86. $sContent = curl_exec($oCurl);
  87. $aStatus = curl_getinfo($oCurl);
  88. curl_close($oCurl);
  89. if (intval($aStatus["http_code"]) == 200) {
  90. return $sContent;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * 发送模板
  97. */
  98. public function sendTemplate($touser,$data)
  99. {
  100. $miniAppid = AdminConfig::getConfigValue('mini_appid', 'wechat');
  101. $web_url = AdminConfig::getConfigValue('web_url', 'wechat');
  102. $agentid = AdminConfig::getConfigValue('agentid', 'wechat');
  103. if (!empty($miniAppid)) {//打开小程序页面
  104. $user = array(
  105. 'touser' => $touser,
  106. 'msgtype' => 'miniprogram_notice',
  107. 'miniprogram_notice' => array(
  108. 'appid' => $miniAppid,
  109. "page" => 'pages/news/theReminder/theReminder',
  110. "emphasis_first_item" => false,
  111. "title" => $data['title'],
  112. "description" => $data['description'],
  113. "content_item" => $data['content_item'],
  114. ),
  115. "enable_id_trans" => 0,
  116. "enable_duplicate_check" => 0,
  117. "duplicate_check_interval" => 0
  118. );
  119. } else {
  120. $user = array(
  121. 'touser' => $touser,
  122. 'msgtype' => 'textcard',
  123. "agentid" => $agentid,
  124. "title" => $data['title'],
  125. "description" => $data['description'],
  126. "url" => $web_url . '/pages/news/theReminder/theReminder',
  127. "enable_id_trans" => 0,
  128. "enable_duplicate_check" => 0,
  129. "duplicate_check_interval" => 0
  130. );
  131. }
  132. $url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' . $this->get_token();
  133. $result = $this->httpPost($url, $user);
  134. $result = json_decode($result, true);
  135. if ($result['errcode'] != 0) {
  136. $this->error = $result['errmsg'];
  137. return false;
  138. }
  139. return true;
  140. }
  141. /**
  142. * post请求
  143. * @param $url
  144. * @param string $param
  145. * @return bool|string
  146. */
  147. public function httpPost($url, $param = '')
  148. {
  149. if (empty($url) || empty($param)) {
  150. return false;
  151. }
  152. if (is_array($param)) {
  153. $param = json_encode($param);
  154. }
  155. $postUrl = $url;
  156. $curlPost = $param;
  157. $ch = curl_init(); //初始化curl
  158. curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
  159. curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
  160. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
  161. curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
  162. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  163. $headers = array();
  164. $headers[] = 'Content-Type: text/plain';
  165. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  166. $result = curl_exec($ch); //运行curl
  167. curl_close($ch);
  168. return $result;
  169. }
  170. /**
  171. * curl请求
  172. */
  173. private function http_get($url)
  174. {
  175. $oCurl = curl_init();
  176. if (stripos($url, "https://") !== FALSE) {
  177. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  178. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  179. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  180. }
  181. curl_setopt($oCurl, CURLOPT_URL, $url);
  182. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  183. $sContent = curl_exec($oCurl);
  184. $aStatus = curl_getinfo($oCurl);
  185. curl_close($oCurl);
  186. if (intval($aStatus["http_code"]) == 200) {
  187. return $sContent;
  188. } else {
  189. return false;
  190. }
  191. }
  192. }