device.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * 设备管理相关API
  3. */
  4. import {
  5. http,
  6. Method
  7. } from '@/utils/request.js';
  8. import storage from "@/utils/storage.js";
  9. import api from "@/config/api.js";
  10. import config from "@/config/config.js";
  11. // const request = http.request;
  12. const userInfo = storage.getUserInfo()
  13. /**
  14. * 登录wvp获取token
  15. */
  16. export function loginWvp() {
  17. return uni.request({
  18. url: `/wvp/api/user/login?username=${config.wvpUsername}&password=${config.wvpPassword}`,
  19. method: Method.GET,
  20. needToken: false,
  21. });
  22. }
  23. /**
  24. * 获取通道列表
  25. */
  26. export async function getChannels(deviceId) {
  27. try {
  28. // 登录获取token
  29. const loginRes = await loginWvp();
  30. console.log("WVP登录结果:", loginRes);
  31. if (loginRes[0]) {
  32. // 处理错误
  33. console.error('登录失败', loginRes[0]);
  34. throw new Error('登录失败');
  35. }
  36. const response = loginRes[1].data;
  37. if (response.code === 0) {
  38. console.log("WVP登录成功");
  39. storage.setWvpAccessToken(response.data.accessToken);
  40. // 查询通道列表
  41. const channelsRes = await uni.request({
  42. url: `/wvp/api/device/query/devices/${deviceId}/channels`,
  43. method: Method.GET,
  44. data: {
  45. page: 1,
  46. count: 10,
  47. online: true,
  48. },
  49. header: {
  50. 'Access-Token': `${storage.getWvpAccessToken()}`,
  51. },
  52. });
  53. console.log("获取通道结果:", channelsRes);
  54. return channelsRes;
  55. } else {
  56. console.error("WVP登录失败", loginRes);
  57. throw new Error("WVP登录失败");
  58. }
  59. } catch (error) {
  60. console.error("获取通道失败", error);
  61. throw error;
  62. }
  63. }
  64. /**
  65. * 开始点播
  66. */
  67. export async function playStart(deviceId, channelId) {
  68. return await uni.request({
  69. url: `/wvp/api/play/start/${deviceId}/${channelId}`,
  70. method: Method.GET,
  71. header: {
  72. 'Access-Token': `${storage.getWvpAccessToken()}`,
  73. },
  74. })
  75. }
  76. /**
  77. * 暂停点播
  78. */
  79. export async function pause(deviceId, channelId) {
  80. return await uni.request({
  81. url: `/wvp/api/play/stop/${deviceId}/${channelId}`,
  82. method: Method.GET,
  83. header: {
  84. 'Access-Token': `${storage.getWvpAccessToken()}`,
  85. },
  86. })
  87. }
  88. /**
  89. * 获取设备概览数据
  90. * @param {string} fieldId - 地块ID,可选
  91. * @returns {Promise} 设备概览数据
  92. */
  93. export function fetchDeviceOverview(fieldId) {
  94. const params = {};
  95. console.log("userInfo请求:",userInfo);
  96. if (userInfo.userid) params.userId = userInfo.userid;
  97. if (fieldId) params.fieldId = fieldId;
  98. return http.request({
  99. url: '/base/device/overview',
  100. method: Method.POST,
  101. needToken: true,
  102. data: params
  103. });
  104. }
  105. /**
  106. * 根据设备类型获取设备列表
  107. * @param {string} type - 设备类型(monitor-监控设备,sensor-采集设备,control-控制设备,irrigation-灌溉设备,tractor-农机设备)
  108. * @param {Object} params - 查询参数
  109. * @returns {Promise} 设备列表
  110. */
  111. export function fetchDevicesByType(params) {
  112. if (userInfo.userid) params.userId = userInfo.userid;
  113. return http.request({
  114. url: `/base/device/typeList`,
  115. method: Method.POST,
  116. needToken: true,
  117. data: params
  118. });
  119. }
  120. /**
  121. * 获取所有设备列表
  122. * @param {Object} params - 查询参数
  123. * @param {number} params.pageNum - 页码
  124. * @param {number} params.pageSize - 每页数量
  125. * @param {string} params.deviceName - 设备名称,可选
  126. * @param {string} params.deviceTypeId - 设备类型ID,可选
  127. * @param {number} params.status - 设备状态,可选
  128. * @returns {Promise} 设备列表
  129. */
  130. export function fetchDeviceList(params = {}) {
  131. return http.request({
  132. url: 'uniapp/device/list',
  133. method: Method.GET,
  134. needToken: true,
  135. params: params
  136. });
  137. }
  138. /**
  139. * 获取设备详情
  140. * @param {number} id - 设备ID
  141. * @returns {Promise} 设备详情
  142. */
  143. export function getDeviceDetail(id) {
  144. return http.request({
  145. url: `uniapp/device/${id}`,
  146. needToken: true,
  147. method: Method.GET
  148. });
  149. }
  150. /**
  151. * 获取设备采集器详情(包含气象/土壤数据和告警信息)
  152. * @param {number} id - 设备编码
  153. * @param {string} code - 设备编码,可选,用于判断设备类型
  154. * @returns {Promise} 设备详情数据
  155. */
  156. export function getDeviceCollectorDetail(deviceId, code) {
  157. return http.request({
  158. url: `/base/device/collector/detail/${deviceId}`,
  159. method: Method.GET,
  160. needToken: true,
  161. params: code ? { code } : {}
  162. });
  163. }
  164. /**
  165. * 添加设备
  166. * @param {Object} data - 设备信息
  167. * @returns {Promise} 添加结果
  168. */
  169. export function addDevice(data) {
  170. return http.request({
  171. url: 'uniapp/device',
  172. method: Method.POST,
  173. needToken: true,
  174. data: data
  175. });
  176. }
  177. /**
  178. * 更新设备信息
  179. * @param {Object} data - 设备信息
  180. * @returns {Promise} 更新结果
  181. */
  182. export function updateDevice(data) {
  183. return http.request({
  184. url: 'uniapp/device',
  185. method: Method.PUT,
  186. needToken: true,
  187. data: data
  188. });
  189. }
  190. /**
  191. * 删除设备
  192. * @param {number|number[]} ids - 设备ID或ID数组
  193. * @returns {Promise} 删除结果
  194. */
  195. export function deleteDevice(ids) {
  196. const idStr = Array.isArray(ids) ? ids.join(',') : ids;
  197. return http.request({
  198. url: `uniapp/device/${idStr}`,
  199. needToken: true,
  200. method: Method.DELETE
  201. });
  202. }
  203. /**
  204. * 根据用户ID获取关联的设备列表
  205. * @param {Object} params - 查询参数
  206. * @returns {Promise} 设备列表
  207. */
  208. export function fetchUserDeviceList(params = {}) {
  209. const user = storage.getUserInfo();
  210. return http.request({
  211. url: 'uniapp/device/user/list',
  212. method: Method.GET,
  213. params: {
  214. ...params,
  215. userId: user.userId
  216. },
  217. needToken: true,
  218. });
  219. }
  220. /**
  221. * 根据地块ID获取设备列表
  222. * @param {string} fieldId - 地块ID
  223. * @param {Object} params - 其他查询参数
  224. * @returns {Promise} 设备列表
  225. */
  226. export function fetchFieldDeviceList(fieldId, params = {}) {
  227. return http.request({
  228. url: 'uniapp/device/list',
  229. method: Method.GET,
  230. params: {
  231. ...params,
  232. fieldId: fieldId
  233. },
  234. needToken: true
  235. });
  236. }
  237. /**
  238. * 获取设备类型统计数据
  239. * @param {string} fieldId - 地块ID,可选
  240. * @returns {Promise} 设备类型统计数据
  241. */
  242. export function fetchDeviceTypeStats(fieldId) {
  243. const params = {};
  244. if (fieldId) params.fieldId = fieldId;
  245. return http.request({
  246. url: 'uniapp/device/stats/type',
  247. method: Method.GET,
  248. params: params,
  249. needToken: true,
  250. });
  251. }