index.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import { Component } from 'react'
  2. import { View, Text, Button, Image, ScrollView, Input } from '@tarojs/components'
  3. import * as api from '../../../service/index'
  4. import tool from '../../../common/tool'
  5. import Taro, { getCurrentInstance } from '@tarojs/taro'
  6. import './index.less'
  7. export default class collection extends Component {
  8. $instance = getCurrentInstance()
  9. state = {
  10. jumpurl: '',//广告id
  11. task_id: 0,//任务id
  12. repeat: 0,//广告次数
  13. current: 0,
  14. interval: null,
  15. draw_status: 0,
  16. draw_infos: [],
  17. que_cnt: 0,
  18. default_img: '',
  19. wallet_balance: 0,//用户当前余额
  20. }
  21. componentWillMount () {
  22. }
  23. componentDidShow () {
  24. let routers = this.$instance.router.params
  25. this.getCoinTaskAd()
  26. api.unlockGcImg({ task_id: routers.task_id }).then(res => {
  27. console.log('解锁');
  28. if (res.code == 200) {
  29. this.getDrawTaskStatus(routers.task_id)
  30. }
  31. })
  32. }
  33. //页面销毁之前调用
  34. componentWillUnmount () {
  35. //避免服务器攻击
  36. clearInterval(this.state.interval)
  37. }
  38. getCoinTaskAd () {
  39. api.getCoinTaskAd().then(res => {
  40. if (res.code == 200) {
  41. this.setState({
  42. jumpurl: res.data[0].jumpurl,//广告id
  43. task_id: res.data[0].id,//任务id
  44. repeat: res.data[0].repeat
  45. })
  46. }
  47. })
  48. }
  49. //解锁成功
  50. unlockGcImg (item, index) {
  51. console.log('解锁002');
  52. api.unlockGcImg({ task_id: item.task_id }).then(res => {
  53. if (res.code == 200) {
  54. this.setState({
  55. current: index
  56. })
  57. this.getDrawTaskStatus(item.task_id)
  58. }
  59. })
  60. }
  61. selectedLabel (item, index) {
  62. let that = this
  63. console.log(item, 'itemitem');
  64. if (item.unlock_status == 0) {
  65. Taro.showModal({
  66. title: '提示',
  67. content: '是否观看广告解锁?',
  68. success: function (res) {
  69. if (res.confirm) {
  70. if (item.status == 4) {
  71. that.generatePictures(item, index)
  72. }
  73. that.playVideo(item, index)
  74. } else if (res.cancel) {
  75. console.log('用户点击取消')
  76. }
  77. }
  78. })
  79. } else {
  80. this.setState({
  81. current: index
  82. })
  83. }
  84. }
  85. //观看广告
  86. playVideo (item, index) {
  87. if (process.env.TARO_ENV == 'tt') {
  88. this.playVideoTT(item, index)
  89. } else {
  90. this.playVideoWeapp(item, index)
  91. }
  92. }
  93. //抖音激励广告
  94. playVideoTT (item, index) {
  95. let that = this
  96. Taro.showLoading({
  97. title: '加载中...',
  98. })
  99. const videoAd = tt.createRewardedVideoAd({ adUnitId: this.state.jumpurl });
  100. videoAd.show()
  101. videoAd.onError((err) => {
  102. Taro.hideLoading()
  103. switch (err.errCode) {
  104. case 1004:
  105. // 无合适的广告
  106. break;
  107. default:
  108. // 更多请参考错误码文档
  109. }
  110. });
  111. try {
  112. if (videoAd.closeHandler) {
  113. videoAd.offClose(videoAd.closeHandler);
  114. console.log("videoAd.offClose卸载成功");
  115. }
  116. } catch (e) {
  117. console.log("videoAd.offClose 卸载失败");
  118. console.error(e);
  119. }
  120. videoAd.closeHandler = function (data) {
  121. Taro.hideLoading()
  122. if (data.isEnded) {
  123. Taro.showToast({
  124. title: '解锁成功',
  125. icon: 'none',
  126. duration: 2000
  127. })
  128. that.unlockGcImg(item, index)
  129. } else {
  130. that.getDrawTaskStatus(item.task_id)
  131. Taro.showToast({
  132. title: '解锁失败',
  133. icon: 'none',
  134. duration: 2000
  135. })
  136. }
  137. }
  138. // 监听视频播放完成
  139. videoAd.onClose(videoAd.closeHandler);
  140. }
  141. //激励广告
  142. playVideoWeapp (item, index) {
  143. console.log(item, 'guanggao');
  144. // 在页面中定义激励视频广告
  145. let adUnitId = this.state.jumpurl
  146. // 在页面中定义激励视频广告
  147. let videoAd = null
  148. let that = this
  149. // 在页面onLoad回调事件中创建激励视频广告实例
  150. if (wx.createRewardedVideoAd) {
  151. videoAd = wx.createRewardedVideoAd({
  152. adUnitId: adUnitId
  153. })
  154. videoAd.onLoad(() => {
  155. })
  156. videoAd.onError((err) => { })
  157. try {
  158. if (videoAd.closeHandler) {
  159. videoAd.offClose(videoAd.closeHandler);
  160. console.log("videoAd.offClose卸载成功");
  161. }
  162. } catch (e) {
  163. console.log("videoAd.offClose 卸载失败");
  164. console.error(e);
  165. }
  166. videoAd.closeHandler = function (res) {
  167. Taro.hideLoading()
  168. // 用户点击了【关闭广告】按钮
  169. if (res && res.isEnded || res === undefined) {
  170. // 正常播放结束,可以下发游戏奖励
  171. Taro.showToast({
  172. title: '解锁成功',
  173. icon: 'none',
  174. duration: 2000
  175. })
  176. console.log(item, '关闭');
  177. that.unlockGcImg(item, index)
  178. } else {
  179. console.log(item, '关闭1');
  180. //提前关闭小程序
  181. that.getDrawTaskStatus(item.task_id)
  182. Taro.showToast({
  183. title: '解锁失败',
  184. icon: 'none',
  185. duration: 2000
  186. })
  187. }
  188. };
  189. videoAd.onClose(videoAd.closeHandler);
  190. }
  191. // 用户触发广告后,显示激励视频广告
  192. if (videoAd) {
  193. videoAd.show().catch(() => {
  194. // 失败重试
  195. videoAd.load()
  196. .then(() => videoAd.show())
  197. .catch(err => {
  198. Taro.showToast({
  199. title: '暂无广告',
  200. icon: 'none',
  201. duration: 2000
  202. })
  203. console.log('激励视频 广告显示失败')
  204. })
  205. })
  206. }
  207. }
  208. //画画
  209. generatePictures (item, index) {
  210. console.log(item, '画画');
  211. api.drawGcImgTask({ task_id: item.task_id }).then(res => {
  212. if (res.code == 200) {
  213. this.getDrawTaskStatus(item.task_id)
  214. }
  215. })
  216. }
  217. getDrawTaskStatus (task_id) {
  218. api.getDrawTaskStatusGc({ task_id: task_id }).then(res => {
  219. //定时器不要轻易改,避免造成服务器攻击
  220. if (res.code == 200) {
  221. let draw_infos = res.data.draw_infos
  222. // if (res.data.draw_status != 2) {
  223. // let params = {}
  224. // params['img'] = res.data.default_img
  225. // params['id'] = -1
  226. // draw_infos.push(params)
  227. // }
  228. console.log(draw_infos, 'draw_infos');
  229. if (res.data.draw_status != 2 && res.data.que_cnt > 1) {
  230. this.state.interval = setInterval(() => {
  231. clearInterval(this.state.interval)
  232. this.getDrawTaskStatus(task_id)
  233. }, 1000 * 10)
  234. } else if (res.data.draw_status != 2 && res.data.que_cnt <= 1) {
  235. clearInterval(this.state.interval)
  236. this.state.interval = setInterval(() => {
  237. this.getDrawTaskStatus(task_id)
  238. }, 1000 * 5)
  239. } else {
  240. clearInterval(this.state.interval)
  241. }
  242. this.setState({
  243. draw_status: res.data.draw_status,
  244. que_cnt: res.data.que_cnt,
  245. draw_infos: draw_infos,
  246. wallet_balance: res.data.wallet_balance
  247. })
  248. }
  249. })
  250. }
  251. download () {
  252. this.downImg()
  253. }
  254. // 鉴权操作 判断是否有保存到相册的权限
  255. // 有就直接下载 没有就弹窗提示给权限
  256. downImg () {
  257. Taro.getSetting({
  258. success: res => {
  259. if (!res.authSetting['scope.writePhotosAlbum']) {
  260. Taro.authorize({
  261. scope: 'scope.writePhotosAlbum',
  262. success: () => {
  263. this.doSaveImg()
  264. },
  265. fail: () => {
  266. this.openConfirm()
  267. }
  268. })
  269. } else {
  270. this.doSaveImg()
  271. }
  272. }
  273. })
  274. }
  275. // 生成临时路径 保存图片到手机
  276. doSaveImg () {
  277. Taro.downloadFile({
  278. url: this.state.draw_infos[this.state.current].img,
  279. success: res => {
  280. Taro.saveImageToPhotosAlbum({
  281. filePath: res.tempFilePath,
  282. success: () => {
  283. Taro.showToast({ title: '已保存到相册', icon: 'success' })
  284. },
  285. fail: () => {
  286. Taro.showToast({ title: '保存失败', icon: 'none' })
  287. }
  288. })
  289. }
  290. })
  291. }
  292. // 权限弹窗
  293. openConfirm () {
  294. Taro.showModal({
  295. content: '检测到您没有打开小程序相册权限,是否取设置打开?',
  296. showCancel: true,
  297. success: res => {
  298. if (res.confirm) {
  299. // 打开权限
  300. Taro.openSetting({
  301. success: res => {
  302. this.doSaveImg()
  303. }
  304. })
  305. }
  306. }
  307. })
  308. }
  309. toIndex () {
  310. Taro.reLaunch({
  311. url: `/pages/index/index`
  312. })
  313. }
  314. previewImage () {
  315. Taro.previewImage({
  316. current: this.state.draw_infos[this.state.current].img, // 当前显示图片的http链接
  317. urls: [`${this.state.draw_infos[this.state.current].img}`] // 需要预览的图片http链接列表
  318. })
  319. }
  320. render () {
  321. return (
  322. <View className='mine'>
  323. {
  324. this.state.draw_infos.length > 0 &&
  325. <View className='banner'>
  326. <View className='banner-content'>
  327. <Image className='content-img'
  328. mode='aspectFit'
  329. onClick={e => (this.previewImage())}
  330. src={this.state.draw_infos[this.state.current].img}
  331. ></Image>
  332. {/* {
  333. this.state.draw_infos[this.state.current].status == 0 &&
  334. this.state.draw_infos[this.state.current].status == 1 &&
  335. <View className='content-wait'>
  336. <View className='wait-date'>预计等待{Math.ceil((this.state.que_cnt - 1) * 0.2 + 1)}分钟...</View>
  337. <View className='wait-middle'>{this.state.draw_status == 0 ? '等待中' : this.state.draw_status == 1 ? '正在绘画...' : '已完成'}</View>
  338. <View className='wait-bottom'>
  339. <View className='wait-num'>当前点券余额:{this.state.wallet_balance}</View>
  340. <View className='wait-get-num' onClick={e => (tool.throttle(this.playVideo()), 2000)}>点击观看广告领取更多点券</View>
  341. </View>
  342. </View>
  343. } */}
  344. </View>
  345. </View>
  346. }
  347. <ScrollView
  348. className='recharge'
  349. scrollX
  350. >
  351. {
  352. this.state.draw_infos.map((item, index) => (
  353. <View className='recharge-info'
  354. key={index}
  355. onClick={e => (this.selectedLabel(item, index))}>
  356. <Image className='recharge-image' mode='aspectFit' src={item.img}></Image>
  357. {
  358. item.unlock_status == 0 &&
  359. <View className='lock'>
  360. <View className='lock-content'>
  361. <Image className='lock-img' src='http://video-img.fyshark.com/1681733630830%E8%A7%A3%E9%94%81%402x.png'></Image>
  362. <View className='lock-title'>解锁</View>
  363. </View>
  364. </View>
  365. }
  366. </View>
  367. ))
  368. }
  369. </ScrollView>
  370. <View className='control'>
  371. <View className='download' onClick={e => (this.download())}>保存图片</View>
  372. <View className='toIndex' onClick={e => (this.toIndex())}>前往探索更多玩法</View>
  373. </View>
  374. </View>
  375. )
  376. }
  377. }