index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <template>
  2. <view>
  3. <view class="vipBox">
  4. <view class="avatar">
  5. <img :src="getUserAvatar(baseInfo?.avatar, baseInfo?.sex)" alt="" class="img-box" :class="{'img-box-atc': userInfo?.vipExpireDate}">
  6. <image v-if="userInfo?.vipExpireDate" src="/static/svg/vip.svg" class="vipIcon"></image>
  7. </view>
  8. <view class="nameBox">
  9. <view class="name font-weight-bold font-size-16">{{ baseInfo?.name || userInfo?.phone }}</view>
  10. <view class="vipInfo font-size-14" v-if="remaining">
  11. {{ pName }}
  12. <view>将于{{ remaining }}后过期</view>
  13. </view>
  14. </view>
  15. </view>
  16. <view>
  17. <swiper class="swiper-box" :current="current" indicator-dots indicator-active-color="#f1b17a">
  18. <swiper-item v-for="(item, index) in memberListLength" :key="index" class="swiper-items">
  19. <view class="swiper-item" v-for="val in item" :key="val.id">
  20. <view
  21. class="card"
  22. :class="{ recommend: val.recommend, vipFlag: val.my, active: val.id === chooseId}"
  23. @tap="handleChoose(val)"
  24. >
  25. <text>{{ val.name }}</text>
  26. <view>
  27. <uni-icons color="#f30" type="icon-renminbi1688" size="16" custom-prefix="iconfont"></uni-icons>
  28. <text>{{ val.price }}</text>
  29. </view>
  30. </view>
  31. </view>
  32. </swiper-item>
  33. </swiper>
  34. <view v-if="typeof chooseId === 'number'" class="itemBox">
  35. 套餐权益 ( {{ list.name }} )
  36. <uni-section
  37. v-for="item in list.list"
  38. :key="item.id"
  39. class="item"
  40. :class="{ active: item.active }"
  41. titleColor="#774e20"
  42. subTitleColor="#774e20"
  43. :title="item.text"
  44. >
  45. <template v-slot:right>
  46. <uni-icons color="#774e20" :type="item.active ? 'checkmarkempty' : 'closeempty'" size="20"/>
  47. </template>
  48. </uni-section>
  49. </view>
  50. </view>
  51. <view class="pay" v-if="!list.my">
  52. <view class="pay-box">
  53. <view class="price">
  54. <uni-icons color="#e68735" type="icon-renminbi1688" size="16" custom-prefix="iconfont"></uni-icons>
  55. {{ amount }}
  56. </view>
  57. <view class="btn" @tap="handleOpen">
  58. 立刻升级
  59. </view>
  60. </view>
  61. </view>
  62. <uni-popup ref="popup" :is-mask-click="false" borderRadius="10px 10px 0 0" background-color="#eee">
  63. <view class="popup-content">
  64. <view class="popup-content-close">
  65. <view class="icon" @tap="handleClose">
  66. <uni-icons
  67. type="closeempty"
  68. color="#999"
  69. size="24"
  70. />
  71. </view>
  72. </view>
  73. <view class="popup-content-main">
  74. <view class="popup-content-main-count">
  75. <view class="title">{{ list.name }} 充值</view>
  76. <view class="pay">
  77. <uni-icons color="#000" type="icon-renminbi1688" size="16" custom-prefix="iconfont"></uni-icons>
  78. <view>{{ amount }}</view>
  79. </view>
  80. </view>
  81. <view class="popup-content-main-type">
  82. <view class="card">
  83. <radio-group @change="radioChange">
  84. <label class="card-label" v-for="(item, index) in payType" :key="item.value">
  85. <view class="name">
  86. <uni-icons :color="item.color" class="mr-1" :type="item.icon" size="24" custom-prefix="iconfont"></uni-icons>
  87. {{item.name}}
  88. </view>
  89. <view>
  90. <radio :value="item.value" :checked="index === payTypeCurrent" />
  91. </view>
  92. </label>
  93. </radio-group>
  94. </view>
  95. </view>
  96. </view>
  97. <view class="popup-content-btn">
  98. <button class="popup-content-btn-s" @tap="handlePay">
  99. 确认支付
  100. <uni-icons color="#FFF" type="icon-renminbi1688" size="16" custom-prefix="iconfont"></uni-icons>
  101. {{ amount }}
  102. </button>
  103. </view>
  104. </view>
  105. </uni-popup>
  106. </view>
  107. </template>
  108. <script setup>
  109. import { ref, computed } from 'vue'
  110. import { getUserAvatar } from '@/utils/avatar'
  111. import { userStore } from '@/store/user'
  112. import { getMembershipPackageList } from '@/api/vip'
  113. import { orderCreated, getOrder, getSocialUser, socialUserBind, payOrderSubmit } from '@/api/common'
  114. const useUserStore = userStore()
  115. const baseInfo = computed(() => useUserStore?.baseInfo)
  116. // const userInfo = computed(() => useUserStore?.userInfo)
  117. const userInfo = ref(useUserStore?.userInfo || {})
  118. const memberList = ref([])
  119. const recommend = ref(null)
  120. const chooseId = ref(null)
  121. const popup = ref()
  122. const amount = computed(() => {
  123. return parseFloat(+list.value.price).toFixed(2)
  124. })
  125. const memberListLength = computed(() => {
  126. const result = [];
  127. for (let i = 0; i < memberList.value.length; i += 2) {
  128. const pair = memberList.value.slice(i, i + 2)
  129. result.push(pair)
  130. }
  131. return result
  132. })
  133. const pName = computed(() => {
  134. return memberList.value.find(item => +item.id === +userInfo.value?.vipFlag)?.name
  135. })
  136. const remaining = computed(() => {
  137. if (!userInfo.value?.vipExpireDate) return null
  138. const diffInMs = userInfo.value?.vipExpireDate - new Date().getTime()
  139. const day = diffInMs / (1000 * 60 * 60 * 24)
  140. return day < 1 ? '今天' : Math.floor(day) + '天'
  141. })
  142. const list = computed(() => {
  143. const item = memberList.value.find(item => item.id === chooseId.value)
  144. return item ?? {}
  145. })
  146. const current = ref(0)
  147. const payType = ref([
  148. {
  149. name: '微信支付',
  150. value: 'wx_lite',
  151. icon: 'icon-weixinzhifu',
  152. color: '#1AAD19'
  153. }
  154. ])
  155. const payTypeCurrent = ref(0)
  156. const channel = ref('')
  157. const radioChange = (index) => {
  158. channel.value = payType[index].value
  159. payTypeCurrent.value = index
  160. }
  161. const chooseItem = ref(null)
  162. const handleChoose = (val) => {
  163. chooseId.value = val.id
  164. chooseItem.value = val
  165. }
  166. const handleOpen = () => {
  167. popup.value.open('bottom')
  168. }
  169. const handleClose = () => {
  170. popup.value.close()
  171. }
  172. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  173. const setOpenid = (openid) => {
  174. uni.setStorageSync('openid', openid)
  175. }
  176. const bind = () => {
  177. return new Promise(async (resolve, reject) => {
  178. // 1. 获得微信 code
  179. const codeResult = await uni.login()
  180. if (codeResult.errMsg !== 'login:ok') {
  181. return resolve(false)
  182. }
  183. // 2. 绑定账号 // // 社交快捷登录
  184. const obj = {
  185. type: socialType,
  186. code: codeResult.code,
  187. state: 'default',
  188. }
  189. const bindResult = await socialUserBind(obj);
  190. if (bindResult.code === 0) {
  191. setOpenid(bindResult.data)
  192. return resolve(true)
  193. } else {
  194. return resolve(false)
  195. }
  196. })
  197. }
  198. const bindWeiXin = () => {
  199. uni.showModal({
  200. title: '微信支付',
  201. content: '请先绑定微信再使用微信支付',
  202. success: function (res) {
  203. if (res.confirm) {
  204. // 微信小程序绑定
  205. bind()
  206. }
  207. },
  208. });
  209. }
  210. const socialType = 34; // 社交类型 - 微信小程序
  211. // 预支付
  212. const prepay = async (channel, orderData) => {
  213. return new Promise(async (resolve, reject) => {
  214. let data = {
  215. id: orderData.payOrder.id,
  216. channelCode: channel,
  217. channelExtras: {},
  218. };
  219. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
  220. if (['wx_pub', 'wx_lite'].includes(channel)) {
  221. const userRes = await getSocialUser(socialType)
  222. const openid = userRes?.data?.openid ? userRes.data.openid : null
  223. // 如果获取不到 openid,微信无法发起支付,此时需要引导
  224. if (!openid) {
  225. bindWeiXin()
  226. return
  227. }
  228. data.channelExtras.openid = openid
  229. }
  230. // 发起预支付 API 调用
  231. payOrderSubmit(data).then((res) => {
  232. // 成功时
  233. res.code === 0 && resolve(res)
  234. // 失败时
  235. if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
  236. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
  237. if (
  238. res.msg.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid
  239. res.msg.indexOf('下单账号与支付账号不一致') >= 0
  240. ) {
  241. bindWeiXin()
  242. }
  243. }
  244. })
  245. })
  246. }
  247. const weChatMiniProgramPay = async (orderData) => {
  248. let res = await prepay('wx_lite', orderData); // 预支付
  249. if (res?.code !== 0) {
  250. return;
  251. }
  252. // 调用微信小程序支付
  253. const payConfig = res?.data?.displayContent ? JSON.parse(res.data.displayContent) : null
  254. if (!payConfig) return uni.showToast({ title: '购买失败', icon: 'none'})
  255. uni.requestPayment({
  256. provider: 'wxpay',
  257. timeStamp: payConfig.timeStamp,
  258. nonceStr: payConfig.nonceStr,
  259. package: payConfig.packageValue,
  260. signType: 'RSA',
  261. paySign: payConfig.paySign,
  262. success: (res) => {
  263. popup.value.close()
  264. uni.showToast({ title: '支付成功', icon: 'none'})
  265. setTimeout(async () => {
  266. const _userInfo = await useUserStore.getUserInfo()
  267. userInfo.value = _userInfo
  268. getMemberList()
  269. }, 1500)
  270. // this.payResult('success');
  271. },
  272. fail: (err) => {
  273. if (err.errMsg === 'requestPayment:fail cancel') {
  274. uni.showToast({ title: '支付已取消', icon: 'none'})
  275. } else {
  276. // this.payResult('fail');
  277. uni.showToast({ title: '支付失败', icon: 'none'})
  278. }
  279. },
  280. });
  281. }
  282. // 支付
  283. const handlePay = async () => {
  284. const val = chooseItem.value
  285. try {
  286. const res = await getOrder({
  287. spuId: val.id, // 商品编号
  288. type: val.type
  289. })
  290. if (res.data) {
  291. // 获取支付码
  292. weChatMiniProgramPay(res.data)
  293. return
  294. }
  295. await orderCreated({
  296. spuId: val.id, // 商品编号
  297. spuName: val.name, // 商品名称
  298. price: val.price*100, // 价格
  299. type: val.type // 订单类型 0平台订单|1求职端订单|2招聘端订单|3会员套餐
  300. })
  301. const _res = await getOrder({
  302. spuId: val.id, // 商品编号
  303. type: val.type
  304. })
  305. // 获取支付码
  306. weChatMiniProgramPay(_res.data)
  307. } catch (error) {
  308. console.log(error)
  309. } finally {
  310. val.loading = false
  311. }
  312. }
  313. const getMemberList = async () => {
  314. try {
  315. const { data } = await getMembershipPackageList()
  316. if (!data || data.length === 0) {
  317. return
  318. }
  319. // memberList.value = data
  320. let vipFlagIndex = null
  321. const list = data.map((item, index) => {
  322. if (+item.id === +userInfo.value?.vipFlag) {
  323. vipFlagIndex = index // 低于当前套餐的(套餐)不展示
  324. }
  325. if (item.recommend) {
  326. recommend.value = index // 推荐套餐
  327. }
  328. return {
  329. ...item,
  330. price: item.price/100,
  331. my: vipFlagIndex === index,
  332. list: JSON.parse(item.text),
  333. type: 3, // 订单类型 0平台订单|1求职端订单|2招聘端订单|3会员套餐
  334. loading: false
  335. }
  336. })
  337. // 低于当前套餐的(套餐)不展示
  338. memberList.value = vipFlagIndex ? list.slice(vipFlagIndex) : list
  339. if ((!userInfo.value?.vipFlag || userInfo.value?.vipExpireDate - new Date().getTime() > 0 ) && typeof recommend.value === 'number') {
  340. current.value = vipFlagIndex === 0 ? 0 : parseInt((vipFlagIndex || recommend.value) / 2)
  341. }
  342. // 设置选中项
  343. const chooseIndex = vipFlagIndex !== null ? vipFlagIndex : recommend.value
  344. handleChoose(memberList.value[chooseIndex])
  345. } catch (error) {
  346. uni.showToast({ title: '查询数据失败,请重试', icon: 'none' })
  347. }
  348. }
  349. getMemberList()
  350. // const getPayMethodsList = async () => {
  351. // try {
  352. // } catch (error) {
  353. // // uni.showToast({ title: '查询数据失败,请重试', icon: 'none' })
  354. // }
  355. // }
  356. // getPayMethodsList()
  357. </script>
  358. <style lang="scss" scoped>
  359. .vipBox {
  360. // color: #a18a0f;
  361. padding: 80rpx 50rpx;
  362. display: flex;
  363. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  364. .avatar{
  365. position: relative;
  366. width: 100rpx;
  367. height: 100rpx;
  368. margin: 0;
  369. .img-box {
  370. width: 100%;
  371. height: 100%;
  372. border: 2rpx solid #ccc;
  373. border-radius: 50%;
  374. border: 1px solid gold;
  375. }
  376. .img-box-atc {
  377. border: 1px solid gold;
  378. }
  379. .vipIcon {
  380. position: absolute;
  381. width: 50%;
  382. height: 50%;
  383. bottom: 0;
  384. right: 0;
  385. transform: translate(0, 30%);
  386. }
  387. }
  388. .nameBox {
  389. display: flex;
  390. flex-direction: column;
  391. justify-content: space-around;
  392. margin-left: 30rpx;
  393. .name {
  394. color: #724d2b;
  395. }
  396. .vipInfo {
  397. color: #572a00;
  398. }
  399. }
  400. }
  401. .swiper-box {
  402. height: 230rpx;
  403. .swiper-items {
  404. display: grid;
  405. grid-template-columns: 1fr 1fr;
  406. }
  407. .swiper-item {
  408. display: flex;
  409. flex-direction: column;
  410. justify-content: center;
  411. align-items: center;
  412. height: 200rpx;
  413. padding: 20rpx 10rpx;
  414. box-sizing: border-box;
  415. .card {
  416. color: #774e20;
  417. background-color: rgb(255, 251, 248);
  418. border: 1px solid #f1b17a;
  419. width: 100%;
  420. height: 100%;
  421. border-radius: 10rpx;
  422. padding: 0 20rpx;
  423. box-sizing: border-box;
  424. display: flex;
  425. justify-content: space-between;
  426. align-items: center;
  427. position: relative;
  428. overflow: hidden;
  429. &.recommend {
  430. &::after {
  431. content: '推荐';
  432. position: absolute;
  433. right: 0;
  434. top: 0;
  435. padding: 6rpx 10rpx;
  436. font-size: 28rpx;
  437. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  438. }
  439. }
  440. &.vipFlag {
  441. &::before {
  442. content: '我的套餐';
  443. position: absolute;
  444. left: 0;
  445. top: 0;
  446. padding: 6rpx 10rpx;
  447. font-size: 28rpx;
  448. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  449. }
  450. }
  451. &.active {
  452. box-shadow: 0 0 18rpx 0 rgb(216 160 82);
  453. }
  454. }
  455. }
  456. }
  457. .itemBox {
  458. padding: 20rpx 40rpx;
  459. .item {
  460. // padding: 10rpx 0;
  461. margin-top: 20rpx;
  462. // color: rgba(119,78,32,.5);
  463. // &.active {
  464. color:#774e20;
  465. // }
  466. }
  467. }
  468. .pay {
  469. position: sticky;
  470. bottom: 0;
  471. padding: 0 40rpx 50rpx 40rpx;
  472. box-sizing: border-box;
  473. &-box {
  474. width: 100%;
  475. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  476. border-radius: 180rpx 0 180rpx 0;
  477. box-shadow: 3rpx 6rpx 10rpx 0rpx rgb(216 160 82);
  478. display: flex;
  479. justify-content: space-between;
  480. align-items: center;
  481. height: 100rpx;
  482. .price {
  483. padding: 0 40rpx;
  484. font-size: 40rpx;
  485. font-weight: 600;
  486. color: #e68735;
  487. }
  488. .btn {
  489. height: 100%;
  490. display: flex;
  491. align-items: center;
  492. padding: 0 40rpx;
  493. border: 2rpx solid #00897B;
  494. background: #00897B;
  495. color: #FFF;
  496. border-radius: 180rpx 0 180rpx 0;
  497. position: relative;
  498. // &::after {
  499. // content: '';
  500. // position: absolute;
  501. // width: 50rpx;
  502. // height: 50rpx;
  503. // background: radial-gradient(top right, transparent 50%, #00897B 50%);
  504. // left: 0;
  505. // top: 0;
  506. // margin-left: -25rpx;
  507. // border-radius: 180rpx;
  508. // }
  509. }
  510. }
  511. }
  512. .popup-content {
  513. max-height: 500px;
  514. display: flex;
  515. flex-direction: column;
  516. &-close {
  517. display: flex;
  518. padding: 10px;
  519. justify-content: flex-end;
  520. .icon {
  521. width: 30px;
  522. height: 30px;
  523. background: #ccc;
  524. border-radius: 30px;
  525. display: flex;
  526. align-items: center;
  527. justify-content: center;
  528. }
  529. }
  530. &-main {
  531. flex: 1;
  532. height: 0;
  533. overflow-y: auto;
  534. &-count {
  535. margin-bottom: 20px;
  536. text-align: center;
  537. .title {
  538. font-size: 28rpx;
  539. color: #666
  540. }
  541. .pay {
  542. font-size: 52rpx;
  543. color: #000;
  544. font-weight: 600;
  545. display: flex;
  546. align-items: center;
  547. justify-content: center;
  548. padding: 10px 0;
  549. }
  550. }
  551. &-type {
  552. width: 100%;
  553. padding: 0 20px;
  554. box-sizing: border-box;
  555. .card {
  556. border-radius: 10px;
  557. margin: 0 auto;
  558. background: #FFF;
  559. padding: 10px;
  560. &-label {
  561. padding: 15px 0;
  562. box-sizing: border-box;
  563. display: flex;
  564. justify-content: space-between;
  565. border-bottom: 1px solid #eee;
  566. &:last-of-type {
  567. border-bottom: none;
  568. }
  569. .name {
  570. display: flex;
  571. align-items: center;
  572. color: #333;
  573. }
  574. }
  575. }
  576. }
  577. }
  578. &-btn {
  579. height: 70px;
  580. width: 100%;
  581. margin-top: 10px;
  582. display: flex;
  583. align-items: center;
  584. justify-content: center;
  585. &-s {
  586. height: 40px;
  587. width: 75%;
  588. line-height: 40px;
  589. color: #FFF;
  590. // color: #724d2b;
  591. background: #00897B;
  592. // background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  593. border-radius: 90px;
  594. }
  595. }
  596. }
  597. .mr-1 {
  598. margin-right: 10px;
  599. }
  600. </style>