侧边栏JS-SDK

侧边栏JS-SDK是面向网页开发者提供的的网页开发工具包,类似企业微信JS-SDK。

使用说明

在需要调用JS接口的页面引入JS文件,通过wx对象调用相关接口。

<script src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="https://cdn.botorange.com/js/sidebar/juzi-helper-1.0.5.js"></script>
<!-- 
  1.juzi-helper仅在句子秒回侧边栏环境下覆写wx对象,成功后window.wx.isJuziWx === true
  2.如果使用npm安装的jweixin或页面不存在wx对象,juzi-helper的覆写可能不会成功,此时可使用window.juziWx
  -->

相关接口

获取当前外部联系人userid

同企业微信JS-SDK

wx.invoke('getCurExternalContact', { }, function(res){
  if (res.err_msg == "getCurExternalContact:ok") {
    //成功处理 {userId: 'xxx', err_msg: 'getCurExternalContact:ok'}
  } else {
    //错误处理 {userId: undefined, err_msg: 'getCurExternalContact:fail'}
  }
});

获取当前客户群的群ID

从客户群的聊天工具栏进入页面时才可成功调用该接口

wx.invoke('getCurExternalChat', {}, function(res){
  if (res.err_msg == "getCurExternalChat:ok") {
    //成功处理 {chatId: 'xxx', err_msg: 'getCurExternalChat:ok'}
  } else {
    //错误处理 {chatId: undefined, err_msg: 'getCurExternalChat:fail'}
  }
});

分享消息到当前会话

type TextMsg = { msgtype: 'text'; text: { content: string; } };

type ImageMsg = { 
  msgtype: 'image';
  image: { 
    url: string;
    title?: string;
  };
}

type VideoMsg = { 
  msgtype: 'video';
  video: { 
    url: string;
    title?: string;
  };
}

type FileMsg = { 
  msgtype: 'file';
  file: { 
    url: string;
    title?: string; 
  };
}

type NewsMsg = {
  msgtype: 'news',
  news: {
    link: string,
    title?: string,
    desc?: string,
    imgUrl?: string,
  },
}

type MiniprogramMsg = {
  msgtype: "miniprogram",
  miniprogram: {
    appid: string,    // 小程序的appid
    title: string,    // 小程序消息的title
    imgUrl: string,   // 小程序消息的封面图。必须带http或者https协议头
    page: string,     // 小程序消息打开后的路径
    desc: string,     // 小程序描述
    iconUrl: string,  // 小程序icon地址
    officialAccountId: string,    // 小程序原始id
  },
}

type SendParams = TextMsg | ImageMsg | VideoMsg | FileMsg | NewsMsg | MiniprogramMsg;

type ErrMsg = 'sendChatMessage:ok' | 'text content is empty' | 'url is empty';

type Callback = (res: { err_msg: ErrMsg; }) => void;

wx.invoke('sendChatMessage', sendParams: SendParams, callback: Callback);

代码示例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>demo</title>
  <script src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
  <script src="https://cdn.botorange.com/js/sidebar/juzi-helper-1.0.5.js"></script>

  <style>
    textarea {
      display: block;
      margin-bottom: 12px;
    }
  </style>
</head>

<body>
  <textarea id="messageInput" rows="4"></textarea>
  <button onclick="sendMessage()">发送</button>
  <script>
    // 如果isJuziWx非true,wx对象没有覆写成功,可使用window.juziWx替代window.wx
    console.log('isJuziWx: ', window.wx.isJuziWx);

    // 获取当前外部联系人userid
    wx.invoke('getCurExternalContact', {}, function (res) {
      if (res.err_msg == "getCurExternalContact:ok") {
        console.log('获取当前外部联系人:ok', res);
      } else {
        console.log('获取当前外部联系人:fail', res);
      }
    });

    // 获取当前客户群的群ID
    wx.invoke('getCurExternalChat', {}, function (res) {
      if (res.err_msg == "getCurExternalChat:ok") {
        console.log('获取当前客户群的群:ok', res);
      } else {
        console.log('获取当前客户群的群:fail', res);
      }
    });

    // 分享消息到当前会话
    function sendMessage() {
      var message = document.getElementById("messageInput").value;
      console.log("发送消息:" + message);
      wx.invoke('sendChatMessage', {
        msgtype: 'text',
        text: { content: message }
      }, res => {
        console.log("发送结果:", res);
      });
    }
  </script>
</body>
</html>

身份认证

提供了OAuth的授权登录方式,可以让从自定义侧边栏打开的网页获取成员的身份信息,从而免去登录的环节。

构造授权链接

如果企业需要在打开的网页里面携带用户的身份信息,第一步需要构造如下的链接来获取code参数:

https://hub.juzibot.com/oauth/connect/authorize?redirect_uri=REDIRECT_URI&state=STATE

参数说明:

参数必须说明
redirect_uri授权后重定向的回调链接地址,请使用urlencode对链接进行处理
state重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值,长度不可超过128个字节

将会以当前聊天窗口所属的托管企业微信进行自动授权,授权后页面将跳转至 redirect_uri?code=CODE&state=STATE&juziBotId=juziBotId,企业可根据code参数获得员工的userId。juziBotId为托管微信的id。

获取访问用户身份

该接口用于根据code获取成员信息

请求地址:

GET https://hub.juzibot.com/api/v1/oauth/getUserInfo?code=**

参数说明:

参数类型备注是否必须
codestring通过成员授权获取到的code。每次成员授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。

返回结果:

{
   "errcode": 0,
   "errmsg": "",
   "data": {
     "userId":"USERID",
   }
}

返回参数说明:

参数类型必定存在备注
errcodenumber返回码
errmsgstring对返回码的文本描述内容
data.userIdstring企业微信,企业成员UserID。
最近更新:
Contributors: iseei, SilentQianyi, fanmenglin