import Taro, { Component } from '@tarojs/taro' import { View, Image, Input } from '@tarojs/components' import ListMore from '@/c/pageDataList/listMore' import LoginPopup from '@/c/login/Popup' import './dtl.scss' import { strTrim } from '@utils' class Index extends Component { onShareAppMessage() { const { id } = this.$router.params return { title: `来谈谈你的见解?`, path: `/pagesMore/qa/dtl?id=${id}`, } } onShareTimeline () { const { id } = this.$router.params return { title: `来谈谈你的见解?`, path: `/pagesMore/qa/dtl?id=${id}`, } } constructor (props) { super(props) this.state = { page_size: 10, page: 1, isListEnd: false, isListLoading: false, isListEmpty: false, dataList: [], curObj: {}, commentVal: '', tempObj: {}, userInfo: '', token: '', isLoginPopupShow: false, } } config = { navigationBarTitleText: '问答详情', } componentWillMount () { const { name } = this.$router.params Taro.setNavigationBarTitle({ title: name || '问答详情' }) this.getDataList() this.getDtl() } getDtl () { const userInfo = Taro.getStorageSync('APP_userInfo') || '' if (userInfo) { this.setState({ userInfo }) } this.setState({ token: Taro.getStorageSync('APP_token') || '' }) } componentDidShow () { } componentDidHide () { } renderTop () { const { curObj } = this.state const HideMan = require('@img/icon_hide_man.png') const zan1Icon = require('@img/i_g_zan.png') const zan2Icon = require('@img/i_g_zan2.png') return ( {curObj.is_anon === '1' ? '匿名粉丝' : curObj.nickname || '洪楼Plus'} 《返回问答列表》 {curObj.question_cont} {curObj.create_at} {/* { curObj.is_zan ? : } {curObj.zan_count} */} ) } linkDtl () { Taro.redirectTo({url: '/pagesMore/news/index?nav=2'}) } zanHandle (answer_id, flag) { let apiStr = flag ? 'apianswerunzan' : 'apianswerzan' Taro.api.news[apiStr]({ answer_id }).then(res => { this.getDataList() }) } getDataList () { let { page_size, page, dataList, isListEmpty } = this.state const { id: question_id } = this.$router.params Taro.api.news.apianswerlist({ page, page_size, question_id, }).then(res => { const curObj = res.question || {} let curData = res.answer_list.list || [] let isListEnd = false if (curData.length > 0) { if (page === 1) { dataList = curData } else { dataList = dataList.concat(curData) } if (curData.length === page_size && res.total !== curData.length) { isListEnd = false } else { isListEnd = true } } if (curData.length === 0 && page === 1) { isListEmpty = true dataList = [] } else { isListEmpty = false } this.setState({ curObj, tempObj: curObj, dataList, isListEnd, isListEmpty, isListLoading: false }) }) } onScrollToLower (e) { let { isListEnd, isListLoading, page } = this.state if (!isListEnd && !isListLoading) { page++ this.setState({ page, isListLoading: true }, () => { this.getDataList() }) } } renderList () { const { dataList, isListEnd, isListLoading, isListEmpty, curObj } = this.state const zan1Icon = require('@img/i_g_zan.png') const zan2Icon = require('@img/i_g_zan2.png') const curItems = dataList.map((item, index) => { return ( {item.nickname} { item.parent_id === curObj.id ? {item.answer_cont} : 回复:{item.parent_user_name}{item.answer_cont} } {item.create_at} { item.is_zan ? : } {item.zan_count} ) }) return ( {curItems} ) } changeSendHandle (tempObj) { this.setState({ commentVal: '', tempObj }) } sendHandle () { const { commentVal, tempObj } = this.state const { id } = this.$router.params if (commentVal) { Taro.api.news.apiansweradd({ question_id: Number(id), answer_cont: commentVal, }).then(res => { Taro.$msg('回答成功~') this.setState({ commentVal: '' }) this.getDataList() }) } else { Taro.$msg('请输入你的回答') } } changeInput (e) { const val = strTrim(e.target.value) this.setState({ commentVal: val }) } renderQaAdd () { const iconGif = require('@img/icon_g_qa.gif') return ( ) } renderShare () { const iconShare = require('@img/images/icon_g_share6.png') return ( ) } getUserProfile (e) { wx.getUserProfile({ desc: '用于完善头像和昵称资料', success: (res) => { const dtlObj = res || {} this.setState({ userInfo: dtlObj.userInfo }) Taro.setStorageSync('APP_userInfo', dtlObj.userInfo) } }) } openLoginPopup (obj) { this.setState({ isLoginPopupShow: true }) } closeLoginPopup (str) { this.setState({ isLoginPopupShow: false }) if (str && str === 'success') { this.getDtl() } } render () { const { tempObj, commentVal, dataList } = this.state const { userInfo, token } = this.state const uInfo = userInfo || Taro.getStorageSync('APP_userInfo') const { isLoginPopupShow } = this.state return ( { uInfo ? token || isLoginPopupShow ? '' : : } {this.renderQaAdd()} {this.renderShare()} {this.renderTop()} 全部回答 ({dataList.length}条) {this.renderList()} {/* focus */} 发送 ) } } export default Index