Guide1Screen.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* eslint-disable react/jsx-one-expression-per-line */
  2. /* eslint-disable no-shadow */
  3. /* eslint-disable camelcase */
  4. import * as WebBrowser from "expo-web-browser";
  5. import * as React from "react";
  6. import { StyleSheet } from "react-native";
  7. import { Layout, Text, useTheme, Button, Card } from "@ui-kitten/components";
  8. import { useModel } from "flooks";
  9. import { useFocusEffect, useRoute } from "@react-navigation/native";
  10. import moment from "moment";
  11. import Textarea from "react-native-textarea";
  12. import ScrollPage from "../components/ScrollPage";
  13. import FormInput from "../components/FormInput";
  14. import ConnectButton from "../components/ConnectButton";
  15. import GuideHeaderBar from "../components/GuideHeaderBar";
  16. import NavHeaderBar from "../components/NavHeaderBar";
  17. const styles = StyleSheet.create({
  18. container: {
  19. flex: 1,
  20. paddingBottom: 33,
  21. },
  22. tabContent: {
  23. backgroundColor: "#fff",
  24. marginTop: 20,
  25. },
  26. img: {
  27. width: 100,
  28. height: 100,
  29. alignSelf: "center",
  30. },
  31. img2: {
  32. width: 97,
  33. height: 21,
  34. alignSelf: "center",
  35. marginTop: 2,
  36. },
  37. text: {
  38. marginTop: 16,
  39. },
  40. layoutLeft: {
  41. // flexDirection: "row",
  42. paddingVertical: 10,
  43. justifyContent: "center",
  44. alignItems: "center",
  45. },
  46. form: {
  47. paddingHorizontal: 26,
  48. paddingVertical: 20,
  49. },
  50. textareaContainer: {
  51. backgroundColor: "#F0F0F0",
  52. height: 100,
  53. borderRadius: 4,
  54. },
  55. textarea: {
  56. textAlignVertical: "top", // hack android
  57. fontSize: 13,
  58. color: "#333",
  59. paddingHorizontal: 14,
  60. paddingVertical: 10,
  61. height: 100,
  62. },
  63. lay: {
  64. flexDirection: "row",
  65. marginLeft: 120,
  66. marginBottom: 10,
  67. },
  68. money: {
  69. marginLeft: 10,
  70. },
  71. });
  72. export default function Guide1Screen({ navigation }) {
  73. const theme = useTheme();
  74. const { changeBackground } = useModel("barModel");
  75. const { httpGet, httpPost } = useModel("httpModel", true);
  76. const { success, loading } = useModel("loadingModel", true);
  77. const { mid, changeGuideStep } = useModel("userModel", true);
  78. const { addClassification, sortClassification } = useModel(
  79. "goodsModel",
  80. true
  81. );
  82. const {
  83. guide1_title1,
  84. guideHome_title2,
  85. guide1_form_1,
  86. guide1_pla_1,
  87. guide1_form_2,
  88. guide1_pla_2,
  89. guide1_form_3,
  90. guide1_pla_3,
  91. guide1_form_4,
  92. guide1_pla_4,
  93. guide1_form_5,
  94. guide1_pla_5,
  95. next,
  96. pass,
  97. passTips,
  98. confirm,
  99. addGoods,
  100. delText,
  101. removeTips,
  102. } = useModel("wordsModel");
  103. const { showDialog } = useModel("dialogModel");
  104. const route = useRoute();
  105. const [routeName, setRouteName] = React.useState("");
  106. const [id, changeId] = React.useState("");
  107. const [name, changeName] = React.useState("");
  108. const [amount, changeAmount] = React.useState("");
  109. const [discountAmount, changeDiscountAmount] = React.useState("");
  110. const [inventory, changeInventory] = React.useState(0);
  111. const [week, changeWeek] = React.useState("");
  112. const [startTime, changeStartTime] = React.useState("08:00");
  113. const [endTime, changeEndTime] = React.useState("22:00");
  114. const [introduction, changeIntroduction] = React.useState();
  115. const [defaultValue, ChangeDefaultValue] = React.useState("");
  116. const [goodsSpecification, changeGoodsSpecification] = React.useState([]);
  117. const [img, changeImg] = React.useState();
  118. function getInfo(res) {
  119. changeName(res.name || "");
  120. if (res.discountAmount) {
  121. changeDiscountAmount(res.discountAmount.toString());
  122. }
  123. if (res.amount) {
  124. changeAmount(res.amount.toString());
  125. }
  126. if (res.inventory) {
  127. changeInventory(res.inventory.toString());
  128. }
  129. changeWeek(res.week || "");
  130. changeStartTime(res.startTime || "");
  131. changeEndTime(res.endTime || "");
  132. changeIntroduction(res.introduction || "");
  133. // if (res.img && res.img.length) {
  134. changeImg(res.img);
  135. // }
  136. if (res.week) {
  137. ChangeDefaultValue([
  138. `${moment(res.startTime, "HH:mm:ss").format("HH:mm")}~${moment(
  139. res.endTime,
  140. "HH:mm:ss"
  141. ).format("HH:mm")}`,
  142. res.week,
  143. ]);
  144. }
  145. }
  146. useFocusEffect(
  147. React.useCallback(() => {
  148. console.log(route);
  149. changeBackground(theme["color-primary-500"]);
  150. const { params, name } = route;
  151. setRouteName(name);
  152. if (params) {
  153. if (params.id) {
  154. changeId(params.id);
  155. loading();
  156. httpGet(`/goods/get/${params.id}`).then(res => {
  157. getInfo(res);
  158. });
  159. httpGet(
  160. "/goodsSpecification/byGoodsId",
  161. {
  162. goodsId: params.id,
  163. },
  164. true
  165. ).then(res => {
  166. changeGoodsSpecification(sortClassification(res));
  167. });
  168. }
  169. } else {
  170. getInfo({});
  171. }
  172. }, [])
  173. );
  174. const specify = () => {
  175. return goodsSpecification.map(item => {
  176. return (
  177. <Layout key={item.id} style={styles.lay}>
  178. {item.parent ? (
  179. <Text category="h1">
  180. 添加
  181. {item.name}
  182. </Text>
  183. ) : (
  184. <Text category="c1">
  185. 分类名:
  186. {item.name}
  187. </Text>
  188. )}
  189. <Layout style={styles.money}>
  190. {item.parent ? (
  191. <Text category="h1">¥{item.amount}</Text>
  192. ) : (
  193. <Text category="c1">{item.multiple ? "多选" : "单选"}</Text>
  194. )}
  195. </Layout>
  196. </Layout>
  197. );
  198. });
  199. };
  200. const canNext = React.useMemo(() => {
  201. if (name && amount && inventory && week && startTime && endTime && img) {
  202. return true;
  203. }
  204. return false;
  205. }, [
  206. name,
  207. amount,
  208. discountAmount,
  209. inventory,
  210. week,
  211. startTime,
  212. endTime,
  213. introduction,
  214. img,
  215. ]);
  216. return (
  217. <>
  218. {routeName !== "AddGoods" ? (
  219. <GuideHeaderBar />
  220. ) : (
  221. <NavHeaderBar title={addGoods} />
  222. )}
  223. <ScrollPage>
  224. <Layout style={styles.container}>
  225. {routeName !== "AddGoods" && (
  226. <Card appearance="headFilled">
  227. <Text category="s1">{guide1_title1}</Text>
  228. <Text category="s1">{guideHome_title2}</Text>
  229. </Card>
  230. )}
  231. <Card appearance="formFilled">
  232. {/* 商品名称 */}
  233. <FormInput
  234. label={guide1_form_1}
  235. placeholder={guide1_pla_1}
  236. value={name}
  237. onChange={changeName}
  238. style={{ paddingVertical: 3 }}
  239. />
  240. {/* 商品价格 */}
  241. <FormInput
  242. label={guide1_form_2}
  243. placeholder={guide1_pla_2}
  244. value={amount}
  245. type="money"
  246. onChange={changeAmount}
  247. textAlign="right"
  248. />
  249. {/* 优惠价格 */}
  250. <FormInput
  251. label={guide1_form_3}
  252. placeholder={guide1_pla_3}
  253. value={discountAmount}
  254. type="money"
  255. onChange={changeDiscountAmount}
  256. textAlign="right"
  257. />
  258. {!!id && (
  259. <>
  260. {/* 商品规格 */}
  261. <FormInput
  262. label="商品规格"
  263. type="url"
  264. changePath={() => {
  265. navigation.navigate("GoodsSpecification", {
  266. goodsId: id,
  267. });
  268. }}
  269. textAlign="right"
  270. />
  271. {specify()}
  272. </>
  273. )}
  274. {/* 每日供应数量 */}
  275. <FormInput
  276. label={guide1_form_4}
  277. placeholder={guide1_pla_4}
  278. value={inventory}
  279. type="amount"
  280. onChange={changeInventory}
  281. />
  282. {/* 供应时间 */}
  283. <FormInput
  284. label={guide1_form_5}
  285. type="openTime"
  286. defaultValue={defaultValue}
  287. onChange={(week, startTime, endTime) => {
  288. changeWeek(week);
  289. changeStartTime(startTime);
  290. changeEndTime(endTime);
  291. }}
  292. />
  293. <Layout>
  294. <Textarea
  295. containerStyle={styles.textareaContainer}
  296. style={styles.textarea}
  297. onChangeText={changeIntroduction}
  298. defaultValue={introduction}
  299. maxLength={50}
  300. placeholder={guide1_pla_5}
  301. placeholderTextColor="#B4B4B4"
  302. underlineColorAndroid="transparent"
  303. />
  304. </Layout>
  305. <FormInput
  306. type="img"
  307. value={img}
  308. textAlign="right"
  309. onChange={changeImg}
  310. />
  311. <Layout style={styles.layoutLeft} level="1">
  312. <Button
  313. status="primary"
  314. disabled={!canNext}
  315. onPress={() => {
  316. httpPost(
  317. "/goods/save",
  318. {
  319. id,
  320. name,
  321. amount,
  322. discountAmount,
  323. inventory,
  324. week,
  325. startTime,
  326. endTime,
  327. introduction,
  328. img,
  329. merchantId: mid,
  330. },
  331. { body: "json" },
  332. true
  333. ).then(res => {
  334. if (!id) {
  335. const { params } = route;
  336. const { classifyId } = params || {};
  337. if (classifyId) {
  338. addClassification(classifyId, res.id);
  339. }
  340. }
  341. success(`${id ? "修改" : "添加"}成功`);
  342. if (routeName !== "AddGoods") {
  343. changeGuideStep("2");
  344. } else {
  345. navigation.goBack();
  346. }
  347. });
  348. }}
  349. >
  350. {routeName !== "AddGoods" ? next : confirm}
  351. </Button>
  352. {routeName === "AddGoods" && !!id && (
  353. <Button
  354. appearance="ghost"
  355. status="info"
  356. style={{ marginTop: 10 }}
  357. onPress={() => {
  358. showDialog({
  359. bodyText: removeTips,
  360. cancelable: true,
  361. confirmCallback: () => {
  362. httpPost(`/goods/del/${id}`, {}, {}, true).then(() => {
  363. success("删除成功");
  364. navigation.goBack();
  365. });
  366. },
  367. });
  368. }}
  369. >
  370. {delText}
  371. </Button>
  372. )}
  373. {routeName !== "AddGoods" && (
  374. <Button
  375. style={{ marginTop: 10 }}
  376. appearance="ghost"
  377. status="primary"
  378. onPress={() => {
  379. showDialog({
  380. bodyText: passTips,
  381. cancelable: true,
  382. confirmCallback: () => {
  383. changeGuideStep(2);
  384. },
  385. });
  386. }}
  387. >
  388. {pass}
  389. </Button>
  390. )}
  391. </Layout>
  392. </Card>
  393. {routeName !== "AddGoods" && <ConnectButton />}
  394. </Layout>
  395. </ScrollPage>
  396. </>
  397. );
  398. }