addRoom.jsx 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import withLayout from '@/layouts'
  2. import Taro from '@tarojs/taro'
  3. import CustomNav from '@/components/CustomNav'
  4. import { View, Input, Button, Label, Textarea, Text } from '@tarojs/components';
  5. import { useEffect, useState } from "react"
  6. import { saveRoom, getRoomDetail, updateRoom } from '@/services/landlord'
  7. import { getExtendContent } from "@/services/home";
  8. import { saveExtend } from '@/services/landlord'
  9. //export default是导出模块的 export也是导出模块的 如果引用时不加{}就是引用默认模块加了就是引用相应的模块
  10. import { uploadFile } from '@/utils/request'
  11. import Popup from '@/components/Popup'
  12. import Extend from "../../components/Extend";
  13. import './addRoom.less'
  14. export default withLayout((props) => {
  15. const { hotelId, hotelName, roomId } = props.router.params
  16. const [roomModel, setRoomModel] = useState({
  17. hotelId,
  18. roomName: '',
  19. address: '',
  20. location: '',
  21. parkingAddress: '',
  22. parkingLocation: '',
  23. wifiName: '',
  24. wifiPassword: '',
  25. weight: '',
  26. })
  27. const [paddress, setpAddress] = useState()
  28. const [wifiName, setwifiName] = useState()
  29. const onRoomMap = () => {
  30. Taro.chooseLocation().then((res) => {
  31. setRoomModel({ ...roomModel, location: res.longitude + ',' + res.latitude })
  32. })
  33. }
  34. const onParkMap = () => {
  35. Taro.chooseLocation().then((res) => {
  36. setRoomModel({ ...roomModel, parkingLocation: res.longitude + ',' + res.latitude })
  37. })
  38. }
  39. const sumbit = () => {
  40. if (
  41. roomModel.roomName != '' &&
  42. roomModel.address != '' &&
  43. roomModel.location != '' &&
  44. roomModel.weight != ''
  45. ) {
  46. const seveices = roomId ? updateRoom : saveRoom
  47. seveices(roomModel, roomId).then(() => {
  48. Taro.showModal({
  49. title: roomId ? '修改成功' : '保存成功',
  50. showCancel: false
  51. }).then(() => {
  52. Taro.navigateBack()
  53. })
  54. })
  55. } else {
  56. Taro.showToast({
  57. title: '有必填项未填哦',
  58. icon: 'none',
  59. })
  60. }
  61. }
  62. const [extend, setExtend] = useState([]);
  63. useEffect(() => {
  64. if (roomId) {
  65. getRoomDetail(roomId).then((res) => {
  66. setRoomModel(res)
  67. setpAddress(res.parkingAddress)
  68. setwifiName(res.wifiName)
  69. })
  70. }
  71. }, [roomId])//这个地方写这个单词的目的是 因为编辑时roomId有可能还没加载成功
  72. const [reset, setReset] = useState(false)
  73. useEffect(() => {
  74. if (roomId) {
  75. getExtendContent('room', roomId, { pageSize: 999 }).then((res) => {
  76. setExtend(res.records || []);
  77. })
  78. setReset(false)
  79. }
  80. }, [reset])
  81. //新增文字
  82. const [showCutover, setShowCutover] = useState(false)
  83. const [cont, setcont] = useState('')
  84. const onClose = () => {
  85. setShowCutover(false)
  86. }
  87. //判断是否只有空 空格 回车
  88. const javaTrim = (str) => {
  89. for (var i = 0; (str.charAt(i) == ' ') && i < str.length; i++);
  90. if (i == str.length) return ''; //whole string is space
  91. var newstr = str.substr(i);
  92. for (var i = newstr.length - 1; newstr.charAt(i) == ' ' && i >= 0; i--);
  93. newstr = newstr.substr(0, i + 1);
  94. return newstr;
  95. }
  96. const handelAdd = () => {
  97. var content = document.getElementById('cont').value
  98. content = content.replace(/\n/g, '');
  99. if (javaTrim(content) == '') {
  100. Taro.showToast({
  101. title: '您还没有添加文字哦',
  102. icon: 'none'
  103. })
  104. return;
  105. }
  106. const date = {
  107. targetId: roomId,
  108. targetType: 'room',
  109. content,
  110. contentType: 'text'
  111. }
  112. saveExtend(date)
  113. setcont('')
  114. onClose()
  115. setReset(!reset)
  116. }
  117. const showText = () => {
  118. setShowCutover(true)
  119. setcont('')
  120. }
  121. const addImage = () => {
  122. Taro.chooseImage({
  123. count: 1,
  124. success: function (res) {
  125. uploadFile(res.tempFilePaths[0]).then((res2) => {
  126. const date = {
  127. targetId: roomId,
  128. targetType: 'room',
  129. content: res2,
  130. contentType: 'image'
  131. }
  132. saveExtend(date)
  133. setReset(!reset)
  134. })
  135. }
  136. })
  137. }
  138. const isError = (s) => {
  139. if (!s) { return true }
  140. else {
  141. // 根据,(逗号)进行分割
  142. let split = s.split(",");
  143. if (split.length != 2) {
  144. return true
  145. } else {
  146. if (!split[0] || !split[1]) {
  147. return true
  148. }
  149. }
  150. }
  151. return false
  152. }
  153. return (
  154. <view className='page-index'>
  155. <view className="index-navbar">
  156. <CustomNav title={hotelName} />
  157. </view>
  158. <view className='roomDetail' style={{ height: '100%', overflow: "hidden", }} >
  159. <scroll-view scrollY style={{ height: '100%' }}>
  160. <View id='det'>
  161. <Popup show={showCutover} onClose={onClose}>
  162. <View className='editword'>
  163. <View style={{ marginBottom: '25px' }}>
  164. <View className='rzline' /><Label className='srl mg'>新增文字</Label><View className='rzline' />
  165. </View>
  166. <View>
  167. <Textarea className='storezn' id='cont' value={cont} placeholder='请输入准备新增的文字' />
  168. </View>
  169. <View>
  170. <Button className='cancel' onClick={onClose}>取消</Button>
  171. <Button className='btn' onClick={handelAdd}>确认增加</Button>
  172. </View>
  173. </View>
  174. </Popup>
  175. </View>
  176. <mp-form models={roomModel} >
  177. <mp-cells title='房屋名称' footer='' ext-class='cells' >
  178. <mp-cell>
  179. <Input style={{ fontWeight: 'bold' }} onInput={(e) => setRoomModel({ ...roomModel, roomName: e.detail.value })} value={roomModel.roomName} placeholder='请输入房屋名称(必填)' />
  180. </mp-cell>
  181. </mp-cells>
  182. <mp-cells title='房屋位置'>
  183. <mp-cell>
  184. <Input style={{ color: '#000', fontWeight: 'bold', marginBottom: '13px' }} onInput={(e) => setRoomModel({ ...roomModel, address: e.detail.value })} value={roomModel.address} placeholder='请输入房屋位置(必填)' />
  185. <Label style={{ color: '#666' }} onClick={onRoomMap}>{isError(roomModel.location) ? '房间定位(必填)' : roomModel.location}</Label>
  186. </mp-cell>
  187. </mp-cells>
  188. {
  189. paddress != '' ?
  190. <mp-cells title='停车场位置'>
  191. <mp-cell>
  192. <Input onInput={(e) => setRoomModel({ ...roomModel, parkingAddress: e.detail.value })} value={roomModel.parkingAddress} placeholder='请输入停车场位置' />
  193. <Label style={{ color: '#666', lineHeight: '30px' }} onClick={onParkMap}>{isError(roomModel.parkingLocation) ? '停车场定位' : roomModel.parkingLocation}</Label>
  194. </mp-cell>
  195. </mp-cells> : null
  196. }
  197. {
  198. wifiName != '' ?
  199. <mp-cells title='WiFi信息'>
  200. <mp-cell>
  201. <View style={{ color: '#666' }}>
  202. <Input style={{ display: 'inline-block', background: '#fff', width: '20%' }} value='名称:' disabled /><Input style={{ display: 'inline-block', width: '80%' }} onInput={(e) => setRoomModel({ ...roomModel, wifiName: e.detail.value })} value={roomModel.wifiName} placeholder='请输入wifi名称' />
  203. <Input style={{ display: 'inline-block', background: '#fff', width: '20%' }} value='密码:' disabled /><Input style={{ display: 'inline-block', width: '80%' }} onInput={(e) => setRoomModel({ ...roomModel, wifiPassword: e.detail.value })} value={roomModel.wifiPassword} placeholder='请输入wifi密码' />
  204. </View>
  205. </mp-cell>
  206. </mp-cells> : null
  207. }
  208. <mp-cells title='权重'>
  209. <mp-cell>
  210. <input type='number' value={roomModel.weight} onInput={(e) => setRoomModel({ ...roomModel, weight: e.detail.value })} placeholder='请输入权重(必填)' />
  211. </mp-cell>
  212. </mp-cells>
  213. {roomId ? <>
  214. <view>
  215. <view className='weui-cells__title'>其他指引</view>
  216. {
  217. extend == '' ? null :
  218. <view>
  219. {extend.map((item) => (
  220. <Extend key={item.extId} item={item} setReset={setReset} />
  221. ))}
  222. </view>
  223. }
  224. </view>
  225. </> : null
  226. }
  227. </mp-form>
  228. {roomId ? <>
  229. <View className='adds' >
  230. <Text className='zuobian'>增加文字</Text>
  231. <View className='add' onClick={showText}><Text className='jiahao'>+</Text><Text className='zengjia'>添加</Text></View>
  232. </View>
  233. <View className='adds' >
  234. <Text className='zuobian'>增加图片</Text>
  235. <View className='add' onClick={addImage}><Text className='jiahao'>+</Text><Text className='zengjia'>添加</Text></View>
  236. </View>
  237. </> : null
  238. }
  239. <Button className='button-OK' onClick={sumbit}>确定</Button>
  240. </scroll-view>
  241. </view>
  242. </view>
  243. )
  244. })