You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.4 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. import { useSelector } from "react-redux";
  2. import bluetooth_nc from "../assets/bluetooth_nc.svg";
  3. import bluetooth_c from "../assets/bluetooth_c.svg";
  4. import icon_usb from "../assets/icon_usb.svg";
  5. import icon_battery from "../assets/icon_battery.svg";
  6. import icon_avatar from "../assets/icon_avatar.svg";
  7. import icon_logout from "../assets/icon_logout.svg";
  8. import check_mark from "../assets/check_mark.svg";
  9. import { useState, useEffect } from "react";
  10. import { Dropdown, MenuProps, message, Button, Popover } from "antd";
  11. import { logout } from "../services/user/user";
  12. import { useNavigate } from "react-router";
  13. import { useAppDispatch, useAppSelector } from "../utils/hooks";
  14. import { updateUser } from "../store/features/contextSlice";
  15. import "./bluetooth.scss";
  16. export default function Header() {
  17. const navigate = useNavigate();
  18. const dispatch = useAppDispatch();
  19. const deviceInfo = useAppSelector(store => store.context.device);
  20. const deviceState = useAppSelector(store => store.deviceState);
  21. const userInfo = useAppSelector(store => store.context.user.loginUser);
  22. let [isConnect, setIsConnect] = useState(deviceState.isConnect);
  23. //获取当前websocet的状态
  24. const showBlueImg = () => {
  25. if (deviceState.isConnect) {
  26. return (
  27. <Popover content={getBtContent()} title="" trigger="click">
  28. <section className="bg-white rounded-md h-9 w-10 flex justify-center items-center mr-3">
  29. <img src={icon_usb} alt="" className="h-6" />
  30. </section>
  31. </Popover>
  32. );
  33. }
  34. return null;
  35. // if(!isConnect){
  36. // return <Popover content={getBtList()} title="可连接设备">
  37. // <div ></div>
  38. // <img src={bluetooth_nc} alt="" className="ext-base ml-2 h-6" />
  39. // </Popover>
  40. // }else {
  41. // return <Popover content={getBtContent()} title="">
  42. // <img src={icon_usb} onClick={onDisconnectBt} alt="" className="ext-base ml-2 h-6" />
  43. // </Popover>
  44. // }
  45. };
  46. let list = [
  47. {
  48. name: "Kdkow_1",
  49. id: "1",
  50. },
  51. {
  52. name: "llwoa_2",
  53. id: "2",
  54. },
  55. ];
  56. let [bluetoothList, setbluetoothList] = useState(list);
  57. //获取mock数据
  58. useEffect(() => {
  59. if (userInfo.nickname) {
  60. setNickname(userInfo.nickname);
  61. } else {
  62. const user = localStorage.getItem("user");
  63. if (user) {
  64. let userData = JSON.parse(user || "");
  65. setNickname(userData.nickname);
  66. }
  67. }
  68. },[userInfo.nickname]);
  69. const [messageApi, contextHolder] = message.useMessage();
  70. const [nickname, setNickname] = useState<string>();
  71. const items: MenuProps["items"] = [
  72. // {
  73. // key: "1",
  74. // label: "修改密码",
  75. // icon: <img src={icon_pwd} alt="" className="w-[18px]" />,
  76. // onClick: () => {
  77. // messageApi.info('ok')
  78. // },
  79. // },
  80. {
  81. key: "2",
  82. label: "退出登录",
  83. icon: <img src={icon_logout} alt="icon" className="w-5" />,
  84. onClick: () => {
  85. logout({}).then(res => {
  86. if (res.status !== 0) {
  87. messageApi.error(res.data.info);
  88. } else {
  89. localStorage.setItem("user", "");
  90. dispatch(
  91. updateUser({
  92. loginFlag: false,
  93. loginUser: {},
  94. })
  95. );
  96. navigate("/login");
  97. }
  98. });
  99. },
  100. },
  101. ];
  102. const getBtList = () => {
  103. let Dom = null;
  104. if (!isConnect) {
  105. Dom = (
  106. <div>
  107. {bluetoothList.map(item => {
  108. return (
  109. <div className="mt-[1rem]" onClick={connectBt}>
  110. <Button type="link">{item.name}</Button>
  111. </div>
  112. );
  113. })}
  114. </div>
  115. );
  116. }
  117. return Dom;
  118. };
  119. //设备已连接
  120. const getBtContent = () => {
  121. return (
  122. <div>
  123. <div>
  124. <div className="bluetooth_c">
  125. <img src={check_mark} alt="" className="ext-base ml-2 h-4" />
  126. <div className="ml-[10px]"></div>
  127. </div>
  128. <div className="pl-[15px]">
  129. <div className="mt-[1rem]">sn码{deviceState.sn}</div>
  130. <div className="mt-[1rem]">{deviceState.descriptivePortName}</div>
  131. {/* <div className="mt-[1rem]">电量:{bluetoothInfo.power}</div> */}
  132. </div>
  133. </div>
  134. </div>
  135. );
  136. };
  137. //断开蓝牙连接
  138. const onDisconnectBt = () => {
  139. setIsConnect(false);
  140. };
  141. const connectBt = () => {
  142. setIsConnect(true);
  143. setTimeout(() => {}, 1000);
  144. };
  145. return (
  146. <>
  147. {contextHolder}
  148. <div className="bg-[--bgColor] h-full flex items-center">
  149. <section className="ml-auto rounded-md h-9 mr-4 flex items-center bg-white gap-2 pl-4">
  150. <span className="w-[94px]">{deviceInfo.temperature.toFixed(1)} </span>
  151. <span className="w-[120px]">X轴倾斜{deviceInfo.inclinatorX}</span>
  152. <span className="w-[120px]">Y轴倾斜{deviceInfo.inclinatorY}</span>
  153. </section>
  154. <section className="bg-white rounded-md h-9 w-12 relative mr-3 flex justify-center items-center">
  155. <img
  156. src={icon_battery}
  157. className="absolute h-8 w-10 left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2"
  158. alt=""
  159. />
  160. <p className="text-primary text-xs">{deviceInfo.power.toFixed()}%</p>
  161. </section>
  162. {/* <section className="bg-white rounded-md h-9 w-10 flex justify-center items-center mr-3">
  163. <img src={icon_usb} className="w-6" alt="icon" />
  164. </section> */}
  165. {showBlueImg()}
  166. <div className="mr-8 flex items-center min-w-[5rem]">
  167. <Dropdown menu={{ items }} trigger={["click"]}>
  168. <section className="flex items-center">
  169. <img src={icon_avatar} alt="" className="h-8" />
  170. <p className="text-base ml-2">{nickname || "未登录"}</p>
  171. </section>
  172. </Dropdown>
  173. </div>
  174. </div>
  175. </>
  176. );
  177. }