|
@@ -0,0 +1,205 @@
|
|
|
+import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import { AlertController, IonicModule, ToastController } from '@ionic/angular';
|
|
|
+import { ActivatedRoute } from '@angular/router';
|
|
|
+import { AccountService } from '../../../services/account.service';
|
|
|
+import { HttpClient } from '@angular/common/http';
|
|
|
+declare var wx: any;
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-pay-comp',
|
|
|
+ templateUrl: './pay-comp.component.html',
|
|
|
+ styleUrls: ['./pay-comp.component.scss'],
|
|
|
+ standalone: true,
|
|
|
+ imports: [IonicModule, CommonModule],
|
|
|
+})
|
|
|
+export class PayCompComponent implements OnInit {
|
|
|
+ @Input('price') price!: number;
|
|
|
+ @Input('orderNum') orderNum: string = 'C20241215050401';
|
|
|
+ @Output() onChange: EventEmitter<any> = new EventEmitter<any>();
|
|
|
+ tradeNo?: string;
|
|
|
+ isOpen: boolean = false;
|
|
|
+ checkpay: string = 'wxpay';
|
|
|
+ userAgent?: string;
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ private accServ: AccountService,
|
|
|
+ private toastController: ToastController,
|
|
|
+ private alertCtrl: AlertController,
|
|
|
+ private http: HttpClient,
|
|
|
+ private activRoute: ActivatedRoute
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ this.activRoute.paramMap.subscribe((params) => {
|
|
|
+ let ua = navigator.userAgent.toLowerCase();
|
|
|
+ if (ua.indexOf('micromessenger') != -1) {
|
|
|
+ this.userAgent = 'weixin';
|
|
|
+ this.accServ.getWXSignPackageInWechat();
|
|
|
+ } else if (ua.indexOf('windows') != -1 || ua.indexOf('Macintosh') != -1) {
|
|
|
+ this.userAgent = 'pc';
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ onchangPay(val: string) {
|
|
|
+ this.checkpay = val;
|
|
|
+ console.log(val);
|
|
|
+ }
|
|
|
+ async showCodeModal(type: string) {
|
|
|
+ switch (type) {
|
|
|
+ case 'wxpay':
|
|
|
+ this.openWxPay();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ openWxPay() {
|
|
|
+ let ua = navigator.userAgent.toLowerCase();
|
|
|
+ let isWeixin = ua.indexOf('micromessenger') != -1;
|
|
|
+ if (isWeixin) {
|
|
|
+
|
|
|
+ this.wxPayH5();
|
|
|
+ } else {
|
|
|
+ this.wxPay();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async wxPay() {
|
|
|
+ this.tradeNo = this.accServ.setTradeNo();
|
|
|
+
|
|
|
+
|
|
|
+ let params = {
|
|
|
+ company: this.accServ.company,
|
|
|
+ out_trade_no: this.tradeNo,
|
|
|
+ total_fee: this.price,
|
|
|
+ body: '充值',
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ let _this = this;
|
|
|
+ Parse.Cloud.run('pay_code2', params).then(async (res) => {
|
|
|
+ let nonce_str = res.nonce_str;
|
|
|
+ let codeLink = res.code_url[0];
|
|
|
+ let order = await this.accServ.setOrder('service', {
|
|
|
+ out_trade_no: this.tradeNo,
|
|
|
+ payType: 'wxpay-pc',
|
|
|
+ total_fee: this.price,
|
|
|
+ code: codeLink,
|
|
|
+ });
|
|
|
+ window.open(
|
|
|
+ `${location.protocol}//${location.host}/account/payment/${order.objectId}/${nonce_str}`
|
|
|
+ );
|
|
|
+ this.isOpen = false;
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ alert(error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ isDisabled: boolean = false;
|
|
|
+
|
|
|
+
|
|
|
+ async wxPayH5() {
|
|
|
+ if (this.isDisabled) return;
|
|
|
+ try {
|
|
|
+ this.isDisabled = true;
|
|
|
+ this.tradeNo = this.accServ.setTradeNo();
|
|
|
+
|
|
|
+ let wechat = Parse.User.current()?.get('wechat');
|
|
|
+ let openid =
|
|
|
+ localStorage.getItem('openid') || wechat['wxb4193c93ae9aa696']?.openid;
|
|
|
+ if (openid) {
|
|
|
+ let params = {
|
|
|
+ company: this.accServ.company,
|
|
|
+ out_trade_no: this.tradeNo,
|
|
|
+ total_fee: +this.price,
|
|
|
+ openid: openid,
|
|
|
+ appid: 'wxb4193c93ae9aa696',
|
|
|
+ };
|
|
|
+ await this.accServ.setOrder('service', {
|
|
|
+ out_trade_no: this.tradeNo,
|
|
|
+ payType: 'wxpay-h5',
|
|
|
+ total_fee: this.price,
|
|
|
+ });
|
|
|
+ let _this = this;
|
|
|
+ this.http
|
|
|
+ .post(`https://server.fmode.cn/api/wxpay/neworder`, params)
|
|
|
+ .subscribe((response) => {
|
|
|
+ let payinfo: any = response;
|
|
|
+ wx.chooseWXPay({
|
|
|
+ timestamp: payinfo.timeStamp,
|
|
|
+ nonceStr: payinfo.nonceStr,
|
|
|
+ package: payinfo.package,
|
|
|
+ signType: 'MD5',
|
|
|
+ paySign: payinfo.paySign,
|
|
|
+ success: async (res: any) => {
|
|
|
+
|
|
|
+ if (res) {
|
|
|
+ let info = {
|
|
|
+ out_trade_no: _this.tradeNo,
|
|
|
+ nonce_str: payinfo.nonceStr,
|
|
|
+ company: this.accServ.company,
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ _this.toast('支付成功', 'success');
|
|
|
+ _this.isOpen = false;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ });
|
|
|
+ this.isDisabled = false;
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+
|
|
|
+ this.isDisabled = false;
|
|
|
+ let alert = await this.alertCtrl.create({
|
|
|
+ header: '提示',
|
|
|
+ subHeader: '',
|
|
|
+ message: '缺少openid,请刷新页面或重新登录',
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ role: 'ok',
|
|
|
+ text: '确认',
|
|
|
+ handler: () => {
|
|
|
+ location.reload();
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ });
|
|
|
+ alert.present();
|
|
|
+ }
|
|
|
+ } catch (err: any) {
|
|
|
+ this.isDisabled = false;
|
|
|
+ let alert = await this.alertCtrl.create({
|
|
|
+ header: '异常错误',
|
|
|
+ subHeader: '',
|
|
|
+ message: err.message,
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ role: 'ok',
|
|
|
+ text: '确认',
|
|
|
+ handler: () => {},
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ });
|
|
|
+ alert.present();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ onClose() {
|
|
|
+ this.isOpen = false;
|
|
|
+ const result = {
|
|
|
+ code: 0,
|
|
|
+ type: this.checkpay,
|
|
|
+ };
|
|
|
+ this.onChange.emit(result);
|
|
|
+ }
|
|
|
+ async toast(text: string, type?: string) {
|
|
|
+ const toast = await this.toastController.create({
|
|
|
+ message: text,
|
|
|
+ color: type ?? 'warning',
|
|
|
+ duration: 1500,
|
|
|
+ });
|
|
|
+ toast.present();
|
|
|
+ }
|
|
|
+}
|