index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { getPagesQuery } from "../../../../core/utils";
  2. import { getCertQrCode, queryCertDetail, queryServiceByCode } from './../../services';
  3. const app = getApp();
  4. Page({
  5. data: {
  6. loading: true,
  7. pageInfo: {},
  8. serviceInfo: {},
  9. shouldRefresh: false,
  10. imgSrcPrefix: app.globalData.imgSrcPrefix
  11. },
  12. async onLoad({
  13. certType,
  14. certServiceCode
  15. }) {
  16. /* 证件类型 */
  17. const _certType = getPagesQuery('certType');
  18. /* 证件对应的服务码 */
  19. const _certServiceCode = getPagesQuery('certServiceCode');
  20. this.certType = certType || _certType;
  21. this.serviceCode = certServiceCode || _certServiceCode;
  22. await this.onStart();
  23. this.onRunTimer();
  24. },
  25. onUnload() {
  26. if (this.timer) clearTimeout(this.timer);
  27. },
  28. updateData(data) {
  29. return new Promise(resolve => this.setData(data, resolve));
  30. },
  31. async onStart() {
  32. await this.fetchCertDetail(this.certType);
  33. await this.fetchServiceInfo(this.serviceCode);
  34. await this.fetchQrCode(this.certType);
  35. await this.updateData({
  36. loading: false
  37. });
  38. },
  39. /*
  40. * 获取服务详情
  41. * */
  42. async fetchServiceInfo(code) {
  43. const [err, data] = await queryServiceByCode(code);
  44. if (err) return;
  45. const {
  46. extInfo,
  47. imgUrl
  48. } = data || {};
  49. const {
  50. pageInfo
  51. } = this.data;
  52. await this.updateData({
  53. serviceInfo: data,
  54. pageInfo: { ...pageInfo,
  55. extInfo,
  56. imgUrl
  57. }
  58. });
  59. },
  60. /*
  61. * 获取二维码详情
  62. * */
  63. async fetchQrCode(certType) {
  64. const [err, data] = await getCertQrCode(certType);
  65. if (err) return;
  66. const {
  67. pageInfo
  68. } = this.data;
  69. await this.updateData({
  70. pageInfo: { ...pageInfo,
  71. ...data
  72. }
  73. });
  74. },
  75. /*
  76. * 获取卡片详情
  77. * */
  78. async fetchCertDetail(certType) {
  79. const [err, data] = await queryCertDetail(certType);
  80. if (err) return;
  81. await this.updateData({
  82. pageInfo: data
  83. });
  84. },
  85. /*
  86. * 定时器
  87. * */
  88. onRunTimer() {
  89. const {
  90. extInfo
  91. } = this.data.pageInfo;
  92. const {
  93. certCodeRate = 60
  94. } = extInfo || {};
  95. this.timer = setTimeout(async () => {
  96. await this.updateData({
  97. shouldRefresh: true
  98. });
  99. }, 1000 * +certCodeRate);
  100. },
  101. async onRefreshHandel() {
  102. this.onRunTimer();
  103. await this.updateData({
  104. shouldRefresh: false
  105. });
  106. await this.fetchQrCode(this.certType);
  107. }
  108. });