index.vue 13 KB

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