Commit 2e2e9a60 authored by “wuhongyu”'s avatar “wuhongyu”

wuhongyu_增加Excel批量添加用户

parent 88fb5d25
This source diff could not be displayed because it is too large. You can view the blob instead.
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
......
using BBM.ChatGLM.Dtos;
using BBM.ChatGLM.User;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp;
using OfficeOpenXml;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Identity;
namespace BBM.ChatGLM.Controllers
{
[Route("User")]
......@@ -63,5 +61,83 @@ namespace BBM.ChatGLM.Controllers
{
return await _usersAppService.GetTokenAsync();
}
[HttpPost("excelFile")]
public async Task<IActionResult> UploadExcel(IFormFile file)
{
/**
* 操作步骤
* 1、React中,使用antd的Upload组件来上传Excel表格数据,并且在上传之前需要将表格数据转换成FormData。
* 2、在ASP.NET中,建立一个Controller用于接收上传的文件,并且使用[HttpPost]属性来指定使用POST方法接收请求。
* 3、在Controller中,使用IFormFile接口来接收上传的Excel文件,然后使用EPPlus等工具来读取Excel文件数据
* 4、将读取到的Excel数据进行处理,例如存储到数据库中或者进行数据分析等操作
*/
Console.WriteLine("\n\n\n\n");
Console.WriteLine("react + antd 访问 \t");
Console.WriteLine("comeback welcome UploadExcel(IFormFile excel)");
var failNum = 0; //添加失败的人数变量
if (file == null || file.Length == 0)
return BadRequest("Excel文件为空");
var fileName = file.FileName;
Console.WriteLine(fileName);
var filePath = Path.Combine("C:\\Excel", fileName);
Console.WriteLine(filePath);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
using (var package = new ExcelPackage(new FileInfo(filePath)))
{
var worksheet = package.Workbook.Worksheets["sheet1"]; //reader space Name 可能要更灵活-通过指定的空间名称
var totalRows = worksheet.Dimension.Rows;
var row = 2; //First line is UserName And Phone
while (row <= totalRows)
{
// 处理Excel数据
var userName = worksheet.Cells[row, 1].Value?.ToString() ?? "";
var phoneNumber = worksheet.Cells[row, 2].Value?.ToString() ?? "";
var password = "Zs"+ phoneNumber.Substring(phoneNumber.Length - 6); //1、可能需要加密操作... 2、以及判断账户是否在数据库重复
UserCreateInput userInput = new UserCreateInput();
userInput.UserName = userName;
userInput.Password = password;
userInput.Name = "";
userInput.Role = "";
userInput.IsActive = false;
failNum++;
//try
//{
// //添加用户方法
//}
//catch (Exception ex)
//{
// failNum ++; //账户添加 失败一次 数量+1
// Console.WriteLine(ex.Message);
// continue;
//}
row++;
}
package.Dispose();
}
return Ok(failNum==0?"用户新增成功":failNum);
//return Ok("Excel文件上传成功");
}
}
}
......@@ -63,7 +63,8 @@
"react-dev-inspector": "^1.7.0",
"react-dom": "^17.0.0",
"react-helmet-async": "^1.2.0",
"umi": "^3.5.0"
"umi": "^3.5.0",
"xlsx": "^0.18.5"
},
"devDependencies": {
"@ant-design/pro-cli": "^2.1.0",
......
......@@ -5,18 +5,23 @@ import {
postDeleteUser,
postResetUser,
postEditUser,
postExcelFile,
} from '@/services/chatGLM/api';
import { PlusOutlined } from '@ant-design/icons';
import { PlusOutlined,UploadOutlined } from '@ant-design/icons';
import type { ActionType } from '@ant-design/pro-components';
import { PageContainer } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { Button, Col, Divider, Form, Input, Modal, Radio, Row, Switch, message } from 'antd';
import type { UploadProps } from 'antd';
import { Button, Col, Divider, Form, Input, Modal, Radio, Row, Switch, message ,Upload,Table } from 'antd';
import classNames from 'classnames';
import { isEmpty } from 'lodash';
import { useRef, useState, useEffect } from 'react';
import React,{ useRef, useState, useEffect } from 'react';
import styles from './index.less';
import { Exclamtion } from '@/components/Icons/Icons';
const { Item } = Form;
const { Password } = Input;
......@@ -65,6 +70,9 @@ const UserManage = () => {
}
};
const columns: any[] = [
{
title: '用户姓名',
......@@ -170,6 +178,26 @@ const UserManage = () => {
},
];
const props = {
name: 'file',
action: 'https://glm-mangement-api.baibaomen.com/User/excelFile',
headers: {
'access-control-allow-origin': '*',
'content-type': 'application/json; charset=utf-8'
},
onChange(info) {
console.log(info);
// if (info.file.status !== 'uploading') {
// console.log(info.file, info.fileList);
// }
// if (info.file.status === 'done') {
// message.success(`${info.file.name} 上传成功!`);
// } else if (info.file.status === 'error') {
// message.error(`${info.file.name} 上传失败!`);
// }
},
};
const handleOk = () => {
setIsModalAccountOpen(false);
};
......@@ -216,6 +244,11 @@ const UserManage = () => {
if (accountModalName === '添加账号') {
createdAccount(values);
}
// if (accountModalName === 'Excel添加账号'){
// createAccountExcel(values);
// }
if (accountModalName === '编辑账号') {
editAccountFinish(values);
}
......@@ -238,6 +271,17 @@ const UserManage = () => {
return Promise.reject(new Error('密码必须包含数字、大小写字母且长度为8-16位'));
};
const validateUserName = (_, value: any) => {
// const phoneNumberRegex = /^1[3456789]\d{9}$/;
//|| phoneNumberRegex.test(value)
if (!value ) {
return Promise.resolve();
}
return Promise.reject(new Error('请输入有效的姓名'));
};
const handleClickOpenModalDelete = () => {
postDeleteUser({
id: deleteMessage.id,
......@@ -378,9 +422,45 @@ const UserManage = () => {
>
<Password />
</Item>
<Item name={'Role'} label="角色" rules={[{ required: true, message: '请选择你的角色' }]}>
<Radio.Group>
{roles.map((item) => {
return (
<Radio key={item} value={`${item}`}>
{item}
</Radio>
);
})}
</Radio.Group>
</Item>
<Item name={'Name'} label="姓名" rules={[{ required: true, message: '请输入你的姓名' }]}>
<Input placeholder="" />
</Item>
<Item label="是否启用" name={'isActive'} valuePropName="checked">
<Switch checked />
</Item>
<Divider />
<Row justify={'end'} gutter={8}>
<Col>
<Item key={'cancelButton'} noStyle={true}>
<Button key="back" onClick={handleModalCancel}>
取消
</Button>
</Item>
</Col>
<Col>
<Item key={'saveButton'} noStyle={true}>
<Button key="submit" type="primary" loading={loading} htmlType="submit">
保存
</Button>
</Item>
</Col>
</Row>
</>
)}
<Item name={'Role'} label="角色" rules={[{ required: true, message: '请选择你的角色' }]}>
{/* <Item name={'Role'} label="角色" rules={[{ required: true, message: '请选择你的角色' }]}>
<Radio.Group>
{roles.map((item) => {
return (
......@@ -413,9 +493,11 @@ const UserManage = () => {
</Button>
</Item>
</Col>
</Row>
</Row> */}
</Form>
</Modal>
<ProTable
request={async (params = { pageSize: 10, current: 0 }, sort, filter) => {
console.log(sort, filter, params);
......@@ -442,6 +524,9 @@ const UserManage = () => {
options={false}
headerTitle="账号列表"
toolBarRender={() => [
<Upload {...props}>
<Button icon={<UploadOutlined />}>Excel表格添加账号</Button>
</Upload>,
<Button
key="button"
icon={<PlusOutlined />}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment