index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <view class="container">
  3. <view class="title">已绑设备</view>
  4. <view class="prompt">长按设备解绑</view>
  5. <view class="equipmentList">
  6. <view class="entry uni-row" v-for="(item,index) in equipmentList">
  7. <view class="equipmentList-item" @longpress="handleShowUnbind(item)">{{item.equipmentDetailCode}}</view>
  8. <view :class="{'unbind': true,'visible': showUnbind === item}" @click="handleUnbind(item)">解绑</view>
  9. </view>
  10. </view>
  11. <view>
  12. <button class="btn" @click="handleScanCode">扫码</button>
  13. </view>
  14. </view>
  15. <dialog-confirm ref="confirm" @submit="handleConfirmUnbind" @reflush="reflush"></dialog-confirm>
  16. </template>
  17. <script setup>
  18. import {
  19. ref
  20. } from 'vue'
  21. import {
  22. onLoad
  23. } from '@dcloudio/uni-app'
  24. import {
  25. getUserInfo
  26. } from '@/api/login/index.js'
  27. import {
  28. saveUserequipment,
  29. getUserequipmentList,
  30. unbindQuipment
  31. } from '@/api/business/userEquipment/userEquipment.js'
  32. import {
  33. getEquipmentUserList
  34. } from '@/api/business/equipmentUser.js'
  35. const showUnbind = ref({}) // 解绑按钮显示于隐藏
  36. const equipmentList = ref([]) // 回显用
  37. const userEquipmentData = ref({}) // 保存用
  38. const curBindDetails = ref([]) // 分配给员工的设备信息
  39. const curBindEquipmentIds = ref([]) // 分配给当前员工的设备id列表
  40. const userInfo = ref({}) // 当前登录用信息
  41. const confirm = ref(null)
  42. onLoad(() => {
  43. init();
  44. })
  45. /**
  46. * 页面初始化
  47. */
  48. function init() {
  49. let data = {}
  50. getUserInfo().then(response => {
  51. userInfo.value = response.data;
  52. data.userId = response.data.userId;
  53. getUserequipmentList(data).then(res => {
  54. equipmentList.value = res.rows;
  55. })
  56. getEquipmentUserList(data).then(res => {
  57. curBindDetails.value = res.rows;
  58. for (var i = 0; i < curBindDetails.value.length; i++) {
  59. curBindEquipmentIds.value.push(curBindDetails.value[i].equipmentDetailId)
  60. }
  61. console.log(curBindEquipmentIds.value)
  62. })
  63. })
  64. }
  65. /**
  66. * 刷新
  67. */
  68. function reflush(){
  69. init();
  70. }
  71. function handleConfirmUnbind(){
  72. handleSaveInfo();
  73. }
  74. /**
  75. * 长按显示解绑按钮
  76. * @param {Object} item
  77. */
  78. function handleShowUnbind(item) {
  79. if (showUnbind.value === item) {
  80. showUnbind.value = {};
  81. } else {
  82. showUnbind.value = item;
  83. }
  84. }
  85. /**
  86. * 解绑
  87. * @param {Object} item 当前选择要解绑的对象
  88. */
  89. function handleUnbind(item) {
  90. unbindQuipment(item.id).then(res => {
  91. init();
  92. })
  93. }
  94. /**
  95. * 扫码
  96. */
  97. function handleScanCode() {
  98. uni.scanCode({
  99. scanType: ['qrCode'], // 条形码扫描
  100. onlyFromCamera: true, // 只允许相机扫码
  101. success: function(res) {
  102. console.log(res.result)
  103. let equipment = JSON.parse(res.result);
  104. // 判断二维码对不对
  105. if (!equipment.equipmentDetailId || equipment.equipmentDetailId == "") {
  106. uni.showToast({
  107. icon: "error",
  108. title: "请扫设备码",
  109. duration: 1000
  110. })
  111. return;
  112. }
  113. // 判断是否已经扫码
  114. for (let i = 0; i < equipmentList.value.length; i++) {
  115. if (equipmentList.value[i].equipmentDetailId == equipment.equipmentDetailId) {
  116. uni.showToast({
  117. icon: "error",
  118. title: "设备已绑定",
  119. duration: 1000
  120. })
  121. return;
  122. }
  123. }
  124. // 设置绑定员工的信息
  125. userEquipmentData.value = {
  126. ...JSON.parse(res.result),
  127. userId: userInfo.value.userId,
  128. nickName: userInfo.value.nickName
  129. }
  130. // 判断该设备是否分配给改员工
  131. if (!curBindEquipmentIds.value.includes(equipment.equipmentDetailId)) {
  132. console.log(equipment.equipmentDetailId)
  133. let msg = '当前员工没有分配到该设备,确定继续绑定吗?';
  134. confirm.value.open(msg);
  135. }else{
  136. handleSaveInfo();
  137. }
  138. }
  139. });
  140. }
  141. /**
  142. * 绑定设备
  143. */
  144. function handleSaveInfo() {
  145. saveUserequipment(userEquipmentData.value).then(res => {
  146. if(res.code === 200){
  147. init();
  148. uni.showToast({
  149. icon: "success",
  150. title: "设备绑定成功"
  151. })
  152. }
  153. })
  154. }
  155. </script>
  156. <style lang="scss">
  157. .container {
  158. height: 100%;
  159. background-color: rgba(245, 245, 245, 1);
  160. .title {
  161. width: 100%;
  162. font-size: 42rpx;
  163. font-weight: bold;
  164. height: 100rpx;
  165. text-align: center;
  166. line-height: 100rpx;
  167. }
  168. .prompt {
  169. width: 100%;
  170. text-align: center;
  171. color: lightgrey;
  172. }
  173. .btn {
  174. background-color: rgba(0, 226, 166, 1);
  175. color: white;
  176. margin: 20rpx auto;
  177. width: 80%;
  178. position: fixed;
  179. bottom: 0;
  180. left: 0;
  181. right: 0;
  182. }
  183. .equipmentList {
  184. width: 94%;
  185. height: calc(90% - 100rpx);
  186. background-color: rgba(255, 255, 255, 1);
  187. margin: 10rpx auto;
  188. padding: 20rpx 0 0 0;
  189. border-radius: 18rpx;
  190. overflow: auto;
  191. .entry {
  192. justify-content: center;
  193. margin: 10rpx 0;
  194. position: relative;
  195. .equipmentList-item {
  196. position: relative;
  197. width: 75%;
  198. height: 80rpx;
  199. color: rgba(255, 255, 255, 1);
  200. background-color: rgba(22, 132, 252, 1);
  201. border-radius: 12rpx;
  202. text-align: center;
  203. line-height: 80rpx;
  204. }
  205. .unbind {
  206. position: relative;
  207. z-index: 2;
  208. margin-left: -15%;
  209. width: 15%;
  210. height: 80rpx;
  211. color: rgba(255, 255, 255, 1);
  212. background-color: red;
  213. border-radius: 0 12rpx 12rpx 0;
  214. text-align: center;
  215. line-height: 80rpx;
  216. visibility: hidden;
  217. }
  218. .visible {
  219. visibility: visible;
  220. }
  221. }
  222. }
  223. }
  224. </style>