Server.vue 5.3 KB

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