page.js 5.3 KB

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