MomentMenuView.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import React, {Component} from 'react';
  2. import StorageUtil from '../utils/StorageUtil';
  3. import Utils from '../utils/Utils';
  4. import Toast from '@remobile/react-native-toast';
  5. import {
  6. Dimensions,
  7. Image,
  8. Modal,
  9. PixelRatio,
  10. StyleSheet,
  11. Text,
  12. TouchableOpacity,
  13. View
  14. } from 'react-native';
  15. const {width, height} = Dimensions.get('window');
  16. export default class MomentMenuView extends Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. show: false,
  21. pageX: 0,
  22. pageY: 0,
  23. height: 0
  24. };
  25. StorageUtil.get('username', (error, object) => {
  26. if (!error && object != null) {
  27. this.setState({username: object.username});
  28. }
  29. });
  30. }
  31. render() {
  32. return (
  33. <View style={[styles.modalContainer, {height: this.state.height}]}>
  34. <Modal transparent={true}
  35. visible={this.state.show}
  36. onRequestClose={() => this.closeModal()}>
  37. <TouchableOpacity style={[styles.modalContainer, {height: this.state.height}]}
  38. onPress={() => this.closeModal()}>
  39. <View style={[styles.container, {left: this.state.pageX - 200, top: this.state.pageY - 20}]}>
  40. <TouchableOpacity onPress={() => this.doFavor()}>
  41. <View style={styles.menuItemContainer}>
  42. <Image style={styles.menuItemImg} source={require('../../images/ic_moment_favor.png')}/>
  43. <Text style={styles.menuItemText}> 赞 </Text>
  44. </View>
  45. </TouchableOpacity>
  46. <View style={styles.divider}/>
  47. <TouchableOpacity onPress={() => this.doComment()}>
  48. <View style={styles.menuItemContainer}>
  49. <Image style={styles.menuItemImg} source={require('../../images/ic_moment_comment.png')}/>
  50. <Text style={styles.menuItemText}>评论</Text>
  51. </View>
  52. </TouchableOpacity>
  53. </View>
  54. </TouchableOpacity>
  55. </Modal>
  56. </View>
  57. );
  58. }
  59. doComment() {
  60. let callback2 = this.state.callback2;
  61. if (!Utils.isEmpty(callback2)) {
  62. callback2(this.state.momentId, this.state.momentUsername);
  63. }
  64. this.closeModal();
  65. }
  66. doFavor() {
  67. let momentId = this.state.momentId;
  68. if (!Utils.isEmpty(momentId)) {
  69. let url = 'http://app.yubo725.top/favor';
  70. let username = this.state.username;
  71. let formData = new FormData();
  72. formData.append('username', username);
  73. formData.append('momentId', momentId);
  74. fetch(url, {method: 'POST', body: formData}).then((res) => res.json())
  75. .then((json) => {
  76. if (!Utils.isEmpty(json)) {
  77. if (json.code == 1) {
  78. // 需要刷新页面
  79. let callback1 = this.state.callback1;
  80. if (!Utils.isEmpty(callback1)) {
  81. callback1(json.msg);
  82. }
  83. } else {
  84. Toast.showShortCenter(json.msg);
  85. }
  86. }
  87. }).catch((e) => {
  88. Toast.showShortCenter(e.toString());
  89. })
  90. }
  91. this.closeModal();
  92. }
  93. closeModal() {
  94. this.setState({show: false, height: 0});
  95. }
  96. showModal(pageX, pageY, momentId, momentUsername, callback1, callback2) {
  97. this.setState({
  98. pageX: pageX,
  99. pageY: pageY,
  100. height: height,
  101. show: true,
  102. momentId: momentId,
  103. momentUsername: momentUsername,
  104. callback1: callback1,
  105. callback2: callback2
  106. });
  107. }
  108. }
  109. const styles = StyleSheet.create({
  110. modalContainer: {
  111. width: width,
  112. },
  113. container: {
  114. backgroundColor: '#393A3E',
  115. borderRadius: 2,
  116. flexDirection: 'row',
  117. justifyContent: 'center',
  118. alignItems: 'center',
  119. width: 180,
  120. padding: 5,
  121. height: 35,
  122. position: 'absolute',
  123. left: 230,
  124. top: 500
  125. },
  126. divider: {
  127. width: 1 / PixelRatio.get(),
  128. marginLeft: 20,
  129. marginRight: 20,
  130. height: 25,
  131. backgroundColor: '#DDDDDD'
  132. },
  133. menuItemContainer: {
  134. flex: 1,
  135. flexDirection: 'row',
  136. alignItems: 'center',
  137. justifyContent: 'center',
  138. },
  139. menuItemImg: {
  140. width: 20,
  141. height: 20,
  142. marginTop: 10,
  143. marginBottom: 10,
  144. },
  145. menuItemText: {
  146. color: '#FFFFFF',
  147. fontSize: 15
  148. }
  149. })