VoteDetailPage.js 14 KB

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