activity.js 16 KB

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