page.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. {
  104. title: '用户ID',
  105. dataIndex: 'userId',
  106. },
  107. {
  108. title: '用户手机',
  109. dataIndex: 'user.mobile',
  110. },
  111. {
  112. title: '用户姓名',
  113. dataIndex: 'user.nickname',
  114. },
  115. {
  116. title: '开通服务',
  117. dataIndex: 'service',
  118. render: (text) => {
  119. return ServiceKeyMap[text];
  120. },
  121. }, {
  122. title: '服务参数',
  123. dataIndex: 'param',
  124. render: (text, record) => {
  125. return (ServiceParamRelation[record.service] || {})[text] || '';
  126. },
  127. }, {
  128. title: '开通时间',
  129. dataIndex: 'time',
  130. render: (text, record) => {
  131. return `${record.startTime ? formatDate(record.startTime) : ''} - ${record.endTime ? formatDate(record.endTime) : ''}`;
  132. },
  133. }, {
  134. title: '开通方式',
  135. dataIndex: 'source',
  136. render: (text) => {
  137. return ServiceSourceMap[text] || '';
  138. },
  139. }, {
  140. title: '累计消费金额',
  141. dataIndex: 'user.totalMoney',
  142. render: (text) => {
  143. return formatMoney(text);
  144. },
  145. }, {
  146. title: '操作',
  147. dataIndex: 'handler',
  148. render: (text, record) => {
  149. return <div className="table-button">
  150. {!record.isUsed && (
  151. <a onClick={() => {
  152. this.editAction(record);
  153. }}>编辑</a>
  154. )}
  155. </div>;
  156. },
  157. },
  158. ];
  159. bindSearch(this.filterForm, 'userId', this, (search) => {
  160. return User.list(search);
  161. }, (row) => {
  162. return {
  163. title: `${row.nickname}(${row.mobile})`,
  164. value: row.id,
  165. };
  166. }, this.state.search.userId ? Number(this.state.search.userId) : [], null);
  167. }
  168. initData() {
  169. User.listService(this.state.search).then(result => {
  170. this.setTableData(result.list, result.total);
  171. });
  172. }
  173. addAction() {
  174. this.itemList[1].disabled = false;
  175. asyncForm('新建', this.itemList, {}, data => {
  176. return User.addService(data).then(() => {
  177. asyncSMessage('添加成功!');
  178. this.refresh();
  179. });
  180. }).then(component => {
  181. this.formF = component;
  182. });
  183. }
  184. editAction(row) {
  185. this.itemList[1].disabled = true;
  186. asyncForm('编辑', this.itemList, row, data => {
  187. return User.editService(data).then(() => {
  188. asyncSMessage('编辑成功!');
  189. this.refresh();
  190. });
  191. }).then(component => {
  192. this.formF = component;
  193. });
  194. }
  195. renderView() {
  196. return <Block flex>
  197. <FilterLayout
  198. show
  199. itemList={this.filterForm}
  200. data={this.state.search}
  201. onChange={data => {
  202. this.search(data);
  203. }} />
  204. <ActionLayout
  205. itemList={this.actionList}
  206. selectedKeys={this.state.selectedKeys}
  207. onAction={key => this.onAction(key)}
  208. />
  209. <TableLayout
  210. columns={this.columns}
  211. list={this.state.list}
  212. pagination={this.state.page}
  213. loading={this.props.core.loading}
  214. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  215. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  216. selectedKeys={this.state.selectedKeys}
  217. />
  218. </Block>;
  219. }
  220. }