luckywheel.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. var app = getApp()
  2. Page({
  3. data: {
  4. awardsList: {},
  5. animationData: {},
  6. btnDisabled: '',
  7. record: [],
  8. prize: '',
  9. mobile: '',
  10. awardsConfig: [
  11. { 'index': 1, 'name': '你要发', awards: '一等奖' },
  12. { 'index': 2, 'name': '路路顺', awards: '二等奖'},
  13. { 'index': 3, 'name': '一定发', awards: '三等奖'},
  14. { 'index': 4, 'name': '财运来', awards: '四等奖'},
  15. { 'index': 5, 'name': '好运来', awards: '五等奖'},
  16. { 'index': 6, 'name': '谢谢参与', awards: '谢谢参与'}
  17. ]
  18. },
  19. onLoad:function () {
  20. let _this = this;
  21. _this.getList();
  22. },
  23. onShow: function () {
  24. let _this = this;
  25. let mobile = wx.getStorageSync('persion').persion.mobile;
  26. if(!mobile) {
  27. wx.showModal({
  28. title: '提示',
  29. content: '您没有绑定手机号!',
  30. confirmText: '绑定抽奖',
  31. cancelText: '残忍拒绝',
  32. success(res) {
  33. if (res.confirm) {
  34. wx.navigateTo({
  35. url: '/pages/mine/wxml/Connect/Connect'
  36. })
  37. } else if (res.cancel) {
  38. wx.navigateBack({
  39. delta: 1
  40. })
  41. }
  42. }
  43. })
  44. } else {
  45. var reg = /^(\d{3})\d{4}(\d{4})$/;
  46. _this.setData({
  47. mobile: mobile.replace(reg, "$1****$2")
  48. })
  49. }
  50. },
  51. startLottery: function (e) {
  52. let _this = this;
  53. _this.setData({
  54. btnDisabled: 'disabled'
  55. })
  56. wx.request({
  57. url: app.globalData.prodUrl + 'web-pm/wx/pm/wx_lottery_raffle',
  58. data: '',
  59. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  60. method: 'GET',
  61. dataType: 'json',
  62. responseType: 'text',
  63. success: function(data) {
  64. if(data.data.tip.type === "success"){
  65. _this.getLottery(data.data.ranking-1)
  66. } else {
  67. wx.showModal({
  68. title: '提示',
  69. content: data.data.tip.msg,
  70. showCancel: false
  71. });
  72. _this.setData({
  73. btnDisabled: ''
  74. })
  75. }
  76. },
  77. fail: function(res) {},
  78. complete: function (res) {},
  79. })
  80. },
  81. getLottery: function (awards) {
  82. var that = this
  83. // var num = Math.random();
  84. // var awardIndex = num * 6 >>> 0;
  85. var awardIndex = awards;
  86. // 获取奖品配置
  87. var awardsConfig = app.awardsConfig
  88. // if (awardIndex < 2) awardsConfig.chance = false
  89. // 初始化 rotate
  90. var animationInit = wx.createAnimation({
  91. duration: 1
  92. })
  93. this.animationInit = animationInit;
  94. animationInit.rotate(0).step()
  95. this.setData({
  96. animationData: animationInit.export(),
  97. btnDisabled: 'disabled'
  98. })
  99. // 旋转抽奖
  100. setTimeout(function () {
  101. var animationRun = wx.createAnimation({
  102. duration: 4000,
  103. timingFunction: 'ease'
  104. })
  105. that.animationRun = animationRun
  106. animationRun.rotate(360 * 8 - awardIndex * (360 / 6)).step()
  107. that.setData({
  108. animationData: animationRun.export()
  109. })
  110. }, 100)
  111. // 中奖提示
  112. setTimeout(function () {
  113. wx.showModal({
  114. title: '恭喜',
  115. content: '您抽中了' + (awardsConfig.awards[awardIndex].awards + awardsConfig.awards[awardIndex].name),
  116. showCancel: false
  117. });
  118. if (awardsConfig.chance) {
  119. that.setData({
  120. btnDisabled: ''
  121. })
  122. };
  123. that.getList();
  124. }, 4100);
  125. /*wx.request({
  126. url: '../../data/getLottery.json',
  127. data: {},
  128. header: {
  129. 'Content-Type': 'application/json'
  130. },
  131. success: function(data) {
  132. },
  133. fail: function(error) {
  134. wx.showModal({
  135. title: '抱歉',
  136. content: '网络异常,请重试',
  137. showCancel: false
  138. })
  139. }
  140. })*/
  141. },
  142. onReady: function (e) {
  143. var that = this;
  144. // getAwardsConfig
  145. app.awardsConfig = {
  146. chance: true,
  147. awards: [
  148. { 'index': 1, 'name': '你要发', awards: '一等奖' },
  149. { 'index': 2, 'name': '路路顺', awards: '二等奖' },
  150. { 'index': 3, 'name': '一定发', awards: '三等奖' },
  151. { 'index': 4, 'name': '财运来', awards: '四等奖' },
  152. { 'index': 5, 'name': '好运来', awards: '五等奖' },
  153. { 'index': 6, 'name': '谢谢参与', awards: '' }
  154. ]
  155. }
  156. // wx.setStorageSync('awardsConfig', JSON.stringify(awardsConfig))
  157. // 绘制转盘
  158. var awardsConfig = app.awardsConfig.awards,
  159. len = awardsConfig.length,
  160. rotateDeg = 360 / len / 2 + 90,
  161. html = [],
  162. turnNum = 1 / len // 文字旋转 turn 值
  163. that.setData({
  164. btnDisabled: app.awardsConfig.chance ? '' : 'disabled'
  165. })
  166. var ctx = wx.createContext()
  167. for (var i = 0; i < len; i++) {
  168. // 保存当前状态
  169. ctx.save();
  170. // 开始一条新路径
  171. ctx.beginPath();
  172. // 位移到圆心,下面需要围绕圆心旋转
  173. ctx.translate(150, 150);
  174. // 从(0, 0)坐标开始定义一条新的子路径
  175. ctx.moveTo(0, 0);
  176. // 旋转弧度,需将角度转换为弧度,使用 degrees * Math.PI/180 公式进行计算。
  177. ctx.rotate((360 / len * i - rotateDeg) * Math.PI / 180);
  178. // 绘制圆弧
  179. ctx.arc(0, 0, 150, 0, 2 * Math.PI / len, false);
  180. // 颜色间隔
  181. if (i % 2 == 0) {
  182. ctx.setFillStyle('#ffb820');
  183. } else {
  184. ctx.setFillStyle('#ffcb3f');
  185. }
  186. // 填充扇形
  187. ctx.fill();
  188. // 绘制边框
  189. ctx.setLineWidth(0.5);
  190. ctx.setStrokeStyle('#e4370e');
  191. ctx.stroke();
  192. // 恢复前一个状态
  193. ctx.restore();
  194. // 奖项列表
  195. html.push({ turn: i * turnNum + 'turn', award: awardsConfig[i].name });
  196. }
  197. that.setData({
  198. awardsList: html
  199. });
  200. wx.drawCanvas({
  201. canvasId: 'lotteryCanvas',
  202. actions: ctx.getActions()
  203. })
  204. },
  205. getList: function () {
  206. let _this = this;
  207. wx.request({
  208. url: app.globalData.prodUrl + 'web-pm/wx/pm/list_lottery',
  209. data: '',
  210. header: { 'ydw-token': wx.getStorageSync('key').ydw_token },
  211. method: 'GET',
  212. dataType: 'json',
  213. responseType: 'text',
  214. success: function (data) {
  215. if (data.data.data.length > 0) {
  216. _this.setData({
  217. record: data.data.data,
  218. prize: _this.data.awardsConfig[data.data.data[0].ranking - 1].name
  219. });
  220. }
  221. },
  222. fail: function (res) { },
  223. complete: function (res) { },
  224. })
  225. }
  226. })