import React from 'react'; import { StyleSheet, Text, Image, TouchableOpacity, View, TextInput, DeviceEventEmitter } from 'react-native'; import { BaseUrl, colors, deepCopy, getFileType, onePx, px2dp, toastShort, uuid, validResponseCode } from '../../utils'; import LottieView from 'lottie-react-native'; import KitVoiceRow from './KitVoiceRow'; import { BjTypeVoice } from '../../utils/types'; import RNFetchBlob from 'rn-fetch-blob'; import redux from './../../redux/store'; import { navigate } from '../../NavigationService'; interface Props { theme?: 'gray' | 'black' rowType?: 'input' | 'text' | 'pick' title?: string isNeeded?: boolean value?: string placeholderText?: string rowInputType?: 'number' | 'string' | 'default' onPress?: Function onChangeText?: Function rowInputMaxLength?: number hideRightIcon?: boolean rightIcon?: any isLast?: boolean onInputBlur?: Function fontBold?: boolean hideTitle?: boolean hasPermission?: boolean handleOffline?: boolean //手动开启离线 } interface State { voiceList: Array } export default class KitVoiceMultipleRecord extends React.Component { constructor(props: Props) { super(props); }; public readonly state: Readonly = { voiceList: [], }; checkHasOffLine = (): boolean => { try { return redux.getState().AppConfig.hasOffLine; } catch (e) { return false; } }; /** * 添加voice * @param voice */ public pushVoiceList(voice: BjTypeVoice) { const { handleOffline = false } = this.props; voice.id = uuid(); voice.uploadProgress = 0; let list = this.state.voiceList; list.unshift(voice); this.setState({ voiceList: deepCopy(list) }); //如果编辑离线数据 或者 强制外部开启离线模式 // if(this.checkHasOffLine() || handleOffline){ // //不做上传处理 // }else { // this.startUploadVoice(voice); // } } startUploadVoice(voice: BjTypeVoice) { let voiceList = this.state.voiceList; const fetchUrl = BaseUrl + '/fontapi/file/upload'; //拼接接口地址 let body = [ { name: 'file', filename: getFileType(voice.audioPath), data: RNFetchBlob.wrap(voice.audioPath) }, { name: "type", data: "2" } ]; RNFetchBlob .fetch('POST', fetchUrl, { }, body) .progress((written, total) => { }) .uploadProgress((received, total) => { // console.log(received, total) // let perent = received / total; // voiceList.forEach(ele => { // if (ele.id === voice.id) { // ele.uploadProgress = perent; // } // }) // this.setState({ voiceList }) }) .then((response) => response.json()) .then((res) => { console.log("文件上传结果", res) if (validResponseCode(res)) { voiceList.forEach(ele => { if (ele.id === voice.id) { ele.voiceUrl = res.info.url; ele.uploadProgress = 1; } }) this.setState({ voiceList }) } }) .catch((error) => { toastShort("录音文件上传失败,请您重新录制后重试") console.log('error1', error); }); }; public setVoiceList(voiceList: Array) { this.setState({ voiceList: deepCopy(voiceList) }) }; public getVoiceList() { return this.state.voiceList; }; jumpToCameraPage() { navigate('Camera') }; onConfirmToDelete(index) { let list = this.state.voiceList; list.splice(index, 1); this.setState({ voiceList: deepCopy(list) }) } showRecordSheet() { const { onPress } = this.props; onPress && onPress(); DeviceEventEmitter.emit('on-voice-row-active-opened', { index: -1 }) } render() { const { title, isNeeded, rowType, value, placeholderText, rowInputType, onPress, onChangeText, rowInputMaxLength = 30, hideRightIcon = false, rightIcon, isLast = false, theme = 'black', onInputBlur, fontBold = false, hideTitle = false, hasPermission = true } = this.props; const { voiceList } = this.state; return ( {title} {isNeeded && hasPermission && *} onChangeText && onChangeText(text)} maxLength={rowInputMaxLength} onBlur={() => onInputBlur && onInputBlur()} /> { voiceList.map((voice, index) => { return ( ) }) } ) } } const styles = StyleSheet.create({ container: { width: px2dp(350), flexDirection: 'column', justifyContent: 'flex-start', alignItems: 'center', // marginTop: px2dp(10), }, formSingleRow: { position: 'relative', width: px2dp(350), height: px2dp(72), flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 1, paddingHorizontal: 0, // backgroundColor: '#fff', // borderRadius: px2dp(6), overflow: 'hidden', // borderWidth: onePx, // borderColor: colors.borderLineColor, }, formSingleRowContent: { width: '100%', justifyContent: 'center', alignItems: 'flex-start', }, formSingleRowRight: { flex: 1, height: '100%', flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center' }, rightIcon: { width: px2dp(14), height: px2dp(14), tintColor: '#909399', }, formGroupTitleContainer: { width: '100%', height: px2dp(24), flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: px2dp(4) }, valueContainer: { width: '100%', height: px2dp(28), justifyContent: 'center', alignItems: 'flex-start' }, formRowTitle: { fontSize: 16, lineHeight: px2dp(20), color: colors.blackColor, }, formRowPlaceHolderText: { fontSize: 14, height: px2dp(20), lineHeight: px2dp(20), color: colors.placeholderColor }, formValueText: { height: px2dp(20), lineHeight: px2dp(20), fontSize: 14, color: '#303133' }, formRowPlaceHolderTextInput: { fontSize: 14, color: '#303133', width: '100%', height: px2dp(20), paddingVertical: px2dp(0), paddingHorizontal: px2dp(0), flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', }, goSearchBtn: { position: 'absolute', top: 0, right: 0, width: px2dp(120), height: px2dp(36), // backgroundColor: 'red', zIndex: 999 }, goSearchBtnContent: { width: '100%', height: '100%', flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center', paddingRight: px2dp(2) }, rowRightBtn: { height: '100%', paddingHorizontal: px2dp(12), justifyContent: 'center', alignItems: 'center' }, cameraIcon: { width: px2dp(24), height: px2dp(24), tintColor: '#666', }, })