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.

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