OrderScreen.tsx 7.5 KB

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