setting.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view class="container">
  3. <view class="setting">
  4. <view class="prop-item">
  5. <view class="prop-title">充电电流</view>
  6. <view class="prop-value">
  7. <view :style="item.style" class="value-tag" @click="checkItem(current,item)" v-for="item in current.items">
  8. {{ item.text }}
  9. </view>
  10. </view>
  11. <view class="prop-input" v-if="current.custom">
  12. <input v-model="current.customValue" class="input" :placeholder="i18('请输入电流')"/>
  13. <view style="color: #0E9F9B">A</view>
  14. </view>
  15. </view>
  16. </view>
  17. <view class="bottom-area">
  18. <view class="btn" @click="confirm">{{i18('确认')}}</view>
  19. <view class="areas">
  20. <view class="areas-item" @click="sendMainboardCmd">
  21. <view class="areas-img">
  22. <image style="width: 50px;height: 50px" :src="imgUrl+'/seting/mainboard.png'"></image>
  23. </view>
  24. <view>
  25. <view>{{i18('获取主板配置')}}</view>
  26. </view>
  27. </view>
  28. <view class="areas-item" @click="resetMainboard">
  29. <view class="areas-img">
  30. <image style="width: 50px;height: 50px" :src="imgUrl+'/seting/reset.png'"></image>
  31. </view>
  32. <view>
  33. <view >{{i18('恢复默认配置')}}</view>
  34. </view>
  35. </view>
  36. <view class="areas-item" @click="restartMainboard">
  37. <view class="areas-img">
  38. <image style="width: 50px;height: 50px" :src="imgUrl+'/seting/restart.png'"></image>
  39. </view>
  40. <view>
  41. <view>{{i18('重启充电桩')}}</view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import {setCurrent, getMainbordConfig,sendMainboardCmd, getDeviceInfo, reset, restart} from '@/api/device/current.js'
  50. import websocket from '@/utils/websocket'
  51. import i18 from '@/utils/i18.js'
  52. export default {
  53. data() {
  54. return {
  55. scriptTask:null,
  56. deviceId: "",
  57. ccid:"",
  58. visitTime: "",
  59. mainBoardInfo:{},
  60. current: {
  61. items: [
  62. {text: "8A", style: "", value: 8},
  63. {text: "10A", style: "", value: 10},
  64. {text: "16A", style: "", value: 16},
  65. {text: "32A", style: "", value: 32},
  66. {text: i18("自定义"), style: "", value: -1}
  67. ],
  68. custom: false,
  69. customValue: ''
  70. }
  71. }
  72. },
  73. onUnload (){
  74. this.closeSocket();
  75. },
  76. computed: {
  77. imgUrl: function () {
  78. return getApp().globalData.config.imgUrl;
  79. }
  80. },
  81. onLoad(opt) {
  82. this.deviceId = opt.id;
  83. this.ccid = opt.ccid;
  84. this.sendMainboardCmd();
  85. },
  86. methods: {
  87. i18(text){
  88. return i18(text)
  89. },
  90. closeSocket(){
  91. this.scriptTask.close();
  92. },
  93. initSocket(key){
  94. let self = this;
  95. let socketUrl = getApp().globalData.config.socketUrl
  96. this.scriptTask = websocket({
  97. url:"/"+key+"/",
  98. });
  99. let scriptTask = this.scriptTask;
  100. scriptTask.onOpen(function (res) {
  101. console.log('WebSocket连接已打开!');
  102. self.connected = true;
  103. });
  104. scriptTask.onError(function (res) {
  105. self.connected = false;
  106. console.log(res);
  107. });
  108. scriptTask.onMessage(function (res) {
  109. let data = JSON.parse(res.data);
  110. let type = data.type;
  111. let real_data = data.real_data;
  112. if(type == 103){
  113. self.portDetail = real_data;
  114. self.$modal.closeLoading();
  115. }
  116. if(type == 116){
  117. self.$modal.closeLoading();
  118. }
  119. if(type == 96){
  120. self.mainBoardInfo = real_data;
  121. self.formatMainboardData();
  122. self.$modal.closeLoading();
  123. }
  124. console.log('收到服务器内容:' + JSON.stringify(data));
  125. });
  126. scriptTask.onClose(function (res) {
  127. self.connected = true;
  128. console.log('WebSocket 已关闭!');
  129. });
  130. },
  131. formatMainboardData(){
  132. let mainBoardInfo = this.mainBoardInfo;
  133. let max_current = mainBoardInfo.max_current;
  134. max_current = max_current/100;
  135. this.setCurrent(max_current);
  136. },
  137. setCurrent(max_current){
  138. this.current.currenValue = max_current;
  139. let items = this.current.items;
  140. let len = items.length;
  141. let hasValue = false;
  142. this.current.custom = false;
  143. for (let i = 0; i < len; i++) {
  144. let dian = items[i];
  145. if(dian.value == max_current){
  146. hasValue = true;
  147. dian.style = "; background: #0E9F9B;color:white"
  148. }else{
  149. dian.style = "; background: rgb(227, 242, 245);color: #0E9F9B;"
  150. }
  151. }
  152. if(!hasValue){
  153. this.current.custom = true;
  154. items[len-1].style = "; background: #0E9F9B;color:white"
  155. }
  156. this.current.customValue = max_current;
  157. },
  158. async checkItem(obj, item) {
  159. let items = obj.items;
  160. let len = items.length;
  161. for (let i = 0; i < len; i++) {
  162. let dian = items[i];
  163. dian.style = "; background: rgb(227, 242, 245);color: #0E9F9B;"
  164. }
  165. item.style = "; background: #0E9F9B;color:white"
  166. if (item.value == -1) {
  167. obj.custom = true;
  168. obj.customValue = 0;
  169. } else {
  170. obj.custom = false;
  171. obj.customValue = item.value;
  172. }
  173. },
  174. async confirm() {
  175. let currenValue = this.current.customValue;
  176. currenValue = currenValue *100;
  177. let deviceId = this.deviceId;
  178. // 发送到后端
  179. let self = this;
  180. this.$modal.loading("保存中。。");
  181. setCurrent({deviceId: deviceId,ccid:this.ccid},currenValue).then(res => {
  182. this.$modal.showToast("保存成功");
  183. setTimeout(function(){
  184. self.sendMainboardCmd();
  185. },1000)
  186. })
  187. },
  188. sendMainboardCmd() {
  189. this.$modal.loading("正在获取状态,请稍等...");
  190. let deviceId = this.deviceId;
  191. // 获取主板配置
  192. sendMainboardCmd({deviceId: deviceId,ccid:this.ccid}).then(res => {
  193. this.visitTime = res.msg;
  194. if(!this.visitTime){
  195. this.$modal.msg("请重新进入页面");
  196. return;
  197. }
  198. if(!this.scriptTask){
  199. this.initSocket(this.deviceId)
  200. }
  201. })
  202. },
  203. getMainboardConfig(){
  204. this.startTimer();
  205. },
  206. startTimer(){
  207. let self = this;
  208. this.timer = setTimeout(function (){
  209. getMainbordConfig({deviceId: self.deviceId,ccid:this.ccid},self.visitTime).then(res=>{
  210. let data = res.data;
  211. if(data != null){
  212. self.mainBoardInfo = data;
  213. self.formatMainboardData();
  214. self.$modal.closeLoading();
  215. }else{
  216. self.startTimer();
  217. }
  218. });
  219. },1000);
  220. },
  221. resetMainboard() {
  222. let deviceId = this.deviceId;
  223. // 恢复默认配置
  224. let self = this;
  225. this.$modal.loading("正在重置,请稍等...");
  226. reset({deviceId:deviceId,ccid:this.ccid}).then(res => {
  227. setTimeout(function(){
  228. self.sendMainboardCmd();
  229. },1000)
  230. })
  231. },
  232. async restartMainboard() {
  233. let deviceId = this.deviceId;
  234. this.$modal.loading("正在重启中,请稍等...");
  235. // 重启充电桩
  236. let self = this;
  237. restart({deviceId:deviceId,ccid:this.ccid}).then(res => {
  238. setTimeout(function(){
  239. self.sendMainboardCmd();
  240. },1000)
  241. })
  242. }
  243. }
  244. }
  245. </script>
  246. <style>
  247. .container {
  248. position: absolute;
  249. inset: 0;
  250. background: rgb(249, 252, 255);
  251. }
  252. .prop-item {
  253. padding: 2vh;
  254. }
  255. .prop-value {
  256. display: flex;
  257. flex-direction: row;
  258. }
  259. .prop-title {
  260. color: #252525;
  261. font-weight: bold;
  262. margin-bottom: 2.5vh;
  263. }
  264. .value-tag {
  265. background: rgb(227, 242, 245);
  266. color: #0E9F9B;
  267. width: 14vw;
  268. height: 4vh;
  269. line-height: 4vh;
  270. border-radius: 3vw;
  271. text-align: center;
  272. margin: 0 1vw;
  273. font-weight: bold;
  274. font-size: 12px;
  275. }
  276. .prop-input {
  277. display: flex;
  278. flex-direction: row;
  279. line-height: 4vh;
  280. margin: 2vh 0;
  281. }
  282. .input {
  283. background: rgb(227, 242, 245);
  284. border-radius: 30px;
  285. font-size: 12px;
  286. height: 4vh;
  287. padding: 0 2vw;
  288. margin: 0 1vw;
  289. width: 90%;
  290. }
  291. .bottom-area {
  292. position: absolute;
  293. bottom: 0px;
  294. height: 24vh;
  295. left: 0;
  296. right: 0;
  297. }
  298. .btn {
  299. background: #0E9F9B;
  300. text-align: center;
  301. color: white;
  302. height: 6vh;
  303. line-height: 6vh;
  304. margin: 0 5vh;
  305. border-radius: 1vh;
  306. border-style: none;
  307. }
  308. .btn1 {
  309. background: #f9fcff;
  310. /* text-align: center;
  311. color: white;
  312. height:6vh;
  313. line-height: 6vh;
  314. margin:0 5vh;
  315. border-radius: 1vh; */
  316. border-color: transparent;
  317. }
  318. .areas {
  319. display: flex;
  320. flex-direction: row;
  321. text-align: center;
  322. margin-top: 3vh
  323. }
  324. .areas-item {
  325. width: 33%;
  326. }
  327. .areas-img {
  328. text-align: center;
  329. }
  330. </style>