text: 'use strict';
  exports.main = async (event, context) => {
  	//event为客户端上传的参数
  	console.log('event : ', event)
  	
  	//返回数据给客户端
  	return event
  };
  'use strict';
  const uniPay = require('uni-pay')
  const db = uniCloud.database();
  const dbCmd = db.command;
  exports.main = async (event, context) => {
  	let uniPayInstance
  	let provider
  	switch (event.path.substring(1)) {
  		case 'wxpay_mp-weixin':
  			provider = 'weixin'
  			uniPayInstance = uniPay.initWeixin({
  				appId: 'wx12476f1a32ccc485',
  				secret: '8782a289edab9ec3e7d18d127b3d698f',
  				mchId: '1579802651',
  				key: 'dgJwmszz795898761234300112356214'
  			})
  			break;
  		case 'wxpay_app-plus':
  			provider = 'weixin'
  			uniPayInstance = uniPay.initWeixin({
  					appId: 'wxbf7866ebafe55458',
  					mchId: '1579802651',
  					key: 'dgJwmszz795898761234300112356214'
  				})
  			break;
  		default:
  			console.log('---------参数错误-------')
  			return {
  				code: -1,
  					msg: '参数错误'
  			}
  	}
  	let verifyResult = await uniPayInstance.verifyPaymentNotify(event)
  	if (!verifyResult) {
  		console.log('---------!verifyResult-------')
  		return {}
  	}
  	let {
  		outTradeNo,
  		totalFee,
  		transactionId,
  		resultCode
  	} = verifyResult
  
  	const orderList = await db.collection('jwvdp-order').where({
  		outTradeNo
  	}).get()
  
  	if (orderList.data.length < 0) {
  		console.log('---------orderList.data.length < 0-------')
  		return {
  			success: false,
  			msg: "在系统中找不到充值订单：" + outTradeNo
  		}
  	}
  
  	const orderDetail = orderList.data[0]
  	console.log(totalFee, orderDetail.totalFee, resultCode, resultCode);
  	if (totalFee == orderDetail.totalFee && (resultCode === 'SUCCESS' || resultCode === 'FINISHED')) {
  		console.log('---------updatedb-------')
  		const log_id = orderDetail._id,
  			user_id = orderDetail.uid;
  		console.log(log_id, user_id);
  		try {
  			const result = await db.runTransaction(async transaction => {
  				const logItem = await transaction.collection('jwvdp-order').doc(log_id).get(); //得到标签购买记录
  				if (logItem.data) {
  					try {
  						// var timezone = 8; //目标时区时间，东八区
  						// var offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差，单位为分钟
  						// var nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜（GMT 时间）之间的毫秒数
  						// var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000).getTime();
  						const updateLogStatus = await transaction.collection('jwvdp-order').doc(log_id).update({
  							status: 2,
  							transactionId,
  							investTime: new Date().getTime(),
  							remark: "微信支付",
  							transactionId
  						}) //更新充值记录状态并且写入微信充值订单号
  				
  					} catch (e) {
  						console.log(e);
  						// 会作为 runTransaction reject 的结果出去
  						await transaction.rollback(-100)
  					}
  				} else {
  					console.log(logItem.data, userItem.data);
  					await transaction.rollback(-100)
  				}
  			})
  				
  			if (provider === 'weixin') {
  				// 微信处理成功之后 
  				return {
  					statusCode: 200,
  					headers: {
  						'content-type': 'text/xml;charset=utf-8'
  					},
  					body: `<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>`
  				}
  			}
  		} catch (e) {
  			console.error(`transaction error`, e)
  
  			return {
  				success: false,
  				error: e
  			}
  		}
  	}
  	console.log("verifyResult: " + JSON.stringify(verifyResult));
  	// 注意如果处理成功需要严格按照下面的格式进行返回，否则厂商会持续通知
  	if (provider === 'weixin') {
  		// 微信处理成功之后 
  		return {
  			statusCode: 200,
  			headers: {
  				'content-type': 'text/xml;charset=utf-8'
  			},
  			body: `<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>`
  		}
  	}
  	// 支付宝处理成功后  
  	return {
  		statusCode: 200,
  		headers: {
  			'content-type': 'text/plain'
  		},
  		body: "success"
  	}
  };
  