index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <view class="padding-sm setting-messagePush">
  3. <form action="">
  4. <checkbox-group class="block" @change="CheckboxChange">
  5. <view class="margin-top">
  6. <view class="flex align-center checkItems" v-for="(item, index) in checkbox" :key="index">
  7. <checkbox class="blue" :class="item.checked == 1 ? 'checked' : ''" :checked="item.checked == 1 ? true : false" :value="item.value"></checkbox>
  8. <view class="title">{{ item.title }}</view>
  9. </view>
  10. </view>
  11. </checkbox-group>
  12. <view class="btn-area submitBottomBtn">
  13. <button class="bg-blue round margin-top" @click="$noMultipleClicks(btnSubmit)">提 交</button>
  14. </view>
  15. </form>
  16. </view>
  17. </template>
  18. <script setup>
  19. import { onLoad, onShow, onHide, onLaunch, onReady } from "@dcloudio/uni-app";
  20. import { ref, onMounted, inject, shallowRef, reactive, toRefs, getCurrentInstance } from "vue";
  21. import { useStores, commonStores } from "@/store/modules/index";
  22. import { setperm } from "@/api/business/zhxf/messagePush/index.js";
  23. const useStore = useStores();
  24. const { proxy } = getCurrentInstance();
  25. const data = reactive({
  26. noClick: true,
  27. flag: false,
  28. getData: {},
  29. checkedValue: [],
  30. checkbox: [
  31. {
  32. value: "m1",
  33. checked: false,
  34. title: "火警报警",
  35. },
  36. {
  37. value: "m2",
  38. checked: false,
  39. title: "火警故障",
  40. },
  41. {
  42. value: "m3",
  43. checked: false,
  44. title: "水压越线",
  45. },
  46. {
  47. value: "m4",
  48. checked: false,
  49. title: "开关变化",
  50. },
  51. {
  52. value: "m5",
  53. checked: false,
  54. title: "设备离线",
  55. },
  56. {
  57. value: "m6",
  58. checked: false,
  59. title: "烟感报警",
  60. },
  61. {
  62. value: "m7",
  63. checked: false,
  64. title: "电气火灾",
  65. },
  66. {
  67. value: "m8",
  68. checked: false,
  69. title: "视频报警",
  70. },
  71. {
  72. value: "m9",
  73. checked: false,
  74. title: "电梯监测",
  75. },
  76. {
  77. value: "m10",
  78. checked: false,
  79. title: "井盖监测",
  80. },
  81. ],
  82. });
  83. const { noClick, flag, getData, checkedValue, checkbox } = toRefs(data);
  84. function CheckboxChange(e) {
  85. flag.value = true;
  86. var items = checkbox.value,
  87. values = e.detail.value;
  88. checkedValue.value = values;
  89. // console.log(values)
  90. // console.log('点击复选框时checkedValue.value')
  91. // console.log(checkedValue.value)
  92. for (var i = 0, lenI = items.length; i < lenI; ++i) {
  93. items[i].checked = false;
  94. for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
  95. if (items[i].value == values[j]) {
  96. items[i].checked = true;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. // 回显数据请求
  103. function getCheckList(params = {}) {
  104. setperm(params).then((res) => {
  105. getData.value = res.data[0];
  106. // 遍历checkbox数组对象
  107. for (let i = 0; i < checkbox.value.length; i++) {
  108. //判断value是否存在于返回对象中.存在的话checked重新赋值
  109. if (getData.value.hasOwnProperty(checkbox.value[i].value)) {
  110. // console.log(checkbox.value[i].value)
  111. checkbox.value[i].checked = true;
  112. }
  113. }
  114. });
  115. }
  116. //提交
  117. async function btnSubmit() {
  118. // 提交验证
  119. let mingParams = {};
  120. if (!flag.value) {
  121. //不点击默认传参
  122. var obgj = getData.value;
  123. console.log("不点击默认传参obgj");
  124. console.log(obgj);
  125. var arr = [];
  126. for (var key in obgj) {
  127. console.log(key);
  128. console.log(obgj[key]);
  129. arr.push([key][0]);
  130. }
  131. arr.map((e) => {
  132. mingParams[e] = true;
  133. });
  134. } else {
  135. //点击复选框后传参
  136. var arr = checkedValue.value;
  137. console.log("自由选中复选框后传参arr");
  138. console.log(arr);
  139. arr.map((e) => {
  140. mingParams[e] = true;
  141. });
  142. console.log("mingParams");
  143. console.log(mingParams);
  144. }
  145. setperm(params).then((res) => {
  146. if (!res.flag) {
  147. uni.showToast({
  148. title: "添加失败",
  149. icon: "none",
  150. });
  151. } else {
  152. uni.showToast({
  153. title: "添加成功",
  154. });
  155. setTimeout(() => {
  156. uni.switchTab({
  157. url: "/pages/setting/setting",
  158. });
  159. }, 1000);
  160. }
  161. });
  162. }
  163. onLoad((option) => {
  164. getCheckList();
  165. });
  166. onReady(() => {
  167. //调用系统主题颜色
  168. proxy.$settingStore.systemThemeColor([1]);
  169. });
  170. </script>
  171. <style lang="scss">
  172. .checkItems {
  173. width: 50%;
  174. display: inline-block;
  175. text-align: center;
  176. margin-bottom: 40rpx;
  177. .title {
  178. display: inline-block;
  179. margin-left: 10rpx;
  180. }
  181. }
  182. </style>