tm-choujiang.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <view class="tm-choujiang py-32">
  3. <view class="tm-choujiang-theme-1 flex-center flex-col">
  4. <view class="tm-choujiang-bg ">
  5. <view
  6. :animation="animationData"
  7. class="tm-choujiang-bg-image"
  8. :style="{
  9. backgroundImage: `url(${them_data.bg})`
  10. }"
  11. ></view>
  12. <view :animation="animationData" class="tm-choujiang-bg-prod ">
  13. <view
  14. class="tm-choujiang-bg-cp text-red absolute text-align-center "
  15. :style="{
  16. width: intewidth,
  17. height: inteheight,
  18. transform: `rotate(${45 * (index + 1)}deg)`
  19. }"
  20. v-for="(item, index) in listData"
  21. :key="index"
  22. >
  23. <tm-images v-if="item.img" :previmage="false" :width="60" :src="item.img"></tm-images>
  24. <view class="mx-40" style="line-height: 1;">
  25. <text v-if="item.name" class="text-size-xs ">{{ item.name }}</text>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="flex-center tm-choujiang-point"><tm-images v-if="them_data.point" @click="clickImgPlay" :previmage="false" :width="120" :src="them_data.point"></tm-images></view>
  30. </view>
  31. <view class="tm-choujiang-pingtai flex-center"><tm-images v-if="them_data.dizuo" :previmage="false" :width="600" :src="them_data.dizuo"></tm-images></view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. /**
  37. * 转盘抽奖
  38. * @property {Number} theme-index = [] 默认0,主题索引,我已内置两套皮肤。
  39. * @property {Array} theme-list = [] 默认[已内置两套],主题列表数据,已有两套。
  40. * @property {Boolean} disabled = [] 默认 false,是否禁用
  41. * @property {Boolean} disabledCenter = [] 默认 false,是否禁用点中间图片开始。
  42. * @property {Number} duration = [] 默认 3000,动画时长
  43. * @property {Number} turns = [] 默认2,转的圈数
  44. * @property {Array} list = [] 默认 [测试的数据],奖品数据。
  45. * @property {Function} start 游戏开始时触发。
  46. * @property {Function} end 游戏结束触发,并返回结果数组。
  47. * @example <tm-choujiang ></tm-choujiang>
  48. */
  49. import tmImages from "@/tm-vuetify/components/tm-images/tm-images.vue"
  50. export default {
  51. components:{tmImages},
  52. name: 'tm-choujiang',
  53. props: {
  54. themeIndex: {
  55. type: Number,
  56. default: 0
  57. },
  58. themeList: {
  59. type: Array,
  60. default: () => {
  61. return [
  62. {
  63. bg: 'https://jx2d.cn/yuwuimages/choujiang_theme_1_0.png?k=9',
  64. point: 'https://jx2d.cn/yuwuimages/choujiang_theme_1_1.png',
  65. dizuo: 'https://jx2d.cn/yuwuimages/choujiang_theme_1_2.png'
  66. },
  67. {
  68. bg: 'https://jx2d.cn/yuwuimages/choujiang_theme_2_0.png?k=9',
  69. point: 'https://jx2d.cn/yuwuimages/choujiang_theme_2_1.png',
  70. dizuo: 'https://jx2d.cn/yuwuimages/choujiang_theme_2_2.png'
  71. }
  72. ];
  73. }
  74. },
  75. disabled: {
  76. type: Boolean,
  77. default: false
  78. },
  79. // 动画时长
  80. duration: {
  81. type: Number,
  82. default: 3000
  83. },
  84. turns:{
  85. type:Number,
  86. default:3
  87. },
  88. //是否禁用点中间图片开始。
  89. disabledCenter:{
  90. type: Boolean,
  91. default: false
  92. },
  93. list: {
  94. type: Array,
  95. default: () => {
  96. return [
  97. { name: '谢谢惠顾', gailv: 30, id: 1, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  98. { name: '90', gailv: 10, id: 2, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' },
  99. { name: '135', gailv: 10, id: 3, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  100. { name: '180', gailv: 10, id: 4, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' },
  101. { name: '225', gailv: 10, id: 5, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  102. { name: '275', gailv: 10, id: 7, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' },
  103. { name: '315', gailv: 10, id: 8, img: 'https://jx2d.cn/yuwuimages/jiangping_2.png' },
  104. { name: '360', gailv: 10, id: 9, img: 'https://jx2d.cn/yuwuimages/jiangping_1.png' }
  105. ];
  106. }
  107. },
  108. //默认返回真执行,否则不执行开始游戏。
  109. playCallback:{
  110. type:Function,
  111. default:()=>{
  112. return function(){
  113. return true;
  114. }
  115. }
  116. }
  117. },
  118. data() {
  119. return {
  120. animationData: '',
  121. aniOff: true,
  122. jiaodu_old:0,
  123. listData: []
  124. };
  125. },
  126. mounted() {
  127. this.listData = this.list;
  128. this.chuliganlv();
  129. },
  130. watch:{
  131. list:{
  132. handler(){
  133. this.listData = this.list;
  134. this.chuliganlv();
  135. },
  136. deep:true
  137. }
  138. },
  139. computed: {
  140. intewidth: function() {
  141. if (!this.list.length) return 0;
  142. return (uni.upx2px(400) * 3.14) / this.list.length + 'px';
  143. },
  144. inteheight: function() {
  145. if (!this.list.length) return 0;
  146. return uni.upx2px(400) / 2 + 'px';
  147. },
  148. them_index: function() {
  149. return this.themeIndex;
  150. },
  151. them_data: function() {
  152. return this.themeList[this.them_index];
  153. }
  154. },
  155. methods: {
  156. chuliganlv() {
  157. var ml = new this.$tm.choujiang(this.listData);
  158. let jieguo = ml.getResult();
  159. let jd = 360 / this.listData.length;
  160. let index = this.listData.findIndex(item => {
  161. return item.id === jieguo.id;
  162. });
  163. return {
  164. index: index,
  165. data: this.listData[index],
  166. jiaodu: 360 - (index + 1) * 45 + 180*(this.turns*2)
  167. };
  168. },
  169. clickImgPlay(){
  170. if (this.disabledCenter) return;
  171. this.play(this.playCallback);
  172. },
  173. async play(callback) {
  174. if (this.disabled) return;
  175. if (!this.aniOff) {
  176. console.warn('未结束动画,不能连续点击.');
  177. return;
  178. }
  179. let isPlay = true;
  180. if(typeof callback ==='function'){
  181. isPlay = callback();
  182. }
  183. if(isPlay==false) return;
  184. // 开始动画.
  185. this.$emit('start');
  186. let zhongjiang = this.chuliganlv();
  187. this.$nextTick(async function() {
  188. await this.resetInit();
  189. this.animationData = '';
  190. this.aniOff = false;
  191. let duration = this.duration;
  192. var animation = uni.createAnimation({
  193. duration: duration,
  194. timingFunction: 'ease'
  195. });
  196. this.animation = animation;
  197. animation.rotate(zhongjiang.jiaodu).step();
  198. this.animationData = animation.export();
  199. await uni.$tm.sleep(duration);
  200. // 结束动画.
  201. this.aniOff = true;
  202. this.$emit('end', this.listData[zhongjiang.index]);
  203. });
  204. },
  205. // 重置 角度。
  206. async resetInit() {
  207. this.animationData = '';
  208. var animation = uni.createAnimation({
  209. duration: 0,
  210. timingFunction: 'ease'
  211. });
  212. this.animation = animation;
  213. animation.rotate(-45).step();
  214. this.animationData = animation.export();
  215. await uni.$tm.sleep(50);
  216. }
  217. }
  218. };
  219. </script>
  220. <style lang="less" scoped>
  221. .tm-choujiang {
  222. .tm-choujiang-theme-1 {
  223. .tm-choujiang-bg {
  224. width: 600upx;
  225. height: 600upx;
  226. position: relative;
  227. z-index: 2;
  228. .tm-choujiang-bg-image {
  229. width: 100%;
  230. height: 100%;
  231. background-repeat: no-repeat;
  232. background-size: cover;
  233. }
  234. .tm-choujiang-point {
  235. position: absolute;
  236. width: 100%;
  237. height: 100%;
  238. top: 0;
  239. left: 0;
  240. }
  241. .tm-choujiang-bg-prod {
  242. position: absolute;
  243. width: 400upx;
  244. height: 400upx;
  245. top: 100upx;
  246. left: 100upx;
  247. display: flex;
  248. justify-content: center;
  249. transform: rotate(0deg);
  250. .tm-choujiang-bg-cp {
  251. transform-origin: center bottom;
  252. }
  253. }
  254. }
  255. .tm-choujiang-pingtai {
  256. position: relative;
  257. z-index: 1;
  258. margin-top: -120upx;
  259. }
  260. }
  261. }
  262. </style>