index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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="new Date().getTime() < 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="new Date().getTime() < userInfo?.vipExpireDate">
  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 v-if="showPayMethods" class="card">
  83. <radio-group @change="radioChange">
  84. <label class="card-label" v-for="item in payTypeList" :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" :disabled="item.disabled" :checked="item.value === channelValue" />
  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 { onHide, onShow } from '@dcloudio/uni-app';
  110. import { ref, computed } from 'vue'
  111. import { getUserAvatar } from '@/utils/avatar'
  112. import { userStore } from '@/store/user'
  113. import { getMembershipPackageList } from '@/api/vip'
  114. import { orderCreated, getOrder, getSocialUser, socialUserBind, payOrderSubmit, getOrderPayStatus, getEnableCodeList } 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 orderInfo = ref(null)
  123. const popup = ref()
  124. const amount = computed(() => {
  125. return parseFloat(+list.value.price).toFixed(2)
  126. })
  127. const memberListLength = computed(() => {
  128. const result = [];
  129. for (let i = 0; i < memberList.value.length; i += 2) {
  130. const pair = memberList.value.slice(i, i + 2)
  131. result.push(pair)
  132. }
  133. return result
  134. })
  135. const pName = computed(() => {
  136. return memberList.value.find(item => +item.id === +userInfo.value?.vipFlag)?.name
  137. })
  138. const remaining = computed(() => {
  139. if (!userInfo.value?.vipExpireDate) return null
  140. const diffInMs = userInfo.value?.vipExpireDate - new Date().getTime()
  141. const day = diffInMs / (1000 * 60 * 60 * 24)
  142. return day < 1 ? '今天' : Math.floor(day) + '天'
  143. })
  144. const list = computed(() => {
  145. const item = memberList.value.find(item => item.id === chooseId.value)
  146. return item ?? {}
  147. })
  148. const current = ref(0)
  149. const channelValue = ref('')
  150. const payType = [
  151. {
  152. name: '微信支付',
  153. value: 'wx_lite',
  154. icon: 'icon-weixinzhifu',
  155. color: '#1AAD19'
  156. },
  157. {
  158. name: '钱包支付',
  159. value: 'wallet',
  160. disabled: true,
  161. icon: 'icon-qianbao1',
  162. // color: '#fd9702'
  163. color: '#00B760'
  164. }
  165. ]
  166. const radioChange = (e) => {
  167. channelValue.value = e?.detail?.value || ''
  168. }
  169. const chooseItem = ref(null)
  170. const handleChoose = (val) => {
  171. chooseId.value = val.id
  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. // console.log('openid:', openid)
  237. data.channelExtras.openid = openid
  238. }
  239. // 发起预支付 API 调用
  240. payOrderSubmit(data).then((res) => {
  241. // 成功时
  242. res.code === 0 && resolve(res)
  243. // 失败时
  244. if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
  245. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
  246. if (
  247. res.msg.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid
  248. res.msg.indexOf('下单账号与支付账号不一致') >= 0
  249. ) {
  250. bindWeiXin()
  251. }
  252. }
  253. })
  254. })
  255. }
  256. let interTimer = null
  257. let payLoading = false
  258. const checkPayStatus = async (id) => {
  259. if (!id) return
  260. try {
  261. if (payLoading || !interTimer) return
  262. payLoading = true
  263. const res = await getOrderPayStatus({ id })
  264. if (res?.data?.status === 10) {
  265. if (interTimer) clearInterval(interTimer)
  266. // emit('paySuccess')
  267. setTimeout(async () => {
  268. const _userInfo = await useUserStore.getUserInfo()
  269. userInfo.value = _userInfo
  270. getMemberList() // 刷新套餐列表
  271. }, 1500)
  272. }
  273. } catch (error) {
  274. console.log(error)
  275. } finally {
  276. payLoading = false
  277. }
  278. }
  279. // 计时器
  280. const initIntervalFun = () => {
  281. if (interTimer) clearInterval(interTimer)
  282. // 查询是否已经支付
  283. const id = orderInfo.value?.payOrder?.id || orderInfo.value?.order?.payOrderId
  284. if (id) {
  285. interTimer = setInterval(() => {
  286. checkPayStatus(id)
  287. }, 1000)
  288. }
  289. }
  290. const weChatMiniProgramPay = async (orderData) => {
  291. orderInfo.value = orderData
  292. let res = await prepay(channelValue.value, orderData); // 预支付
  293. if (res?.code !== 0) {
  294. return;
  295. }
  296. // 调用微信小程序支付
  297. const payConfig = res?.data?.displayContent ? JSON.parse(res.data.displayContent) : null
  298. if (!payConfig) return uni.showToast({ title: '购买失败', icon: 'none'})
  299. uni.requestPayment({
  300. provider: 'wxpay',
  301. timeStamp: payConfig.timeStamp,
  302. nonceStr: payConfig.nonceStr,
  303. package: payConfig.packageValue,
  304. signType: 'RSA',
  305. paySign: payConfig.paySign,
  306. success: (res) => {
  307. initIntervalFun()
  308. popup.value.close()
  309. uni.showToast({ title: '支付成功', icon: 'none'})
  310. },
  311. fail: (err) => {
  312. if (err.errMsg === 'requestPayment:fail cancel') {
  313. uni.showToast({ title: '支付已取消', icon: 'none'})
  314. } else {
  315. // this.payResult('fail');
  316. uni.showToast({ title: '支付失败', icon: 'none'})
  317. }
  318. },
  319. });
  320. }
  321. // 支付
  322. const handlePay = async () => {
  323. if (!channelValue.value) {
  324. uni.showToast({ title: '请选择支付方式', icon: 'none'})
  325. return
  326. }
  327. const val = chooseItem.value
  328. try {
  329. const res = await getOrder({
  330. spuId: val.id, // 商品编号
  331. type: val.type
  332. })
  333. if (res.data) {
  334. // 获取支付码
  335. weChatMiniProgramPay(res.data)
  336. return
  337. }
  338. await orderCreated({
  339. spuId: val.id, // 商品编号
  340. spuName: val.name, // 商品名称
  341. price: val.price*100, // 价格
  342. type: val.type // 订单类型 0平台订单|1求职端订单|2招聘端订单|3会员套餐
  343. })
  344. const _res = await getOrder({
  345. spuId: val.id, // 商品编号
  346. type: val.type
  347. })
  348. // 获取支付码
  349. weChatMiniProgramPay(_res.data)
  350. } catch (error) {
  351. console.log(error)
  352. } finally {
  353. val.loading = false
  354. }
  355. }
  356. const getMemberList = async () => {
  357. try {
  358. const { data } = await getMembershipPackageList()
  359. if (!data || data.length === 0) {
  360. return
  361. }
  362. // memberList.value = data
  363. let vipFlagIndex = null
  364. const list = data.map((item, index) => {
  365. if (new Date().getTime() < userInfo.value?.vipExpireDate && +item.id === +userInfo.value?.vipFlag) {
  366. vipFlagIndex = index // 低于当前套餐的(套餐)不展示
  367. }
  368. if (item.recommend) {
  369. recommend.value = index // 推荐套餐
  370. }
  371. return {
  372. ...item,
  373. price: item.price/100,
  374. my: vipFlagIndex === index,
  375. list: JSON.parse(item.text),
  376. type: 3, // 订单类型 0平台订单|1求职端订单|2招聘端订单|3会员套餐
  377. loading: false
  378. }
  379. })
  380. memberList.value = vipFlagIndex ? list.slice(vipFlagIndex) : list // 低于当前套餐的(套餐)不展示
  381. // 轮播current位置
  382. // if ((!userInfo.value?.vipFlag || userInfo.value?.vipExpireDate - new Date().getTime() > 0 ) && typeof recommend.value === 'number') {
  383. // current.value = parseInt(recommend.value/2)
  384. // }
  385. handleChoose(memberList.value[0]) // 已购买或者未购买都选中第一个
  386. } catch (error) {
  387. uni.showToast({ title: '查询数据失败,请重试', icon: 'none' })
  388. }
  389. }
  390. getMemberList()
  391. onShow(() => {
  392. if (orderInfo && orderInfo.value?.id) initIntervalFun()
  393. })
  394. onHide(() => {
  395. if (interTimer) clearInterval(interTimer)
  396. })
  397. // 余额和其他还没有接暂时只支持微信支付
  398. let payTypeList = []
  399. const showPayMethods = ref(false)
  400. // 获取支付方式
  401. const getPayMethodsList = async () => {
  402. showPayMethods.value = false
  403. payTypeList = []
  404. try {
  405. const res = await getEnableCodeList({appId: 14})
  406. if (!res?.data?.length) {
  407. return
  408. }
  409. payTypeList.push(...payType.filter(e => res.data.includes(e.value)))
  410. const result = payType.find(item => !item.disabled && item.value)
  411. if (result) channelValue.value = result.value
  412. showPayMethods.value = true
  413. } catch (error) {
  414. console.log(error)
  415. }
  416. }
  417. getPayMethodsList()
  418. </script>
  419. <style lang="scss" scoped>
  420. .vipBox {
  421. // color: #a18a0f;
  422. padding: 80rpx 50rpx;
  423. display: flex;
  424. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  425. .avatar{
  426. position: relative;
  427. width: 100rpx;
  428. height: 100rpx;
  429. margin: 0;
  430. .img-box {
  431. width: 100%;
  432. height: 100%;
  433. border: 2rpx solid #ccc;
  434. border-radius: 50%;
  435. border: 1px solid gold;
  436. }
  437. .img-box-atc {
  438. border: 1px solid gold;
  439. }
  440. .vipIcon {
  441. position: absolute;
  442. width: 50%;
  443. height: 50%;
  444. bottom: 0;
  445. right: 0;
  446. transform: translate(0, 30%);
  447. }
  448. }
  449. .nameBox {
  450. display: flex;
  451. flex-direction: column;
  452. justify-content: space-around;
  453. margin-left: 30rpx;
  454. .name {
  455. color: #724d2b;
  456. }
  457. .vipInfo {
  458. color: #572a00;
  459. }
  460. }
  461. }
  462. .swiper-box {
  463. height: 230rpx;
  464. .swiper-items {
  465. display: grid;
  466. grid-template-columns: 1fr 1fr;
  467. }
  468. .swiper-item {
  469. display: flex;
  470. flex-direction: column;
  471. justify-content: center;
  472. align-items: center;
  473. height: 200rpx;
  474. padding: 20rpx 10rpx;
  475. box-sizing: border-box;
  476. .card {
  477. color: #774e20;
  478. background-color: rgb(255, 251, 248);
  479. border: 1px solid #f1b17a;
  480. width: 100%;
  481. height: 100%;
  482. border-radius: 10rpx;
  483. padding: 0 20rpx;
  484. box-sizing: border-box;
  485. display: flex;
  486. justify-content: space-between;
  487. align-items: center;
  488. position: relative;
  489. overflow: hidden;
  490. &.recommend {
  491. &::after {
  492. content: '推荐';
  493. position: absolute;
  494. right: 0;
  495. top: 0;
  496. padding: 6rpx 10rpx;
  497. font-size: 28rpx;
  498. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  499. }
  500. }
  501. &.vipFlag {
  502. &::before {
  503. content: '我的套餐';
  504. position: absolute;
  505. left: 0;
  506. top: 0;
  507. padding: 6rpx 10rpx;
  508. font-size: 28rpx;
  509. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  510. }
  511. }
  512. &.active {
  513. box-shadow: 0 0 18rpx 0 rgb(216 160 82);
  514. }
  515. }
  516. }
  517. }
  518. .itemBox {
  519. padding: 20rpx 40rpx;
  520. .item {
  521. // padding: 10rpx 0;
  522. margin-top: 20rpx;
  523. // color: rgba(119,78,32,.5);
  524. // &.active {
  525. color:#774e20;
  526. // }
  527. }
  528. }
  529. .pay {
  530. position: sticky;
  531. bottom: 0;
  532. padding: 0 40rpx 50rpx 40rpx;
  533. box-sizing: border-box;
  534. &-box {
  535. width: 100%;
  536. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  537. border-radius: 180rpx 0 180rpx 0;
  538. box-shadow: 3rpx 6rpx 10rpx 0rpx rgb(216 160 82);
  539. display: flex;
  540. justify-content: space-between;
  541. align-items: center;
  542. height: 100rpx;
  543. .price {
  544. padding: 0 40rpx;
  545. font-size: 40rpx;
  546. font-weight: 600;
  547. color: #e68735;
  548. }
  549. .btn {
  550. height: 100%;
  551. display: flex;
  552. align-items: center;
  553. padding: 0 40rpx;
  554. border: 2rpx solid #00B760;
  555. background: #00B760;
  556. color: #FFF;
  557. border-radius: 180rpx 0 180rpx 0;
  558. position: relative;
  559. // &::after {
  560. // content: '';
  561. // position: absolute;
  562. // width: 50rpx;
  563. // height: 50rpx;
  564. // background: radial-gradient(top right, transparent 50%, #00B760 50%);
  565. // left: 0;
  566. // top: 0;
  567. // margin-left: -25rpx;
  568. // border-radius: 180rpx;
  569. // }
  570. }
  571. }
  572. }
  573. .popup-content {
  574. max-height: 500px;
  575. display: flex;
  576. flex-direction: column;
  577. &-close {
  578. display: flex;
  579. padding: 10px;
  580. justify-content: flex-end;
  581. .icon {
  582. width: 30px;
  583. height: 30px;
  584. background: #ccc;
  585. border-radius: 30px;
  586. display: flex;
  587. align-items: center;
  588. justify-content: center;
  589. }
  590. }
  591. &-main {
  592. flex: 1;
  593. height: 0;
  594. overflow-y: auto;
  595. &-count {
  596. margin-bottom: 20px;
  597. text-align: center;
  598. .title {
  599. font-size: 28rpx;
  600. color: #666
  601. }
  602. .pay {
  603. font-size: 52rpx;
  604. color: #000;
  605. font-weight: 600;
  606. display: flex;
  607. align-items: center;
  608. justify-content: center;
  609. padding: 10px 0;
  610. }
  611. }
  612. &-type {
  613. width: 100%;
  614. padding: 0 20px;
  615. box-sizing: border-box;
  616. .card {
  617. border-radius: 10px;
  618. margin: 0 auto;
  619. background: #FFF;
  620. padding: 10px;
  621. &-label {
  622. padding: 15px 0;
  623. box-sizing: border-box;
  624. display: flex;
  625. justify-content: space-between;
  626. border-bottom: 1px solid #eee;
  627. &:last-of-type {
  628. border-bottom: none;
  629. }
  630. .name {
  631. display: flex;
  632. align-items: center;
  633. color: #333;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. &-btn {
  640. height: 70px;
  641. width: 100%;
  642. margin-top: 10px;
  643. display: flex;
  644. align-items: center;
  645. justify-content: center;
  646. &-s {
  647. height: 40px;
  648. width: 75%;
  649. line-height: 40px;
  650. color: #FFF;
  651. // color: #724d2b;
  652. background: #00B760;
  653. // background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  654. border-radius: 90px;
  655. }
  656. }
  657. }
  658. .mr-1 {
  659. margin-right: 10px;
  660. }
  661. </style>