java微信企业号开发之通讯录

网友投稿 251 2023-07-13


java微信企业号开发之通讯录

上篇文章中介绍了聊天功能,这里介绍通讯录是如何实现的。首先要加载公司的所有部门,树形结构,然后点击进入部门的人员列表,点击人员能查看详细信息。

一、界面

公司部门的树形结构:

部门成员列表:

个人详细信息:

二、代码实现

1.controller

/**

* 加载部门列表

*/

@RequestMapping("/addressListDepartmentjsp.do")

public void addressListDepartment(HttpServletRequest request, HttpServletResponse response) throws IOException{

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

List jsList = addressListService.getTree();

JSONArray jsonArray = JSONArray.fromObject(jsList);

PrintWriter out = response.getWriter();

out.print(jsonArray);

}

/**

* 加载部门成员列表

*/

@RequestMapping("/addressListUserList.do")

public String addressListuserList(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

String deptId=request.getParameter("Departmentid");

String d=request.getParameter("departmentName");

String departmentName = new String(d.getBytes("ISO-8859-1"),"utf-8");

List userDetail = addressListService.getUserDetail(deptId);

request.setAttribute("userDetail", userDetail);

request.setAttribute("departmentName", departmentName);

return "addressListUserList";

}

/**

* 查看员工详细信息

*/

@RequestMapping("/addressListUserInfo.do")

public String addressListuserInfo(HttpServletRequest request,HttpServletRespohttp://nse response) throws UnsupportedEncodingException{

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

String n=request.getParameter("name");

String name = new String(n.getBytes("ISO-8859-1"),"utf-8");

String mobile=request.getParameter("mobile");

String email=request.getParameter("email");

String weixinid=request.getParameter("weixinid");

String avatar=request.getParameter("avatar");

String d=request.getParameter("departmentName");

String departmentName = new String(d.getBytes("ISO-8859-1"),"utf-8");

request.setAttribute("name", name);

request.setAttribute("mobile", mobile);

request.setAttribute("email",email);

request.setAttribute("weixinid", weixinid);

requestLzMaKdK.setAttribute("avatar", avatar);

request.setAttribute("departmentName", departmentName);

return "addressListUserInfo";

}

2.serviceImpl

/**

* 加载部门列表

*/

public List getTree(){

//1.先获取token

String accessToken = CommonUtil.getAccessToken("wxe510946434680dab", "eWTaho766INvp4e1MCsz1mHYuT2DAleb62REQ3vsFizhY4vtmwZpKweuxUVh33G0").getAccessToken();

//2.获取部门列表

List departmentList = AdvancedUtil.getDepartment(accessToken);

//根据部门列表转换为页面需要的格式

List jsList = this.convertList(departmentList);

return jsList;

}

/**

* 转为ZTree的格式

*/

public List convertList( List departmentList)

{

List rootNode = new ArrayList();

for (int i = 0; i < departmentList.size(); i++) {

for (int j = i+1; j

if (departmentList.get(i).getId()==departmentList.get(j).getParentid()) {

JsonTree jt = new JsonTree();

jt.setId(departmentList.get(i).getId());

jt.setName(departmentList.get(i).getName());

jt.setpId(departmentList.get(i).getParentid());

jt.setOpen(false);

jt.setUrl("");

rootNode.add(jt);

break;

}else {

JsonTree jt = new JsonTree();

jt.setId(departmentList.get(i).getId());

jt.setName(departmentList.get(i).getName());

jt.setpId(departmentList.get(i).getParentid());

jt.setOpen(false);

jt.setUrl("addressListUserList.do?Departmentid="+departmentList.get(i).getId()+"&departmentName="+departmentList.get(i).getName());

rootNode.add(jt);

break;

}

}

}

return rootNode;

}

/**

* 加载部门成员列表

*/

public List getUserDetail(String deptId){

//1.先获取token

String accessToken = CommonUtil.getAccessToken("wxe510946434680dab", "eWTaho766INvp4e1MCsz1mHYuT2DAleb62REQ3vsFizhY4vtmwZpKweuxUVh33G0").getAccessToken();

//2.根据部门id和token的值获取部门成员列表

List userDetail = AdvancedUtil.getUserDetail(accessToken, deptId);

return userDetail;

}

3.工具类

//获取部门列表

public static List getDepartment(String accessToken) {

List departmentList = null;

// https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN

String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN";

requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);

JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);

if (null != jsonObject) {

try {

departmentList = JSONArray.toList(jsonObject.getJSONArray("department"), Department.class);

} catch (JSONException e) {

departmentList = null;

int errorCode = jsonObject.getInt("errcode");

String errorMsg = jsonObject.getString("errmsg");

}

}

return departmentList;

}//获取部门成员详情

public static List getUserDetail(String accessToken,String departmentId){

List userDetail = null;

String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=1&status=0";

requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("DEPARTMENT_ID", departmentId);

JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);

if (null != jsonObject) {

try {

userDetail = JSONArray.toList(jsonObject.getJSONArray("userlist"),UserDetail.class);

} catch (JSONException e) {

userDetail = null;

int errorCode = jsonObject.getInt("errcode");

String errorMsg = jsonObject.getString("errmsg");

}

}

return userDetail;

}

4.js

if (departmentList.get(i).getId()==departmentList.get(j).getParentid()) {

JsonTree jt = new JsonTree();

jt.setId(departmentList.get(i).getId());

jt.setName(departmentList.get(i).getName());

jt.setpId(departmentList.get(i).getParentid());

jt.setOpen(false);

jt.setUrl("");

rootNode.add(jt);

break;

}else {

JsonTree jt = new JsonTree();

jt.setId(departmentList.get(i).getId());

jt.setName(departmentList.get(i).getName());

jt.setpId(departmentList.get(i).getParentid());

jt.setOpen(false);

jt.setUrl("addressListUserList.do?Departmentid="+departmentList.get(i).getId()+"&departmentName="+departmentList.get(i).getName());

rootNode.add(jt);

break;

}

}

}

return rootNode;

}

/**

* 加载部门成员列表

*/

public List getUserDetail(String deptId){

//1.先获取token

String accessToken = CommonUtil.getAccessToken("wxe510946434680dab", "eWTaho766INvp4e1MCsz1mHYuT2DAleb62REQ3vsFizhY4vtmwZpKweuxUVh33G0").getAccessToken();

//2.根据部门id和token的值获取部门成员列表

List userDetail = AdvancedUtil.getUserDetail(accessToken, deptId);

return userDetail;

}

3.工具类

//获取部门列表

public static List getDepartment(String accessToken) {

List departmentList = null;

// https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN

String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN";

requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);

JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);

if (null != jsonObject) {

try {

departmentList = JSONArray.toList(jsonObject.getJSONArray("department"), Department.class);

} catch (JSONException e) {

departmentList = null;

int errorCode = jsonObject.getInt("errcode");

String errorMsg = jsonObject.getString("errmsg");

}

}

return departmentList;

}//获取部门成员详情

public static List getUserDetail(String accessToken,String departmentId){

List userDetail = null;

String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=1&status=0";

requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("DEPARTMENT_ID", departmentId);

JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);

if (null != jsonObject) {

try {

userDetail = JSONArray.toList(jsonObject.getJSONArray("userlist"),UserDetail.class);

} catch (JSONException e) {

userDetail = null;

int errorCode = jsonObject.getInt("errcode");

String errorMsg = jsonObject.getString("errmsg");

}

}

return userDetail;

}

4.js

三、总结

通讯录功能并没有想象中的难,树结构采用ztree框架,后台查到的数据必须转换为ztree定义的名称,然后部门成员列表的显示和查询用到jquery mobile,在以后的文章中再介绍这种js的使用,从名字上就知道它是专门为手机页面开发的。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:BootStrap初学者对弹出框和进度条的使用感觉
下一篇:详解Java中的File文件类以及FileDescriptor文件描述类
相关文章

 发表评论

暂时没有评论,来抢沙发吧~