import { Component } from 'react' import { View, Text, Button, Image, Swiper, SwiperItem, ScrollView, Input, Textarea, Slider, Block } from '@tarojs/components' import Taro, { getCurrentInstance } from '@tarojs/taro' import tool from '../../common/tool' import { AtFloatLayout } from 'taro-ui' import "taro-ui/dist/style/components/float-layout.scss"; import * as api from '../../service/index' import './index.less' export default class Mine extends Component { $instance = getCurrentInstance() state = { models: [],//模板 imgSize: [],//图片尺寸 sampling: [],//采样方式 draw_infos: [ ],//banner samplingIndex: 0,//当前选择的采样方式 loraIndex: 0,//当前选择的风格 sizeIndex: 0,//当前选择的尺寸 isOpen: false, value: '杰作,顶级质量,女孩',//绘画描述 negativeValue: '',//不要的元素 modelIndex: 0,//当前选择的模型 sliderNum: 7,//色彩丰富度 drawNum: 1,//图片数量 mass: 0,//图片质量 tmplIds: [],//订阅列标 tags: [],//标签列表 tagsIndex: 0,//当前选择的主标签 isTags: false,//是否打开标签弹窗 consultImg: '',//参考图 lastTime: 0 } componentDidShow () { //接收裁剪图片 let pages = Taro.getCurrentPages(); let currentPage = pages[pages.length - 1]; // 获取当前页面 if (process.env.TARO_ENV == 'weapp') { if (currentPage.__data__.consultImg) { // 获取值 this.setState({ consultImg: currentPage.__data__.consultImg, sizeIndex: currentPage.__data__.sizeIndex }) currentPage.__data__.consultImg = '' } } else if (process.env.TARO_ENV == 'tt') { if (currentPage.data.consultImg) { // 获取值 this.setState({ consultImg: currentPage.data.consultImg, sizeIndex: currentPage.data.sizeIndex }) currentPage.data.consultImg = '' } } this.getDrawIndex() this.getWxTemplate() this.getTagsDev() } componentDidMount () { } getTagsDev () { api.getTagsDev().then(res => { if (res.code == 200) { let _tags = res.data _tags.forEach(item => { item.tags.forEach(e => { e['isSelect'] = false }) }) console.log(_tags, '_tags'); this.setState({ tags: _tags }) } }) } onShareAppMessage = (res) => { let path = `/pages/index/index?c=${Taro.getStorageSync('user_id')}&s=rightTap` if (process.env.TARO_ENV == 'tt') { path = `/pages/index/index?c=channel_name&s=douyinguazai` } let shareData = { title: tool.shareTitle, path: path, imageUrl: tool.shareImg } return shareData; } getMyDraw () { api.getMyDraw({ page_size: 100 }) } getDrawIndex () { api.getDrawIndex().then(res => { if (res.code == 200) { this.setState({ models: res.data.models, imgSize: res.data.img_size, sampling: res.data.Sampler, draw_infos: res.data.banner_info }, () => { let routers = this.$instance.router.params console.log(routers, 'rou'); if (routers.task_id) { Taro.showLoading({ title: '加载中', }) this.getDrawTaskStatus() } }) } }) } //获取同款信息 getDrawTaskStatus () { let routers = this.$instance.router.params api.getImgDetail({ img_id: routers.task_id }).then(res => { Taro.hideLoading() if (res.code == 200) { let model_id_index = 0 this.state.models.forEach((item, index) => { if (item.id == res.data.task_info.model_id) { model_id_index = index } }); let style_id_index = 0 this.state.models[model_id_index].lora_infos.forEach((item, index) => { if (item.id == res.data.task_info.style_id) { style_id_index = index } }) let _sizeIndex = 0 this.state.imgSize.forEach((item, index) => { if (res.data.task_info.img_size == item.name) { _sizeIndex = index } }) let _samplingIndex = 0 this.state.sampling.forEach((item, index) => { if (item == res.data.task_info.sampler) { _samplingIndex = index } }) //定时器不要轻易改,避免造成服务器攻击 if (res.code == 200) { this.setState({ value: res.data.task_info.prompt, negativeValue: res.data.task_info.negative_prompt, modelIndex: model_id_index, loraIndex: style_id_index, sizeIndex: _sizeIndex, mass: res.data.task_info.img_type, samplingIndex: _samplingIndex }) } } }) } getRadomPrompt () { api.getRadomPrompt().then(res => { if (res.code == 200) { this.setState({ value: res.data.prompt, negativeValue: res.data.negative_prompt }) document.getElementById('textarea-num').innerHTML = res.data.prompt.length + "/1000"; document.getElementById('negative_prompt').innerHTML = res.data.negative_prompt.length + "/1000"; } }) } deleteDrawText () { setTimeout(() => { this.setState({ value: '' }) document.getElementById('textarea-num').innerHTML = 0 + "/1000"; }, 200) } deleteNegativeValue () { setTimeout(() => { this.setState({ negativeValue: '' }) document.getElementById('negative_prompt').innerHTML = 0 + "/1000"; }, 200) } onInput (e) { var num = e.detail.cursor; document.getElementById('textarea-num').innerHTML = num + "/1000"; } onBlur (e) { this.setState({ value: e.detail.value }) } onInputNegative (e) { var num = e.detail.cursor; document.getElementById('negative_prompt').innerHTML = num + "/1000"; } onBlurNegative (e) { this.setState({ negativeValue: e.detail.value }) } changeOpen () { this.setState({ isOpen: !this.state.isOpen }) } selectModel (item, index) { this.setState({ modelIndex: index, loraIndex: 0 }) } subNum () { if (this.state.drawNum <= 1) return this.setState({ drawNum: this.state.drawNum - 1 }) } addNum () { if (this.state.drawNum >= 4) return this.setState({ drawNum: this.state.drawNum + 1 }) } changeMeasurement (item, index) { if (this.state.consultImg.length > 0) { Taro.showToast({ title: `您已选择图尺寸`, icon: 'error', }); return } this.setState({ sizeIndex: index }) } changeSampling (item, index) { this.setState({ samplingIndex: index }) } changeLora (item, index) { this.setState({ loraIndex: index }) } changeMass (e) { this.setState({ mass: e }) } getWxTemplate () { api.getWxTemplate().then(res => { let tmplIds = [] res.data.forEach(item => { tmplIds.push(item.template_id) }) this.setState({ tmplIds: tmplIds }) }) } //订阅 subscribe (task_id) { let that = this console.log('订阅', this.state.tmplIds); Taro.requestSubscribeMessage({ tmplIds: this.state.tmplIds, success: function (item) { console.log('订阅成功'); if (item.errMsg === 'requestSubscribeMessage:ok') { let keys = Object.keys(item); let tempids = []; keys.forEach(element => { if (item[element] == "accept") { tempids.push(element) } }); console.log(tempids, 'tempidstempids'); let pramas = { task_id: task_id, template_ids: tempids } api.subscribeTemplate(pramas) } Taro.showToast({ title: '已订阅', icon: 'none', duration: 2000 }) }, complete: function (res) { }, fail: function (res) { } }) } //生成图片 generatePictures () { let that = this Taro.showLoading({ title: '加载中', }) console.log('生成图片'); console.log(); // setTimeout(() => { if (this.state.value.length == 0) { Taro.showToast({ title: `请填写绘画描述`, icon: 'error', }); return } let pramas = { // model_name: this.state.models[this.state.modelIndex].model_name, // file_name: this.state.models[this.state.modelIndex].lora_infos.length > 0 ? this.state.models[this.state.modelIndex].lora_infos[this.state.loraIndex].file_name : '', model_id: this.state.models[this.state.modelIndex].id, style_id: this.state.models[this.state.modelIndex].lora_infos.length > 0 ? this.state.models[this.state.modelIndex].lora_infos[this.state.loraIndex].id : 0, img_size: this.state.imgSize[this.state.sizeIndex].name, img_cnt: this.state.drawNum, cfg_scale: this.state.sliderNum, sampler: this.state.sampling[this.state.samplingIndex], prompt: this.state.value, negative_prompt: this.state.negativeValue, img_type: this.state.mass, img_url: this.state.consultImg } api.drawTxt2img(pramas).then(res => { if (res.code == 200) { Taro.hideLoading() if (process.env.TARO_ENV == 'weapp') { this.subscribe(res.data.task_id) } Taro.navigateTo({ url: `/moduleA/pages/darwDetailIndex/index?task_id=${res.data.task_id}` }) } else { console.log('11111111111'); Taro.hideLoading() if (res.code == 4001) { Taro.showModal({ title: '您的余额不足', content: '是否前往获取点券?', success: function (res) { if (res.confirm) { Taro.navigateTo({ url: `/moduleA/pages/earnPoints/index` }) } else if (res.cancel) { console.log('用户点击取消') } } }) } if (res.code == 4002) { Taro.showModal({ title: '提示', content: res.msg, success: function (item) { if (item.confirm) { if (process.env.TARO_ENV != 'weapp') { that.playVideoTT(res.data) } else { that.playVideo(res.data) } } else if (item.cancel) { console.log('用户点击取消') } } }) } else { Taro.showToast({ icon: 'none', title: res.msg }) } } }) // }, 1000) } //抖音激励广告 playVideoTT (item) { let that = this let lastTime = this.state.lastTime; let now = new Date().getTime(); if (now - lastTime < 2000) return console.log(item, 'itehndodjd'); this.setState({ lastTime: now }) Taro.showLoading({ title: '加载中...' }) const videoAd = tt.createRewardedVideoAd({ adUnitId: item.jumpurl }); videoAd.show() videoAd.onError((err) => { Taro.hideLoading(); switch (err.errCode) { case 1004: // 无合适的广告 break; default: // 更多请参考错误码文档 } }); try { if (videoAd.closeHandler) { videoAd.offClose(videoAd.closeHandler); console.log("videoAd.offClose卸载成功"); } } catch (e) { console.log("videoAd.offClose 卸载失败"); console.error(e); } videoAd.closeHandler = function (data) { Taro.hideLoading(); if (data.isEnded) { api.finishTask({ task_id: item.id }).then(res => { if (res.code == 200) { Taro.showToast({ title: '领取点券成功', icon: 'none', duration: 2000 }) } }) } else { Taro.showToast({ title: '领取点券失败', icon: 'none', duration: 2000 }) } } // 监听视频播放完成 videoAd.onClose(videoAd.closeHandler); } //微信激励广告 playVideo (item) { let lastTime = this.state.lastTime; let now = new Date().getTime(); if (now - lastTime < 2000) return console.log(item, 'itehndodjd'); this.setState({ lastTime: now }) // 在页面中定义激励视频广告 let adUnitId = item.jumpurl // 在页面中定义激励视频广告 let videoAd = null let that = this console.log(videoAd, 'videoAd'); // 在页面onLoad回调事件中创建激励视频广告实例 if (wx.createRewardedVideoAd) { videoAd = wx.createRewardedVideoAd({ adUnitId: adUnitId }) videoAd.onLoad(() => { }) videoAd.onError((err) => { }) try { if (videoAd.closeHandler) { videoAd.offClose(videoAd.closeHandler); console.log("videoAd.offClose卸载成功"); } } catch (e) { console.log("videoAd.offClose 卸载失败"); console.error(e); } videoAd.closeHandler = function (res) { // 用户点击了【关闭广告】按钮 if (res && res.isEnded || res === undefined) { // 正常播放结束,可以下发游戏奖励 api.finishTask({ task_id: item.id }).then(res => { if (res.code == 200) { Taro.showToast({ title: '领取点券成功', icon: 'none', duration: 2000 }) } }) } else { //提前关闭小程序 } }; videoAd.onClose(videoAd.closeHandler); } // 用户触发广告后,显示激励视频广告 if (videoAd) { videoAd.show().catch(() => { // 失败重试 videoAd.load() .then(() => videoAd.show()) .catch(err => { Taro.showToast({ title: '暂无广告', icon: 'none', duration: 2000 }) console.log('激励视频 广告显示失败') }) }) } } toChat (e) { } //选择标签 selectedLabel (item, index) { this.setState({ tagsIndex: index, isTags: true }) } //选择子标签 selectedSubtag (item, index) { let tags = this.state.tags tags[this.state.tagsIndex].tags[index].isSelect = !tags[this.state.tagsIndex].tags[index].isSelect this.setState({ tags: tags }) } //确定标签 defineSubtag () { console.log('defineSubtag'); let nowSelectTag = [] let value = this.state.value let nowSelectTagObj = '' this.state.tags[this.state.tagsIndex].tags.forEach(item => { if (item.isSelect) { nowSelectTag.push(item.zh_name) } }) nowSelectTagObj = nowSelectTag.toString() let _value = value.length > 0 ? `${value},${nowSelectTagObj}` : nowSelectTagObj this.setState({ value: _value, isTags: !this.state.isTags }, () => { let _tags = this.state.tags _tags.forEach(item => { item.tags.forEach(e => { e['isSelect'] = false }) }) this.setState({ tags: _tags }) }) } onClose (e) { console.log('onClose'); this.setState({ isTags: false }) } //上传参考图 upLode () { let that = this Taro.chooseMedia({ count: 1, // 默认9 sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 mediaType:['image'], sourceType: ['album', 'camera'], success: function (res) { console.log(res, 'res'); const file = res.tempFiles[0].tempFilePath if (res.tempFiles[0].size > 2000000) { Taro.showToast({ title: '图片大小不能超过2M', icon: 'none' }) return } Taro.hideLoading() Taro.navigateTo({ url: `/moduleA/pages/upload/index?image=${file}` }) } }) } deleteImg () { this.setState({ consultImg: '' }) } toRealDetail (item, index) { Taro.navigateTo({ url: item.jpath }) } render () { return ( { this.state.draw_infos.length > 0 && { this.state.draw_infos.map((item, index) => ( (this.toRealDetail(item, index))}> )) } } 绘画描述 (this.getRadomPrompt())}>使用推荐词 (this.deleteDrawText())}>清空