1212.vue 7.3 KB

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