index.vue 17 KB

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