index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. "use client";
  2. import { getGiveReceiveApi, SlotParams, SlotType } from "@/api/slots";
  3. import { debounce, 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 = debounce(() => {
  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. // 数据获取
  205. getGiveReceiveApi(params as SlotParams)
  206. .then((res) => {
  207. setTimeout(() => {
  208. const rollover = flatPoint(res.data.rollover / 100);
  209. slotsRef.current?.stop(numberPadding(res.data.amount));
  210. setRatio(numberPadding(rollover > 100 ? 99 : rollover, 2));
  211. }, 2500);
  212. })
  213. .catch((error) => {
  214. slotsRef.current.init();
  215. rotating.current = false;
  216. Toast.show(t(`code.${error.data.code}`));
  217. });
  218. }, 300);
  219. // 结束旋转
  220. const endHandler = (prize: any) => {
  221. // rotating.current = false;
  222. onRotateAfter && onRotateAfter();
  223. };
  224. return (
  225. <>
  226. <div className={"w-[100%]"}>
  227. <div className={"relative w-[100%]"}>
  228. <img src="/slots/aura.png" alt="" className={"absolute -top-[23%]"} />
  229. <img
  230. src={"/slots/slots-bg.png"}
  231. className={"h-[3.6333rem] w-[100%]" + "object-cover"}
  232. alt={""}
  233. />
  234. {/*light*/}
  235. <div className={"absolute top-0 h-[2.69rem] w-[100%]"}>
  236. <img
  237. src={"/slots/light-1.png"}
  238. alt={""}
  239. className={`h-[100%] w-[100%] ${animation.flashingAnimation}`}
  240. />
  241. <img
  242. src="/slots/light-2.png"
  243. alt=""
  244. className={`absolute top-0 h-[100%] w-[100%] ${animation.antiFlashingAnimation} `}
  245. />
  246. </div>
  247. {/*header*/}
  248. <div
  249. className={
  250. "absolute left-[50%] top-[0.1875rem] -translate-x-1/2" +
  251. " ml-[0.0694rem] h-[0.6111rem] w-[1.48rem] sm:w-[1.58rem]" +
  252. " overflow-hidden rounded-tl-[10px] rounded-tr-[10px] border-[1px]" +
  253. " border-[transparent]"
  254. }
  255. >
  256. <div
  257. className={
  258. "h-[100%] w-[100%] " + " relative flex items-center justify-center"
  259. }
  260. >
  261. <div
  262. className={
  263. "absolute left-0 top-0 flex w-[100%] flex-col" +
  264. " -z-1 h-[100%] flex-wrap justify-between overflow-hidden"
  265. }
  266. >
  267. <ColorWill />
  268. </div>
  269. <div className={"z-10 flex h-[100%] items-center"}>
  270. <img src="/slots/ratio/x.png" alt="" className={"h-[0.4167rem]"} />
  271. {ratio.map((n, index) => (
  272. <img
  273. key={index}
  274. src={`/slots/ratio/${n}.png`}
  275. alt={""}
  276. className={"h-[0.4514rem]"}
  277. />
  278. ))}
  279. </div>
  280. </div>
  281. </div>
  282. {/*slots*/}
  283. <div
  284. className={
  285. "absolute bottom-[51%] left-0 flex w-[100%] translate-y-1/2 " +
  286. " justify-center"
  287. }
  288. >
  289. <div className={""}>
  290. <SlotMachine
  291. slots={slots}
  292. ref={slotsRef}
  293. width="3.1944rem"
  294. onEnd={endHandler}
  295. height="1.5625rem"
  296. prizes={prizes}
  297. defaultConfig={defaultConfig}
  298. ></SlotMachine>
  299. </div>
  300. </div>
  301. <div
  302. className={
  303. "absolute bottom-[0.2083rem] left-0 flex w-[100%] justify-center"
  304. }
  305. >
  306. <img
  307. onClick={handler}
  308. ref={buttonRef}
  309. src={"/slots/button.png"}
  310. className={"h-[0.6944rem] w-[1.25rem]" + " object-fill"}
  311. />
  312. </div>
  313. </div>
  314. </div>
  315. </>
  316. );
  317. };
  318. export type SlotModalRefProps = {
  319. onClose: () => void;
  320. onOpen: (value: SlotType) => void;
  321. };
  322. const SlotsModal = forwardRef<SlotModalRefProps, Props>(function SlotsModal(props, ref) {
  323. const [visible, setVisible] = useState(false);
  324. const { onAfterHandler } = props;
  325. const [willStatus, setWillStatus] = useState(false);
  326. const [slotSource, setSlotSource] = useState<SlotType | null>(null);
  327. useImperativeHandle(ref, () => {
  328. return {
  329. onClose: () => setVisible(false),
  330. onOpen: (source: SlotType) => {
  331. setVisible(true);
  332. setSlotSource(source);
  333. },
  334. };
  335. });
  336. const globalCls = "absolute z-[8] transition transform duration-1000 ease-in ";
  337. const light1 = clsx(globalCls, willStatus ? `${animation.expandAnimation} ` : "hidden", {});
  338. const light2 = clsx(globalCls, willStatus ? `${animation.expandAnimation} ` : "hidden", {});
  339. const light3 = clsx(
  340. " object-contain absolute z-[8] ",
  341. willStatus ? ` ${animation.scaleLxAnimation}` : "hidden"
  342. );
  343. const light4 = clsx(
  344. " object-contain absolute z-[8] ",
  345. willStatus ? `${animation.scaleRxAnimation}` : "hidden"
  346. );
  347. const will = clsx("", willStatus ? "opacity-1" : "opacity-0");
  348. const handler = () => {
  349. setWillStatus(true);
  350. onAfterHandler();
  351. };
  352. return (
  353. <>
  354. <Mask visible={visible} destroyOnClose={true} getContainer={null}>
  355. <div
  356. className={"absolute right-[0.2083rem] top-[18%] z-50"}
  357. onClick={() => setVisible(false)}
  358. >
  359. <span className={"iconfont icon-guanbi"}></span>
  360. </div>
  361. {/*body*/}
  362. <div className={`absolute top-[8%] w-[100%]`}>
  363. <div className={`h-[1.1111rem]`}>
  364. <div
  365. className={`relative flex h-[100%] items-center justify-center transition-[all] duration-500 ease-in ${will}`}
  366. >
  367. <img
  368. src="/slots/will-bg.png"
  369. className={"z-10 h-[0.6944rem] object-contain"}
  370. alt=""
  371. />
  372. <img src="/slots/will-light1.png" alt="" className={light1} />
  373. <img src="/slots/will-light2.png" alt="" className={light2} />
  374. <img src="/slots/will-light3.png" alt="" className={light3} />
  375. <img src="/slots/will-light3.png" alt="" className={light4} />
  376. </div>
  377. </div>
  378. {slotSource && <SlotsClient onRotateAfter={handler} slotSource={slotSource} />}
  379. </div>
  380. </Mask>
  381. </>
  382. );
  383. });
  384. export default memo(SlotsModal);