index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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_mock from '@/assets/svgs/pay/icon/mock.svg'
  131. defineOptions({ name: 'PayCashier' })
  132. const message = useMessage() // 消息弹窗
  133. const route = useRoute() // 路由
  134. const { push, currentRoute } = useRouter() // 路由
  135. const { delView } = useTagsViewStore() // 视图操作
  136. const id = ref(undefined) // 支付单号
  137. const returnUrl = ref<string | undefined>(undefined) // 支付完的回调地址
  138. const loading = ref(false) // 支付信息的 loading
  139. const payOrder = ref({}) // 支付信息
  140. const channelsAlipay = [
  141. {
  142. name: '支付宝 PC 网站支付',
  143. icon: svg_alipay_pc,
  144. code: 'alipay_pc'
  145. },
  146. {
  147. name: '支付宝 Wap 网站支付',
  148. icon: svg_alipay_wap,
  149. code: 'alipay_wap'
  150. },
  151. {
  152. name: '支付宝 App 网站支付',
  153. icon: svg_alipay_app,
  154. code: 'alipay_app'
  155. },
  156. {
  157. name: '支付宝扫码支付',
  158. icon: svg_alipay_qr,
  159. code: 'alipay_qr'
  160. },
  161. {
  162. name: '支付宝条码支付',
  163. icon: svg_alipay_bar,
  164. code: 'alipay_bar'
  165. }
  166. ]
  167. const channelsWechat = [
  168. {
  169. name: '微信公众号支付',
  170. icon: svg_wx_pub,
  171. code: 'wx_pub'
  172. },
  173. {
  174. name: '微信小程序支付',
  175. icon: svg_wx_lite,
  176. code: 'wx_lite'
  177. },
  178. {
  179. name: '微信 App 支付',
  180. icon: svg_wx_app,
  181. code: 'wx_app'
  182. },
  183. {
  184. name: '微信扫码支付',
  185. icon: svg_wx_native,
  186. code: 'wx_native'
  187. },
  188. {
  189. name: '微信条码支付',
  190. icon: svg_wx_bar,
  191. code: 'wx_bar'
  192. }
  193. ]
  194. const channelsMock = [
  195. {
  196. name: '模拟支付',
  197. icon: svg_mock,
  198. code: 'mock'
  199. }
  200. ]
  201. const submitLoading = ref(false) // 提交支付的 loading
  202. const interval = ref<any>(undefined) // 定时任务,轮询是否完成支付
  203. const qrCode = ref({
  204. // 展示形式:二维码
  205. url: '',
  206. title: '',
  207. visible: false
  208. })
  209. const barCode = ref({
  210. // 展示形式:条形码
  211. channelCode: '',
  212. value: '',
  213. title: '',
  214. visible: false
  215. })
  216. /** 获得支付信息 */
  217. const getDetail = async () => {
  218. // 1.1 未传递订单编号
  219. if (!id.value) {
  220. message.error('未传递支付单号,无法查看对应的支付信息')
  221. goReturnUrl('cancel')
  222. return
  223. }
  224. const data = await PayOrderApi.getOrder(id.value)
  225. payOrder.value = data
  226. // 1.2 无法查询到支付信息
  227. if (!data) {
  228. message.error('支付订单不存在,请检查!')
  229. goReturnUrl('cancel')
  230. return
  231. }
  232. // 1.3 如果已支付、或者已关闭,则直接跳转
  233. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  234. message.success('支付成功')
  235. goReturnUrl('success')
  236. return
  237. } else if (data.status === PayOrderStatusEnum.CLOSED.status) {
  238. message.error('无法支付,原因:订单已关闭')
  239. goReturnUrl('close')
  240. return
  241. }
  242. }
  243. /** 提交支付 */
  244. const submit = (channelCode) => {
  245. // 条形码支付,需要特殊处理
  246. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  247. barCode.value = {
  248. channelCode: channelCode,
  249. value: '',
  250. title: '“支付宝”条码支付',
  251. visible: true
  252. }
  253. return
  254. }
  255. if (channelCode === PayChannelEnum.WX_BAR.code) {
  256. barCode.value = {
  257. channelCode: channelCode,
  258. value: '',
  259. title: '“微信”条码支付',
  260. visible: true
  261. }
  262. return
  263. }
  264. // 微信公众号、小程序支付,无法在 PC 网页中进行
  265. if (channelCode === PayChannelEnum.WX_PUB.code) {
  266. message.error('微信公众号支付:不支持 PC 网站')
  267. return
  268. }
  269. if (channelCode === PayChannelEnum.WX_LITE.code) {
  270. message.error('微信小程序:不支持 PC 网站')
  271. return
  272. }
  273. // 默认的提交处理
  274. submit0(channelCode)
  275. }
  276. const submit0 = async (channelCode) => {
  277. submitLoading.value = true
  278. try {
  279. const formData = {
  280. id: id.value,
  281. channelCode: channelCode,
  282. returnUrl: location.href, // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
  283. ...buildSubmitParam(channelCode)
  284. }
  285. const data = await PayOrderApi.submitOrder(formData)
  286. // 直接返回已支付的情况,例如说扫码支付
  287. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  288. clearQueryInterval()
  289. message.success('支付成功!')
  290. goReturnUrl('success')
  291. return
  292. }
  293. // 展示对应的界面
  294. if (data.displayMode === PayDisplayModeEnum.URL.mode) {
  295. displayUrl(channelCode, data)
  296. } else if (data.displayMode === PayDisplayModeEnum.QR_CODE.mode) {
  297. displayQrCode(channelCode, data)
  298. } else if (data.displayMode === PayDisplayModeEnum.APP.mode) {
  299. displayApp(channelCode)
  300. }
  301. // 打开轮询任务
  302. createQueryInterval()
  303. } finally {
  304. submitLoading.value = false
  305. }
  306. }
  307. /** 构建提交支付的额外参数 */
  308. const buildSubmitParam = (channelCode) => {
  309. // ① 支付宝 BarCode 支付时,需要传递 authCode 条形码
  310. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  311. return {
  312. channelExtras: {
  313. auth_code: barCode.value.value
  314. }
  315. }
  316. }
  317. // ② 微信 BarCode 支付时,需要传递 authCode 条形码
  318. if (channelCode === PayChannelEnum.WX_BAR.code) {
  319. return {
  320. channelExtras: {
  321. authCode: barCode.value.value
  322. }
  323. }
  324. }
  325. return {}
  326. }
  327. /** 提交支付后,URL 的展示形式 */
  328. const displayUrl = (_channelCode, data) => {
  329. location.href = data.displayContent
  330. submitLoading.value = false
  331. }
  332. /** 提交支付后(扫码支付) */
  333. const displayQrCode = (channelCode, data) => {
  334. let title = '请使用手机浏览器“扫一扫”'
  335. if (channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  336. // 考虑到 WAP 测试,所以引导手机浏览器搞
  337. } else if (channelCode.indexOf('alipay_') === 0) {
  338. title = '请使用支付宝“扫一扫”扫码支付'
  339. } else if (channelCode.indexOf('wx_') === 0) {
  340. title = '请使用微信“扫一扫”扫码支付'
  341. }
  342. qrCode.value = {
  343. title: title,
  344. url: data.displayContent,
  345. visible: true
  346. }
  347. submitLoading.value = false
  348. }
  349. /** 提交支付后(App) */
  350. const displayApp = (channelCode) => {
  351. if (channelCode === PayChannelEnum.ALIPAY_APP.code) {
  352. message.error('支付宝 App 支付:无法在网页支付!')
  353. }
  354. if (channelCode === PayChannelEnum.WX_APP.code) {
  355. message.error('微信 App 支付:无法在网页支付!')
  356. }
  357. submitLoading.value = false
  358. }
  359. /** 轮询查询任务 */
  360. const createQueryInterval = () => {
  361. if (interval.value) {
  362. return
  363. }
  364. interval.value = setInterval(async () => {
  365. const data = await PayOrderApi.getOrder(id.value)
  366. // 已支付
  367. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  368. clearQueryInterval()
  369. message.success('支付成功!')
  370. goReturnUrl('success')
  371. }
  372. // 已取消
  373. if (data.status === PayOrderStatusEnum.CLOSED.status) {
  374. clearQueryInterval()
  375. message.error('支付已关闭!')
  376. goReturnUrl('close')
  377. }
  378. }, 1000 * 2)
  379. }
  380. /** 清空查询任务 */
  381. const clearQueryInterval = () => {
  382. // 清空各种弹窗
  383. qrCode.value = {
  384. title: '',
  385. url: '',
  386. visible: false
  387. }
  388. // 清空任务
  389. clearInterval(interval.value)
  390. interval.value = undefined
  391. }
  392. /**
  393. * 回到业务的 URL
  394. *
  395. * @param payResult 支付结果
  396. * ① success:支付成功
  397. * ② cancel:取消支付
  398. * ③ close:支付已关闭
  399. */
  400. const goReturnUrl = (payResult) => {
  401. // 清理任务
  402. clearQueryInterval()
  403. // 未配置的情况下,只能关闭
  404. if (!returnUrl.value) {
  405. delView(unref(currentRoute))
  406. return
  407. }
  408. const url =
  409. returnUrl.value.indexOf('?') >= 0
  410. ? returnUrl.value + '&payResult=' + payResult
  411. : returnUrl.value + '?payResult=' + payResult
  412. // 如果有配置,且是 http 开头,则浏览器跳转
  413. if (returnUrl.value.indexOf('http') === 0) {
  414. location.href = url
  415. } else {
  416. delView(unref(currentRoute))
  417. push({
  418. path: url
  419. })
  420. }
  421. }
  422. /** 初始化 */
  423. onMounted(() => {
  424. id.value = route.query.id
  425. if (route.query.returnUrl) {
  426. returnUrl.value = decodeURIComponent(route.query.returnUrl)
  427. }
  428. getDetail()
  429. })
  430. </script>
  431. <style lang="scss" scoped>
  432. .pay-channel-container {
  433. display: flex;
  434. margin-top: -10px;
  435. .box {
  436. width: 160px;
  437. padding-top: 10px;
  438. padding-bottom: 5px;
  439. margin-right: 10px;
  440. text-align: center;
  441. cursor: pointer;
  442. border: 1px solid #e6ebf5;
  443. img {
  444. width: 40px;
  445. height: 40px;
  446. }
  447. .title {
  448. padding-top: 5px;
  449. }
  450. }
  451. }
  452. </style>