index.vue 5.0 KB

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