index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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="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 channels" :key="channel.code"
  37. v-if="channel.code.indexOf('alipay_') === -1 && channel.code.indexOf('wx_') === -1">
  38. <img :src="icons[channel.code]">
  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. <!-- 展示形式:IFrame -->
  49. <el-dialog :title="iframe.title" :visible.sync="iframe.visible" width="800px" height="800px" append-to-body
  50. :close-on-press-escape="false">
  51. <iframe :src="iframe.url" width="100%" />
  52. </el-dialog>
  53. <!-- 展示形式:Form -->
  54. <div ref="formRef" v-html="form.value" />
  55. <!-- 展示形式:BarCode 条形码 -->
  56. <el-dialog :title="barCode.title" :visible.sync="barCode.visible" width="500px" append-to-body
  57. :close-on-press-escape="false">
  58. <el-form ref="form" label-width="80px">
  59. <el-row>
  60. <el-col :span="24">
  61. <el-form-item label="条形码" prop="name">
  62. <el-input v-model="barCode.value" placeholder="请输入条形码" required />
  63. </el-form-item>
  64. </el-col>
  65. <el-col :span="24">
  66. <div style="text-align: right">
  67. 或使用
  68. <el-link type="danger" target="_blank"
  69. href="https://baike.baidu.com/item/条码支付/10711903">(扫码枪/扫码盒)</el-link>
  70. 扫码
  71. </div>
  72. </el-col>
  73. </el-row>
  74. </el-form>
  75. <div slot="footer" class="dialog-footer">
  76. <el-button type="primary" @click="submit0(barCode.channelCode)"
  77. :disabled="barCode.value.length === 0">确认支付</el-button>
  78. <el-button @click="barCode.visible = false">取 消</el-button>
  79. </div>
  80. </el-dialog>
  81. </div>
  82. </template>
  83. <script>
  84. import QrcodeVue from 'qrcode.vue'
  85. import { DICT_TYPE, getDictDatas } from "@/utils/dict";
  86. import { getOrder, submitOrder } from '@/api/pay/order';
  87. import { PayChannelEnum, PayDisplayModeEnum, PayOrderStatusEnum } from "@/utils/constants";
  88. export default {
  89. name: "PayCashier",
  90. components: {
  91. QrcodeVue,
  92. },
  93. data() {
  94. return {
  95. id: undefined, // 请假编号
  96. returnUrl: undefined, // 支付完的回调地址
  97. loading: false, // 支付信息的 loading
  98. payOrder: {}, // 支付信息
  99. channels: [{
  100. name: '支付宝 PC 网站支付',
  101. icon: require("@/assets/images/pay/icon/alipay_pc.svg"),
  102. code: "alipay_pc"
  103. }, {
  104. name: '支付宝 Wap 网站支付',
  105. icon: require("@/assets/images/pay/icon/alipay_wap.svg"),
  106. code: "alipay_wap"
  107. }, {
  108. name: '支付宝 App 网站支付',
  109. icon: require("@/assets/images/pay/icon/alipay_app.svg"),
  110. code: "alipay_app"
  111. }, {
  112. name: '支付宝扫码支付',
  113. icon: require("@/assets/images/pay/icon/alipay_app.svg"),
  114. code: "alipay_qr"
  115. }, {
  116. name: '支付宝条码支付',
  117. icon: require("@/assets/images/pay/icon/alipay_bar.svg"),
  118. code: "alipay_bar"
  119. }, {
  120. name: '微信公众号支付',
  121. icon: require("@/assets/images/pay/icon/wx_pub.svg"),
  122. code: "wx_pub"
  123. }, {
  124. name: '微信小程序支付',
  125. icon: require("@/assets/images/pay/icon/wx_lite.svg"),
  126. code: "wx_lite"
  127. }, {
  128. name: '微信 App 支付',
  129. icon: require("@/assets/images/pay/icon/wx_lite.svg"),
  130. code: "wx_app"
  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. iframe: { // 展示形式:iframe
  144. url: '',
  145. title: '',
  146. visible: false
  147. },
  148. form: { // 展示形式:form
  149. html: '',
  150. },
  151. barCode: { // 展示形式:条形码
  152. channelCode: '',
  153. value: '',
  154. title: '',
  155. visible: false,
  156. },
  157. };
  158. },
  159. created() {
  160. this.id = this.$route.query.id;
  161. if (this.$route.query.returnUrl) {
  162. this.returnUrl = decodeURIComponent(this.$route.query.returnUrl)
  163. }
  164. this.getDetail();
  165. },
  166. methods: {
  167. /** 获得支付信息 */
  168. getDetail() {
  169. // 1.1 未传递订单编号
  170. if (!this.id) {
  171. this.$message.error('未传递支付单号,无法查看对应的支付信息');
  172. this.goBackToList();
  173. return;
  174. }
  175. getOrder(this.id).then(response => {
  176. // 1.2 无法查询到支付信息
  177. if (!response.data) {
  178. this.$message.error('支付订单不存在,请检查!');
  179. this.goBackToList();
  180. return;
  181. }
  182. // 1.3 订单已支付
  183. // TODO 芋艿:已支付
  184. if (response.data.status !== PayOrderStatusEnum.WAITING.status) {
  185. this.$message.error('支付订单不处于待支付状态,请检查!');
  186. this.goBackToList();
  187. return;
  188. }
  189. // 2. 可以展示
  190. this.payOrder = response.data;
  191. });
  192. },
  193. /** 提交支付 */
  194. submit(channelCode) {
  195. // 条形码支付,需要特殊处理
  196. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  197. this.barCode = {
  198. channelCode: channelCode,
  199. value: '',
  200. title: '“支付宝”条码支付',
  201. visible: true
  202. }
  203. return;
  204. }
  205. // 默认的提交处理
  206. this.submit0(channelCode)
  207. },
  208. submit0(channelCode) {
  209. this.submitLoading = true
  210. submitOrder({
  211. id: this.id,
  212. channelCode: channelCode,
  213. ...this.buildSubmitParam(channelCode)
  214. }).then(response => {
  215. const data = response.data
  216. if (data.displayMode === PayDisplayModeEnum.IFRAME.mode) {
  217. this.displayIFrame(channelCode, data)
  218. } else if (data.displayMode === PayDisplayModeEnum.URL.mode) {
  219. this.displayUrl(channelCode, data)
  220. } else if (data.displayMode === PayDisplayModeEnum.FORM.mode) {
  221. this.displayForm(channelCode, data)
  222. } else if (data.displayMode === PayDisplayModeEnum.QR_CODE.mode) {
  223. this.displayQrCode(channelCode, data)
  224. }
  225. // 打开轮询任务
  226. this.createQueryInterval()
  227. }).catch(() => {
  228. this.submitLoading = false
  229. });
  230. },
  231. /** 构建提交支付的额外参数 */
  232. buildSubmitParam(channelCode) {
  233. // ① 支付宝 PC 支付时,有多种展示形态
  234. if (channelCode === PayChannelEnum.ALIPAY_PC.code) {
  235. // 情况【前置模式】:将二维码前置到商户的订单确认页的模式。需要商户在自己的页面中以 iframe 方式请求支付宝页面。具体支持的枚举值有以下几种:
  236. // 0:订单码-简约前置模式,对应 iframe 宽度不能小于 600px,高度不能小于 300px
  237. // return {
  238. // "channelExtras": {
  239. // "qr_pay_mode": "0"
  240. // }
  241. // }
  242. // 1:订单码-前置模式,对应iframe 宽度不能小于 300px,高度不能小于 600px
  243. // return {
  244. // "channelExtras": {
  245. // "qr_pay_mode": "1"
  246. // }
  247. // }
  248. // 3:订单码-迷你前置模式,对应 iframe 宽度不能小于 75px,高度不能小于 75px
  249. // return {
  250. // "channelExtras": {
  251. // "qr_pay_mode": "3"
  252. // }
  253. // }
  254. // 4:订单码-可定义宽度的嵌入式二维码,商户可根据需要设定二维码的大小
  255. // return {
  256. // "channelExtras": {
  257. // "qr_pay_mode": "4"
  258. // }
  259. // }
  260. // 情况【跳转模式】:跳转模式下,用户的扫码界面是由支付宝生成的,不在商户的域名下。支持传入的枚举值有
  261. return {
  262. "channelExtras": {
  263. "qr_pay_mode": "2"
  264. }
  265. }
  266. // 情况【表单模式】:直接提交当前页面到支付宝
  267. // return {
  268. // displayMode: PayDisplayModeEnum.FORM.mode
  269. // }
  270. }
  271. // ② 支付宝 Wap 支付时,引导手机扫码支付
  272. if (channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  273. return {
  274. displayMode: PayDisplayModeEnum.QR_CODE.mode
  275. }
  276. }
  277. // ③ 支付宝 BarCode 支付时,需要传递 authCode 条形码
  278. if (channelCode === PayChannelEnum.ALIPAY_BAR.code) {
  279. return {
  280. "channelExtras": {
  281. "auth_code": this.barCode.value
  282. }
  283. }
  284. }
  285. return {}
  286. },
  287. /** 提交支付后,IFrame 内置 URL 的展示形式 */
  288. displayIFrame(channelCode, data) {
  289. // TODO 芋艿:目前有点奇怪,支付宝总是会显示“支付环境存在风险”
  290. this.iframe = {
  291. title: '支付窗口',
  292. url: data.displayContent,
  293. visible: true
  294. }
  295. this.submitLoading = false
  296. },
  297. /** 提交支付后,URL 的展示形式 */
  298. displayUrl(channelCode, data) {
  299. // window.open(data.displayContent)window
  300. location.href = data.displayContent
  301. this.submitLoading = false
  302. },
  303. /** 提交支付后,Form 的展示形式 */
  304. displayForm(channelCode, data) {
  305. // 渲染支付页面
  306. this.form = {
  307. value: data.displayContent
  308. }
  309. // 防抖避免重复支付
  310. this.$nextTick(() => {
  311. // 提交支付表单
  312. this.$refs.formRef.children[0].submit();
  313. setTimeout(() => {
  314. this.submitLoading = false
  315. }, 1000);
  316. });
  317. },
  318. /** 提交支付后(支付宝扫码支付) */
  319. displayQrCode(channelCode, data) {
  320. let title = '请使用手机浏览器“扫一扫”';
  321. if (channelCode === PayChannelEnum.ALIPAY_WAP.code) {
  322. // 考虑到 WAP 测试,所以引导手机浏览器搞
  323. } else if (channelCode.indexOf('alipay_') === 0) {
  324. title = '请使用支付宝“扫一扫”扫码支付';
  325. } else if (channelCode.indexOf('wx_') === 0) {
  326. title = '请使用微信“扫一扫”扫码支付';
  327. }
  328. this.qrCode = {
  329. title: title,
  330. url: data.displayContent,
  331. visible: true
  332. }
  333. this.submitLoading = false
  334. },
  335. /** 轮询查询任务 */
  336. createQueryInterval() {
  337. if (this.interval) {
  338. return
  339. }
  340. this.interval = setInterval(() => {
  341. getOrder(this.id).then(response => {
  342. // 已支付
  343. if (response.data.status === PayOrderStatusEnum.SUCCESS.status) {
  344. this.clearQueryInterval();
  345. this.$message.success('支付成功!');
  346. this.goBackToList();
  347. }
  348. // 已取消
  349. if (response.data.status === PayOrderStatusEnum.CLOSED.status) {
  350. this.clearQueryInterval();
  351. this.$message.error('支付已关闭!');
  352. this.goBackToList();
  353. }
  354. })
  355. }, 1000 * 2)
  356. },
  357. /** 清空查询任务 */
  358. clearQueryInterval() {
  359. // 清空各种弹窗
  360. this.qrCode = {
  361. title: '',
  362. url: '',
  363. visible: false
  364. }
  365. // 清空任务
  366. clearInterval(this.interval)
  367. this.interval = undefined
  368. },
  369. /** 回到列表 **/
  370. goBackToList() {
  371. this.$tab.closePage();
  372. this.$router.go(-1);
  373. // TODO 芋艿:需要优化;
  374. // this.$router.push({
  375. // path: this.returnUrl
  376. // });
  377. }
  378. }
  379. };
  380. </script>
  381. <style lang="scss" scoped>
  382. .pay-channel-container {
  383. display: flex;
  384. margin-top: -10px;
  385. .box {
  386. width: 130px;
  387. border: 1px solid #e6ebf5;
  388. cursor: pointer;
  389. text-align: center;
  390. padding-top: 10px;
  391. padding-bottom: 5px;
  392. margin-right: 10px;
  393. img {
  394. width: 40px;
  395. height: 40px;
  396. }
  397. .title {
  398. padding-top: 5px
  399. }
  400. }
  401. }
  402. </style>