Surveyvote.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <template>
  2. <!-- 投票 start ---------------------------------------->
  3. <div class="index_3_box_vote">
  4. <div class="voteTitle">投票区</div>
  5. <div class="inquire">
  6. <p v-if="voteList.length>0">{{voteList[0].survey_name}}</p>
  7. <div class="radioBox">
  8. <!--投票选项-->
  9. <div v-if="!isDisabled">
  10. <div class="radio" v-if="isRadio">
  11. <el-radio-group v-model="radio1" @change="handleRadioChange">
  12. <el-radio v-for="item in voteList" :key="item.id" :value="item.id" size="large">
  13. <span v-if="item.is_other == 0">{{item.choice_name}}</span>
  14. <span v-else>其他</span>
  15. </el-radio>
  16. </el-radio-group>
  17. <el-input
  18. v-if="showUserChoice"
  19. v-model="userChoice"
  20. :rows="2"
  21. type="textarea"
  22. resize="none"
  23. placeholder="请输入.."
  24. />
  25. </div>
  26. <div class="checkInputBox" v-else>
  27. <el-checkbox-group v-model="check1" @change="handleCheckboxChange">
  28. <span v-for="item in voteList" :key="item.id">
  29. <span v-if="item.is_other == 0">
  30. <el-checkbox size="large" :label="item.choice_name" :value="item.id"/>
  31. </span>
  32. <span v-else>
  33. <el-checkbox size="large" label="其他" :value="item.id"/>
  34. </span>
  35. </span>
  36. </el-checkbox-group>
  37. <el-input
  38. v-if="showUserChoice"
  39. v-model="userChoice"
  40. :rows="2"
  41. type="textarea"
  42. resize="none"
  43. placeholder="请输入.."
  44. />
  45. </div>
  46. </div>
  47. <!--投票结果-->
  48. <div class="inquireData" v-else>
  49. <div v-for="item in websiteSurveyData.data" :key="item.id">
  50. <div class="inquireDataItem active" v-if="item.status == 1">
  51. <div class="inquireDataItemTitle">
  52. <span v-if="item.choice_name == ''">其他</span>
  53. <span v-else>{{item.choice_name}}</span>
  54. </div>
  55. <div class="inquireDataItemNum">{{item.results}}票</div>
  56. </div>
  57. <div class="inquireDataItem" v-else>
  58. <div class="inquireDataItemTitle">
  59. <span v-if="item.choice_name == ''">其他</span>
  60. <span v-else>{{item.choice_name}}</span>
  61. </div>
  62. <div class="inquireDataItemNum">{{item.results}}票</div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. <div class="btn">
  68. <button class="voting" @click="addWebsiteSurvey" :disabled="isDisabled" v-if="!isDisabled">投票</button>
  69. </div>
  70. </div>
  71. </div>
  72. <!-- 投票 end ---------------------------------------->
  73. </template>
  74. <script setup>
  75. //1.页面依赖 start ---------------------------------------->
  76. const route = useRoute();
  77. const articleId = parseInt(route.params.id); //获得该页面的id
  78. import { ElRadio, ElRadioGroup,ElCheckbox,ElCheckboxGroup,ElMessage,ElInput } from 'element-plus'
  79. //1.页面依赖 start ---------------------------------------->
  80. //2.投票 start---------------------------------------->
  81. const radio1 = ref(''); //单选
  82. const check1 = ref([]); //多选
  83. const isDisabled = ref(false);//是否禁用提交按钮
  84. const isRadio = ref(true);//是否渲染单选
  85. const userSurId = ref('');//投票属于哪一篇文章
  86. const userChoice = ref('');//用于判断用户选择了其他选项以后,输入的值
  87. const userIsChoice = ref('');//用于判断其他选项目前是什么值
  88. const showUserChoice = ref(false);//是否显示其他输入框
  89. const websiteSurveyData = ref([]);//投票结果
  90. //7.2获得投票列表
  91. let voteList = ref([]);
  92. async function getVoteList(){
  93. const voteData = await requestDataPromise('/web/getWebsiteSurvey',{method:'GET',query:{'art_id':articleId}});
  94. console.log(778899)
  95. console.log(voteData)
  96. if(voteData.code == 200){
  97. voteList.value = voteData.data;
  98. console.log(voteList.value)
  99. //判断显示单选还是多选
  100. //survey_type 0是单选 1是多选
  101. if(voteData.data[0].survey_type == 0){
  102. isRadio.value = true;
  103. console.log("1111")
  104. }else{
  105. isRadio.value = false;
  106. }
  107. //把最后一个的值拿出来 用于判断用户是否选择了其他
  108. for(let item of voteData.data){
  109. //如果含有其他
  110. if(item.is_other==1){
  111. userIsChoice.value = item.id;
  112. }
  113. }
  114. //用户投票属于哪一篇文章
  115. userSurId.value = voteData.data[0].sur_id;
  116. }else{
  117. console.log("投票错误:",voteData.message)
  118. }
  119. }
  120. getVoteList()
  121. //7.3当用户选择了选项,判断是否展示其他输入框
  122. const handleRadioChange = (value) => {
  123. if(value == userIsChoice.value){
  124. showUserChoice.value = true;
  125. }else{
  126. showUserChoice.value = false;
  127. }
  128. }
  129. const handleCheckboxChange = (value) => {
  130. if (value.includes(userIsChoice.value)) {
  131. showUserChoice.value = true;
  132. } else {
  133. showUserChoice.value = false;
  134. }
  135. }
  136. //7.4发起投票
  137. async function addWebsiteSurvey(){
  138. //判断当前是单选还是多选
  139. console.log(isRadio.value)
  140. if(isRadio.value){
  141. console.log("用户单选!")
  142. if(radio1.value!=''){
  143. //先判断一下是否使用了其他选项
  144. if(showUserChoice.value){
  145. if(userChoice.value!=''){
  146. //文章id
  147. // console.log(userSurId.value)
  148. // 用户输入的值
  149. // console.log(userChoice.value)
  150. //如果使用了其他,其他的选项需要增加进去
  151. const ChoiceData = await requestHome('/web/addWebsiteSurveyOption',{
  152. method:'GET',
  153. query:{
  154. 'sur_id':userSurId.value,//投票的新闻id
  155. 'choice_name':userChoice.value,//投票的选项id
  156. }
  157. });
  158. if(ChoiceData.code == 200){
  159. //提交完其他选项以后,再正式发起投票
  160. const mkData = await requestHome('/web/addWebsiteSurveyVote',{
  161. method:'GET',
  162. query:{
  163. 'sur_id':userSurId.value,
  164. 'choice_id':ChoiceData.data
  165. }
  166. });
  167. if(mkData.code == 200){
  168. ElMessage.success('投票成功!')
  169. //把投票结果显示到页面上 禁用投票按钮
  170. isDisabled.value = true;
  171. websiteSurveyData.value = mkData.data;
  172. //遍历一下,把用户选中的那个设置status为1
  173. let data = mkData.data;
  174. //遍历一下,把用户选中的那个设置status为1
  175. for(let item of data.data){
  176. for(let i of data.choice){
  177. if(item.id == i){
  178. console.log(item.id)
  179. item.status = 1;
  180. }
  181. }
  182. }
  183. websiteSurveyData.value = data;
  184. }else{
  185. ElMessage.error(mkData.message)
  186. }
  187. }else{
  188. ElMessage.error('其他投票失败!')
  189. }
  190. }else{
  191. ElMessage.error('请输入选项内容!')
  192. }
  193. }else{
  194. //如果没选择其他,直接提交选择的内容
  195. const mkData = await requestHome('/web/addWebsiteSurveyVote',{
  196. method:'GET',
  197. query:{
  198. 'sur_id':userSurId.value,
  199. 'choice_id':radio1.value
  200. }
  201. });
  202. if(mkData.code == 200){
  203. ElMessage.success('投票成功!')
  204. //把投票结果显示到页面上 禁用投票按钮
  205. isDisabled.value = true;
  206. let data = mkData.data;
  207. //遍历一下,把用户选中的那个设置status为1
  208. for(let item of data.data){
  209. for(let i of data.choice){
  210. if(item.id == i){
  211. item.status = 1;
  212. }
  213. }
  214. }
  215. websiteSurveyData.value = data;
  216. }else{
  217. ElMessage.error('投票失败!')
  218. }
  219. }
  220. }else{
  221. ElMessage.error('请选择一个选项')
  222. }
  223. }else{
  224. console.log("多选!")
  225. //多选
  226. if(check1.value!=[]){
  227. //先判断一下是否使用了其他选项
  228. if(showUserChoice.value){
  229. if(userChoice.value!=''){
  230. //判断用户是否只选择了一个其他
  231. if(check1.value.length == 1){
  232. const ChoiceData = await requestDataPromise('/web/addWebsiteSurveyOption',{
  233. method:'GET',
  234. query:{
  235. 'sur_id':userSurId.value,//投票的新闻id
  236. 'choice_name':userChoice.value,//用户输入的其他选项文字
  237. }
  238. });
  239. if(ChoiceData.code == 200){
  240. //提交完其他选项以后,再正式发起投票
  241. const mkData = await requestDataPromise('/web/addWebsiteSurveyVote',{
  242. method:'GET',
  243. query:{
  244. 'sur_id':userSurId.value,
  245. 'choice_id':ChoiceData.data
  246. }
  247. });
  248. if(mkData.code == 200){
  249. ElMessage.success('投票成功!')
  250. //把投票结果显示到页面上 禁用投票按钮
  251. isDisabled.value = true;
  252. websiteSurveyData.value = mkData.data;
  253. //遍历一下,把用户选中的那个设置status为1
  254. let data = mkData.data;
  255. //遍历一下,把用户选中的那个设置status为1
  256. for(let item of data.data){
  257. for(let i of data.choice){
  258. if(item.id == i){
  259. console.log(item.id)
  260. item.status = 1;
  261. }
  262. }
  263. }
  264. websiteSurveyData.value = data;
  265. }else{
  266. ElMessage.error(mkData.message)
  267. }
  268. }else{
  269. ElMessage.error('其他投票失败!')
  270. }
  271. }else{
  272. //用户选择了除了其他以外,还包括别的选项
  273. const ChoiceData = await requestDataPromise('/web/addWebsiteSurveyOption',{
  274. method:'GET',
  275. query:{
  276. 'sur_id':userSurId.value,//投票的新闻id
  277. 'choice_name':userChoice.value,//用户输入的其他选项文字
  278. }
  279. });
  280. if(ChoiceData.code == 200){
  281. let data = check1.value;
  282. //找到多选的数组,把其他默认值给替换掉
  283. for (let i = 0; i < data.length; i++) {
  284. if (data[i] == userIsChoice.value) {
  285. data[i] = ChoiceData.data;
  286. }
  287. }
  288. let jsonArray = JSON.stringify(data);
  289. //提交完其他选项以后,再正式发起投票
  290. const mkData = await requestDataPromise('/web/addWebsiteSurveyVote',{
  291. method:'GET',
  292. query:{
  293. 'sur_id':userSurId.value,
  294. 'choice_id':jsonArray
  295. }
  296. });
  297. if(mkData.code == 200){
  298. ElMessage.success('投票成功!')
  299. //把投票结果显示到页面上 禁用投票按钮
  300. isDisabled.value = true;
  301. websiteSurveyData.value = mkData.data;
  302. //遍历一下,把用户选中的那个设置status为1
  303. let data = mkData.data;
  304. //遍历一下,把用户选中的那个设置status为1
  305. for(let item of data.data){
  306. for(let i of data.choice){
  307. if(item.id == i){
  308. //console.log(item.id)
  309. item.status = 1;
  310. }
  311. }
  312. }
  313. websiteSurveyData.value = data;
  314. }else{
  315. ElMessage.error(mkData.message)
  316. }
  317. }else{
  318. ElMessage.error('其他投票失败!')
  319. }
  320. }
  321. }else{
  322. ElMessage.error('请输入选项内容!')
  323. }
  324. }else{
  325. let jsonArray = JSON.stringify(check1.value);
  326. //如果没选择其他,直接提交选择的内容
  327. const mkData = await requestDataPromise('/web/addWebsiteSurveyVote',{
  328. method:'GET',
  329. query:{
  330. 'sur_id':userSurId.value,
  331. 'choice_id':jsonArray
  332. }
  333. });
  334. if(mkData.code == 200){
  335. ElMessage.success('投票成功!')
  336. //把投票结果显示到页面上 禁用投票按钮
  337. isDisabled.value = true;
  338. websiteSurveyData.value = mkData.data;
  339. //遍历一下,把用户选中的那个设置status为1
  340. let data = mkData.data;
  341. //遍历一下,把用户选中的那个设置status为1
  342. for(let item of data.data){
  343. for(let i of data.choice){
  344. if(item.id == i){
  345. console.log(item.id)
  346. item.status = 1;
  347. }
  348. }
  349. }
  350. websiteSurveyData.value = data;
  351. }else{
  352. ElMessage.error('投票失败!')
  353. }
  354. }
  355. }else{
  356. ElMessage.error('请选择一个选项')
  357. }
  358. }
  359. }
  360. //2.投票 end---------------------------------------->
  361. </script>
  362. <style lang="less" scoped>
  363. //投票
  364. .index_3_box_vote{
  365. .voteTitle {
  366. font-size: 20px;
  367. height: 40px;
  368. line-height: 40px;
  369. color: #333333;
  370. padding-left: 20px;
  371. width: 100%;
  372. border-bottom: 1px solid #E7E7E7;
  373. border-top: 1px solid #255590;
  374. box-sizing: border-box;
  375. }
  376. width:100%;
  377. box-sizing:border-box;
  378. border:solid 1px #FBFBFB;
  379. background: #FBFBFB;
  380. .inquire {
  381. height: 394px;
  382. margin-top: 20px;
  383. border-radius: 6px 6px 6px 6px;
  384. padding: 4px 40px 4px 6px;
  385. box-sizing: border-box;
  386. p {
  387. font-weight: bold;
  388. height: 69px;
  389. font-family: PingFang SC, PingFang SC;
  390. font-size:20px;
  391. color: #333333;
  392. line-height: 21px;
  393. text-align: left;
  394. font-style: normal;
  395. text-transform: none;
  396. padding: 12px 20px 0 32px;
  397. }
  398. .radioBox {
  399. height: 250px;
  400. padding-left: 30px;
  401. overflow-y: auto;
  402. box-sizing: border-box;
  403. padding-bottom: 20px;
  404. }
  405. .radio {
  406. /deep/.el-radio {
  407. --el-radio-input-border-color-hover: #27881a;
  408. }
  409. /deep/.el-radio-group {
  410. align-items: center;
  411. display: inline-flex;
  412. flex-wrap: wrap;
  413. font-size: 0;
  414. //padding-left: 35px;
  415. }
  416. /deep/.el-radio.el-radio--large {
  417. width: 100%;
  418. height: 29px;
  419. margin-bottom: 15px;
  420. }
  421. /deep/.el-radio.el-radio--large .el-radio__label {
  422. font-family: PingFang SC, PingFang SC;
  423. font-weight: 400;
  424. font-size: 16px;
  425. color: #333333;
  426. white-space: nowrap;
  427. overflow: hidden;
  428. text-overflow: ellipsis;
  429. width: 300px;
  430. }
  431. /deep/.el-radio.el-radio--large .el-radio__inner {
  432. height: 16px;
  433. width: 16px;
  434. }
  435. /deep/.el-radio__input.is-checked+.el-radio__label {
  436. color: #27881a;
  437. }
  438. /deep/.el-radio__input.is-checked .el-radio__inner {
  439. background: #33b023;
  440. border-color: #27881a;
  441. }
  442. }
  443. .checkInputBox {
  444. /deep/.el-checkbox {
  445. --el-radio-input-border-color-hover: #27881a;
  446. }
  447. /deep/.el-checkbox-group {
  448. align-items: center;
  449. display: inline-flex;
  450. flex-wrap: wrap;
  451. font-size: 0;
  452. //padding-left: 35px;
  453. }
  454. /deep/.el-checkbox.el-checkbox--large {
  455. width: 330px;
  456. height: 29px;
  457. margin-bottom: 15px;
  458. }
  459. /deep/.el-checkbox.el-checkbox--large .el-checkbox__label {
  460. font-family: PingFang SC, PingFang SC;
  461. font-weight: 400;
  462. font-size: 16px;
  463. color: #333333;
  464. white-space: nowrap;
  465. overflow: hidden;
  466. text-overflow: ellipsis;
  467. width: 300px;
  468. }
  469. /deep/.el-checkbox.el-checkbox--large .el-checkbox__inner {
  470. height: 16px;
  471. width: 16px;
  472. }
  473. /deep/.el-checkbox__input.is-checked+.el-checkbox__label {
  474. color: #27881a;
  475. }
  476. /deep/.el-checkbox__input.is-checked .el-checkbox__inner {
  477. background: #33b023;
  478. border-color: #27881a;
  479. }
  480. }
  481. .btn {
  482. padding-left: 30px;
  483. button {
  484. width: 78px;
  485. height: 37px;
  486. line-height: 37px;
  487. border-radius: 6px;
  488. border: none;
  489. font-family: PingFang SC, PingFang SC;
  490. font-weight: 400;
  491. font-size: 16px;
  492. color: #999999;
  493. }
  494. .voting {
  495. background-color: #255590;
  496. color: #fff;
  497. margin-right: 44px;
  498. cursor: pointer;
  499. }
  500. .look {
  501. cursor: pointer;
  502. }
  503. }
  504. }
  505. .inquireData {
  506. .inquireDataItem {
  507. width: 100%;
  508. height: 38px;
  509. display: flex;
  510. align-items: center;
  511. justify-content: space-between;
  512. background: #F3F3F3;
  513. border: 1px solid #D2D2D2;
  514. margin-bottom: 10px;
  515. border-radius: 6px;
  516. padding: 0 15px;
  517. box-sizing: border-box;
  518. color: #999999;
  519. .inquireDataItemTitle {
  520. width: 290px;
  521. height: 38px;
  522. font-size: 16px;
  523. line-height: 38px;
  524. white-space: nowrap;
  525. overflow: hidden;
  526. text-overflow: ellipsis;
  527. }
  528. .inquireDataItemNum {
  529. font-size: 16px;
  530. }
  531. }
  532. .active {
  533. color:#49A769;
  534. background: #dff7e8;
  535. border: 1px solid #49A769;
  536. }
  537. }
  538. }
  539. </style>