Esempio n. 1
0
        public async Task <ApiResult <string> > SysItem2(int ID, APIUserViewModel vm)
        {
            // 以接口的形式返回数据
            var res    = new ApiResult <string>();
            int UserID = GetUserID();

            if (UserID > 0)
            {
                if (!string.IsNullOrWhiteSpace(vm.UserName))
                {
                    UserInfo m = db.GetId(ID);
                    if (m != null)
                    {
                        m.ID           = ID;
                        m.UserName     = vm.UserName;
                        m.HeadPortrait = vm.HeadPortrait;
                        m.NickName     = vm.NickName;
                        m.FullName     = vm.FullName;
                        m.Position     = vm.Position;
                        m.IdCard       = Utility.IdCardEncrypt(vm.IdCard);
                        m.Gender       = vm.Gender;
                        m.MobilePhone  = vm.MobilePhone;
                        m.QQ           = vm.QQ;
                        m.Mail         = vm.Mail;
                        m.State        = vm.State;
                        m.RoleID       = vm.RoleID;

                        // 设置默认密码
                        if (string.IsNullOrWhiteSpace(vm.Password))
                        {
                            vm.Password = "******";
                        }
                        // 如果设置了密码、就进行加密
                        if (!string.IsNullOrWhiteSpace(vm.Password))
                        {
                            m.Password = MD5Encode.GetEncrypt(vm.Password);
                        }
                    }
                    else
                    {
                        res.msg = "参数丢失";
                    }
                    try
                    {
                        res.success = db.Update(m) > 0;
                        if (res.success)
                        {
                            res.msg = "修改成功";
                        }
                        else
                        {
                            res.msg        = "修改失败";
                            res.statusCode = (int)ApiEnum.Status;
                        }
                    }
                    catch (Exception ex)
                    {
                        res.statusCode = (int)ApiEnum.Error;
                        res.msg        = ApiEnum.Error.GetEnumText() + ex.Message;
                    }
                }
                else
                {
                    res.msg = "参数丢失";
                }
            }
            else
            {
                res.msg        = "无法获取用户信息!";
                res.statusCode = (int)ApiEnum.Status;
            }
            return(await Task.Run(() => res));
        }
Esempio n. 2
0
        public ApiResult <IEnumerable <SysUserViewModel> > Import(IFormFile excelfile)
        {
            var res = new ApiResult <IEnumerable <SysUserViewModel> >()
            {
                statusCode = (int)ApiEnum.Status
            };

            if (excelfile != null)
            {
                List <UserInfo> userInfos  = new List <UserInfo>(); // 更新的列表
                List <UserInfo> userInfos2 = new List <UserInfo>(); // 添加的列表
                UserInfo        sysUserView;
                string          sWebRootFolder = _webHostEnvironment.WebRootPath + "\\UploadFiles\\excel\\import";
                string          sFileName      = $"{Guid.NewGuid()}.xlsx";
                FileInfo        file           = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
                try
                {
                    using (FileStream fs = new FileStream(file.ToString(), FileMode.Create))
                    {
                        excelfile.CopyTo(fs);
                        fs.Flush();
                    }
                    using (ExcelPackage package = new ExcelPackage(file))
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                        int            rowCount  = worksheet.Dimension.Rows;
                        int            ColCount  = worksheet.Dimension.Columns;
                        DateTime       now       = DateTime.Now;
                        string         password  = MD5Encode.GetEncrypt("888888");
                        for (int row = 2; row <= rowCount; row++)
                        {
                            // 查询账号是否已经存在 - 如果单次数据超过100条建议把所有用户取出来对比
                            sysUserView = db.GetFirst(o => o.UserName == C.String(worksheet.Cells[row, 2].Value));
                            if (sysUserView != null)
                            {
                                userInfos.Add(new UserInfo
                                {
                                    UserName    = C.String(worksheet.Cells[row, 1].Value),
                                    NickName    = C.String(worksheet.Cells[row, 2].Value),
                                    FullName    = C.String(worksheet.Cells[row, 3].Value),
                                    MobilePhone = C.String(worksheet.Cells[row, 4].Value),
                                });
                            }
                            else
                            {
                                userInfos2.Add(new UserInfo
                                {
                                    UserName    = C.String(worksheet.Cells[row, 1].Value),
                                    NickName    = C.String(worksheet.Cells[row, 2].Value),
                                    FullName    = C.String(worksheet.Cells[row, 3].Value),
                                    MobilePhone = C.String(worksheet.Cells[row, 4].Value),
                                    RoleID      = 4, // 用户
                                    State       = 2, // 已审
                                    AddDate     = now,
                                    Password    = password,
                                });
                            }
                        }
                    }
                    // 新增或更新
                    int addCount = 0, updateCount = 0;
                    if (userInfos.Count > 0)
                    {
                        addCount = db.Update(userInfos);
                        res.msg += $"导入{addCount}条,";
                    }
                    if (userInfos2.Count > 0)
                    {
                        updateCount = db.Add(userInfos2);
                        res.msg    += $"更新{addCount}条,";
                    }
                    // 结果
                    res.success = addCount > 0 || updateCount > 0;
                    if (res.msg != null)
                    {
                        res.msg = res.msg.TrimEnd(',');
                    }
                }
                catch (Exception ex)
                {
                    res.success = false;
                    res.msg     = ApiEnum.Error.GetEnumText() + ex.Message;
                }
            }
            return(res);
        }
Esempio n. 3
0
        public async Task <ApiResult <object> > UpdatePwd(string Oldpassword, string Newpassword1, string Newpassword2)
        {
            // 以接口的形式返回数据
            var res    = new ApiResult <object>();
            int UserID = GetUserID();

            if (UserID > 0)
            {
                TokenModelJwt tokenModelJwt = TokenDecode();
                int           UID           = C.Int(tokenModelJwt.Uid);
                try
                {
                    if (Newpassword1 == Newpassword2)
                    {
                        UserInfo m = db.GetId(UID);
                        if (m != null)
                        {
                            var user = db.GetFirst(a => a.UserName == m.UserName && a.Password == MD5Encode.GetEncrypt(Oldpassword));
                            if (user == null)
                            {
                                res.statusCode = (int)ApiEnum.ParameterError;
                                res.msg        = "原密码错误!";
                            }
                            else
                            {
                                if (!string.IsNullOrWhiteSpace(Newpassword1))
                                {
                                    m.Password  = MD5Encode.GetEncrypt(Newpassword1);
                                    res.success = db.Update(m) > 0;
                                }
                                else
                                {
                                    res.statusCode = (int)ApiEnum.ParameterError;
                                    res.msg        = "密码不能为空!";
                                }
                                if (res.success)
                                {
                                    res.statusCode = (int)ApiEnum.Status;
                                }
                            }
                        }
                        else
                        {
                            res.statusCode = (int)ApiEnum.Status;
                            res.msg        = "无该账号";
                        }
                    }
                    else
                    {
                        res.statusCode = (int)ApiEnum.ParameterError;
                        res.msg        = "两次密码不一样!";
                    }
                }
                catch (Exception ex)
                {
                    res.statusCode = (int)ApiEnum.Error;
                    res.msg        = ApiEnum.Error.GetEnumText() + ex.Message;
                }
            }
            else
            {
                res.statusCode = (int)ApiEnum.Status;
                res.msg        = "无法获取用户信息!";
            }
            return(await Task.Run(() => res));
        }