index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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.value?.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">
  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, index)"
  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, payOrderSubmit } from '@/api/common'
  114. import { orderCreated, getOrder, getSocialUser, socialUserBind, payOrderSubmit } from '@/api/common'
  115. const useUserStore = userStore()
  116. const baseInfo = computed(() => useUserStore?.baseInfo)
  117. const userInfo = computed(() => 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. // name: '银行卡支付',
  156. // value: 'alipay',
  157. // icon: 'icon-alipay',
  158. // color: '#1296db'
  159. // }
  160. ])
  161. const payTypeCurrent = ref(0)
  162. const channel = ref('')
  163. const radioChange = (index) => {
  164. channel.value = payType[index].value
  165. payTypeCurrent.value = index
  166. }
  167. const chooseIndex = ref(0)
  168. const chooseItem = ref(null)
  169. const handleChoose = (val, index) => {
  170. chooseId.value = val.id
  171. chooseIndex.value = index
  172. chooseItem.value = val
  173. }
  174. const handleOpen = () => {
  175. popup.value.open('bottom')
  176. }
  177. const handleClose = () => {
  178. popup.value.close()
  179. }
  180. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  181. const setOpenid = (openid) => {
  182. uni.setStorageSync('openid', openid)
  183. }
  184. const bind = () => {
  185. return new Promise(async (resolve, reject) => {
  186. // 1. 获得微信 code
  187. const codeResult = await uni.login()
  188. if (codeResult.errMsg !== 'login:ok') {
  189. return resolve(false)
  190. }
  191. // 2. 绑定账号 // // 社交快捷登录
  192. const obj = {
  193. type: socialType,
  194. code: codeResult.code,
  195. state: 'default',
  196. }
  197. const bindResult = await socialUserBind(obj);
  198. if (bindResult.code === 0) {
  199. setOpenid(bindResult.data)
  200. return resolve(true)
  201. } else {
  202. return resolve(false)
  203. }
  204. })
  205. }
  206. const bindWeiXin = () => {
  207. uni.showModal({
  208. title: '微信支付',
  209. content: '请先绑定微信再使用微信支付',
  210. success: function (res) {
  211. if (res.confirm) {
  212. // 微信小程序绑定
  213. bind()
  214. }
  215. },
  216. });
  217. }
  218. const socialType = 34; // 社交类型 - 微信小程序
  219. // 预支付
  220. const prepay = async (channel, orderData) => {
  221. return new Promise(async (resolve, reject) => {
  222. let data = {
  223. id: orderData.payOrder.id,
  224. channelCode: channel,
  225. channelExtras: {},
  226. };
  227. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
  228. if (['wx_pub', 'wx_lite'].includes(channel)) {
  229. const userRes = await getSocialUser(socialType)
  230. const openid = userRes?.data?.openid ? userRes.data.openid : null
  231. // 如果获取不到 openid,微信无法发起支付,此时需要引导
  232. if (!openid) {
  233. bindWeiXin()
  234. return
  235. }
  236. data.channelExtras.openid = openid
  237. }
  238. // 发起预支付 API 调用
  239. payOrderSubmit(data).then((res) => {
  240. // 成功时
  241. res.code === 0 && resolve(res)
  242. // 失败时
  243. if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
  244. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
  245. if (
  246. res.msg.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid
  247. res.msg.indexOf('下单账号与支付账号不一致') >= 0
  248. ) {
  249. // https://developers.weixin.qq.com/community/develop/doc/00008c53c347804beec82aed051c00
  250. bindWeiXin()
  251. }
  252. }
  253. })
  254. })
  255. }
  256. const weChatMiniProgramPay = async (orderData) => {
  257. let res = await prepay('wx_lite', orderData); // 预支付
  258. if (res?.code !== 0) {
  259. return;
  260. }
  261. // 调用微信小程序支付
  262. const payConfig = res?.data?.displayContent ? JSON.parse(res.data.displayContent) : null
  263. if (!payConfig) return uni.showToast({ title: '购买失败', icon: 'none'})
  264. uni.requestPayment({
  265. provider: 'wxpay',
  266. timeStamp: payConfig.timeStamp,
  267. nonceStr: payConfig.nonceStr,
  268. package: payConfig.packageValue,
  269. signType: 'RSA',
  270. paySign: payConfig.paySign,
  271. success: (res) => {
  272. popup.value.close()
  273. uni.showToast({ title: '支付成功', icon: 'none'})
  274. useUserStore.getUserInfo()
  275. // this.payResult('success');
  276. },
  277. fail: (err) => {
  278. if (err.errMsg === 'requestPayment:fail cancel') {
  279. uni.showToast({ title: '支付已取消', icon: 'none'})
  280. } else {
  281. // this.payResult('fail');
  282. uni.showToast({ title: '支付失败', icon: 'none'})
  283. }
  284. },
  285. });
  286. }
  287. // uni.showToast({ title: '支付升级中', icon: 'none'})
  288. // 支付
  289. const handlePay = async () => {
  290. const val = chooseItem.value
  291. try {
  292. const res = await getOrder({
  293. spuId: val.id, // 商品编号
  294. type: val.type
  295. })
  296. if (res.data) {
  297. // 获取支付码
  298. weChatMiniProgramPay(res.data)
  299. return
  300. }
  301. await orderCreated({
  302. spuId: val.id, // 商品编号
  303. spuName: val.name, // 商品名称
  304. price: val.price*100, // 价格
  305. type: val.type // 订单类型 0平台订单|1求职端订单|2招聘端订单|3会员套餐
  306. })
  307. const _res = await getOrder({
  308. spuId: val.id, // 商品编号
  309. type: val.type
  310. })
  311. // 获取支付码
  312. weChatMiniProgramPay(_res.data)
  313. } catch (error) {
  314. console.log(error)
  315. } finally {
  316. val.loading = false
  317. }
  318. }
  319. const getMemberList = async () => {
  320. try {
  321. const { data } = await getMembershipPackageList()
  322. if (!data || data.length === 0) {
  323. return
  324. }
  325. // memberList.value = data
  326. let vipFlagIndex = null
  327. const list = data.map((item, index) => {
  328. if (+item.id === +userInfo.value?.vipFlag) {
  329. vipFlagIndex = index // 低于当前套餐的(套餐)不展示
  330. }
  331. if (item.recommend) {
  332. recommend.value = index // 推荐套餐
  333. chooseIndex.value = index
  334. chooseItem.value = item
  335. }
  336. return {
  337. ...item,
  338. price: item.price/100,
  339. my: vipFlagIndex === index,
  340. list: JSON.parse(item.text),
  341. type: 3, // 订单类型 0平台订单|1求职端订单|2招聘端订单|3会员套餐
  342. loading: false
  343. }
  344. })
  345. // 低于当前套餐的(套餐)不展示
  346. memberList.value = vipFlagIndex ? list.slice(vipFlagIndex) : list
  347. handleChoose(memberList.value[0], recommend.value)
  348. if ((!userInfo.value?.vipFlag || userInfo.value?.vipExpireDate - new Date().getTime() > 0 ) && typeof recommend.value === 'number') {
  349. current.value = parseInt(recommend.value / 2)
  350. chooseId.value = memberList.value[recommend.value]?.id
  351. chooseIndex.value = recommend.value
  352. chooseItem.value = memberList.value[recommend.value]
  353. }
  354. } catch (error) {
  355. uni.showToast({ title: '查询数据失败,请重试', icon: 'none' })
  356. }
  357. }
  358. getMemberList()
  359. // const getPayMethodsList = async () => {
  360. // try {
  361. // } catch (error) {
  362. // // uni.showToast({ title: '查询数据失败,请重试', icon: 'none' })
  363. // }
  364. // }
  365. // getPayMethodsList()
  366. </script>
  367. <style lang="scss" scoped>
  368. .vipBox {
  369. // color: #a18a0f;
  370. padding: 80rpx 50rpx;
  371. display: flex;
  372. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  373. .avatar{
  374. position: relative;
  375. width: 100rpx;
  376. height: 100rpx;
  377. margin: 0;
  378. .img-box {
  379. width: 100%;
  380. height: 100%;
  381. border: 2rpx solid #ccc;
  382. border-radius: 50%;
  383. border: 1px solid gold;
  384. }
  385. .img-box-atc {
  386. border: 1px solid gold;
  387. }
  388. .vipIcon {
  389. position: absolute;
  390. width: 50%;
  391. height: 50%;
  392. bottom: 0;
  393. right: 0;
  394. transform: translate(0, 30%);
  395. }
  396. }
  397. .nameBox {
  398. display: flex;
  399. flex-direction: column;
  400. justify-content: space-around;
  401. margin-left: 30rpx;
  402. .name {
  403. color: #724d2b;
  404. }
  405. .vipInfo {
  406. color: #572a00;
  407. }
  408. }
  409. }
  410. .swiper-box {
  411. height: 200rpx;
  412. .swiper-items {
  413. display: grid;
  414. grid-template-columns: 1fr 1fr;
  415. }
  416. .swiper-item {
  417. display: flex;
  418. flex-direction: column;
  419. justify-content: center;
  420. align-items: center;
  421. height: 200rpx;
  422. padding: 20rpx 10rpx;
  423. box-sizing: border-box;
  424. .card {
  425. color: #774e20;
  426. background-color: rgb(255, 251, 248);
  427. border: 1px solid #f1b17a;
  428. width: 100%;
  429. height: 100%;
  430. border-radius: 10rpx;
  431. padding: 0 20rpx;
  432. box-sizing: border-box;
  433. display: flex;
  434. justify-content: space-between;
  435. align-items: center;
  436. position: relative;
  437. overflow: hidden;
  438. &.recommend {
  439. &::after {
  440. content: '推荐';
  441. position: absolute;
  442. right: 0;
  443. top: 0;
  444. padding: 6rpx 10rpx;
  445. font-size: 28rpx;
  446. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  447. }
  448. }
  449. &.vipFlag {
  450. &::before {
  451. content: '我的套餐';
  452. position: absolute;
  453. left: 0;
  454. top: 0;
  455. padding: 6rpx 10rpx;
  456. font-size: 28rpx;
  457. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  458. }
  459. }
  460. &.active {
  461. box-shadow: 0 0 18rpx 0 rgb(216 160 82);
  462. }
  463. }
  464. }
  465. }
  466. .itemBox {
  467. padding: 20rpx 40rpx;
  468. .item {
  469. // padding: 10rpx 0;
  470. margin-top: 20rpx;
  471. // color: rgba(119,78,32,.5);
  472. // &.active {
  473. color:#774e20;
  474. // }
  475. }
  476. }
  477. .pay {
  478. position: sticky;
  479. bottom: 0;
  480. padding: 0 40rpx 50rpx 40rpx;
  481. box-sizing: border-box;
  482. &-box {
  483. width: 100%;
  484. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  485. border-radius: 180rpx 0 180rpx 0;
  486. box-shadow: 3rpx 6rpx 10rpx 0rpx rgb(216 160 82);
  487. display: flex;
  488. justify-content: space-between;
  489. align-items: center;
  490. height: 100rpx;
  491. .price {
  492. padding: 0 40rpx;
  493. font-size: 40rpx;
  494. font-weight: 600;
  495. color: #e68735;
  496. }
  497. .btn {
  498. height: 100%;
  499. display: flex;
  500. align-items: center;
  501. padding: 0 40rpx;
  502. border: 2rpx solid #00897B;
  503. background: #00897B;
  504. color: #FFF;
  505. border-radius: 180rpx 0 180rpx 0;
  506. position: relative;
  507. // &::after {
  508. // content: '';
  509. // position: absolute;
  510. // width: 50rpx;
  511. // height: 50rpx;
  512. // background: radial-gradient(top right, transparent 50%, #00897B 50%);
  513. // left: 0;
  514. // top: 0;
  515. // margin-left: -25rpx;
  516. // border-radius: 180rpx;
  517. // }
  518. }
  519. }
  520. }
  521. .popup-content {
  522. max-height: 500px;
  523. display: flex;
  524. flex-direction: column;
  525. &-close {
  526. display: flex;
  527. padding: 10px;
  528. justify-content: flex-end;
  529. .icon {
  530. width: 30px;
  531. height: 30px;
  532. background: #ccc;
  533. border-radius: 30px;
  534. display: flex;
  535. align-items: center;
  536. justify-content: center;
  537. }
  538. }
  539. &-main {
  540. flex: 1;
  541. height: 0;
  542. overflow-y: auto;
  543. &-count {
  544. margin-bottom: 20px;
  545. text-align: center;
  546. .title {
  547. font-size: 28rpx;
  548. color: #666
  549. }
  550. .pay {
  551. font-size: 52rpx;
  552. color: #000;
  553. font-weight: 600;
  554. display: flex;
  555. align-items: center;
  556. justify-content: center;
  557. padding: 10px 0;
  558. }
  559. }
  560. &-type {
  561. width: 100%;
  562. padding: 0 20px;
  563. box-sizing: border-box;
  564. .card {
  565. border-radius: 10px;
  566. margin: 0 auto;
  567. background: #FFF;
  568. padding: 10px;
  569. &-label {
  570. padding: 15px 0;
  571. box-sizing: border-box;
  572. display: flex;
  573. justify-content: space-between;
  574. border-bottom: 1px solid #eee;
  575. &:last-of-type {
  576. border-bottom: none;
  577. }
  578. .name {
  579. display: flex;
  580. align-items: center;
  581. color: #333;
  582. }
  583. }
  584. }
  585. }
  586. }
  587. &-btn {
  588. height: 70px;
  589. width: 100%;
  590. margin-top: 10px;
  591. display: flex;
  592. align-items: center;
  593. justify-content: center;
  594. &-s {
  595. height: 40px;
  596. width: 75%;
  597. line-height: 40px;
  598. color: #FFF;
  599. // color: #724d2b;
  600. background: #00897B;
  601. // background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  602. border-radius: 90px;
  603. }
  604. }
  605. }
  606. .mr-1 {
  607. margin-right: 10px;
  608. }
  609. </style>