submit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <div class="app-container">
  3. <!-- 支付信息 -->
  4. <el-card v-loading="loading">
  5. <el-descriptions title="支付信息" :column="3" border>
  6. <el-descriptions-item label="支付单号">{{ payOrder.id }}</el-descriptions-item>
  7. <el-descriptions-item label="商品标题">{{ payOrder.subject }}</el-descriptions-item>
  8. <el-descriptions-item label="商品内容">{{ payOrder.body }}</el-descriptions-item>
  9. <el-descriptions-item label="支付金额">¥{{ (payOrder.amount / 100.0).toFixed(2) }}</el-descriptions-item>
  10. <el-descriptions-item label="创建时间">{{ parseTime(payOrder.createTime) }}</el-descriptions-item>
  11. <el-descriptions-item label="过期时间">{{ parseTime(payOrder.expireTime) }}</el-descriptions-item>
  12. </el-descriptions>
  13. </el-card>
  14. <!-- 支付选择框 -->
  15. <el-card style="margin-top: 10px" v-loading="submitLoading" element-loading-text="提交支付中...">
  16. <!-- 支付宝 -->
  17. <el-descriptions title="选择支付宝支付">
  18. </el-descriptions>
  19. <div class="pay-channel-container">
  20. <div class="box" v-for="channel in aliPayChannels" :key="channel.code" @click="submit(channel.code)">
  21. <img :src="icons[channel.code]">
  22. <div class="title">{{ channel.name }}</div>
  23. </div>
  24. </div>
  25. <!-- 微信支付 -->
  26. <el-descriptions title="选择微信支付" style="margin-top: 20px;" />
  27. <div class="pay-channel-container">
  28. <div class="box" v-for="channel in wxPayChannels" :key="channel.code">
  29. <img :src="icons[channel.code]">
  30. <div class="title">{{ channel.name }}</div>
  31. </div>
  32. </div>
  33. <!-- 其它支付 -->
  34. <el-descriptions title="选择其它支付" style="margin-top: 20px;" />
  35. <div class="pay-channel-container">
  36. <div class="box" v-for="channel in otherPayChannels" :key="channel.code">
  37. <img :src="icons[channel.code]">
  38. <div class="title">{{ channel.name }}</div>
  39. </div>
  40. </div>
  41. </el-card>
  42. <!-- 展示形式:二维码 URL -->
  43. <el-dialog :title="qrCode.title" :visible.sync="qrCode.visible" width="350px" append-to-body
  44. :close-on-press-escape="false">
  45. <qrcode-vue :value="qrCode.url" size="310" level="H" />
  46. </el-dialog>
  47. <!-- 展示形式:IFrame -->
  48. <el-dialog :title="iframe.title" :visible.sync="iframe.visible" width="800px" height="800px" append-to-body
  49. :close-on-press-escape="false">
  50. <iframe :src="iframe.url" width="100%" />
  51. </el-dialog>
  52. <!-- 展示形式: -->
  53. <div ref="formRef" v-html="form.value" />
  54. </div>
  55. </template>
  56. <script>
  57. import QrcodeVue from 'qrcode.vue'
  58. import { DICT_TYPE, getDictDatas } from "@/utils/dict";
  59. import { getOrder, submitOrder } from '@/api/pay/order';
  60. import {PayChannelEnum, PayDisplayModeEnum, PayOrderStatusEnum} from "@/utils/constants";
  61. export default {
  62. name: "PayOrderSubmit",
  63. components: {
  64. QrcodeVue,
  65. },
  66. data() {
  67. return {
  68. id: undefined, // 请假编号
  69. loading: false, // 支付信息的 loading
  70. payOrder: {}, // 支付信息
  71. aliPayChannels: [], // 阿里支付的渠道
  72. wxPayChannels: [], // 微信支付的渠道
  73. otherPayChannels: [], // 其它的支付渠道
  74. icons: {
  75. alipay_qr: require("@/assets/images/pay/icon/alipay_qr.svg"),
  76. alipay_app: require("@/assets/images/pay/icon/alipay_app.svg"),
  77. alipay_wap: require("@/assets/images/pay/icon/alipay_wap.svg"),
  78. alipay_pc: require("@/assets/images/pay/icon/alipay_pc.svg"),
  79. wx_app: require("@/assets/images/pay/icon/wx_app.svg"),
  80. wx_lite: require("@/assets/images/pay/icon/wx_lite.svg"),
  81. wx_pub: require("@/assets/images/pay/icon/wx_pub.svg"),
  82. mock: require("@/assets/images/pay/icon/mock.svg"),
  83. },
  84. submitLoading: false, // 提交支付的 loading
  85. interval: undefined, // 定时任务,轮询是否完成支付
  86. qrCode: { // 展示形式:二维码
  87. url: '',
  88. title: '',
  89. visible: false,
  90. },
  91. iframe: { // 展示形式:iframe
  92. url: '',
  93. title: '',
  94. visible: false
  95. },
  96. form: { // 展示形式:form
  97. html: '',
  98. },
  99. };
  100. },
  101. created() {
  102. this.id = this.$route.query.id;
  103. this.getDetail();
  104. this.initPayChannels();
  105. },
  106. methods: {
  107. /** 初始化支付渠道 */
  108. initPayChannels() {
  109. // 微信支付
  110. for (const dict of getDictDatas(DICT_TYPE.PAY_CHANNEL_CODE_TYPE)) {
  111. const payChannel = {
  112. name: dict.label,
  113. code: dict.value
  114. }
  115. if (dict.value.indexOf('wx_') === 0) {
  116. this.wxPayChannels.push(payChannel);
  117. } else if (dict.value.indexOf('alipay_') === 0) {
  118. this.aliPayChannels.push(payChannel);
  119. } else {
  120. this.otherPayChannels.push(payChannel);
  121. }
  122. }
  123. },
  124. /** 获得支付信息 */
  125. getDetail() {
  126. // 1.1 未传递订单编号
  127. if (!this.id) {
  128. this.$message.error('未传递支付单号,无法查看对应的支付信息');
  129. this.goBackToList();
  130. return;
  131. }
  132. getOrder(this.id).then(response => {
  133. // 1.2 无法查询到支付信息
  134. if (!response.data) {
  135. this.$message.error('支付订单不存在,请检查!');
  136. this.goBackToList();
  137. return;
  138. }
  139. // 1.3 订单已支付
  140. if (response.data.status !== PayOrderStatusEnum.WAITING.status) {
  141. this.$message.error('支付订单不处于待支付状态,请检查!');
  142. this.goBackToList();
  143. return;
  144. }
  145. // 2. 可以展示
  146. this.payOrder = response.data;
  147. });
  148. },
  149. /** 提交支付 */
  150. submit(channelCode) {
  151. this.submitLoading = true
  152. submitOrder({
  153. id: this.id,
  154. channelCode: channelCode,
  155. ...this.buildSubmitParam(channelCode)
  156. }).then(response => {
  157. const data = response.data
  158. if (data.displayMode === 'iframe') {
  159. this.displayIFrame(channelCode, data)
  160. } else if (data.displayMode === 'url') {
  161. this.displayUrl(channelCode, data)
  162. } else if (data.displayMode === 'form') {
  163. this.displayForm(channelCode, data)
  164. }
  165. // 不同的支付,调用不同的策略
  166. // if (channelCode === PayChannelEnum.ALIPAY_QR.code) {
  167. // this.submitAfterAlipayQr(invokeResponse)
  168. // } else if (channelCode === PayChannelEnum.ALIPAY_PC.code
  169. // || channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  170. // this.submitAfterAlipayPc(invokeResponse)
  171. // }
  172. // 打开轮询任务
  173. this.createQueryInterval()
  174. })
  175. },
  176. /** 构建提交支付的额外参数 */
  177. buildSubmitParam(channelCode) {
  178. // 支付宝网页支付时,有多种展示形态
  179. if (channelCode === PayChannelEnum.ALIPAY_PC.code) {
  180. // 情况【前置模式】:将二维码前置到商户的订单确认页的模式。需要商户在自己的页面中以 iframe 方式请求支付宝页面。具体支持的枚举值有以下几种:
  181. // 0:订单码-简约前置模式,对应 iframe 宽度不能小于 600px,高度不能小于 300px
  182. // return {
  183. // "channelExtras": {
  184. // "qr_pay_mode": "0"
  185. // }
  186. // }
  187. // 1:订单码-前置模式,对应iframe 宽度不能小于 300px,高度不能小于 600px
  188. // return {
  189. // "channelExtras": {
  190. // "qr_pay_mode": "1"
  191. // }
  192. // }
  193. // 3:订单码-迷你前置模式,对应 iframe 宽度不能小于 75px,高度不能小于 75px
  194. // return {
  195. // "channelExtras": {
  196. // "qr_pay_mode": "3"
  197. // }
  198. // }
  199. // 4:订单码-可定义宽度的嵌入式二维码,商户可根据需要设定二维码的大小
  200. // return {
  201. // "channelExtras": {
  202. // "qr_pay_mode": "4"
  203. // }
  204. // }
  205. // 情况【跳转模式】:跳转模式下,用户的扫码界面是由支付宝生成的,不在商户的域名下。支持传入的枚举值有
  206. return {
  207. "channelExtras": {
  208. "qr_pay_mode": "2"
  209. }
  210. }
  211. // 情况【表单模式】:直接提交当前页面到支付宝
  212. // return {
  213. // displayMode: PayDisplayModeEnum.FORM.mode
  214. // }
  215. }
  216. return {}
  217. },
  218. /** 提交支付后(支付宝扫码支付) */
  219. submitAfterAlipayQr(invokeResponse) {
  220. this.qrCode = {
  221. title: '请使用支付宝“扫一扫”扫码支付',
  222. url: invokeResponse.qrCode,
  223. visible: true
  224. }
  225. this.submitLoading = false
  226. },
  227. /** 提交支付后,IFrame 内置 URL 的展示形式 */
  228. displayIFrame(channelCode, data) {
  229. // TODO 芋艿:目前有点奇怪,支付宝总是会显示“支付环境存在风险”
  230. this.iframe = {
  231. title: '支付窗口',
  232. url: data.displayContent,
  233. visible: true
  234. }
  235. this.submitLoading = false
  236. },
  237. /** 提交支付后,URL 的展示形式 */
  238. displayUrl(channelCode, data) {
  239. window.open(data.displayContent)
  240. this.submitLoading = false
  241. },
  242. /** 提交支付后,Form 的展示形式 */
  243. displayForm(channelCode, data) {
  244. // 渲染支付页面
  245. this.form = {
  246. value: data.displayContent
  247. }
  248. // 防抖避免重复支付
  249. this.$nextTick(() => {
  250. // 提交支付表单
  251. this.$refs.formRef.children[0].submit();
  252. setTimeout(() => {
  253. this.submitLoading = false
  254. }, 1000);
  255. });
  256. },
  257. /** 轮询查询任务 */
  258. createQueryInterval() {
  259. if (!this.interval) {
  260. return
  261. }
  262. this.interval = setInterval(() => {
  263. getOrder(this.id).then(response => {
  264. // 已支付
  265. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  266. this.clearQueryInterval();
  267. this.$message.success('支付成功!');
  268. this.goBackToList();
  269. }
  270. // 已取消
  271. if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  272. this.clearQueryInterval();
  273. this.$message.error('支付已关闭!');
  274. this.goBackToList();
  275. }
  276. })
  277. }, 1000 * 2)
  278. },
  279. /** 清空查询任务 */
  280. clearQueryInterval() {
  281. // 清空各种弹窗
  282. this.qrCode = {
  283. title: '',
  284. url: '',
  285. visible: false
  286. }
  287. // 清空任务
  288. clearInterval(this.interval)
  289. this.interval = undefined
  290. },
  291. /** 回到列表 **/
  292. goBackToList() {
  293. this.$tab.closePage();
  294. this.$router.go(-1);
  295. }
  296. }
  297. };
  298. </script>
  299. <style lang="scss" scoped>
  300. .pay-channel-container {
  301. display: flex;
  302. margin-top: -10px;
  303. .box {
  304. width: 130px;
  305. border: 1px solid #e6ebf5;
  306. cursor: pointer;
  307. text-align: center;
  308. padding-top: 10px;
  309. padding-bottom: 5px;
  310. margin-right: 10px;
  311. img {
  312. width: 40px;
  313. height: 40px;
  314. }
  315. .title {
  316. padding-top: 5px
  317. }
  318. }
  319. }
  320. </style>