Server.vue 5.4 KB

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