award.js 3.46 KB
import api from '../../api/api'
import {
	httpGet,
	httpPost
} from '../../api/fetch-api.js'
import {
	Toast,
	Dialog
} from 'vant';
import {
	checkMobile
} from "../../utils/utils";

let urls = {
	index: "/jiajiaCHApi/app/index"
};

export default {
	data() {
		return {
			key: 'value',
			form: {
				username: "",
				mobile: "",
				address: "",
			},
			userInfo: {},
			rank: 0,
		}
	},
	components: {},
	computed: {
		isAward() {
			let rank = this.rank;
			return rank > 0 && rank <= 80;
		}
	},
	methods: {
		initData() {
			let link = location.origin + location.pathname;
			link += "?f=award";
			as.setShare(link, "", "", "");
			this.toSign().then((result) => {
				this.getUserInfo();
			})
		},
		getUserInfo() {
			return new Promise((resolve, reject) => {
				as.queryFunV2('queryUserInfo', {
					openid: this.userInfo.openid || ""
				}, res => {
					if (res && res[0] && res[0][0] && res[0][0].openid) {
						// console.log("this.userInfo:",this.userInfo);
						this.userInfo.hdp_id = res[0][0].hdp_id;
					}
				})
			})
		},
		toRule() {
			this.$router.push({
				path: "/"
			})
		},
		checkSubmit() {
			return new Promise((resolve, reject) => {
				let {
					username,
					mobile,
					address
				} = this.form;
				let rank = this.rank;
				if (this.isAward) {
					Dialog.alert({
						message: `您的排行是${rank}名,非获奖用户`
					}).then(() => {
						// on close
					});
					reject();
					return;
				}
				if (!username) {
					Toast("请填写真实姓名");
					reject();
					return;
				}
				if (!mobile) {
					Toast("请填写手机号码");
					reject();
					return;
				}
				if (!checkMobile(mobile)) {
					Toast("请填写有效手机号码");
					reject();
					return;
				}
				if (!address) {
					Toast("请填写真实收件地址");
					reject();
					return;
				}
				resolve();
			})

		},
		onSubmitHandler() {
			this.checkSubmit().then((result) => {
				Dialog.confirm({
					message: '确认提交?'
				}).then(() => {
					Toast.loading({
						mask: true,
						message: '加载中...'
					});
					let data = this.form;
					let userInfo = this.userInfo;
					data.rank = this.rank;
					data.id = userInfo && userInfo.hdp_id || "";
					data.openid = userInfo && userInfo.openid || "";
					data.avatar = userInfo && userInfo.avatar || "";
					data.nickname = userInfo && userInfo.nickname || "";
					data.unionid = userInfo && userInfo.unionid || "";
					data.userid = userInfo && userInfo.userId || "";
					console.log("data:", data);
					as.saveTable('user', data, res => {
						console.log("saveTable:", res);
						Toast.clear();
						if (res && res.id) {
							if (!this.userInfo.hdp_id) {
								this.userInfo.hdp_id = res.id
							}
							Dialog.alert({
								title: '提交成功',
								message: '提交成功后,我们将尽快与您联系'
							}).then(() => {});
						} else {
							Toast("网络异常,提交失败");
						}
					})
				}).catch(() => {
					// on cancel
				});
			});
		},
		toSign() {
			return new Promise((resolve, reject) => {
				Toast.loading({
					mask: true,
					message: "请稍等..."
				});

				httpGet({
					url: urls.index
				}).then(res => {
					Toast.clear();
					this.rank = res.rank;
					// this.rank = 30;
					this.userInfo = res.dto;
					this.form.username = this.userInfo && this.userInfo.userName || "";
					this.form.mobile = this.userInfo && this.userInfo.mobile || "";
					resolve();
				});
			})
		}
	},
	mounted() {},
	created() {
		this.initData();
	}
}