// 根目录的static下创建js文件夹并创建mixin.js文件  
export const mixin = {
	data() {
		return {

		}
	},
	methods: {

		//订单操作按钮
		sureOrd(item, val) {
			// val  0支付保证金  2 取消订单
			this.order_news = item;
			// order_status 订单状态: 0=取消 1支付 2 收货 3退货
			if (val == '0') {
				this.ordHandle('orders/cancel_order', item);
			} else if (val == 1) {
				this.pay('orders/order_pay', item);
			} else if (val == 2) {
				this.ordHandle('orders/confirm', item);
			} else if (val == 3) { 
				uni.navigateTo({ 
					url:'/pagesA/mall/ordReturn?id='+item.id
				})
			} else if (val == 4) {
				uni.navigateTo({
					url:'/pagesA/mall/logisticsInfo?exp_id='+item.exp_list.exp_id+'&exp_no='+item.exp_list.exp_no 
				})
			}  
		},
		pay(url, item) {
			this.$api
				.request(url, {
					id: item.id,
					type: Number(this.tabcur + 1),
				})
				.then(res => {
					if (res.code && res.code != 200) {
						// this.$refs.confirmBtn.stop();
						this.$api.toast(res.msg || '获取支付信息失败');
						return;
					}
					const orderInfo = res;
					const {
						timeStamp,
						nonceStr,
						paySign
					} = orderInfo;
					const payParams = {
						provider: 'wxpay',
						orderInfo: res,
						timeStamp,
						nonceStr,
						package: orderInfo.package,
						signType: 'MD5',
						paySign,
						success: e => {
							this.$api.toast('支付成功');
							setTimeout(res => { 
								this.page = 1;
								this.orderList = [];
								this.loadData();
							}, 800)
						},
						fail: err => {
							if (err.errMsg.indexOf('取消') > -1 || err.errMsg.indexOf('cancel') > 1 || err
								.errMsg.indexOf('-2') > -1) {
								this.$api.toast('取消支付');
							} else {
								this.$api.toast('支付遇到错误，请稍候重试');
								console.log(err);
							}
						}
					};
					uni.requestPayment(payParams);
				})
		},
		// 处理订单
		ordHandle(url, item) {
			this.$api
				.request(url, {
					id: item.id,
					type: Number(this.tabcur + 1),
				})
				.then(data => {
					if (data.code == 200) {
						this.page = 1;
						this.orderList = [];
						this.loadData();
						if (item.status == 5) {
							uni.redirectTo({
								url: '/pages/moneyBox/payOk?type=1'
							})
						} else if (item.order_status == 6) {
							setTimeout(res => {
								uni.redirectTo({
									url: '/pages/moneyBox/payOk?type=1'
								})
							}, 1000);
						}
					} else {
						this.$api.toast(data.msg);
					}
				})
		},
	},
}
