index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.price / 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 channels" v-if="channel.code.indexOf('alipay_') === 0" :key="channel.code" @click="submit(channel.code)">
  21. <img :src="channel.icon">
  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 channels" v-if="channel.code.indexOf('wx_') === 0" :key="channel.code" @click="submit(channel.code)">
  29. <img :src="channel.icon">
  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 channels" :key="channel.code"
  37. v-if="channel.code.indexOf('alipay_') === -1 && channel.code.indexOf('wx_') === -1" @click="submit(channel.code)">
  38. <img :src="channel.icon">
  39. <div class="title">{{ channel.name }}</div>
  40. </div>
  41. </div>
  42. </el-card>
  43. <!-- 展示形式:二维码 URL -->
  44. <el-dialog :title="qrCode.title" :visible.sync="qrCode.visible" width="350px" append-to-body
  45. :close-on-press-escape="false">
  46. <qrcode-vue :value="qrCode.url" size="310" level="L" />
  47. </el-dialog>
  48. <!-- 展示形式:BarCode 条形码 -->
  49. <el-dialog :title="barCode.title" :visible.sync="barCode.visible" width="500px" append-to-body
  50. :close-on-press-escape="false">
  51. <el-form ref="form" label-width="80px">
  52. <el-row>
  53. <el-col :span="24">
  54. <el-form-item label="条形码" prop="name">
  55. <el-input v-model="barCode.value" placeholder="请输入条形码" required />
  56. </el-form-item>
  57. </el-col>
  58. <el-col :span="24">
  59. <div style="text-align: right">
  60. 或使用
  61. <el-link type="danger" target="_blank"
  62. href="https://baike.baidu.com/item/条码支付/10711903">(扫码枪/扫码盒)</el-link>
  63. 扫码
  64. </div>
  65. </el-col>
  66. </el-row>
  67. </el-form>
  68. <div slot="footer" class="dialog-footer">
  69. <el-button type="primary" @click="submit0(barCode.channelCode)"
  70. :disabled="barCode.value.length === 0">确认支付</el-button>
  71. <el-button @click="barCode.visible = false">取 消</el-button>
  72. </div>
  73. </el-dialog>
  74. </div>
  75. </template>
  76. <script>
  77. import QrcodeVue from 'qrcode.vue'
  78. import { getOrder, submitOrder } from '@/api/pay/order';
  79. import { PayChannelEnum, PayDisplayModeEnum, PayOrderStatusEnum } from "@/utils/constants";
  80. export default {
  81. name: "PayCashier",
  82. components: {
  83. QrcodeVue,
  84. },
  85. data() {
  86. return {
  87. id: undefined, // 请假编号
  88. returnUrl: undefined, // 支付完的回调地址
  89. loading: false, // 支付信息的 loading
  90. payOrder: {}, // 支付信息
  91. channels: [{
  92. name: '支付宝 PC 网站支付',
  93. icon: require("@/assets/images/pay/icon/alipay_pc.svg"),
  94. code: "alipay_pc"
  95. }, {
  96. name: '支付宝 Wap 网站支付',
  97. icon: require("@/assets/images/pay/icon/alipay_wap.svg"),
  98. code: "alipay_wap"
  99. }, {
  100. name: '支付宝 App 网站支付',
  101. icon: require("@/assets/images/pay/icon/alipay_app.svg"),
  102. code: "alipay_app"
  103. }, {
  104. name: '支付宝扫码支付',
  105. icon: require("@/assets/images/pay/icon/alipay_qr.svg"),
  106. code: "alipay_qr"
  107. }, {
  108. name: '支付宝条码支付',
  109. icon: require("@/assets/images/pay/icon/alipay_bar.svg"),
  110. code: "alipay_bar"
  111. }, {
  112. name: '微信公众号支付',
  113. icon: require("@/assets/images/pay/icon/wx_pub.svg"),
  114. code: "wx_pub"
  115. }, {
  116. name: '微信小程序支付',
  117. icon: require("@/assets/images/pay/icon/wx_lite.svg"),
  118. code: "wx_lite"
  119. }, {
  120. name: '微信 App 支付',
  121. icon: require("@/assets/images/pay/icon/wx_app.svg"),
  122. code: "wx_app"
  123. }, {
  124. name: '微信扫码支付',
  125. icon: require("@/assets/images/pay/icon/wx_native.svg"),
  126. code: "wx_native"
  127. }, {
  128. name: '微信条码支付',
  129. icon: require("@/assets/images/pay/icon/wx_bar.svg"),
  130. code: "wx_bar"
  131. }, {
  132. name: '模拟支付',
  133. icon: require("@/assets/images/pay/icon/mock.svg"),
  134. code: "mock"
  135. }],
  136. submitLoading: false, // 提交支付的 loading
  137. interval: undefined, // 定时任务,轮询是否完成支付
  138. qrCode: { // 展示形式:二维码
  139. url: '',
  140. title: '',
  141. visible: false,
  142. },
  143. barCode: { // 展示形式:条形码
  144. channelCode: '',
  145. value: '',
  146. title: '',
  147. visible: false,
  148. },
  149. };
  150. },
  151. created() {
  152. this.id = this.$route.query.id;
  153. if (this.$route.query.returnUrl) {
  154. this.returnUrl = decodeURIComponent(this.$route.query.returnUrl)
  155. }
  156. this.getDetail();
  157. },
  158. methods: {
  159. /** 获得支付信息 */
  160. getDetail() {
  161. // 1.1 未传递订单编号
  162. if (!this.id) {
  163. this.$message.error('未传递支付单号,无法查看对应的支付信息');
  164. this.goReturnUrl('cancel');
  165. return;
  166. }
  167. getOrder(this.id).then(response => {
  168. // 1.2 无法查询到支付信息
  169. if (!response.data) {
  170. this.$message.error('支付订单不存在,请检查!');
  171. this.goReturnUrl('cancel');
  172. return;
  173. }
  174. // 1.3 如果已支付、或者已关闭,则直接跳转
  175. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  176. this.$message.success('支付成功');
  177. this.goReturnUrl('success');
  178. return;
  179. } else if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  180. this.$message.error('无法支付,原因:订单已关闭');
  181. this.goReturnUrl('close');
  182. return;
  183. }
  184. // 2. 可以展示
  185. this.payOrder = response.data;
  186. });
  187. },
  188. /** 提交支付 */
  189. submit(channelCode) {
  190. // 条形码支付,需要特殊处理
  191. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  192. this.barCode = {
  193. channelCode: channelCode,
  194. value: '',
  195. title: '“支付宝”条码支付',
  196. visible: true
  197. }
  198. return;
  199. }
  200. if (channelCode === PayChannelEnum.WX_BAR.code) {
  201. this.barCode = {
  202. channelCode: channelCode,
  203. value: '',
  204. title: '“微信”条码支付',
  205. visible: true
  206. }
  207. return;
  208. }
  209. // 微信公众号、小程序支付,无法在 PC 网页中进行
  210. if (channelCode === PayChannelEnum.WX_PUB.code) {
  211. this.$message.error('微信公众号支付:不支持 PC 网站');
  212. return;
  213. }
  214. if (channelCode === PayChannelEnum.WX_LITE.code) {
  215. this.$message.error('微信小程序:不支持 PC 网站');
  216. return;
  217. }
  218. // 默认的提交处理
  219. this.submit0(channelCode)
  220. },
  221. submit0(channelCode) {
  222. this.submitLoading = true
  223. submitOrder({
  224. id: this.id,
  225. channelCode: channelCode,
  226. returnUrl: location.href, // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
  227. ...this.buildSubmitParam(channelCode)
  228. }).then(response => {
  229. // 直接返回已支付的情况,例如说扫码支付
  230. const data = response.data
  231. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  232. this.clearQueryInterval();
  233. this.$message.success('支付成功!');
  234. this.goReturnUrl();
  235. return
  236. }
  237. // 展示对应的界面
  238. if (data.displayMode === PayDisplayModeEnum.URL.mode) {
  239. this.displayUrl(channelCode, data)
  240. } else if (data.displayMode === PayDisplayModeEnum.QR_CODE.mode) {
  241. this.displayQrCode(channelCode, data)
  242. } else if (data.displayMode === PayDisplayModeEnum.APP.mode) {
  243. this.displayApp(channelCode, data)
  244. }
  245. // 打开轮询任务
  246. this.createQueryInterval()
  247. }).catch(() => {
  248. this.submitLoading = false
  249. });
  250. },
  251. /** 构建提交支付的额外参数 */
  252. buildSubmitParam(channelCode) {
  253. // ① 支付宝 BarCode 支付时,需要传递 authCode 条形码
  254. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  255. return {
  256. "channelExtras": {
  257. "auth_code": this.barCode.value
  258. }
  259. }
  260. }
  261. // ② 微信 BarCode 支付时,需要传递 authCode 条形码
  262. if (channelCode === PayChannelEnum.WX_BAR.code) {
  263. return {
  264. "channelExtras": {
  265. "authCode": this.barCode.value
  266. }
  267. }
  268. }
  269. return {}
  270. },
  271. /** 提交支付后,URL 的展示形式 */
  272. displayUrl(channelCode, data) {
  273. location.href = data.displayContent
  274. this.submitLoading = false
  275. },
  276. /** 提交支付后(扫码支付) */
  277. displayQrCode(channelCode, data) {
  278. let title = '请使用手机浏览器“扫一扫”';
  279. if (channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  280. // 考虑到 WAP 测试,所以引导手机浏览器搞
  281. } else if (channelCode.indexOf('alipay_') === 0) {
  282. title = '请使用支付宝“扫一扫”扫码支付';
  283. } else if (channelCode.indexOf('wx_') === 0) {
  284. title = '请使用微信“扫一扫”扫码支付';
  285. }
  286. this.qrCode = {
  287. title: title,
  288. url: data.displayContent,
  289. visible: true
  290. }
  291. this.submitLoading = false
  292. },
  293. /** 提交支付后(App) */
  294. displayApp(channelCode, data) {
  295. if (channelCode === PayChannelEnum.ALIPAY_APP.code) {
  296. this.$message.error('支付宝 App 支付:无法在网页支付!');
  297. }
  298. if (channelCode === PayChannelEnum.WX_APP.code) {
  299. this.$message.error('微信 App 支付:无法在网页支付!');
  300. }
  301. this.submitLoading = false
  302. },
  303. /** 轮询查询任务 */
  304. createQueryInterval() {
  305. if (this.interval) {
  306. return
  307. }
  308. this.interval = setInterval(() => {
  309. getOrder(this.id).then(response => {
  310. // 已支付
  311. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  312. this.clearQueryInterval();
  313. this.$message.success('支付成功!');
  314. this.goReturnUrl();
  315. }
  316. // 已取消
  317. if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  318. this.clearQueryInterval();
  319. this.$message.error('支付已关闭!');
  320. this.goReturnUrl();
  321. }
  322. })
  323. }, 1000 * 2)
  324. },
  325. /** 清空查询任务 */
  326. clearQueryInterval() {
  327. // 清空各种弹窗
  328. this.qrCode = {
  329. title: '',
  330. url: '',
  331. visible: false
  332. }
  333. // 清空任务
  334. clearInterval(this.interval)
  335. this.interval = undefined
  336. },
  337. /**
  338. * 回到业务的 URL
  339. *
  340. * @param payResult 支付结果
  341. * ① success:支付成功
  342. * ② cancel:取消支付
  343. * ③ close:支付已关闭
  344. */
  345. goReturnUrl(payResult) {
  346. // 清理任务
  347. this.clearQueryInterval();
  348. // 未配置的情况下,只能关闭
  349. if (!this.returnUrl) {
  350. this.$tab.closePage();
  351. return
  352. }
  353. const url = this.returnUrl.indexOf('?') >= 0
  354. ? this.returnUrl + '&payResult=' + payResult
  355. : this.returnUrl + '?payResult=' + payResult
  356. // 如果有配置,且是 http 开头,则浏览器跳转
  357. if (this.returnUrl.indexOf('http') === 0) {
  358. location.href = url;
  359. } else {
  360. this.$tab.closePage(() => {
  361. this.$router.push({
  362. path: url
  363. });
  364. });
  365. }
  366. }
  367. }
  368. };
  369. </script>
  370. <style lang="scss" scoped>
  371. .pay-channel-container {
  372. display: flex;
  373. margin-top: -10px;
  374. .box {
  375. width: 130px;
  376. border: 1px solid #e6ebf5;
  377. cursor: pointer;
  378. text-align: center;
  379. padding-top: 10px;
  380. padding-bottom: 5px;
  381. margin-right: 10px;
  382. img {
  383. width: 40px;
  384. height: 40px;
  385. }
  386. .title {
  387. padding-top: 5px
  388. }
  389. }
  390. }
  391. </style>