uniapp-草稿与发布 逻辑封装

91 阅读6分钟

未封装版本


  /**
			 * 存草稿箱
			 * **/
			saveBumitButton() {
				let that = this
				/**
				 * 
				 * 图文的存草稿箱
				 * **/
				if (this.paramsItem.articleType == 0) {

					// 存草稿已发布
					if (this.paramsItem.deleteState == 1) {
						// 过滤出有选中的数据 备份
						let checkedData = this.labelList.filter(item => item.checked)

						let labels = checkedData.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						if (that.labelList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							uni.showLoading({
								title: '存储中'
							})
							let resources = {
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: uni.getStorageSync('richText'), // 富文本
								articleTitle: that.paramsItem.textModel, // 文章标题 
								isEmployeeLook: that.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: that.paramsItem.articleType, // 是视頻还是文本
								deployState: 0, //判断是发布还是草稿
								id:that.paramsItem.id, // 修改文章
							}
							apijournal.RequeryjournalArticlecreate(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: res.data.errmsg
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})

								}
							})
						}
					} else {
						// 存草稿未发布
						let labels = that.labelDataList.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						if (that.labelDataList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							uni.showLoading({
								title: '存储中'
							})
							let resources = {
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: uni.getStorageSync('richText'), // 富文本
								articleTitle: that.paramsItem.textModel, // 文章标题 
								isEmployeeLook: that.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: that.paramsItem.articleType, // 是视頻还是文本
								deployState: 0, //判断是发布还是草稿
							}
							apijournal.RequeryjournalArticlecreate(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: res.data.errmsg
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})

								}
							})
						}
					}

					// 存草稿已发布


				} else if (this.paramsItem.articleType == 1) {
					/**
					 * 判断上传视频
					 * **/
					let that = this
					/***
					 存草稿已发布的视频逻辑处理
					*/
					if (this.paramsItem.deleteState == 1) {
						// 过滤出有选中的数据 备份
						let checkedData = this.labelList.filter(item => item.checked)

						let labels = checkedData.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						if (that.labelList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							uni.showLoading({
								title: '存储中'
							})
							let resources = {
								articleMedium: that.paramsItem.articleMedium, // 上传视频地址
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: that.paramsItem.articleContent, //视频简介
								articleTitle: that.paramsItem.articleTitle, // 视频标题 
								videoDuration: that.paramsItem.timeval, // 视频时长
								isEmployeeLook: this.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: this.paramsItem.articleType, // 是视頻还是文本
								deployState: 0, //判断是发布还是草稿
								id:that.paramsItem.id, // 修改文章
							}
							apijournal.RequerycreateVideo(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: res.data.data
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})
								}
							})
						}
					} else {
						/**
						 * 	// 存草稿未发布的视频逻辑
						 *  拆分合并
						 * **/
						let labels = this.labelDataList.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						if (that.labelDataList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							uni.showLoading({
								title: '存储中'
							})
							let resources = {
								articleMedium: that.paramsItem.articleMedium, // 上传视频地址
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: that.paramsItem.articleContent, //视频简介
								articleTitle: that.paramsItem.articleTitle, // 视频标题 
								videoDuration: that.paramsItem.timeval, // 视频时长
								isEmployeeLook: this.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: this.paramsItem.articleType, // 是视頻还是文本
								deployState: 0, //判断是发布还是草稿
							}
							apijournal.RequerycreateVideo(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: res.data.data
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})
								}
							})
						}
					}

				} else {
					return
				}
			},
                        
                        
//  发布代码
 // 发布
			submitImg() {
				let that = this
				if (this.paramsItem.articleType == 0) {
					let that = this

					// 已发布功能-图文
					if (this.paramsItem.deleteState) {
						// 过滤出有选中的数据 备份
						let checkedData = this.labelList.filter(item => item.checked)

						let labels = checkedData.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						if (that.labelList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							let resources = {
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: uni.getStorageSync('richText'), // 富文本
								articleTitle: that.paramsItem.textModel, // 文章标题 
								isEmployeeLook: that.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: that.paramsItem.articleType, // 是视頻还是文本
								deployState: 1, //判断是发布还是草稿
								id:that.paramsItem.id // 数据id
							}
							apijournal.RequeryjournalArticlecreate(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.showToast({
										icon: 'none',
										title: res.data.errmsg
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})

								}
							})
						}
					} else {
						let labels = this.labelDataList.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						// 未发布图文功能
						if (that.labelDataList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							uni.showLoading({
								title:'发布中'
							})
							let resources = {
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: uni.getStorageSync('richText'), // 富文本
								articleTitle: that.paramsItem.textModel, // 文章标题 
								isEmployeeLook: that.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: that.paramsItem.articleType, // 是视頻还是文本
								deployState: 1, //判断是发布还是草稿
							}
							apijournal.RequeryjournalArticlecreate(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: res.data.errmsg
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})
								}
							})
						}
					}


				} else if (this.paramsItem.articleType == 1) {
					/**
					 * 判断上传视频
					 * **/
					let that = this
					// 已发布视频
					if (this.paramsItem.deleteState == 1) {
						let checkedData = this.labelList.filter(item => item.checked)
						let labels = checkedData.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						if (that.labelList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							uni.showLoading({
								title:'发布中'
							})
							let resources = {
								articleMedium: that.paramsItem.articleMedium, // 上传视频地址
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: that.paramsItem.articleContent, //视频简介
								articleTitle: that.paramsItem.articleTitle, // 视频标题 
								videoDuration: that.paramsItem.timeval, // 视频时长
								isEmployeeLook: this.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: this.paramsItem.articleType, // 是视頻还是文本
								deployState: 1, //判断是发布还是草稿
								id:that.paramsItem.id
							}
							apijournal.RequerycreateVideo(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: res.data.data
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})
								}
							})
						}
					} else {
						// 未发布视频
						let labels = this.labelDataList.map((ele) => {
							return ({
								id: ele.value,
								name: ele.name
							})
						})
						if (that.labelDataList.length == 0) {
							uni.showToast({
								title: '请上传必填信息',
								duration: 2000
							})
						} else {
							uni.showLoading({
								title:'发布中'
							})
							let resources = {
								articleMedium: that.paramsItem.articleMedium, // 上传视频地址
								articleCoverImg: that.preImgUrl, // 上传的封面地址
								articleContent: that.paramsItem.articleContent, //视频简介
								articleTitle: that.paramsItem.articleTitle, // 视频标题 
								videoDuration: that.paramsItem.timeval, // 视频时长
								isEmployeeLook: this.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
								labels: labels, // 词条   this.labelDataList
								articleType: this.paramsItem.articleType, // 是视頻还是文本
								deployState: 1, //判断是发布还是草稿
							}
							apijournal.RequerycreateVideo(resources).then((res) => {
								if (res.data.errno == 200) {
									uni.hideLoading()
									uni.showToast({
										icon: 'none',
										title: res.data.data
									})
									uni.removeStorageSync('preImgUrl')
									uni.switchTab({
										url: "/pages/DemoMins/release/release"
									})
								}
							})
						}
					}

				}
 
 
 

版本二

<!-- 底部按钮区-->
		<view class="btn_item">
			<!-- 草稿箱 -->
			<view class="brtn_item_btn" @click="saveBumitButton(0)">
				<span>存草稿</span>
			</view>
			<!--  发布 -->
			<view class="brtn_item_item_btn">
				<view @click="saveBumitsubmit(1)">
					<span>发布</span>
				</view>
			</view>
		</view>


/**
			 * 草稿箱
			 * @param id 传入的id
			 * @param ite 是存草稿(0),还是发布(1)
			 * @param labels 是词条
			 * @param articleType 是图片文(0),还是视频(1)
			 */
			/**
			 * 发布
			 * @param id 传入的id
			 * @param ite 是存草稿(0),还是发布(1)
			 * @param labels 是词条
			 * @param articleType 是图片文(0),还是视频(1)
			 */
			ImageTextDraftsHandle(id, ite, labels, articleType) {
				let that = this
				console.log("传递过来的参数====>", id, ite, labels, articleType)
				uni.showLoading({
					title: '存储中'
				})
				let resources = {
					articleMedium: that.paramsItem.articleMedium ? that.paramsItem.articleMedium : null, // 上传视频地址
					articleCoverImg: that.preImgUrl, // 上传的封面地址
					articleContent: that.paramsItem.articleContent ? that.paramsItem.articleContent : uni
						.getStorageSync('richText'), //视频简介&& 富文本
					articleTitle: that.paramsItem.articleTitle ? that.paramsItem.articleTitle : that.paramsItem
						.textModel, // 视频标题 && 文章标题
					isEmployeeLook: that.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
					videoDuration: that.paramsItem.timeval ? that.paramsItem.timeval : null, // 视频时长
					labels: labels, // 词条   this.labelDataList
					articleType: articleType, // 是视頻还是图文
					deployState: ite, //判断是发布还是草稿
					id: id, // 修改文章
				}
				// 图文
				if (articleType == 0) {
					apijournal.RequeryjournalArticlecreate(resources).then((res) => {
						if (res.data.errno == 200) {
							uni.hideLoading()
							uni.showToast({
								icon: 'none',
								title: res.data.errmsg
							})
							uni.removeStorageSync('preImgUrl')
							uni.switchTab({
								url: "/pages/DemoMins/release/release"
							})

						}
					})
					// 视频
				} else {
					apijournal.RequerycreateVideo(resources).then((res) => {
						if (res.data.errno == 200) {
							uni.hideLoading()
							uni.showToast({
								icon: 'none',
								title: res.data.data
							})
							uni.removeStorageSync('preImgUrl')
							uni.switchTab({
								url: "/pages/DemoMins/release/release"
							})
						}
					})
				}
			},
			postContentHandle(id, ite, labels, articleType) {
				console.log('发布接收的数据是什么参数===>', id, ite, labels, articleType)
				let that = this
				uni.showLoading({
					title: '存储中'
				})
				let resources = {
					articleMedium: that.paramsItem.articleMedium ? that.paramsItem.articleMedium : null, // 上传视频地址
					articleCoverImg: that.preImgUrl, // 上传的封面地址
					articleContent: that.paramsItem.articleContent ? that.paramsItem.articleContent : uni
						.getStorageSync('richText'), //视频简介&& 富文本
					articleTitle: that.paramsItem.articleTitle ? that.paramsItem.articleTitle : that.paramsItem
						.textModel, // 视频标题 && 文章标题
					videoDuration: that.paramsItem.timeval ? that.paramsItem.timeval : null, // 视频时长
					isEmployeeLook: this.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
					labels: labels, // 词条   this.labelDataList
					articleType: articleType, // 是视頻还是文本
					deployState: ite, //判断是发布还是草稿
					id: id, // 修改文章
				}
				// 图文
				if (articleType == 0) {
					apijournal.RequeryjournalArticlecreate(resources).then((res) => {
						if (res.data.errno == 200) {
							uni.hideLoading()
							uni.showToast({
								icon: 'none',
								title: res.data.errmsg
							})
							uni.removeStorageSync('preImgUrl')
							uni.switchTab({
								url: "/pages/DemoMins/release/release"
							})

						}
					})
					// 视频
				} else {
					apijournal.RequerycreateVideo(resources).then((res) => {
						if (res.data.errno == 200) {
							uni.hideLoading()
							uni.showToast({
								icon: 'none',
								title: res.data.data
							})
							uni.removeStorageSync('preImgUrl')
							uni.switchTab({
								url: "/pages/DemoMins/release/release"
							})
						}
					})
				}

			},
                        
                        
                        
                        
  // 调用
  /**
			 * 存草稿箱
			 * **/
			saveBumitButton(ite) {
				console.log("当前值是发布还是存草稿",ite)
				let that = this
				/**
				 * 
				 * 图文的存草稿箱
				 * **/

				let labels = that.labelDataList.map((ele) => {
					return ({
						id: ele.value,
						name: ele.name
					})
				})
				if (labels.length == 0) {
					uni.showToast({
						title: '请上传必填信息',
						duration: 2000
					})
				} else {
					let id = that.paramsItem.id // 是否为编辑还是新增
					let articleType = that.paramsItem.articleType // 是否为图文还是视频
					that.ImageTextDraftsHandle(id, ite, labels, articleType)
				}

			},
                        
       // 发布                 
   saveBumitsubmi(ite) {
				console.log("当前值是发布还是存草稿",ite)
				let that = this
				/**
				 * 
				 * 图文的存草稿箱
				 * **/

				let labels = that.labelDataList.map((ele) => {
					return ({
						id: ele.value,
						name: ele.name
					})
				})
				if (labels.length == 0) {
					uni.showToast({
						title: '请上传必填信息',
						duration: 2000
					})
				} else {
					let id = that.paramsItem.id // 是否为编辑还是新增
					let articleType = that.paramsItem.articleType // 是否为图文还是视频
					that.ImageTextDraftsHandle(id, ite, labels, articleType)
				}

			},                     

版本三


	<!-- 底部按钮区-->
		<view class="btn_item">
			<!-- 草稿箱 -->
			<view class="brtn_item_btn" @click="saveBumitButton(0)">
				<span>存草稿</span>
			</view>
			<!--  发布 -->
			<view class="brtn_item_item_btn">
				<view @click="saveBumitButton(1)">
					<span>发布</span>
				</view>
			</view>
		</view>
                
                
                
/**
			 * 存草稿箱&& 发布
			 * **/
			saveBumitButton(ite) {
				let that = this
				/**
				 * 
				 * 图文的存草稿箱
				 * **/

				let labels = that.labelDataList.map((ele) => {
					return ({
						id: ele.value,
						name: ele.name
					})
				})
				if (labels.length == 0) {
					uni.showToast({
						title: '请上传必填信息',
						duration: 2000
					})
				} else {
					let id = that.paramsItem.id // 是否为编辑还是新增
					let articleType = that.paramsItem.articleType // 是否为图文还是视频
					that.ImageTextDraftsHandle(id, ite, labels, articleType)
				}

			},     
     
     
     
     
     
   	/**
			 * 草稿箱
			 * @param id 传入的id
			 * @param ite 是存草稿(0),还是发布(1)
			 * @param labels 是词条
			 * @param articleType 是图片文(0),还是视频(1)
			 */
			/**
			 * 发布
			 * @param id 传入的id
			 * @param ite 是存草稿(0),还是发布(1)
			 * @param labels 是词条
			 * @param articleType 是图片文(0),还是视频(1)
			 */
			ImageTextDraftsHandle(id, ite, labels, articleType) {
				let that = this
				console.log("传递过来的参数====>", id, ite, labels, articleType)
				uni.showLoading({
					title: '存储中'
				})
				let resources = {
					articleMedium: that.paramsItem.articleMedium ? that.paramsItem.articleMedium : null, // 上传视频地址
					articleCoverImg: that.preImgUrl, // 上传的封面地址
					articleContent: that.paramsItem.articleContent ? that.paramsItem.articleContent : uni
						.getStorageSync('richText'), //视频简介&& 富文本
					articleTitle: that.paramsItem.articleTitle ? that.paramsItem.articleTitle : that.paramsItem
						.textModel, // 视频标题 && 文章标题
					isEmployeeLook: that.isEmployeeLook === true ? 1 : 0, // 内部员工是否可见 ,0,1
					videoDuration: that.paramsItem.timeval ? that.paramsItem.timeval : null, // 视频时长
					labels: labels, // 词条   this.labelDataList
					articleType: articleType, // 是视頻还是图文
					deployState: ite, //判断是发布还是草稿
					id: id, // 修改文章
				}
				// 图文
				if (articleType == 0) {
					apijournal.RequeryjournalArticlecreate(resources).then((res) => {
						if (res.data.errno == 200) {
							uni.hideLoading()
							uni.showToast({
								icon: 'none',
								title: res.data.errmsg
							})
							uni.removeStorageSync('preImgUrl')
							uni.switchTab({
								url: "/pages/DemoMins/release/release"
							})

						}
					})
					// 视频
				} else {
					apijournal.RequerycreateVideo(resources).then((res) => {
						if (res.data.errno == 200) {
							uni.hideLoading()
							uni.showToast({
								icon: 'none',
								title: res.data.data
							})
							uni.removeStorageSync('preImgUrl')
							uni.switchTab({
								url: "/pages/DemoMins/release/release"
							})
						}
					})
				}
			},