index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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">{{ $t("alarmClockSet.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 { t } = useI18n();
  97. const onConfirm = ({ selectedValues }) => {
  98. console.log(selectedValues);
  99. currentTime.value = selectedValues;
  100. currentTimeShow.value = false;
  101. hour.value = selectedValues.join(':');
  102. console.log(hour.value);
  103. };
  104. console.log(t("alarmClockSet.sunday"));
  105. // 周
  106. const weekDayList = ref([
  107. {
  108. name: t("alarmClockSet.sunday"),
  109. value: '7',
  110. },
  111. {
  112. name: t("alarmClockSet.monday"),
  113. value: '1',
  114. },
  115. {
  116. name: t("alarmClockSet.tuesday"),
  117. value: '2',
  118. },
  119. {
  120. name: t("alarmClockSet.wednesday"),
  121. value: '3',
  122. },
  123. {
  124. name: t("alarmClockSet.thursday"),
  125. value: '4',
  126. },
  127. {
  128. name: t("alarmClockSet.friday"),
  129. value: '5',
  130. },
  131. {
  132. name: t("alarmClockSet.saturday"),
  133. value: '6',
  134. },
  135. ]);
  136. // 选中的周
  137. const selectList = ref(['1', '2', '3', '4', '5', '6', '7']);
  138. // 点击周
  139. const selectDayClk = (val) => {
  140. // 如果已经选过了,则需要取消
  141. if (selectList.value.includes(val)) {
  142. const idx = selectList.value.indexOf(val);
  143. selectList.value.splice(idx, 1);
  144. } else {
  145. selectList.value.push(val);
  146. }
  147. };
  148. // 全选
  149. const checkedAll = ref(true);
  150. const checkedAllClk = () => {
  151. checkedAll.value = !checkedAll.value;
  152. if (!checkedAll.value) {
  153. selectList.value = ['1', '2', '3', '4', '5', '6', '7'];
  154. } else {
  155. selectList.value = [];
  156. }
  157. };
  158. // 监听选中的周
  159. watch(() => selectList, (newVal) => {
  160. if (newVal.value.length === 7) {
  161. checkedAll.value = true;
  162. } else {
  163. checkedAll.value = false;
  164. }
  165. }, { immediate: true, deep: true });
  166. const user = getLoginUser();
  167. const route = useRoute();
  168. const router = useRouter();
  169. const equipmentIds = ref([]);
  170. const equipmentIdsText = ref("");
  171. const equipmentShow = ref(false);
  172. const equipmentList = ref([]);
  173. // 初始化页面获取列表
  174. onMounted(async () => {
  175. // 加载样式
  176. styleUrl('alarmClock');
  177. if (route.query.deviceId) {
  178. equipmentIds.value.push(route.query.deviceId);
  179. }
  180. if (route.query.alarmId) {
  181. getAlaramClockFun();
  182. }
  183. getListEquipmentFun();
  184. });
  185. let oldAlarmClock = {};
  186. // 闹钟回显
  187. const getAlaramClockFun = async () => {
  188. const { data } = await getAlaramClock({ id: route.query.alarmId });
  189. if (data.code) {
  190. oldAlarmClock = data.data;
  191. // 回显时间
  192. const hourVal = data.data.hour.slice(0, 5).split(":");
  193. currentTime.value = hourVal;
  194. hour.value = data.data.hour.slice(0, 5);
  195. alarmClockName.value = data.data.name;
  196. checked.value = data.data.status === "1";
  197. alarmClockType.value = data.data.type;
  198. // 选中的周
  199. selectList.value = data.data.week.split(',');
  200. equipmentIds.value = data.data.equipmentIds.split(",");
  201. isEquipemntIdsText();
  202. // 回显闹钟类型
  203. const statusTxt = alarmClockTypeOptions.value.filter(
  204. (item) => item.value === data.data.type
  205. );
  206. alarmClockTypeText.value = statusTxt[0]["text"];
  207. } else {
  208. showFailToast(data.message);
  209. }
  210. };
  211. // 获取旗下机器
  212. const getListEquipmentFun = async () => {
  213. const { data } = await getListEquipment({ adminId: user.id });
  214. if (data.code === "00000") {
  215. equipmentList.value = [
  216. { label: t("device.allMachines"), value: "All" },
  217. ].concat(
  218. data.data.map((item, index) => {
  219. return { label: item.name || index, value: item.id.toString() };
  220. })
  221. );
  222. isEquipemntIdsText();
  223. } else {
  224. showFailToast(data.message);
  225. }
  226. };
  227. // 设备多选
  228. const equipementCheckItem = (index) => {
  229. const list = equipmentIds.value.filter((v) => v === index);
  230. if (list.length > 0) {
  231. equipmentIds.value = equipmentIds.value.filter((v) => v !== index);
  232. } else {
  233. equipmentIds.value.push(index);
  234. }
  235. isAllEquipement();
  236. };
  237. const equipemntIdsChange = (event) => {
  238. const isAll = equipmentIds.value.filter((v) => v === "All");
  239. if (
  240. isAll.length > 0 &&
  241. equipmentIds.value.length !== equipmentList.value.length
  242. ) {
  243. equipmentIds.value = [];
  244. isAllEquipement();
  245. } else {
  246. equipmentIds.value = event;
  247. isEquipemntIdsText();
  248. }
  249. };
  250. const isEquipemntIdsText = () => {
  251. const equipmentLabelList = [];
  252. const isAll = equipmentIds.value.filter((v) => v === "All");
  253. if (isAll.length > 0) {
  254. equipmentLabelList.push(t("device.allMachines"));
  255. } else {
  256. equipmentIds.value.forEach((item) => {
  257. const equipment = equipmentList.value.filter((v) => v.value === item);
  258. if (equipment.length > 0) {
  259. equipmentLabelList.push(equipment[0].label);
  260. }
  261. });
  262. }
  263. equipmentIdsText.value = equipmentLabelList.join(",");
  264. };
  265. const isAllEquipement = () => {
  266. const isAll = equipmentIds.value.filter((v) => v === "All");
  267. if (isAll.length > 0) {
  268. equipmentIds.value = equipmentList.value.map((item) => {
  269. return item.value;
  270. });
  271. }
  272. isEquipemntIdsText();
  273. };
  274. const hour = ref("");
  275. const currentTime = ref(['12', '00']);
  276. const currentTimeShow = ref(false);
  277. const alarmClockTypeText = ref("");
  278. const alarmClockType = ref("");
  279. const alarmClockTypeShow = ref(false);
  280. const alarmClockTypeOptions = ref([
  281. { text: t("device.furnaceHeadOn"), value: "0" },
  282. { text: t("device.furnaceHeadClosed"), value: "1" },
  283. { text: t("device.startSleep"), value: "2" },
  284. { text: t("device.endSleep"), value: "3" },
  285. ]);
  286. const alarmClockTypeFinish = ({ selectedOptions }) => {
  287. alarmClockTypeShow.value = false;
  288. alarmClockTypeText.value = selectedOptions
  289. .map((option) => option.text)
  290. .join("/");
  291. };
  292. const alarmClockName = ref("");
  293. const checked = ref(false);
  294. const submitSet = async () => {
  295. const equipmentIdsList = equipmentIds.value.filter((v) => v !== "All");
  296. // 选中的周
  297. const weekParams = selectList.value.join(',');
  298. const params = {
  299. adminId: user.id,
  300. equipmentIds: equipmentIdsList.join(","),
  301. hour: hour.value,
  302. name: alarmClockName.value,
  303. status: checked.value ? "1" : "0",
  304. type: alarmClockType.value,
  305. week: weekParams,
  306. };
  307. // 校验
  308. if (!params.equipmentIds) {
  309. showToast(t("device.selectDevice"));
  310. return;
  311. } else if (!params.hour) {
  312. showToast(t("device.selectTime"));
  313. return;
  314. } else if (!params.week) {
  315. showToast(t("device.repetitionsPlaceholder"));
  316. return;
  317. } else if (!params.type) {
  318. showToast(t("device.alarmTypePlaceholder"));
  319. return;
  320. }
  321. if (route.query.alarmId) {
  322. params.id = oldAlarmClock.id;
  323. }
  324. const { data } = route.query.deviceId
  325. ? await addAlaramClock(params)
  326. : await alaramClockUpdate(params);
  327. console.log("data", data);
  328. if (data.code) {
  329. showSuccessToast(t("device.successfullyAddedAlarmClock"));
  330. router.back();
  331. } else {
  332. showFailToast(data.message);
  333. }
  334. };
  335. return {
  336. equipmentIds,
  337. equipmentIdsText,
  338. equipmentShow,
  339. equipmentList,
  340. equipementCheckItem,
  341. equipemntIdsChange,
  342. hour,
  343. currentTime,
  344. currentTimeShow,
  345. alarmClockTypeText,
  346. alarmClockTypeShow,
  347. alarmClockType,
  348. alarmClockTypeOptions,
  349. alarmClockTypeFinish,
  350. alarmClockName,
  351. checked,
  352. submitSet,
  353. weekDayList,
  354. selectList,
  355. selectDayClk,
  356. checkedAll,
  357. checkedAllClk,
  358. onConfirm,
  359. };
  360. },
  361. };
  362. </script>
  363. <style lang="less" scoped>
  364. @import "../../../common/style/common";
  365. </style>