SectionsContentView.js 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
import React, {Component} from "react";
import {ActivityIndicator, Image, StyleSheet, Text, TouchableOpacity, View, WebView, Platform} from "react-native";
import {convertCurrency, xnBorderWidth} from "../utils/utils";
import AppService from "../service/AppService";
import {NoDoublePress, onResponseError} from "../utils/Common";

const BaseScript =
    `
(function () {
    var height = null;
    function changeHeight() {
      if (document.body.scrollHeight != height) {
        height = document.body.scrollHeight;
        if (window.postMessage) {
          window.postMessage(JSON.stringify({
            type: 'setHeight',
            height: height,
          }))
        }
      }
    }
    setInterval(changeHeight, 100);
} ())
`

export default class SectionsContentView extends Component {
    constructor(props) {
        super(props);
        this.flowId = this.props.flowId;
        this.state = {
            data: {},// 查询结果
            loading: true,// 是否正在查询
            isForm: false, // 是否是FORM格式的返回结果(返回结果可能是FORM或者HTML)
            hasSections: false,// 返回结果是否具有sections数组
            webHeight: 80
        };
    }

    componentDidMount() {
        this._query();
    }

    _query = () => {
        let _this = this;
        let vm = {flowId: this.flowId};
        _this.setState({
            loading: true,
        });
        AppService.getFlowDescribe(vm).then(data => {
            _this.setState({
                loading: false,
            });
            console.log(data);
            if (!onResponseError(data, false)) {
                if (!!!data.flowId) {
                    data.flowId = this.flowId
                }
                let isForm = false;
                let hasSections = false;
                if (!!data.resultFormat && data.resultFormat == 'FORM') {
                    isForm = true;
                    if (!!data.result) {
                        try {
                            let dic = JSON.parse(data.result);
                            // 加判sections是否存在,发现有result是一个"{}"的情况
                            if(!!dic.sections){
                                data.sections = dic.sections;
                                hasSections = true;
                            }
                        } catch (e) {
                            console.log('0000000');
                        }
                    }
                }
                _this.setState({
                    data: data,
                    isForm: isForm,
                    hasSections: hasSections
                });
            }
        }).catch((error) => {
            _this.setState({
                loading: false,
            });
            // xnToast(error)
        });
    }

    _renderSections = (v, i) => {
        if (v.type === "FILES") {// 附件
            return (
                <View style={styles.detailRowBox} key={i}>
                    {v.items && v.items.length > 0 &&
                    <View style={styles.detailLableBox}>
                        <Text style={styles.detailLable}>{v.items[0].label}</Text>
                    </View>
                    }
                    <View style={styles.detailList}>
                        {v.items && v.items.map((m, n) => this._renderFiles(m, n))}
                    </View>
                </View>
            )
        } else if (v.type === "PICTURES") {// 图片
            return (
                <View style={styles.detailRowBox} key={i}>
                    {v.items && v.items.length > 0 &&
                    <View style={styles.detailLableBox}>
                        <Text style={styles.detailLable}>{v.items[0].label}</Text>
                    </View>
                    }
                    <View style={styles.detailList}>
                        {v.items && v.items.map((m, n) => this._renderImg(m, n, v.items))}
                    </View>
                </View>
            )
        } else if (v.type === "LINES") {// 容器布局
            return (
                <View>
                    <View style={styles.detailRow} key={i}>
                        <Text style={styles.detailLable}>{v.title || ''}</Text>
                    </View>
                    {
                        v.fields && v.fields.map((m, n) => this._renderSections(m, n))
                    }
                </View>
            )
        } else if (v.type === "USERS") {// 用户
            return (
                <View style={styles.detailRowBox} key={i}>
                    <View style={[styles.detailLableBox, {height: 20}]}>
                        <Text style={styles.detailLable}>{v.items[0].label}</Text>
                    </View>
                    <View style={styles.detailList}>
                        {v.userList && v.userList.map((m, n) => this._renderUser(m, n))}
                    </View>
                </View>
            )
        } else if (v.type === "SCORE") {// 评分
            return (
                <View style={[styles.detailRowBox, {flexDirection: "column",}]} key={i}>
                    {v.items && v.items.map((m, n) => this._renderScore(m, n))}
                </View>
            )
        } else if (v.type === "MONEY") {// 金额
            return (
                <View style={[styles.detailRowBox, {flexDirection: "column",}]} key={i}>
                    {v.items && v.items.map((m, n) => this._renderMoney(m, n))}
                </View>
            )
        } else {
            // 文本
            return (
                <View style={[styles.detailRowBox, {flexDirection: "column",}]} key={i}>
                    {v.items && v.items.map((m, n) => this._renderText(m, n))}
                </View>
            )
        }
    };

    // 图片
    _renderImg = (v, i, items) => {
        return (
            <TouchableOpacity style={styles.detailItem} key={i} onPress={() => {
                // FIXME:浏览图片回调
                // this.toImageBrowser(items, i)
            }}>
                <View style={styles.attachmentsItemBoxImg}>
                    <Image style={styles.attachmentsItemImage}
                           source={{uri: v.url + "?x-oss-process=image/resize,m_lfit,w_100,h_100"}}
                           resizeMode="contain"/>
                </View>
                <View style={styles.attachmentsItemName}><Text style={styles.attachmentsItemNameInfo} numberOfLines={1}
                                                               ellipsizeMode='tail'>{v.title}</Text></View>
            </TouchableOpacity>
        )
    };

    // 附件
    _renderFiles = (v, i) => {
        let type = ""
        if (v.type === "IMAGE") {
            type = "img"
        }

        return (
            <TouchableOpacity style={styles.detailItem} key={i} onPress={() => {
                // FIXME:浏览附件回调
                // this.viewImg(type, v.url, v.name, v.size)
            }}>
                <View style={[styles.attachmentsItemBoxImg]}>
                    {v.type === "FOLDER" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/other.png')}
                           resizeMode="contain"/>}
                    {v.type === "EXCEL" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/excle.png')}
                           resizeMode="contain"/>}
                    {v.type === "WORD" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/word.png')}
                           resizeMode="contain"/>}
                    {v.type === "PPT" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/ppt.png')}
                           resizeMode="contain"/>}
                    {v.type === "TXT" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/txt.png')}
                           resizeMode="contain"/>}
                    {v.type === "PDF" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/pdf.png')}
                           resizeMode="contain"/>}
                    {v.type === "ZIP" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/rar.png')}
                           resizeMode="contain"/>}
                    {v.type === "OTHER" &&
                    <Image style={styles.attachmentsItemImage} source={require('../img/other.png')}
                           resizeMode="contain"/>}
                    {v.type === "IMAGE" && <Image style={styles.attachmentsItemImage}
                                                  source={{uri: v.url + "?x-oss-process=image/resize,m_lfit,w_100,h_100"}}
                                                  resizeMode="contain"/>}
                </View>
                <View style={styles.attachmentsItemName}><Text style={styles.attachmentsItemNameInfo} numberOfLines={1}
                                                               ellipsizeMode='tail'>{v.name}</Text></View>
            </TouchableOpacity>
        )
    };

    // 用户
    _renderUser = (v, i) => {
        return (
            <View style={styles.detailItem} key={i} onPress={() => {
                <TouchableOpacity activeOpacity={0.8} style={[styles.modelImg, {backgroundColor: global.homeColor}]}
                                  onPress={() => {
                                      // FIXME:查看用户信息回调
                                      // toPersonalPageWithUserId(v.id);
                                  }}>
                    {!v.avatar &&
                    <Text style={styles.modelImgText}>{v.name || ''}</Text>}
                    {v.avatar &&
                    <Image style={styles.modelImgAvatar} source={{uri: v.avatar}}
                           resizeMode="contain"/>}
                </TouchableOpacity>
            }}>
            </View>
        )
    };

    // 评分
    _renderScore = (v, i) => {
        return (
            <View style={styles.detailRow} key={i}>
                <Text style={styles.detailLable}>{v.label || ''}</Text>
                {!!v.score && v.maxValue && <Text style={styles.detailContent}>{v.score}-{v.maxValue}</Text>}
            </View>
        )
    };

    // 金额
    _renderMoney = (v, i) => {
        let showValue = v.value || '';
        if (v.showChinese) {
            showValue = convertCurrency(showValue);
        }
        return (
            <View style={styles.detailRow} key={i}>
                <Text style={styles.detailLable}>{v.label || ''}</Text>
                {!!v.score && v.maxValue && <Text style={styles.detailContent}>{showValue}</Text>}
            </View>
        )
    };

    // 文本
    _renderText = (v, i) => {
        return (
            <View style={styles.detailRow} key={i}>
                <Text style={styles.detailLable}>{v.label || ''}</Text>
                <Text style={styles.detailContent}>{v.text || ''}</Text>
            </View>
        )
    };

    //设置高度
    onMessage(event) {
        console.log(event.nativeEvent.data);
        try {
            const action = JSON.parse(event.nativeEvent.data)
            if (action.type === 'setHeight' && action.height > 0) {
                this.setState({webHeight: action.height})
                // alert(action.height);
            }
        } catch (error) {
            // pass
            // alert('action.height');
        }
    }

    render() {
        // ios因为WebView的实现改造为了WKWebview,存在字体太小的问题,这里判断是ios在最外层指定了字体大小来调整
        let _htmlStr = this.state.data.result;
        if(!!this.state.data.result && !this.state.isForm && Platform.OS === 'ios'){
            _htmlStr = '<div style="font-size:40px"}>'+this.state.data.result+'</div>';
        }

        return (
            <View style={styles.detailLayout}>
                {/*查询中*/}
                {this.state.loading && <View style={{
                    width: '100%',
                    height: 80,
                    justifyContent: 'center',
                    alignItems: 'center',
                    backgroundColor: '#fff'
                }}>
                    <View style={styles.xnLoadingBd}>
                        <ActivityIndicator style={styles.xnLoadingActivity} size='small' color='#d9d9d9'
                                           animating={true}/>
                        <Text style={styles.xnLoadingBdText}>加载中...</Text>
                    </View>
                </View>}
                {/*查询结果是FORM,且有sections*/}
                {!this.state.loading && !!this.state.data.result && this.state.isForm && this.state.hasSections &&
                this.state.data.sections.map((v, i) => this._renderSections(v, i))}
                {/*查询结果是FORM,且没有sections*/}
                {!this.state.loading && !!this.state.data.result && this.state.isForm && !this.state.hasSections &&
                <View style={{
                    width: '100%', justifyContent: 'center', alignItems: 'center', height: 80,
                    backgroundColor: '#fff'
                }} />
                }
                {/*查询结果是HTML*/}
                {!this.state.loading && !!this.state.data.result && !this.state.isForm &&
                <WebView
                    style={{
                        width: '100%',
                        height: this.state.webHeight
                    }}
                    injectedJavaScript={BaseScript}
                    scalesPageToFit={true}
                    javaScriptEnabled={true}
                    decelerationRate='normal'
                    startInLoadingState={true}
                    source={{html: _htmlStr, baseUrl: ''}}
                    bounces={false}
                    scrollEnabled={false}
                    automaticallyAdjustContentInsets={true}
                    contentInset={{top: 0, left: 0,right:0,bottom:0}}
                    onMessage={this.onMessage.bind(this)}
                />
                }
                {/*查询结果没有data.result*/}
                {!this.state.loading && !!!this.state.data.result &&
                <View style={{
                    width: '100%', justifyContent: 'center', alignItems: 'center', height: 80,
                    backgroundColor: '#fff'
                }}/>
                }
            </View>
        )
    }
}

const styles = StyleSheet.create({
    modelImg: {
        width: 40,
        height: 40,
        marginRight: 10,
        flexDirection: "column",
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: '#39f',
        borderRadius: 4,
    },
    modelImgText: {
        color: "#fff",
        fontSize: 10
    },
    modelImgAvatar: {
        width: 40,
        height: 40,
        borderRadius: 4,
    },
    detailLayout: {
        marginTop: 10,
        marginLeft: 15,
        marginRight: 15,
    },
    detailRowBox: {
        flexDirection: "row",
    },
    detailRow: {
        flexDirection: "row",
        paddingTop: 5,
        paddingBottom: 5,
    },
    detailLable: {
        flex: 1,
        color: "#666",
        fontSize:14
    },
    detailContent: {
        flex: 2,
        textAlign: "right",
        color: "#333",
        fontSize:14
    },
    detailLableBox: {
        width: 80
    },
    detailList: {
        flex: 1,
        flexDirection: "row",
        justifyContent: "flex-end",
        flexWrap: "wrap"
    },
    detailItem: {
        width: 50,
        height: 70,
        marginBottom: 10,
        marginLeft: 10,
        justifyContent: "center",
    },
    approvedLayout: {
        marginTop: 0,
        marginBottom: 15,
    },
    attachmentsItemBoxImg: {
        height: 50,
        width: 50,
        justifyContent: "center",
        alignItems: "center",
        borderRadius: 3,
        borderColor: "#ddd",
        borderStyle: "solid",
        borderWidth: xnBorderWidth(),
    },
    attachmentsItemImage: {
        height: 49,
        width: 49,
    },
    attachmentsItemName: {
        height: 20,
        width: 50,
        justifyContent: "center",
        flexDirection: "row",
        alignItems: "center",
    },
    attachmentsItemNameInfo: {
        fontSize: 10,
        color: "#333333",
        textAlign: "center",
        paddingLeft: 3,
        paddingRight: 3,
        lineHeight: 15,
        height: 15,
    },
    xnLoadingBd: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'center',
    },
    xnLoadingActivity: {
        marginRight: 15,
    },
    xnLoadingBdText: {
        fontSize: 14,
        color: "#000"
    },
});