index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <div class="l-flex--col center has-border radius has-padding">
  3. <i
  4. class="o-icon lg u-pointer"
  5. @click="invoke"
  6. />
  7. <div class="has-padding u-color--black u-bold">网络设置</div>
  8. <confirm-dialog
  9. ref="networkDialog"
  10. title="网络设置"
  11. @confirm="onSave"
  12. >
  13. <!-- <tabbar
  14. :items="tabs"
  15. :active.sync="active"
  16. /> -->
  17. <wired
  18. key="wired"
  19. class="u-align-self--center"
  20. :eth.sync="eth"
  21. :eths="eths"
  22. />
  23. <!-- <wireless
  24. v-if="isWireless"
  25. key="wireless"
  26. class="u-align-self--center"
  27. :wireless="wireless"
  28. /> -->
  29. </confirm-dialog>
  30. </div>
  31. </template>
  32. <script>
  33. import {
  34. subscribe,
  35. unsubscribe
  36. } from '@/utils/mqtt'
  37. import {
  38. validIPv4,
  39. validIPv6
  40. } from '@/utils/validate'
  41. import { saveBoxLogger } from '@/api/platform'
  42. import baseMixin from '../mixins/base'
  43. import Wired from './Wired'
  44. // import Wireless from './Wireless'
  45. export default {
  46. name: 'DeviceNetwork',
  47. components: {
  48. Wired
  49. // Wired,
  50. // Wireless
  51. },
  52. mixins: [baseMixin],
  53. data () {
  54. return {
  55. tabs: [
  56. { key: 'wired', name: '有线网络' },
  57. { key: 'wireless', name: '无线网络' }
  58. ],
  59. active: null,
  60. eth: '0',
  61. eths: null,
  62. wireless: null
  63. }
  64. },
  65. computed: {
  66. isWired () {
  67. return this.active === 'wired'
  68. },
  69. wired () {
  70. return this.eths?.[this.eth]
  71. },
  72. isWireless () {
  73. return this.active === 'wireless'
  74. },
  75. replyTopic () {
  76. return `${this.device.productId}/${this.device.id}/function/invoke/reply`
  77. }
  78. },
  79. created () {
  80. subscribe([this.replyTopic], this.onMessage)
  81. },
  82. beforeDestroy () {
  83. unsubscribe([this.replyTopic], this.onMessage)
  84. },
  85. methods: {
  86. onMessage (topic, message) {
  87. if (!this.$messageId || topic !== this.replyTopic) {
  88. return
  89. }
  90. message = JSON.parse(message)
  91. if (message.messageId === this.$messageId) {
  92. this.closeAsyncLoading()
  93. try {
  94. const eths = JSON.parse(message.output)
  95. if (eths?.length) {
  96. this.showNetwork(eths)
  97. return
  98. }
  99. } catch (e) {
  100. console.log(e)
  101. }
  102. this.$message({
  103. type: 'warning',
  104. message: '未发现设备网口,请稍后重试'
  105. })
  106. }
  107. },
  108. invoke () {
  109. this.sendTopic(
  110. 'network',
  111. [],
  112. '未获取到设备网络状态,请稍后重试'
  113. )
  114. },
  115. showNetwork (eths) {
  116. this.active = 'wired'
  117. this.eth = '0'
  118. this.eths = eths.map(eth => this.createEth(eth))
  119. this.wireless = {
  120. enable: true,
  121. name: '',
  122. password: '',
  123. auto: false
  124. }
  125. this.$refs.networkDialog.show()
  126. },
  127. createEth (eth) {
  128. const { name, setting, address, subnetMask, defaultGateway, preferredDNS, alternativeDNS } = eth
  129. return {
  130. name,
  131. mode: setting === 'manual' ? 'manual' : 'dhcp',
  132. ip4: {
  133. enable: true,
  134. address: address || '',
  135. mask: subnetMask || '',
  136. gateway: defaultGateway || '',
  137. dns1: preferredDNS || '',
  138. dns2: alternativeDNS || ''
  139. },
  140. ip6: this.createIPv6Setting()
  141. }
  142. },
  143. createIPv6Setting () {
  144. return {
  145. enable: false,
  146. address: '',
  147. mask: 32,
  148. gateway: '',
  149. dns1: '',
  150. dns2: ''
  151. }
  152. },
  153. onSave (done) {
  154. this.isWired && this.onWired(done)
  155. this.isWireless && this.onWireless(done)
  156. },
  157. onWired (done) {
  158. if (!this.validWired()) {
  159. return
  160. }
  161. const { name, mode, ip4, ip6 } = this.wired
  162. const inputs = []
  163. if (mode === 'dhcp') {
  164. inputs.push({
  165. name,
  166. value: JSON.stringify({ setting: mode })
  167. })
  168. // inputs.push({
  169. // name: `${name}-ipv6`,
  170. // value: JSON.stringify({ setting: mode })
  171. // })
  172. } else {
  173. if (ip4.enable) {
  174. const { address, mask, gateway, dns1, dns2 } = ip4
  175. inputs.push({
  176. name,
  177. value: JSON.stringify({
  178. setting: mode,
  179. address,
  180. subnetMask: mask,
  181. defaultGateway: gateway,
  182. preferredDNS: dns1,
  183. alternativeDNS: dns2
  184. })
  185. })
  186. }
  187. if (ip6.enable) {
  188. const { address, mask, gateway, dns1, dns2 } = ip6
  189. inputs.push({
  190. name: `${name}-ipv6`,
  191. value: JSON.stringify({
  192. name,
  193. setting: mode,
  194. address,
  195. subnetPrefixLegth: mask,
  196. defaultGateway: gateway,
  197. preferredDNS: dns1,
  198. alternativeDNS: dns2
  199. })
  200. })
  201. }
  202. }
  203. this.$confirm(
  204. `将${name}有线网络设置为配置内容?`,
  205. { type: 'warning' }
  206. ).then(() => {
  207. done()
  208. this.sendTopic('wiredNetwork', inputs).then(() => {
  209. saveBoxLogger({
  210. description: `将设备【${this.device.name}】的IP模式设为${mode === 'dhcp' ? 'DHCP' : '静态IP'}`,
  211. method: '网络设置',
  212. params: JSON.stringify({
  213. id: this.deviceId,
  214. name: this.device.name,
  215. ...inputs
  216. })
  217. })
  218. })
  219. })
  220. },
  221. onWireless (done) {
  222. if (!this.validWireless()) {
  223. return
  224. }
  225. const inputs = []
  226. const { enable, name, password, auto } = this.wireless
  227. if (enable) {
  228. inputs.push({
  229. name: 'wirelessFidelity',
  230. value: {
  231. status: 1,
  232. name,
  233. password,
  234. auto: auto ? 1 : 0
  235. }
  236. })
  237. } else {
  238. inputs.push({
  239. name: 'wirelessFidelity',
  240. value: JSON.stringify({ status: -1 })
  241. })
  242. }
  243. this.$confirm(
  244. '将无线网络设置为配置内容?',
  245. { type: 'warning' }
  246. ).then(() => {
  247. done()
  248. this.sendTopic('wirelessFidelity', inputs)
  249. })
  250. },
  251. onError (message) {
  252. this.$message({
  253. type: 'warning',
  254. message
  255. })
  256. return false
  257. },
  258. validWired () {
  259. if (this.wired.mode === 'manual') {
  260. const { ip4, ip6 } = this.wired
  261. if (!ip4.enable && !ip6.enable) {
  262. return this.onError('请配置IPv4或IPv6')
  263. }
  264. if (ip4.enable && !this.validIPv4(ip4)) {
  265. return false
  266. }
  267. if (ip6.enable && !this.validIPv6(ip6)) {
  268. return false
  269. }
  270. }
  271. return true
  272. },
  273. validIPv4 (ip4) {
  274. const { address, mask, gateway, dns1, dns2 } = ip4
  275. const type = 'IPv4'
  276. if (!this.isValid(address, validIPv4, type, 'IP地址')) {
  277. return false
  278. }
  279. if (!this.isValid(mask, validIPv4, type, '子网掩码')) {
  280. return false
  281. }
  282. if (!this.isValid(gateway, validIPv4, type, '网关')) {
  283. return false
  284. }
  285. if (dns1 && !validIPv4(dns1)) {
  286. return this.onError('IPv4的主DNS格式错误')
  287. }
  288. if (dns2 && !validIPv4(dns2)) {
  289. return this.onError('IPv4的备DNS格式错误')
  290. }
  291. return true
  292. },
  293. validIPv6 (ip6) {
  294. const { address, gateway, dns1, dns2 } = ip6
  295. const type = 'IPv6'
  296. if (!this.isValid(address, validIPv6, type, 'IP地址')) {
  297. return false
  298. }
  299. if (gateway && !validIPv6(gateway)) {
  300. return this.onError('IPv6的网关格式错误')
  301. }
  302. if (dns1 && !validIPv6(dns1)) {
  303. return this.onError('IPv6的主DNS格式错误')
  304. }
  305. if (dns2 && !validIPv6(dns2)) {
  306. return this.onError('IPv6的备DNS格式错误')
  307. }
  308. return true
  309. },
  310. isValid (val, validate, type, name) {
  311. if (!val) {
  312. return this.onError(`请输入${type}的${name}`)
  313. }
  314. if (!validate(val)) {
  315. return this.onError(`${type}的${name}格式错误`)
  316. }
  317. return true
  318. },
  319. validWireless () {
  320. if (this.wireless.enable) {
  321. const { name, password } = this.wireless
  322. if (!name) {
  323. return this.onError('请输入网络名称')
  324. }
  325. if (!password) {
  326. return this.onError('请输入密码')
  327. }
  328. }
  329. return true
  330. }
  331. }
  332. }
  333. </script>
  334. <style lang="scss" scoped>
  335. .o-icon {
  336. background-image: url("~@/assets/icon_screen_network.png");
  337. }
  338. </style>