vote-detail.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <template>
  2. <view class="app-page">
  3. <view v-if="loading != 'success'" class="loading-wrap">
  4. <tm-skeleton model="listAvatr"></tm-skeleton>
  5. <tm-skeleton model="listAvatr"></tm-skeleton>
  6. <tm-skeleton model="listAvatr"></tm-skeleton>
  7. <tm-skeleton model="listAvatr"></tm-skeleton>
  8. </view>
  9. <block v-else>
  10. <view v-if="!detail" class="empty">
  11. <tm-empty icon="icon-shiliangzhinengduixiang-" label="未查询到数据"></tm-empty>
  12. </view>
  13. <block v-else>
  14. <view class="vote-card">
  15. <view class="sub-title"> 投票信息 </view>
  16. <view class="vote-card-body flex flex-col"
  17. style="margin-top:12rpx;font-size:28rpx;gap:12rpx 0;background:#F3F4F6;color:#2B2F33;padding:24rpx;border-radius:12rpx;">
  18. <view class="">
  19. 投票类型:<tm-tags v-if="vote.spec.type==='single'" color="light-blue" :shadow="0" size="xs"
  20. model="fill">单选</tm-tags>
  21. <tm-tags v-else-if="vote.spec.type==='multiple'" color="light-blue" :shadow="0" size="xs"
  22. model="fill">多选</tm-tags>
  23. <tm-tags v-else-if="vote.spec.type==='pk'" color="light-blue" :shadow="0" size="xs"
  24. model="fill">双选PK</tm-tags>
  25. </view>
  26. <view class="">
  27. 投票状态:<tm-tags v-if="vote.spec.hasEnded" color="red" size="xs" :shadow="0"
  28. model="fill">已结束</tm-tags>
  29. <tm-tags v-else color="green" size="xs" :shadow="0" model="fill">进行中</tm-tags>
  30. </view>
  31. <view class="">
  32. 投票方式:<tm-tags v-if="vote.spec.canAnonymously" color="light-blue" size="xs" :shadow="0"
  33. model="fill">匿名</tm-tags>
  34. <tm-tags v-else color="red" size="xs" :shadow="0" model="fill">不匿名</tm-tags>
  35. </view>
  36. <view v-if="vote.spec.remark" class="">
  37. 投票说明:{{ vote.spec.remark||"暂无说明" }}
  38. </view>
  39. <view class="">
  40. 截止时间:{{ {d: vote.spec.endDate, f: 'yyyy-MM-dd HH:mm'} | formatTime }}
  41. </view>
  42. </view>
  43. </view>
  44. <view class="vote-card">
  45. <view class="vote-card-head flex flex-col items-start mb-12">
  46. <view class="sub-title"> 投票内容 </view>
  47. <view class="sub-content">
  48. {{ vote.spec.title }}
  49. </view>
  50. </view>
  51. <view class="vote-card-body">
  52. <view class="sub-title"> 投票选项 <text v-if="vote.spec.type==='multiple'"
  53. class="sub-title-count">(最多选择 {{ vote.spec.maxVotes }} 项)</text> </view>
  54. <view v-if="vote.spec.type==='single'" class="single">
  55. <!-- <tm-groupradio @change="onOptionRadioChange">
  56. <tm-radio v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex" dense
  57. :disabled="vote.spec.disabled" v-model="option.checked" :extendData="option">
  58. <template v-slot:default="{checkData}">
  59. <tm-button :shadow="0" :theme="checkData.checked?'light-blue':'grey-lighten-3'"
  60. :plan="false" size="m" :height="72">
  61. <view class="flex flex-between w-full">
  62. <text class="text-align-left text-overflow">
  63. {{ checkData.extendData.title }}
  64. </text>
  65. <text v-if="checkData.extendData.isVoted" class="flex-shrink ml-12">
  66. {{checkData.extendData.percent }}%
  67. </text>
  68. </view>
  69. </tm-button>
  70. </template>
  71. </tm-radio>
  72. </tm-groupradio> -->
  73. <view class="w-full flex flex-col gap-8">
  74. <tm-button v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  75. :shadow="0" :theme="option.checked?'light-blue':'grey-lighten-3'" :plan="false"
  76. size="m" :height="72" :block="true" class="flex-1 w-full"
  77. @click="handleSelectSingleOption(option)">
  78. <view class="flex flex-between w-full">
  79. <text class="text-align-left text-overflow">
  80. {{option.title }}
  81. </text>
  82. <text v-if="vote.spec.isVoted" class="flex-shrink ml-12">
  83. {{option.percent }}%
  84. </text>
  85. </view>
  86. </tm-button>
  87. </view>
  88. </view>
  89. <view v-else-if="vote.spec.type==='multiple'" class="multiple">
  90. <!-- <tm-groupcheckbox @change="onOptionCheckboxChange">
  91. <tm-checkbox v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex" dense
  92. :disabled="vote.spec.disabled" v-model="option.checked" :extendData="option">
  93. <template v-slot:default="{checkData}">
  94. <tm-button :shadow="0" :theme="checkData.checked?'light-blue':'grey-lighten-3'"
  95. :plan="false" size="m" :height="72">
  96. <view class="flex flex-between w-full">
  97. <text class="text-align-left text-overflow">
  98. {{ checkData.extendData.title }}
  99. </text>
  100. <text v-if="checkData.extendData.isVoted" class="flex-shrink ml-12">
  101. {{checkData.extendData.percent }}%
  102. </text>
  103. </view>
  104. </tm-button>
  105. </template>
  106. </tm-checkbox>
  107. </tm-groupcheckbox> -->
  108. <view class="w-full flex flex-col gap-8">
  109. <tm-button v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  110. :shadow="0" :theme="option.checked?'light-blue':'grey-lighten-3'" :plan="false"
  111. size="m" :height="72" :block="true" class="flex-1 full"
  112. @click="handleSelectCheckboxOption(option)">
  113. <view class="flex flex-between w-full">
  114. <text class="text-align-left text-overflow">
  115. {{option.title }}
  116. </text>
  117. <text v-if="vote.spec.isVoted" class="flex-shrink ml-12">
  118. {{option.percent }}%
  119. </text>
  120. </view>
  121. </tm-button>
  122. </view>
  123. </view>
  124. <view v-else-if="vote.spec.type==='pk'" class="pk">
  125. <view class="pk-container">
  126. <view class="radio-item" v-for="(option,optionIndex) in vote.spec.options"
  127. :key="optionIndex" :class="[optionIndex==0?'radio-left':'radio-right']"
  128. :style="{width:option.percent + '%'}">
  129. <view class="option-item"
  130. :class="[optionIndex==0?'option-item-left':'option-item-right']">
  131. {{option.percent }}%
  132. </view>
  133. </view>
  134. </view>
  135. <view class="option-foot w-full flex flex-between">
  136. <!-- <tm-groupradio @change="onOptionPkChange" >
  137. <tm-radio v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex" dense
  138. :disabled="vote.spec.disabled" v-model="option.checked"
  139. :extendData="{optionIndex:optionIndex,...option}" >
  140. <template v-slot:default="{checkData}">
  141. <tm-button :shadow="0"
  142. :theme="checkData.checked?'light-blue':'grey-lighten-3'" :plan="false"
  143. size="m" :height="72">
  144. <view class="flex flex-between w-full">
  145. <text class="text-align-left text-overflow">
  146. 选项{{checkData.extendData.optionIndex+1}}:{{ checkData.extendData.title }}
  147. </text>
  148. </view>
  149. </tm-button>
  150. </template>
  151. </tm-radio>
  152. </tm-groupradio> -->
  153. <view class="w-full flex flex-between gap-8">
  154. <tm-button v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  155. :shadow="0" :theme="option.checked?'light-blue':'grey-lighten-3'" :plan="false"
  156. size="m" :height="72" :block="true" class="flex-1"
  157. @click="handleSelectSingleOption(option)">
  158. <view class="flex flex-between w-full">
  159. <text class="text-align-left text-overflow">
  160. 选项{{ optionIndex+1}}:{{option.title }}
  161. </text>
  162. </view>
  163. </tm-button>
  164. </view>
  165. </view>
  166. </view>
  167. </view>
  168. </view>
  169. <view class="vote-card">
  170. <view class="vote-card-body">
  171. <view class="sub-title"> 投票统计 </view>
  172. <view class="">
  173. <tm-tags color="grey-darken-4" size="s" model="text">{{ vote.stats.voteCount }}
  174. 人已参与</tm-tags>
  175. </view>
  176. </view>
  177. </view>
  178. <view class="vote-submit flex w-full flex-center" :style="{
  179. paddingBottom:safeAreaBottom + 'rpx'
  180. }">
  181. <tm-button v-if="!vote.spec.canAnonymously" theme="red" :shadow="0" class="w-full" text
  182. :block="true" @click="handleSubmit()">不允许匿名投票</tm-button>
  183. <tm-button v-else-if="fnCalcIsVoted()" theme="white" text :block="true"
  184. class="w-full">您已参与投票</tm-button>
  185. <tm-button v-else theme="light-blue" class="w-full" :block="true"
  186. @click="handleSubmit()">提交投票</tm-button>
  187. </view>
  188. </block>
  189. </block>
  190. </view>
  191. </template>
  192. <script>
  193. import tmSkeleton from '@/tm-vuetify/components/tm-skeleton/tm-skeleton.vue';
  194. import tmEmpty from '@/tm-vuetify/components/tm-empty/tm-empty.vue';
  195. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  196. import tmGroupradio from '@/tm-vuetify/components/tm-groupradio/tm-groupradio.vue';
  197. import tmRadio from '@/tm-vuetify/components/tm-radio/tm-radio.vue';
  198. import tmGroupcheckbox from '@/tm-vuetify/components/tm-groupcheckbox/tm-groupcheckbox.vue';
  199. import tmCheckbox from '@/tm-vuetify/components/tm-checkbox/tm-checkbox.vue';
  200. import tmTags from '@/tm-vuetify/components/tm-tags/tm-tags.vue';
  201. import {
  202. voteCacheUtil
  203. } from '@/utils/vote.js'
  204. const types = {
  205. "pk": "双选PK",
  206. "multiple": "多选",
  207. "single": "单选"
  208. }
  209. export default {
  210. components: {
  211. tmSkeleton,
  212. tmEmpty,
  213. tmButton,
  214. tmGroupradio,
  215. tmRadio,
  216. tmGroupcheckbox,
  217. tmCheckbox,
  218. tmTags,
  219. },
  220. data() {
  221. return {
  222. safeAreaBottom: 24,
  223. loading: 'loading',
  224. pageTitle: '加载中...',
  225. name: '',
  226. detail: null,
  227. vote: null,
  228. submitForm: {
  229. voteData: []
  230. },
  231. votedSelected: {
  232. checkbox: [],
  233. radio: [],
  234. pk: []
  235. }
  236. };
  237. },
  238. onLoad(e) {
  239. this.name = e.name;
  240. this.fnGetData();
  241. // #ifndef H5
  242. const systemInfo = uni.getSystemInfoSync();
  243. this.safeAreaBottom = systemInfo.safeAreaInsets.bottom + 12;
  244. // #endif
  245. },
  246. onPullDownRefresh() {
  247. this.fnGetData();
  248. },
  249. methods: {
  250. fnGetData() {
  251. // 设置状态为加载中
  252. this.loading = 'loading';
  253. this.pageTitle = "加载中..."
  254. this.$httpApi.v2
  255. .getVoteDetail(this.name)
  256. .then(res => {
  257. this.pageTitle = "投票详情" + `(${types[res.vote.spec.type]})`
  258. const tempVoteRes = res;
  259. tempVoteRes.vote.spec.isVoted = this.fnCalcIsVoted()
  260. tempVoteRes.vote.spec.disabled = this.fnCalcIsVoted()
  261. tempVoteRes.vote.spec.options.map((option, index) => {
  262. option.value = option.id
  263. option.label = option.title
  264. option.isVoted = this.fnCalcIsVoted()
  265. option.checked = this.fnCalcIsChecked(option)
  266. if (tempVoteRes.vote.spec.type === 'single') {
  267. option.percent = this.fnCalcPercent(option, tempVoteRes.vote.stats);
  268. } else if (tempVoteRes.vote.spec.type === 'multiple') {
  269. option.percent = this.fnCalcPercent(option, tempVoteRes.vote.stats);
  270. } else if (tempVoteRes.vote.spec.type === 'pk') {
  271. option.percent = this.fnCalcPercent(option, tempVoteRes.vote.stats);
  272. }
  273. option.dataStr = JSON.stringify(option)
  274. return option
  275. })
  276. this.vote = tempVoteRes.vote
  277. console.log("this.vote", this.vote)
  278. this.detail = tempVoteRes;
  279. setTimeout(() => {
  280. this.loading = 'success';
  281. }, 200);
  282. })
  283. .catch(err => {
  284. console.error(err);
  285. this.loading = 'error';
  286. this.pageTitle = "加载失败,请重试..."
  287. })
  288. .finally(() => {
  289. setTimeout(() => {
  290. uni.hideLoading();
  291. uni.stopPullDownRefresh();
  292. this.fnSetPageTitle(this.pageTitle);
  293. }, 200);
  294. });
  295. },
  296. fnCalcPercent(voteOption, stats) {
  297. if (!this.fnCalcIsVoted()) return 0;
  298. if (!stats?.voteDataList) return 0;
  299. const option = stats.voteDataList.find(x => x.id == voteOption.id)
  300. if (!option) return 0;
  301. const percent = (option.voteCount / stats.voteCount) * 100
  302. return Math.round(percent)
  303. },
  304. fnCalcIsVoted() {
  305. return voteCacheUtil.has(this.name)
  306. },
  307. fnCalcIsChecked(option) {
  308. const data = voteCacheUtil.get(this.name)
  309. if (!data) return false;
  310. const checked = data.selected.includes(option.id)
  311. return checked
  312. },
  313. onOptionRadioChange(e) {
  314. this.submitForm.voteData = e.map(item => this.vote.spec.options[item.index]?.id);
  315. },
  316. onOptionCheckboxChange(e) {
  317. this.submitForm.voteData = e.map(item => this.vote.spec.options[item.index]?.id);
  318. },
  319. onOptionPkChange(e) {
  320. this.submitForm.voteData = e.map(item => this.vote.spec.options[item.index]?.id);
  321. },
  322. formatJsonStr(jsonStr) {
  323. return jsonStr ? JSON.parse(jsonStr) : {}
  324. },
  325. handleSubmit() {
  326. if (!this.vote.spec.canAnonymously) {
  327. uni.showModal({
  328. icon: "none",
  329. title: "提示",
  330. content: "该投票不允许匿名,请到博主的 网站端 进行投票!",
  331. cancelColor: "#666666",
  332. cancelText: "关闭",
  333. confirmText: "复制地址",
  334. success: (res) => {
  335. if (res.confirm) {
  336. this.$utils.copyText(this.$baseApiUrl, "复制成功")
  337. }
  338. }
  339. })
  340. return
  341. }
  342. uni.showLoading({
  343. title: "正在保存..."
  344. })
  345. // 使用简单版
  346. this.submitForm.voteData = this.vote.spec.options.filter(x => x.checked).map(item => item.id)
  347. this.$httpApi.v2
  348. .submitVote(this.name, this.submitForm, this.vote.spec.canAnonymously)
  349. .then(res => {
  350. uni.showToast({
  351. icon: "none",
  352. title: "提交成功"
  353. })
  354. voteCacheUtil.set(this.name, {
  355. selected: [...this.submitForm.voteData],
  356. data: this.vote
  357. })
  358. setTimeout(() => {
  359. uni.startPullDownRefresh()
  360. }, 1500);
  361. })
  362. .catch(err => {
  363. console.error(err);
  364. uni.showToast({
  365. icon: "none",
  366. title: "提交失败,请重试"
  367. })
  368. })
  369. },
  370. handleSelectSingleOption(option) {
  371. if (this.vote.spec.disabled) return
  372. this.vote.spec.options.map(item => {
  373. if (option.id == item.id) {
  374. item.checked = true
  375. } else {
  376. item.checked = false
  377. }
  378. })
  379. },
  380. handleSelectCheckboxOption(option) {
  381. if (this.vote.spec.disabled) return
  382. this.vote.spec.options.map(item => {
  383. if (option.id == item.id) {
  384. item.checked = !item.checked
  385. }
  386. })
  387. }
  388. }
  389. };
  390. </script>
  391. <style lang="scss" scoped>
  392. .app-page {
  393. box-sizing: border-box;
  394. width: 100vw;
  395. min-height: 100vh;
  396. display: flex;
  397. flex-direction: column;
  398. padding: 24rpx 0;
  399. padding-bottom: 160rpx;
  400. background-color: #fafafd;
  401. }
  402. .loading-wrap {
  403. padding: 0 24rpx;
  404. min-height: 100vh;
  405. }
  406. .empty {
  407. height: 60vh;
  408. display: flex;
  409. align-items: center;
  410. justify-content: center;
  411. }
  412. .w-full {
  413. width: 100%;
  414. }
  415. .wp-50 {
  416. width: 50%;
  417. }
  418. .vote-card {
  419. display: flex;
  420. flex-direction: column;
  421. box-sizing: border-box;
  422. margin: 0 24rpx;
  423. padding: 24rpx;
  424. border-radius: 12rpx;
  425. background-color: #ffff;
  426. box-shadow: 0rpx 2rpx 24rpx rgba(0, 0, 0, 0.03);
  427. overflow: hidden;
  428. margin-bottom: 24rpx;
  429. }
  430. .vote-card-head {
  431. margin-bottom: 12rpx;
  432. display: flex;
  433. align-items: flex-satrt;
  434. justify-content: space-between;
  435. .left {
  436. .title {
  437. font-size: 28rpx;
  438. font-weight: bold;
  439. }
  440. }
  441. }
  442. .vote-card-body {
  443. .remark {
  444. box-sizing: border-box;
  445. padding: 12rpx 6rpx;
  446. padding-top: 0;
  447. color: rgba(0, 0, 0, 0.75);
  448. }
  449. }
  450. .vote-card-foot {
  451. box-sizing: border-box;
  452. margin-top: 12px;
  453. padding-top: 6px;
  454. border-top: 2rpx solid #eee;
  455. }
  456. .single {
  457. ::v-deep {
  458. .tm-groupradio {
  459. box-sizing: border-box;
  460. display: flex;
  461. flex-wrap: wrap;
  462. gap: 16rpx 0;
  463. }
  464. .tm-checkbox {
  465. box-sizing: border-box;
  466. // display: block;
  467. // width: 100%;
  468. }
  469. .tm-button-label {
  470. width: 100%;
  471. }
  472. }
  473. }
  474. .multiple {
  475. ::v-deep {
  476. .tm-groupcheckbox {
  477. box-sizing: border-box;
  478. display: flex;
  479. flex-wrap: wrap;
  480. gap: 16rpx 0;
  481. }
  482. .tm-checkbox {
  483. box-sizing: border-box;
  484. // display: block;
  485. // width: 100%;
  486. }
  487. .tm-button-label {
  488. width: 100%;
  489. }
  490. }
  491. }
  492. .pk {
  493. box-sizing: border-box;
  494. width: 100%;
  495. ::v-deep {
  496. .pk-container {
  497. box-sizing: border-box;
  498. width: 100%;
  499. display: flex;
  500. }
  501. .radio-item {
  502. flex-grow: 1;
  503. min-width: 30% !important;
  504. max-width: 70% !important;
  505. }
  506. .radio-left {}
  507. .radio-right {}
  508. .option-item {
  509. box-sizing: border-box;
  510. width: 100%;
  511. padding: 24rpx;
  512. border-radius: 12rpx;
  513. }
  514. .option-item-left {
  515. background: linear-gradient(90deg, #3B82F6, #60A5FA);
  516. color: white;
  517. clip-path: polygon(0 0, calc(100% - 40rpx) 0, 100% 100%, 0 100%);
  518. }
  519. .option-item-right {
  520. background: linear-gradient(90deg, #F87171, #EF4444);
  521. color: white;
  522. clip-path: polygon(0 0, 100% 0, 100% 100%, 40rpx 100%);
  523. text-align: right;
  524. }
  525. .option-foot {
  526. margin-top: 24rpx;
  527. width: 100%;
  528. font-size: 24rpx;
  529. color: #666;
  530. .tm-groupradio {
  531. display: flex;
  532. gap: 12rpx;
  533. }
  534. .tm-checkbox {
  535. // width: 100%;
  536. max-width: initial !important;
  537. }
  538. .tm-button {
  539. width: 100%;
  540. }
  541. .left {
  542. box-sizing: border-box;
  543. }
  544. .right {
  545. box-sizing: border-box;
  546. .flex-start.fulled {
  547. justify-content: flex-end;
  548. }
  549. }
  550. }
  551. }
  552. }
  553. .sub-content {
  554. font-weight: bold;
  555. color: #2B2F33;
  556. padding: 12rpx 0;
  557. font-size: 32rpx;
  558. margin-bottom: 12rpx;
  559. }
  560. .sub-title {
  561. box-sizing: border-box;
  562. position: relative;
  563. margin-bottom: 12rpx;
  564. padding-left: 24rpx;
  565. font-weight: bold;
  566. font-size: 30rpx;
  567. &:before {
  568. content: "";
  569. width: 8rpx;
  570. height: 28rpx;
  571. position: absolute;
  572. left: 0;
  573. top: 6rpx;
  574. background: #03A9F4;
  575. border-radius: 6rpx;
  576. }
  577. .sub-title-count {
  578. font-size: 24rpx;
  579. font-weight: normal;
  580. }
  581. }
  582. .vote-submit {
  583. box-sizing: border-box;
  584. padding: 24rpx 36rpx;
  585. position: fixed;
  586. left: 0;
  587. width: 100vw;
  588. bottom: 0;
  589. background-color: rgba(255, 255, 255, 0.98);
  590. box-shadow: 0rpx 2rpx 24rpx rgba(0, 0, 0, 0.03);
  591. border-top: 2rpx solid #eee;
  592. z-index: 99;
  593. ::v-deep {
  594. .tm-button {
  595. text-align: center;
  596. }
  597. .tm-button-btn {
  598. margin: 0;
  599. width: 100%;
  600. }
  601. }
  602. }
  603. </style>