OrderScreen.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { StackScreenProps } from '@react-navigation/stack';
  2. import * as React from 'react';
  3. import { RefreshControl } from 'react-native';
  4. import { useFocusEffect } from '@react-navigation/native';
  5. import { Div, Button, Image, Text, Avatar, Icon } from 'react-native-magnus';
  6. import { FlatList } from 'react-native-gesture-handler';
  7. import Constants from 'expo-constants';
  8. import TimScreen from '../notice/TimScreen';
  9. import useModel from 'flooks';
  10. import Map from '../map/model';
  11. import OrderModel from './model';
  12. import User from '../stores/User';
  13. import { useMount, useRequest, useCreation } from 'ahooks';
  14. import { useTranslation } from 'react-i18next';
  15. import moment from 'moment';
  16. import { promot } from '../utils/SystemUtils';
  17. import { orderRiderStatus, RiderStatusMap } from '../utils/RiderInfoUtils';
  18. import request from '../utils/RequestUtils';
  19. import { toastSuccess, alert } from '../utils/SystemUtils';
  20. import { goMap, changeCord } from '../utils/MapUtils';
  21. import OrderCom from './OrderCom';
  22. export default function OrderScreen({ navigation }: StackScreenProps) {
  23. const { t } = useTranslation();
  24. const { locationInfo, getNowLocation } = useModel(Map, ['locationInfo']);
  25. const { receiverOrder, changeStatus, changeStatusAll } = useModel(
  26. OrderModel,
  27. []
  28. );
  29. const { getSignInfo, sign, riderOrder } = useModel(User, [
  30. 'getSignInfo',
  31. 'sign',
  32. ]);
  33. const [chooseStatus, setChooseStatus] = React.useState<string>(
  34. 'NOT_RECEIVED'
  35. );
  36. useMount(() => {
  37. getNowLocation();
  38. getSignInfo();
  39. });
  40. const chooseStatusInfo = useCreation(() => {
  41. return orderRiderStatus.get(chooseStatus);
  42. }, [chooseStatus]);
  43. useFocusEffect(
  44. React.useCallback(() => {
  45. run();
  46. }, [])
  47. );
  48. const { data, loading, reload, run } = useRequest(
  49. () => {
  50. return request.get(chooseStatusInfo.requestUrl, {
  51. params: chooseStatusInfo.params,
  52. });
  53. },
  54. {
  55. manual: false,
  56. formatResult: chooseStatusInfo.formatResult,
  57. defaultLoading: false,
  58. debounceInterval: 1000,
  59. initialData: [],
  60. }
  61. );
  62. const showData = useCreation(() => {
  63. if (chooseStatusInfo.status) {
  64. return data.filter((item) => {
  65. return chooseStatusInfo.status.indexOf(item.riderStatus) !== -1;
  66. });
  67. } else {
  68. return data;
  69. }
  70. }, [data, chooseStatusInfo]);
  71. return (
  72. <Div bg="gray100" flex={1}>
  73. <Div
  74. pt={Constants.statusBarHeight + 11}
  75. pb={11}
  76. bg="yellow500"
  77. row
  78. alignItems="center"
  79. px={14}
  80. px={15}
  81. >
  82. {/* <Image w={50} h={50} source={require('../assets/images/logo.png')} /> */}
  83. <Button
  84. bg="transparent"
  85. onPress={() => {
  86. if (sign) {
  87. alert(navigation, {
  88. msg: '确定要休息吗?',
  89. submitEvent: riderOrder,
  90. hasCancel: true,
  91. });
  92. } else {
  93. riderOrder();
  94. }
  95. }}
  96. >
  97. <Div alignItems="center">
  98. <Icon
  99. fontSize="6xl"
  100. color="white"
  101. fontFamily="MaterialCommunityIcons"
  102. name={sign ? 'coffee' : 'motorbike'}
  103. />
  104. <Text fontSize="sm" color="white" fontWeight="bold">
  105. {sign ? '休息' : '开工'}
  106. </Text>
  107. </Div>
  108. </Button>
  109. <Text fontSize="xl" color="white" mr={50} textAlign="center" flex={1}>
  110. 骑手客户端
  111. </Text>
  112. </Div>
  113. <Div row>
  114. {[...orderRiderStatus.keys()].map((item) => {
  115. const info = orderRiderStatus.get(item);
  116. return (
  117. <Button
  118. key={item}
  119. flex={1}
  120. bg="transparent"
  121. color={chooseStatus === item ? 'yellow500' : 'black'}
  122. fontSize="lg"
  123. py={15}
  124. onPress={() => {
  125. setChooseStatus(item);
  126. run();
  127. }}
  128. >
  129. {t(info.name)}
  130. </Button>
  131. );
  132. })}
  133. </Div>
  134. <FlatList
  135. renderItem={({ item }) => {
  136. return (
  137. <OrderCom
  138. sign={sign}
  139. info={item}
  140. type={chooseStatusInfo.type}
  141. goDetail={() =>
  142. navigation.navigate('OrderStack', {
  143. screen: 'OrderDetail',
  144. params: {
  145. orderId: item.id,
  146. },
  147. })
  148. }
  149. receiverOrder={() => {
  150. receiverOrder(item.id, (res) => {
  151. toastSuccess(t('jie-dan-cheng-gong'));
  152. });
  153. }}
  154. goMap={() => {
  155. const orderStatusInfo = RiderStatusMap.get(item.riderStatus);
  156. goMap(
  157. orderStatusInfo.type === 'merchant'
  158. ? item.merShowName
  159. : item.userAddress,
  160. orderStatusInfo.type === 'merchant'
  161. ? changeCord(item.merLocation)
  162. : changeCord(item.location),
  163. navigation
  164. );
  165. }}
  166. changeStatus={() => {
  167. const orderStatusInfo = RiderStatusMap.get(item.riderStatus);
  168. alert(navigation, {
  169. msg: orderStatusInfo.infoText,
  170. hasCancel: true,
  171. dangers: true,
  172. submitEvent: () => {
  173. changeStatus(
  174. item.id,
  175. orderStatusInfo.nextStatus,
  176. (res) => {
  177. toastSuccess(orderStatusInfo.successText);
  178. },
  179. () => {
  180. alert(navigation, {
  181. msg: orderStatusInfo.errorText,
  182. hasCancel: true,
  183. dangers: true,
  184. submitEvent: () => {
  185. changeStatusAll(
  186. item.id,
  187. orderStatusInfo.nextStatus,
  188. (res) => {
  189. toastSuccess(orderStatusInfo.successText);
  190. run();
  191. }
  192. );
  193. },
  194. });
  195. }
  196. );
  197. },
  198. });
  199. }}
  200. />
  201. );
  202. }}
  203. data={showData}
  204. contentContainerStyle={{
  205. flexGrow: 1,
  206. paddingHorizontal: 15,
  207. backgroundColor: '#f2f2f2',
  208. }}
  209. refreshControl={
  210. <RefreshControl refreshing={loading} onRefresh={reload} />
  211. }
  212. ListEmptyComponent={() => {
  213. if (!loading) {
  214. return (
  215. <Text color="gary200" fontSize="sm" textAlign="center" py={20}>
  216. {t('zan-wu-shu-ju')}
  217. </Text>
  218. );
  219. } else {
  220. return <></>;
  221. }
  222. }}
  223. extraData={sign}
  224. />
  225. <Div position="absolute" w={0} h={0} bottom={0} left={0} zIndex={0}>
  226. <TimScreen />
  227. </Div>
  228. </Div>
  229. );
  230. }