index.jsx 14 KB

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