simple-apig-auth.component.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { Component, OnInit, ViewChild } from "@angular/core";
  2. import { ActivatedRoute, Router } from "@angular/router";
  3. import Parse from "parse";
  4. import { HttpClient } from "@angular/common/http";
  5. import { NzMessageService } from "ng-zorro-antd/message";
  6. import { DeveloperServer } from "../../../service/developer";
  7. @Component({
  8. selector: "app-simple-apig-auth",
  9. templateUrl: "./simple-apig-auth.component.html",
  10. styleUrls: ["./simple-apig-auth.component.scss"],
  11. })
  12. export class SimpleApigAuthComponent implements OnInit {
  13. apigAuthId: any;
  14. apig: any;
  15. codeLink: string;
  16. company: any;
  17. // department: any;
  18. user: any;
  19. tradeNo: string;
  20. index: number = 0;
  21. price: number = 0;
  22. count: number;
  23. oldCount: number;
  24. codeModal: boolean;
  25. sucessModal: boolean = false;
  26. timer: any; //定时器
  27. order: any; // 创建的待支付订单
  28. @ViewChild("custom", { static: false }) custom: any;
  29. com:Parse.Object
  30. constructor(
  31. private activRoute: ActivatedRoute,
  32. private http: HttpClient,
  33. private message: NzMessageService,
  34. public developerSer: DeveloperServer
  35. ) {}
  36. ngOnInit(): void {
  37. this.activRoute.paramMap.subscribe((params) => {
  38. this.apigAuthId = params.get("PobjectId");
  39. this.company = params.get("company") || this.developerSer.companyId
  40. // this.company = localStorage.getItem("company");
  41. console.log(this.company);
  42. this.getCom()
  43. this.user = Parse.User.current();
  44. this.getAuth();
  45. });
  46. }
  47. async getCom(){
  48. let query = new Parse.Query('Company')
  49. query.equalTo('objectId',this.company)
  50. query.select('name','title')
  51. let com = await query.first()
  52. if(com?.id){
  53. this.com = com
  54. }
  55. }
  56. async getAuth() {
  57. let url = "https://server.fmode.cn/api/apig/getApig";
  58. this.http.post(url, { authid: this.apigAuthId }).subscribe(
  59. (data: any) => {
  60. console.log(data);
  61. this.apig = data.data;
  62. },
  63. (err) => {
  64. this.message.error("网络错误,请稍后重试");
  65. }
  66. );
  67. }
  68. changeRadio(index) {
  69. this.index = index;
  70. }
  71. async showCodeModal(type) {
  72. switch (type) {
  73. case "wxpay":
  74. this.wxPay();
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. async wxPay() {
  81. this.tradeNo = this.setTradeNo();
  82. console.log(this.company, this.tradeNo);
  83. let title = this.apig.title;
  84. let params = {
  85. company: "1AiWpTEDH9",
  86. out_trade_no: this.tradeNo,
  87. total_fee: +this.apig.priceStep[this.index].price,
  88. body: title + "接口充值",
  89. };
  90. try {
  91. let _this = this;
  92. Parse.Cloud.run("pay_code2", params).then(async (res) => {
  93. await this.setOrder("wxpay", params);
  94. let nonce_str = res.nonce_str;
  95. this.codeLink = res.code_url[0];
  96. this.codeModal = true;
  97. // 定时查询订单支付状态
  98. _this.timer = setInterval(function () {
  99. let info = {
  100. out_trade_no: _this.tradeNo,
  101. nonce_str: nonce_str,
  102. company: "1AiWpTEDH9",
  103. };
  104. console.log(info);
  105. Parse.Cloud.run("order_status2", info)
  106. .then(async (res) => {
  107. if (res.status && res.status[0] == "SUCCESS") {
  108. clearInterval(_this.timer);
  109. await _this.saveRecharge();
  110. _this.message.success("支付成功");
  111. _this.codeModal = false;
  112. _this.sucessModal = true;
  113. }
  114. })
  115. .catch((err) => {});
  116. }, 3000);
  117. });
  118. } catch (error) {
  119. alert(error);
  120. }
  121. }
  122. // 存支付记录
  123. async setOrder(type, params) {
  124. let url = "https://server.fmode.cn/api/apig/created-apigorder";
  125. this.http
  126. .post(url, {
  127. fcompany: this.company,
  128. type: type,
  129. authid: this.apigAuthId,
  130. params: params,
  131. apigid: this.apig.objectId,
  132. oldCount: this.apig.count,
  133. count: this.apig.priceStep[this.index].count,
  134. })
  135. .subscribe(
  136. (data: any) => {
  137. console.log(data);
  138. this.order = data.data;
  139. },
  140. (err) => {
  141. this.message.error("网络错误,订单创建失败");
  142. }
  143. );
  144. }
  145. setTradeNo() {
  146. let now = new Date();
  147. let tradeNo =
  148. "C" +
  149. this.company +
  150. String(now.getFullYear()) +
  151. (now.getMonth() + 1) +
  152. now.getDate() +
  153. now.getHours() +
  154. now.getMinutes() +
  155. now.getSeconds() +
  156. now.getMilliseconds();
  157. return tradeNo;
  158. }
  159. cancelPush(type) {
  160. switch (type) {
  161. case "code":
  162. this.codeModal = false;
  163. clearInterval(this.timer);
  164. break;
  165. default:
  166. this.sucessModal = false;
  167. break;
  168. }
  169. }
  170. async saveRecharge() {
  171. let url = "https://server.fmode.cn/api/apig/saveRecharge";
  172. console.log(this.order);
  173. this.http
  174. .post(url, {
  175. authComp: this.company,
  176. authid: this.apigAuthId,
  177. apigid: this.apig.objectId,
  178. oldCount: this.apig.count,
  179. count: this.apig.priceStep[this.index].count,
  180. orderid: this.order.objectId,
  181. })
  182. .subscribe(
  183. (data: any) => {
  184. console.log(data);
  185. // this.apig = data.data;
  186. },
  187. (err) => {
  188. this.message.error("网络错误,请稍后重试");
  189. }
  190. );
  191. }
  192. back(){
  193. history.back()
  194. }
  195. }