Server.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <view>
  3. <view
  4. @click="navTo"
  5. class="serve"
  6. :style="{ left: left + 'rpx', top: top + 'rpx' }"
  7. @touchstart="onTouchStart"
  8. @touchmove="onTouchMove"
  9. @touchend="onTouchEnd"
  10. >
  11. <view class="serve-content">
  12. <image class="w120 h120"
  13. src="@/static/image/btn_sjtj.png"
  14. mode="aspectFit"></image>
  15. <!-- <text class="text">客服投诉</text> -->
  16. <!-- <button class="contact-btn" open-type="contact"></button> -->
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name:"Server",
  24. data() {
  25. return {
  26. left: 0,
  27. top: 0,
  28. startX: 0,
  29. startY: 0,
  30. isDragging: false,
  31. screenWidth: 0,
  32. screenHeight: 0
  33. };
  34. },
  35. mounted() {
  36. this.getSystemInfo();
  37. this.initPosition();
  38. },
  39. methods: {
  40. navTo(){
  41. uni.navigateTo({
  42. url: '/pages_echarts/statistics'
  43. })
  44. },
  45. // 初始化位置
  46. initPosition() {
  47. // 从本地存储加载位置
  48. const position = uni.getStorageSync('server_position');
  49. if (position) {
  50. this.left = position.left;
  51. this.top = position.top;
  52. } else {
  53. // 默认位置:右侧80%高度
  54. this.left = 750 - 150 - 20; // 屏幕宽度 - 容器宽度 - 右边距
  55. this.top = this.px2rpx(this.screenHeight * 0.8 - 75); // 80%高度,居中偏移
  56. }
  57. },
  58. // 获取系统信息
  59. getSystemInfo() {
  60. uni.getSystemInfo({
  61. success: (res) => {
  62. this.screenWidth = res.windowWidth;
  63. this.screenHeight = res.windowHeight;
  64. }
  65. });
  66. },
  67. // 触摸开始
  68. onTouchStart(e) {
  69. // 阻止事件冒泡,不影响其他元素
  70. e.stopPropagation();
  71. e.preventDefault();
  72. this.startX = e.touches[0].clientX;
  73. this.startY = e.touches[0].clientY;
  74. this.isDragging = false;
  75. },
  76. // 触摸移动
  77. onTouchMove(e) {
  78. // 阻止事件冒泡和默认行为
  79. e.stopPropagation();
  80. e.preventDefault();
  81. if (!this.startX || !this.startY) return;
  82. const currentX = e.touches[0].clientX;
  83. const currentY = e.touches[0].clientY;
  84. const diffX = currentX - this.startX;
  85. const diffY = currentY - this.startY;
  86. // 判断是否开始拖拽(移动超过5px)
  87. if (!this.isDragging && (Math.abs(diffX) > 5 || Math.abs(diffY) > 5)) {
  88. this.isDragging = true;
  89. }
  90. if (this.isDragging) {
  91. // 计算新的位置(rpx)
  92. const newLeft = this.rpx2px(this.left) + diffX;
  93. const newTop = this.rpx2px(this.top) + diffY;
  94. // 边界检查
  95. const boundedLeft = Math.max(10, Math.min(newLeft, this.screenWidth - this.rpx2px(150)));
  96. const boundedTop = Math.max(10, Math.min(newTop, this.screenHeight - this.rpx2px(150)));
  97. // 转换回rpx
  98. this.left = this.px2rpx(boundedLeft);
  99. this.top = this.px2rpx(boundedTop);
  100. // 更新起始位置
  101. this.startX = currentX;
  102. this.startY = currentY;
  103. }
  104. },
  105. // 触摸结束
  106. onTouchEnd(e) {
  107. // 阻止事件冒泡
  108. e.stopPropagation();
  109. e.preventDefault();
  110. if (this.isDragging) {
  111. // 吸边效果
  112. this.attachToEdge();
  113. // 保存位置
  114. this.savePosition();
  115. }
  116. this.startX = 0;
  117. this.startY = 0;
  118. this.isDragging = false;
  119. },
  120. // 吸边效果
  121. attachToEdge() {
  122. const containerWidth = 150; // 容器宽度
  123. const screenWidthRpx = 750;
  124. const centerX = screenWidthRpx / 2;
  125. // 根据当前位置决定吸附到左边还是右边
  126. if (this.left < centerX) {
  127. this.left = 20; // 吸附到左边
  128. } else {
  129. this.left = screenWidthRpx - containerWidth - 20; // 吸附到右边
  130. }
  131. },
  132. // 保存位置到本地存储
  133. savePosition() {
  134. uni.setStorageSync('server_position', {
  135. left: this.left,
  136. top: this.top
  137. });
  138. },
  139. // rpx转px
  140. rpx2px(rpx) {
  141. return rpx / 750 * this.screenWidth;
  142. },
  143. // px转rpx
  144. px2rpx(px) {
  145. return px * 750 / this.screenWidth;
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. .serve{
  152. position: fixed;
  153. z-index: 9999;
  154. width: 150rpx;
  155. height: 150rpx;
  156. // 确保拖拽时不影响其他元素
  157. touch-action: none;
  158. user-select: none;
  159. -webkit-user-select: none;
  160. .serve-content {
  161. width: 100%;
  162. height: 100%;
  163. display: flex;
  164. flex-direction: column;
  165. align-items: center;
  166. justify-content: center;
  167. position: relative;
  168. // 拖拽时的视觉反馈
  169. &:active {
  170. opacity: 0.9;
  171. transition: opacity 0.2s ease;
  172. }
  173. }
  174. .w130 {
  175. width: 130rpx !important;
  176. height: 130rpx !important;
  177. flex-shrink: 0; // 防止图片变形
  178. }
  179. .h130 {
  180. width: 130rpx !important;
  181. height: 130rpx !important;
  182. flex-shrink: 0; // 防止图片变形
  183. }
  184. .text{
  185. margin-top: 8rpx;
  186. font-weight: 500;
  187. font-size: 28rpx;
  188. color: #333;
  189. background: rgba(255, 255, 255, 0.9);
  190. padding: 6rpx 12rpx;
  191. border-radius: 20rpx;
  192. white-space: nowrap;
  193. flex-shrink: 0; // 防止文字变形
  194. border: 1rpx solid rgba(0, 0, 0, 0.1);
  195. backdrop-filter: blur(10rpx);
  196. }
  197. .contact-btn {
  198. position: absolute;
  199. top: 0;
  200. left: 0;
  201. width: 100%;
  202. height: 100%;
  203. opacity: 0;
  204. z-index: 10;
  205. // 确保按钮不变形
  206. border: none;
  207. border-radius: 0;
  208. background: transparent;
  209. padding: 0;
  210. margin: 0;
  211. &::after {
  212. border: none;
  213. }
  214. }
  215. }
  216. // 防止拖拽时页面滚动
  217. page {
  218. overflow: hidden;
  219. height: 100%;
  220. }
  221. </style>