Analysis.js 15.4 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
2
import { connect } from 'dva';
ddcat1115's avatar
ddcat1115 committed
3
import { injectIntl } from 'react-intl';
niko's avatar
niko committed
4 5 6 7 8 9 10 11 12 13 14 15 16
import {
  Row,
  Col,
  Icon,
  Card,
  Tabs,
  Table,
  Radio,
  DatePicker,
  Tooltip,
  Menu,
  Dropdown,
} from 'antd';
偏右's avatar
偏右 committed
17
import {
niko's avatar
niko committed
18 19 20 21 22 23 24 25
  ChartCard,
  MiniArea,
  MiniBar,
  MiniProgress,
  Field,
  Bar,
  Pie,
  TimelineChart,
niko's avatar
niko committed
26 27 28
} from 'components/Charts';
import Trend from 'components/Trend';
import NumberInfo from 'components/NumberInfo';
jim's avatar
jim committed
29
import numeral from 'numeral';
愚道's avatar
愚道 committed
30
import GridContent from '../layouts/GridContent';
jim's avatar
jim committed
31
import Yuan from '../../utils/Yuan';
32 33 34 35
import { getTimeDistance } from '../../utils/utils';

import styles from './Analysis.less';

afc163's avatar
afc163 committed
36
const { TabPane } = Tabs;
37 38 39 40 41 42 43 44 45 46
const { RangePicker } = DatePicker;

const rankingListData = [];
for (let i = 0; i < 7; i += 1) {
  rankingListData.push({
    title: `工专路 ${i} 号店`,
    total: 323234,
  });
}

Andreas Cederström's avatar
Andreas Cederström committed
47 48 49
@connect(({ chart, loading }) => ({
  chart,
  loading: loading.effects['chart/fetch'],
50
}))
ddcat1115's avatar
ddcat1115 committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
class Analysis extends Component {
  constructor(props) {
    super(props);
    const { intl } = props;
    this.rankingListData = [];
    for (let i = 0; i < 7; i += 1) {
      this.rankingListData.push({
        title: intl.formatMessage({ id: 'app.analysis.test' }, { no: i }),
        total: 323234,
      });
    }
    this.state = {
      salesType: 'all',
      currentTabKey: '',
      rangePickerValue: getTimeDistance('year'),
    };
  }
68 69 70
  state = {
    salesType: 'all',
    currentTabKey: '',
afc163's avatar
afc163 committed
71
    rangePickerValue: getTimeDistance('year'),
niko's avatar
niko committed
72
  };
73 74

  componentDidMount() {
75 76
    const { dispatch } = this.props;
    dispatch({
77
      type: 'chart/fetch',
78
    });
79 80 81 82 83 84 85 86 87
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'chart/clear',
    });
  }

jim's avatar
jim committed
88
  handleChangeSalesType = e => {
89 90 91
    this.setState({
      salesType: e.target.value,
    });
niko's avatar
niko committed
92
  };
93

jim's avatar
jim committed
94
  handleTabChange = key => {
95 96 97
    this.setState({
      currentTabKey: key,
    });
niko's avatar
niko committed
98
  };
99

jim's avatar
jim committed
100
  handleRangePickerChange = rangePickerValue => {
101
    const { dispatch } = this.props;
102 103 104
    this.setState({
      rangePickerValue,
    });
nikogu's avatar
nikogu committed
105

106
    dispatch({
nikogu's avatar
nikogu committed
107 108
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
109
  };
110

jim's avatar
jim committed
111
  selectDate = type => {
112
    const { dispatch } = this.props;
113 114 115 116
    this.setState({
      rangePickerValue: getTimeDistance(type),
    });

117
    dispatch({
118 119
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
120
  };
121

afc163's avatar
afc163 committed
122 123 124 125 126 127
  isActive(type) {
    const { rangePickerValue } = this.state;
    const value = getTimeDistance(type);
    if (!rangePickerValue[0] || !rangePickerValue[1]) {
      return;
    }
niko's avatar
niko committed
128 129 130 131
    if (
      rangePickerValue[0].isSame(value[0], 'day') &&
      rangePickerValue[1].isSame(value[1], 'day')
    ) {
afc163's avatar
afc163 committed
132 133 134 135
      return styles.currentDate;
    }
  }

136
  render() {
137
    const { rangePickerValue, salesType, currentTabKey } = this.state;
Andreas Cederström's avatar
Andreas Cederström committed
138
    const { chart, loading } = this.props;
139 140
    const {
      visitData,
afc163's avatar
afc163 committed
141
      visitData2,
142 143 144 145 146 147 148
      salesData,
      searchData,
      offlineData,
      offlineChartData,
      salesTypeData,
      salesTypeDataOnline,
      salesTypeDataOffline,
afc163's avatar
afc163 committed
149
    } = chart;
150

niko's avatar
niko committed
151 152 153
    const salesPieData =
      salesType === 'all'
        ? salesTypeData
jim's avatar
jim committed
154 155 156
        : salesType === 'online'
          ? salesTypeDataOnline
          : salesTypeDataOffline;
157

afc163's avatar
afc163 committed
158 159 160 161 162 163 164
    const menu = (
      <Menu>
        <Menu.Item>操作一</Menu.Item>
        <Menu.Item>操作二</Menu.Item>
      </Menu>
    );

165 166
    const iconGroup = (
      <span className={styles.iconGroup}>
afc163's avatar
afc163 committed
167 168 169
        <Dropdown overlay={menu} placement="bottomRight">
          <Icon type="ellipsis" />
        </Dropdown>
170 171 172
      </span>
    );

afc163's avatar
afc163 committed
173 174 175
    const salesExtra = (
      <div className={styles.salesExtraWrap}>
        <div className={styles.salesExtra}>
afc163's avatar
afc163 committed
176 177 178 179 180 181 182 183 184 185 186 187
          <a className={this.isActive('today')} onClick={() => this.selectDate('today')}>
            今日
          </a>
          <a className={this.isActive('week')} onClick={() => this.selectDate('week')}>
            本周
          </a>
          <a className={this.isActive('month')} onClick={() => this.selectDate('month')}>
            本月
          </a>
          <a className={this.isActive('year')} onClick={() => this.selectDate('year')}>
            全年
          </a>
afc163's avatar
afc163 committed
188 189 190 191 192 193
        </div>
        <RangePicker
          value={rangePickerValue}
          onChange={this.handleRangePickerChange}
          style={{ width: 256 }}
        />
194
      </div>
afc163's avatar
afc163 committed
195
    );
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

    const columns = [
      {
        title: '排名',
        dataIndex: 'index',
        key: 'index',
      },
      {
        title: '搜索关键词',
        dataIndex: 'keyword',
        key: 'keyword',
        render: text => <a href="/">{text}</a>,
      },
      {
        title: '用户数',
        dataIndex: 'count',
        key: 'count',
        sorter: (a, b) => a.count - b.count,
afc163's avatar
afc163 committed
214
        className: styles.alignRight,
215 216 217 218 219 220 221
      },
      {
        title: '周涨幅',
        dataIndex: 'range',
        key: 'range',
        sorter: (a, b) => a.range - b.range,
        render: (text, record) => (
偏右's avatar
偏右 committed
222
          <Trend flag={record.status === 1 ? 'down' : 'up'}>
223 224 225 226
            <span style={{ marginRight: 4 }}>
              {text}
              %
            </span>
偏右's avatar
偏右 committed
227
          </Trend>
228
        ),
afc163's avatar
afc163 committed
229
        align: 'right',
230 231 232
      },
    ];

nikogu's avatar
nikogu committed
233 234
    const activeKey = currentTabKey || (offlineData[0] && offlineData[0].name);

235
    const CustomTab = ({ data, currentTabKey: currentKey }) => (
niko's avatar
niko committed
236
      <Row gutter={8} style={{ width: 138, margin: '8px 0' }}>
237 238 239 240
        <Col span={12}>
          <NumberInfo
            title={data.name}
            subTitle="转化率"
偏右's avatar
偏右 committed
241 242
            gap={2}
            total={`${data.cvr * 100}%`}
niko's avatar
niko committed
243
            theme={currentKey !== data.name && 'light'}
244 245 246 247
          />
        </Col>
        <Col span={12} style={{ paddingTop: 36 }}>
          <Pie
nikogu's avatar
nikogu committed
248
            animate={false}
niko's avatar
niko committed
249
            color={currentKey !== data.name && '#BDE4FF'}
250 251 252 253 254 255 256 257 258 259 260 261 262
            inner={0.55}
            tooltip={false}
            margin={[0, 0, 0, 0]}
            percent={data.cvr * 100}
            height={64}
          />
        </Col>
      </Row>
    );

    const topColResponsiveProps = {
      xs: 24,
      sm: 12,
niko's avatar
niko committed
263 264 265
      md: 12,
      lg: 12,
      xl: 6,
266 267 268 269
      style: { marginBottom: 24 },
    };

    return (
jim's avatar
jim committed
270
      <GridContent>
271 272 273 274
        <Row gutter={24}>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
niko's avatar
niko committed
275
              title="总销售额"
niko's avatar
niko committed
276 277 278 279 280
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
281
              loading={loading}
jim's avatar
jim committed
282
              total={() => <Yuan>126560</Yuan>}
afc163's avatar
afc163 committed
283
              footer={<Field label="日均销售额" value={`¥${numeral(12423).format('0,0')}`} />}
284 285
              contentHeight={46}
            >
afc163's avatar
afc163 committed
286
              <Trend flag="up" style={{ marginRight: 16 }}>
287 288
                周同比
                <span className={styles.trendText}>12%</span>
afc163's avatar
afc163 committed
289 290
              </Trend>
              <Trend flag="down">
291 292
                日环比
                <span className={styles.trendText}>11%</span>
afc163's avatar
afc163 committed
293
              </Trend>
294 295 296 297 298
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
299
              loading={loading}
300
              title="访问量"
niko's avatar
niko committed
301 302 303 304 305
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
306 307 308 309
              total={numeral(8846).format('0,0')}
              footer={<Field label="日访问量" value={numeral(1234).format('0,0')} />}
              contentHeight={46}
            >
niko's avatar
niko committed
310
              <MiniArea color="#975FE4" data={visitData} />
311 312 313 314 315
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
316
              loading={loading}
317
              title="支付笔数"
niko's avatar
niko committed
318 319 320 321 322
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
323 324 325 326
              total={numeral(6560).format('0,0')}
              footer={<Field label="转化率" value="60%" />}
              contentHeight={46}
            >
niko's avatar
niko committed
327
              <MiniBar data={visitData} />
328 329 330 331
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
332
              loading={loading}
333
              bordered={false}
niko's avatar
niko committed
334
              title="运营活动效果"
niko's avatar
niko committed
335 336 337 338 339
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
340
              total="78%"
afc163's avatar
afc163 committed
341
              footer={
afc163's avatar
afc163 committed
342
                <div style={{ whiteSpace: 'nowrap', overflow: 'hidden' }}>
afc163's avatar
afc163 committed
343
                  <Trend flag="up" style={{ marginRight: 16 }}>
344 345
                    周同比
                    <span className={styles.trendText}>12%</span>
afc163's avatar
afc163 committed
346 347
                  </Trend>
                  <Trend flag="down">
348 349
                    日环比
                    <span className={styles.trendText}>11%</span>
afc163's avatar
afc163 committed
350
                  </Trend>
偏右's avatar
偏右 committed
351
                </div>
afc163's avatar
afc163 committed
352
              }
353 354
              contentHeight={46}
            >
niko's avatar
niko committed
355
              <MiniProgress percent={78} strokeWidth={8} target={80} color="#13C2C2" />
356 357 358 359
            </ChartCard>
          </Col>
        </Row>

niko's avatar
niko committed
360
        <Card loading={loading} bordered={false} bodyStyle={{ padding: 0 }}>
361
          <div className={styles.salesCard}>
afc163's avatar
afc163 committed
362
            <Tabs tabBarExtraContent={salesExtra} size="large" tabBarStyle={{ marginBottom: 24 }}>
363
              <TabPane tab="销售额" key="sales">
niko's avatar
niko committed
364
                <Row>
nikogu's avatar
nikogu committed
365
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
366
                    <div className={styles.salesBar}>
nikogu's avatar
nikogu committed
367
                      <Bar height={295} title="销售额趋势" data={salesData} />
368
                    </div>
369
                  </Col>
nikogu's avatar
nikogu committed
370
                  <Col xl={8} lg={12} md={12} sm={24} xs={24}>
371 372 373
                    <div className={styles.salesRank}>
                      <h4 className={styles.rankingTitle}>门店销售额排名</h4>
                      <ul className={styles.rankingList}>
ddcat1115's avatar
ddcat1115 committed
374
                        {this.rankingListData.map((item, i) => (
niko's avatar
niko committed
375 376 377 378 379 380
                          <li key={item.title}>
                            <span className={i < 3 ? styles.active : ''}>{i + 1}</span>
                            <span>{item.title}</span>
                            <span>{numeral(item.total).format('0,0')}</span>
                          </li>
                        ))}
381 382
                      </ul>
                    </div>
383 384 385
                  </Col>
                </Row>
              </TabPane>
386
              <TabPane tab="访问量" key="views">
陈帅's avatar
陈帅 committed
387
                <Row>
388 389
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
                    <div className={styles.salesBar}>
niko's avatar
niko committed
390
                      <Bar height={292} title="访问量趋势" data={salesData} />
391 392 393 394 395 396
                    </div>
                  </Col>
                  <Col xl={8} lg={12} md={12} sm={24} xs={24}>
                    <div className={styles.salesRank}>
                      <h4 className={styles.rankingTitle}>门店访问量排名</h4>
                      <ul className={styles.rankingList}>
ddcat1115's avatar
ddcat1115 committed
397
                        {this.rankingListData.map((item, i) => (
niko's avatar
niko committed
398
                          <li key={item.title}>
399
                            <span className={i < 3 ? styles.active : ''}>{i + 1}</span>
niko's avatar
niko committed
400 401 402 403
                            <span>{item.title}</span>
                            <span>{numeral(item.total).format('0,0')}</span>
                          </li>
                        ))}
404 405 406 407
                      </ul>
                    </div>
                  </Col>
                </Row>
408 409 410 411 412 413
              </TabPane>
            </Tabs>
          </div>
        </Card>

        <Row gutter={24}>
414
          <Col xl={12} lg={24} md={24} sm={24} xs={24}>
415
            <Card
416
              loading={loading}
417 418 419 420 421 422 423 424
              bordered={false}
              title="线上热门搜索"
              extra={iconGroup}
              style={{ marginTop: 24 }}
            >
              <Row gutter={68}>
                <Col sm={12} xs={24} style={{ marginBottom: 24 }}>
                  <NumberInfo
afc163's avatar
afc163 committed
425 426 427 428 429 430 431 432
                    subTitle={
                      <span>
                        搜索用户数
                        <Tooltip title="指标文案">
                          <Icon style={{ marginLeft: 8 }} type="info-circle-o" />
                        </Tooltip>
                      </span>
                    }
偏右's avatar
偏右 committed
433
                    gap={8}
434 435 436 437
                    total={numeral(12321).format('0,0')}
                    status="up"
                    subTotal={17.1}
                  />
niko's avatar
niko committed
438
                  <MiniArea line height={45} data={visitData2} />
439 440 441 442 443 444 445
                </Col>
                <Col sm={12} xs={24} style={{ marginBottom: 24 }}>
                  <NumberInfo
                    subTitle="人均搜索次数"
                    total={2.7}
                    status="down"
                    subTotal={26.2}
偏右's avatar
偏右 committed
446
                    gap={8}
447
                  />
niko's avatar
niko committed
448
                  <MiniArea line height={45} data={visitData2} />
449 450 451 452
                </Col>
              </Row>
              <Table
                rowKey={record => record.index}
afc163's avatar
afc163 committed
453
                size="small"
454 455 456 457 458 459 460 461 462
                columns={columns}
                dataSource={searchData}
                pagination={{
                  style: { marginBottom: 0 },
                  pageSize: 5,
                }}
              />
            </Card>
          </Col>
463
          <Col xl={12} lg={24} md={24} sm={24} xs={24}>
464
            <Card
465
              loading={loading}
niko's avatar
niko committed
466
              className={styles.salesCard}
467 468
              bordered={false}
              title="销售额类别占比"
469
              bodyStyle={{ padding: 24 }}
niko's avatar
niko committed
470
              extra={
niko's avatar
niko committed
471 472 473 474 475 476 477 478 479 480
                <div className={styles.salesCardExtra}>
                  {iconGroup}
                  <div className={styles.salesTypeRadio}>
                    <Radio.Group value={salesType} onChange={this.handleChangeSalesType}>
                      <Radio.Button value="all">全部渠道</Radio.Button>
                      <Radio.Button value="online">线上</Radio.Button>
                      <Radio.Button value="offline">门店</Radio.Button>
                    </Radio.Group>
                  </div>
                </div>
niko's avatar
niko committed
481
              }
afc163's avatar
afc163 committed
482
              style={{ marginTop: 24, minHeight: 509 }}
483
            >
偏右's avatar
偏右 committed
484
              <h4 style={{ marginTop: 8, marginBottom: 32 }}>销售额</h4>
afc163's avatar
afc163 committed
485 486 487
              <Pie
                hasLegend
                subTitle="销售额"
jim's avatar
jim committed
488
                total={() => <Yuan>{salesPieData.reduce((pre, now) => now.y + pre, 0)}</Yuan>}
afc163's avatar
afc163 committed
489
                data={salesPieData}
jim's avatar
jim committed
490
                valueFormat={value => <Yuan>{value}</Yuan>}
afc163's avatar
afc163 committed
491 492 493
                height={248}
                lineWidth={4}
              />
494 495 496 497 498
            </Card>
          </Col>
        </Row>

        <Card
499
          loading={loading}
niko's avatar
niko committed
500
          className={styles.offlineCard}
501
          bordered={false}
niko's avatar
niko committed
502 503
          bodyStyle={{ padding: '0 0 32px 0' }}
          style={{ marginTop: 32 }}
504
        >
niko's avatar
niko committed
505 506 507 508 509 510 511 512 513 514 515 516
          <Tabs activeKey={activeKey} onChange={this.handleTabChange}>
            {offlineData.map(shop => (
              <TabPane tab={<CustomTab data={shop} currentTabKey={activeKey} />} key={shop.name}>
                <div style={{ padding: '0 24px' }}>
                  <TimelineChart
                    height={400}
                    data={offlineChartData}
                    titleMap={{ y1: '客流量', y2: '支付笔数' }}
                  />
                </div>
              </TabPane>
            ))}
517 518
          </Tabs>
        </Card>
jim's avatar
jim committed
519
      </GridContent>
520 521
    );
  }
afc163's avatar
afc163 committed
522
}
ddcat1115's avatar
ddcat1115 committed
523 524

export default injectIntl(Analysis);