activity.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. canIUseGetUserProfile: false,
  12. showWinning: true,
  13. showNoWinning: true,
  14. showNoCount: true,
  15. companyobj: {},
  16. shareobj: {},
  17. turntableDetail: {},
  18. prizesImage: '',
  19. prizesText: '',
  20. actid: '',
  21. textContent: '',
  22. prizesContent: '',
  23. clientype: '',
  24. uploadText: '截图上传',
  25. lottery: 0,
  26. blocks: [{
  27. padding: '30px',
  28. imgs: [{
  29. src: 'https://o.nczyzs.com/xcx/ciecleground.png',
  30. width: '650rpx',
  31. height: '650rpx',
  32. rotate: false,
  33. top: '14rpx'
  34. }]
  35. }],
  36. prizes: [],
  37. buttons: [{
  38. radius: '50px',
  39. pointer: true,
  40. imgs: [{
  41. src: 'https://o.nczyzs.com/xcx/roundcircle.png',
  42. width: '100%',
  43. top: '-130%'
  44. }],
  45. fonts: [{
  46. text: '抽奖',
  47. top: '-20px',
  48. fontSize: '24px',
  49. fontColor: '#ffffff'
  50. }, {
  51. text: '剩余3次',
  52. top: '10px',
  53. fontSize: '12px',
  54. fontColor: '#ffffff'
  55. }]
  56. }
  57. ],
  58. },
  59. start() {
  60. if (!this.data.prizes.length) {
  61. wx.showToast({
  62. title: '没有奖品,无法抽奖',
  63. icon: 'none'
  64. })
  65. return;
  66. }
  67. if (this.data.lottery <= 0) {
  68. this.setData({
  69. showNoCount: false
  70. })
  71. return;
  72. }
  73. // 获取抽奖组件实例
  74. const child = this.selectComponent('#myLucky')
  75. // 调用play方法开始旋转
  76. child.$lucky.play()
  77. // 用定时器模拟请求接口
  78. setTimeout(() => {
  79. var that = this;
  80. utils.$post({
  81. url: app.globalData.webUrl + 'api/lottery/user_lottery',
  82. header: {
  83. 'Authorization': 'bearer ' + app.globalData.token
  84. },
  85. data: {
  86. actid: that.data.actid
  87. },
  88. success: function (res) {
  89. let data = res.data.data;
  90. if (res.data.code == '0') {
  91. let lotteryCount = that.data.lottery - 1;
  92. that.setData({
  93. lottery: lotteryCount
  94. })
  95. that.setsurplusCount(that.data.lottery);
  96. // 3s 后得到中奖索引 (假设抽到第0个奖品)
  97. if (data == -1) {
  98. const index = 0
  99. // 调用stop方法然后缓慢停止
  100. child.$lucky.stop(index)
  101. } else {
  102. const index = that.getWinningPrizeIndex(data);
  103. // 调用stop方法然后缓慢停止
  104. child.$lucky.stop(index)
  105. }
  106. } else {
  107. child.$lucky.stop(0)
  108. }
  109. }
  110. })
  111. }, 3000)
  112. },
  113. end(event) {
  114. // 中奖奖品详情
  115. let data = event.detail.fonts[0];
  116. if (data.id == '111') {
  117. this.setData({
  118. showNoWinning: false,
  119. })
  120. } else {
  121. let imageData = event.detail.imgs[0];
  122. this.setData({
  123. showWinning: false,
  124. prizesImage: imageData.src,
  125. prizesText: data.text
  126. })
  127. }
  128. },
  129. againPlayTurntable() {
  130. this.setData({
  131. showWinning: true,
  132. showNoWinning: true,
  133. showNoCount: true,
  134. })
  135. this.start();
  136. },
  137. getWinningPrizeIndex(prizesId) {
  138. let index = 0;
  139. this.data.prizes.forEach((v, i) => {
  140. if (v.fonts[0].id == prizesId) {
  141. index = i;
  142. }
  143. })
  144. return index;
  145. },
  146. copyTextContent(e) {
  147. wx.setClipboardData({
  148. data: e.currentTarget.dataset.text,
  149. success: function (res) {
  150. console.log(res)
  151. },
  152. fail: function (err) {
  153. console.log(err)
  154. }
  155. })
  156. },
  157. cutUpload() {
  158. let that = this;
  159. if (this.data.uploadText == '已截图上传') {
  160. wx.showToast({
  161. title: '只能截图上传一次',
  162. icon: 'none'
  163. })
  164. return;
  165. }
  166. wx.chooseMedia({
  167. count: 1,
  168. mediaType: ['image'],
  169. sourceType: ['album'],
  170. success: function (res) {
  171. console.log(res)
  172. let tempFilePath = res.tempFiles[0].tempFilePath;
  173. that.uploadImage(tempFilePath)
  174. }
  175. })
  176. },
  177. uploadImage(filePath) {
  178. let that = this;
  179. wx.uploadFile({
  180. url: app.globalData.webUrl + 'api/lottery/user_upimg',
  181. filePath: filePath,
  182. name: 'img',
  183. header: {
  184. 'Authorization': 'bearer ' + app.globalData.token
  185. },
  186. formData: {
  187. actid: this.data.actid
  188. },
  189. success: function (res) {
  190. let data = JSON.parse(res.data);
  191. if (data.code == 0) {
  192. that.setData({
  193. uploadText: '已截图上传'
  194. })
  195. that.addActivityUser();
  196. } else {
  197. wx.showToast({
  198. title: data.msg,
  199. icon: 'none'
  200. })
  201. }
  202. },
  203. fail: function (res) {
  204. console.log(res, '>>>')
  205. }
  206. })
  207. },
  208. closeMask(e) {
  209. this.setData({
  210. showWinning: true,
  211. showNoWinning: true,
  212. showNoCount: true
  213. })
  214. },
  215. preventEvent(e) { },
  216. setsurplusCount(count) {
  217. this.setData({
  218. buttons: [{
  219. radius: '50px',
  220. pointer: true,
  221. imgs: [{
  222. src: 'https://o.nczyzs.com/xcx/roundcircle.png',
  223. width: '100%',
  224. top: '-130%'
  225. }],
  226. fonts: [{
  227. text: '抽奖',
  228. top: '-20px',
  229. fontSize: '24px',
  230. fontColor: '#ffffff'
  231. }, {
  232. text: `剩余${count}次`,
  233. top: '10px',
  234. fontSize: '12px',
  235. fontColor: '#ffffff'
  236. }]
  237. }
  238. ]
  239. })
  240. },
  241. /**
  242. * 客户授权登录
  243. */
  244. login: function () {
  245. var that = this;
  246. wx.login({
  247. success: function (data) {
  248. if (data.errMsg == 'login:ok') {
  249. utils.$post({
  250. method: "POST", //TESTAPIURL APIURL/users/auth
  251. url: app.globalData.webUrl + 'api/users/code2session',
  252. data: {
  253. client_type: that.clientype,
  254. code: data.code,
  255. share: app.globalData.shareuserid
  256. },
  257. success: function (r) {
  258. if (r.data.code == '0') {
  259. app.globalData.sharepersonobj = r.data.share;
  260. app.globalData.token = r.data.token;
  261. that.setData({
  262. shareobj: app.globalData.sharepersonobj
  263. })
  264. if (r.data.user != null && (!!r.data.user.headimgurl && r.data.user.headimgurl != "" || !!r.data.user.nickname && r.data.user.nickname != "" && r.data.user.nickname != '游客')) {
  265. that.setData({
  266. datashow: true,
  267. loginFlag: false,
  268. employeeflag: (!!r.data.user.binded && r.data.user.binded.state == '在职') ? true : false
  269. })
  270. app.globalData.personMsg = r.data.user;
  271. app.globalData.userflag = false; //有个人信息
  272. var nickname = r.data.user.nickname;
  273. var phone = r.data.user.phone;
  274. app.globalData.phone = false; //没有手机号
  275. if (r.data.user.phone == '') {
  276. wx.hideLoading();
  277. app.globalData.phone = false; //没有手机号
  278. that.loginbox = that.selectComponent("#loginbox");
  279. that.loginbox.loginfun();
  280. } else {
  281. app.globalData.phone = true; //有手机号
  282. }
  283. that.unreadMsg();
  284. } else {
  285. wx.hideLoading();
  286. that.setData({
  287. loginFlag: 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. this.login();
  470. wx.hideHomeButton()
  471. },
  472. /**
  473. * 生命周期函数--监听页面初次渲染完成
  474. */
  475. onReady() {
  476. },
  477. /**
  478. * 生命周期函数--监听页面显示
  479. */
  480. onShow() {
  481. },
  482. /**
  483. * 生命周期函数--监听页面隐藏
  484. */
  485. onHide() {
  486. },
  487. /**
  488. * 生命周期函数--监听页面卸载
  489. */
  490. onUnload() {
  491. },
  492. /**
  493. * 页面相关事件处理函数--监听用户下拉动作
  494. */
  495. onPullDownRefresh() {
  496. },
  497. /**
  498. * 页面上拉触底事件的处理函数
  499. */
  500. onReachBottom() {
  501. },
  502. /**
  503. * 用户点击右上角分享
  504. */
  505. })