article-vote.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <template>
  2. <view class="vote-card" :class="[loading]">
  3. <view v-if="loading !=='success'" class="vote-error" @click="fnGetData()">
  4. {{loadingText}}
  5. </view>
  6. <template v-else>
  7. <view class="vote-card-head flex flex-col">
  8. <view class="flex justify-between">
  9. <view class="flex">
  10. <tm-tags color="orange" style="min-width:40rpx;" :shadow="0" rounded size="s"
  11. model="fill">{{ index + 1 }}</tm-tags>
  12. <tm-tags color="light-blue" :shadow="0" rounded size="s"
  13. model="fill">{{ vote.spec._uh_type }}</tm-tags>
  14. <tm-tags :color="vote.spec._uh_state.color" size="s" rounded :shadow="0"
  15. model="fill">{{vote.spec._uh_state.state}}</tm-tags>
  16. </view>
  17. <view class="flex-shrink">
  18. <tm-button theme="light-blue" :shadow="0" dense size="s"
  19. @click="handleToVoteDetail(vote)">查看投票详情</tm-button>
  20. </view>
  21. </view>
  22. <view class="title">
  23. {{ vote.spec.title }}
  24. </view>
  25. </view>
  26. <view class="vote-card-body">
  27. <view v-if="vote.spec.remark" class="remark text-size-s">
  28. {{vote.spec.remark}}
  29. </view>
  30. <template>
  31. <!-- 单选 -->
  32. <view v-if="vote.spec.type==='single'" class="single">
  33. <view class="w-full flex flex-col uh-gap-8">
  34. <template v-if="vote.spec.isVoted || vote.spec.hasEnded">
  35. <view v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  36. class="is-voted-item" :class="[option.checked?'selected':'']" :style="{
  37. '--percent': option._uh_percent + '%'
  38. }">
  39. <view class="is-voted-item-content flex w-full flex-between uh-gap-4">
  40. <view class="text-align-left flex-1 text-break">
  41. {{option.title }}
  42. </view>
  43. <view class="flex-shrink ">
  44. {{option._uh_percent }}%
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <template v-else>
  50. <view v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  51. class="vote-select-option flex-1 w-full text-break"
  52. :class="[option.checked?'light-blue':'grey-lighten-3']"
  53. @click="handleSelectSingleOption(option)">
  54. {{option.title }}
  55. </view>
  56. </template>
  57. </view>
  58. </view>
  59. <!-- 多选 -->
  60. <view v-else-if="vote.spec.type==='multiple'" class="multiple">
  61. <view class="w-full flex flex-col uh-gap-8">
  62. <template v-if="vote.spec.isVoted || vote.spec.hasEnded">
  63. <view v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  64. class="is-voted-item" :class="[option.checked?'selected':'']" :style="{
  65. '--percent': option._uh_percent + '%'
  66. }">
  67. <view class="is-voted-item-content flex w-full flex-between uh-gap-4">
  68. <view class="text-align-left flex-1 text-break">
  69. {{option.title }}
  70. </view>
  71. <view class="flex-shrink ">
  72. {{option._uh_percent }}%
  73. </view>
  74. </view>
  75. </view>
  76. </template>
  77. <template v-else>
  78. <view v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  79. class="vote-select-option flex-1 w-full text-break"
  80. :class="[option.checked?'light-blue':'grey-lighten-3']"
  81. @click="handleSelectCheckboxOption(option)">
  82. {{option.title }}
  83. </view>
  84. </template>
  85. </view>
  86. </view>
  87. <!-- PK -->
  88. <view v-else-if="vote.spec.type==='pk'" class="pk">
  89. <view class="pk-container">
  90. <view class="radio-item" v-for="(option,optionIndex) in vote.spec.options"
  91. :key="optionIndex" :class="[optionIndex==0?'radio-left':'radio-right']"
  92. :style="{width:option._uh_percent + '%'}">
  93. <view class="option-item"
  94. :class="[optionIndex==0?'option-item-left':'option-item-right']">
  95. {{option._uh_percent }}%
  96. </view>
  97. </view>
  98. </view>
  99. <view class="option-foot w-full flex flex-between uh-mt-12">
  100. <view class="w-full flex flex-col uh-gap-8">
  101. <template v-if="vote.spec.isVoted || vote.spec.hasEnded">
  102. <view v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  103. class="is-voted-item" :class="[option.checked?'selected':'']" :style="{
  104. '--percent': option._uh_percent + '%'
  105. }">
  106. <view class="is-voted-item-content flex w-full flex-between uh-gap-4">
  107. <view class="text-align-left flex-1 text-break">
  108. {{option.title }}
  109. </view>
  110. <view class="flex-shrink ">
  111. {{option._uh_percent }}%
  112. </view>
  113. </view>
  114. </view>
  115. </template>
  116. <template v-else>
  117. <view v-for="(option,optionIndex) in vote.spec.options" :key="optionIndex"
  118. class="vote-select-option flex-1 w-full text-break"
  119. :class="[option.checked?'light-blue':'grey-lighten-3']"
  120. @click="handleSelectSingleOption(option)">
  121. 选项{{ optionIndex+1}}:{{option.title }}
  122. </view>
  123. </template>
  124. </view>
  125. </view>
  126. </view>
  127. </template>
  128. </view>
  129. <view class="vote-card-foot flex flex-between">
  130. <view class="left flex">
  131. <tm-tags v-if="vote.spec.timeLimit==='permanent'" color="grey-darken-2" rounded size="s"
  132. model="text">结束:永久有效 </tm-tags>
  133. <tm-tags v-else color="grey-darken-2" rounded size="s" model="text">
  134. <template
  135. v-if="vote.spec._state=='未开始'">开始:{{ {d: vote.spec.startDate, f: 'yyyy-MM-dd HH:mm'} | formatTime }}
  136. </template>
  137. <template v-else>结束:{{ {d: vote.spec.endDate, f: 'yyyy-MM-dd HH:mm'} | formatTime }}
  138. </template>
  139. </tm-tags>
  140. </view>
  141. <view class="right flex flex-end">
  142. <tm-tags color="grey-darken-2" rounded size="s" model="text">{{ vote.stats.voteCount }}
  143. 人已参与</tm-tags>
  144. <tm-tags v-if="vote.spec.isVoted" color="blue" rounded size="s" model="text">已投票</tm-tags>
  145. </view>
  146. </view>
  147. <view v-if="submitForm.voteData.length!==0" class="box-border uh-mt-12 w-full uh-px-2">
  148. <tm-button v-if="fnCalcIsVoted()" theme="white" text :block="true" class="w-full">您已参与投票</tm-button>
  149. <tm-button v-else-if="vote.spec._uh_state.state==='未开始'" theme="orange" text class="w-full" :height="72"
  150. :block="true" @click="handleSubmitTip('投票未开始')">投票未开始</tm-button>
  151. <tm-button v-else-if="vote.spec._uh_state.state==='已结束'" theme="red" text class="w-full" :height="72"
  152. :block="true" @click="handleSubmitTip('投票已结束')">投票已结束</tm-button>
  153. <tm-button v-else-if="!vote.spec.canAnonymously" theme="red" :shadow="0" class="w-full" :height="72"
  154. text :block="true" @click="handleSubmit()">不支持匿名投票</tm-button>
  155. <tm-button v-else-if="submitForm.voteData.length===0" theme="white" text class="w-full" :height="72"
  156. :block="true" @click="handleSubmitTip('请选择选项')">提交投票(请选择选项)</tm-button>
  157. <tm-button v-else theme="light-blue" class="w-full" :height="72" :block="true" :loading="submitLoading"
  158. :disabled="submitLoading" @click="handleSubmit()">提交投票</tm-button>
  159. </view>
  160. </template>
  161. </view>
  162. </template>
  163. <script>
  164. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  165. import tmTags from '@/tm-vuetify/components/tm-tags/tm-tags.vue';
  166. import {
  167. VOTE_TYPES,
  168. calcVoteState,
  169. calcVotePercent,
  170. voteCacheUtil
  171. } from '@/utils/vote.js'
  172. export default {
  173. name: "ArticleVote",
  174. components: {
  175. tmButton,
  176. tmTags
  177. },
  178. props: {
  179. voteId: {
  180. type: String,
  181. default: ""
  182. },
  183. index: {
  184. type: Number,
  185. default: 0
  186. },
  187. article: {
  188. type: Object,
  189. default: () => ({})
  190. }
  191. },
  192. data() {
  193. return {
  194. loading: "loading",
  195. loadingText: "加载中,请稍等...",
  196. detail: null,
  197. vote: null,
  198. submitForm: {
  199. voteData: []
  200. },
  201. submitLoading: false
  202. }
  203. },
  204. created() {
  205. this.fnGetData();
  206. },
  207. methods: {
  208. fnGetData() {
  209. this.loadingText = "加载中,请稍等...";
  210. this.loading = "loading";
  211. this.$httpApi.v2
  212. .getVoteDetail(this.voteId)
  213. .then(res => {
  214. this.pageTitle = "投票详情" + `(${VOTE_TYPES[res.vote.spec.type]})`
  215. const tempVoteRes = res;
  216. tempVoteRes.vote.spec.isVoted = this.fnCalcIsVoted()
  217. tempVoteRes.vote.spec.disabled = this.fnCalcIsVoted()
  218. tempVoteRes.vote.spec._uh_state = calcVoteState(tempVoteRes.vote)
  219. tempVoteRes.vote.spec._uh_type = VOTE_TYPES[tempVoteRes.vote.spec.type]
  220. tempVoteRes.vote.spec.options.map((option, index) => {
  221. option.value = option.id
  222. option.label = option.title
  223. option.isVoted = this.fnCalcIsVoted()
  224. option.checked = this.fnCalcIsChecked(option)
  225. option._uh_percent = calcVotePercent(tempVoteRes.vote, option);
  226. option.dataStr = JSON.stringify(option)
  227. return option
  228. })
  229. this.vote = tempVoteRes.vote
  230. this.detail = tempVoteRes;
  231. setTimeout(() => {
  232. this.loading = 'success';
  233. }, 200);
  234. })
  235. .catch(err => {
  236. console.error(err);
  237. this.loading = 'error';
  238. this.loadingText = "投票内容加载失败,点击重试"
  239. })
  240. },
  241. fnCalcIsVoted() {
  242. return voteCacheUtil.has(this.voteId)
  243. },
  244. fnCalcIsChecked(option) {
  245. const data = voteCacheUtil.get(this.voteId)
  246. if (!data) return false;
  247. const checked = data.selected.includes(option.id)
  248. return checked
  249. },
  250. formatJsonStr(jsonStr) {
  251. return jsonStr ? JSON.parse(jsonStr) : {}
  252. },
  253. handleSubmitTip(text) {
  254. uni.showToast({
  255. icon: "none",
  256. title: text
  257. })
  258. },
  259. handleSubmit() {
  260. if (!this.vote.spec.canAnonymously) {
  261. uni.showModal({
  262. icon: "none",
  263. title: "提示",
  264. content: "该投票不支持匿名,请到博主的 网站端 进行投票!",
  265. cancelColor: "#666666",
  266. cancelText: "关闭",
  267. confirmText: "复制地址",
  268. success: (res) => {
  269. if (res.confirm) {
  270. console.log("this.article", this.article)
  271. const articleUrl = this.$baseApiUrl + (this.article?.status?.permalink ?? "")
  272. this.$utils.copyText(articleUrl, "原文地址复制成功")
  273. }
  274. }
  275. })
  276. return
  277. }
  278. uni.showLoading({
  279. title: "正在保存..."
  280. })
  281. this.submitLoading = true
  282. this.$httpApi.v2
  283. .submitVote(this.voteId, this.submitForm, this.vote.spec.canAnonymously)
  284. .then(res => {
  285. uni.showToast({
  286. icon: "none",
  287. title: "提交成功"
  288. })
  289. voteCacheUtil.set(this.voteId, {
  290. selected: [...this.submitForm.voteData],
  291. data: this.vote
  292. })
  293. this.fnGetData()
  294. })
  295. .catch(err => {
  296. console.error(err);
  297. uni.showToast({
  298. icon: "none",
  299. title: "提交失败,请重试"
  300. })
  301. }).finally(() => {
  302. this.submitLoading = false
  303. })
  304. },
  305. handleSelectSingleOption(option) {
  306. if (this.vote.spec._uh_state.state == '未开始') {
  307. this.showToast(`投票未开始`)
  308. return
  309. }
  310. if (this.vote.spec.hasEnded) return
  311. if (this.vote.spec.disabled) return
  312. this.vote.spec.options.map(item => {
  313. if (option.id == item.id) {
  314. item.checked = true
  315. } else {
  316. item.checked = false
  317. }
  318. })
  319. this.submitForm.voteData = this.vote.spec.options.filter(x => x.checked).map(item => item.id)
  320. },
  321. handleSelectCheckboxOption(option) {
  322. if (this.vote.spec._uh_state.state == '未开始') {
  323. this.showToast(`投票未开始`)
  324. return
  325. }
  326. if (this.vote.spec.hasEnded) return
  327. if (this.vote.spec.disabled) return
  328. const checkedList = this.vote.spec.options.filter(x => x.checked && x.id != option.id)
  329. if (this.vote.spec.type === 'multiple' && checkedList.length >= this.vote.spec.maxVotes) {
  330. this.showToast(`最多选择 ${this.vote.spec.maxVotes} 项`)
  331. return
  332. }
  333. this.vote.spec.options.map(item => {
  334. if (option.id == item.id) {
  335. item.checked = !item.checked
  336. }
  337. })
  338. this.submitForm.voteData = this.vote.spec.options.filter(x => x.checked).map(item => item.id)
  339. },
  340. handleToVoteDetail(vote) {
  341. uni.navigateTo({
  342. url: `/pagesA/vote-detail/vote-detail?name=${vote.metadata.name}`
  343. });
  344. },
  345. showToast(content) {
  346. uni.showToast({
  347. icon: "none",
  348. title: content,
  349. mask: true
  350. })
  351. }
  352. }
  353. }
  354. </script>
  355. <style lang="scss" scoped>
  356. .w-full {
  357. width: 100%;
  358. }
  359. .wp-50 {
  360. width: 50%;
  361. }
  362. .vote-card {
  363. display: flex;
  364. flex-direction: column;
  365. box-sizing: border-box;
  366. padding: 24rpx;
  367. border-radius: 12rpx;
  368. background-color: #ffff;
  369. overflow: hidden;
  370. margin-bottom: 12rpx;
  371. border: 1px solid #eee;
  372. &.error {
  373. padding: 0;
  374. border-style: dashed;
  375. border-color: #e88080;
  376. color: #e88080;
  377. background-color: rgba(232, 128, 128, 0.075);
  378. }
  379. &.loading {
  380. padding: 0;
  381. border-style: dashed;
  382. border-color: rgba(3, 174, 252, 1);
  383. color: rgba(3, 174, 252, 1);
  384. background-color: rgba(3, 174, 252, 0.075);
  385. }
  386. }
  387. .vote-error {
  388. padding: 50rpx 24rpx;
  389. font-size: 24rpx;
  390. border-radius: 12rpx;
  391. text-align: center;
  392. }
  393. .vote-card-head {
  394. margin-bottom: 12rpx;
  395. .title {
  396. padding: 12rpx 0;
  397. font-size: 28rpx;
  398. font-weight: bold;
  399. }
  400. }
  401. .vote-card-body {
  402. .remark {
  403. box-sizing: border-box;
  404. color: rgba(0, 0, 0, 0.75);
  405. margin-bottom: 24rpx;
  406. }
  407. }
  408. .vote-card-foot {
  409. box-sizing: border-box;
  410. padding-top: 6px;
  411. margin-top: 12px;
  412. border-top: 2rpx solid #eee;
  413. .left {}
  414. }
  415. .is-voted-item {
  416. min-height: 72rpx;
  417. box-sizing: border-box;
  418. position: relative;
  419. border-radius: 12rpx;
  420. background-color: rgba(229, 229, 229, 0.75);
  421. font-size: 24rpx;
  422. overflow: hidden;
  423. &::before {
  424. content: "";
  425. width: var(--percent);
  426. position: absolute;
  427. left: 0;
  428. top: 0;
  429. bottom: 0;
  430. background-color: rgba(208, 208, 208, 1);
  431. z-index: 0;
  432. border-radius: 6rpx;
  433. }
  434. &.selected {
  435. background-color: rgba(3, 169, 244, 0.35);
  436. color: #ffffff;
  437. &::before {
  438. background-color: rgba(3, 169, 244, 1);
  439. }
  440. }
  441. }
  442. .is-voted-item-content {
  443. box-sizing: border-box;
  444. min-height: 72rpx;
  445. padding: 12rpx 24rpx;
  446. position: relative;
  447. z-index: 2;
  448. }
  449. .vote-select-option {
  450. box-sizing: border-box;
  451. padding: 20rpx 24rpx;
  452. font-size: 24rpx;
  453. border-radius: 12rpx;
  454. transition: all 0.1s ease-in-out;
  455. }
  456. .single {
  457. ::v-deep {}
  458. }
  459. .multiple {
  460. ::v-deep {}
  461. }
  462. .pk {
  463. box-sizing: border-box;
  464. width: 100%;
  465. padding: 0 12rpx;
  466. ::v-deep {
  467. .pk-container {
  468. box-sizing: border-box;
  469. width: 100%;
  470. display: flex;
  471. }
  472. .radio-item {
  473. flex-grow: 1;
  474. min-width: 30% !important;
  475. max-width: 70% !important;
  476. }
  477. .radio-left {}
  478. .radio-right {}
  479. .option-item {
  480. box-sizing: border-box;
  481. width: 100%;
  482. padding: 24rpx;
  483. border-radius: 12rpx;
  484. }
  485. .option-item-left {
  486. background: linear-gradient(90deg, #3B82F6, #60A5FA);
  487. color: white;
  488. clip-path: polygon(0 0, calc(100% - 40rpx) 0, 100% 100%, 0 100%);
  489. }
  490. .option-item-right {
  491. background: linear-gradient(90deg, #F87171, #EF4444);
  492. color: white;
  493. clip-path: polygon(0 0, 100% 0, 100% 100%, 40rpx 100%);
  494. text-align: right;
  495. }
  496. .option-foot {
  497. width: 100%;
  498. margin-top: 6rpx;
  499. font-size: 24rpx;
  500. color: #666;
  501. .left {
  502. box-sizing: border-box;
  503. padding-right: 24rpx;
  504. }
  505. .right {
  506. box-sizing: border-box;
  507. padding-left: 24rpx;
  508. }
  509. }
  510. }
  511. }
  512. </style>