index.vue 5.5 KB

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