index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <template>
  2. <!-- 支付信息 -->
  3. <el-card v-loading="loading">
  4. <el-descriptions title="支付信息" :column="3" border>
  5. <el-descriptions-item label="支付单号">{{ payOrder.id }}</el-descriptions-item>
  6. <el-descriptions-item label="商品标题">{{ payOrder.subject }}</el-descriptions-item>
  7. <el-descriptions-item label="商品内容">{{ payOrder.body }}</el-descriptions-item>
  8. <el-descriptions-item label="支付金额">
  9. ¥{{ (payOrder.price / 100.0).toFixed(2) }}
  10. </el-descriptions-item>
  11. <el-descriptions-item label="创建时间">
  12. {{ formatDate(payOrder.createTime) }}
  13. </el-descriptions-item>
  14. <el-descriptions-item label="过期时间">
  15. {{ formatDate(payOrder.expireTime) }}
  16. </el-descriptions-item>
  17. </el-descriptions>
  18. </el-card>
  19. <!-- 支付选择框 -->
  20. <el-card style="margin-top: 10px" v-loading="submitLoading" element-loading-text="提交支付中...">
  21. <!-- 支付宝 -->
  22. <el-descriptions title="选择支付宝支付" />
  23. <div class="pay-channel-container">
  24. <div
  25. class="box"
  26. v-for="channel in channelsAlipay"
  27. :key="channel.code"
  28. @click="submit(channel.code)"
  29. >
  30. <img :src="channel.icon" />
  31. <div class="title">{{ channel.name }}</div>
  32. </div>
  33. </div>
  34. <!-- 微信支付 -->
  35. <el-descriptions title="选择微信支付" style="margin-top: 20px" />
  36. <div class="pay-channel-container">
  37. <div
  38. class="box"
  39. v-for="channel in channelsWechat"
  40. :key="channel.code"
  41. @click="submit(channel.code)"
  42. >
  43. <img :src="channel.icon" />
  44. <div class="title">{{ channel.name }}</div>
  45. </div>
  46. </div>
  47. <!-- 其它支付 -->
  48. <el-descriptions title="选择其它支付" style="margin-top: 20px" />
  49. <div class="pay-channel-container">
  50. <div
  51. class="box"
  52. v-for="channel in channelsMock"
  53. :key="channel.code"
  54. @click="submit(channel.code)"
  55. >
  56. <img :src="channel.icon" />
  57. <div class="title">{{ channel.name }}</div>
  58. </div>
  59. </div>
  60. </el-card>
  61. <!-- 展示形式:二维码 URL -->
  62. <Dialog
  63. :title="qrCode.title"
  64. v-model="qrCode.visible"
  65. width="350px"
  66. append-to-body
  67. :close-on-press-escape="false"
  68. >
  69. <Qrcode :text="qrCode.url" :width="310" />
  70. </Dialog>
  71. <!-- 展示形式:BarCode 条形码 -->
  72. <Dialog
  73. :title="barCode.title"
  74. v-model="barCode.visible"
  75. width="500px"
  76. append-to-body
  77. :close-on-press-escape="false"
  78. >
  79. <el-form ref="form" label-width="80px">
  80. <el-row>
  81. <el-col :span="24">
  82. <el-form-item label="条形码" prop="name">
  83. <el-input v-model="barCode.value" placeholder="请输入条形码" required />
  84. </el-form-item>
  85. </el-col>
  86. <el-col :span="24">
  87. <div style="text-align: right">
  88. 或使用
  89. <el-link
  90. type="danger"
  91. target="_blank"
  92. href="https://baike.baidu.com/item/条码支付/10711903"
  93. >
  94. (扫码枪/扫码盒)
  95. </el-link>
  96. 扫码
  97. </div>
  98. </el-col>
  99. </el-row>
  100. </el-form>
  101. <template #footer>
  102. <el-button
  103. type="primary"
  104. @click="submit0(barCode.channelCode)"
  105. :disabled="barCode.value.length === 0"
  106. >
  107. 确认支付
  108. </el-button>
  109. <el-button @click="barCode.visible = false">取 消</el-button>
  110. </template>
  111. </Dialog>
  112. </template>
  113. <script lang="ts" setup>
  114. import { Qrcode } from '@/components/Qrcode'
  115. import * as PayOrderApi from '@/api/pay/order'
  116. import { PayChannelEnum, PayDisplayModeEnum, PayOrderStatusEnum } from '@/utils/constants'
  117. import { formatDate } from '@/utils/formatTime'
  118. import { useTagsViewStore } from '@/store/modules/tagsView'
  119. // 导入图标
  120. import svg_alipay_pc from '@/assets/svgs/pay/icon/alipay_pc.svg'
  121. import svg_alipay_wap from '@/assets/svgs/pay/icon/alipay_wap.svg'
  122. import svg_alipay_app from '@/assets/svgs/pay/icon/alipay_app.svg'
  123. import svg_alipay_qr from '@/assets/svgs/pay/icon/alipay_qr.svg'
  124. import svg_alipay_bar from '@/assets/svgs/pay/icon/alipay_bar.svg'
  125. import svg_wx_pub from '@/assets/svgs/pay/icon/wx_pub.svg'
  126. import svg_wx_lite from '@/assets/svgs/pay/icon/wx_lite.svg'
  127. import svg_wx_app from '@/assets/svgs/pay/icon/wx_app.svg'
  128. import svg_wx_native from '@/assets/svgs/pay/icon/wx_native.svg'
  129. import svg_wx_bar from '@/assets/svgs/pay/icon/wx_bar.svg'
  130. import svg_wallet from '@/assets/svgs/pay/icon/wallet.svg'
  131. import svg_mock from '@/assets/svgs/pay/icon/mock.svg'
  132. defineOptions({ name: 'PayCashier' })
  133. const message = useMessage() // 消息弹窗
  134. const route = useRoute() // 路由
  135. const { push, currentRoute } = useRouter() // 路由
  136. const { delView } = useTagsViewStore() // 视图操作
  137. const id = ref(undefined) // 支付单号
  138. const returnUrl = ref<string | undefined>(undefined) // 支付完的回调地址
  139. const loading = ref(false) // 支付信息的 loading
  140. const payOrder = ref({}) // 支付信息
  141. const channelsAlipay = [
  142. {
  143. name: '支付宝 PC 网站支付',
  144. icon: svg_alipay_pc,
  145. code: 'alipay_pc'
  146. },
  147. {
  148. name: '支付宝 Wap 网站支付',
  149. icon: svg_alipay_wap,
  150. code: 'alipay_wap'
  151. },
  152. {
  153. name: '支付宝 App 网站支付',
  154. icon: svg_alipay_app,
  155. code: 'alipay_app'
  156. },
  157. {
  158. name: '支付宝扫码支付',
  159. icon: svg_alipay_qr,
  160. code: 'alipay_qr'
  161. },
  162. {
  163. name: '支付宝条码支付',
  164. icon: svg_alipay_bar,
  165. code: 'alipay_bar'
  166. }
  167. ]
  168. const channelsWechat = [
  169. {
  170. name: '微信公众号支付',
  171. icon: svg_wx_pub,
  172. code: 'wx_pub'
  173. },
  174. {
  175. name: '微信小程序支付',
  176. icon: svg_wx_lite,
  177. code: 'wx_lite'
  178. },
  179. {
  180. name: '微信 App 支付',
  181. icon: svg_wx_app,
  182. code: 'wx_app'
  183. },
  184. {
  185. name: '微信扫码支付',
  186. icon: svg_wx_native,
  187. code: 'wx_native'
  188. },
  189. {
  190. name: '微信条码支付',
  191. icon: svg_wx_bar,
  192. code: 'wx_bar'
  193. }
  194. ]
  195. const channelsMock = [
  196. {
  197. name: '钱包支付',
  198. icon: svg_wallet,
  199. code: 'wallet'
  200. },
  201. {
  202. name: '模拟支付',
  203. icon: svg_mock,
  204. code: 'mock'
  205. }
  206. ]
  207. const submitLoading = ref(false) // 提交支付的 loading
  208. const interval = ref<any>(undefined) // 定时任务,轮询是否完成支付
  209. const qrCode = ref({
  210. // 展示形式:二维码
  211. url: '',
  212. title: '',
  213. visible: false
  214. })
  215. const barCode = ref({
  216. // 展示形式:条形码
  217. channelCode: '',
  218. value: '',
  219. title: '',
  220. visible: false
  221. })
  222. /** 获得支付信息 */
  223. const getDetail = async () => {
  224. // 1.1 未传递订单编号
  225. if (!id.value) {
  226. message.error('未传递支付单号,无法查看对应的支付信息')
  227. goReturnUrl('cancel')
  228. return
  229. }
  230. const data = await PayOrderApi.getOrder(id.value, true)
  231. payOrder.value = data
  232. // 1.2 无法查询到支付信息
  233. if (!data) {
  234. message.error('支付订单不存在,请检查!')
  235. goReturnUrl('cancel')
  236. return
  237. }
  238. // 1.3 如果已支付、或者已关闭,则直接跳转
  239. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  240. message.success('支付成功')
  241. goReturnUrl('success')
  242. return
  243. } else if (data.status === PayOrderStatusEnum.CLOSED.status) {
  244. message.error('无法支付,原因:订单已关闭')
  245. goReturnUrl('close')
  246. return
  247. }
  248. }
  249. /** 提交支付 */
  250. const submit = (channelCode) => {
  251. // 条形码支付,需要特殊处理
  252. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  253. barCode.value = {
  254. channelCode: channelCode,
  255. value: '',
  256. title: '“支付宝”条码支付',
  257. visible: true
  258. }
  259. return
  260. }
  261. if (channelCode === PayChannelEnum.WX_BAR.code) {
  262. barCode.value = {
  263. channelCode: channelCode,
  264. value: '',
  265. title: '“微信”条码支付',
  266. visible: true
  267. }
  268. return
  269. }
  270. // 微信公众号、小程序支付,无法在 PC 网页中进行
  271. if (channelCode === PayChannelEnum.WX_PUB.code) {
  272. message.error('微信公众号支付:不支持 PC 网站')
  273. return
  274. }
  275. if (channelCode === PayChannelEnum.WX_LITE.code) {
  276. message.error('微信小程序:不支持 PC 网站')
  277. return
  278. }
  279. // 默认的提交处理
  280. submit0(channelCode)
  281. }
  282. const submit0 = async (channelCode) => {
  283. submitLoading.value = true
  284. try {
  285. const formData = {
  286. id: id.value,
  287. channelCode: channelCode,
  288. returnUrl: location.href, // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
  289. ...buildSubmitParam(channelCode)
  290. }
  291. const data = await PayOrderApi.submitOrder(formData)
  292. // 直接返回已支付的情况,例如说扫码支付
  293. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  294. clearQueryInterval()
  295. message.success('支付成功!')
  296. goReturnUrl('success')
  297. return
  298. }
  299. // 展示对应的界面
  300. if (data.displayMode === PayDisplayModeEnum.URL.mode) {
  301. displayUrl(channelCode, data)
  302. } else if (data.displayMode === PayDisplayModeEnum.QR_CODE.mode) {
  303. displayQrCode(channelCode, data)
  304. } else if (data.displayMode === PayDisplayModeEnum.APP.mode) {
  305. displayApp(channelCode)
  306. }
  307. // 打开轮询任务
  308. createQueryInterval()
  309. } finally {
  310. submitLoading.value = false
  311. }
  312. }
  313. /** 构建提交支付的额外参数 */
  314. const buildSubmitParam = (channelCode) => {
  315. // ① 支付宝 BarCode 支付时,需要传递 authCode 条形码
  316. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  317. return {
  318. channelExtras: {
  319. auth_code: barCode.value.value
  320. }
  321. }
  322. }
  323. // ② 微信 BarCode 支付时,需要传递 authCode 条形码
  324. if (channelCode === PayChannelEnum.WX_BAR.code) {
  325. return {
  326. channelExtras: {
  327. authCode: barCode.value.value
  328. }
  329. }
  330. }
  331. return {}
  332. }
  333. /** 提交支付后,URL 的展示形式 */
  334. const displayUrl = (_channelCode, data) => {
  335. location.href = data.displayContent
  336. submitLoading.value = false
  337. }
  338. /** 提交支付后(扫码支付) */
  339. const displayQrCode = (channelCode, data) => {
  340. let title = '请使用手机浏览器“扫一扫”'
  341. if (channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  342. // 考虑到 WAP 测试,所以引导手机浏览器搞
  343. } else if (channelCode.indexOf('alipay_') === 0) {
  344. title = '请使用支付宝“扫一扫”扫码支付'
  345. } else if (channelCode.indexOf('wx_') === 0) {
  346. title = '请使用微信“扫一扫”扫码支付'
  347. }
  348. qrCode.value = {
  349. title: title,
  350. url: data.displayContent,
  351. visible: true
  352. }
  353. submitLoading.value = false
  354. }
  355. /** 提交支付后(App) */
  356. const displayApp = (channelCode) => {
  357. if (channelCode === PayChannelEnum.ALIPAY_APP.code) {
  358. message.error('支付宝 App 支付:无法在网页支付!')
  359. }
  360. if (channelCode === PayChannelEnum.WX_APP.code) {
  361. message.error('微信 App 支付:无法在网页支付!')
  362. }
  363. submitLoading.value = false
  364. }
  365. /** 轮询查询任务 */
  366. const createQueryInterval = () => {
  367. if (interval.value) {
  368. return
  369. }
  370. interval.value = setInterval(async () => {
  371. const data = await PayOrderApi.getOrder(id.value)
  372. // 已支付
  373. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  374. clearQueryInterval()
  375. message.success('支付成功!')
  376. goReturnUrl('success')
  377. }
  378. // 已取消
  379. if (data.status === PayOrderStatusEnum.CLOSED.status) {
  380. clearQueryInterval()
  381. message.error('支付已关闭!')
  382. goReturnUrl('close')
  383. }
  384. }, 1000 * 2)
  385. }
  386. /** 清空查询任务 */
  387. const clearQueryInterval = () => {
  388. // 清空各种弹窗
  389. qrCode.value = {
  390. title: '',
  391. url: '',
  392. visible: false
  393. }
  394. // 清空任务
  395. clearInterval(interval.value)
  396. interval.value = undefined
  397. }
  398. /**
  399. * 回到业务的 URL
  400. *
  401. * @param payResult 支付结果
  402. * ① success:支付成功
  403. * ② cancel:取消支付
  404. * ③ close:支付已关闭
  405. */
  406. const goReturnUrl = (payResult) => {
  407. // 清理任务
  408. clearQueryInterval()
  409. // 未配置的情况下,只能关闭
  410. if (!returnUrl.value) {
  411. delView(unref(currentRoute))
  412. return
  413. }
  414. const url =
  415. returnUrl.value.indexOf('?') >= 0
  416. ? returnUrl.value + '&payResult=' + payResult
  417. : returnUrl.value + '?payResult=' + payResult
  418. // 如果有配置,且是 http 开头,则浏览器跳转
  419. if (returnUrl.value.indexOf('http') === 0) {
  420. location.href = url
  421. } else {
  422. delView(unref(currentRoute))
  423. push({
  424. path: url
  425. })
  426. }
  427. }
  428. /** 初始化 */
  429. onMounted(() => {
  430. id.value = route.query.id
  431. if (route.query.returnUrl) {
  432. returnUrl.value = decodeURIComponent(route.query.returnUrl)
  433. }
  434. getDetail()
  435. })
  436. </script>
  437. <style lang="scss" scoped>
  438. .pay-channel-container {
  439. display: flex;
  440. margin-top: -10px;
  441. .box {
  442. width: 160px;
  443. padding-top: 10px;
  444. padding-bottom: 5px;
  445. margin-right: 10px;
  446. text-align: center;
  447. cursor: pointer;
  448. border: 1px solid #e6ebf5;
  449. img {
  450. width: 40px;
  451. height: 40px;
  452. }
  453. .title {
  454. padding-top: 5px;
  455. }
  456. }
  457. }
  458. </style>