page.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import { asyncSMessage } from '@src/services/AsyncTools';
  5. import UserLayout from '../../../layouts/User';
  6. import menu from '../index';
  7. import UserTable from '../../../components/UserTable';
  8. import UserAction from '../../../components/UserAction';
  9. import Tabs from '../../../components/Tabs';
  10. import { My } from '../../../stores/my';
  11. import { MessageType } from '../../../../Constant';
  12. import { getMap, formatDate } from '../../../../../src/services/Tools';
  13. import { User } from '../../../stores/user';
  14. const MessageTypeMap = getMap(MessageType, 'value', 'label');
  15. export default class extends Page {
  16. constructor(props) {
  17. props.size = 15;
  18. super(props);
  19. this.columns = [
  20. {
  21. title: '消息',
  22. key: 'title',
  23. render: (text, row) => {
  24. return <div onClick={() => {
  25. this.realMessage(row);
  26. }}>
  27. {!row.isRead ? <span className='dot'>{text}</span> : text}
  28. {row.content && <div className='ws-pl'>{row.content}{row.link && <a className='m-l-5' href={row.link} target="_blank">{row.linkTitle || '查看详情'}</a>}{row.linkSecond && <a className='m-l-5' href={row.linkSecond} target="_blank">{row.linkSecondTitle || '查看详情'}</a>}</div>}
  29. </div>;
  30. },
  31. },
  32. {
  33. title: '类型',
  34. key: 'type',
  35. width: 120,
  36. },
  37. {
  38. title: '发送时间',
  39. key: 'date',
  40. width: 180,
  41. render: (text) => {
  42. return <div className="sub">
  43. <div className="t-2 t-s-12">{text.split(' ')[0]}</div>
  44. <div className="t-6 t-s-12">{text.split(' ')[1]}</div>
  45. </div>;
  46. },
  47. }];
  48. }
  49. initState() {
  50. return {
  51. tab: '',
  52. filterMap: {},
  53. };
  54. }
  55. initData() {
  56. const data = Object.assign(this.state, this.state.search);
  57. if (data.order) {
  58. data.sortMap = { [data.order]: data.direction };
  59. }
  60. data.filterMap = this.state.search;
  61. data.messageTypeSelect = MessageType.map(row => {
  62. row.title = row.label;
  63. row.key = row.value;
  64. return row;
  65. });
  66. data.messageTypeSelect.unshift({
  67. title: '全部消息',
  68. key: '',
  69. });
  70. this.setState(data);
  71. My.message(Object.assign({ read: data.tab === 'unread' ? 0 : null }, this.state.search)).then(result => {
  72. result.list = result.list.map(row => {
  73. row.type = MessageTypeMap[row.type];
  74. row.date = formatDate(row.createTime, 'YYYY-MM-DD HH:mm:ss');
  75. return row;
  76. });
  77. result.page = data.page;
  78. this.setState(result);
  79. });
  80. }
  81. onFilter(value) {
  82. this.search(value);
  83. }
  84. onTabChange(tab) {
  85. const data = { tab };
  86. this.refreshQuery(data);
  87. }
  88. onChangePage(page) {
  89. this.search({ page });
  90. }
  91. readAllMessage() {
  92. My.readAllMessage().then(() => {
  93. asyncSMessage('操作成功');
  94. User.refreshToken();
  95. this.refresh();
  96. });
  97. }
  98. realMessage(record) {
  99. My.readMessage(record.id).then(() => {
  100. this.refresh();
  101. });
  102. }
  103. renderView() {
  104. const { config } = this.props;
  105. return <UserLayout active={config.key} menu={menu} center={this.renderTable()} />;
  106. }
  107. renderTable() {
  108. const { tab, list, messageTypeSelect, filterMap, page, total } = this.state;
  109. return (
  110. <div className="table-layout">
  111. <UserAction
  112. left={
  113. <Tabs
  114. type="tag"
  115. theme="white"
  116. size="small"
  117. space={5}
  118. width={54}
  119. active={tab}
  120. tabs={[{ key: '', title: '全部' }, { key: 'unread', title: '未读' }]}
  121. onChange={key => this.onTabChange(key)}
  122. />
  123. }
  124. right={<span style={{ cursor: 'pointer' }} onClick={() => {
  125. this.readAllMessage();
  126. }}>全部已读</span>}
  127. selectList={[
  128. {
  129. select: messageTypeSelect,
  130. placeholder: '消息类型',
  131. key: 'messageType',
  132. },
  133. ]}
  134. filterMap={filterMap}
  135. onFilter={value => this.onFilter(value)}
  136. />
  137. <UserTable
  138. size="small"
  139. columns={this.columns}
  140. data={list}
  141. current={page}
  142. total={total}
  143. pageSize={this.state.search.size}
  144. onChange={(value) => {
  145. this.onChangePage(value);
  146. }}
  147. />
  148. </div>
  149. );
  150. }
  151. }