index.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import { Component } from 'react'
  2. import { View, Text, Button, Image, scrollView, Video, Input } from '@tarojs/components'
  3. import * as api from '../../../service/index'
  4. import tool from '../../../common/tool'
  5. import Taro, { } from '@tarojs/taro'
  6. import './index.less'
  7. export default class collection extends Component {
  8. state = {
  9. consultImg: '',
  10. sizeIndex: 0,
  11. mass: 0,
  12. modelIndex: 0,
  13. isImage: false,
  14. isOpen: false,
  15. jumpurl: '',//广告id
  16. task_id: 0,//任务id
  17. repeat: 0,//广告次数
  18. drawTaskId: 0,//任务id
  19. is_week: 0,//是否是周卡用户
  20. models: {},//活动数据
  21. isIos: false,
  22. }
  23. componentWillMount () {
  24. let that = this
  25. Taro.getSystemInfo({
  26. success: function (res) {
  27. if (res.platform != "android") {
  28. that.setState({
  29. isIos: true
  30. })
  31. }
  32. }
  33. })
  34. this.getCoinTaskAd()
  35. this.getDraw51ImgIndex()
  36. setTimeout(() => {
  37. this.getCoinTaskAd()
  38. this.getDraw51ImgIndex()
  39. }, 1000);
  40. }
  41. componentDidShow () {
  42. //接收裁剪图片
  43. let pages = Taro.getCurrentPages();
  44. let currentPage = pages[pages.length - 1]; // 获取当前页面
  45. console.log(currentPage, 'currentPage');
  46. if (process.env.TARO_ENV == 'weapp') {
  47. if (currentPage.__data__.consultImg) { // 获取值
  48. this.setState({
  49. consultImg: currentPage.__data__.consultImg,
  50. sizeIndex: currentPage.__data__.sizeIndex,
  51. isImage: true
  52. })
  53. currentPage.__data__.consultImg = ''
  54. }
  55. } else if (process.env.TARO_ENV == 'tt') {
  56. if (currentPage.data.consultImg) { // 获取值
  57. this.setState({
  58. consultImg: currentPage.data.consultImg,
  59. sizeIndex: currentPage.data.sizeIndex,
  60. isImage: true
  61. })
  62. currentPage.data.consultImg = ''
  63. }
  64. }
  65. }
  66. getDraw51ImgIndex () {
  67. api.getDraw51ImgIndex().then(res => {
  68. if (res.code == 200) {
  69. this.setState({
  70. is_week: res.data.is_week,
  71. models: res.data,
  72. consultImg: res.data.img
  73. })
  74. }
  75. })
  76. }
  77. getCoinTaskAd () {
  78. api.getCoinTaskAd().then(res => {
  79. if (res.code == 200) {
  80. this.setState({
  81. jumpurl: res.data[0].jumpurl,//广告id
  82. task_id: res.data[0].id,//任务id
  83. repeat: res.data[0].repeat
  84. })
  85. }
  86. })
  87. }
  88. changeOpen () {
  89. this.setState({
  90. isOpen: !this.state.isOpen
  91. })
  92. }
  93. //上传图片
  94. upLode () {
  95. let that = this
  96. Taro.chooseMedia({
  97. count: 1, // 默认
  98. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  99. mediaType: ['image'],
  100. sourceType: ['album', 'camera'],
  101. success: function (res) {
  102. console.log(res, 'res');
  103. const file = res.tempFiles[0].tempFilePath
  104. if (res.tempFiles[0].size > 2000000) {
  105. Taro.showToast({ title: '图片大小不能超过2M', icon: 'none' })
  106. return
  107. }
  108. let base64Img = Taro.getFileSystemManager().readFileSync(file, 'base64')
  109. Taro.showLoading({
  110. title: '图片检测中...',
  111. })
  112. api.imageDetection({ img_data: base64Img }).then(item => {
  113. if (item.code == 200) {
  114. Taro.hideLoading()
  115. Taro.navigateTo({
  116. url: `/moduleA/pages/upload/index?image=${file}`
  117. })
  118. } else {
  119. Taro.hideLoading()
  120. Taro.showToast({ title: item.msg, icon: 'none' })
  121. }
  122. })
  123. }
  124. })
  125. }
  126. //看广告画图
  127. playVideoGeneratePictures () {
  128. if (!this.state.isImage) {
  129. Taro.showToast({ title: '请先上传图片', icon: 'none' })
  130. return
  131. }
  132. api.drawGcImg({ img_url: this.state.consultImg, style_id: this.state.models.style_id }).then(res => {
  133. if (res.code == 200) {
  134. if (this.state.is_week == 1) {
  135. Taro.navigateTo({
  136. url: `/moduleB/pages/comicActivityDetail/index?task_id=${res.data.task_id}`
  137. })
  138. } else {
  139. this.setState({
  140. drawTaskId: res.data.task_id
  141. }, () => {
  142. this.playVideo()
  143. })
  144. }
  145. } else {
  146. Taro.hideLoading()
  147. Taro.showToast({ icon: 'none', title: res.msg })
  148. }
  149. })
  150. }
  151. //观看广告
  152. playVideo () {
  153. if (process.env.TARO_ENV == 'tt') {
  154. this.playVideoTT()
  155. } else {
  156. this.playVideoWeapp()
  157. }
  158. }
  159. //抖音激励广告
  160. playVideoTT () {
  161. let that = this
  162. Taro.showLoading({
  163. title: '加载中...',
  164. })
  165. const videoAd = tt.createRewardedVideoAd({ adUnitId: this.state.jumpurl });
  166. videoAd.show()
  167. videoAd.onError((err) => {
  168. Taro.hideLoading()
  169. switch (err.errCode) {
  170. case 1004:
  171. // 无合适的广告
  172. break;
  173. default:
  174. // 更多请参考错误码文档
  175. }
  176. });
  177. try {
  178. if (videoAd.closeHandler) {
  179. videoAd.offClose(videoAd.closeHandler);
  180. console.log("videoAd.offClose卸载成功");
  181. }
  182. } catch (e) {
  183. console.log("videoAd.offClose 卸载失败");
  184. console.error(e);
  185. }
  186. videoAd.closeHandler = function (data) {
  187. Taro.hideLoading()
  188. if (data.isEnded) {
  189. Taro.navigateTo({
  190. url: `/moduleB/pages/comicActivityDetail/index?task_id=${that.state.drawTaskId}`
  191. })
  192. } else {
  193. }
  194. }
  195. // 监听视频播放完成
  196. videoAd.onClose(videoAd.closeHandler);
  197. }
  198. //激励广告
  199. playVideoWeapp () {
  200. // 在页面中定义激励视频广告
  201. let adUnitId = this.state.jumpurl
  202. // 在页面中定义激励视频广告
  203. let videoAd = null
  204. let that = this
  205. console.log(videoAd, 'videoAd');
  206. // 在页面onLoad回调事件中创建激励视频广告实例
  207. if (wx.createRewardedVideoAd) {
  208. videoAd = wx.createRewardedVideoAd({
  209. adUnitId: adUnitId
  210. })
  211. videoAd.onLoad(() => { })
  212. videoAd.onError((err) => { })
  213. try {
  214. if (videoAd.closeHandler) {
  215. videoAd.offClose(videoAd.closeHandler);
  216. console.log("videoAd.offClose卸载成功");
  217. }
  218. } catch (e) {
  219. console.log("videoAd.offClose 卸载失败");
  220. console.error(e);
  221. }
  222. videoAd.closeHandler = function (res) {
  223. // 用户点击了【关闭广告】按钮
  224. if (res && res.isEnded || res === undefined) {
  225. // 正常播放结束,可以下发游戏奖励
  226. Taro.navigateTo({
  227. url: `/moduleB/pages/comicActivityDetail/index?task_id=${that.state.drawTaskId}`
  228. })
  229. } else {
  230. //提前关闭小程序
  231. }
  232. };
  233. videoAd.onClose(videoAd.closeHandler);
  234. }
  235. // 用户触发广告后,显示激励视频广告
  236. if (videoAd) {
  237. videoAd.show().catch(() => {
  238. // 失败重试
  239. videoAd.load()
  240. .then(() => videoAd.show())
  241. .catch(err => {
  242. Taro.showToast({
  243. title: '暂无广告',
  244. icon: 'none',
  245. duration: 2000
  246. })
  247. console.log('激励视频 广告显示失败')
  248. })
  249. })
  250. }
  251. }
  252. deleteImg () {
  253. this.setState({
  254. consultImg: this.state.models.img,
  255. isImage: false,
  256. })
  257. }
  258. onShareAppMessage = (res) => {
  259. let path = `/pages/index/index?c=${Taro.getStorageSync('user_id')}&s=rightTap`
  260. if (process.env.TARO_ENV == 'tt') {
  261. path = `/moduleB/pages/comic/index?c=channel_name&s=neibuguazai`
  262. }
  263. let shareData = {
  264. title: tool.shareTitle,
  265. path: path,
  266. imageUrl: tool.shareImg
  267. }
  268. return shareData;
  269. }
  270. changeMass (e) {
  271. this.setState({
  272. mass: e
  273. })
  274. }
  275. selectModel (item, index) {
  276. this.setState({
  277. modelIndex: index,
  278. })
  279. }
  280. //充值
  281. toOrder () {
  282. if (this.state.isIos) {
  283. Taro.showToast({
  284. title: 'ios暂不支持充值',
  285. icon: 'none',
  286. duration: 2000
  287. })
  288. return
  289. }
  290. let that = this
  291. if (process.env.TARO_ENV === 'tt') {
  292. Taro.showLoading({
  293. title: '加载中...',
  294. })
  295. api.ttCreateOrder({ pay_info_id: this.state.models.shop_id }).then(res => {
  296. if (res.code == 200) {
  297. Taro.hideLoading()
  298. tt.pay({
  299. orderInfo: {
  300. order_id: res.data.order_id,
  301. order_token: res.data.order_token
  302. },
  303. service: 5,
  304. success (item) {
  305. if (item.code == 0) {
  306. Taro.showToast({
  307. title: '购买成功',
  308. icon: 'none',
  309. duration: 2000
  310. })
  311. that.getDraw51ImgIndex()
  312. setTimeout(() => {
  313. that.getDraw51ImgIndex()
  314. }, 2000);
  315. // 支付成功处理逻辑,只有res.code=0时,才表示支付成功
  316. // 但是最终状态要以商户后端结果为准
  317. }
  318. },
  319. fail (item) {
  320. // 调起收银台失败处理逻辑
  321. },
  322. });
  323. }
  324. })
  325. } else if (process.env.TARO_ENV === 'weapp') {
  326. Taro.getSystemInfo({
  327. success: function (res) {
  328. Taro.showLoading({
  329. title: '加载中...',
  330. })
  331. api.createOrder({ shop_id: that.state.models.shop_id }).then(res => {
  332. if (res.code == 200) {
  333. Taro.hideLoading()
  334. Taro.requestPayment({
  335. timeStamp: res.data.timeStamp,
  336. nonceStr: res.data.nonceStr,
  337. package: res.data.package,
  338. signType: res.data.signType,
  339. paySign: res.data.paySign,
  340. success: function (res) {
  341. // console.log(res);
  342. //添加2s延迟在刷新数据
  343. setTimeout(() => {
  344. that.getDraw51ImgIndex()
  345. Taro.showToast({
  346. title: '购买成功',
  347. icon: 'none',
  348. duration: 2000
  349. })
  350. }, 2000);
  351. },
  352. fail: function (err) {
  353. try {
  354. const res = Taro.getSystemInfoSync()
  355. console.log(res, 'iphone=====');
  356. } catch (e) {
  357. }
  358. }
  359. })
  360. }
  361. })
  362. }
  363. })
  364. }
  365. }
  366. toComic () {
  367. Taro.navigateTo({
  368. url: `/moduleB/pages/comic/index`
  369. })
  370. }
  371. render () {
  372. return (
  373. <View className='mine'>
  374. {
  375. !this.state.isImage ?
  376. <View className='before-painting'>
  377. <View className='title'>
  378. <Image className='title-img' src='http://video-img.fyshark.com/1682475695399title.png'></Image>
  379. </View>
  380. <View className='banner'>
  381. <View className='banner-content'>
  382. <Image className='content-img'
  383. mode='aspectFit'
  384. src={this.state.consultImg}
  385. ></Image>
  386. </View>
  387. <View className='step'>
  388. <Image className='step-img' src='http://video-img.fyshark.com/1683196371096123.png'>
  389. </Image>
  390. </View>
  391. </View>
  392. <View className='update' >
  393. <View className='update-button-more'
  394. onClick={e => (this.toComic())}
  395. >
  396. 更多风格
  397. </View>
  398. <View className='update-button'
  399. onClick={e => (this.upLode())}
  400. >
  401. 上传图片
  402. </View>
  403. </View>
  404. </View>
  405. :
  406. <View className='after-painting'>
  407. <View className='result-banner'>
  408. <View className='result-banner-content'>
  409. <Image className='result-content-img'
  410. mode='widthFix'
  411. src={this.state.consultImg}
  412. ></Image>
  413. {
  414. this.state.isImage &&
  415. <Image className='delete' src='http://video-img.fyshark.com/1680147210173guanbi.png' onClick={e => (this.deleteImg())}></Image>
  416. }
  417. </View>
  418. </View>
  419. <View className='after-tips'>照片已上传,还差最后一步</View>
  420. <View className='result-update' >
  421. {
  422. this.state.is_week == 0 &&
  423. <View className='result-recharge-button update-active'
  424. onClick={e => (tool.throttle(this.toOrder()), 2000)}
  425. >
  426. <View className='result-recharge-button-content'>
  427. <View className='result-recharge-button-top'>【限时抢购】无广告!</View>
  428. <View className='result-recharge-button-bottom'>9.9元→{this.state.models.money_week / 100}元/周!
  429. </View>
  430. </View>
  431. </View>
  432. }
  433. <View className='result-update-button'
  434. onClick={e => (tool.throttle(this.playVideoGeneratePictures()), 2000)}
  435. >
  436. {
  437. this.state.is_week == 0 &&
  438. <Image className='result-update-img' src='http://video-img.fyshark.com/1682414441783aghidjnl.png'></Image>
  439. }
  440. {
  441. this.state.is_week == 1 ? '开始制作' : '看广告免费制作'
  442. }
  443. </View>
  444. </View>
  445. </View>
  446. }
  447. </View>
  448. )
  449. }
  450. }