index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. })
  62. })
  63. }
  64. /**
  65. * 刷新
  66. */
  67. function reflush(){
  68. init();
  69. }
  70. function handleConfirmUnbind(){
  71. handleSaveInfo();
  72. }
  73. /**
  74. * 长按显示解绑按钮
  75. * @param {Object} item
  76. */
  77. function handleShowUnbind(item) {
  78. if (showUnbind.value === item) {
  79. showUnbind.value = {};
  80. } else {
  81. showUnbind.value = item;
  82. }
  83. }
  84. /**
  85. * 解绑
  86. * @param {Object} item 当前选择要解绑的对象
  87. */
  88. function handleUnbind(item) {
  89. unbindQuipment(item.id).then(res => {
  90. init();
  91. })
  92. }
  93. /**
  94. * 扫码
  95. */
  96. function handleScanCode() {
  97. uni.scanCode({
  98. scanType: ['qrCode'], // 条形码扫描
  99. onlyFromCamera: true, // 只允许相机扫码
  100. success: function(res) {
  101. let equipment = JSON.parse(res.result);
  102. for (let i = 0; i < equipmentList.value.length; i++) {
  103. // 判断是否已经扫码
  104. if (equipmentList.value[i].equipmentId == equipment.equipmentId) {
  105. uni.showToast({
  106. icon: "error",
  107. title: "设备已绑定"
  108. })
  109. return;
  110. }
  111. }
  112. // 设置绑定员工的信息
  113. userEquipmentData.value = {
  114. ...JSON.parse(res.result),
  115. userId: userInfo.value.userId,
  116. nickName: userInfo.value.nickName
  117. }
  118. // 判断该设备是否分配给改员工
  119. if (!curBindEquipmentIds.value.includes(equipment.equipmentId)) {
  120. console.log(equipment.equipmentId)
  121. let msg = '当前员工没有分配到该设备,确定继续绑定吗?';
  122. confirm.value.open(msg);
  123. }else{
  124. handleSaveInfo();
  125. }
  126. }
  127. });
  128. }
  129. /**
  130. * 绑定设备
  131. */
  132. function handleSaveInfo() {
  133. // console.log(userEquipmentData.value)
  134. saveUserequipment(userEquipmentData.value).then(res => {
  135. if(res.code === 200){
  136. init();
  137. uni.showToast({
  138. icon: "success",
  139. title: "设备绑定成功"
  140. })
  141. }
  142. })
  143. }
  144. </script>
  145. <style lang="scss">
  146. .container {
  147. height: 100%;
  148. background-color: rgba(245, 245, 245, 1);
  149. .title {
  150. width: 100%;
  151. font-size: 42rpx;
  152. font-weight: bold;
  153. height: 100rpx;
  154. text-align: center;
  155. line-height: 100rpx;
  156. }
  157. .prompt {
  158. width: 100%;
  159. text-align: center;
  160. color: lightgrey;
  161. }
  162. .btn {
  163. background-color: rgba(0, 226, 166, 1);
  164. color: white;
  165. margin: 20rpx auto;
  166. width: 80%;
  167. position: fixed;
  168. bottom: 0;
  169. left: 0;
  170. right: 0;
  171. }
  172. .equipmentList {
  173. width: 94%;
  174. height: calc(90% - 100rpx);
  175. background-color: rgba(255, 255, 255, 1);
  176. margin: 10rpx auto;
  177. padding: 20rpx 0 0 0;
  178. border-radius: 18rpx;
  179. overflow: auto;
  180. .entry {
  181. justify-content: center;
  182. margin: 10rpx 0;
  183. position: relative;
  184. .equipmentList-item {
  185. position: relative;
  186. width: 75%;
  187. height: 80rpx;
  188. color: rgba(255, 255, 255, 1);
  189. background-color: rgba(22, 132, 252, 1);
  190. border-radius: 12rpx;
  191. text-align: center;
  192. line-height: 80rpx;
  193. }
  194. .unbind {
  195. position: relative;
  196. z-index: 2;
  197. margin-left: -15%;
  198. width: 15%;
  199. height: 80rpx;
  200. color: rgba(255, 255, 255, 1);
  201. background-color: red;
  202. border-radius: 0 12rpx 12rpx 0;
  203. text-align: center;
  204. line-height: 80rpx;
  205. visibility: hidden;
  206. }
  207. .visible {
  208. visibility: visible;
  209. }
  210. }
  211. }
  212. }
  213. </style>