index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. autoZoom: false,
  102. success: function(res) {
  103. console.log(res.result)
  104. let equipment = JSON.parse(res.result);
  105. // 判断二维码对不对
  106. if (!equipment.equipmentDetailId || equipment.equipmentDetailId == "") {
  107. uni.showToast({
  108. icon: "error",
  109. title: "请扫设备码",
  110. duration: 1000
  111. })
  112. return;
  113. }
  114. // 判断是否已经扫码
  115. for (let i = 0; i < equipmentList.value.length; i++) {
  116. if (equipmentList.value[i].equipmentDetailId == equipment.equipmentDetailId) {
  117. uni.showToast({
  118. icon: "error",
  119. title: "设备已绑定",
  120. duration: 1000
  121. })
  122. return;
  123. }
  124. }
  125. // 设置绑定员工的信息
  126. userEquipmentData.value = {
  127. ...JSON.parse(res.result),
  128. userId: userInfo.value.userId,
  129. nickName: userInfo.value.nickName
  130. }
  131. // 判断该设备是否分配给改员工
  132. if (!curBindEquipmentIds.value.includes(equipment.equipmentDetailId)) {
  133. console.log(equipment.equipmentDetailId)
  134. let msg = '当前员工没有分配到该设备,确定继续绑定吗?';
  135. confirm.value.open(msg);
  136. }else{
  137. handleSaveInfo();
  138. }
  139. }
  140. });
  141. }
  142. /**
  143. * 绑定设备
  144. */
  145. function handleSaveInfo() {
  146. saveUserequipment(userEquipmentData.value).then(res => {
  147. if(res.code === 200){
  148. init();
  149. uni.showToast({
  150. icon: "success",
  151. title: "设备绑定成功"
  152. })
  153. }
  154. })
  155. }
  156. </script>
  157. <style lang="scss">
  158. .container {
  159. height: 100%;
  160. background-color: rgba(245, 245, 245, 1);
  161. .title {
  162. width: 100%;
  163. font-size: 42rpx;
  164. font-weight: bold;
  165. height: 100rpx;
  166. text-align: center;
  167. line-height: 100rpx;
  168. }
  169. .prompt {
  170. width: 100%;
  171. text-align: center;
  172. color: lightgrey;
  173. }
  174. .btn {
  175. background-color: rgba(0, 226, 166, 1);
  176. color: white;
  177. margin: 20rpx auto;
  178. width: 80%;
  179. position: fixed;
  180. bottom: 0;
  181. left: 0;
  182. right: 0;
  183. }
  184. .equipmentList {
  185. width: 94%;
  186. height: calc(90% - 100rpx);
  187. background-color: rgba(255, 255, 255, 1);
  188. margin: 10rpx auto;
  189. padding: 20rpx 0 0 0;
  190. border-radius: 18rpx;
  191. overflow: auto;
  192. .entry {
  193. justify-content: center;
  194. margin: 10rpx 0;
  195. position: relative;
  196. .equipmentList-item {
  197. position: relative;
  198. width: 75%;
  199. height: 80rpx;
  200. color: rgba(255, 255, 255, 1);
  201. background-color: rgba(22, 132, 252, 1);
  202. border-radius: 12rpx;
  203. text-align: center;
  204. line-height: 80rpx;
  205. }
  206. .unbind {
  207. position: relative;
  208. z-index: 2;
  209. margin-left: -15%;
  210. width: 15%;
  211. height: 80rpx;
  212. color: rgba(255, 255, 255, 1);
  213. background-color: red;
  214. border-radius: 0 12rpx 12rpx 0;
  215. text-align: center;
  216. line-height: 80rpx;
  217. visibility: hidden;
  218. }
  219. .visible {
  220. visibility: visible;
  221. }
  222. }
  223. }
  224. }
  225. </style>