index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { connect } from 'herculex';
  2. import dayjs from 'dayjs';
  3. import { storageAPI } from "../../../../core/utils/jsapi";
  4. import { prefixPagePath, dynamicPageName } from "../../../../core/utils";
  5. import { getQrCode } from '../utils';
  6. const app = getApp();
  7. Component(connect({
  8. mapStateToProps: {
  9. userInfo: state => state.$global.userInfo,
  10. passport: state => state.$global.passport,
  11. titleBarHeight: state => state.$global.titleBarHeight
  12. }
  13. })({
  14. props: {
  15. componentData: {}
  16. },
  17. data: {
  18. imgSrcPrefix: app.globalData.imgSrcPrefix,
  19. expiresTime: 300,
  20. qrExpiresTime: 60,
  21. defaultTips: '',
  22. timer: null,
  23. menuList: [{
  24. name: '重选来访原因',
  25. type: 'onReselectReason',
  26. icon: 'https://gw.alipayobjects.com/mdn/rms_41760b/afts/img/A*FrMRR7uHtDIAAAAAAAAAAAAAARQnAQ'
  27. }]
  28. },
  29. async didMount() {
  30. const {
  31. componentExtInfo
  32. } = this.props.componentData;
  33. const {
  34. passport
  35. } = this.data;
  36. const expiresTime = parseInt(componentExtInfo.expiresTime || this.data.expiresTime);
  37. const qrExpiresTime = parseInt(componentExtInfo.qrExpiresTime || this.data.qrExpiresTime);
  38. this.accessTime = dayjs();
  39. this.setData({
  40. time: this.accessTime.format('MM月DD日 HH:mm:ss')
  41. });
  42. this.timer = setInterval(() => {
  43. const nowtime = dayjs(); //
  44. if (passport.qrCodeUrl) {
  45. if (nowtime.diff(this.accessTime, 'second') >= qrExpiresTime) {
  46. this.accessTime = dayjs();
  47. this.refreshQRcode();
  48. }
  49. } else if (nowtime.diff(this.accessTime, 'second') >= expiresTime) {
  50. clearInterval(this.timer);
  51. my.reLaunch({
  52. url: `${prefixPagePath}/${dynamicPageName}/index?pageCode=certificateExpiredPage`
  53. });
  54. return;
  55. }
  56. this.setData({
  57. time: nowtime.format('MM月DD日 HH:mm:ss')
  58. });
  59. }, 1000);
  60. },
  61. didUnmount() {
  62. clearInterval(this.timer);
  63. this.timer = null;
  64. },
  65. methods: {
  66. async handleClick() {
  67. const [, {
  68. data = {}
  69. }] = await storageAPI.getStorage({
  70. key: 'passport'
  71. });
  72. const {
  73. communityCode,
  74. communityName,
  75. sceneCode
  76. } = data;
  77. const passport = {
  78. communityCode,
  79. communityName,
  80. sceneCode
  81. };
  82. await storageAPI.setStorage({
  83. key: 'passport',
  84. data: passport
  85. });
  86. this.commit('$global:updateState', {
  87. passport
  88. });
  89. my.navigateBack();
  90. },
  91. async refreshQRcode() {
  92. const {
  93. passport
  94. } = this.data;
  95. const params = {
  96. communityCode: passport.communityCode,
  97. communityName: passport.communityName,
  98. sceneCode: passport.sceneCode
  99. };
  100. const [err, res = {}] = await getQrCode(params);
  101. if (err || res.result !== 'success') return;
  102. const newPassport = { ...passport,
  103. qrCodeUrl: res.qrCodeUrl || ''
  104. }; // 更新storage
  105. this.commit('$global:updateState', {
  106. passport: newPassport
  107. });
  108. await storageAPI.setStorage({
  109. key: 'passport',
  110. data: newPassport
  111. });
  112. }
  113. }
  114. }));