king
2024-08-25 326aa6b3effaccc71cfe0775d47b0f29eb3695a6
src/views/interface/history/index.jsx
@@ -1,6 +1,7 @@
import React, {Component} from 'react'
import { fromJS } from 'immutable'
import { Input, Modal, Empty, Icon } from 'antd'
import { Input, Modal } from 'antd'
import { DeleteOutlined, RightOutlined } from '@ant-design/icons'
import moment from 'moment'
import Api from '@/views/interface/api'
@@ -21,69 +22,13 @@
  componentDidMount() {
    MKEmitter.addListener('insertInterface', this.insertInterface)
    setTimeout(() => {
      Api.getInterfaces().then(res => {
        if (!res || !res.rows) return
        let rows = [...res.rows]
        rows.sort((a,b) => {
          return a.createDate < b.createDate ? 1 : -1
        })
        let list = []
        let item = null
        rows.forEach(m => {
          let date = m.createDate.substring(0, 10)
          if (m.params) {
            try {
              m.params = JSON.parse(m.params)
            } catch {
              m.params = []
            }
          } else {
            m.params = []
          }
          if (m.headers) {
            try {
              m.headers = JSON.parse(m.headers)
            } catch {
              m.headers = []
            }
          } else {
            m.headers = []
          }
          if (m.formData) {
            try {
              m.formData = JSON.parse(m.formData)
            } catch {
              m.formData = []
            }
          } else {
            m.formData = []
          }
          if (item && item.date !== date) {
            list.push(item)
            item = null
          }
          if (!item) {
            item = {date, sublist: []}
            item.sublist.push(m)
          } else if (item && item.date === date) {
            item.sublist.push(m)
          }
        })
        if (item) {
          list.push(item)
        }
        this.setState({list, historys: fromJS(list).toJS()})
      })
      if (window.GLOB.IndexDB) {
        this.getHistory()
      } else {
        setTimeout(() => {
          this.getHistory()
        }, 1000)
      }
    }, 200)
  }
@@ -98,99 +43,178 @@
  }
  clear = () => {
    const _this = this
    const that = this
    confirm({
      content: 'Are you sure you want to clear all your history requests?',
      onOk() {
        Api.clearInterfaces().then(res => {
          if (res && res.rows.length === 0) {
            _this.setState({list: [], historys: []})
            Modal.success({
              title: '清除成功。'
            })
          } else {
            Modal.error({
              title: '清除失败!请刷新重试。'
            })
          }
        })
        Api.clearInterfaces()
        that.setState({list: [], historys: []})
      },
      onCancel() {}
    })
  }
  delete = (m) => {
    const { searchKey } = this.state
    Api.delInterface(m.uuid).then(res => {
      if (res) {
        let list = this.state.list.filter(item => {
          item.sublist = item.sublist.filter(cell => cell.uuid !== m.uuid)
          return item.sublist.length > 0
        })
  getHistory = () => {
    Api.getInterfaces().then(res => {
      if (!res) return
        let historys = fromJS(list).toJS()
        if (searchKey) {
          historys = historys.filter(item => {
            item.sublist = item.sublist.filter(cell => cell.interface.indexOf(searchKey) > -1)
            return item.sublist.length > 0
          })
      res.sort((a,b) => {
        return a.createDate < b.createDate ? 1 : -1
      })
      let list = []
      let item = null
      res.forEach(m => {
        let date = m.createDate.substring(0, 10)
        if (m.params) {
          try {
            m.params = JSON.parse(m.params)
          } catch (e) {
            m.params = []
          }
        } else {
          m.params = []
        }
        this.setState({list, historys})
      } else {
        Modal.error({
          title: '删除失败!请刷新重试。'
        })
        if (m.headers) {
          try {
            m.headers = JSON.parse(m.headers)
          } catch (e) {
            m.headers = []
          }
        } else {
          m.headers = []
        }
        if (m.formData) {
          try {
            m.formData = JSON.parse(m.formData)
          } catch (e) {
            m.formData = []
          }
        } else {
          m.formData = []
        }
        if (item && item.date !== date) {
          list.push(item)
          item = null
        }
        if (!item) {
          item = {date, sublist: []}
          item.sublist.push(m)
        } else if (item && item.date === date) {
          item.sublist.push(m)
        }
      })
      if (item) {
        list.push(item)
      }
      this.setState({list, historys: fromJS(list).toJS()})
    })
  }
  delete = (m) => {
    const { searchKey } = this.state
    Api.delInterface(m.id)
    let list = this.state.list.filter(item => {
      item.sublist = item.sublist.filter(cell => cell.id !== m.id)
      return item.sublist.length > 0
    })
    let historys = fromJS(list).toJS()
    if (searchKey) {
      historys = historys.filter(item => {
        item.sublist = item.sublist.filter(cell => cell.interface.indexOf(searchKey) > -1)
        return item.sublist.length > 0
      })
    }
    this.setState({list, historys})
  }
  insertInterface = (item) => {
    item.uuid = Utils.getuuid()
    item.id = Utils.getuuid()
    item.createDate = moment().format('YYYY-MM-DD HH:mm:ss')
    Api.writeInWebSql([item.uuid, item.createDate, item.method, item.interface, JSON.stringify(item.params), JSON.stringify(item.headers), item.active, item.raw, JSON.stringify(item.formData)]).then(res => {
      if (res) {
        let list = fromJS(this.state.list).toJS()
    Api.writeInIndexDB(item)
        if (list[0]) {
          if (list[0].date === item.createDate.substring(0, 10)) {
            list[0].sublist.unshift(item)
          } else {
            list.unshift({
              date: item.createDate.substring(0, 10),
              sublist: [item]
            })
          }
        } else {
          list.push({
            date: item.createDate.substring(0, 10),
            sublist: [item]
          })
        }
    let list = fromJS(this.state.list).toJS()
        let historys = fromJS(list).toJS()
        if (this.state.searchKey) {
          historys = historys.filter(item => {
            item.sublist = item.sublist.filter(cell => cell.interface.indexOf(this.state.searchKey) > -1)
            return item.sublist.length > 0
          })
        }
        this.setState({ list, historys })
    if (list[0]) {
      if (list[0].date === item.createDate.substring(0, 10)) {
        list[0].sublist.unshift(item)
      } else {
        Modal.error({
          title: '添加失败!请刷新重试。'
        list.unshift({
          date: item.createDate.substring(0, 10),
          sublist: [item]
        })
      }
    })
    } else {
      list.push({
        date: item.createDate.substring(0, 10),
        sublist: [item]
      })
    }
    let historys = fromJS(list).toJS()
    if (this.state.searchKey) {
      historys = historys.filter(item => {
        item.sublist = item.sublist.filter(cell => cell.interface.indexOf(this.state.searchKey) > -1)
        return item.sublist.length > 0
      })
    }
    this.setState({ list, historys })
  }
  use = (m) => {
    MKEmitter.emit('useInterface', fromJS(m).toJS())
  }
  uselogon = () => {
    let m = {
      active: 'raw',
      createDate: '',
      formData: [],
      headers: [],
      interface: window.GLOB.baseurl + 'webapi/dologon',
      method: 'POST',
      params: [],
      raw: "{\n \"UserName\":\"******\",\n \"Password\":\"******\",\n \"systemType\":\"local\",\n \"Type\":\"公钥\",\n \"privatekey\":\"私钥\",\n \"timestamp\":\"" + moment().format('YYYY-MM-DD HH:mm:ss') + "\",\n \"appkey\":\"" + window.GLOB.appkey + "\"\n}",
      id: 'dologon'
    }
    if (window.GLOB.mainSystemApi) {
      m.raw = "{\n \"UserName\":\"******\",\n \"Password\":\"******\",\n \"systemType\":\"local\",\n \"Type\":\"公钥\",\n \"privatekey\":\"私钥\",\n \"timestamp\":\"" + moment().format('YYYY-MM-DD HH:mm:ss') + "\",\n \"appkey\":\"" + window.GLOB.appkey + "\",\n \"rduri\":\"" + window.GLOB.mainSystemApi.replace(/\/webapi(.*)/, '/webapi/dologon') + "\"\n}"
    }
    MKEmitter.emit('useInterface', m)
  }
  usedostars = () => {
    let m = {
      active: 'raw',
      createDate: '',
      formData: [],
      headers: [],
      interface: window.GLOB.baseurl + 'webapi/dostars',
      method: 'POST',
      params: [],
      raw: "{\n \"func\":\"******\",\n \"LoginUID\":\"@loginuid@\",\n \"UserID\":\"@userid@\",\n \"nonc\":\"" + Utils.getguid() + "\",\n \"t\":" + parseInt(new Date().getTime() / 1000) + "\n}",
      id: 'dologon'
    }
    MKEmitter.emit('useInterface', m)
  }
  changeSearch = (value) => {
@@ -219,24 +243,38 @@
          <span onClick={this.clear}>Clear all</span>
        </div>
        <div className="list-view">
          {historys.length === 0 ?
            <Empty /> :
            historys.map((item, index) => (
              <div className="list-line" key={index}>
                <div className="line-title">{item.date}</div>
                {item.sublist.map(m => (
                  <div className="line-item" key={m.uuid}>
                    <div className="method">POST</div>
                    <div className="inter">{m.interface}</div>
                    <div className="action">
                      <Icon type="delete" onClick={() => this.delete(m)} />
                      <Icon type="right" onClick={() => this.use(m)} />
                    </div>
          {historys.map((item, index) => (
            <div className="list-line" key={index}>
              <div className="line-title">{item.date}</div>
              {item.sublist.map(m => (
                <div className="line-item" key={m.id}>
                  <div className="method">POST</div>
                  <div className="inter">{m.interface}</div>
                  <div className="action">
                    <DeleteOutlined onClick={() => this.delete(m)} />
                    <RightOutlined onClick={() => this.use(m)} />
                  </div>
                ))}
                </div>
              ))}
            </div>
          ))}
          <div className="list-line" key="example">
            <div className="line-title">示例</div>
              <div className="line-item" style={{cursor: 'pointer'}} key="dologon" onClick={this.uselogon}>
                <div className="method">POST</div>
                <div className="inter" style={{lineHeight: '40px'}}>dologon</div>
                <div className="action" style={{paddingLeft: '40px'}}>
                  <RightOutlined/>
                </div>
              </div>
            ))
          }
              <div className="line-item" style={{cursor: 'pointer'}} key="dostars" onClick={this.usedostars}>
                <div className="method">POST</div>
                <div className="inter" style={{lineHeight: '40px'}}>dostars</div>
                <div className="action" style={{paddingLeft: '40px'}}>
                  <RightOutlined/>
                </div>
              </div>
          </div>
        </div>
      </aside>
    )