123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import React from 'react';
- import './index.less';
- import Page from '@src/containers/Page';
- import Assets from '@src/components/Assets';
- import { asyncSMessage } from '@src/services/AsyncTools';
- import { dataURLtoBlob, formatDate } from '@src/services/Tools';
- import Icon from '../../../components/Icon';
- import { Common } from '../../../stores/common';
- // import { Main } from '../../../stores/main';
- import { My } from '../../../stores/my';
- export default class extends Page {
- initState() {
- return { state: 'wait' };
- }
- init() {
- this.wx = null;
- Common.readyWechat(window.location.href, ['chooseImage', 'getLocalImgData']).then(wx => {
- this.wx = wx;
- });
- }
- submitFront() {
- const self = this;
- if (this.wx) {
- this.wx.chooseImage({
- count: 1, // 默认9
- sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
- sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
- success(response) {
- wx.getLocalImgData({
- localId: response.localIds[0].toString(),
- success: res => {
- const { localData } = res;
- let imageBase64 = '';
- if (localData.indexOf('data:image') === 0) {
- // 苹果的直接赋值,默认生成'data:image/jpeg;base64,'的头部拼接
- imageBase64 = localData;
- } else {
- // 此处是安卓中的唯一得坑!在拼接前需要对localData进行换行符的全局替换
- // 此时一个正常的base64图片路径就完美生成赋值到img的src中了
- imageBase64 = `data:image/jpeg;base64,${localData.replace(/\n/g, '')}`;
- }
- self.setState({ front: imageBase64 });
- My.realFront(new File([dataURLtoBlob(imageBase64)], 'front.jpg'))
- .then(() => {
- self.front = true;
- self.submit();
- })
- .catch(err => {
- asyncSMessage(err.message, 'error');
- });
- },
- });
- },
- });
- }
- }
- submitBack() {
- const self = this;
- if (this.wx) {
- this.wx.chooseImage({
- count: 1, // 默认9
- sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
- sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
- success(response) {
- wx.getLocalImgData({
- localId: response.localIds[0].toString(),
- success: res => {
- const { localData } = res;
- let imageBase64 = '';
- if (localData.indexOf('data:image') === 0) {
- // 苹果的直接赋值,默认生成'data:image/jpeg;base64,'的头部拼接
- imageBase64 = localData;
- } else {
- // 此处是安卓中的唯一得坑!在拼接前需要对localData进行换行符的全局替换
- // 此时一个正常的base64图片路径就完美生成赋值到img的src中了
- imageBase64 = `data:image/jpeg;base64,${localData.replace(/\n/g, '')}`;
- }
- self.setState({ back: imageBase64 });
- My.realBack(new File([dataURLtoBlob(imageBase64)], 'back.jpg'))
- .then(() => {
- self.back = true;
- self.submit();
- })
- .catch(err => {
- asyncSMessage(err.message, 'error');
- });
- },
- });
- },
- });
- }
- }
- submit() {
- if (!this.front || !this.back) {
- return;
- }
- My.realFinish().then((result) => {
- this.setState({ state: 'finish', data: result });
- });
- }
- renderWait() {
- return (
- <div>
- <div className="text">
- 请扫描您的居民身份证原件,领取30天VIP权限。
- <br />
- 千行将重视和保护您的隐私。
- </div>
- <div className="id-card-1">
- <Assets
- src={this.state.front}
- name={this.state.front ? '' : 'IDcard'}
- />
- <Assets
- className="scan"
- name='scan1'
- onClick={() => {
- this.submitFront();
- }}
- />
- </div>
- <div className="id-card-2">
- <Assets
- src={this.state.back}
- name={this.state.back ? '' : 'IDcard2'}
- />
- <Assets
- className="scan"
- name='scan2'
- onClick={() => {
- this.submitBack();
- }}
- />
- </div>
- </div>
- );
- }
- renderFinish() {
- const { data = {} } = this.state;
- const { record } = data;
- return (
- <div className="finish">
- <div className="icon">
- <Icon type="check-circle" />
- </div>
- <div className="title">认证完成!</div>
- <div className="desc">30天VIP权限已赠送至您的账户</div>
- <div className="desc">生效时间:{record.useStartTime && formatDate(record.useStartTime, 'YYYY-MM-DD')}</div>
- </div>
- );
- }
- renderView() {
- const { state } = this.state;
- switch (state) {
- case 'finish':
- return this.renderFinish();
- default:
- return this.renderWait();
- }
- }
- }
|