No Description

QuestionCreateAddImageViewController.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * Created by zack on 2018/5/23.
  3. */
  4. import {
  5. View,
  6. Text,
  7. StyleSheet,
  8. TextInput,
  9. TouchableOpacity,
  10. Platform,
  11. FlatList,
  12. Image,
  13. NativeModules
  14. } from 'react-native'
  15. import React, {Component} from 'react'
  16. import {NavigationBarHeight, TabBarHeight, ScreenDimensions} from '../../../utils/DimensionsTools'
  17. import BaseNavigationBarStyle from '../../base/BaseNavigationBarStyle'
  18. import ImagePicker from 'react-native-image-crop-picker';
  19. import ActionSheet from 'react-native-actionsheet'
  20. import ToastMsg from '../../../utils/ToastMsg'
  21. import HttpTools from '../../../network/HttpTools'
  22. import {auth, question} from '../../../network/API'
  23. import {GlobalMD5EncryptTool} from '../../../utils/GlobalEncryptTool'
  24. const ImageListViewMaxHeight = 320
  25. const ImageListItemWidth = ((ScreenDimensions.width - 56 - 30)/3.0)
  26. const ImageListItemHeight = ((ScreenDimensions.width - 56 - 30)/3.0)
  27. const ImageBaseUrl = 'https://links123-images.oss-cn-hangzhou.aliyuncs.com/'
  28. //test png 0a682be491c4f2938a9c2a927c3bc16f.jpg 76b4fee490acc691f1421e9ed45a2a9b.jpeg
  29. export default class QuestionCreateAddImageViewController extends Component {
  30. static navigatorButtons = {
  31. rightButtons: [
  32. {
  33. title: '发布', // for a textual button, provide the button title (label)
  34. id: 'Send', // id for this button, given in onNavigatorEvent(event) to help understand which button was clicked
  35. disabled: false, // optional, used to disable the button (appears faded and doesn't interact)
  36. buttonColor: '#449edc', // Optional, iOS only. Set color for the button (can also be used in setButtons function to set different button style programatically)
  37. buttonFontSize: 16,
  38. },
  39. ],
  40. }
  41. constructor(props) {
  42. super(props)
  43. this.state = {
  44. imageListViewHeight: ImageListViewMaxHeight,
  45. selectedImagesMap: new Map(),
  46. questionTitle: props.questionTitle,
  47. questionDesc: ''
  48. }
  49. }
  50. componentDidMount() {
  51. this.props.navigator.setOnNavigatorEvent(event => {
  52. if (event.type === 'NavBarButtonPress') {
  53. if (event.id === 'Send') {
  54. // this.postQuestion()
  55. if (Platform.OS === 'ios') {
  56. this.props.navigator.dismissAllModals()
  57. }else {
  58. this.props.navigator.popToRoot()
  59. }
  60. }else if (event.id === 'cancel'){
  61. this.props.navigator.dismissModal()
  62. }
  63. }
  64. })
  65. }
  66. postQuestion() {
  67. let imageNames = []
  68. let imageBase64Strings = []
  69. for (let [url, imageInfo] of this.state.selectedImagesMap.entries()) {
  70. let urlStatementArrays = url.split('/')
  71. if (urlStatementArrays.length) {
  72. let fileFullName = urlStatementArrays[urlStatementArrays.length - 1]
  73. //MD5(filename + userID )
  74. let fileFullNameArray = fileFullName.split('.')
  75. let fileName = fileFullNameArray[0]
  76. let fileExtension = fileFullNameArray[1]
  77. let userId = "zack" //以后会使用USER id替代
  78. let timeStamp = (new Date()).getTime()
  79. let imageName = GlobalMD5EncryptTool.encrypt(userId + fileName + timeStamp + '')
  80. imageNames.push(imageName + '.' + fileExtension)
  81. imageBase64Strings.push(imageInfo.data)
  82. }
  83. }
  84. if (Platform.OS === 'ios') {
  85. const aliOSSModule = NativeModules.AliOSSModule
  86. aliOSSModule.uploadImages(imageNames, imageBase64Strings, (state) => {
  87. // this.createNewQuestion(this.state.questionTitle, this.state.questionDesc, imageNames)
  88. })
  89. }else {
  90. //beginTask
  91. const aliOSSModule = NativeModules.AliOSSModule
  92. aliOSSModule.uploadImages(imageNames, imageBase64Strings, (state) => {
  93. alert(state)
  94. // this.createNewQuestion(this.state.questionTitle, this.state.questionDesc, imageNames)
  95. })
  96. }
  97. }
  98. androidUploadImage(imageName, imageBase64Strings) {
  99. const aliOSSModule = NativeModules.AliOSSModule
  100. aliOSSModule.uploadImage(imageNames[0], imageBase64Strings[0], (taksIndex)=> {
  101. } )
  102. }
  103. createNewQuestion(questionTitle, questionDesc, imageUrls) {
  104. const param = JSON.stringify({
  105. title: questionTitle,
  106. img_urls: imageUrls,
  107. content: questionDesc
  108. })
  109. HttpTools.post(question, param, (response) => {
  110. console.log(response)
  111. this.props.navigator.dismissAllModals(); //回到首页
  112. }, (error) => {
  113. console.log(error)
  114. })
  115. }
  116. goToGallery() {
  117. ImagePicker.openPicker({
  118. multiple: true,
  119. includeBase64: true,
  120. mediaType: 'photo',
  121. writeTempFile: false,
  122. }).then(images => {
  123. const imageMap = this.state.selectedImagesMap
  124. for(let i = 0; i < images.length; i ++) {
  125. const url = Platform.OS === 'ios' ? images[i].sourceURL : images[i].path
  126. imageMap.set(url, images[i])
  127. }
  128. if (imageMap.size > 9) {
  129. ToastMsg.show("最多还可以添加" + images.length - imageMap.size + '张图片!')
  130. // return
  131. }
  132. this.setState({selectedImagesMap: imageMap})
  133. });
  134. }
  135. openCamera() {
  136. ImagePicker.openCamera({
  137. width: 300,
  138. height: 400,
  139. cropping: true,
  140. includeBase64: true
  141. }).then(image => {
  142. const imageMap = this.state.selectedImagesMap
  143. const url = Platform.OS === 'ios' ? image.sourceURL : image.path
  144. imageMap.set(url, image)
  145. this.setState({selectedImagesMap: imageMap})
  146. });
  147. }
  148. deleteSelectedImage(key) {
  149. let selectedImageSources = this.state.selectedImagesMap
  150. selectedImageSources.delete(key)
  151. this.setState({selectedImagesMap: selectedImageSources})
  152. }
  153. showActionSheet() {
  154. this.ActionSheet.show()
  155. }
  156. actionSheetClickedIndex(index) {
  157. if (index === 1) {
  158. this.openCamera()
  159. }else if (index === 2) {
  160. this.goToGallery()
  161. }
  162. }
  163. renderImageBgItem(rows) {
  164. let selectedImageSources = []
  165. let imageItems = []
  166. for (let [key, value] of this.state.selectedImagesMap.entries()) {
  167. selectedImageSources.push(value)
  168. }
  169. for (let i = 0; i < selectedImageSources.length; i ++) {
  170. const imageItem = this.renderImageItem(selectedImageSources[i], i)
  171. imageItems.push(imageItem)
  172. }
  173. //还需要添加一个add 图片的按钮,根据数量决定是否正确显示
  174. if (selectedImageSources.length < 9 ) {
  175. let addButtonItem = this.renderAddButton()
  176. imageItems.push(addButtonItem)
  177. }
  178. return(
  179. <View style={{backgroundColor: 'white', width: ScreenDimensions.width, flexDirection: 'row', flexWrap: 'wrap'}}>
  180. {imageItems}
  181. </View>
  182. )
  183. }
  184. renderImageItem(imageItem, i) {
  185. let marginLeft = 0
  186. if (i%3 === 0) {
  187. marginLeft = 28
  188. }else {
  189. marginLeft = 15
  190. }
  191. let marginRight = 0
  192. if (i%3 === 2) {
  193. marginRight = 28
  194. }else {
  195. marginRight = 0
  196. }
  197. let marginTop = 0
  198. if (i <= 2) {
  199. marginTop = 0
  200. }else {
  201. marginTop = 15
  202. }
  203. return (
  204. <View key = {i} style={{width: ImageListItemWidth, height: ImageListItemHeight, backgroundColor: 'white',
  205. marginLeft: marginLeft, marginRight: marginRight, marginTop: marginTop, marginBottom: 5}}>
  206. <Image
  207. source={{uri: imageItem.path}}
  208. style={{width: ImageListItemWidth, height: ImageListItemHeight}}/>
  209. <TouchableOpacity onPress={() => {
  210. const key = Platform.OS === 'ios' ? imageItem.sourceURL : imageItem.path
  211. this.deleteSelectedImage(key)
  212. }} style={{width: 21, height: 21, position: 'absolute', left: 10, top: 10, justifyContent: 'center', alignItems: 'center'}}>
  213. <Image source={require('../../../resources/images/TabBar/Community/Answer/close.png')}/>
  214. </TouchableOpacity>
  215. </View>
  216. )
  217. }
  218. renderAddButton() {
  219. return (
  220. <TouchableOpacity onPress={() => {
  221. this.showActionSheet()
  222. }} key = {9} style={{width: ImageListItemWidth, height: ImageListItemHeight, backgroundColor: 'white',
  223. marginLeft: (this.state.selectedImagesMap.size%6 === 0 ? 28 : 15), marginRight: 0, marginTop: (this.state.selectedImagesMap.size <=2 ? 0 : 15),
  224. marginBottom: 5}}>
  225. <Image
  226. source={require('../../../resources/images/TabBar/Community/Answer/add.png')}
  227. style={{width: ImageListItemWidth, height: ImageListItemHeight}}/>
  228. </TouchableOpacity>
  229. )
  230. }
  231. render() {
  232. return (
  233. <View style={styles.View}>
  234. <Text numberOfLines={2} style={styles.TitleText}>{this.state.questionTitle}</Text>
  235. <TextInput underlineColorAndroid={'transparent'} multiline={true} style={styles.TextInput} placeholderTextColor={'rgba(28, 28, 28, 0.18)'} placeholder={'添加描述和配图(选填)'}/>
  236. <View style={[styles.ImageListView,{height: this.state.imageListViewHeight, backgroundColor: 'white'} ]}>
  237. <FlatList
  238. data = {[['a',]]}
  239. renderItem={(item) => this.renderImageBgItem(item)}
  240. keyboardDismissMode = 'on-drag'
  241. keyExtractor = {(item,index) =>{
  242. return 'key' + item.key + index
  243. }}
  244. />
  245. </View>
  246. <ActionSheet
  247. ref={o => this.ActionSheet = o}
  248. title={'Choose images to send.'}
  249. options={['Cancel', 'Take a photo', 'Select from album']}
  250. cancelButtonIndex={0}
  251. destructiveButtonIndex={-1}
  252. onPress={(index) => this.actionSheetClickedIndex(index)}
  253. />
  254. </View>
  255. );
  256. }
  257. }
  258. const styles = StyleSheet.create({
  259. View: {
  260. width: ScreenDimensions.width,
  261. height: ScreenDimensions.height,
  262. backgroundColor: 'white'
  263. },
  264. TitleText: {
  265. fontSize: 17,
  266. color: '#4A4A4A',
  267. marginLeft: 28,
  268. marginTop: NavigationBarHeight.height + 57,
  269. width: ScreenDimensions.width - 28 - 10,
  270. textAlignVertical: 'top'
  271. },
  272. TextInput: {
  273. width: ScreenDimensions.width - 56,
  274. fontSize: 16,
  275. color: '#4A4A4A',
  276. marginLeft: 28,
  277. marginTop: 42,
  278. height: 44,
  279. paddingLeft: 0,
  280. paddingTop: 0,
  281. paddingRight: 0,
  282. paddingBottom: 0
  283. },
  284. ImageListView: {
  285. width: ScreenDimensions.width,
  286. backgroundColor: '#efeff4',
  287. marginTop: 32,
  288. },
  289. })