index.vue 12 KB

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