index.vue 5.1 KB

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