开发者社区> 问答> 正文

【创联云】钉钉通讯录(部门,成员)demo

一、工具和环境:eclipse+springBoot+Maven+jquery+51手机模拟器
二、阅读官方开发文档
[attachment=126183]
三、请参考帖子:【创联云】钉钉免登demo https://bbs.aliyun.com/read/317081.html?spm=0.0.0.0.xIkNnr  获取access_token
四、部门与成员查看,创建,删除方法
//获取部门列表
    public String getDepartmentList(String accessToken) throws OApiException{
        String url= Env.OAPI_HOST+"/department/list?access_token="+accessToken;
        JSONObject response = HttpHelper.httpGet(url);
        
        return response.toJSONString();
    }
    
    //创建部门
    public void createDepartment(String accessToken, String name,
            String parentId, String order, boolean createDeptGroup ) throws Exception {
        CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
        corpDepartmentService.deptCreate(accessToken, name, parentId, order, createDeptGroup);
    }
    
    //删除部门
    public void deleteDepartment(String accessToken, Long id) throws Exception{
        CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
        corpDepartmentService.deptDelete(accessToken, id);
    }
    
    //获取部门下成员
    public String getuserbydepart(String accessToken, Long id) throws Exception{
        String url= Env.OAPI_HOST+"/user/simplelist?access_token="+accessToken + "&department_id=" + id;
        JSONObject response = HttpHelper.httpGet(url);
        return response.toJSONString();
    }
    
    //创建成员
    public JSONObject createUser(String accessToken, String name,List department, String mobile) throws Exception {
            JSONObject json = new JSONObject();
            json.put("name", name);
            json.put("department", department);
            json.put("mobile", mobile);
            String url = Env.OAPI_HOST + "/user/create?access_token=" + accessToken ;
            JSONObject reponseJson = null;
            try {
                reponseJson = HttpHelper.httpPost(url,json);
            } catch (OApiException e) {
                e.printStackTrace();
            }
            return reponseJson;
    }
    
    //删除成员
    public void deleteUser(String accessToken, String userid) throws Exception{
        String url= Env.OAPI_HOST+"/user/delete?access_token="+accessToken + "&userid=" + userid;
        JSONObject response = HttpHelper.httpGet(url);
    }
五、控制层访问方法代码
@CrossOrigin
    @RequestMapping("/get-departmentList")
    @ResponseBody
    public JSONObject getDepartmentList() throws OApiException{
        return JSONObject.parseObject(serviceHelper.getDepartmentList(AuthHelper.getAccessToken()));
    }
    
    @CrossOrigin
    @RequestMapping("/create-department")
    @ResponseBody
    public void createDepartment(String name) throws Exception{
        String parentId = "1";
        String order = "1";
        boolean createDeptGroup = true;
        serviceHelper.createDepartment(AuthHelper.getAccessToken(), name, parentId, order, createDeptGroup);
    }
    
    @CrossOrigin
    @RequestMapping("/del-department")
    @ResponseBody
    public void deleteDepartment(Long departmentId) throws Exception{
        serviceHelper.deleteDepartment(AuthHelper.getAccessToken(), departmentId);
    }
    
    @CrossOrigin
    @RequestMapping("/get-user-department")
    @ResponseBody
    public JSONObject getuserbydepart(Long departmentId) throws Exception{
        return JSONObject.parseObject(serviceHelper.getuserbydepart(AuthHelper.getAccessToken(), departmentId));
    }
    
    @CrossOrigin
    @RequestMapping("/create-user")
    @ResponseBody
    public void createUser(String name,String departmentId,String mobile) throws Exception{
        List<String> departList = new ArrayList<String>();
        departList.add(departmentId);
        serviceHelper.createUser(AuthHelper.getAccessToken(), name, departList, mobile);
    }
    
    @CrossOrigin
    @RequestMapping("/del-userbydepartmentid")
    @ResponseBody
    public void deleteUser(String userId) throws Exception{
        serviceHelper.deleteUser(AuthHelper.getAccessToken(), userId);
    }
六、页面js调用
function getdepartmentList(){
        $.get(
            'get-departmentList',
            function(result){
                $("#departmentList").empty();
                var html="";
                $.each( result.department, function(index, depar)
                {
                    
                    html+= '<table align="center"><tr><td align="center" valign="middle" width="80px">'+depar.name+'</td><td align="center" valign="middle"><button class="btn" onclick="getUserList('+depar.id+')">获取部门成员</button><br/></td><td align="center" valign="middle"><button class="btn" onclick="delDepartment('+depar.id+')">删除部门</button></td></tr></table>'
                });
                $("#departmentList").append(html);
            }
        );
    }
    
    function createDepartment(){
        var name = $("#deparName").val();
            $.get(
                'create-department',
                {
                    "name":name
                },
                function(result){
                    getdepartmentList();
                }
            );
        
    }
    
    function delDepartment(departmentId){
        $.get(
            'del-department',
            {
                "departmentId":departmentId
            },
            function(result){
                getdepartmentList();                
            }
        );
    }
    
    
    function getUserList(departmentId){
        $.get(
            'get-user-department',
            {
                "departmentId":departmentId
            },
            function(result){
                $("#userList").empty();
                var html="";
                $.each(result.userlist, function(index, user)
                {
                    html+= '<table align="center"><tr><td align="center" valign="middle" width="100px">'+user.name+'</td><td align="center" valign="middle"><button class="btn" onclick="delUser('+user.userid+','+departmentId+')">删除成员</button><input id="departmentId" type="hidden" value="'+departmentId+'"/></td></tr></table>'
                });
                $("#userList").append(html);
            }
        );
    }
    
    function delUser(userId,departmentId){
        alert(userId);
        alert(departmentId);
        $.get(
            'del-userbydepartmentid',
            {
                "userId":userId
            },
            function(result){
                getUserList(departmentId);            
            }
        );
    }
    
    function createUser(){
        var name = $("#userName").val();
        var departmentId = $("#departmentId").val();
        var mobile =  $("#mobile").val();
        $.get(
                'create-user',
                {
                    "name":name,
                    "departmentId":departmentId,
                    "mobile":mobile
                },
                function(result){
                    $("#userName").empty();
                    $("#mobile").empty();
                    getUserList(departmentId);    
                }
            );
        
    }
七、模拟器演示图
[attachment=126184]





展开
收起
steven_1205 2017-05-31 09:51:09 3478 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
钉钉小程序——为工作方式插上翅膀 立即下载
钉钉客户端自动化的应用 立即下载
使命必达 —— 钉钉企业级 消息服务的机遇与挑战 立即下载