index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <!-- 设置闹钟 -->
  3. <div class="alarmClockSetPage flex-col">
  4. <s-header :name="$t('device.setAlarm')" :noback="false"></s-header>
  5. <div class="alarmClockSetBox flex-col">
  6. <div style="width: 100%; height: 18px"></div>
  7. <van-field class="equipmentField" is-link readonly :label="$t('device.equipmentName')"
  8. :placeholder="$t('device.selectDevice')" v-model="equipmentIdsText" @click="equipmentShow = true" />
  9. <van-field v-model="hour" is-link readonly :label="$t('device.selectTime')" :placeholder="$t('device.selectTime')"
  10. @click="currentTimeShow = true" class="field" />
  11. <div class="repeatTimeBox">
  12. <span class="txt6">{{
  13. $t("device.selectTheNumberOfAlarmClockRepetitions")
  14. }}</span>
  15. <div class="o-pl-15 o-ptb-10">
  16. <van-checkbox @click="checkedAllClk" v-model="checkedAll">全选</van-checkbox>
  17. </div>
  18. <div class="l-flex-between dayCon">
  19. <div @click="selectDayClk(item.value)" class="dayName" :class="{ active: selectList.includes(item.value) }"
  20. v-for="(item, index) in weekDayList" :key="index">
  21. {{ item.name }}
  22. </div>
  23. </div>
  24. </div>
  25. <van-field v-model="alarmClockTypeText" is-link readonly :label="$t('device.alarmTypeLabel')"
  26. :placeholder="$t('device.alarmTypePlaceholder')" @click="alarmClockTypeShow = true" class="field" />
  27. <van-field v-model="alarmClockName" name="alarmClockName" :label="$t('device.alarmNameLabel')"
  28. :placeholder="$t('device.alarmNamePlaceholder')" />
  29. <div class="ImageText1 flex-col">
  30. <div class="mod4 flex-row justify-between">
  31. <div class="TextGroup1 flex-col">
  32. <span class="info4">{{ $t("device.effectiveImmediately") }}:</span>
  33. </div>
  34. <div class="section1 flex-col">
  35. <van-switch v-model="checked" />
  36. </div>
  37. </div>
  38. </div>
  39. <van-row justify="space-around" style="padding: 1em">
  40. <van-button span="5" round type="primary" style="height: 2em; padding: 0 2em" @click="submitSet">
  41. {{ $t("device.submitSettings") }}</van-button>
  42. </van-row>
  43. </div>
  44. <van-popup v-model:show="alarmClockTypeShow" round position="bottom">
  45. <van-cascader v-model="alarmClockType" :title="$t('device.alarmTypePlaceholder')" :options="alarmClockTypeOptions"
  46. @close="alarmClockTypeShow = false" @finish="alarmClockTypeFinish" />
  47. </van-popup>
  48. <van-popup v-model:show="currentTimeShow" round position="bottom">
  49. <van-time-picker v-model="currentTime" :title="$t('device.clickToSelectTheTime')" @confirm="onConfirm"
  50. @cancel="currentTimeShow = false" />
  51. </van-popup>
  52. <van-popup v-model:show="equipmentShow" position="bottom">
  53. <div class="van-picker__toolbar">
  54. <button type="button" class="van-picker__cancel" style="margin: 0 0.5em" @click="equipmentShow = false">
  55. {{ $t("device.cancel") }}
  56. </button>
  57. <div class="van-ellipsis van-picker__title">
  58. {{ $t("device.selectDevice") }}
  59. </div>
  60. <button type="button" class="van-picker__confirm" style="margin: 0 0.5em" @click="equipmentShow = false">
  61. {{ $t("device.confirm") }}
  62. </button>
  63. </div>
  64. <div class="checkbox-con" style="max-height: 264px; overflow-y: auto">
  65. <van-checkbox-group v-model="equipmentIds" @change="equipemntIdsChange" ref="checkboxGroup">
  66. <van-cell-group>
  67. <van-cell v-for="(item, index) in equipmentList" clickable :key="index" :title="item.label"
  68. @click="equipementCheckItem(item.value)">
  69. <template #right-icon>
  70. <van-checkbox :name="item.value" />
  71. </template>
  72. </van-cell>
  73. </van-cell-group>
  74. </van-checkbox-group>
  75. </div>
  76. </van-popup>
  77. </div>
  78. </template>
  79. <script>
  80. import { showSuccessToast, showFailToast, showToast } from "vant";
  81. import { onMounted, ref, watch } from "vue";
  82. import sHeader from "@/components/SimpleHeader";
  83. import { getLoginUser, styleUrl } from "@/common/js/utils";
  84. import {
  85. getListEquipment,
  86. addAlaramClock,
  87. getAlaramClock,
  88. alaramClockUpdate,
  89. } from "@/service/device";
  90. import { useRoute, useRouter } from "vue-router";
  91. import { useI18n } from "vue-i18n";
  92. export default {
  93. name: "alarm-clock-set",
  94. components: { sHeader },
  95. setup() {
  96. const onConfirm = ({ selectedValues }) => {
  97. console.log(selectedValues);
  98. currentTime.value = selectedValues;
  99. currentTimeShow.value = false;
  100. hour.value = selectedValues.join(':');
  101. console.log(hour.value);
  102. };
  103. // 周
  104. const weekDayList = ref([
  105. {
  106. name: '日',
  107. value: '7',
  108. },
  109. {
  110. name: '一',
  111. value: '1',
  112. },
  113. {
  114. name: '二',
  115. value: '2',
  116. },
  117. {
  118. name: '三',
  119. value: '3',
  120. },
  121. {
  122. name: '四',
  123. value: '4',
  124. },
  125. {
  126. name: '五',
  127. value: '5',
  128. },
  129. {
  130. name: '六',
  131. value: '6',
  132. },
  133. ]);
  134. // 选中的周
  135. const selectList = ref(['1', '2', '3', '4', '5', '6', '7']);
  136. // 点击周
  137. const selectDayClk = (val) => {
  138. // 如果已经选过了,则需要取消
  139. if (selectList.value.includes(val)) {
  140. const idx = selectList.value.indexOf(val);
  141. selectList.value.splice(idx, 1);
  142. } else {
  143. selectList.value.push(val);
  144. }
  145. };
  146. // 全选
  147. const checkedAll = ref(true);
  148. const checkedAllClk = () => {
  149. checkedAll.value = !checkedAll.value;
  150. if (!checkedAll.value) {
  151. selectList.value = ['1', '2', '3', '4', '5', '6', '7'];
  152. } else {
  153. selectList.value = [];
  154. }
  155. };
  156. // 监听选中的周
  157. watch(() => selectList, (newVal) => {
  158. if (newVal.value.length === 7) {
  159. checkedAll.value = true;
  160. } else {
  161. checkedAll.value = false;
  162. }
  163. }, { immediate: true, deep: true });
  164. const { t } = useI18n();
  165. const user = getLoginUser();
  166. const route = useRoute();
  167. const router = useRouter();
  168. const equipmentIds = ref([]);
  169. const equipmentIdsText = ref("");
  170. const equipmentShow = ref(false);
  171. const equipmentList = ref([]);
  172. // 初始化页面获取列表
  173. onMounted(async () => {
  174. // 加载样式
  175. styleUrl('alarmClock');
  176. if (route.query.deviceId) {
  177. equipmentIds.value.push(route.query.deviceId);
  178. }
  179. if (route.query.alarmId) {
  180. getAlaramClockFun();
  181. }
  182. getListEquipmentFun();
  183. });
  184. let oldAlarmClock = {};
  185. // 闹钟回显
  186. const getAlaramClockFun = async () => {
  187. const { data } = await getAlaramClock({ id: route.query.alarmId });
  188. if (data.code) {
  189. oldAlarmClock = data.data;
  190. // 回显时间
  191. const hourVal = data.data.hour.slice(0, 5).split(":");
  192. currentTime.value = hourVal;
  193. hour.value = data.data.hour.slice(0, 5);
  194. alarmClockName.value = data.data.name;
  195. checked.value = data.data.status === "1";
  196. alarmClockType.value = data.data.type;
  197. // 选中的周
  198. selectList.value = data.data.week.split(',');
  199. equipmentIds.value = data.data.equipmentIds.split(",");
  200. isEquipemntIdsText();
  201. // 回显闹钟类型
  202. const statusTxt = alarmClockTypeOptions.value.filter(
  203. (item) => item.value === data.data.type
  204. );
  205. alarmClockTypeText.value = statusTxt[0]["text"];
  206. } else {
  207. showFailToast(data.message);
  208. }
  209. };
  210. // 获取旗下机器
  211. const getListEquipmentFun = async () => {
  212. const { data } = await getListEquipment({ adminId: user.id });
  213. if (data.code === "00000") {
  214. equipmentList.value = [
  215. { label: t("device.allMachines"), value: "All" },
  216. ].concat(
  217. data.data.map((item, index) => {
  218. return { label: item.name || index, value: item.id.toString() };
  219. })
  220. );
  221. isEquipemntIdsText();
  222. } else {
  223. showFailToast(data.message);
  224. }
  225. };
  226. // 设备多选
  227. const equipementCheckItem = (index) => {
  228. const list = equipmentIds.value.filter((v) => v === index);
  229. if (list.length > 0) {
  230. equipmentIds.value = equipmentIds.value.filter((v) => v !== index);
  231. } else {
  232. equipmentIds.value.push(index);
  233. }
  234. isAllEquipement();
  235. };
  236. const equipemntIdsChange = (event) => {
  237. const isAll = equipmentIds.value.filter((v) => v === "All");
  238. if (
  239. isAll.length > 0 &&
  240. equipmentIds.value.length !== equipmentList.value.length
  241. ) {
  242. equipmentIds.value = [];
  243. isAllEquipement();
  244. } else {
  245. equipmentIds.value = event;
  246. isEquipemntIdsText();
  247. }
  248. };
  249. const isEquipemntIdsText = () => {
  250. const equipmentLabelList = [];
  251. const isAll = equipmentIds.value.filter((v) => v === "All");
  252. if (isAll.length > 0) {
  253. equipmentLabelList.push(t("device.allMachines"));
  254. } else {
  255. equipmentIds.value.forEach((item) => {
  256. const equipment = equipmentList.value.filter((v) => v.value === item);
  257. if (equipment.length > 0) {
  258. equipmentLabelList.push(equipment[0].label);
  259. }
  260. });
  261. }
  262. equipmentIdsText.value = equipmentLabelList.join(",");
  263. };
  264. const isAllEquipement = () => {
  265. const isAll = equipmentIds.value.filter((v) => v === "All");
  266. if (isAll.length > 0) {
  267. equipmentIds.value = equipmentList.value.map((item) => {
  268. return item.value;
  269. });
  270. }
  271. isEquipemntIdsText();
  272. };
  273. const hour = ref("");
  274. const currentTime = ref(['12', '00']);
  275. const currentTimeShow = ref(false);
  276. const alarmClockTypeText = ref("");
  277. const alarmClockType = ref("");
  278. const alarmClockTypeShow = ref(false);
  279. const alarmClockTypeOptions = ref([
  280. { text: t("device.furnaceHeadOn"), value: "0" },
  281. { text: t("device.furnaceHeadClosed"), value: "1" },
  282. { text: t("device.startSleep"), value: "2" },
  283. { text: t("device.endSleep"), value: "3" },
  284. ]);
  285. const alarmClockTypeFinish = ({ selectedOptions }) => {
  286. alarmClockTypeShow.value = false;
  287. alarmClockTypeText.value = selectedOptions
  288. .map((option) => option.text)
  289. .join("/");
  290. };
  291. const alarmClockName = ref("");
  292. const checked = ref(false);
  293. const submitSet = async () => {
  294. const equipmentIdsList = equipmentIds.value.filter((v) => v !== "All");
  295. // 选中的周
  296. const weekParams = selectList.value.join(',');
  297. const params = {
  298. adminId: user.id,
  299. equipmentIds: equipmentIdsList.join(","),
  300. hour: hour.value,
  301. name: alarmClockName.value,
  302. status: checked.value ? "1" : "0",
  303. type: alarmClockType.value,
  304. week: weekParams,
  305. };
  306. // 校验
  307. if (!params.equipmentIds) {
  308. showToast(t("device.selectDevice"));
  309. return;
  310. } else if (!params.hour) {
  311. showToast(t("device.selectTime"));
  312. return;
  313. } else if (!params.week) {
  314. showToast(t("device.repetitionsPlaceholder"));
  315. return;
  316. } else if (!params.type) {
  317. showToast(t("device.alarmTypePlaceholder"));
  318. return;
  319. }
  320. if (route.query.alarmId) {
  321. params.id = oldAlarmClock.id;
  322. }
  323. const { data } = route.query.deviceId
  324. ? await addAlaramClock(params)
  325. : await alaramClockUpdate(params);
  326. console.log("data", data);
  327. if (data.code) {
  328. showSuccessToast(t("device.successfullyAddedAlarmClock"));
  329. router.back();
  330. } else {
  331. showFailToast(data.message);
  332. }
  333. };
  334. return {
  335. equipmentIds,
  336. equipmentIdsText,
  337. equipmentShow,
  338. equipmentList,
  339. equipementCheckItem,
  340. equipemntIdsChange,
  341. hour,
  342. currentTime,
  343. currentTimeShow,
  344. alarmClockTypeText,
  345. alarmClockTypeShow,
  346. alarmClockType,
  347. alarmClockTypeOptions,
  348. alarmClockTypeFinish,
  349. alarmClockName,
  350. checked,
  351. submitSet,
  352. weekDayList,
  353. selectList,
  354. selectDayClk,
  355. checkedAll,
  356. checkedAllClk,
  357. onConfirm,
  358. };
  359. },
  360. };
  361. </script>
  362. <style lang="less" scoped>
  363. @import "../../../common/style/common";
  364. </style>