index.vue 12 KB

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