东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

494 line
13KB

  1. // pages/luckyStar/luckyStar.js
  2. const app = getApp();
  3. var query = null;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. imgUrl: app.globalData.urlStatic,//图片路径
  10. ruleShow: true,//是否显示游戏玩法
  11. ruleCloseShow: true,//是否显示游戏玩法关闭按钮
  12. clawTop:-440,//爪子的top值
  13. clawLeft: 268,//爪子的left值
  14. clawScale: 1,//爪子的scale值
  15. downNum:30,//倒计时时间
  16. setInt:null,//倒计时元素
  17. pizeTip:0,//抓奖提示框
  18. setGroup:{
  19. left:null,
  20. right: null,
  21. top: null,
  22. bottom: null,
  23. },
  24. btnGroup:{
  25. left: false,
  26. right: false,
  27. top: false,
  28. bottom: false,
  29. claw:false
  30. },
  31. gameSign: null, //游戏结束时需要
  32. gameState:false,//游戏状态
  33. endGameData:null,//中奖数据
  34. isAddress:false,//是否有地址
  35. configure:{
  36. page:1,
  37. count:10
  38. },
  39. gameAwardList:"",
  40. gameAwardListArr:"",
  41. userData:null,
  42. scrollNum:0,
  43. setInt:null,
  44. startDown:-1,//游戏开始倒计时
  45. clawType:false,//是否正在抓取
  46. gameEnd:true,
  47. ani:null
  48. },
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. onLoad: function (options) {
  53. query = wx.createSelectorQuery();
  54. app.globalData.nowPage = 3;
  55. if (app.globalData.openid) {
  56. this.loadFun();
  57. } else {
  58. app.globalData.openidSuccessFuc = this.loadFun;
  59. }
  60. },
  61. loadFun: function () {
  62. if (app.globalData.userInfoData) {
  63. this.setData({
  64. userData: app.globalData.userInfoData
  65. })
  66. }
  67. this.getAddress();
  68. this.getGameAwardList();
  69. },
  70. /**
  71. * 生命周期函数--监听页面初次渲染完成
  72. */
  73. onReady: function () {
  74. },
  75. /**
  76. * 生命周期函数--监听页面显示
  77. */
  78. onShow: function () {
  79. },
  80. /**
  81. * 生命周期函数--监听页面隐藏
  82. */
  83. onHide: function () {
  84. },
  85. /**
  86. * 生命周期函数--监听页面卸载
  87. */
  88. onUnload: function () {
  89. },
  90. /**
  91. * 页面相关事件处理函数--监听用户下拉动作
  92. */
  93. onPullDownRefresh: function () {
  94. },
  95. /**
  96. * 页面上拉触底事件的处理函数
  97. */
  98. onReachBottom: function () {
  99. },
  100. /**
  101. * 用户点击右上角分享
  102. */
  103. onShareAppMessage: function () {
  104. return app.sharePack();
  105. },
  106. closeRule:function(){//关闭游戏玩法
  107. this.setData({
  108. ruleShow:false
  109. })
  110. },
  111. openGameRule: function () {//打开游戏玩法
  112. if(!this.data.gameState){
  113. this.setData({
  114. ruleShow: true
  115. })
  116. }
  117. },
  118. gameStart: function () {//开始游戏按钮
  119. this.setData({
  120. ruleShow: false,
  121. ruleCloseShow: true
  122. })
  123. this.beginGame();
  124. },
  125. beginGame: function () {//开始游戏
  126. app.wxRequest(app.globalData.urlRoot + "dollGame/beginGame", {}, res => {
  127. if(res.code==200){
  128. this.setData({
  129. startDown: 3
  130. })
  131. var setInt2 = setInterval(() => {
  132. if (this.data.startDown > -1) {
  133. this.data.startDown -= 1;
  134. this.setData({
  135. startDown: this.data.startDown
  136. })
  137. if (this.data.startDown==-1){
  138. clearInterval(setInt2);
  139. this.data.gameSign = res.data.sign;
  140. this.endGame();
  141. if (this.data.gameSign) {
  142. this.downTimeFun();
  143. }
  144. }
  145. }
  146. }, 1000);
  147. }else{
  148. wx.showToast({
  149. title: res.msg,
  150. icon:"none"
  151. })
  152. }
  153. },this)
  154. },
  155. endGame:function(){//结束游戏
  156. app.wxRequest(app.globalData.urlRoot + "dollGame/endGame", { sign:this.data.gameSign}, res => {
  157. if(res.code=200){
  158. this.setData({
  159. endGameData: res.data
  160. })
  161. }else{
  162. console.log(res);
  163. }
  164. }, this,"POST")
  165. },
  166. startClaw:function(e){//开始控制爪子方向
  167. if(!this.data.gameState){
  168. return;
  169. }
  170. let direction = e.currentTarget.dataset.direction;
  171. if (direction == "L") {//向左
  172. this.data.btnGroup.left = true;
  173. this.setData({
  174. btnGroup: this.data.btnGroup
  175. })
  176. if (this.data.clawLeft <= 110) {
  177. return;
  178. }
  179. this.data.setGroup.left = setInterval(() => {
  180. this.setData({
  181. clawLeft: this.data.clawLeft-=2
  182. })
  183. if (this.data.clawLeft <= 110) {
  184. clearInterval(this.data.setGroup.left);
  185. }
  186. },20);
  187. } else if (direction == "R") {//向右
  188. this.data.btnGroup.right = true;
  189. this.setData({
  190. btnGroup: this.data.btnGroup
  191. })
  192. if (this.data.clawLeft >= 415) {
  193. return;
  194. }
  195. this.data.setGroup.right = setInterval(() => {
  196. this.setData({
  197. clawLeft: this.data.clawLeft += 2
  198. })
  199. if (this.data.clawLeft >= 415) {
  200. clearInterval(this.data.setGroup.right);
  201. }
  202. }, 20);
  203. } else if (direction == "T") {//向后
  204. this.data.btnGroup.top = true;
  205. this.setData({
  206. btnGroup: this.data.btnGroup
  207. })
  208. if (this.data.clawScale <= 0.8) {
  209. return;
  210. }
  211. this.data.setGroup.top = setInterval(() => {
  212. this.setData({
  213. clawScale: this.data.clawScale -= 0.01
  214. })
  215. if (this.data.clawScale <= 0.8) {
  216. clearInterval(this.data.setGroup.top);
  217. }
  218. }, 20);
  219. } else if (direction == "B") {//向前
  220. this.data.btnGroup.bottom = true;
  221. this.setData({
  222. btnGroup: this.data.btnGroup
  223. })
  224. if (this.data.clawScale >= 1.2) {
  225. return;
  226. }
  227. this.data.setGroup.bottom = setInterval(() => {
  228. this.setData({
  229. clawScale: this.data.clawScale += 0.01
  230. })
  231. if (this.data.clawScale >= 1.2) {
  232. clearInterval(this.data.setGroup.bottom);
  233. }
  234. }, 20);
  235. }
  236. },
  237. endClaw: function (e) {//结束爪子动作
  238. if (!this.data.gameState) {
  239. return;
  240. }
  241. let direction = e.currentTarget.dataset.direction;
  242. if (direction == "L") {//向左
  243. this.data.btnGroup.left = false;
  244. this.setData({
  245. btnGroup: this.data.btnGroup
  246. })
  247. clearInterval(this.data.setGroup.left);
  248. } else if (direction == "R") {//向右
  249. this.data.btnGroup.right = false;
  250. this.setData({
  251. btnGroup: this.data.btnGroup
  252. })
  253. clearInterval(this.data.setGroup.right);
  254. } else if (direction == "T") {//向后
  255. this.data.btnGroup.top = false;
  256. this.setData({
  257. btnGroup: this.data.btnGroup
  258. })
  259. clearInterval(this.data.setGroup.top);
  260. } else if (direction == "B") {//向前
  261. this.data.btnGroup.bottom = false;
  262. this.setData({
  263. btnGroup: this.data.btnGroup
  264. })
  265. clearInterval(this.data.setGroup.bottom);
  266. }
  267. },
  268. getClaw:function(){//抓取
  269. this.setData({
  270. clawType:true
  271. })
  272. this.setData({
  273. clawTop: this.data.clawScale > 1 ? -100 - (2 * (this.data.clawScale - 1) * 440) : -100 + (2 * (1 - this.data.clawScale) * 440 )
  274. })
  275. this.closeSetInt();
  276. setTimeout(() => {
  277. this.setData({
  278. clawType: false
  279. })
  280. this.data.gameState = false;
  281. if (this.data.endGameData) {
  282. this.setData({
  283. pizeTip: 1
  284. })
  285. } else {
  286. if (this.data.isAddress) {
  287. this.setData({
  288. pizeTip: 2
  289. })
  290. } else {
  291. this.setData({
  292. pizeTip: 3
  293. })
  294. }
  295. }
  296. },800)
  297. },
  298. downTimeFun: function () {//游戏倒计时
  299. this.data.gameState = true;
  300. this.data.setInt = setInterval(()=>{
  301. this.data.downNum -= 1;
  302. if (this.data.downNum<10){
  303. this.data.downNum = '0' + this.data.downNum;
  304. }
  305. this.setData({
  306. downNum: this.data.downNum
  307. })
  308. if (this.data.downNum < 1) {
  309. this.getClaw();
  310. }
  311. },1000);
  312. },
  313. closeSetInt: function () {//关闭倒计时
  314. clearInterval(this.data.setInt);
  315. this.setData({
  316. downNum: 30,
  317. clawTop: this.data.clawScale > 1 ? -100 - (2 * (this.data.clawScale - 1) * 440) : -100 + (2 * (1 - this.data.clawScale) * 440)
  318. })
  319. setTimeout(() => {
  320. this.setData({
  321. gameEnd:false
  322. })
  323. this.setData({
  324. downNum: 30,
  325. clawScale: 1
  326. })
  327. setTimeout(() => {
  328. this.setData({
  329. clawTop: -440,
  330. })
  331. }, 800)
  332. setTimeout(()=>{
  333. this.setData({
  334. clawLeft: 268
  335. })
  336. },1600)
  337. setTimeout(()=>{
  338. this.setData({
  339. gameEnd: true
  340. })
  341. },2400)
  342. }, 800);
  343. },
  344. prizeLook:function(){//活动奖品
  345. if(!this.data.gameState){
  346. wx.navigateTo({
  347. url: '../prizes/prizes'
  348. })
  349. }
  350. },
  351. getAddress: function () {//获取地址
  352. app.wxRequest(app.globalData.urlRoot + "address/getAddress", {}, res => {
  353. if (res.code == 200) {
  354. if (res.data) {
  355. this.setData({
  356. isAddress:true
  357. })
  358. }
  359. }
  360. }, this)
  361. },
  362. receive:function(){//立即领取
  363. wx.redirectTo({
  364. url: '../myCenter/myCenter',
  365. })
  366. },
  367. invitation: function () {//邀请好友一起来玩
  368. },
  369. userRegister: function () {//立即注册,探索更多星探好礼
  370. wx.navigateTo({
  371. url: '../address/address',
  372. })
  373. },
  374. closeWindow:function(){//关闭中奖
  375. this.setData({
  376. pizeTip:0,
  377. ruleShow:true
  378. })
  379. },
  380. getGameAwardList: function () {//获取游戏中奖数据
  381. app.wxRequest(app.globalData.urlRoot + "award/getGameAwardList", this.data.configure,res=>{
  382. if(res.code==200){
  383. if(res.data){
  384. var stringContent = "";//字符串内容
  385. for (let i = 0; i < res.data.length; i++) {//内容整合
  386. res.data[i].content = "恭喜" + (res.data[i].nickName ? res.data[i].nickName : '***') + "获得" + res.data[i].awardName+" ";
  387. stringContent += res.data[i].content;
  388. }
  389. this.data.gameAwardListArr += stringContent;
  390. stringContent = stringContent.substring(0, stringContent.length - 2);
  391. this.setData({
  392. gameAwardList: stringContent
  393. })
  394. query.select('.textFrame').boundingClientRect();
  395. query.select('.tipContent').boundingClientRect();
  396. var duration = 0;//动画时常
  397. query.exec((option) => {
  398. duration = option[1].width < option[0].width ? 10 : option[1].width / option[0].width * 10;
  399. this.aniFun(duration * 1000, -option[1].width);
  400. if (res.data.length >= this.data.configure.count) {
  401. console.log("还有更多数据");
  402. setTimeout(() => {
  403. this.aniFun(0, '100%');
  404. this.data.configure.page += 1;
  405. this.getGameAwardList();
  406. }, duration * 1000)
  407. } else {
  408. console.log("没有更多数据了");
  409. setTimeout(() => {
  410. this.aniFun(0, '100%');
  411. this.data.gameAwardListArr = this.data.gameAwardListArr.substring(0, this.data.gameAwardListArr.length - 2);
  412. this.setData({
  413. gameAwardList: this.data.gameAwardListArr
  414. })
  415. query.select('.textFrame').boundingClientRect();
  416. query.select('.tipContent').boundingClientRect();
  417. query.exec((option) => {
  418. duration = option[1].width < option[0].width ? 10 : option[1].width / option[0].width * 10;
  419. this.noMoreData(duration * 1000, -option[1].width);
  420. })
  421. },duration * 1000);
  422. }
  423. })
  424. }
  425. }
  426. },this)
  427. },
  428. noMoreData: function (duration,left){
  429. console.log(1);
  430. this.aniFun(duration, left);
  431. setTimeout(() => {
  432. this.aniFun(0, '100%');
  433. setTimeout(() => {
  434. this.noMoreData(duration, left);
  435. },50);
  436. }, duration)
  437. },
  438. aniFun(duration,left){
  439. var animation = wx.createAnimation({
  440. duration: duration
  441. });
  442. animation.left(left).step();
  443. this.setData({
  444. ani: animation.export()
  445. })
  446. },
  447. getUserWxMsg: function (e) {//通过微信获取用户信息
  448. if (e.detail.errMsg == "getUserInfo:ok") {
  449. this.gameStart();
  450. this.setData({
  451. userData: e.detail.userInfo
  452. })
  453. app.submitUserMsg(e.detail.userInfo.avatarUrl, e.detail.userInfo.nickName);
  454. }else{
  455. this.gameStart();
  456. }
  457. },
  458. cutPage:function(){//500元待领取
  459. wx.redirectTo({
  460. url: '/pages/star/star',
  461. })
  462. },
  463. openClaw:function(){
  464. if (!this.data.gameState) {
  465. return;
  466. }
  467. this.data.btnGroup.claw = true;
  468. this.setData({
  469. btnGroup: this.data.btnGroup
  470. })
  471. setTimeout(()=>{
  472. this.data.btnGroup.claw = false;
  473. this.setData({
  474. btnGroup: this.data.btnGroup
  475. })
  476. this.getClaw();
  477. },200);
  478. }
  479. })