Index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Log;
  6. /**
  7. * 首页接口
  8. */
  9. class Index extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. // 允许跨域访问的域名,* 表示允许任何域名,也可以指定具体域名如 'http://localhost:8080'
  17. header('Access-Control-Allow-Origin: http://dev-rfid.7in6.com:23609/');
  18. // 允许的请求方法
  19. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
  20. // 允许的请求头
  21. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, Token');
  22. // 如果是OPTIONS请求,直接退出并返回200状态码(预检请求)
  23. if (request()->isOptions()) {
  24. exit();
  25. }
  26. }
  27. public function index()
  28. {
  29. $this->success('请求成功');
  30. }
  31. /**
  32. * 用户输入账号密码
  33. * 1。验证登录返回信息
  34. */
  35. public function Login()
  36. {
  37. header('Access-Control-Allow-Origin: *');
  38. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  39. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  40. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  41. exit;
  42. }
  43. $param = input();
  44. $username = isset($param['username']) ? trim($param['username']) : '';
  45. $password = isset($param['password']) ? trim($param['password']) : '';
  46. // 查询用户表
  47. $user = Db::name('rfid_user')
  48. ->where('username', $username)
  49. ->where('password', md5($password))
  50. ->find();
  51. if (!$user) {
  52. return json([
  53. 'code' => 401,
  54. 'msg' => '用户名或密码错误',
  55. 'data' => null
  56. ]);
  57. }
  58. // 获取当前时间戳 & 今天结束时间戳
  59. $now = time();
  60. $todayEnd = strtotime(date('Y-m-d 23:59:59'));
  61. // 判断 token 是否有效
  62. $tokenExpireTime = isset($user['token_expire_time']) && !empty($user['token_expire_time'])
  63. ? strtotime($user['token_expire_time'])
  64. : 0;
  65. if (!empty($user['token']) && $tokenExpireTime > $now) {
  66. $token = $user['token']; // 继续使用原 token
  67. } else {
  68. // 生成新 token
  69. $token = md5($username . time() . uniqid());
  70. // 更新用户表 token 字段
  71. Db::name('rfid_user')
  72. ->where('id', $user['id'])
  73. ->update([
  74. 'token' => $token,
  75. 'token_expire_time' => date('Y-m-d H:i:s', $todayEnd),
  76. 'sys_rq' => date('Y-m-d H:i:s')
  77. ]);
  78. }
  79. // 构造返回数据
  80. $returnData = [
  81. 'user_info' => [
  82. 'id' => $user['id'],
  83. 'username' => $user['username'],
  84. 'nickname' => $user['nickname'] ?? $user['username'],
  85. 'building' => $user['building'],
  86. 'room' => $user['room'],
  87. 'pen' => $user['pen']
  88. ],
  89. 'token' => $token,
  90. 'token_expire_time' => date('Y-m-d H:i:s', $todayEnd)
  91. ];
  92. return json([
  93. 'code' => 0,
  94. 'msg' => '登录成功',
  95. 'data' => $returnData
  96. ]);
  97. }
  98. /**
  99. * 通过提交的用户id保存当前编号信息
  100. */
  101. public function Post_Usersetup()
  102. {
  103. header('Access-Control-Allow-Origin: *');
  104. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  105. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  106. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  107. exit;
  108. }
  109. $param = input();
  110. $id = trim($param['userid']);
  111. $arr = [
  112. 'building' => $param['building'],
  113. 'room' => $param['room'],
  114. 'pen' => $param['pen'],
  115. ];
  116. Db::name('rfid_user')
  117. ->where('id', $id)
  118. ->update($arr);
  119. return json([
  120. 'code' => 0,
  121. 'msg' => '保存成功',
  122. 'data' => ''
  123. ]);
  124. }
  125. /**
  126. * 通过用户的id获取当前用户信息
  127. * rfid_user表可以通过id获取到
  128. * 用户,栋舍编号,房间编号,栏位编号
  129. */
  130. public function UserList()
  131. {
  132. header('Access-Control-Allow-Origin: *');
  133. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  134. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  135. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  136. exit;
  137. }
  138. $param = input();
  139. $id = trim($param['userid']);
  140. $res = Db::name('rfid_user')
  141. ->field('nickname,username,id,token,token_expire_time,building,room,pen')
  142. ->where('id', $id)
  143. ->find();
  144. if($res){
  145. return json([
  146. 'code' => 0,
  147. 'msg' => '获取用户数据信息',
  148. 'data' => $res
  149. ]);
  150. }
  151. }
  152. //获取栋舍编号
  153. public function Get_Building(){
  154. header('Access-Control-Allow-Origin: *');
  155. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  156. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  157. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  158. exit;
  159. }
  160. $res = Db::name('building')
  161. ->select();
  162. return json([
  163. 'code' => 0,
  164. 'msg' => '栋舍编号',
  165. 'data' => $res
  166. ]);
  167. }
  168. //获取房间编号
  169. public function Get_Room(){
  170. header('Access-Control-Allow-Origin: *');
  171. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  172. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  173. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  174. exit;
  175. }
  176. $res = Db::name('room')
  177. ->select();
  178. return json([
  179. 'code' => 0,
  180. 'msg' => '房间编号',
  181. 'data' => $res
  182. ]);
  183. }
  184. //获取栏位编号
  185. public function Get_Pen(){
  186. header('Access-Control-Allow-Origin: *');
  187. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  188. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  189. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  190. exit;
  191. }
  192. $res = Db::name('pen')
  193. ->select();
  194. return json([
  195. 'code' => 0,
  196. 'msg' => '栏位编号',
  197. 'data' => $res
  198. ]);
  199. }
  200. /**
  201. * 获取前端提交的数据
  202. */
  203. public function Post_ListAdd()
  204. {
  205. header('Access-Control-Allow-Origin: *');
  206. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  207. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  208. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  209. exit;
  210. }
  211. $param = input();
  212. $insertData = [];
  213. foreach ($param as $item) {
  214. $insertData[] = [
  215. 'username' => $item['username'] ?? '',
  216. 'userId' => $item['userId'] ?? '',
  217. 'rfid' => trim($item['rfid']),
  218. 'rfid_num' => count($param), // 总记录数
  219. 'buildingName' => $item['buildingName'] ?? '',
  220. 'roomName' => $item['roomName'] ?? '',
  221. 'penNo' => $item['penNo'] ?? '',
  222. 'model' => $item['deviceModel'] ?? '',
  223. 'version' => $item['deviceVersion'] ?? '',
  224. 'type' => $item['type'] ?? '',
  225. 'sys_rq' => date('Y-m-d H:i:s'),
  226. 'gender' => $item['gender'] ?? '',
  227. 'birthDate' => $item['birthDate'] ?? '',
  228. 'notes' => $item['notes'] ?? '',
  229. ];
  230. }
  231. if (empty($insertData)) {
  232. return json([
  233. 'code' => 1,
  234. 'msg' => '无有效数据',
  235. ]);
  236. }
  237. try {
  238. $result = Db::name("records")->insertAll($insertData);
  239. return json([
  240. 'code' => 0,
  241. 'msg' => '数据已提交成功'
  242. ]);
  243. } catch (\Exception $e) {
  244. Log::error('插入失败:' . $e->getMessage());
  245. return json([
  246. 'code' => 500,
  247. 'msg' => '插入异常:' . $e->getMessage(),
  248. ]);
  249. }
  250. }
  251. //获取数据信息
  252. public function GetListArr(){
  253. header('Access-Control-Allow-Origin: *');
  254. header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
  255. header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
  256. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  257. exit;
  258. }
  259. // 创建最终返回的数据数组
  260. $list = [];
  261. // 查询new_records表的所有数据,保持原样
  262. $newRecordsData = Db::name("new_records")
  263. ->select();
  264. // 将new_records表的数据添加到结果数组
  265. if (!empty($newRecordsData)) {
  266. $list = array_merge($list, $newRecordsData);
  267. }
  268. // 查询records表的数据,按sys_rq倒序
  269. $recordsData = Db::name("records")
  270. ->order('sys_rq DESC')
  271. ->select();
  272. // 对records表数据按rfid去重,只保留每个rfid最新的一条记录
  273. $rfidMap = [];
  274. $uniqueRecords = [];
  275. foreach ($recordsData as $record) {
  276. $rfid = trim($record['rfid']);
  277. // 如果这个rfid还没有处理过,则添加到结果中
  278. if (!isset($rfidMap[$rfid])) {
  279. $rfidMap[$rfid] = true;
  280. $uniqueRecords[] = $record;
  281. }
  282. }
  283. // 将去重后的records表数据添加到结果数组
  284. if (!empty($uniqueRecords)) {
  285. $list = array_merge($list, $uniqueRecords);
  286. }
  287. return json([
  288. 'code' => 0,
  289. 'msg' => '数据获取成功',
  290. 'list' => $list
  291. ]);
  292. }
  293. }