| | |
| | | import React, {Component} from 'react' |
| | | import PropTypes from 'prop-types' |
| | | import { fromJS } from 'immutable' |
| | | import { Form, Row, Col, Select, Radio, Tooltip, Input } from 'antd' |
| | | import { Form, Row, Col, Select, Radio, Tooltip, Input, message } from 'antd' |
| | | import { QuestionCircleOutlined } from '@ant-design/icons' |
| | | |
| | | import Api from '@/api' |
| | |
| | | state = { |
| | | wxTemps: [], |
| | | selectTemp: null, |
| | | miniTemps: [] |
| | | WXApps: null |
| | | } |
| | | |
| | | componentDidMount() { |
| | | let wxTemps = sessionStorage.getItem('wxTemplates') |
| | | const { appType, verify } = this.props |
| | | |
| | | if (window.GLOB.WXAppID && window.GLOB.nginx && !wxTemps) { |
| | | Api.wxAccessToken().then(res => { |
| | | if (res.access_token) { |
| | | Api.wxNginxRequest(`cgi-bin/template/get_all_private_template?access_token=${res.access_token}`, 'get').then(res => { |
| | | let temps = [] |
| | | if (res.template_list) { |
| | | temps = res.template_list.filter(item => { |
| | | if (!item.primary_industry || sysTempsIds.includes(item.template_id)) return false |
| | | if (item.content) { |
| | | item.content = item.content.replace('{{first.DATA}}\n', '').replace('\n{{remark.DATA}}', '') |
| | | } |
| | | let appId = window.GLOB.WXAppID || '' |
| | | |
| | | return true |
| | | }) |
| | | } |
| | | |
| | | sessionStorage.setItem('wxTemplates', JSON.stringify(temps)) |
| | | localStorage.setItem('wxTemplates', JSON.stringify(temps)) |
| | | if (window.GLOB.WXApps && !appType) { |
| | | let apps = window.GLOB.WXApps.filter(app => app.appType === 'public') |
| | | |
| | | localStorage.removeItem('wxTemplates') |
| | | if (apps.length > 0) { |
| | | this.setState({WXApps: apps}) |
| | | } |
| | | |
| | | this.resetTemps(temps) |
| | | }) |
| | | } else { |
| | | sessionStorage.setItem('wxTemplates', JSON.stringify([])) |
| | | localStorage.setItem('wxTemplates', JSON.stringify([])) |
| | | if (verify.wxAppId && apps.findIndex(item => item.appId === verify.wxAppId) > -1) { |
| | | appId = verify.wxAppId |
| | | } |
| | | } |
| | | |
| | | localStorage.removeItem('wxTemplates') |
| | | } |
| | | }) |
| | | let wxTemps = sessionStorage.getItem('wxTemplates' + appId) |
| | | |
| | | if (appId && window.GLOB.nginx && !wxTemps) { |
| | | this.getTemps(appId) |
| | | } else if (wxTemps) { |
| | | wxTemps = JSON.parse(wxTemps) |
| | | |
| | |
| | | } else { |
| | | this.resetTemps([]) |
| | | } |
| | | } |
| | | |
| | | getTemps = (appId) => { |
| | | Api.wxAccessToken(appId).then(res => { |
| | | if (res.status && res.access_token) { |
| | | Api.wxNginxRequest(`cgi-bin/template/get_all_private_template?access_token=${res.access_token}`, 'get').then(res => { |
| | | let temps = [] |
| | | if (res.template_list) { |
| | | temps = res.template_list.filter(item => { |
| | | if (!item.primary_industry || sysTempsIds.includes(item.template_id)) return false |
| | | if (item.content) { |
| | | item.content = item.content.replace('{{first.DATA}}\n', '').replace('\n{{remark.DATA}}', '') |
| | | } |
| | | |
| | | return true |
| | | }) |
| | | } else if (res.errcode && res.errmsg) { |
| | | message.warning(res.errcode + ': ' + res.errmsg) |
| | | } |
| | | |
| | | sessionStorage.setItem('wxTemplates' + appId, JSON.stringify(temps)) |
| | | |
| | | this.resetTemps(temps) |
| | | }) |
| | | } else { |
| | | message.warning(res.message || '微信授权失败!') |
| | | sessionStorage.setItem('wxTemplates' + appId, JSON.stringify([])) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | resetTemps = (wxTemps) => { |
| | |
| | | delete _verify.pre_func |
| | | } |
| | | if (_verify.wxNote !== 'true') { |
| | | delete _verify.wxAppId |
| | | delete _verify.wxTemplateId |
| | | delete _verify.wxNoteLink |
| | | delete _verify.wxNoteLinkUrl |
| | |
| | | this.props.onChange(_verify) |
| | | } |
| | | |
| | | onWxAppChange = (val) => { |
| | | const { verify } = this.props |
| | | |
| | | let _verify = {...verify, wxAppId: val, wxNote: 'false'} |
| | | |
| | | delete _verify.wxTemplateId |
| | | delete _verify.wxNoteLink |
| | | delete _verify.wxNoteLinkUrl |
| | | delete _verify.wxNoteMiniId |
| | | delete _verify.wxNoteLinkMenuId |
| | | delete _verify.wxNoteCallback |
| | | delete _verify.wxCustomTempId |
| | | |
| | | _verify.wxNoteKeys = null |
| | | |
| | | if (this.state.selectTemp) { |
| | | this.setState({selectTemp: null}) |
| | | } |
| | | |
| | | let _verify_ = fromJS(_verify).toJS() |
| | | _verify_.wxNote = 'true' |
| | | |
| | | this.props.onChange(_verify) |
| | | |
| | | setTimeout(() => { |
| | | this.props.onChange(_verify_) |
| | | |
| | | let wxTemps = sessionStorage.getItem('wxTemplates' + val) |
| | | |
| | | if (wxTemps) { |
| | | wxTemps = JSON.parse(wxTemps) |
| | | |
| | | this.resetTemps(wxTemps) |
| | | } else { |
| | | this.getTemps(val) |
| | | } |
| | | }, 20) |
| | | } |
| | | |
| | | onWxNoteKeyChange = (key, val) => { |
| | | let _verify = fromJS(this.props.verify).toJS() |
| | | |
| | |
| | | |
| | | render() { |
| | | const { unionFields, verify, notes, emailCodes, card, appType, columns, fields } = this.props |
| | | const { wxTemps, selectTemp } = this.state |
| | | const { wxTemps, selectTemp, WXApps } = this.state |
| | | const formItemLayout = { |
| | | labelCol: { |
| | | xs: { span: 24 }, |
| | |
| | | </Radio.Group> |
| | | </Form.Item> |
| | | </Col> |
| | | {verify.wxNote === 'true' && WXApps ? <Col span={8}> |
| | | <Form.Item label={ |
| | | <Tooltip placement="bottomLeft" title="请选择发送消息的公众号。"> |
| | | <QuestionCircleOutlined className="mk-form-tip" /> |
| | | 公众号 |
| | | </Tooltip> |
| | | } required> |
| | | <Select value={verify.wxAppId || window.GLOB.WXAppID} onSelect={this.onWxAppChange}> |
| | | {WXApps.map(option => |
| | | <Select.Option key={option.appId} value={option.appId}> |
| | | {option.appName} |
| | | </Select.Option> |
| | | )} |
| | | </Select> |
| | | </Form.Item> |
| | | </Col> : null} |
| | | {verify.wxNote === 'true' ? <Col span={8}> |
| | | <Form.Item label="消息模板" required> |
| | | <Select value={verify.wxTemplateId} onSelect={this.onWxTemplateChange}> |