tm-choujiangGame.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <view class="tm-choujiangGame py-32">
  3. <view class="tm-choujiangGame-theme-1 flex-center flex-col">
  4. <view class="tm-choujiangGame-bg ">
  5. <view
  6. @click="clickImgPlay"
  7. class="tm-choujiangGame-bg-image"
  8. :style="{
  9. backgroundImage: `url(${them_data.bg})`
  10. }"
  11. ></view>
  12. <view class="tm-choujiangGame-bg-prod ">
  13. <view
  14. class="tm-choujiangGame-bg-cp text-red text-align-center "
  15. :style="{
  16. width: '25%',
  17. height: '100%',
  18. transform: `translateY(${reisetInitTop}px)`
  19. }"
  20. :animation="animationData0"
  21. >
  22. <view
  23. v-for="(item, index) in listData"
  24. :key="index"
  25. style="height: 100%;"
  26. class="flex-center flex-col"
  27. >
  28. <tm-images v-if="item.img" :previmage="false" :width="60" :src="item.img"></tm-images>
  29. {{item.name}}
  30. </view>
  31. </view>
  32. <view
  33. class="tm-choujiangGame-bg-cp text-red text-align-center "
  34. :style="{
  35. width: '25%',
  36. height: '100%',
  37. transform: `translateY(${reisetInitTop}px)`
  38. }"
  39. :animation="animationData1"
  40. >
  41. <view
  42. v-for="(item, index) in listData"
  43. :key="index"
  44. style="height: 100%;"
  45. class="flex-center flex-col"
  46. >
  47. <tm-images v-if="item.img" :previmage="false" :width="60" :src="item.img"></tm-images>
  48. {{item.name}}
  49. </view>
  50. </view>
  51. <view
  52. class="tm-choujiangGame-bg-cp text-red text-align-center "
  53. :style="{
  54. width: '25%',
  55. height: '100%',
  56. transform: `translateY(${reisetInitTop}px)`
  57. }"
  58. :animation="animationData2"
  59. >
  60. <view
  61. v-for="(item, index) in listData"
  62. :key="index"
  63. style="height: 100%;"
  64. class="flex-center flex-col"
  65. >
  66. <tm-images v-if="item.img" :previmage="false" :width="60" :src="item.img"></tm-images>
  67. {{item.name}}
  68. </view>
  69. </view>
  70. <view
  71. class="tm-choujiangGame-bg-cp text-red text-align-center "
  72. :style="{
  73. width: '25%',
  74. height: '100%',
  75. transform: `translateY(${reisetInitTop}px)`
  76. }"
  77. :animation="animationData3"
  78. >
  79. <view
  80. v-for="(item, index) in listData"
  81. :key="index"
  82. style="height: 100%;"
  83. class="flex-center flex-col"
  84. >
  85. <tm-images v-if="item.img" :previmage="false" :width="60" :src="item.img"></tm-images>
  86. {{item.name}}
  87. </view>
  88. </view>
  89. </view>
  90. <view class="flex-end tm-choujiangGame-point">
  91. <view class="tm-choujiangGame-point-bar"
  92. :style="{
  93. transform: aniOff?'rotate(0)':'rotate(25deg)'
  94. }"
  95. >
  96. <tm-images :previmage="false" :width="120" :src="them_data.point"></tm-images>
  97. </view>
  98. </view>
  99. </view>
  100. <!-- transform: choujiangJieGuo==null?`translateY(-100%)`:`translateY(0)` -->
  101. <view class="tm-choujiangGame-pingtai flex-center">
  102. <view class="tm-choujiangGame-pingtai-coll" :style="{
  103. transition: 'all 0.5s',
  104. transform: choujiangJieGuo==null?`translateY(-100%)`:`translateY(0)`
  105. }">
  106. <view class="tm-choujiangGame-pingtai-text">中奖啦~</view>
  107. <tm-images :previmage="false" :width="380" :height="200" :src="them_data.dizuo"></tm-images>
  108. </view>
  109. </view>
  110. </view>
  111. </view>
  112. </template>
  113. <script>
  114. /**
  115. * 转盘抽奖
  116. * @property {Number} theme-index = [] 默认0,主题索引,我已内置两套皮肤。
  117. * @property {Array} theme-list = [] 默认[已内置两套],主题列表数据,已有两套。
  118. * @property {Boolean} disabled = [] 默认 false,是否禁用
  119. * @property {Boolean} disabledCenter = [] 默认 false,是否禁用点中间图片开始。
  120. * @property {Number} duration = [] 默认 3000,动画时长
  121. * @property {Array} list = [] 默认 [测试的数据],奖品数据。
  122. * @property {Function} start 游戏开始时触发。
  123. * @property {Function} end 游戏结束触发,并返回奖品数据。
  124. * @example <tm-choujiangGame ></tm-choujiangGame>
  125. */
  126. import tmImages from "@/tm-vuetify/components/tm-images/tm-images.vue"
  127. export default {
  128. components:{tmImages},
  129. name: 'tm-choujiangGame',
  130. props: {
  131. themeIndex: {
  132. type: Number,
  133. default: 0
  134. },
  135. themeList: {
  136. type: Array,
  137. default: () => {
  138. return [
  139. {
  140. bg: 'https://jx2d.cn/yuwuimages/choujiangGame_1.png?k=9',
  141. point: 'https://jx2d.cn/yuwuimages/choujiangGame_1_0.png',
  142. dizuo: 'https://jx2d.cn/yuwuimages/choujiangGame_2_1.png'
  143. },
  144. {
  145. bg: 'https://jx2d.cn/yuwuimages/choujiangGame_2.png?k=9',
  146. point: 'https://jx2d.cn/yuwuimages/choujiangGame_2_0.png',
  147. dizuo: 'https://jx2d.cn/yuwuimages/choujiangGame_2_1.png'
  148. }
  149. ];
  150. }
  151. },
  152. disabled: {
  153. type: Boolean,
  154. default: false
  155. },
  156. //是否禁用点中间图片开始。
  157. disabledCenter:{
  158. type: Boolean,
  159. default: false
  160. },
  161. // 动画时长
  162. duration: {
  163. type: Number,
  164. default: 150
  165. },
  166. list: {
  167. type: Array,
  168. default: () => {
  169. return [
  170. { name: '1', gailv: 30, id: 1, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  171. { name: '2', gailv: 10, id: 2, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' },
  172. { name: '3', gailv: 10, id: 3, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  173. { name: '4', gailv: 10, id: 4, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' },
  174. { name: '5', gailv: 10, id: 5, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  175. { name: '7', gailv: 10, id: 7, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' },
  176. { name: '8', gailv: 10, id: 8, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  177. { name: '9', gailv: 10, id: 9, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' }
  178. ];
  179. }
  180. }
  181. },
  182. data() {
  183. return {
  184. animationData0: '',
  185. animationData1: '',
  186. animationData2: '',
  187. animationData3: '',
  188. aniOff: true,
  189. choujiangJieGuo:null,
  190. listData: [],
  191. };
  192. },
  193. mounted() {
  194. this.listData = this.list;
  195. },
  196. computed: {
  197. reisetInitTop:function(){
  198. return -(this.listData.length-1)*uni.upx2px(160)
  199. },
  200. them_index: function() {
  201. return this.themeIndex;
  202. },
  203. them_data: function() {
  204. return this.themeList[this.them_index];
  205. }
  206. },
  207. methods: {
  208. clickImgPlay(){
  209. if (this.disabledCenter) return;
  210. this.play();
  211. },
  212. chuliganlv() {
  213. var ml = new this.$tm.choujiang(this.listData);
  214. let zhongle = [];
  215. let xh = [
  216. ml.getResult(),
  217. ml.getResult(),
  218. ml.getResult(),
  219. ml.getResult()
  220. ]
  221. for(let i=0 ;i<xh.length;i++){
  222. let index = 0
  223. for(let j=0;j<this.listData.length;j++){
  224. if(this.listData[j].id === xh[i].id){
  225. index = j;
  226. break;
  227. }
  228. }
  229. //(this.listData.length-1)*2
  230. let cisu = (this.listData.length-1)*2+1 + (this.listData.length - index);
  231. zhongle.push({
  232. index:index,
  233. data:xh[i],
  234. playNum:cisu
  235. })
  236. }
  237. return zhongle;
  238. },
  239. async play() {
  240. let t = this;
  241. if (this.disabled) return;
  242. if (!this.aniOff) {
  243. console.warn('未结束动画,不能连续点击.');
  244. return;
  245. }
  246. // 开始动画.
  247. this.$emit('start');
  248. this.choujiangJieGuo = null;
  249. let zhongjiang = this.chuliganlv();
  250. this.$nextTick(async function() {
  251. await this.resetInit();
  252. this.aniOff = false;
  253. let duration = this.duration;
  254. let dhIndex=[0,0,0,0];//内部播放次数。
  255. let ydIndex =[0,0,0,0] ; //达到长度时,要循环。
  256. let isok = [false,false,false,false]
  257. async function aniOpen(ix){
  258. if(ydIndex[ix]>t.listData.length-1){
  259. if(ix==0){
  260. var animation0 = uni.createAnimation({
  261. duration: 0
  262. });
  263. t.animationData0 = animation0;
  264. animation0.translateY(-(t.listData.length-1)*uni.upx2px(160)).step({duration:0});
  265. t.animationData0 = animation0.export();
  266. }else if(ix==1){
  267. var animation1 = uni.createAnimation({
  268. duration: 0
  269. });
  270. t.animationData1 = animation1;
  271. animation1.translateY(-(t.listData.length-1)*uni.upx2px(160)).step({duration:0});
  272. t.animationData1 = animation1.export();
  273. }else if(ix==2){
  274. var animation2 = uni.createAnimation({
  275. duration: 0
  276. });
  277. t.animationData2 = animation2;
  278. animation2.translateY(-(t.listData.length-1)*uni.upx2px(160)).step({duration:0});
  279. t.animationData2 = animation2.export();
  280. }else if(ix==3){
  281. var animation3 = uni.createAnimation({
  282. duration: 0
  283. });
  284. t.animationData3 = animation3;
  285. animation3.translateY(-(t.listData.length-1)*uni.upx2px(160)).step({duration:0});
  286. t.animationData3 = animation3.export();
  287. }
  288. await uni.$tm.sleep(50);
  289. ydIndex[ix] = 0;
  290. }else{
  291. var animation = uni.createAnimation({
  292. duration: duration,
  293. timingFunction: 'linear'
  294. });
  295. if(ix==0){
  296. t.animationData0 = animation;
  297. let jl = -(t.listData.length-1)*160+ydIndex[ix]*160;
  298. animation.translateY(uni.upx2px(jl)).step();
  299. t.animationData0 = animation.export();
  300. }else if(ix==1){
  301. t.animationData1 = animation;
  302. let jl = -(t.listData.length-1)*160+ydIndex[ix]*160;
  303. animation.translateY(uni.upx2px(jl)).step();
  304. t.animationData1 = animation.export();
  305. }else if(ix==2){
  306. t.animationData2 = animation;
  307. let jl = -(t.listData.length-1)*160+ydIndex[ix]*160;
  308. animation.translateY(uni.upx2px(jl)).step();
  309. t.animationData2 = animation.export();
  310. }else if(ix==3){
  311. let jl = -(t.listData.length-1)*160+ydIndex[ix]*160;
  312. animation.translateY(uni.upx2px(jl)).step();
  313. t.animationData3 = animation.export();
  314. }
  315. await uni.$tm.sleep(duration);
  316. }
  317. dhIndex[ix] = dhIndex[ix]+1;
  318. ydIndex[ix] = ydIndex[ix]+1;
  319. if(dhIndex[ix] > zhongjiang[ix].playNum){
  320. isok[ix] = true;
  321. if(isok[0]&&isok[1]&&isok[2]&&isok[3]){
  322. // 结束动画.
  323. t.aniOff = true;
  324. t.$emit('end',zhongjiang)
  325. t.choujiangJieGuo = zhongjiang;
  326. }
  327. }else{
  328. await aniOpen(ix);
  329. }
  330. }
  331. for(let i =0;i<zhongjiang.length;i++){
  332. aniOpen(i);
  333. }
  334. });
  335. },
  336. // 重置 角度。
  337. async resetInit() {
  338. this.animationData0 = '';
  339. var animation0 = uni.createAnimation({duration: 0});
  340. this.animationData0 = animation0;
  341. animation0.translateY(-uni.upx2px((this.listData.length-1)*160)).step();
  342. this.animationData0 = animation0.export();
  343. this.animationData1 = '';
  344. var animation1 = uni.createAnimation({duration: 0});
  345. this.animationData1 = animation1;
  346. animation1.translateY(-uni.upx2px((this.listData.length-1)*160)).step();
  347. this.animationData1 = animation1.export();
  348. this.animationData2 = '';
  349. var animation2 = uni.createAnimation({duration: 0});
  350. this.animationData2 = animation2;
  351. animation2.translateY(-uni.upx2px((this.listData.length-1)*160)).step();
  352. this.animationData2 = animation2.export();
  353. this.animationData3 = '';
  354. var animation3 = uni.createAnimation({duration: 0});
  355. this.animationData3 = animation3;
  356. animation3.translateY(-uni.upx2px((this.listData.length-1)*160)).step();
  357. this.animationData3 = animation3.export();
  358. await uni.$tm.sleep(50);
  359. }
  360. }
  361. };
  362. </script>
  363. <style lang="less" scoped>
  364. .tm-choujiangGame {
  365. overflow: hidden;
  366. .tm-choujiangGame-theme-1 {
  367. .tm-choujiangGame-bg {
  368. width: 600upx;
  369. height: 600upx;
  370. position: relative;
  371. z-index: 2;
  372. .tm-choujiangGame-bg-image {
  373. width: 100%;
  374. height: 100%;
  375. position: relative;
  376. z-index: 3;
  377. background-repeat: no-repeat;
  378. background-size: cover;
  379. }
  380. .tm-choujiangGame-point {
  381. position: absolute;
  382. width: 100%;
  383. height: 100%;
  384. top: 0;
  385. left: 0;
  386. right: 0;
  387. .tm-choujiangGame-point-bar{
  388. position: relative;
  389. right: 0;
  390. z-index: 0;
  391. top: -50upx;
  392. transform-origin: left right;
  393. transition:all 0.5s;
  394. }
  395. }
  396. .tm-choujiangGame-bg-prod {
  397. position: absolute;
  398. overflow: hidden;
  399. width: 370upx;
  400. height: 160upx;
  401. top: 95upx;
  402. left: 103upx;
  403. display: flex;
  404. z-index: 4;
  405. .tm-choujiangGame-bg-cp {
  406. transform-origin: center bottom;
  407. }
  408. }
  409. }
  410. .tm-choujiangGame-pingtai {
  411. position: relative;
  412. z-index: 2;
  413. margin-left: -20upx;
  414. margin-top: -140upx;
  415. overflow: hidden;
  416. height: 190upx;
  417. .tm-choujiangGame-pingtai-coll{
  418. position: relative;
  419. .tm-choujiangGame-pingtai-text{
  420. position: absolute;
  421. z-index: 3;
  422. font-size: 48upx;
  423. text-align: center;
  424. width: 100%;
  425. top: 10upx;
  426. color: red;
  427. font-weight: bold;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. </style>