index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. "use client";
  2. import { getGiveReceiveApi, SlotParams, SlotType } from "@/api/slots";
  3. import { flatPoint } from "@/utils/methods";
  4. import { SlotMachine } from "@lucky-canvas/react";
  5. import { Mask, Toast } from "antd-mobile";
  6. import clsx from "clsx";
  7. import { useTranslations } from "next-intl";
  8. import { FC, forwardRef, memo, useEffect, useImperativeHandle, useRef, useState } from "react";
  9. import animation from "../animations.module.scss";
  10. interface Props {
  11. onAfterHandler: () => void;
  12. }
  13. const getRandomRedColor = () => {
  14. const r = 255;
  15. const g = 255;
  16. const b = 255;
  17. return `rgba(${r},${g},${b},${Math.random().toFixed(1)})`;
  18. };
  19. const defaultConfig = {
  20. rowSpacing: "0.4861rem",
  21. direction: -1,
  22. };
  23. const prizes = [
  24. {
  25. imgs: [
  26. {
  27. width: "80%",
  28. height: "100%",
  29. src: "/slots/0.png",
  30. },
  31. ],
  32. },
  33. {
  34. imgs: [
  35. {
  36. width: "80%",
  37. height: "100%",
  38. src: "/slots/1.png",
  39. },
  40. ],
  41. },
  42. {
  43. imgs: [
  44. {
  45. width: "80%",
  46. height: "100%",
  47. src: "/slots/2.png",
  48. },
  49. ],
  50. },
  51. {
  52. imgs: [
  53. {
  54. width: "80%",
  55. height: "100%",
  56. src: "/slots/3.png",
  57. },
  58. ],
  59. },
  60. {
  61. imgs: [
  62. {
  63. width: "80%",
  64. height: "100%",
  65. src: "/slots/4.png",
  66. },
  67. ],
  68. },
  69. {
  70. imgs: [
  71. {
  72. width: "80%",
  73. height: "100%",
  74. src: "/slots/5.png",
  75. },
  76. ],
  77. },
  78. {
  79. imgs: [
  80. {
  81. width: "80%",
  82. height: "100%",
  83. src: "/slots/6.png",
  84. },
  85. ],
  86. },
  87. {
  88. imgs: [
  89. {
  90. width: "80%",
  91. height: "100%",
  92. src: "/slots/7.png",
  93. },
  94. ],
  95. },
  96. {
  97. imgs: [
  98. {
  99. width: "80%",
  100. height: "100%",
  101. src: "/slots/8.png",
  102. },
  103. ],
  104. },
  105. {
  106. imgs: [
  107. {
  108. width: "80%",
  109. height: "100%",
  110. src: "/slots/9.png",
  111. },
  112. ],
  113. },
  114. ];
  115. const slots = [
  116. { order: [9, 7, 6, 8, 2, 5, 4, 3, 1, 0], speed: 30 },
  117. { order: [9, 6, 5, 3, 8, 7, 4, 0, 1, 2], speed: 26 },
  118. { order: [9, 2, 4, 7, 3, 1, 8, 5, 0, 6], speed: 22 },
  119. { order: [9, 3, 8, 5, 0, 1, 2, 4, 6, 7], speed: 18 },
  120. ];
  121. const numberPadding = (n: number, index = 4): number[] => {
  122. const s = `${n}`.padStart(index, "0");
  123. return s.split("").map((v, i) => Number(v));
  124. };
  125. const ColorWill = () => {
  126. const rows = 9; // 总行数
  127. const cols = 22; // 总列数
  128. const [blocks, setBlocks] = useState<string[][]>(
  129. Array.from({ length: rows }, () => Array.from({ length: cols }, () => getRandomRedColor()))
  130. );
  131. useEffect(() => {}, []);
  132. return (
  133. <>
  134. <div className={`relative flex flex-col`}>
  135. <div
  136. className={`absolute -z-[1] h-[100%] w-[100%] bg-[url(/slots/color-bg.png)] bg-cover bg-repeat-x ${animation.moveAnimation}`}
  137. ></div>
  138. {/*<img*/}
  139. {/* src="/slots/color-bg.png"*/}
  140. {/* className={`absolute -z-[1] h-[100%] w-[10000px] ${animation.moveAnimation}`}*/}
  141. {/* alt=""*/}
  142. {/*/>*/}
  143. {blocks.map((row, rowIndex) => (
  144. <div key={rowIndex} className={"flex justify-between"}>
  145. {row.map((n: string, index: number) => {
  146. return (
  147. <div
  148. key={index}
  149. style={{ background: n, transition: "background-color 0.2s" }}
  150. className={"h-[0.0694rem] w-[0.0694rem] " + " rounded-[50%]"}
  151. ></div>
  152. );
  153. })}
  154. </div>
  155. ))}
  156. </div>
  157. </>
  158. );
  159. };
  160. interface SlotsClientProps {
  161. slotSource: SlotType;
  162. onRotateAfter?: () => void;
  163. }
  164. const SlotsClient: FC<SlotsClientProps> = (props) => {
  165. const { slotSource, onRotateAfter } = props;
  166. const slotsRef = useRef<any>(null);
  167. const t = useTranslations();
  168. const rotating = useRef<boolean>(false);
  169. const buttonRef = useRef<HTMLImageElement | null>(null);
  170. // const ratio = `${slotSource?.rollover.reduce(
  171. // (pre, next) => (pre > next.weight ? pre : next.weight),
  172. // 0
  173. // )}`
  174. // .split("")
  175. // .map((n) => Number(n)) || [9, 9];
  176. const [ratio, setRatio] = useState([9, 9]);
  177. const params = {
  178. activity_id: slotSource?.id,
  179. start_time: slotSource?.times[0].start_time,
  180. end_time: slotSource?.times[0].end_time,
  181. };
  182. useEffect(() => {
  183. // getSlots();
  184. }, []);
  185. // 开始旋转
  186. const handler = () => {
  187. // 按下动画
  188. if (params && !params.activity_id && !params.start_time) return;
  189. if (rotating.current) return;
  190. // slotsRef.current?.play();
  191. // setTimeout(() => {
  192. // slotsRef.current?.stop(numberPadding(Math.floor(Math.random() * 9999)));
  193. // }, 2500);
  194. // return;
  195. // 图片按下修改高度
  196. let height = buttonRef.current!.clientHeight;
  197. buttonRef.current!.style.transition = "height 0.3s";
  198. buttonRef.current!.style.height = `${height * 0.8}px`;
  199. rotating.current = true;
  200. setTimeout(() => {
  201. buttonRef.current!.style.height = `${height}px`;
  202. }, 200);
  203. slotsRef.current?.play();
  204. const Interval = setInterval(() => {
  205. const num = Math.floor(Math.random() * 99) + 1;
  206. setRatio(() => numberPadding(num, 2));
  207. }, 500);
  208. // 数据获取
  209. getGiveReceiveApi(params as SlotParams)
  210. .then((res) => {
  211. setTimeout(() => {
  212. Interval && clearInterval(Interval);
  213. const rollover = flatPoint(res.data.rollover / 100);
  214. slotsRef.current?.stop(numberPadding(res.data.amount));
  215. setRatio(numberPadding(rollover > 100 ? 99 : rollover, 2));
  216. }, 3000);
  217. })
  218. .catch((error) => {
  219. Interval && clearInterval(Interval);
  220. slotsRef.current.init();
  221. rotating.current = false;
  222. Toast.show(t(`code.${error.data.code}`));
  223. });
  224. };
  225. // 结束旋转
  226. const endHandler = (prize: any) => {
  227. // rotating.current = false;
  228. onRotateAfter && onRotateAfter();
  229. };
  230. return (
  231. <>
  232. <div className={"w-[100%]"}>
  233. <div className={"relative w-[100%]"}>
  234. <img src="/slots/aura.png" alt="" className={"absolute -top-[23%]"} />
  235. <img
  236. src={"/slots/slots-bg.png"}
  237. className={"h-[3.6333rem] w-[100%]" + "object-cover"}
  238. alt={""}
  239. />
  240. {/*light*/}
  241. <div className={"absolute top-0 h-[2.69rem] w-[100%]"}>
  242. <img
  243. src={"/slots/light-1.png"}
  244. alt={""}
  245. className={`h-[100%] w-[100%] ${animation.flashingAnimation}`}
  246. />
  247. <img
  248. src="/slots/light-2.png"
  249. alt=""
  250. className={`absolute top-0 h-[100%] w-[100%] ${animation.antiFlashingAnimation} `}
  251. />
  252. </div>
  253. {/*header*/}
  254. <div
  255. className={
  256. "absolute left-[50%] top-[0.1875rem] -translate-x-1/2" +
  257. " ml-[0.0694rem] h-[0.6111rem] w-[1.48rem] sm:w-[1.58rem]" +
  258. " overflow-hidden rounded-tl-[10px] rounded-tr-[10px] border-[1px]" +
  259. " border-[transparent]"
  260. }
  261. >
  262. <div
  263. className={
  264. "h-[100%] w-[100%] " + " relative flex items-center justify-center"
  265. }
  266. >
  267. <div
  268. className={
  269. "absolute left-0 top-0 flex w-[100%] flex-col" +
  270. " -z-1 h-[100%] flex-wrap justify-between overflow-hidden"
  271. }
  272. >
  273. <ColorWill />
  274. </div>
  275. <div className={"z-10 flex h-[100%] items-center"}>
  276. <img src="/slots/ratio/x.png" alt="" className={"h-[0.4167rem]"} />
  277. {ratio.map((n, index) => (
  278. <img
  279. key={index}
  280. src={`/slots/ratio/${n}.png`}
  281. alt={""}
  282. className={"h-[0.4514rem]"}
  283. />
  284. ))}
  285. </div>
  286. </div>
  287. </div>
  288. {/*slots*/}
  289. <div
  290. className={
  291. "absolute bottom-[51%] left-0 flex w-[100%] translate-y-1/2 " +
  292. " justify-center"
  293. }
  294. >
  295. <div className={""}>
  296. <SlotMachine
  297. slots={slots}
  298. ref={slotsRef}
  299. width="3.1944rem"
  300. onEnd={endHandler}
  301. height="1.5625rem"
  302. prizes={prizes}
  303. defaultConfig={defaultConfig}
  304. ></SlotMachine>
  305. </div>
  306. </div>
  307. <div
  308. className={
  309. "absolute bottom-[0.2083rem] left-0 flex w-[100%] justify-center"
  310. }
  311. >
  312. <img
  313. onClick={handler}
  314. ref={buttonRef}
  315. src={"/slots/button.png"}
  316. className={"h-[0.6944rem] w-[1.25rem]" + " object-fill"}
  317. />
  318. </div>
  319. </div>
  320. </div>
  321. </>
  322. );
  323. };
  324. export type SlotModalRefProps = {
  325. onClose: () => void;
  326. onOpen: (value: SlotType) => void;
  327. };
  328. const SlotsModal = forwardRef<SlotModalRefProps, Props>(function SlotsModal(props, ref) {
  329. const [visible, setVisible] = useState(false);
  330. const { onAfterHandler } = props;
  331. const [willStatus, setWillStatus] = useState(false);
  332. const [slotSource, setSlotSource] = useState<SlotType | null>(null);
  333. useImperativeHandle(ref, () => {
  334. return {
  335. onClose: () => setVisible(false),
  336. onOpen: (source: SlotType) => {
  337. setVisible(true);
  338. setSlotSource(source);
  339. },
  340. };
  341. });
  342. const globalCls = "absolute z-[8] transition transform duration-1000 ease-in ";
  343. const light1 = clsx(globalCls, willStatus ? `${animation.expandAnimation} ` : "hidden", {});
  344. const light2 = clsx(globalCls, willStatus ? `${animation.expandAnimation} ` : "hidden", {});
  345. const light3 = clsx(
  346. " object-contain absolute z-[8] ",
  347. willStatus ? ` ${animation.scaleLxAnimation}` : "hidden"
  348. );
  349. const light4 = clsx(
  350. " object-contain absolute z-[8] ",
  351. willStatus ? `${animation.scaleRxAnimation}` : "hidden"
  352. );
  353. const will = clsx("", willStatus ? "opacity-1" : "opacity-0");
  354. const handler = () => {
  355. setWillStatus(true);
  356. onAfterHandler();
  357. };
  358. return (
  359. <>
  360. <Mask visible={visible} destroyOnClose={true} getContainer={null}>
  361. <div
  362. className={"absolute right-[0.2083rem] top-[18%] z-50"}
  363. onClick={() => setVisible(false)}
  364. >
  365. <span className={"iconfont icon-guanbi"}></span>
  366. </div>
  367. {/*body*/}
  368. <div className={`absolute top-[8%] w-[100%]`}>
  369. <div className={`h-[1.1111rem]`}>
  370. <div
  371. className={`relative flex h-[100%] items-center justify-center transition-[all] duration-500 ease-in ${will}`}
  372. >
  373. <img
  374. src="/slots/will-bg.png"
  375. className={"z-10 h-[0.6944rem] object-contain"}
  376. alt=""
  377. />
  378. <img src="/slots/will-light1.png" alt="" className={light1} />
  379. <img src="/slots/will-light2.png" alt="" className={light2} />
  380. <img src="/slots/will-light3.png" alt="" className={light3} />
  381. <img src="/slots/will-light3.png" alt="" className={light4} />
  382. </div>
  383. </div>
  384. {slotSource && <SlotsClient onRotateAfter={handler} slotSource={slotSource} />}
  385. </div>
  386. </Mask>
  387. </>
  388. );
  389. });
  390. export default memo(SlotsModal);