add.vue 4.77 KB
<template>
	<view>
		<view class="content-top">
			<view class="content-item u-f u-f-aic p-10">
				<view class="title require u-f1">
					物料编码
				</view>
				<view class="inp u-f3">
					<input v-model="form.code" placeholder="请输入编码" />
				</view>
			</view>
			<view class="content-item u-f u-f-aic p-10">
				<view class="title require u-f1">
					物料名称
				</view>
				<view class="inp u-f3">
					<input v-model="form.name" placeholder="请输入名称" />
				</view>
			</view>
			<view class="content-item u-f u-f-aic p-10">
				<view class="title u-f1">
					物料规格
				</view>
				<view class="inp u-f3">
					<input v-model="form.spec" placeholder="请输入规格" />
				</view>
			</view>
			<view class="content-item u-f p-10" @tap="selectUnit">
				<view class="title u-f1">
					物料单位
				</view>
				<view class="inp u-f3">
					<view class="u-f u-f-aic fr">
						<text class="unit">{{abc}}</text>
						<uni-icons type="arrowright" size="18" color="gray"></uni-icons>
					</view>
					
				</view>
			</view>
			<view class="content-item u-f u-f-aic p-10">
				<view class="title u-f1">
					物料类型
				</view>
				<view class="inp u-f3">
					<picker @change="bindPickerChange" :value="typeIndex" :range-key="'name'" :range="materialTypeArr">
						<view class="uni-input">
							<text class="placeholder-color" v-if="typeIndex == -1">请选择类型</text>
							<text v-else>{{materialTypeArr[typeIndex].name}}</text>
						</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="footer u-f u-f-aic">
			<button class="btn" :loading="loading" type="warn" @tap="save" :disabled="disabled">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				materialTypeArr: [],
				typeIndex: -1,
				loading: false,
				disabled: false,
				form: {
					code: undefined,
					name: undefined,
					spec: undefined,
					unit: undefined,
					type: undefined
				},
				msg: "",
				timer: null,
				abc: ''
			}
		},
		onLoad() {
			this.getMaterialType();
			this.getMaterialCode();
            this.$http.material.listUnit().then(res => {
				console.log(res)
			});
			
		},
		onShow() {
			var pages = getCurrentPages();
			  var currPage = pages[pages.length - 1]; 
			  console.log(this.abc)
		},
		onUnload() {
			if(this.timer) {  
				clearTimeout(this.timer);  
				this.timer = null;  
			}  
		},
		methods: {
			// 获取物料编码
			getMaterialCode() {
				this.$http.material.createMaterialCode().then(res => {
					this.form.code = res.data.data
				})
			},
			// 获取物料类型
			getMaterialType() {
				this.$http.material.getMaterialType().then(res => {
					console.log(res);
					this.materialTypeArr = res.data.data
				})
			},
			// 选择单位
			selectUnit() {
				uni.navigateTo({
					url: "unit/unit"
				})
			},
			// 物料类型选择点击
			bindPickerChange: function(e) {
				this.typeIndex = e.target.value;
				this.form.type = this.materialTypeArr[this.typeIndex].code;
			},

			// 保存按钮操作
			save() {
				if (this.loading) {
					//判断是否加载中,避免重复点击请求
					return false;
				}
				if (!this.form.code.trim()) {
					this.$msg('物料编码不能为空');
					return
				}
				if (!this.form.name.trim()) {
					this.$msg('物料名称不能为空');
					return
				}
				this.loading = true;
				this.disabled = true;
				this.$http.material.addMaterial(this.form).then(res => {
					console.log(res);
					if (res.data.code == 200) {
						this.loading = false;
						this.disabled = false;
						this.$msg(res.data.msg);
						uni.$emit('updateMaterial');
						this.timer = setTimeout(() => {
							uni.navigateBack()
						}, 500)
					} else {
						this.$msg(res.data.msg);
						this.loading = false;
						this.disabled = false;
					}

				}).catch(err => {
					this.loading = false;
					this.disabled = false;
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content-top {
		background-color: $uni-bg-color;
		.content-item {
			height: 90rpx;
			line-height: 90rpx;
			border-bottom: 2rpx solid $uni-border-color;

			.title {
				font-size: $uni-font-size-lg;
			}

			.inp {
				text-align: right;
				font-size: $uni-font-size-lg;
			}
			
			.require {
				position: relative;
                padding-left: 10rpx;
				&::after {
					position: absolute;
					content: '*';
					color: red;
					font-size: 50rpx;
					top: 6rpx;
					left: -14rpx;
				}
			}
		}
	}

	.footer {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 140rpx;
		background-color: #fff;
		border-top: 2rpx solid #E1F3D8;

		.btn {
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			color: #fff;
			text-align: center;
			font-size: $uni-font-size-lg;
			border-radius: 50rpx;
			margin: 0 30rpx;
			background-color: $uni-color-primary;

		}
	}
</style>