index.jsx 13 KB

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