dtl.jsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import Taro, { Component } from '@tarojs/taro'
  2. import { View, Image, Input } from '@tarojs/components'
  3. import ListMore from '@/c/pageDataList/listMore'
  4. import LoginPopup from '@/c/login/Popup'
  5. import './dtl.scss'
  6. import { strTrim } from '@utils'
  7. class Index extends Component {
  8. onShareAppMessage() {
  9. const { id } = this.$router.params
  10. return {
  11. title: `来谈谈你的见解?`,
  12. path: `/pagesMore/qa/dtl?id=${id}`,
  13. }
  14. }
  15. onShareTimeline () {
  16. const { id } = this.$router.params
  17. return {
  18. title: `来谈谈你的见解?`,
  19. path: `/pagesMore/qa/dtl?id=${id}`,
  20. }
  21. }
  22. constructor (props) {
  23. super(props)
  24. this.state = {
  25. page_size: 10,
  26. page: 1,
  27. isListEnd: false,
  28. isListLoading: false,
  29. isListEmpty: false,
  30. dataList: [],
  31. curObj: {},
  32. commentVal: '',
  33. tempObj: {},
  34. userInfo: '',
  35. token: '',
  36. isLoginPopupShow: false,
  37. }
  38. }
  39. config = {
  40. navigationBarTitleText: '问答详情',
  41. }
  42. componentWillMount () {
  43. const { name } = this.$router.params
  44. Taro.setNavigationBarTitle({
  45. title: name || '问答详情'
  46. })
  47. this.getDataList()
  48. this.getDtl()
  49. }
  50. getDtl () {
  51. const userInfo = Taro.getStorageSync('APP_userInfo') || ''
  52. if (userInfo) {
  53. this.setState({
  54. userInfo
  55. })
  56. }
  57. this.setState({
  58. token: Taro.getStorageSync('APP_token') || ''
  59. })
  60. }
  61. componentDidShow () { }
  62. componentDidHide () { }
  63. renderTop () {
  64. const { curObj } = this.state
  65. const HideMan = require('@img/icon_hide_man.png')
  66. const zan1Icon = require('@img/i_g_zan.png')
  67. const zan2Icon = require('@img/i_g_zan2.png')
  68. return (
  69. <View className="l-comment-box">
  70. <View className="lcb-item" onClick={this.changeSendHandle.bind(this, curObj)}>
  71. <View className="lb-img">
  72. <Image className="img" src={curObj.is_anon === '1' ? HideMan : curObj.avatar} />
  73. </View>
  74. <View className="lb-info">
  75. <View className="lb-p1">
  76. {curObj.is_anon === '1' ? '匿名粉丝' : curObj.nickname || '洪楼Plus'}
  77. <View onClick={this.linkDtl.bind(this)} className="more">《返回问答列表》</View>
  78. </View>
  79. <View className="lb-p2">{curObj.question_cont}</View>
  80. <View className="lb-time">{curObj.create_at}</View>
  81. </View>
  82. {/* <View className="lb-zan" onClick={this.zanHandle.bind(this, curObj.id, curObj.is_zan)}>
  83. {
  84. curObj.is_zan
  85. ?
  86. <Image className="i" src={zan2Icon}/>
  87. :
  88. <Image className="i" src={zan1Icon}/>
  89. }
  90. <View className={curObj.is_zan ? 'n cur' : 'n'}>{curObj.zan_count}</View>
  91. </View> */}
  92. </View>
  93. </View>
  94. )
  95. }
  96. linkDtl () {
  97. Taro.redirectTo({url: '/pagesMore/news/index?nav=2'})
  98. }
  99. zanHandle (answer_id, flag) {
  100. let apiStr = flag ? 'apianswerunzan' : 'apianswerzan'
  101. Taro.api.news[apiStr]({
  102. answer_id
  103. }).then(res => {
  104. this.getDataList()
  105. })
  106. }
  107. getDataList () {
  108. let { page_size, page, dataList, isListEmpty } = this.state
  109. const { id: question_id } = this.$router.params
  110. Taro.api.news.apianswerlist({
  111. page,
  112. page_size,
  113. question_id,
  114. }).then(res => {
  115. const curObj = res.question || {}
  116. let curData = res.answer_list.list || []
  117. let isListEnd = false
  118. if (curData.length > 0) {
  119. if (page === 1) {
  120. dataList = curData
  121. } else {
  122. dataList = dataList.concat(curData)
  123. }
  124. if (curData.length === page_size && res.total !== curData.length) {
  125. isListEnd = false
  126. } else {
  127. isListEnd = true
  128. }
  129. }
  130. if (curData.length === 0 && page === 1) {
  131. isListEmpty = true
  132. dataList = []
  133. } else {
  134. isListEmpty = false
  135. }
  136. this.setState({
  137. curObj,
  138. tempObj: curObj,
  139. dataList,
  140. isListEnd,
  141. isListEmpty,
  142. isListLoading: false
  143. })
  144. })
  145. }
  146. onScrollToLower (e) {
  147. let { isListEnd, isListLoading, page } = this.state
  148. if (!isListEnd && !isListLoading) {
  149. page++
  150. this.setState({
  151. page,
  152. isListLoading: true
  153. }, () => {
  154. this.getDataList()
  155. })
  156. }
  157. }
  158. renderList () {
  159. const { dataList, isListEnd, isListLoading, isListEmpty, curObj } = this.state
  160. const zan1Icon = require('@img/i_g_zan.png')
  161. const zan2Icon = require('@img/i_g_zan2.png')
  162. const curItems = dataList.map((item, index) => {
  163. return (
  164. <View className="lcb-item" key={index}>
  165. <View className="lb-img">
  166. <Image className="img" src={item.avatar} />
  167. </View>
  168. <View className="lb-info">
  169. <View className="lb-p1">{item.nickname}</View>
  170. {
  171. item.parent_id === curObj.id
  172. ?
  173. <View className="lb-p2">{item.answer_cont}</View>
  174. :
  175. <View className="lb-p2">回复:<View className="link">{item.parent_user_name}</View>{item.answer_cont}</View>
  176. }
  177. <View className="lb-time">{item.create_at}</View>
  178. <View className="lb-zan" onClick={this.zanHandle.bind(this, item.answer_id, item.is_zan)}>
  179. {
  180. item.is_zan
  181. ?
  182. <Image className="i" src={zan2Icon}/>
  183. :
  184. <Image className="i" src={zan1Icon}/>
  185. }
  186. <View className={item.is_zan ? 'n cur' : 'n'}>{item.zan_count}</View>
  187. </View>
  188. </View>
  189. </View>
  190. )
  191. })
  192. return (
  193. <ScrollView
  194. className='l-scroll-view'
  195. scrollY
  196. scrollWithAnimation
  197. scrollTop="0"
  198. lowerThreshold="30"
  199. onScrollToLower={this.onScrollToLower.bind(this)}
  200. >
  201. <View className="l-comment-box">
  202. {curItems}
  203. </View>
  204. <ListMore isListEnd={isListEnd} isListLoading={isListLoading} isListEmpty={isListEmpty} text="暂无回复"/>
  205. </ScrollView>
  206. )
  207. }
  208. changeSendHandle (tempObj) {
  209. this.setState({
  210. commentVal: '',
  211. tempObj
  212. })
  213. }
  214. sendHandle () {
  215. const { commentVal, tempObj } = this.state
  216. const { id } = this.$router.params
  217. if (commentVal) {
  218. Taro.api.news.apiansweradd({
  219. question_id: Number(id),
  220. answer_cont: commentVal,
  221. }).then(res => {
  222. Taro.$msg('回答成功~')
  223. this.setState({
  224. commentVal: ''
  225. })
  226. this.getDataList()
  227. })
  228. } else {
  229. Taro.$msg('请输入你的回答')
  230. }
  231. }
  232. changeInput (e) {
  233. const val = strTrim(e.target.value)
  234. this.setState({
  235. commentVal: val
  236. })
  237. }
  238. renderQaAdd () {
  239. const iconGif = require('@img/icon_g_qa.gif')
  240. return (
  241. <Navigator url={'/pagesQa/add'} className="scoped-fix-q2 t2">
  242. <Image className="img" src={iconGif} />
  243. </Navigator>
  244. )
  245. }
  246. renderShare () {
  247. const iconShare = require('@img/images/icon_g_share6.png')
  248. return (
  249. <Button className="g-icon-share btn-to-div" openType="share">
  250. <Image className="img" src={iconShare} />
  251. </Button>
  252. )
  253. }
  254. getUserProfile (e) {
  255. wx.getUserProfile({
  256. desc: '用于完善头像和昵称资料',
  257. success: (res) => {
  258. const dtlObj = res || {}
  259. this.setState({
  260. userInfo: dtlObj.userInfo
  261. })
  262. Taro.setStorageSync('APP_userInfo', dtlObj.userInfo)
  263. }
  264. })
  265. }
  266. openLoginPopup (obj) {
  267. this.setState({
  268. isLoginPopupShow: true
  269. })
  270. }
  271. closeLoginPopup (str) {
  272. this.setState({
  273. isLoginPopupShow: false
  274. })
  275. if (str && str === 'success') {
  276. this.getDtl()
  277. }
  278. }
  279. render () {
  280. const { tempObj, commentVal, dataList } = this.state
  281. const { userInfo, token } = this.state
  282. const uInfo = userInfo || Taro.getStorageSync('APP_userInfo')
  283. const { isLoginPopupShow } = this.state
  284. return (
  285. <View className="l-box has-footer">
  286. {
  287. uInfo
  288. ? token || isLoginPopupShow
  289. ? ''
  290. : <Button className="g-u-btn" onClick={this.openLoginPopup.bind(this)}></Button>
  291. : <Button className="g-u-btn" onClick={this.getUserProfile.bind(this)}></Button>
  292. }
  293. <LoginPopup show={isLoginPopupShow} close={this.closeLoginPopup.bind(this)} />
  294. {this.renderQaAdd()}
  295. {this.renderShare()}
  296. {this.renderTop()}
  297. <View className="scoped-main">
  298. <View className="sm-title">
  299. 全部回答
  300. <View className="t">({dataList.length}条)</View>
  301. </View>
  302. {this.renderList()}
  303. </View>
  304. <View className="l-floor-footer">
  305. <View className="scoped-footer">
  306. <View className="sf-input">
  307. {/* focus */}
  308. <Input className="input" placeholder={`我来回答`} value={commentVal} onInput={this.changeInput.bind(this)} />
  309. <View className="btn" onClick={this.sendHandle.bind(this)}>发送</View>
  310. </View>
  311. </View>
  312. </View>
  313. </View>
  314. )
  315. }
  316. }
  317. export default Index