activity.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // share/pages/activity/activity.js
  2. const app = getApp();
  3. const utils = require("../../../utils/http");
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. imgUrl:app.globalData.imgUrl,
  10. loginFlag: false,
  11. mobileflag:false,
  12. canIUseGetUserProfile: false,
  13. showWinning: true,
  14. showNoWinning: true,
  15. showNoCount: true,
  16. companyobj: {},
  17. shareobj: {},
  18. turntableDetail: {},
  19. prizesImage: '',
  20. prizesText: '',
  21. actid: '',
  22. textContent: '',
  23. prizesContent: '',
  24. clientype: '',
  25. uploadText: '截图上传',
  26. lottery: 0,
  27. blocks: [{
  28. padding: '30px',
  29. imgs: [{
  30. src: 'https://o.nczyzs.com/xcx/ciecleground.png',
  31. width: '650rpx',
  32. height: '650rpx',
  33. rotate: false,
  34. top: '14rpx'
  35. }]
  36. }],
  37. prizes: [],
  38. buttons: [{
  39. radius: '50px',
  40. pointer: true,
  41. imgs: [{
  42. src: 'https://o.nczyzs.com/xcx/roundcircle.png',
  43. width: '100%',
  44. top: '-130%'
  45. }],
  46. fonts: [{
  47. text: '抽奖',
  48. top: '-20px',
  49. fontSize: '24px',
  50. fontColor: '#ffffff'
  51. }, {
  52. text: '剩余3次',
  53. top: '10px',
  54. fontSize: '12px',
  55. fontColor: '#ffffff'
  56. }]
  57. }
  58. ],
  59. },
  60. start() {
  61. if (!this.data.prizes.length) {
  62. wx.showToast({
  63. title: '没有奖品,无法抽奖',
  64. icon: 'none'
  65. })
  66. return;
  67. }
  68. if (this.data.lottery <= 0) {
  69. this.setData({
  70. showNoCount: false
  71. })
  72. return;
  73. }
  74. // 获取抽奖组件实例
  75. const child = this.selectComponent('#myLucky')
  76. // 调用play方法开始旋转
  77. child.$lucky.play()
  78. // 用定时器模拟请求接口
  79. setTimeout(() => {
  80. var that = this;
  81. utils.$post({
  82. url: app.globalData.webUrl + 'api/lottery/user_lottery',
  83. header: {
  84. 'Authorization': 'bearer ' + app.globalData.token
  85. },
  86. data: {
  87. actid: that.data.actid
  88. },
  89. success: function (res) {
  90. let data = res.data.data;
  91. if (res.data.code == '0') {
  92. let lotteryCount = that.data.lottery - 1;
  93. that.setData({
  94. lottery: lotteryCount
  95. })
  96. that.setsurplusCount(that.data.lottery);
  97. // 3s 后得到中奖索引 (假设抽到第0个奖品)
  98. if (data == -1) {
  99. const index = 0
  100. // 调用stop方法然后缓慢停止
  101. child.$lucky.stop(index)
  102. } else {
  103. const index = that.getWinningPrizeIndex(data);
  104. // 调用stop方法然后缓慢停止
  105. child.$lucky.stop(index)
  106. }
  107. } else {
  108. child.$lucky.stop(0)
  109. }
  110. }
  111. })
  112. }, 3000)
  113. },
  114. end(event) {
  115. // 中奖奖品详情
  116. let data = event.detail.fonts[0];
  117. if (data.id == '111') {
  118. this.setData({
  119. showNoWinning: false,
  120. })
  121. } else {
  122. let imageData = event.detail.imgs[0];
  123. this.setData({
  124. showWinning: false,
  125. prizesImage: imageData.src,
  126. prizesText: data.text
  127. })
  128. }
  129. },
  130. againPlayTurntable() {
  131. this.setData({
  132. showWinning: true,
  133. showNoWinning: true,
  134. showNoCount: true,
  135. })
  136. this.start();
  137. },
  138. getWinningPrizeIndex(prizesId) {
  139. let index = 0;
  140. this.data.prizes.forEach((v, i) => {
  141. if (v.fonts[0].id == prizesId) {
  142. index = i;
  143. }
  144. })
  145. return index;
  146. },
  147. copyTextContent(e) {
  148. wx.setClipboardData({
  149. data: e.currentTarget.dataset.text,
  150. success: function (res) {
  151. console.log(res)
  152. },
  153. fail: function (err) {
  154. console.log(err)
  155. }
  156. })
  157. },
  158. cutUpload() {
  159. let that = this;
  160. if (this.data.uploadText == '已截图上传') {
  161. wx.showToast({
  162. title: '只能截图上传一次',
  163. icon: 'none'
  164. })
  165. return;
  166. }
  167. wx.chooseMedia({
  168. count: 1,
  169. mediaType: ['image'],
  170. sourceType: ['album'],
  171. success: function (res) {
  172. console.log(res)
  173. let tempFilePath = res.tempFiles[0].tempFilePath;
  174. that.uploadImage(tempFilePath)
  175. }
  176. })
  177. },
  178. uploadImage(filePath) {
  179. let that = this;
  180. wx.uploadFile({
  181. url: app.globalData.webUrl + 'api/lottery/user_upimg',
  182. filePath: filePath,
  183. name: 'img',
  184. header: {
  185. 'Authorization': 'bearer ' + app.globalData.token
  186. },
  187. formData: {
  188. actid: this.data.actid
  189. },
  190. success: function (res) {
  191. let data = JSON.parse(res.data);
  192. if (data.code == 0) {
  193. that.setData({
  194. uploadText: '已截图上传'
  195. })
  196. that.addActivityUser();
  197. } else {
  198. wx.showToast({
  199. title: data.msg,
  200. icon: 'none'
  201. })
  202. }
  203. },
  204. fail: function (res) {
  205. console.log(res, '>>>')
  206. }
  207. })
  208. },
  209. closeMask(e) {
  210. this.setData({
  211. showWinning: true,
  212. showNoWinning: true,
  213. showNoCount: true
  214. })
  215. },
  216. preventEvent(e) { },
  217. setsurplusCount(count) {
  218. this.setData({
  219. buttons: [{
  220. radius: '50px',
  221. pointer: true,
  222. imgs: [{
  223. src: 'https://o.nczyzs.com/xcx/roundcircle.png',
  224. width: '100%',
  225. top: '-130%'
  226. }],
  227. fonts: [{
  228. text: '抽奖',
  229. top: '-20px',
  230. fontSize: '24px',
  231. fontColor: '#ffffff'
  232. }, {
  233. text: `剩余${count}次`,
  234. top: '10px',
  235. fontSize: '12px',
  236. fontColor: '#ffffff'
  237. }]
  238. }
  239. ]
  240. })
  241. },
  242. /**
  243. * 客户授权登录
  244. */
  245. login: function () {
  246. var that = this;
  247. wx.login({
  248. success: function (data) {
  249. if (data.errMsg == 'login:ok') {
  250. utils.$post({
  251. method: "POST", //TESTAPIURL APIURL/users/auth
  252. url: app.globalData.webUrl + 'api/users/code2sessionmini',
  253. data: {
  254. client_type: that.clientype,
  255. code: data.code,
  256. share: app.globalData.shareuserid
  257. },
  258. success: function (r) {
  259. if (r.data.code == '0') {
  260. app.globalData.sharepersonobj = r.data.share;
  261. app.globalData.token = r.data.token;
  262. that.setData({
  263. shareobj: app.globalData.sharepersonobj
  264. })
  265. if (!!r.data.user.phone) {
  266. that.setData({
  267. datashow: true,
  268. loginFlag: false,
  269. mobileflag:false,
  270. employeeflag: (!!r.data.user.binded && r.data.user.binded.state == '在职') ? true : false
  271. })
  272. app.globalData.personMsg = r.data.user;
  273. app.globalData.phone = true; //有手机号
  274. that.unreadMsg();
  275. } else {
  276. that.unreadMsg();
  277. wx.hideLoading();
  278. that.setData({
  279. loginFlag: false,
  280. mobileflag:true
  281. })
  282. }
  283. } else {
  284. setTimeout(function () {
  285. wx.hideLoading()
  286. }, 500)
  287. }
  288. }
  289. })
  290. }
  291. }
  292. })
  293. },
  294. unreadMsg: function () {
  295. this.addActivityUser();
  296. },
  297. addActivityUser() {
  298. var that = this;
  299. console.log(that.data.actid,app.globalData.currentUserId,app.globalData.shareuserid,'<<<活动ID')
  300. utils.$post({
  301. url: app.globalData.webUrl + 'api/lottery/add_user',
  302. header: {
  303. 'Authorization': 'bearer ' + app.globalData.token
  304. },
  305. data: {
  306. actid: that.data.actid,
  307. empid: app.globalData.currentUserId
  308. },
  309. success: function (res) {
  310. let data = res.data.data;
  311. console.log(data,'>>>活动信息')
  312. if (res.data.code == '0') {
  313. that.setsurplusCount(data.user_data.lottery);
  314. that.setData({
  315. turntableDetail: data.act_data,
  316. lottery: data.user_data.lottery,
  317. prizes: data.act_data ? that.handleGoods(data.act_data.good) : [],
  318. prizesContent: data.act_data ? data.act_data.good.map(v => v.good_name).join('/') : '',
  319. uploadText: data.user_data.share_status == 1 ? '截图上传' : '已截图上传'
  320. })
  321. wx.setNavigationBarTitle({
  322. title: data.act_data.title,
  323. })
  324. that.getActivityInfo();
  325. } else {
  326. let msg = res.data.msg;
  327. wx.showModal({
  328. title: '大转盘提示',
  329. content: msg,
  330. showCancel: false,
  331. confirmText: '退出应用',
  332. success: () => {
  333. wx.exitMiniProgram();
  334. }
  335. })
  336. }
  337. }
  338. })
  339. },
  340. getActivityInfo() {
  341. var that = this;
  342. utils.$post({
  343. url: app.globalData.webUrl + 'api/lottery/user_detail_lukus',
  344. header: {
  345. 'Authorization': 'bearer ' + app.globalData.token
  346. },
  347. data: {
  348. actid: that.data.actid
  349. },
  350. success: function (res) {
  351. let data = res.data.data;
  352. if (res.data.code == '0') {
  353. let str = that.setWinningString(data);
  354. that.setData({
  355. textContent: '中奖客户:' + str
  356. })
  357. }
  358. }
  359. })
  360. },
  361. setWinningString(data) {
  362. if (data.length > 3) {
  363. let client = data.slice(0, 3);
  364. return client.map(v => v.user.nickname).join(',');
  365. } else {
  366. return data.map(v => v.user.nickname).join(',');
  367. }
  368. },
  369. handleGoods(goodList) {
  370. let goods = goodList.map((v, i) => {
  371. return {
  372. fonts: [{
  373. text: v.good_name,
  374. top: '10%',
  375. id: v.id,
  376. fontSize: '24rpx'
  377. }],
  378. imgs: [{
  379. src: v.good_img || 'https://o.nczyzs.com/xcx/circlegift.png',
  380. width: '50rpx',
  381. height: '50rpx',
  382. top: '50%'
  383. }],
  384. background: i % 2 == 0 ? '#FFFFFF' : '#FFD6B1',
  385. }
  386. });
  387. goods.splice(0, '', {
  388. fonts: [{
  389. text: '谢谢参与',
  390. top: '10%',
  391. id: '111',
  392. fontSize: '24rpx',
  393. }],
  394. imgs: [{
  395. src: 'https://o.nczyzs.com/xcx/circlegift.png',
  396. width: '50rpx',
  397. height: '50rpx',
  398. top: '50%'
  399. }],
  400. background: '#f0f0f0'
  401. })
  402. return goods;
  403. },
  404. sharecompany: function () {
  405. const that = this;
  406. utils.$post({
  407. url: app.globalData.webUrl + 'client/index/content_belong_company',
  408. header: {
  409. 'Authorization': 'bearer ' + app.globalData.token
  410. },
  411. data: {
  412. client_type: app.globalData.clientype,
  413. uid: app.globalData.shareuserid
  414. },
  415. success: function (res) {
  416. if (res.data.code == '0') {
  417. app.globalData.companyobj=res.data.data;
  418. that.setData({
  419. companyobj: res.data.data
  420. })
  421. }
  422. },
  423. complete(res) {
  424. wx.hideLoading()
  425. }
  426. })
  427. },
  428. myWinning() {
  429. wx.navigateTo({
  430. url: '/pages/other/other?type=63&actid=' + this.data.actid,
  431. })
  432. },
  433. /**
  434. * 生命周期函数--监听页面加载
  435. */
  436. onLoad(options) {
  437. if (options.actid) {
  438. this.setData({
  439. actid: options.actid
  440. })
  441. }
  442. if (options.empid) {
  443. app.globalData.currentUserId = options.empid
  444. }
  445. if (options.uid) {
  446. app.globalData.shareuserid = options.uid;
  447. }
  448. if (options.clientype) {
  449. this.setData({
  450. clientype: options.clientype
  451. })
  452. app.globalData.clientype = options.clientype;
  453. }
  454. if (wx.getUserProfile) {
  455. this.setData({
  456. canIUseGetUserProfile: true
  457. })
  458. }
  459. this.setData({
  460. imgUrl:app.globalData.imgUrl,
  461. })
  462. this.login();
  463. wx.hideHomeButton()
  464. },
  465. /**
  466. * 生命周期函数--监听页面初次渲染完成
  467. */
  468. onReady() {
  469. },
  470. /**
  471. * 生命周期函数--监听页面显示
  472. */
  473. onShow() {
  474. },
  475. /**
  476. * 生命周期函数--监听页面隐藏
  477. */
  478. onHide() {
  479. },
  480. /**
  481. * 生命周期函数--监听页面卸载
  482. */
  483. onUnload() {
  484. },
  485. /**
  486. * 页面相关事件处理函数--监听用户下拉动作
  487. */
  488. onPullDownRefresh() {
  489. },
  490. /**
  491. * 页面上拉触底事件的处理函数
  492. */
  493. onReachBottom() {
  494. },
  495. /**
  496. * 用户点击右上角分享
  497. */
  498. })