index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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">
  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/wx_native.svg"),
  127. code: "wx_native"
  128. }, {
  129. name: '微信条码支付',
  130. icon: require("@/assets/images/pay/icon/wx_bar.svg"),
  131. code: "wx_bar"
  132. }, {
  133. name: '模拟支付',
  134. icon: require("@/assets/images/pay/icon/mock.svg"),
  135. code: "mock"
  136. }],
  137. submitLoading: false, // 提交支付的 loading
  138. interval: undefined, // 定时任务,轮询是否完成支付
  139. qrCode: { // 展示形式:二维码
  140. url: '',
  141. title: '',
  142. visible: false,
  143. },
  144. barCode: { // 展示形式:条形码
  145. channelCode: '',
  146. value: '',
  147. title: '',
  148. visible: false,
  149. },
  150. };
  151. },
  152. created() {
  153. this.id = this.$route.query.id;
  154. if (this.$route.query.returnUrl) {
  155. this.returnUrl = decodeURIComponent(this.$route.query.returnUrl)
  156. }
  157. this.getDetail();
  158. },
  159. methods: {
  160. /** 获得支付信息 */
  161. getDetail() {
  162. // 1.1 未传递订单编号
  163. if (!this.id) {
  164. this.$message.error('未传递支付单号,无法查看对应的支付信息');
  165. this.goReturnUrl('cancel');
  166. return;
  167. }
  168. getOrder(this.id).then(response => {
  169. // 1.2 无法查询到支付信息
  170. if (!response.data) {
  171. this.$message.error('支付订单不存在,请检查!');
  172. this.goReturnUrl('cancel');
  173. return;
  174. }
  175. // 1.3 如果已支付、或者已关闭,则直接跳转
  176. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  177. this.$message.success('支付成功');
  178. this.goReturnUrl('success');
  179. return;
  180. } else if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  181. this.$message.error('无法支付,原因:订单已关闭');
  182. this.goReturnUrl('close');
  183. return;
  184. }
  185. // 2. 可以展示
  186. this.payOrder = response.data;
  187. });
  188. },
  189. /** 提交支付 */
  190. submit(channelCode) {
  191. // 条形码支付,需要特殊处理
  192. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  193. this.barCode = {
  194. channelCode: channelCode,
  195. value: '',
  196. title: '“支付宝”条码支付',
  197. visible: true
  198. }
  199. return;
  200. }
  201. if (channelCode === PayChannelEnum.WX_BAR.code) {
  202. this.barCode = {
  203. channelCode: channelCode,
  204. value: '',
  205. title: '“微信”条码支付',
  206. visible: true
  207. }
  208. return;
  209. }
  210. // 默认的提交处理
  211. this.submit0(channelCode)
  212. },
  213. submit0(channelCode) {
  214. this.submitLoading = true
  215. submitOrder({
  216. id: this.id,
  217. channelCode: channelCode,
  218. returnUrl: location.href, // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
  219. ...this.buildSubmitParam(channelCode)
  220. }).then(response => {
  221. // 直接返回已支付的情况,例如说扫码支付
  222. const data = response.data
  223. if (data.status === PayOrderStatusEnum.SUCCESS.status) {
  224. this.clearQueryInterval();
  225. this.$message.success('支付成功!');
  226. this.goReturnUrl();
  227. return
  228. }
  229. // 展示对应的界面
  230. if (data.displayMode === PayDisplayModeEnum.URL.mode) {
  231. this.displayUrl(channelCode, data)
  232. } else if (data.displayMode === PayDisplayModeEnum.QR_CODE.mode) {
  233. this.displayQrCode(channelCode, data)
  234. }
  235. // 打开轮询任务
  236. this.createQueryInterval()
  237. }).catch(() => {
  238. this.submitLoading = false
  239. });
  240. },
  241. /** 构建提交支付的额外参数 */
  242. buildSubmitParam(channelCode) {
  243. // ① 支付宝 BarCode 支付时,需要传递 authCode 条形码
  244. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  245. return {
  246. "channelExtras": {
  247. "auth_code": this.barCode.value
  248. }
  249. }
  250. }
  251. // ② 微信 BarCode 支付时,需要传递 authCode 条形码
  252. if (channelCode === PayChannelEnum.WX_BAR.code) {
  253. return {
  254. "channelExtras": {
  255. "authCode": this.barCode.value
  256. }
  257. }
  258. }
  259. return {}
  260. },
  261. /** 提交支付后,URL 的展示形式 */
  262. displayUrl(channelCode, data) {
  263. location.href = data.displayContent
  264. this.submitLoading = false
  265. },
  266. /** 提交支付后(支付宝扫码支付) */
  267. displayQrCode(channelCode, data) {
  268. let title = '请使用手机浏览器“扫一扫”';
  269. if (channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  270. // 考虑到 WAP 测试,所以引导手机浏览器搞
  271. } else if (channelCode.indexOf('alipay_') === 0) {
  272. title = '请使用支付宝“扫一扫”扫码支付';
  273. } else if (channelCode.indexOf('wx_') === 0) {
  274. title = '请使用微信“扫一扫”扫码支付';
  275. }
  276. this.qrCode = {
  277. title: title,
  278. url: data.displayContent,
  279. visible: true
  280. }
  281. this.submitLoading = false
  282. },
  283. /** 轮询查询任务 */
  284. createQueryInterval() {
  285. if (this.interval) {
  286. return
  287. }
  288. this.interval = setInterval(() => {
  289. getOrder(this.id).then(response => {
  290. // 已支付
  291. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  292. this.clearQueryInterval();
  293. this.$message.success('支付成功!');
  294. this.goReturnUrl();
  295. }
  296. // 已取消
  297. if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  298. this.clearQueryInterval();
  299. this.$message.error('支付已关闭!');
  300. this.goReturnUrl();
  301. }
  302. })
  303. }, 1000 * 2)
  304. },
  305. /** 清空查询任务 */
  306. clearQueryInterval() {
  307. // 清空各种弹窗
  308. this.qrCode = {
  309. title: '',
  310. url: '',
  311. visible: false
  312. }
  313. // 清空任务
  314. clearInterval(this.interval)
  315. this.interval = undefined
  316. },
  317. /**
  318. * 回到业务的 URL
  319. *
  320. * @param payResult 支付结果
  321. * ① success:支付成功
  322. * ② cancel:取消支付
  323. * ③ close:支付已关闭
  324. */
  325. goReturnUrl(payResult) {
  326. // 清理任务
  327. this.clearQueryInterval();
  328. // 未配置的情况下,只能关闭
  329. if (!this.returnUrl) {
  330. this.$tab.closePage();
  331. return
  332. }
  333. const url = this.returnUrl.indexOf('?') >= 0
  334. ? this.returnUrl + '&payResult=' + payResult
  335. : this.returnUrl + '?payResult=' + payResult
  336. // 如果有配置,且是 http 开头,则浏览器跳转
  337. if (this.returnUrl.indexOf('http') === 0) {
  338. location.href = url;
  339. } else {
  340. this.$tab.closePage(() => {
  341. this.$router.push({
  342. path: url
  343. });
  344. });
  345. }
  346. }
  347. }
  348. };
  349. </script>
  350. <style lang="scss" scoped>
  351. .pay-channel-container {
  352. display: flex;
  353. margin-top: -10px;
  354. .box {
  355. width: 130px;
  356. border: 1px solid #e6ebf5;
  357. cursor: pointer;
  358. text-align: center;
  359. padding-top: 10px;
  360. padding-bottom: 5px;
  361. margin-right: 10px;
  362. img {
  363. width: 40px;
  364. height: 40px;
  365. }
  366. .title {
  367. padding-top: 5px
  368. }
  369. }
  370. }
  371. </style>