index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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">
  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">
  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 { DICT_TYPE, getDictDatas } from "@/utils/dict";
  79. import { getOrder, submitOrder } from '@/api/pay/order';
  80. import { PayChannelEnum, PayDisplayModeEnum, PayOrderStatusEnum } from "@/utils/constants";
  81. export default {
  82. name: "PayCashier",
  83. components: {
  84. QrcodeVue,
  85. },
  86. data() {
  87. return {
  88. id: undefined, // 请假编号
  89. returnUrl: undefined, // 支付完的回调地址
  90. loading: false, // 支付信息的 loading
  91. payOrder: {}, // 支付信息
  92. channels: [{
  93. name: '支付宝 PC 网站支付',
  94. icon: require("@/assets/images/pay/icon/alipay_pc.svg"),
  95. code: "alipay_pc"
  96. }, {
  97. name: '支付宝 Wap 网站支付',
  98. icon: require("@/assets/images/pay/icon/alipay_wap.svg"),
  99. code: "alipay_wap"
  100. }, {
  101. name: '支付宝 App 网站支付',
  102. icon: require("@/assets/images/pay/icon/alipay_app.svg"),
  103. code: "alipay_app"
  104. }, {
  105. name: '支付宝扫码支付',
  106. icon: require("@/assets/images/pay/icon/alipay_app.svg"),
  107. code: "alipay_qr"
  108. }, {
  109. name: '支付宝条码支付',
  110. icon: require("@/assets/images/pay/icon/alipay_bar.svg"),
  111. code: "alipay_bar"
  112. }, {
  113. name: '微信公众号支付',
  114. icon: require("@/assets/images/pay/icon/wx_pub.svg"),
  115. code: "wx_pub"
  116. }, {
  117. name: '微信小程序支付',
  118. icon: require("@/assets/images/pay/icon/wx_lite.svg"),
  119. code: "wx_lite"
  120. }, {
  121. name: '微信 App 支付',
  122. icon: require("@/assets/images/pay/icon/wx_app.svg"),
  123. code: "wx_app"
  124. }, {
  125. name: '模拟支付',
  126. icon: require("@/assets/images/pay/icon/mock.svg"),
  127. code: "mock"
  128. }],
  129. submitLoading: false, // 提交支付的 loading
  130. interval: undefined, // 定时任务,轮询是否完成支付
  131. qrCode: { // 展示形式:二维码
  132. url: '',
  133. title: '',
  134. visible: false,
  135. },
  136. barCode: { // 展示形式:条形码
  137. channelCode: '',
  138. value: '',
  139. title: '',
  140. visible: false,
  141. },
  142. };
  143. },
  144. created() {
  145. this.id = this.$route.query.id;
  146. if (this.$route.query.returnUrl) {
  147. this.returnUrl = decodeURIComponent(this.$route.query.returnUrl)
  148. }
  149. this.getDetail();
  150. },
  151. methods: {
  152. /** 获得支付信息 */
  153. getDetail() {
  154. // 1.1 未传递订单编号
  155. if (!this.id) {
  156. this.$message.error('未传递支付单号,无法查看对应的支付信息');
  157. this.goReturnUrl('cancel');
  158. return;
  159. }
  160. getOrder(this.id).then(response => {
  161. // 1.2 无法查询到支付信息
  162. if (!response.data) {
  163. this.$message.error('支付订单不存在,请检查!');
  164. this.goReturnUrl('cancel');
  165. return;
  166. }
  167. // 1.3 如果已支付、或者已关闭,则直接跳转
  168. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  169. this.$message.success('支付成功');
  170. this.goReturnUrl('success');
  171. return;
  172. } else if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  173. this.$message.error('无法支付,原因:订单已关闭');
  174. this.goReturnUrl('close');
  175. return;
  176. }
  177. // 2. 可以展示
  178. this.payOrder = response.data;
  179. });
  180. },
  181. /** 提交支付 */
  182. submit(channelCode) {
  183. // 条形码支付,需要特殊处理
  184. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  185. this.barCode = {
  186. channelCode: channelCode,
  187. value: '',
  188. title: '“支付宝”条码支付',
  189. visible: true
  190. }
  191. return;
  192. }
  193. // 默认的提交处理
  194. this.submit0(channelCode)
  195. },
  196. submit0(channelCode) {
  197. this.submitLoading = true
  198. submitOrder({
  199. id: this.id,
  200. channelCode: channelCode,
  201. returnUrl: location.href, // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
  202. ...this.buildSubmitParam(channelCode)
  203. }).then(response => {
  204. const data = response.data
  205. if (data.displayMode === PayDisplayModeEnum.URL.mode) {
  206. this.displayUrl(channelCode, data)
  207. } else if (data.displayMode === PayDisplayModeEnum.QR_CODE.mode) {
  208. this.displayQrCode(channelCode, data)
  209. }
  210. // 打开轮询任务
  211. this.createQueryInterval()
  212. }).catch(() => {
  213. this.submitLoading = false
  214. });
  215. },
  216. /** 构建提交支付的额外参数 */
  217. buildSubmitParam(channelCode) {
  218. // ① 支付宝 BarCode 支付时,需要传递 authCode 条形码
  219. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  220. return {
  221. "channelExtras": {
  222. "auth_code": this.barCode.value
  223. }
  224. }
  225. }
  226. return {}
  227. },
  228. /** 提交支付后,URL 的展示形式 */
  229. displayUrl(channelCode, data) {
  230. // window.open(data.displayContent)
  231. location.href = data.displayContent
  232. this.submitLoading = false
  233. },
  234. /** 提交支付后(支付宝扫码支付) */
  235. displayQrCode(channelCode, data) {
  236. let title = '请使用手机浏览器“扫一扫”';
  237. if (channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  238. // 考虑到 WAP 测试,所以引导手机浏览器搞
  239. } else if (channelCode.indexOf('alipay_') === 0) {
  240. title = '请使用支付宝“扫一扫”扫码支付';
  241. } else if (channelCode.indexOf('wx_') === 0) {
  242. title = '请使用微信“扫一扫”扫码支付';
  243. }
  244. this.qrCode = {
  245. title: title,
  246. url: data.displayContent,
  247. visible: true
  248. }
  249. this.submitLoading = false
  250. },
  251. /** 轮询查询任务 */
  252. createQueryInterval() {
  253. if (this.interval) {
  254. return
  255. }
  256. this.interval = setInterval(() => {
  257. getOrder(this.id).then(response => {
  258. // 已支付
  259. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  260. this.clearQueryInterval();
  261. this.$message.success('支付成功!');
  262. this.goReturnUrl();
  263. }
  264. // 已取消
  265. if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  266. this.clearQueryInterval();
  267. this.$message.error('支付已关闭!');
  268. this.goReturnUrl();
  269. }
  270. })
  271. }, 1000 * 2)
  272. },
  273. /** 清空查询任务 */
  274. clearQueryInterval() {
  275. // 清空各种弹窗
  276. this.qrCode = {
  277. title: '',
  278. url: '',
  279. visible: false
  280. }
  281. // 清空任务
  282. clearInterval(this.interval)
  283. this.interval = undefined
  284. },
  285. /**
  286. * 回到业务的 URL
  287. *
  288. * @param payResult 支付结果
  289. * ① success:支付成功
  290. * ② cancel:取消支付
  291. * ③ close:支付已关闭
  292. */
  293. goReturnUrl(payResult) {
  294. // 未配置的情况下,只能关闭
  295. if (!this.returnUrl) {
  296. this.$tab.closePage();
  297. return
  298. }
  299. const url = this.returnUrl.indexOf('?') >= 0
  300. ? this.returnUrl + '&payResult=' + payResult
  301. : this.returnUrl + '?payResult=' + payResult
  302. // 如果有配置,且是 http 开头,则浏览器跳转
  303. if (this.returnUrl.indexOf('http') === 0) {
  304. location.href = url;
  305. } else {
  306. this.$tab.closePage(() => {
  307. this.$router.push({
  308. path: url
  309. });
  310. });
  311. }
  312. }
  313. }
  314. };
  315. </script>
  316. <style lang="scss" scoped>
  317. .pay-channel-container {
  318. display: flex;
  319. margin-top: -10px;
  320. .box {
  321. width: 130px;
  322. border: 1px solid #e6ebf5;
  323. cursor: pointer;
  324. text-align: center;
  325. padding-top: 10px;
  326. padding-bottom: 5px;
  327. margin-right: 10px;
  328. img {
  329. width: 40px;
  330. height: 40px;
  331. }
  332. .title {
  333. padding-top: 5px
  334. }
  335. }
  336. }
  337. </style>