VoteDetailPage.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**
  2. * Created by Arison on 2018/11/15.
  3. */
  4. import React from 'react';
  5. import 'bootstrap/dist/css/bootstrap.css'
  6. import 'bootstrap/dist/css/bootstrap.min.css'
  7. import {List, Button, Checkbox, Progress} from 'antd';
  8. import './VoteDetailPage.css'
  9. import {fetchPost, fetchGet} from "../../utils/fetchRequest";
  10. import {API, _baseURL} from "../../configs/api.config";
  11. import {Toast} from 'antd-mobile'
  12. import ImageGrid from "../../components/image/ImageGrid";
  13. import {connect} from 'react-redux'
  14. import {getIntValue, getSearchParams, getStrValue, isObjEmpty} from "../../utils/common";
  15. import {saveListState} from "../../redux/actions/listState";
  16. let role //parent ,teacher
  17. /**
  18. * Created by Arison on 15:51.
  19. */
  20. class VoteDetailPage extends React.Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. name: 'VoteDetailPage',
  25. id: this.props.match.params.id,
  26. voteState: false,
  27. data: {
  28. title: '三年级2班',
  29. state: '进行中',
  30. voter: '',
  31. voterPhoto: '',
  32. files: [],
  33. endTime: '2018-11-15 08:00',
  34. selectState: 0,//0 单选 1 多选
  35. votes: [],
  36. voteStatus: ''
  37. }
  38. };
  39. }
  40. componentWillMount() {
  41. document.title = "投票";
  42. role = this.props.match.params.role
  43. this.searchParams = getSearchParams(this.props.location.search)
  44. }
  45. componentDidMount() {
  46. this.getVoteDetail();
  47. }
  48. getVoteDetail() {
  49. var url_detail
  50. var params = null
  51. // console.log("role",role)
  52. // console.log("user",this.props.userInfo.user)
  53. // console.log("stuId",this.props.userInfo.user.student.stuId)
  54. // console.log("voteId",this.state.id)
  55. if (role === 'teacher') {
  56. url_detail = API.voteDetailTeacher
  57. let id = this.props.userInfo.user.userFunId
  58. if (this.searchParams && this.searchParams.teacherId) {
  59. id = this.searchParams.teacherId
  60. }
  61. params = {
  62. teacherId: id,
  63. voteId: this.state.id
  64. }
  65. } else if (role === 'parent') {
  66. url_detail = API.voteDetailParent
  67. let id = this.props.userInfo.user.student.stuId
  68. if (this.searchParams && this.searchParams.stuId) {
  69. id = this.searchParams.stuId
  70. }
  71. params = {
  72. stuId: id,
  73. voteId: this.state.id
  74. }
  75. }
  76. Toast.loading("", 0)
  77. fetchPost(url_detail, params).then((response) => {
  78. Toast.hide();
  79. if (response.data) {
  80. console.log("getVoteDetail", response)
  81. this.state.data.title = response.data.voteTopicDOS[0].topicName;
  82. this.state.data.voter = response.data.teacherName;
  83. this.state.data.files.length = 0;
  84. if (response.data.enclosure != null) {
  85. for (let i = 0; i < response.data.enclosure.length; i++) {
  86. this.state.data.files.push(_baseURL + response.data.enclosure[i]);
  87. }
  88. }
  89. this.state.data.endTime = response.data.voteEndDate;
  90. this.state.data.voterPhoto = response.data.userPhoto
  91. this.state.data.selectState = response.data.voteTopicDOS[0].topicType === 1 ? 0 : 1
  92. this.state.data.state = response.data.voteStatus === 1 ? "进行中" : "已结束"
  93. this.state.data.voteStatus = response.data.voteStatus
  94. if (this.state.data.state === "进行中") {
  95. this.state.voteState = true;
  96. } else {
  97. this.state.voteState = false;
  98. }
  99. this.state.data.votes.length = 0;
  100. for (let i = 0; i < response.data.voteTopicDOS[0].voteOptionsDOS.length; i++) {
  101. let model = {
  102. optionId: response.data.voteTopicDOS[0].voteOptionsDOS[i].optionId,
  103. topicId: response.data.voteTopicDOS[0].voteOptionsDOS[i].topicId,
  104. count: response.data.voteTopicDOS[0].voteOptionsDOS[i].count,
  105. percent: parseInt(response.data.voteTopicDOS[0].voteOptionsDOS[i].percent),
  106. name: response.data.voteTopicDOS[0].voteOptionsDOS[i].optionName,
  107. checked: false
  108. }
  109. this.state.data.votes.push(model);
  110. }
  111. this.setState({
  112. data: this.state.data
  113. })
  114. console.log(this.props.listState)
  115. const item = response.data
  116. let voteBean = {}
  117. voteBean.voteId = getIntValue(item, 'voteId')
  118. voteBean.voteName = getStrValue(item, 'voteName')
  119. voteBean.voteType = getIntValue(response.data.voteTopicDOS, 'topicType')
  120. voteBean.voteStatusCode = getIntValue(item, 'voteStatus')
  121. if (voteBean.voteStatusCode === 1) {
  122. voteBean.voteStatus = '进行中'
  123. } else if (voteBean.voteStatusCode === 4) {
  124. voteBean.voteStatus = '已截止'
  125. } else {
  126. voteBean.voteStatus = '已投票'
  127. }
  128. voteBean.creatDate = getStrValue(item, 'creatDate')
  129. voteBean.voteEndDate = getStrValue(item, 'voteEndDate')
  130. voteBean.voteRemarks = getStrValue(item, 'voteRemarks')
  131. const topics = getStrValue(item, 'voteTopicDOS')
  132. if (!isObjEmpty(topics)) {
  133. voteBean.options = topics[0].voteOptionsDOS
  134. }
  135. if (this.props.listState.tabIndex >= 0) {
  136. const tabIndex = this.props.listState.tabIndex
  137. if (tabIndex === 0) {
  138. if (this.props.listState && !isObjEmpty(this.props.listState.listData)) {
  139. if (this.props.listState.itemIndex >= 0) {
  140. this.props.listState.listData[this.props.listState.itemIndex] = voteBean
  141. }
  142. saveListState({
  143. listData: this.props.listState.listData,
  144. })()
  145. }
  146. } else if (tabIndex === 1) {
  147. if (this.props.listState && !isObjEmpty(this.props.listState.listData2)) {
  148. if (this.props.listState.itemIndex >= 0) {
  149. this.props.listState.listData2[this.props.listState.itemIndex] = voteBean
  150. }
  151. saveListState({
  152. listData2: this.props.listState.listData2,
  153. })()
  154. }
  155. }
  156. }
  157. }
  158. }).catch((error) => {
  159. Toast.hide()
  160. if (typeof error === 'string') {
  161. Toast.fail(error, 2)
  162. } else {
  163. Toast.fail('请求异常', 2)
  164. }
  165. })
  166. }
  167. onChangeEvent = (index, event) => {
  168. console.log("onChangeEvent()", event.target.checked);
  169. if (this.state.data.selectState === 0) {//单选
  170. this.state.data.votes[index].checked = event.target.checked;
  171. for (let i = 0; i < this.state.data.votes.length; i++) {
  172. if (i != index) {
  173. this.state.data.votes[i].checked = false;
  174. }
  175. }
  176. this.setState({
  177. data: this.state.data
  178. })
  179. } else {//多选
  180. this.state.data.votes[index].checked = event.target.checked;
  181. this.setState({
  182. data: this.state.data
  183. })
  184. }
  185. }
  186. onClickEvent = () => {
  187. //投票状态
  188. let options = [];
  189. for (let i = 0; i < this.state.data.votes.length; i++) {
  190. let model = this.state.data.votes[i];
  191. console.log("onClickEvent():model:", model);
  192. if (model.checked) {
  193. if (this.state.data.selectState === 0) {
  194. options.push(model.optionId);
  195. } else {
  196. options.push(model.optionId);
  197. }
  198. }
  199. }
  200. if (options.length == 0) {
  201. Toast.info("请选择一个投票项")
  202. return
  203. }
  204. Toast.loading('正在投票...', 0)
  205. var url_detail2
  206. var params = null
  207. if (role === 'teacher') {
  208. url_detail2 = API.voteActionTeacher
  209. params = {
  210. optionIds: JSON.stringify(options),
  211. teacherId: this.props.userInfo.user.userFunId,
  212. voteId: this.state.id
  213. }
  214. } else if (role === 'parent') {
  215. url_detail2 = API.voteActionParent
  216. params = {
  217. optionIds: JSON.stringify(options),
  218. parentId: this.props.userInfo.user.userFunId,
  219. stuId: this.props.userInfo.user.student.stuId,
  220. voteId: this.state.id
  221. }
  222. }
  223. fetchPost(url_detail2, params).then((response) => {
  224. Toast.hide()
  225. Toast.info(response.data)
  226. this.getVoteDetail();
  227. this.setState({
  228. voteState: true
  229. })
  230. }).catch((error) => {
  231. Toast.hide()
  232. if (typeof error === 'string') {
  233. Toast.fail(error, 2)
  234. } else {
  235. Toast.fail('请求异常', 2)
  236. }
  237. })
  238. }
  239. render() {
  240. return (
  241. <div className="common-column-layout">
  242. <div className="col-xs-12">
  243. <div className="row" id="pager_header">
  244. <div className="flex_row">
  245. <img class="img-circle"
  246. style={{marginLeft: "20px", marginTop: '24px', border: "1px solid #e4e4e4"}}
  247. src={this.props.userInfo.userAvatar} width={54}
  248. height={54}/>
  249. <div className="vote-header ">
  250. {this.state.data.title}
  251. </div>
  252. </div>
  253. <div className="vote-detail-name">{this.state.data.voter}</div>
  254. <div id="row_right">
  255. <span className="vote-detail-time-caption">截止时间:</span>
  256. <span className="vote-detail-time-value">{this.state.data.endTime}</span>
  257. </div>
  258. </div>
  259. <div className="row">
  260. <div className='common-flex-row vote-detail-type'>
  261. {this.state.data.selectState === 0 ? (<span>单选</span>) : (<span>多选</span>)}
  262. <div style={{height: '1px', flex: 1, background: '#EEEEEE', marginLeft: '5px'}}></div>
  263. </div>
  264. <div className="col-xs-12">
  265. <List dataSource={this.state.data.votes}
  266. renderItem={(item, index) => (
  267. <div className='common-flex-row vote-detail-option-layout'>
  268. {this.state.voteState ? (<Checkbox
  269. checked={item.checked}
  270. onChange={this.onChangeEvent.bind(this, index)}
  271. style={{marginLeft: "20px", display: "flex", alignItems: "center"}}>
  272. </Checkbox>) :
  273. (<Checkbox disabled
  274. checked={item.checked}
  275. onChange={this.onChangeEvent.bind(this, index)}
  276. style={{
  277. marginLeft: "20px",
  278. display: "flex",
  279. alignItems: "center"
  280. }}>
  281. </Checkbox>)}
  282. <div style={{
  283. flex: 1,
  284. display: "inline",
  285. marginRight: "10px",
  286. marginLeft: "10px",
  287. alignItems: "center",
  288. height: "100%"
  289. }}>
  290. <Progress percent={item.percent} size="small"/>
  291. </div>
  292. <span className='vote-detail-option-text'>{item.name}</span>
  293. </div>
  294. )}/>
  295. </div>
  296. </div>
  297. <div className="row flex_center" id="row_vote">
  298. {this.state.voteState == true ? (
  299. <Button onClick={this.onClickEvent.bind(this)} type="primary" block
  300. style={{margin: '30px 10px'}}
  301. className='commonButton'>
  302. 投票</Button>
  303. ) : (
  304. <Button className='commonButton'
  305. style={{
  306. width: 350,
  307. height: 35,
  308. backgroundColor: "#9D9D9D",
  309. color: "#ffffff",
  310. border: "1px solid #ffffff",
  311. margin: '30px 10px'
  312. }}>{this.state.data.voteStatus == 4 ? '已截止' : '已投票'}</Button>
  313. )
  314. }
  315. </div>
  316. {this.state.data.files.length === 0 ? ("") : (<div>
  317. <div className="row" id="page_block_min"></div>
  318. <div className="row margin_left_right_15">
  319. <div className="margin_top_20"><span className="span_15">附件</span></div>
  320. <ImageGrid images={this.state.data.files}/>
  321. </div>
  322. </div>)}
  323. </div>
  324. </div>)
  325. }
  326. }
  327. let mapStateToProps = (state) => ({
  328. userInfo: {...state.redUserInfo},
  329. listState: {...state.redListState}
  330. })
  331. let mapDispatchToProps = (dispatch) => ({})
  332. export default connect(mapStateToProps, mapDispatchToProps)(VoteDetailPage)