doRecharge.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <div class="app-container">
  3. <el-tabs v-model="activeName" >
  4. <el-tab-pane label="在线充值" name="first">
  5. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  6. <!-- <el-form-item label="当前余额" >
  7. <span style="font-size:20px;color:red;">{{money}}</span>元
  8. </el-form-item>-->
  9. <el-form-item v-if="showRedPacket" label="红包余额" >
  10. <span style="font-size:20px;color:red;">{{redMoney}}</span>元
  11. </el-form-item>
  12. <el-form-item label="充值金额" prop="money">
  13. <el-input-number v-model="form.money" style="width:200px" placeholder="请输入充值金额" />
  14. </el-form-item>
  15. <el-form-item label="支付方式" prop="payType">
  16. <div class="radio">
  17. <div class="radio-item">
  18. <el-radio v-model="form.payType" :label=1><img class="radio-img" src="../../../assets/image/WeChat_icon30.png">微信</el-radio>
  19. </div>
  20. </div>
  21. </el-form-item>
  22. <div class="footer-btn">
  23. <el-button type="primary" @click="submitForm">充值</el-button>
  24. </div>
  25. </el-form>
  26. </el-tab-pane>
  27. </el-tabs>
  28. <el-dialog :title="payTitle" :visible.sync="payVisible" top="10vh" width="550px" class="my-dialog" @close="handleDialogClose">
  29. <div class="pay-dialog-cont">
  30. <div class="price">
  31. <span class="label">应付金额</span>
  32. <span class="unit">¥</span>
  33. <span class="num">{{form.money}}</span>
  34. </div>
  35. <div class="order-sn">订单编号:{{orderNo}}</div>
  36. <div class="code-box">
  37. <div class="qrcode" ref="qrCodeUrl"></div>
  38. </div>
  39. <div class="tip-box">
  40. <span>请使用<span class="orange">微信</span>扫一扫</span>
  41. <span>二维码完成支付</span>
  42. </div>
  43. </div>
  44. </el-dialog>
  45. </div>
  46. </template>
  47. <script>
  48. import { listCompanyRecharge, getCompanyRecharge, delCompanyRecharge, addCompanyRecharge, updateCompanyRecharge, exportCompanyRecharge } from "@/api/company/companyRecharge";
  49. import { getCompanyInfo } from "@/api/company/company";
  50. import { getConfigByKey } from '@/api//company/companyConfig'
  51. import {queryOrder, weixinPay, wxQrPay} from "@/api/company/pay";
  52. import QRCode from 'qrcodejs2'
  53. export default {
  54. name: "CompanyRecharge",
  55. data() {
  56. return {
  57. qrLoading: false,
  58. qrPayUrl: '',
  59. orderNo:"",
  60. showRedPacket: false,
  61. payVisible: false,
  62. payTitle: '微信支付',
  63. toPayFlag:true,
  64. activeName:"first",
  65. timer: null,
  66. money:0,
  67. redMoney:0,
  68. form: {
  69. payType:1,
  70. },
  71. rules: {
  72. money: [
  73. { required: true, message: "金额不能为空", trigger: "blur" },
  74. {
  75. validator: (rule, value, callback) => {
  76. if (value === '' || value === undefined || value === null) {
  77. callback();
  78. return;
  79. }
  80. const numValue = Number(value);
  81. if (isNaN(numValue)) {
  82. callback(new Error('请输入有效的金额'));
  83. return;
  84. }
  85. if (numValue < 0.01) {
  86. callback(new Error('最小充值金额为0.01元'));
  87. return;
  88. }
  89. if (numValue > 10000) {
  90. callback(new Error('最大充值金额为10000元'));
  91. return;
  92. }
  93. if (!/^\d+(\.\d{1,2})?$/.test(value.toString())) {
  94. callback(new Error('金额最多支持两位小数'));
  95. return;
  96. }
  97. callback();
  98. },
  99. trigger: 'blur'
  100. }
  101. ],
  102. }
  103. };
  104. },
  105. created() {
  106. this.getDicts("company_pay_status").then((response) => {
  107. this.statusOptions = response.data;
  108. });
  109. this.getDicts("company_pay_type").then((response) => {
  110. this.payTypeOptions = response.data;
  111. });
  112. getConfigByKey("course.config").then(response => {
  113. if(response.data && response.data.configValue) {
  114. const config = JSON.parse(response.data.configValue);
  115. if( config.isRedPackageBalanceDeduction && config.isRedPackageBalanceDeduction==1){
  116. this.showRedPacket=true
  117. }
  118. } else {
  119. this.showRedPacket = false;
  120. }
  121. });
  122. getCompanyInfo().then(response => {
  123. this.money = response.data.money;
  124. this.redMoney = response.data.redPackageMoney;
  125. });
  126. redMoney
  127. },
  128. beforeDestroy() {
  129. this.clearTimer();
  130. },
  131. methods: {
  132. clearTimer() {
  133. if (this.timer) {
  134. clearInterval(this.timer);
  135. this.timer = null;
  136. }
  137. },
  138. /**
  139. * 创建二维码
  140. * @param {string} url - 二维码内容URL
  141. */
  142. creatQrCode(url) {
  143. // 首先清空容器,避免重复创建
  144. if (this.$refs.qrCodeUrl) {
  145. this.$refs.qrCodeUrl.innerHTML = '';
  146. }
  147. // 使用nextTick确保DOM已更新
  148. this.$nextTick(() => {
  149. try {
  150. if (this.$refs.qrCodeUrl) {
  151. new QRCode(this.$refs.qrCodeUrl, {
  152. text: url, // 需要转换为二维码的内容
  153. width: 200,
  154. height: 200,
  155. colorDark: '#000000',
  156. colorLight: '#ffffff',
  157. correctLevel: QRCode.CorrectLevel.H
  158. });
  159. console.log('二维码创建成功');
  160. } else {
  161. console.error('二维码容器不存在');
  162. this.$message.error('二维码生成失败:容器不存在');
  163. }
  164. } catch (error) {
  165. console.error('二维码创建失败:', error);
  166. this.$message.error('二维码生成失败,请重试');
  167. }
  168. });
  169. },
  170. handleDialogClose(){
  171. clearInterval(this.timer);
  172. this.timer=null;
  173. },
  174. /** 提交按钮 */
  175. submitForm() {
  176. var that=this;
  177. this.$refs["form"].validate(valid => {
  178. if (valid) {
  179. that.toPayFlag=false;
  180. wxQrPay({
  181. amount: this.form.money
  182. }).then(res=>{
  183. if(res.code === 200) {
  184. this.payVisible = true;
  185. this.creatQrCode(res.data.qrLink);
  186. this.orderNo = res.data.orderNo;
  187. // 轮询订单是否完成
  188. this.timer = setInterval(()=>{
  189. queryOrder(res.data.orderNo).then(res=>{
  190. if(res.code === 200 && res.data.transactionId != null) {
  191. this.$message.success("支付成功!");
  192. clearInterval(this.timer);
  193. this.timer=null;
  194. setTimeout(()=>{
  195. window.location.reload()
  196. },2000)
  197. }
  198. })
  199. },1000)
  200. }
  201. })
  202. }
  203. });
  204. },
  205. }
  206. };
  207. </script>
  208. <style lang="scss" scoped>
  209. .el-radio{
  210. margin-right: 0px;
  211. }
  212. .radio{
  213. display: flex;
  214. align-items: center;
  215. }
  216. .radio-item{
  217. padding-right:15px;
  218. .radio-img{
  219. margin-right: 10px;
  220. }
  221. .el-radio{
  222. display: flex;
  223. align-items: center;
  224. .el-radio__label{
  225. display: flex;
  226. align-items: center;
  227. }
  228. }
  229. }
  230. .footer-btn{
  231. padding-top: 30px;
  232. border-top: 1px solid #eee;
  233. display: flex;
  234. justify-content: flex-end;
  235. }
  236. .pay-dialog-cont{
  237. padding: 5px 0 50px;
  238. display: flex;
  239. flex-direction: column;
  240. align-items: center;
  241. .price{
  242. display: flex;
  243. align-items: flex-end;
  244. .label{
  245. font-size: 14px;
  246. color: #666666;
  247. margin-right: 10px;
  248. line-height: 20px;
  249. }
  250. .unit{
  251. font-size: 16px;
  252. font-weight: bold;
  253. color: #FF6633;
  254. margin-right: 5px;
  255. line-height: 20px;
  256. }
  257. .num{
  258. font-size: 24px;
  259. font-weight: bold;
  260. color: #FF6633;
  261. }
  262. }
  263. .order-sn{
  264. font-size: 14px;
  265. color: #666666;
  266. line-height: 1;
  267. margin-top: 20px;
  268. }
  269. .code-box{
  270. width: 200px;
  271. height: 200px;
  272. margin-top: 40px;
  273. img{
  274. width: 100%;
  275. height: 100%;
  276. object-fit: cover;
  277. }
  278. }
  279. .tip-box{
  280. margin-top: 30px;
  281. display: flex;
  282. align-items: center;
  283. flex-direction: column;
  284. font-size: 14px;
  285. color: #333333;
  286. line-height: 22px;
  287. .orange{
  288. color: #FF6633;
  289. }
  290. }
  291. }
  292. </style>