page.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import Assets from '@src/components/Assets';
  5. import { asyncSMessage } from '@src/services/AsyncTools';
  6. import { dataURLtoBlob, formatDate } from '@src/services/Tools';
  7. import Icon from '../../../components/Icon';
  8. import { Common } from '../../../stores/common';
  9. // import { Main } from '../../../stores/main';
  10. import { My } from '../../../stores/my';
  11. export default class extends Page {
  12. initState() {
  13. return { state: 'wait' };
  14. }
  15. init() {
  16. this.wx = null;
  17. Common.readyWechat(window.location.href, ['chooseImage', 'getLocalImgData']).then(wx => {
  18. this.wx = wx;
  19. });
  20. }
  21. submitFront() {
  22. if (this.wx) {
  23. this.wx.chooseImage({
  24. count: 1, // 默认9
  25. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  26. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  27. success(response) {
  28. wx.getLocalImgData({
  29. localId: response.localIds[0].toString(),
  30. success: res => {
  31. const { localData } = res;
  32. let imageBase64 = '';
  33. if (localData.indexOf('data:image') === 0) {
  34. // 苹果的直接赋值,默认生成'data:image/jpeg;base64,'的头部拼接
  35. imageBase64 = localData;
  36. } else {
  37. // 此处是安卓中的唯一得坑!在拼接前需要对localData进行换行符的全局替换
  38. // 此时一个正常的base64图片路径就完美生成赋值到img的src中了
  39. imageBase64 = `data:image/jpeg;base64,${localData.replace(/\n/g, '')}`;
  40. }
  41. this.setState({ front: imageBase64 });
  42. My.realFront(new File(dataURLtoBlob(imageBase64), 'front.jpg', { type: 'image/jpeg' }))
  43. .then(() => {
  44. this.back = true;
  45. this.submit();
  46. })
  47. .catch(err => {
  48. asyncSMessage(err.message, 'error');
  49. });
  50. },
  51. });
  52. },
  53. });
  54. }
  55. }
  56. submitBack() {
  57. if (this.wx) {
  58. this.wx.chooseImage({
  59. count: 1, // 默认9
  60. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  61. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  62. success(response) {
  63. wx.getLocalImgData({
  64. localId: response.localIds[0].toString(),
  65. success: res => {
  66. const { localData } = res;
  67. let imageBase64 = '';
  68. if (localData.indexOf('data:image') === 0) {
  69. // 苹果的直接赋值,默认生成'data:image/jpeg;base64,'的头部拼接
  70. imageBase64 = localData;
  71. } else {
  72. // 此处是安卓中的唯一得坑!在拼接前需要对localData进行换行符的全局替换
  73. // 此时一个正常的base64图片路径就完美生成赋值到img的src中了
  74. imageBase64 = `data:image/jpeg;base64,${localData.replace(/\n/g, '')}`;
  75. }
  76. this.setState({ back: imageBase64 });
  77. dataURLtoBlob(imageBase64);
  78. My.realBack(new File(dataURLtoBlob(imageBase64), 'back.jpg', { type: 'image/jpeg' }))
  79. .then(() => {
  80. this.back = true;
  81. this.submit();
  82. })
  83. .catch(err => {
  84. asyncSMessage(err.message, 'error');
  85. });
  86. },
  87. });
  88. },
  89. });
  90. }
  91. }
  92. submit() {
  93. if (!this.front || !this.back) {
  94. return;
  95. }
  96. My.realFinish().then((result) => {
  97. this.setState({ state: 'finish', data: result });
  98. });
  99. }
  100. renderWait() {
  101. return (
  102. <div>
  103. <div className="text">
  104. 请扫描您的居民身份证原件,领取90天VIP权限。
  105. <br />
  106. 千行将重视和保护您的隐私。
  107. </div>
  108. <div className="id-card-1">
  109. <Assets name="IDcard" />
  110. <Assets
  111. className="scan"
  112. src={this.state.front}
  113. name="scan1"
  114. onClick={() => {
  115. this.submitFront();
  116. }}
  117. />
  118. </div>
  119. <div className="id-card-2">
  120. <Assets name="IDcard2" />
  121. <Assets
  122. className="scan"
  123. src={this.state.back}
  124. name="scan2"
  125. onClick={() => {
  126. this.submitBack();
  127. }}
  128. />
  129. </div>
  130. </div>
  131. );
  132. }
  133. renderFinish() {
  134. const { data } = this.state;
  135. return (
  136. <div className="finish">
  137. <div className="icon">
  138. <Icon type="check-circle" />
  139. </div>
  140. <div className="title">认证完成!</div>
  141. <div className="desc">90天VIP权限已赠送至您的账户</div>
  142. <div className="desc">生效时间:{data.useStartTime && formatDate(data.useStartTime, 'YYYY-MM-DD')}</div>
  143. </div>
  144. );
  145. }
  146. renderView() {
  147. const { state } = this.state;
  148. switch (state) {
  149. case 'finish':
  150. return this.renderFinish();
  151. default:
  152. return this.renderWait();
  153. }
  154. }
  155. }