index.vue 18 KB

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