ReplyPopWin.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, {Component} from 'react';
  2. import StorageUtil from '../utils/StorageUtil';
  3. import Utils from '../utils/Utils';
  4. import Base64Utils from '../utils/Base64';
  5. import Toast from '@remobile/react-native-toast';
  6. import {Button, Dimensions, Modal, StyleSheet, TextInput, TouchableOpacity, View} from 'react-native';
  7. const {width} = Dimensions.get('window');
  8. export default class ReplyPopWin extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. show: false,
  13. inputContent: '',
  14. isUpdateUserInfo: false
  15. };
  16. StorageUtil.get('username', (error, object) => {
  17. if (!error && object != null) {
  18. this.setState({username: object.username});
  19. }
  20. });
  21. }
  22. componentDidMount() {
  23. let input = this.refs.textInput;
  24. if (!Utils.isEmpty(input)) {
  25. input.focus();
  26. }
  27. }
  28. render() {
  29. let placeholderText = '';
  30. if (!this.state.isUpdateUserInfo) {
  31. placeholderText = "回复" + this.state.momentUsername;
  32. } else {
  33. placeholderText = "取个中文昵称,当然英文的也没啥问题";
  34. }
  35. return (
  36. <View style={styles.container}>
  37. <Modal transparent={true}
  38. visible={this.state.show}
  39. onRequestClose={() => this.closeModal()}>
  40. <TouchableOpacity onPress={() => this.closeModal()} style={styles.modalContainer}>
  41. <View style={styles.modalContainer}>
  42. <View style={{
  43. flexDirection: 'row',
  44. alignItems: 'center',
  45. backgroundColor: '#EEEEEE',
  46. paddingLeft: 10,
  47. paddingRight: 10
  48. }}>
  49. <TextInput
  50. style={{flex: 1}}
  51. ref="textInput"
  52. placeholder={placeholderText}
  53. autoFocus={true}
  54. onChangeText={(text) => this.setState({inputContent: text})}
  55. />
  56. {
  57. !Utils.isEmpty(this.state.inputContent) ? (
  58. <Button color={'#49BC1C'} title={this.state.isUpdateUserInfo ? "修改" : "回复"}
  59. onPress={() => this.handleBtnClick()}/>
  60. ) : (null)
  61. }
  62. </View>
  63. </View>
  64. </TouchableOpacity>
  65. </Modal>
  66. </View>
  67. );
  68. }
  69. handleBtnClick() {
  70. if (!this.state.isUpdateUserInfo) {
  71. this.sendPost();
  72. } else {
  73. this.updateUserInfo();
  74. }
  75. }
  76. sendPost() {
  77. let momentId = this.state.momentId;
  78. let replyContent = this.state.inputContent;
  79. if (Utils.isEmpty(replyContent)) {
  80. Toast.showShortCenter('回复内容不能为空');
  81. return;
  82. }
  83. replyContent = Base64Utils.encoder(replyContent);
  84. let replyUsername = this.state.username;
  85. let formData = new FormData();
  86. formData.append('momentId', momentId);
  87. formData.append('replyUsername', replyUsername);
  88. formData.append('replyContent', replyContent);
  89. let url = "http://app.yubo725.top/reply";
  90. this.closeModal();
  91. fetch(url, {method: 'POST', body: formData}).then((res) => res.json())
  92. .then((json) => {
  93. if (json != null) {
  94. // 回复成功
  95. if (json.code == 1) {
  96. // 刷新回复列表
  97. this.refreshReply(momentId, json.msg);
  98. } else {
  99. Toast.showShortCenter(json.msg);
  100. }
  101. } else {
  102. Toast.showShortCenter('回复失败');
  103. }
  104. }).catch((e) => {
  105. Toast.showShortCenter(e.toString());
  106. });
  107. }
  108. refreshReply(momentId, data) {
  109. let callback = this.state.successCallback;
  110. if (!Utils.isEmpty(callback)) {
  111. callback(momentId, data);
  112. }
  113. }
  114. closeModal() {
  115. this.setState({show: false})
  116. }
  117. showModal(momentId, momentUsername, successCallback) {
  118. this.setState({
  119. momentId: momentId,
  120. momentUsername: momentUsername,
  121. show: true, successCallback: successCallback,
  122. isUpdateUserInfo: false
  123. });
  124. }
  125. showModalWhenUpdateInfo(contactId, updateCallback) {
  126. this.setState({
  127. show: true,
  128. contactId: contactId,
  129. isUpdateUserInfo: true,
  130. updateCallback: updateCallback
  131. });
  132. }
  133. updateUserInfo() {
  134. let contactId = this.state.contactId;
  135. let newNickName = this.state.inputContent;
  136. if (Utils.isEmpty(newNickName)) {
  137. Toast.showShortCenter('请输入昵称');
  138. return;
  139. }
  140. if (newNickName.length > 8) {
  141. Toast.showShortCenter('昵称要不要取这么长');
  142. return;
  143. }
  144. // 请求服务器修改昵称
  145. if (this.state.updateCallback) {
  146. this.closeModal();
  147. this.state.updateCallback(contactId, newNickName);
  148. }
  149. }
  150. }
  151. const styles = StyleSheet.create({
  152. container: {
  153. width: width
  154. },
  155. modalContainer: {
  156. flex: 1,
  157. flexDirection: 'column',
  158. justifyContent: 'flex-end'
  159. }
  160. });