index.jsx 8.9 KB
Newer Older
TZW's avatar
TZW committed
1 2 3 4
/* 用户管理
 * @Author: Li Hanlin
 * @Date: 2022-11-09 14:44:44
 * @Last Modified by: Li Hanlin
TZW's avatar
TZW committed
5
 * @Last Modified time: 2022-12-19 10:32:30
TZW's avatar
TZW committed
6 7
 */

wuhao's avatar
wuhao committed
8
import * as React from 'react';
TZW's avatar
TZW committed
9
import { useState, useMemo, useRef, useEffect, useCallback } from 'react';
wuhao's avatar
wuhao committed
10 11 12
import DrawerPro from '@/components/DrawerPro';
import AutoTable from '@/components/AutoTable';
import PremButton from '@/components/PremButton';
TZW's avatar
TZW committed
13
import InitForm from '@/components/InitForm';
wuhao's avatar
wuhao committed
14
import getcolumns from './columns';
TZW's avatar
TZW committed
15
import { doFetch } from '@/utils/doFetch';
TZW's avatar
TZW committed
16
import { message, AutoComplete } from 'antd';
TZW's avatar
TZW committed
17
import ExtendField from '@/components/ExtendField';
TZW's avatar
TZW committed
18
import { useModel } from 'umi';
TZW's avatar
1  
TZW committed
19
import { useRequest } from 'ahooks';
TZW's avatar
TZW committed
20
import { indexOf } from 'lodash';
wuhao's avatar
wuhao committed
21

TZW's avatar
TZW committed
22
function User(props) {
TZW's avatar
TZW committed
23
  const { initialState, loading, error, refresh, setInitialState } = useModel('@@initialState');
TZW's avatar
TZW committed
24
  const [username, setusername] = useState(null);
TZW's avatar
TZW committed
25

wuhao's avatar
wuhao committed
26 27
  const actionRef = useRef(),
    formRef = useRef();
TZW's avatar
TZW committed
28
  const [drawer, setDrawer] = useState({
wuhao's avatar
wuhao committed
29 30 31
    visible: false,
  });

TZW's avatar
TZW committed
32
  const urlParams = {
TZW's avatar
TZW committed
33 34
    save: '/auth/sysUser/save',
    remove: '/auth/sysUser/deleteById',
TZW's avatar
TZW committed
35
    list: '/auth/sysUser/queryList',
TZW's avatar
TZW committed
36
    detail: '/auth/sysUser/detail',
TZW's avatar
TZW committed
37 38
  };

wuhao's avatar
wuhao committed
39 40 41 42 43 44 45
  const detail = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          type: 'link',
          onClick: () => {
TZW's avatar
TZW committed
46
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
47 48
              ...s,
              visible: true,
TZW's avatar
TZW committed
49
              // item: row,
wuhao's avatar
wuhao committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
              title: '详情',
              val: 'detail',
              title: row.userName + '的详细信息',
            }));
          },
        }}
      >
        详情
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
TZW's avatar
TZW committed
68
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
69 70
              ...s,
              visible: true,
TZW's avatar
TZW committed
71
              item: row,
TZW's avatar
TZW committed
72
              type: 'edit',
wuhao's avatar
wuhao committed
73
              title: '编辑',
TZW's avatar
TZW committed
74
              val: 'only',
TZW's avatar
TZW committed
75 76
              // onFinish: async (vals) => {
              //   let userCharReqList = [];
TZW's avatar
TZW committed
77
              //   //console.log('id:', row.id);
TZW's avatar
TZW committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
              //   for (let i in vals) {
              //     if (!isNaN(Number(i))) {
              //       userCharReqList.push({
              //         fieldId: i,
              //         fieldRealValue: vals[i],
              //       });
              //       delete vals[i];
              //     }
              //   }
              //   let params = {
              //     ...vals,
              //     id: row?.id,
              //     shopIdList: vals.shopId,
              //     userCharReqList,
              //   };
              //   delete params.shopId;
              //   let res = await doFetch({
              //     url: urlParams.save,
              //     params,
              //   });
              //   if (res.code === '0000') {
              //     message.success('编辑成功!');
              //     setDrawer((s) => ({
              //       ...s,
              //       visible: false,
              //     }));
              //     actionRef.current.reload();
              //   }
              // },
wuhao's avatar
wuhao committed
107 108 109 110 111 112 113 114 115 116 117 118 119
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
TZW's avatar
TZW committed
120
          title: '是否删除该用户?',
wuhao's avatar
wuhao committed
121 122
          okText: '确认',
          cancelText: '取消',
TZW's avatar
TZW committed
123 124 125 126 127 128 129 130 131 132
          onConfirm: async () => {
            let res = await doFetch({ url: urlParams.remove, params: { id: row.id } });
            if (res.code === '0000') {
              message.success('删除成功!');
              setDrawer((s) => ({
                ...s,
                visible: false,
              }));
              actionRef.current.reload();
            }
wuhao's avatar
wuhao committed
133 134 135 136 137 138 139 140 141 142 143 144 145
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

  const columns = useMemo(() => {
TZW's avatar
TZW committed
146
    let defcolumn = getcolumns(setDrawer, false, formRef);
wuhao's avatar
wuhao committed
147 148 149 150
    return defcolumn.concat({
      title: '操作',
      valueType: 'option',
      width: 150,
TZW's avatar
TZW committed
151
      render: (text, row, _, action) => [edit(text, row, _, action), remove(text, row, _, action)],
wuhao's avatar
wuhao committed
152
    });
TZW's avatar
TZW committed
153
  }, []);
wuhao's avatar
wuhao committed
154

TZW's avatar
TZW committed
155 156 157 158 159 160 161
  function selectType(type) {
    switch (type) {
      case 'add':
        return (
          <ExtendField
            setDrawer={setDrawer}
            drawer={drawer}
TZW's avatar
TZW committed
162
            formRef={formRef}
TZW's avatar
TZW committed
163
            columns={getcolumns(setDrawer, false, formRef)}
TZW's avatar
TZW committed
164 165 166 167 168 169 170 171
            formId={'1'}
            // urlParams={urlParams}
            onFinish={async (vals) => {
              let userCharReqList = [];
              for (let i in vals) {
                if (!isNaN(Number(i))) {
                  userCharReqList.push({
                    fieldId: i,
TZW's avatar
TZW committed
172
                    fieldRealValue: vals[i],
TZW's avatar
TZW committed
173 174 175 176 177 178
                  });
                  delete vals[i];
                }
              }
              let params = {
                ...vals,
TZW's avatar
TZW committed
179
                username: vals?.username.slice(0, vals?.username?.indexOf('-')),
TZW's avatar
TZW committed
180 181 182 183
                id: drawer?.title == '编辑' ? drawer?.item?.id : '',
                userCharReqList,
              };
              delete params.shopId;
TZW's avatar
TZW committed
184
              //console.log(params, '!!!!');
TZW's avatar
TZW committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
              let res = await doFetch({
                url: urlParams.save,
                params,
              });
              if (res.code === '0000') {
                if (drawer?.title == '编辑') {
                  message.success('编辑成功!');
                } else {
                  message.success('新增成功!');
                }
                setDrawer((s) => ({
                  ...s,
                  visible: false,
                }));
                actionRef.current.reload();
              }
            }}
TZW's avatar
TZW committed
202
            defaultFormValue={drawer.item}
TZW's avatar
TZW committed
203 204 205 206 207 208
            // onValuesChange={(changedValues, allValues) => {
            //   console.log(changedValues, allValues);
            //   if (changedValues['username']) {
            //     console.log(changedValues, allValues);
            //   }
            // }}
TZW's avatar
TZW committed
209 210 211 212 213 214 215 216 217
          />
        );

      case 'edit':
        return (
          <ExtendField
            setDrawer={setDrawer}
            drawer={drawer}
            actionRef={actionRef}
TZW's avatar
TZW committed
218
            columns={getcolumns(setDrawer, true, formRef)}
TZW's avatar
TZW committed
219 220 221 222 223 224 225 226
            formId={'1'}
            urlParams={urlParams}
            onFinish={async (vals) => {
              let userCharReqList = [];
              for (let i in vals) {
                if (!isNaN(Number(i))) {
                  userCharReqList.push({
                    fieldId: i,
TZW's avatar
TZW committed
227
                    fieldRealValue: vals[i],
TZW's avatar
TZW committed
228 229 230 231 232 233 234 235 236 237
                  });
                  delete vals[i];
                }
              }
              let params = {
                ...vals,
                id: drawer?.title == '编辑' ? drawer?.item?.id : '',
                userCharReqList,
              };
              delete params.shopId;
TZW's avatar
TZW committed
238
              //console.log(params, '!!!!');
TZW's avatar
TZW committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
              let res = await doFetch({
                url: urlParams.save,
                params,
              });
              if (res.code === '0000') {
                if (drawer?.title == '编辑') {
                  message.success('编辑成功!');
                } else {
                  message.success('新增成功!');
                }
                setDrawer((s) => ({
                  ...s,
                  visible: false,
                }));
                actionRef.current.reload();
              }
            }}
          />
        );
        break;

      default:
        return null;
    }
  }

wuhao's avatar
wuhao committed
265 266 267
  return (
    <div style={{ position: 'relative' }}>
      <AutoTable
TZW's avatar
TZW committed
268
        pagetitle={<h3 className="page-title">用户管理</h3>}
wuhao's avatar
wuhao committed
269
        columns={columns}
TZW's avatar
TZW committed
270
        path={urlParams.list}
wuhao's avatar
wuhao committed
271 272
        actionRef={actionRef}
        pageextra={'add'}
TZW's avatar
TZW committed
273
        resizeable={false}
wuhao's avatar
wuhao committed
274 275 276
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
TZW's avatar
TZW committed
277
            type: 'primary',
wuhao's avatar
wuhao committed
278 279
            disabled: false,
            onClick: () => {
TZW's avatar
TZW committed
280
              // let res = await doFetch({url:})
TZW's avatar
TZW committed
281
              setDrawer((s) => ({
wuhao's avatar
wuhao committed
282 283
                ...s,
                visible: true,
TZW's avatar
TZW committed
284 285 286
                item: {
                  status: 1,
                },
TZW's avatar
TZW committed
287 288
                detailpath: '',
                params: {},
wuhao's avatar
wuhao committed
289
                title: '新增',
TZW's avatar
TZW committed
290 291
                type: 'add',
                val: 'only',
wuhao's avatar
wuhao committed
292 293 294 295 296 297 298 299
              }));
            },
          },
        }}
      />

      <DrawerPro
        fields={columns}
TZW's avatar
TZW committed
300 301 302
        // detailpath={urlParams.detail}
        // params={{ id: drawer?.item?.id }}
        defaultFormValue={drawer?.item ?? {}}
wuhao's avatar
wuhao committed
303 304 305
        formRef={formRef}
        placement="right"
        onClose={() => {
TZW's avatar
TZW committed
306
          setDrawer((s) => ({
wuhao's avatar
wuhao committed
307 308 309 310 311
            ...s,
            visible: false,
          }));
        }}
        {...drawer}
TZW's avatar
TZW committed
312
      >
TZW's avatar
TZW committed
313
        {selectType(drawer?.type)}
TZW's avatar
TZW committed
314
      </DrawerPro>
wuhao's avatar
wuhao committed
315 316 317 318
    </div>
  );
}

TZW's avatar
TZW committed
319
export default User;