help-choose.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <view class="container flex-common-css iPhone-bottom box-border-box">
  3. <Nav :title="school_name"></Nav>
  4. <image class="bg-img" src="https://sjz-school.oss-cn-chengdu.aliyuncs.com/webImages/bg.png" mode=""></image>
  5. <!-- 泡泡容器 -->
  6. <view class="q-box z-2">
  7. <view v-for="(bubble, index) in bubbles" :key="index" :style="{
  8. transform: `translate(${bubble.x}px, ${bubble.y}px)`,
  9. width: bubble.size + 'px',
  10. height: bubble.size + 'px',
  11. backgroundColor: selectedBubbleIndex === index ? '#ffcc00' : '', // 选中时高亮
  12. }" class="bubble box-border-box">
  13. <image class="qp-img" src="/pages-takeout/static/qp.png" mode=""></image>
  14. <image class="shop-logo z-2 b-rad-20" :src="bubble.shop_logo" mode=""></image>
  15. <view class="fs-24 fc-f z-2 text-overflow-ellipsis-one max-w-118 text-align-center pl-15 box-border-box">
  16. {{bubble.shop_name}}
  17. </view>
  18. </view>
  19. </view>
  20. <view class="but-box" @click="selectRandomBubble">
  21. <image class="but-img" src="/pages-takeout/static/but-bg.png" mode=""></image>
  22. <view class="fs-38 fc-f fw-b text-align-center z-2 pt-49 mb-82">
  23. 君子帮你选,今日吃点啥?
  24. </view>
  25. <view class="fs-28 fc-1089D1 text-align-center">
  26. 全部商家随机抽取
  27. </view>
  28. </view>
  29. <!-- 弹窗 -->
  30. <view v-if="showPopup" class="popup">
  31. <view class="popup-content">
  32. <text>选中的泡泡编号:{{ selectedBubbleIndex + 1 }}</text>
  33. <view class="close-button" @click="closePopup">关闭</view>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import {
  40. shopAll
  41. } from "@/api/shop.js";
  42. export default {
  43. data() {
  44. return {
  45. school_name: '',
  46. shopId: 0,
  47. school_name: '',
  48. bubbles: [], // 存储泡泡信息
  49. screenWidth: 0, // 屏幕宽度
  50. screenHeight: 0, // 屏幕高度
  51. animationTimer: null, // 动画定时器
  52. selectedBubbleIndex: null, // 当前选中的泡泡索引
  53. showPopup: false, // 是否显示弹窗
  54. isSelecting: false, // 是否正在选择中
  55. };
  56. },
  57. onLoad(data) {
  58. this.school_name = uni.getStorageSync('school_name');
  59. },
  60. mounted() {
  61. this.getScreenSize(); // 获取屏幕尺寸
  62. this.shopAll(); // 泡泡信息
  63. this.startAnimation(); // 开始动画
  64. },
  65. beforeDestroy() {
  66. // 组件销毁时停止动画
  67. if (this.animationTimer) {
  68. clearTimeout(this.animationTimer);
  69. }
  70. },
  71. methods: {
  72. // 获取屏幕尺寸
  73. getScreenSize() {
  74. const systemInfo = uni.getSystemInfoSync();
  75. this.screenWidth = systemInfo.windowWidth; // 屏幕宽度
  76. this.screenHeight = systemInfo.windowHeight * 0.71; // 屏幕高度
  77. },
  78. shopAll() {
  79. shopAll().then((res) => {
  80. if (res.code === 1) {
  81. this.bubbles = res.data;
  82. console.log(this.bubbles, this.bubbles.length);
  83. // 生成随机泡泡
  84. this.bubbles.forEach((v, k) => {
  85. v.x = Math.random() * (this.screenWidth - 50); // 随机水平位置
  86. v.y = Math.random() * (this.screenHeight - 50); // 随机垂直位置
  87. v.size = 59; // 随机大小(20px - 60px)
  88. v.speedX = (Math.random() - 0.5) * 2; // 水平速度(-1 到 1)
  89. v.speedY = (Math.random() - 0.5) * 2; // 垂直速度(-1 到 1)
  90. })
  91. }
  92. });
  93. },
  94. // 开始动画
  95. startAnimation() {
  96. const animate = () => {
  97. this.updateBubblesPosition();
  98. this.animationTimer = setTimeout(animate, 16); // 模拟 60 FPS
  99. };
  100. animate(); // 启动动画
  101. },
  102. // 更新泡泡位置
  103. updateBubblesPosition() {
  104. this.bubbles.forEach((bubble) => {
  105. // 更新位置
  106. bubble.x += bubble.speedX;
  107. bubble.y += bubble.speedY;
  108. // 碰到左右边框时反转水平速度
  109. if (bubble.x <= 0 || bubble.x + bubble.size >= this.screenWidth) {
  110. bubble.speedX = -bubble.speedX;
  111. }
  112. // 碰到上下边框时反转垂直速度
  113. if (bubble.y <= 0 || bubble.y + bubble.size >= this.screenHeight) {
  114. bubble.speedY = -bubble.speedY;
  115. }
  116. });
  117. },
  118. // 随机选择一个泡泡
  119. selectRandomBubble() {
  120. if (this.isSelecting) return; // 防止重复点击
  121. this.isSelecting = true;
  122. // 随机选择过程动画
  123. const totalSteps = 20; // 随机闪烁次数
  124. let step = 0;
  125. const interval = setInterval(() => {
  126. this.selectedBubbleIndex = Math.floor(Math.random() * this.bubbles.length);
  127. step++;
  128. if (step >= totalSteps) {
  129. clearInterval(interval);
  130. this.isSelecting = false;
  131. this.showPopup = true; // 显示弹窗
  132. }
  133. }, 100); // 每 100 毫秒闪烁一次
  134. },
  135. // 关闭弹窗
  136. closePopup() {
  137. this.showPopup = false;
  138. this.selectedBubbleIndex = null; // 重置选中状态
  139. },
  140. },
  141. };
  142. </script>
  143. <style>
  144. .max-w-118{
  145. max-width: 118rpx;
  146. }
  147. .qp-img {
  148. width: 118rpx;
  149. height: 118rpx;
  150. position: absolute;
  151. left: 0;
  152. top: 0;
  153. }
  154. .shop-logo {
  155. width: 68.5rpx;
  156. height: 68.5rpx;
  157. margin: 0 0 34rpx 29rpx;
  158. }
  159. .pt-49 {
  160. padding-top: 49rpx;
  161. }
  162. .mb-82 {
  163. margin-bottom: 82rpx;
  164. }
  165. .bg-img {
  166. width: 100%;
  167. height: 89%;
  168. position: absolute;
  169. left: 0;
  170. top: 0;
  171. }
  172. .z-2 {
  173. position: relative;
  174. z-index: 2;
  175. }
  176. .container {
  177. position: relative;
  178. overflow: hidden;
  179. }
  180. .q-box {}
  181. .bubble {
  182. position: absolute;
  183. /* 泡泡颜色 */
  184. border-radius: 50%;
  185. will-change: transform;
  186. padding: 28rpx 20rpx 29rpx 0;
  187. /* 优化性能 */
  188. }
  189. .but-box {
  190. width: 100%;
  191. height: 170rpx;
  192. position: absolute;
  193. bottom: 135rpx;
  194. left: 0;
  195. }
  196. .but-img {
  197. position: absolute;
  198. left: 0;
  199. top: 0;
  200. z-index: 1;
  201. width: 100%;
  202. height: 100%;
  203. }
  204. .popup {
  205. position: fixed;
  206. top: 0;
  207. left: 0;
  208. width: 100%;
  209. height: 100%;
  210. background-color: rgba(0, 0, 0, 0.5);
  211. display: flex;
  212. justify-content: center;
  213. align-items: center;
  214. }
  215. .popup-content {
  216. background-color: white;
  217. padding: 20px;
  218. border-radius: 10px;
  219. text-align: center;
  220. }
  221. .close-button {
  222. margin-top: 10px;
  223. padding: 5px 10px;
  224. background-color: #007aff;
  225. color: white;
  226. border-radius: 5px;
  227. cursor: pointer;
  228. }
  229. </style>