[关闭]
@Chiang 2021-10-02T17:14:43.000000Z 字数 5922 阅读 907

企业微信打开已有群聊并发送消息

企业微信 2021-10


demo

  1. <?php
  2. function createNonceStr($length = 16) {
  3. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  4. $str = "";
  5. for ($i = 0; $i < $length; $i++) {
  6. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  7. }
  8. return $str;
  9. }
  10. function http_get($url) {
  11. $curl = curl_init();
  12. curl_setopt($curl, CURLOPT_URL, $url);
  13. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  15. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  16. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  17. $res = curl_exec($curl);
  18. curl_close($curl);
  19. return $res;
  20. }
  21. // 获取access_token
  22. // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=corpid&corpsecret=corpsecret";
  23. // 应用secret
  24. // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=corpid&corpsecret=corpsecret";
  25. // $response = http_get($url);
  26. // $jsoninfo = json_decode($response, true);
  27. // echo "<pre>";
  28. // var_dump($jsoninfo);die;
  29. /*
  30. array(4) {
  31. ["errcode"]=>
  32. int(0)
  33. ["errmsg"]=>
  34. string(2) "ok"
  35. ["access_token"]=>
  36. string(214) "hD_6Is2FDFizoxeSrNVq2xfLS8JXWCZAqUTXBLOPM2g4TZPe_dptVBGnZ8tH_khhQ-BTFJrd5-vp7kX2azXbug_j-XJHNriG6_UO77awAOrCcwj6unRmKsXhiSaXsx40F7etkWdQJx6W0hor-1JLlOkYleTLew4P1aRVFrTSDPVCQ4Z4NWXGjfYN4m-lTHNQ_5Fh_fK3fLtDkYHBV8upwg"
  37. ["expires_in"]=>
  38. int(7200)
  39. }
  40. */
  41. $access_token = "hD_6Is2FDFizoxeSrNVq2xfLS8JXWCZAqUTXBLOPM2g4TZPe_dptVBGnZ8tH_khhQ-BTFJrd5-vp7kX2azXbug_j-XJHNriG6_UO77awAOrCcwj6unRmKsXhiSaXsx40F7etkWdQJx6W0hor-1JLlOkYleTLew4P1aRVFrTSDPVCQ4Z4NWXGjfYN4m-lTHNQ_5Fh_fK3fLtDkYHBV8upwg";
  42. /*
  43. array(4) {
  44. ["errcode"]=>
  45. int(0)
  46. ["errmsg"]=>
  47. string(2) "ok"
  48. ["access_token"]=>
  49. string(214) "NeIN8z19iZA9eCkIXxngtUOD1-27TwhDeu7iERqzPK3sIt6ScnLsDiNYrL1XhrPkCfsYgDubOwM9saP42BFqEw035yM_lXN4KGLCjGkzwYMnFIua5AGsTII-G72Htvdgs7fIdDWvlNL1rLqg0t-sLlRIXX-_At_5DW4dJXbGK4QFGqUZWkxyhn5zzNbSArsj5CRYmSF0Q9jNs9oJffFkRA"
  50. ["expires_in"]=>
  51. int(7200)
  52. }
  53. */
  54. $access_tokenyy = "54K1fRAkPpQD-c_J7KBsdvLb34Pk7c1seyBpsyhFhMhbxrJNpf-0So-J4BzaydnEXFDrouEu9ZDDRUb39TT_lHf9bk3de5SB82AE9sWXbZrFeUTs6HGqn3Dd5q7cBojOZ57S-kmZGb4RipZ0p4hq8XISr3tGLM9aqxv2_3-pnqiXBgZG7rKXoPisz0QIp5bTE6d7yhrMjZO8hjh5ljarNg";
  55. //参考文档:https://work.weixin.qq.com/api/doc/90000/90136/90506
  56. //获取企业的jsapi_ticket
  57. // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$access_token";
  58. // $response = http_get($url);
  59. // $jsoninfo = json_decode($response, true);
  60. // echo "<pre>";
  61. // var_dump($jsoninfo);die;
  62. /*
  63. array(4) {
  64. ["errcode"]=>
  65. int(0)
  66. ["errmsg"]=>
  67. string(2) "ok"
  68. ["ticket"]=>
  69. string(86) "sM4AOVdWfPE4DxkXGEs8VB4023ffWqYKtJMsCkairdH61Hzb6ZP_KijzeYqKxxXms65JmSpQNC6HDyUzQdiRMA"
  70. ["expires_in"]=>
  71. int(7200)
  72. }
  73. */
  74. $jsapiTicketweb = "sM4AOVdWfPE4DxkXGEs8VB4023ffWqYKtJMsCkairdH61Hzb6ZP_KijzeYqKxxXms65JmSpQNC6HDyUzQdiRMA";
  75. //获取应用的jsapi_ticket
  76. // $url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=$access_tokenyy&type=agent_config";
  77. // $response = http_get($url);
  78. // $jsoninfo = json_decode($response, true);
  79. // echo "<pre>";
  80. // var_dump($jsoninfo);die;
  81. /*
  82. array(4) {
  83. ["errcode"]=>
  84. int(0)
  85. ["errmsg"]=>
  86. string(2) "ok"
  87. ["ticket"]=>
  88. string(24) "iqTxs+T4mCtEaQ5OAOYibQ=="
  89. ["expires_in"]=>
  90. int(7200)
  91. }
  92. */
  93. $jsapiTicket = "SzjxS+qa3voGix3YB4YJZQ==";
  94. $url = "http://zzdemo.zbserp.com/fazhansdk.php";
  95. $cropId = 'wxec82c081d811bbf1';
  96. $timestamp = time();
  97. $nonceStr = createNonceStr();
  98. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  99. $stringweb = "jsapi_ticket=$jsapiTicketweb&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  100. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  101. $signatureweb = sha1($stringweb);
  102. $signature = sha1($string);
  103. ?>
  104. <!DOCTYPE html>
  105. <html>
  106. <head>
  107. <title>title</title>
  108. <!-- <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  109. <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
  110. <script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script> -->
  111. <script src="https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js"></script>
  112. </head>
  113. <body>
  114. <br>
  115. <br>
  116. <br>
  117. <button
  118. id="dianji"
  119. title="title1"
  120. desc="desc1"
  121. url="https://work.weixin.qq.com/api/doc/90000/90136/94549"
  122. imgUrl="https://wwcdn.weixin.qq.com/node/wwnl/wwnl/style/images/independent/index/v3style/WxContactPic1$df4a387c.png"
  123. chatId="wrCEskDwAAss0HDLkCt6WqFdFbh4FamQ"
  124. >点击</button>
  125. <script type="text/javascript">
  126. // wx.config({
  127. // beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  128. // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  129. // appId: '<?= $cropId; ?>',
  130. // timestamp: '<?= $timestamp; ?>',
  131. // nonceStr: '<?= $nonceStr; ?>',
  132. // signature: '<?= $signatureweb; ?>',
  133. // jsApiList: [
  134. // 'openExistedChatWithMsg'
  135. // ]
  136. // });
  137. // 阿亮chatID: wrCEskDwAAYxY_6e_joqCxzxCiJOKqUQ
  138. // 发展chatID: wrCEskDwAAss0HDLkCt6WqFdFbh4FamQ
  139. wx.agentConfig({
  140. // beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  141. // debug: true,
  142. corpid: '<?= $cropId; ?>', // 必填,企业微信的corpid,必须与当前登录的企业一致
  143. agentid: '1000014', // 必填,企业微信的应用id (e.g. 1000247)
  144. timestamp: '<?= $timestamp; ?>', // 必填,生成签名的时间戳
  145. nonceStr: '<?= $nonceStr; ?>', // 必填,生成签名的随机串
  146. signature: '<?= $signature; ?>',// 必填,签名,见附录-JS-SDK使用权限签名算法
  147. jsApiList: ['openExistedChatWithMsg'], //必填,传入需要使用的接口名称
  148. success: function(res) {
  149. // 回调
  150. // document.querySelector("#dianji").onclick = function(){
  151. document.getElementById("dianji").onclick = function(){
  152. var title = this.getAttribute("title");
  153. var desc = this.getAttribute("desc");
  154. var url = this.getAttribute("url");
  155. var imgUrl = this.getAttribute("imgUrl");
  156. var chatId = this.getAttribute("chatId");
  157. wx.invoke("openExistedChatWithMsg", {
  158. chatId: chatId,
  159. msg: {
  160. msgtype: "link",
  161. link: {
  162. title: title,
  163. desc: desc,
  164. url: url,
  165. imgUrl: imgUrl
  166. }
  167. }
  168. },function(res){
  169. if (res.err_msg == "openExistedChatWithMsg:ok")
  170. {
  171. alert('fazhanok');
  172. }else{
  173. alert(res.err_msg);
  174. }
  175. }
  176. );
  177. };
  178. },
  179. fail: function(res) {
  180. if(res.errMsg.indexOf('function not exist') > -1){
  181. alert('版本过低请升级')
  182. }else{
  183. alert(res.errMsg);
  184. }
  185. }
  186. });
  187. // wx.ready(function () {
  188. // document.querySelector("#dianji").onclick = function(){
  189. // console.log('asdf');
  190. // wx.invoke("openExistedChatWithMsg", {
  191. // chatId: "wrCEskDwAAss0HDLkCt6WqFdFbh4FamQ"
  192. // // msg: {
  193. // // msgtype: "link",
  194. // // link: {
  195. // // title: "title1",
  196. // // desc: "desc1",
  197. // // url: "link1",
  198. // // imgUrl: "imgurl1"
  199. // // }
  200. // // }
  201. // },function(res){
  202. // if (res.err_msg == "openExistedChatWithMsg:ok")
  203. // {
  204. // // alert('fazhanok');
  205. // }
  206. // }
  207. // );
  208. // };
  209. // });
  210. </script>
  211. </body>
  212. </html>

参考资料:
打开已有群聊并发送消息
苹果手机调用wx.agentConfig无反应?已开启debug模式

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注