PHP企业微信生成agentConfig注入应用的权限

2023-01-03   阅读:1230   分类:后端    标签: 企业微信

PHP企业微信生成agentConfig注入应用的权限步骤

1、调用 wx.agentConfig需要引入 jwxwork sdk

<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>

2、配置文件:

wx.agentConfigwx.agentConfig({
  corpid: '', // 必填,企业微信的corpid,必须与当前登录的企业一致
  agentid: '', // 必填,企业微信的应用id (e.g. 1000247)
  timestamp: , // 必填,生成签名的时间戳
  nonceStr: '', // 必填,生成签名的随机串
  signature: '',// 必填,签名,见附录-JS-SDK使用权限签名算法
  jsApiList: ['selectExternalContact'], //必填,传入需要使用的接口名称
	success: function(res) {
    // 回调
  },
  fail: function(res) {
    if(res.errMsg.indexOf('function not exist') > -1){
      alert('版本过低请升级')
    }
  }
});

3、以上配置获取前提:

(1)获取accessToken

(2)获取ticket

(3)获取当前URL

(4)获取当前时间戳:timestamp

(5)获取签名:signature签名生成规则如下:

参与签名的参数有四个: noncestr(随机字符串), jsapi_ticket(如何获取参考“获取企业jsapi_ticket”以及“获取应用的jsapi_ticket接口”), timestamp(时间戳), url(当前网页的URL, 不包含#及其后面部分) 将这些参数使用URL键值对的格式 (即 key1=value1&key2=value2…)拼接成字符串string1。

有两个注意点:1. 字段值采用原始值,不要进行URL转义;2. 必须严格按照如下格式拼接,不可变动字段顺序。 jsapi_ticket=JSAPITICKET&noncestr=NONCESTR&timestamp=TIMESTAMP&url=URL 然后对string1作sha1加密即可。

4、代码实现:

<?php
	$appid = "wwwxssacy554cacvd"; //企业信息中的ID
  $SECRET = "NHjVLxxxccdpqCH4feADLAOHr2hJHj20"; //应用secret
  $agentid = "200225";
  // 1、获取access_token
  $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$appid."&corpsecret=".$SECRET;
  $result = getJson($url);
  // 通过code和access_token获取userid和user_ticket
  $access_token = $result['access_token'];

  // 2、获取企业的jsapi_ticket
  $res3 = getJson("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=".$access_token."&type=agent_config");
  // var_dump(json_encode($res3));exit;

  // $res3 = getJson("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=".$access_token);
  if($res3['errcode']==0){
	 // 3、生成签名
  $jsapi_ticket=$res3['ticket'];
  $noncestr = noncestr(16);
  $timestamp = time();
  $surl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];;
  $signatureString = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."&timestamp=".$timestamp."&url=".$surl;
  $signature = sha1 ( $signatureString ); //对signatureString进行sha1签名,得到signature:
   // var_dump($signature);exit;
   
  $wxagentConfig = wxagentConfig($appid,$agentid,$timestamp,$noncestr,$signature,$surl,$jsapi_ticket);
  // var_dump($wxagentConfig);exit;

  }
   
  // 生成企业微信所需字段
  function wxagentConfig($appid,$agentid,$timestamp,$noncestr,$signature,$surl,$jsapi_ticket){
  $wx['corpid'] =$appid; // 必填,企业微信的corpid,必须与当前登录的企业一致
  $wx['agentid'] = $agentid; // 必填,企业微信的应用id (e.g. 1000247)
  $wx['timestamp'] = $timestamp; // 必填,生成签名的时间戳
  $wx['nonceStr'] = $noncestr; // 必填,生成签名的随机串
  $wx['signature'] = $signature;// 必填,签名,见附录-JS-SDK使用权限签名算法
   
  return $wx;
  }

 // 生成签名的随机串
 function nonceStr($length){
  $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJK1NGJBQRSTUVWXYZ';//随即串,62个字符
  $strlen = 62;
  while($length > $strlen){
   $str .= $str;
   $strlen += 62;
  }
  $str = str_shuffle($str);
  return substr($str,0,$length);
 }

 function getJson($url){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($ch);
  curl_close($ch);
  return json_decode($output, true);
 }
?>

5、使用方法:

<script>
wx.agentConfig({
	beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
	debug: true,
	corpid: '<?php echo $wxagentConfig["corpid"];?>', // 必填,企业微信的corpid,必须与当前登录的企业一致
	agentid: '<?php echo $wxagentConfig["agentid"];?>', // 必填,企业微信的应用id (e.g. 1000247)
	timestamp: '<?php echo $wxagentConfig["timestamp"];?>', // 必填,生成签名的时间戳
	nonceStr: '<?php echo $wxagentConfig["nonceStr"];?>', // 必填,生成签名的随机串
	signature: '<?php echo $wxagentConfig["signature"];?>',// 必填,签名,见附录-JS-SDK使用权限签名算法
	jsApiList: ['selectExternalContact','saveApprovalSelectedItems','getApprovalSelectedItems'], //必填,传入需要使用的接口名称
	success: function(res) {
 // alert(11222);
		// 回调
		alert(JSON.stringify(res))
	},
	fail: function(res) {
		if(res.errMsg.indexOf('function not exist') > -1){
			alert('版本过低请升级')
		}
		alert(JSON.stringify(res))
	}
});
</script>

以上是PHP企业微信生成agentConfig注入应用的权限步骤,供大家参考和使用。

附API错误状态码查询地址:https://open.work.weixin.qq.com/devtool/query?e=80001

【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/955.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×