/// <summary> /// 员工列表 /// </summary> /// <param name="storeId"></param> public void UserList(int storeId, int deptId = 0) { var permanentCode = WorkWeChats.GetAccessToken(storeId); var list = WorkWeiXinSDK.GetUserList(permanentCode.access_token, deptId.ToString()); foreach (var item in list.userlist) { Response.Write(item.name + "<br/>"); } }
public void ImportUser(int storeId) { var permanentCode = WorkWeChats.GetAccessToken(storeId); //已存在的成员 var oldUserList = StaffManager.GetListByStoreId(storeId); //已存在的部门 var oldDeptList = DepartmentManager.GetList(storeId); //已存在的员工部门关系 var oldUnionList = StaffDepartmentManager.GetListByStoreId(storeId); var wxDeptId = oldDeptList.Find(x => x.wxDepartmentId > 0 && x.Parentid == 0); var list = WorkWeiXinSDK.GetUserList(permanentCode.access_token, wxDeptId.wxDepartmentId.ToString()); //导入微信通讯录的成员 foreach (var item in list.userlist) { //成员不存在,创建成员 var old = oldUserList.Find(x => x.Name.ToLower().Equals(item.name.ToLower())); if (old == null) { //创建用户 var userInfo = Staffs.CreateUser(storeId, item.name); //开通聊天的用户 WebIMUtil.ImportUser(userInfo); old = new Staff() { StoreId = storeId, Uid = userInfo.Uid, Name = item.name, wxUserId = item.userid, Wxid = string.Empty, Avater = string.Empty, AiState = 0, BossState = 0, MobileState = "1", Company = permanentCode.corp_full_name, ClickCount = 0, SignUpCount = 0, UpCount = 0, ShareCount = 0, QrCode = "", Sign = string.Empty, Mobile = string.Empty, Phone = string.Empty, Email = string.Empty, wxMobile = string.Empty, Address = string.Empty, Position = string.Empty, Department = string.Empty, Addtime = DateTime.Now }; StaffManager.Add(old); //Response.Write($"{item.name} 导入成功 (新增) " + userInfo.Uid + "<br/>"); } else { //创建用户 if (old.Uid < 1) { //创建用户 var userInfo = Staffs.CreateUser(storeId, item.name); //开通聊天的用户 WebIMUtil.ImportUser(userInfo); old.Uid = userInfo.Uid; } StaffManager.Update(old); //Response.Write($"{item.name} 导入成功 (更新)<br/>"); } //导入所属部门 foreach (var departmentId in item.department) { //用微信的部门ID,找到系统的部门ID var dept = oldDeptList.Find(x => x.wxDepartmentId == departmentId); var deptId = dept != null ? dept.Id : 0; //判断 员工ID,系统部门ID是否相同 var oldUnion = oldUnionList.Find(x => x.StaffId == old.Id && x.DepartmentId == deptId); if (oldUnion == null) { StaffDepartment newUnion = new StaffDepartment() { DepartmentId = deptId, StaffId = old.Id, StoreId = storeId }; StaffDepartmentManager.Add(newUnion); } } } }