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'])
  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. try {
  200. if (payLoading || !interTimer) return
  201. payLoading = true
  202. const res = await getOrderPayStatus({ id })
  203. if (res?.data?.status === 10) {
  204. if (interTimer) clearInterval(interTimer)
  205. uni.showToast({ title: '支付成功', icon: 'none'})
  206. setTimeout(async () => {
  207. emit('paySuccess')
  208. }, 1500)
  209. }
  210. } catch (error) {
  211. console.log(error)
  212. } finally {
  213. payLoading = false
  214. }
  215. }
  216. // 计时器
  217. const initIntervalFun = () => {
  218. if (interTimer) clearInterval(interTimer)
  219. // 查询是否已经支付
  220. const id = orderInfo.value?.payOrder?.id || orderInfo.value?.order?.payOrderId
  221. if (id) {
  222. interTimer = setInterval(() => {
  223. checkPayStatus(id)
  224. }, 1000)
  225. }
  226. }
  227. const orderInfo = ref({})
  228. const weChatMiniProgramPay = async (data) => {
  229. orderInfo.value = data
  230. let res = await prepay(channelValue.value, data); // 预支付
  231. if (res?.code !== 0) {
  232. return;
  233. }
  234. // 调用微信小程序支付
  235. const payConfig = res?.data?.displayContent ? JSON.parse(res.data.displayContent) : null
  236. if (!payConfig) return uni.showToast({ title: '购买失败', icon: 'none'})
  237. uni.requestPayment({
  238. provider: 'wxpay',
  239. timeStamp: payConfig.timeStamp,
  240. nonceStr: payConfig.nonceStr,
  241. package: payConfig.packageValue,
  242. signType: 'RSA',
  243. paySign: payConfig.paySign,
  244. success: (res) => {
  245. // 用户支付成功
  246. initIntervalFun()
  247. handleClose()
  248. },
  249. fail: (err) => {
  250. if (err.errMsg === 'requestPayment:fail cancel') {
  251. uni.showToast({ title: '支付已取消', icon: 'none'})
  252. } else {
  253. uni.showToast({ title: '支付失败', icon: 'none'})
  254. }
  255. },
  256. });
  257. }
  258. // 确认支付
  259. const handlePay = async (retry = false) => {
  260. if (!channelValue.value) {
  261. uni.showToast({ title: '请选择支付方式', icon: 'none'})
  262. return
  263. }
  264. // 判断是否已有创建好的订单,有就直接跳转支付,没有就创建订单
  265. try {
  266. const params = {
  267. spuId: query.value?.spuId, // 商品编号
  268. type: query.value?.type // 订单类型 0平台订单|1发布职位|2发布众聘职位|3会员套餐|4企业会员套餐|5招聘会门票
  269. }
  270. if (!params.spuId || params.type === null || params.type === undefined) return
  271. const res = await getUnpaidOrder(params) // 查询订单
  272. if (res.data) {
  273. // 获取支付码
  274. weChatMiniProgramPay(res.data)
  275. return
  276. }
  277. if (!retry) setOrderCreated() // 创建订单
  278. } catch (error) {
  279. console.log(error)
  280. }
  281. }
  282. const setOrderCreated = async () => {
  283. const params = {
  284. spuId: query.value?.spuId, // 商品编号
  285. type: query.value?.type, // 订单类型 0平台订单|1发布职位|2发布众聘职位|3会员套餐|4企业会员套餐|5招聘会门票
  286. price: query.value?.price, // 价格
  287. spuName: query.value?.spuName, // 商品名称
  288. }
  289. if (!params.spuId || params.type === null || params.type === undefined || !params.price || !params.spuName) return
  290. await createTradeOrder(params)
  291. handlePay(true) // 避免死循环
  292. }
  293. onShow(() => {
  294. if (orderInfo && orderInfo.value?.id) initIntervalFun()
  295. })
  296. onHide(() => {
  297. if (interTimer) clearInterval(interTimer)
  298. })
  299. const popupChange = (e) => {
  300. if (!e || e.show === undefined) return
  301. if (e.show) {
  302. // 支付弹窗打开
  303. tabBarShow(false)
  304. if (orderInfo && orderInfo.value?.id) initIntervalFun()
  305. } else {
  306. // 支付弹窗关闭
  307. tabBarShow(true)
  308. if (interTimer) clearInterval(interTimer)
  309. }
  310. }
  311. defineExpose({
  312. handleOpen
  313. })
  314. </script>
  315. <style lang="scss" scoped>
  316. .pay {
  317. position: sticky;
  318. bottom: 0;
  319. padding: 0 40rpx 50rpx 40rpx;
  320. box-sizing: border-box;
  321. &-box {
  322. width: 100%;
  323. background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  324. border-radius: 180rpx 0 180rpx 0;
  325. box-shadow: 3rpx 6rpx 10rpx 0rpx rgb(216 160 82);
  326. display: flex;
  327. justify-content: space-between;
  328. align-items: center;
  329. height: 100rpx;
  330. .price {
  331. padding: 0 40rpx;
  332. font-size: 40rpx;
  333. font-weight: 600;
  334. color: #e68735;
  335. }
  336. .btn {
  337. height: 100%;
  338. display: flex;
  339. align-items: center;
  340. padding: 0 40rpx;
  341. border: 2rpx solid #00B760;
  342. background: #00B760;
  343. color: #FFF;
  344. border-radius: 180rpx 0 180rpx 0;
  345. position: relative;
  346. // &::after {
  347. // content: '';
  348. // position: absolute;
  349. // width: 50rpx;
  350. // height: 50rpx;
  351. // background: radial-gradient(top right, transparent 50%, #00B760 50%);
  352. // left: 0;
  353. // top: 0;
  354. // margin-left: -25rpx;
  355. // border-radius: 180rpx;
  356. // }
  357. }
  358. }
  359. }
  360. .popup-content {
  361. z-index: 999;
  362. max-height: 500px;
  363. display: flex;
  364. flex-direction: column;
  365. &-close {
  366. display: flex;
  367. padding: 10px;
  368. justify-content: flex-end;
  369. .icon {
  370. width: 30px;
  371. height: 30px;
  372. background: #ccc;
  373. border-radius: 30px;
  374. display: flex;
  375. align-items: center;
  376. justify-content: center;
  377. }
  378. }
  379. &-main {
  380. flex: 1;
  381. height: 0;
  382. overflow-y: auto;
  383. &-count {
  384. margin-bottom: 20px;
  385. text-align: center;
  386. .title {
  387. font-size: 28rpx;
  388. color: #666
  389. }
  390. .pay {
  391. font-size: 52rpx;
  392. color: #000;
  393. font-weight: 600;
  394. display: flex;
  395. align-items: center;
  396. justify-content: center;
  397. padding: 10px 0;
  398. }
  399. }
  400. &-type {
  401. width: 100%;
  402. padding: 0 20px;
  403. box-sizing: border-box;
  404. .card {
  405. border-radius: 10px;
  406. margin: 0 auto;
  407. background: #FFF;
  408. padding: 10px;
  409. &-label {
  410. padding: 15px 0;
  411. box-sizing: border-box;
  412. display: flex;
  413. justify-content: space-between;
  414. border-bottom: 1px solid #eee;
  415. &:last-of-type {
  416. border-bottom: none;
  417. }
  418. .name {
  419. display: flex;
  420. align-items: center;
  421. color: #333;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. &-btn {
  428. height: 70px;
  429. width: 100%;
  430. margin-top: 10px;
  431. display: flex;
  432. align-items: center;
  433. justify-content: center;
  434. &-s {
  435. height: 40px;
  436. width: 75%;
  437. line-height: 40px;
  438. color: #FFF;
  439. // color: #724d2b;
  440. background: #00B760;
  441. // background: linear-gradient(121deg,#fde2c2 29.02%,#c19164 104.03%);
  442. border-radius: 90px;
  443. }
  444. }
  445. }
  446. .mr-1 {
  447. margin-right: 10px;
  448. }
  449. </style>