page.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import Block from '@src/components/Block';
  5. import FilterLayout from '@src/layouts/FilterLayout';
  6. import ActionLayout from '@src/layouts/ActionLayout';
  7. import TableLayout from '@src/layouts/TableLayout';
  8. import { getMap, formatDate, formatMoney, bindSearch } from '@src/services/Tools';
  9. import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
  10. import { ServiceParamMap, ServiceKey, ServiceSource } from '../../../../Constant';
  11. import { User } from '../../../stores/user';
  12. const ServiceKeyMap = getMap(ServiceKey, 'value', 'label');
  13. const ServiceSourceMap = getMap(ServiceSource, 'value', 'label');
  14. const ServiceParamList = getMap(Object.keys(ServiceParamMap).map(key => {
  15. return { list: ServiceParamMap[key], key };
  16. }), 'key', 'list');
  17. const ServiceParamRelation = getMap(Object.keys(ServiceParamMap).map(key => {
  18. return { map: getMap(ServiceParamMap[key], 'value', 'label'), key };
  19. }), 'key', 'map');
  20. export default class extends Page {
  21. init() {
  22. this.timeout = null;
  23. this.mobile = null;
  24. this.actionList = [{
  25. key: 'add',
  26. type: 'primary',
  27. name: '创建',
  28. }];
  29. this.formF = null;
  30. this.itemList = [{
  31. key: 'id',
  32. type: 'hidden',
  33. }, {
  34. key: 'mobile',
  35. type: 'input',
  36. name: '手机号',
  37. placeholder: '请输入',
  38. option: {
  39. normalize: (value) => {
  40. if (this.mobile === value) return value;
  41. if (this.timeout) {
  42. clearTimeout(this.timeout);
  43. this.timeout = null;
  44. }
  45. this.timeout = setTimeout(() => {
  46. User.validMobile({ mobile: value }).then(result => {
  47. this.mobile = value;
  48. this.itemList[1].suffix = result ? '已注册' : '未注册';
  49. this.formF.setFieldsValue({ load: true });
  50. });
  51. }, 1500);
  52. return value;
  53. },
  54. },
  55. }, {
  56. key: 'service',
  57. type: 'select',
  58. name: '开通服务',
  59. select: ServiceKey,
  60. placeholder: '请选择',
  61. onChange: (value) => {
  62. this.itemList[3].select = ServiceParamList[value] || [];
  63. this.formF.setFieldsValue({ param: '' });
  64. },
  65. }, {
  66. key: 'param',
  67. type: 'select',
  68. name: '服务参数',
  69. select: [],
  70. }, {
  71. key: 'source',
  72. type: 'select',
  73. name: '开通方式',
  74. select: ServiceSource,
  75. placeholder: '请选择',
  76. }];
  77. this.filterForm = [{
  78. key: 'userId',
  79. type: 'select',
  80. allowClear: true,
  81. name: '用户',
  82. select: [],
  83. number: true,
  84. placeholder: '请输入',
  85. }, {
  86. key: 'service',
  87. type: 'select',
  88. allowClear: true,
  89. name: '服务',
  90. select: ServiceKey,
  91. onChange: (value) => {
  92. this.filterForm[2].select = ServiceParamList[value] || [];
  93. },
  94. }, {
  95. key: 'param',
  96. type: 'select',
  97. name: '参数',
  98. select: [],
  99. allowClear: true,
  100. notFound: null,
  101. }];
  102. this.columns = [{
  103. title: '用户ID',
  104. dataIndex: 'userId',
  105. }, {
  106. title: '用户手机',
  107. dataIndex: 'user.mobile',
  108. }, {
  109. title: '用户姓名',
  110. dataIndex: 'user.nickname',
  111. }, {
  112. title: '开通服务',
  113. dataIndex: 'service',
  114. render: (text, record) => {
  115. return `${ServiceKeyMap[text]}${(ServiceParamRelation[record.service] || {})[text] || ''}`;
  116. },
  117. }, {
  118. title: '开通时间',
  119. dataIndex: 'time',
  120. render: (text, record) => {
  121. return `${record.startTime ? formatDate(record.startTime) : ''} - ${record.endTime ? formatDate(record.endTime) : ''}`;
  122. },
  123. }, {
  124. title: '开通方式',
  125. dataIndex: 'source',
  126. render: (text) => {
  127. return ServiceSourceMap[text] || '';
  128. },
  129. }, {
  130. title: '累计消费金额',
  131. dataIndex: 'user.totalMoney',
  132. render: (text) => {
  133. return formatMoney(text);
  134. },
  135. }, {
  136. title: '操作',
  137. dataIndex: 'handler',
  138. render: (text, record) => {
  139. return <div className="table-button">
  140. {!record.isUsed && (
  141. <a onClick={() => {
  142. this.editAction(record);
  143. }}>编辑</a>
  144. )}
  145. </div>;
  146. },
  147. },
  148. ];
  149. bindSearch(this.filterForm, 'userId', this, (search) => {
  150. return User.list(search);
  151. }, (row) => {
  152. return {
  153. title: `${row.nickname}(${row.mobile})`,
  154. value: row.id,
  155. };
  156. }, this.state.search.userId ? Number(this.state.search.userId) : [], null);
  157. }
  158. initData() {
  159. User.listService(this.state.search).then(result => {
  160. this.setTableData(result.list, result.total);
  161. });
  162. }
  163. addAction() {
  164. this.itemList[1].disabled = false;
  165. asyncForm('新建', this.itemList, {}, data => {
  166. return User.addService(data).then(() => {
  167. asyncSMessage('添加成功!');
  168. this.refresh();
  169. });
  170. }).then(component => {
  171. this.formF = component;
  172. });
  173. }
  174. editAction(row) {
  175. this.itemList[1].disabled = true;
  176. asyncForm('编辑', this.itemList, row, data => {
  177. return User.editService(data).then(() => {
  178. asyncSMessage('编辑成功!');
  179. this.refresh();
  180. });
  181. }).then(component => {
  182. this.formF = component;
  183. });
  184. }
  185. renderView() {
  186. return <Block flex>
  187. <FilterLayout
  188. show
  189. itemList={this.filterForm}
  190. data={this.state.search}
  191. onChange={data => {
  192. this.search(data);
  193. }} />
  194. <ActionLayout
  195. itemList={this.actionList}
  196. selectedKeys={this.state.selectedKeys}
  197. onAction={key => this.onAction(key)}
  198. />
  199. <TableLayout
  200. columns={this.columns}
  201. list={this.state.list}
  202. pagination={this.state.page}
  203. loading={this.props.core.loading}
  204. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  205. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  206. selectedKeys={this.state.selectedKeys}
  207. />
  208. </Block>;
  209. }
  210. }