index.jsx 11 KB

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