king
2021-07-28 137fb8ea6af2789b3238b22bac31d80bced41dfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Input, Form, Row, Col, Pagination, Empty, Button, Modal, notification } from 'antd'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
import asyncComponent from '@/utils/asyncComponent'
import './index.scss'
 
const { TextArea } = Input
const { Search } = Input
const FileUpload = asyncComponent(() => import('@/tabviews/zshare/fileupload'))
const Video = asyncComponent(() => import('@/menu/picturecontroller/video'))
const Image = asyncComponent(() => import('@/components/Image'))
const EditForm = asyncComponent(() => import('@/menu/picturecontroller/editform'))
 
class PopSource extends Component {
  static propTpyes = {
    btnlog: PropTypes.array,
    keyword: PropTypes.string,
    handlelog: PropTypes.func
  }
 
  state = {
    url: '',
    originlist: [],
    list: [],
    pagelist: [],
    searchKey: '',
    pageSize: 12,
    pageIndex: 1,
    selectId: '',
    editvisible: false,
    card: null
  }
 
  UNSAFE_componentWillMount () {
    if (this.props.keyword === 'system') {
      this.init()
    }
  }
 
  componentDidMount() {
    try {
      let _form = document.getElementById('source-input')
 
      if (_form && _form.focus) {
        _form.focus()
      }
    } catch {}
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  init = () => {
    const { type } = this.props
 
    let originlist = []
    if (type === 'video') {
      let videos = sessionStorage.getItem('app_videos')
      try {
        originlist = JSON.parse(videos)
      } catch {
        originlist = []
      }
    } else {
      let pictures = sessionStorage.getItem('app_pictures')
      try {
        originlist = JSON.parse(pictures)
      } catch {
        originlist = []
      }
    }
 
    let list = originlist
    let pagelist = list.filter((item, index) => index < this.state.pageSize)
 
    this.setState({originlist, list, url: '', searchKey: '', pageIndex: 1, pagelist})
  }
 
  changeSearch = () => {
    const { originlist, pageSize, searchKey } = this.state
    let list = originlist.filter(item => item.remark.indexOf(searchKey) > -1)
    let pagelist = list.filter((item, index) => index < pageSize)
 
    this.setState({list, pagelist, pageIndex: 1})
  }
 
  changeValue = (e) => {
    this.setState({url: e.target.value})
  }
 
  changeSize = (page) => {
    const { list, pageSize } = this.state
    let pagelist = list.filter((item, index) => index < pageSize * page && index >= pageSize * (page - 1))
    this.setState({pageIndex: page, pagelist})
  }
 
  changeFile = (val) => {
    this.setState({url: val})
  }
 
  selectItem = (item) => {
    if (item.linkurl) {
      this.setState({url: item.linkurl, selectId: item.id})
    }
  }
 
  handleSource = (item) => {
    this.setState({
      editvisible: true,
      card: item
    })
  }
 
  save = () => {
    const { card } = this.state
    this.editFormRef.handleConfirm().then(res => {
      res = {...card, ...res}
 
      if (!res.id) {
        res.id = Utils.getuuid()
      }
 
      Api.getSystemConfig({
        func: 's_url_db_adduptdel',
        id: res.id,
        PageIndex: 0, // 0 代表全部
        PageSize: 0,  // 0 代表全部
        remark: res.remark || '',
        linkurl: res.linkurl,
        typecharone: card.typecharone,
        type: 'add'
      }).then(result => {
        if (result.status) {
          if (card.typecharone === 'image') {
            sessionStorage.setItem('app_pictures', JSON.stringify(result.data || []))
            this.init()
          } else {
            sessionStorage.setItem('app_videos', JSON.stringify(result.data || []))
            this.init()
          }
          this.setState({editvisible: false})
        } else {
          notification.warning({
            top: 92,
            message: result.message,
            duration: 5
          })
        }
      })
    })
  }
 
  render () {
    const { type, keyword } = this.props
    const { list, url, pagelist, searchKey, pageIndex, pageSize, selectId, editvisible, card } = this.state
    
    return (
      <div className="mk-source-pop-wrap">
        {keyword === 'input' ? <Form.Item label="地址" labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={{xs: { span: 24 }, sm: { span: 20 }}}>
          <TextArea id="source-input" value={url} rows={4} onChange={this.changeValue}/>
        </Form.Item> : null}
        {keyword === 'upload' ? <Form.Item label="上传" labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={{xs: { span: 24 }, sm: { span: 20 }}}>
          <FileUpload config={{
            initval: '',
            suffix: type === 'video' ? '.mp4,.webm,.ogg' : '.jpg,.png,.gif,.pjp,.pjpeg,.jpeg,.jfif,.webp',
            maxfile: 1,
            fileType: type === 'video' ? 'text' : 'picture'
          }} onChange={this.changeFile} />
        </Form.Item> : null}
        {keyword === 'system' ?
          <Search value={searchKey} placeholder="" onChange={(e) => this.setState({searchKey: e.target.value})} onSearch={this.changeSearch} enterButton/> : null}
        {keyword === 'system' ? <Button className="picture-plus mk-green" icon="plus" onClick={() => this.handleSource({typecharone: type})}>
          添加
        </Button> : null}
        {keyword === 'system' && list.length ?
          <Row gutter={16} style={{minHeight: '250px'}}>
            {pagelist.map(item => (
              <Col span={4} key={item.id}>
                <div className={'image-video-box' + (selectId === item.id ? ' active' : '')} onClick={() => this.selectItem(item)}>
                  <div className="image-video-box-body">
                    {type !== 'video' ? <Image url={item.linkurl} /> : null}
                    {type === 'video' ? <Video value={item.linkurl} /> : null}
                  </div>
                </div>
              </Col>
            ))}
          </Row> : null}
        {keyword === 'system' && list.length === 0 ? <Empty description={null}/> : null}
        {keyword === 'system' && list.length > pageSize ? <Pagination size="small" current={pageIndex} pageSize={pageSize} onChange={this.changeSize} total={list.length} /> : null}
        <Modal
          title={'新建'}
          wrapClassName="picture-edit-model"
          visible={editvisible}
          width={600}
          maskClosable={false}
          onOk={this.save}
          onCancel={() => {this.setState({editvisible: false})}}
          destroyOnClose
        >
          <EditForm card={card} wrappedComponentRef={(inst) => this.editFormRef = inst} inputSubmit={this.save}/>
        </Modal>
      </div>
    )
  }
}
 
export default PopSource