index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <template>
  2. <view style="z-index: 999;">
  3. <uni-popup ref="popup" :is-mask-click="false" borderRadius="10px 10px 0 0" background-color="#eee" @change="popupChange">
  4. <view class="popup-content">
  5. <view class="popup-content-close">
  6. <view class="icon" @tap="handleClose">
  7. <uni-icons
  8. type="closeempty"
  9. color="#999"
  10. size="24"
  11. />
  12. </view>
  13. </view>
  14. <view class="popup-content-main">
  15. <view class="popup-content-main-count">
  16. <view class="title">{{ title }}</view>
  17. <view class="pay">
  18. <uni-icons color="#000" type="icon-renminbi1688" size="16" custom-prefix="iconfont"></uni-icons>
  19. <view>{{ amount }}</view>
  20. </view>
  21. </view>
  22. <view class="popup-content-main-type">
  23. <view v-if="payTypeList?.length" class="card">
  24. <radio-group @change="radioChange">
  25. <label class="card-label" v-for="item in payTypeList" :key="item.value">
  26. <view class="name">
  27. <uni-icons :color="item.color" class="mr-1" :type="item.icon" size="24" custom-prefix="iconfont"></uni-icons>
  28. {{item.name}}
  29. </view>
  30. <view>
  31. <radio :value="item.value" :disabled="item.disabled" :checked="item.value === channelValue" />
  32. </view>
  33. </label>
  34. </radio-group>
  35. </view>
  36. </view>
  37. </view>
  38. <view class="popup-content-btn">
  39. <button class="popup-content-btn-s" @tap="handlePay(false)">
  40. 确认支付
  41. <uni-icons color="#FFF" type="icon-renminbi1688" size="16" custom-prefix="iconfont"></uni-icons>
  42. {{ amount }}
  43. </button>
  44. </view>
  45. </view>
  46. </uni-popup>
  47. </view>
  48. </template>
  49. <script setup>
  50. import { ref } from 'vue'
  51. import { onHide, onShow } from '@dcloudio/uni-app'
  52. // import { userStore } from '@/store/user'; const useUserStore = userStore()
  53. import { getSocialUser, socialUserBind, payOrderSubmit, getOrderPayStatus, getEnableCodeList } from '@/api/common'
  54. import { createTradeOrder, getUnpaidOrder } from '@/api/new/position'
  55. const emit = defineEmits(['paySuccess'])
  56. const props = defineProps({
  57. title: { // 标题
  58. type: String,
  59. default: '支付'
  60. },
  61. amount: { // 支付金额
  62. type: [String, Number],
  63. default: '金额获取失败'
  64. },
  65. })
  66. const payType = [
  67. {
  68. name: '微信支付',
  69. value: 'wx_lite',
  70. icon: 'icon-weixinzhifu',
  71. color: '#1AAD19'
  72. },
  73. // {
  74. // name: '钱包支付',
  75. // value: 'wallet',
  76. // disabled: true,
  77. // icon: 'icon-qianbao1',
  78. // color: '#00B760'
  79. // }
  80. ]
  81. // 余额和其他还没有接暂时只支持微信支付
  82. const payTypeList = ref([])
  83. // 获取支付方式
  84. const getPayMethodsList = async () => {
  85. payTypeList.value = []
  86. try {
  87. const res = await getEnableCodeList({appId: 10})
  88. if (!res?.data?.length) {
  89. return
  90. }
  91. payTypeList.value.push(...payType.filter(e => res.data.includes(e.value)))
  92. const result = payType.find(item => !item.disabled && item.value)
  93. if (result) channelValue.value = result.value
  94. } catch (error) {
  95. console.log(error)
  96. }
  97. }
  98. getPayMethodsList()
  99. const channelValue = ref('')
  100. const radioChange = (e) => {
  101. channelValue.value = e?.detail?.value || ''
  102. }
  103. const tabBarShow = (show = false) => { // 显示/隐藏TabBar
  104. const currentPage = getCurrentPages()
  105. if (!currentPage) return
  106. const currentTabBar = currentPage[0]?.getTabBar?.()
  107. currentTabBar?.setData({ show })
  108. }
  109. const popup = ref()
  110. const handleClose = () => {
  111. tabBarShow(true)
  112. popup.value.close()
  113. }
  114. const query = ref(null)
  115. const handleOpen = (val) => {
  116. query.value = val
  117. tabBarShow(false)
  118. popup.value.open('bottom')
  119. }
  120. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  121. const setOpenid = (openid) => {
  122. uni.setStorageSync('openid', openid)
  123. }
  124. const bind = () => {
  125. return new Promise(async (resolve, reject) => {
  126. // 1. 获得微信 code
  127. const codeResult = await uni.login()
  128. if (codeResult.errMsg !== 'login:ok') {
  129. return resolve(false)
  130. }
  131. // 2. 绑定账号 // // 社交快捷登录
  132. const obj = {
  133. type: socialType,
  134. code: codeResult.code,
  135. state: 'default',
  136. }
  137. const bindResult = await socialUserBind(obj);
  138. if (bindResult.code === 0) {
  139. setOpenid(bindResult.data)
  140. return resolve(true)
  141. } else {
  142. return resolve(false)
  143. }
  144. })
  145. }
  146. const bindWeiXin = () => {
  147. uni.showModal({
  148. title: '微信支付',
  149. content: '请先绑定微信再使用微信支付',
  150. success: function (res) {
  151. if (res.confirm) {
  152. // 微信小程序绑定
  153. bind()
  154. }
  155. },
  156. });
  157. }
  158. const socialType = 34; // 社交类型 - 微信小程序
  159. // 预支付
  160. const prepay = async (channel, orderData) => {
  161. return new Promise(async (resolve, reject) => {
  162. let data = {
  163. id: orderData?.payOrder?.id,
  164. channelCode: channel,
  165. channelExtras: {},
  166. };
  167. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
  168. if (['wx_pub', 'wx_lite'].includes(channel)) {
  169. const userRes = await getSocialUser(socialType)
  170. const openid = userRes?.data?.openid ? userRes.data.openid : null
  171. // 如果获取不到 openid,微信无法发起支付,此时需要引导
  172. if (!openid) {
  173. bindWeiXin()
  174. return
  175. }
  176. data.channelExtras.openid = openid
  177. }
  178. // 发起预支付 API 调用
  179. payOrderSubmit(data).then((res) => {
  180. // 成功时
  181. res.code === 0 && resolve(res)
  182. // 失败时
  183. if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
  184. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
  185. if (
  186. res.msg.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid
  187. res.msg.indexOf('下单账号与支付账号不一致') >= 0
  188. ) {
  189. bindWeiXin()
  190. }
  191. }
  192. })
  193. })
  194. }
  195. let interTimer = null
  196. let payLoading = false
  197. const checkPayStatus = async (id) => {
  198. if (!id) return
  199. uni.showLoading({ title: '加载中' })
  200. try {
  201. if (payLoading || !interTimer) return
  202. payLoading = true
  203. const res = await getOrderPayStatus({ id })
  204. if (res?.data?.status === 10) {
  205. handleClose()
  206. if (interTimer) clearInterval(interTimer)
  207. uni.hideLoading()
  208. uni.showToast({ title: '支付成功', icon: 'none'})
  209. setTimeout(async () => {
  210. emit('paySuccess')
  211. }, 1500)
  212. }
  213. } catch (error) {
  214. console.log(error)
  215. handleClose()
  216. uni.hideLoading()
  217. if (interTimer) clearInterval(interTimer)
  218. } finally {
  219. payLoading = false
  220. }
  221. }
  222. // 计时器
  223. const initIntervalFun = () => {
  224. if (interTimer) clearInterval(interTimer)
  225. // 查询是否已经支付
  226. const id = orderInfo.value?.payOrder?.id || orderInfo.value?.order?.payOrderId
  227. if (id) {
  228. interTimer = setInterval(() => {
  229. checkPayStatus(id)
  230. }, 1000)
  231. }
  232. }
  233. const orderInfo = ref({})
  234. const weChatMiniProgramPay = async (data) => {
  235. orderInfo.value = data
  236. let res = await prepay(channelValue.value, data); // 预支付
  237. if (res?.code !== 0) {
  238. return;
  239. }
  240. // 调用微信小程序支付
  241. const payConfig = res?.data?.displayContent ? JSON.parse(res.data.displayContent) : null
  242. if (!payConfig) return uni.showToast({ title: '购买失败', icon: 'none'})
  243. uni.requestPayment({
  244. provider: 'wxpay',
  245. timeStamp: payConfig.timeStamp,
  246. nonceStr: payConfig.nonceStr,
  247. package: payConfig.packageValue,
  248. signType: 'RSA',
  249. paySign: payConfig.paySign,
  250. success: (res) => {
  251. // 用户支付成功
  252. initIntervalFun()
  253. // handleClose()
  254. },
  255. fail: (err) => {
  256. if (err.errMsg === 'requestPayment:fail cancel') {
  257. uni.showToast({ title: '支付已取消', icon: 'none'})
  258. } else {
  259. uni.showToast({ title: '支付失败', icon: 'none'})
  260. }
  261. },
  262. });
  263. }
  264. // 确认支付
  265. const handlePay = async (retry = false) => {
  266. if (!channelValue.value) {
  267. uni.showToast({ title: '请选择支付方式', icon: 'none'})
  268. return
  269. }
  270. // 判断是否已有创建好的订单,有就直接跳转支付,没有就创建订单
  271. try {
  272. const params = {
  273. spuId: query.value?.spuId, // 商品编号
  274. type: query.value?.type // 订单类型 0平台订单|1发布职位|2发布众聘职位|3会员套餐|4企业会员套餐|5招聘会门票
  275. }
  276. if (!params.spuId || params.type === null || params.type === undefined) return
  277. const res = await getUnpaidOrder(params) // 查询订单
  278. if (res.data) {
  279. // 获取支付码
  280. weChatMiniProgramPay(res.data)
  281. return
  282. }
  283. if (!retry) setOrderCreated() // 创建订单
  284. } catch (error) {
  285. console.log(error)
  286. }
  287. }
  288. const setOrderCreated = async () => {
  289. const params = {
  290. spuId: query.value?.spuId, // 商品编号
  291. type: query.value?.type, // 订单类型 0平台订单|1发布职位|2发布众聘职位|3会员套餐|4企业会员套餐|5招聘会门票
  292. price: query.value?.price, // 价格
  293. spuName: query.value?.spuName, // 商品名称
  294. }
  295. if (!params.spuId || params.type === null || params.type === undefined || !params.price || !params.spuName) return
  296. await createTradeOrder(params)
  297. handlePay(true) // 避免死循环
  298. }
  299. onShow(() => {
  300. if (orderInfo && orderInfo.value?.id) initIntervalFun()
  301. })
  302. onHide(() => {
  303. if (interTimer) clearInterval(interTimer)
  304. })
  305. const popupChange = (e) => {
  306. if (!e || e.show === undefined) return
  307. if (e.show) {
  308. // 支付弹窗打开
  309. tabBarShow(false)
  310. if (orderInfo && orderInfo.value?.id) initIntervalFun()
  311. } else {
  312. // 支付弹窗关闭
  313. tabBarShow(true)
  314. if (interTimer) clearInterval(interTimer)
  315. }
  316. }
  317. defineExpose({
  318. handleOpen
  319. })
  320. </script>
  321. <style lang="scss" scoped>
  322. .pay {
  323. position: sticky;
  324. bottom: 0;
  325. padding: 0 40rpx 50rpx 40rpx;
  326. box-sizing: border-box;
  327. &-box {
  328. width: 100%;
  329. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  330. border-radius: 180rpx 0 180rpx 0;
  331. box-shadow: 3rpx 6rpx 10rpx 0rpx rgb(216 160 82);
  332. display: flex;
  333. justify-content: space-between;
  334. align-items: center;
  335. height: 100rpx;
  336. .price {
  337. padding: 0 40rpx;
  338. font-size: 40rpx;
  339. font-weight: 600;
  340. color: #e68735;
  341. }
  342. .btn {
  343. height: 100%;
  344. display: flex;
  345. align-items: center;
  346. padding: 0 40rpx;
  347. border: 2rpx solid #00B760;
  348. background: #00B760;
  349. color: #FFF;
  350. border-radius: 180rpx 0 180rpx 0;
  351. position: relative;
  352. // &::after {
  353. // content: '';
  354. // position: absolute;
  355. // width: 50rpx;
  356. // height: 50rpx;
  357. // background: radial-gradient(top right, transparent 50%, #00B760 50%);
  358. // left: 0;
  359. // top: 0;
  360. // margin-left: -25rpx;
  361. // border-radius: 180rpx;
  362. // }
  363. }
  364. }
  365. }
  366. .popup-content {
  367. z-index: 999;
  368. max-height: 500px;
  369. display: flex;
  370. flex-direction: column;
  371. &-close {
  372. display: flex;
  373. padding: 10px;
  374. justify-content: flex-end;
  375. .icon {
  376. width: 30px;
  377. height: 30px;
  378. background: #ccc;
  379. border-radius: 30px;
  380. display: flex;
  381. align-items: center;
  382. justify-content: center;
  383. }
  384. }
  385. &-main {
  386. flex: 1;
  387. height: 0;
  388. overflow-y: auto;
  389. &-count {
  390. margin-bottom: 20px;
  391. text-align: center;
  392. .title {
  393. font-size: 28rpx;
  394. color: #666
  395. }
  396. .pay {
  397. font-size: 52rpx;
  398. color: #000;
  399. font-weight: 600;
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. padding: 10px 0;
  404. }
  405. }
  406. &-type {
  407. width: 100%;
  408. padding: 0 20px;
  409. box-sizing: border-box;
  410. .card {
  411. border-radius: 10px;
  412. margin: 0 auto;
  413. background: #FFF;
  414. padding: 10px;
  415. &-label {
  416. padding: 15px 0;
  417. box-sizing: border-box;
  418. display: flex;
  419. justify-content: space-between;
  420. border-bottom: 1px solid #eee;
  421. &:last-of-type {
  422. border-bottom: none;
  423. }
  424. .name {
  425. display: flex;
  426. align-items: center;
  427. color: #333;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. &-btn {
  434. height: 70px;
  435. width: 100%;
  436. margin-top: 10px;
  437. display: flex;
  438. align-items: center;
  439. justify-content: center;
  440. &-s {
  441. height: 40px;
  442. width: 75%;
  443. line-height: 40px;
  444. color: #FFF;
  445. // color: #724d2b;
  446. background: #00B760;
  447. // background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  448. border-radius: 90px;
  449. }
  450. }
  451. }
  452. .mr-1 {
  453. margin-right: 10px;
  454. }
  455. </style>