Browse Source

代码整合

wangshan 6 years ago
parent
commit
f6194f1b9c

+ 1 - 1
src/jfw/modules/app/src/web/templates/dataExport/dataExPort.html

@@ -572,7 +572,7 @@
                 dataType: 'json',
                 dataType: 'json',
                 success: function(data){
                 success: function(data){
                     if(data._id!==""){
                     if(data._id!==""){
-                        // window.location.href="/weixin/pay/dataexport?id="+data._id+"&source=d"
+                        window.location.href="/jyapp/front/wx_dataExport/toCreateOrderPage?id="+data._id+"&source=d"
                     }
                     }
                 }
                 }
             })
             })

+ 96 - 218
src/jfw/pay/dataExportPay.go

@@ -17,211 +17,16 @@ import (
 
 
 type DataExportPayAction struct {
 type DataExportPayAction struct {
 	*xweb.Action
 	*xweb.Action
-	sacnPay_WaitPay       xweb.Mapper `xweb:"/dataExport/sacnPay/waitPay"`        //生成支付二维码
-	sacnPay_CreateOrder   xweb.Mapper `xweb:"/dataExport/sacnPay/createOrder"`    //创建订单
-	isPaySuccess          xweb.Mapper `xweb:"/dataExport/pay/isPaySuccess"`       //是否支付成功
-	getOrderPayMsg        xweb.Mapper `xweb:"/dataexport/getOrderPayMsg"`         //我的订单页面获取支付二维码
-	wxSacnPay_CreateOrder xweb.Mapper `xweb:"/dataExport/wx_sacnPay/createOrder"` //微信端-创建订单
+	sacnPay_WaitPay     xweb.Mapper `xweb:"/dataExport/sacnPay/waitPay"`     //生成支付二维码
+	sacnPay_CreateOrder xweb.Mapper `xweb:"/dataExport/sacnPay/createOrder"` //创建订单
+	isPaySuccess        xweb.Mapper `xweb:"/dataExport/pay/isPaySuccess"`    //是否支付成功
+	getOrderPayMsg      xweb.Mapper `xweb:"/dataexport/getOrderPayMsg"`      //我的订单页面获取支付二维码
 }
 }
 
 
 func init() {
 func init() {
 	xweb.AddAction(&DataExportPayAction{})
 	xweb.AddAction(&DataExportPayAction{})
 }
 }
 
 
-//微信端创建订单
-func (p *DataExportPayAction) WxSacnPay_CreateOrder() {
-	defer util.Catch()
-	openId, _ := p.GetSession("s_m_openid").(string)
-	userId, _ := p.GetSession("userId").(string)
-	id := util.SE.Decode4Hex(p.GetString("id"))
-	if userId == "" || id == "" {
-		log.Println("userId,id错误", userId, id)
-		p.ServeJson(map[string]interface{}{
-			"status": "n",
-		})
-		return
-	}
-	//
-	data_spec := p.GetString("data_spec")
-	if data_spec == "standard" {
-		data_spec = "标准字段包"
-	} else if data_spec == "senior" {
-		data_spec = "高级字段包"
-	}
-	user_mail_status := util.ObjToString(p.GetSession("DataExportVerifyEmail_val")) != ""
-	pay_way := p.GetString("pay_way")
-	user_mail, _ := p.GetSession("DataExportVerifyEmail_val").(string)
-	user_phone, _ := p.GetSession("DataExportVerifyPhone").(string)
-	if user_phone != "" {
-		p.SetSession("DataExportVerifyPhone_val", user_phone) //提交后下次默认带出手机号
-	} else {
-		user_phone, _ = p.GetSession("DataExportVerifyPhone_val").(string)
-	}
-	//先看有没有创建过预支付订单
-	oldOrder := public.Mysql.FindOne("dataexport_order", map[string]interface{}{
-		"user_id":   userId,
-		"filter_id": id,
-	}, "id,order_status,order_code,order_money,user_mail,user_phone,data_spec,prepay_time,prepay_id", "")
-	if oldOrder != nil {
-		if user_mail != "" && user_mail != util.ObjToString((*oldOrder)["user_mail"]) {
-			public.Mysql.Update("dataexport_order", map[string]interface{}{
-				"user_id":   userId,
-				"filter_id": id,
-			}, map[string]interface{}{
-				"user_mail": user_mail,
-			})
-		}
-		if user_phone != "" && user_phone != util.ObjToString((*oldOrder)["user_phone"]) {
-			public.Mysql.Update("dataexport_order", map[string]interface{}{
-				"user_id":   userId,
-				"filter_id": id,
-			}, map[string]interface{}{
-				"user_phone": user_phone,
-			})
-		}
-		//如果已支付,则返回
-		if util.IntAll((*oldOrder)["order_status"]) != 0 {
-			log.Println("该订单已存在,订单状态不允许支付!", openId)
-			p.ServeJson(map[string]interface{}{
-				"status": "n",
-			})
-			return
-		}
-		//价格支付方式变动需要重新生成订单二维码
-		if data_spec != "" && data_spec == util.ObjToString((*oldOrder)["data_spec"]) {
-			var res map[string]interface{}
-			nonceStr := util.GetRandom(16) + util.GetLetterRandom(16)
-			timestamp := time.Now().Unix()
-			sign := util.WxSign(fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%d&key=%s", public.WxStruct.Appid, nonceStr, "prepay_id="+util.ObjToString((*oldOrder)["prepay_id"]), "MD5", timestamp, public.WxStruct.Key))
-			res = map[string]interface{}{
-				"appId":     public.WxStruct.Appid,
-				"timestamp": fmt.Sprint(timestamp),
-				"signType":  "MD5",
-				"sign":      sign,
-				"nonceStr":  nonceStr,
-				"prepayId":  "prepay_id=" + util.ObjToString((*oldOrder)["prepay_id"]),
-			}
-			p.ServeJson(map[string]interface{}{
-				"status": "y",
-				"code":   util.ObjToString((*oldOrder)["order_code"]),
-				"token":  public.GetWaitPayToken(util.Int64All((*oldOrder)["id"]), util.IntAll((*oldOrder)["order_money"]), util.ObjToString((*oldOrder)["order_code"]), "微信", userId),
-				"res":    res,
-			})
-			return
-		}
-	}
-	//
-	data_count := public.GetDataExportSearchCountUseId(id)
-	if data_count > public.ExConf.MsgMaxCount {
-		data_count = public.ExConf.MsgMaxCount
-	}
-	order_money_, original_price_ := float64(0), float64(0)
-	if data_spec == "标准字段包" {
-		original_price_ = float64(data_count) * public.ExConf.UnitPrice_normal
-	} else if data_spec == "高级字段包" {
-		original_price_ = float64(data_count) * public.ExConf.UnitPrice_senior
-	}
-	order_money_ = float64(original_price_) * public.ExConf.Discount
-	if order_money_ < public.ExConf.OrderMinPrice {
-		order_money_ = public.ExConf.OrderMinPrice
-	}
-	if original_price_ < public.ExConf.OrderMinPrice {
-		original_price_ = public.ExConf.OrderMinPrice
-	}
-	order_money := int(order_money_ * 100)
-	original_price := int(original_price_ * 100)
-	//order_money = 1
-	//original_price = 1
-	filter_keys, filter_publishtime, filter := "", "", ""
-	//
-	isPass := func() bool {
-		if pay_way == "" || user_mail == "" || user_phone == "" || !user_mail_status || data_spec == "" || order_money <= 0 || data_count <= 0 || original_price <= 0 {
-			log.Println("参数不合法! pay_way, user_mail,user_phone, user_mail_status, data_spec, order_money, data_count, original_price", pay_way, user_mail, user_phone, user_mail_status, data_spec, order_money, data_count, original_price)
-			return false
-		}
-		userfilter, ok := public.MQFW.FindById("export_search", id, nil)
-		if ok && userfilter != nil {
-			keywords, _ := (*userfilter)["keywords"].([]interface{})
-			var buffer bytes.Buffer
-			for _, v := range keywords {
-				vm, _ := v.(map[string]interface{})
-				if buffer.Len() > 0 {
-					buffer.WriteString(",")
-				}
-				buffer.WriteString(util.ObjToString(vm["keyword"]))
-			}
-			filter_keys = buffer.String()
-			filter_publishtime = util.ObjToString((*userfilter)["publishtime"])
-			filterByte, _ := json.Marshal(userfilter)
-			filter = string(filterByte)
-		} else {
-			log.Println("id is not find in mongodb", userId, id)
-			return false
-		}
-		return true
-	}()
-	nickname, _ := p.GetSession("s_nickname").(string)
-	result := map[string]interface{}{
-		"status": "n",
-	}
-	if isPass {
-		//创建微信预支付订单,微信端支付tradeclass:c
-		tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), "c", p.IP(), openId, "", order_money)
-		log.Println("支付交易单", openId, ret)
-		if ret != nil && (*ret)["status"] == "1" {
-			now := time.Now()
-			ordercode := public.GetOrderCode(openId)
-			orderid := public.Mysql.Insert("dataexport_order", map[string]interface{}{
-				"order_money":        order_money,
-				"order_status":       0,
-				"service_status":     0,
-				"user_nickname":      nickname,
-				"user_openid":        openId,
-				"filter":             filter,
-				"prepay_id":          (*ret)["prepayid"],
-				"code_url":           (*ret)["codeurl"],
-				"out_trade_no":       tradeno,
-				"order_code":         ordercode,
-				"product_type":       "历史数据",
-				"create_time":        util.FormatDate(&now, util.Date_Full_Layout),
-				"prepay_time":        util.FormatDate(&now, util.Date_Full_Layout),
-				"original_price":     original_price,
-				"data_spec":          data_spec,
-				"user_mail":          user_mail,
-				"user_phone":         user_phone,
-				"data_count":         data_count,
-				"filter_publishtime": filter_publishtime,
-				"filter_keys":        filter_keys,
-				"filter_id":          id,
-				"pay_way":            pay_way,
-				"user_id":            userId, //20190719 移动端数据导出 生订单添加用户id
-			})
-			if orderid > 0 {
-				var res map[string]interface{}
-				nonceStr := util.GetRandom(16) + util.GetLetterRandom(16)
-				timestamp := time.Now().Unix()
-				sign := util.WxSign(fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%d&key=%s", public.WxStruct.Appid, nonceStr, "prepay_id="+util.ObjToString((*ret)["prepayid"]), "MD5", timestamp, public.WxStruct.Key))
-				res = map[string]interface{}{
-					"appId":     public.WxStruct.Appid,
-					"timestamp": fmt.Sprint(timestamp),
-					"signType":  "MD5",
-					"sign":      sign,
-					"nonceStr":  nonceStr,
-					"prepayId":  "prepay_id=" + util.ObjToString((*ret)["prepayid"]),
-				}
-				p.SetSession("DataExportVerifyPhone_val", user_phone)
-				result["status"] = "y"
-				result["code"] = ordercode
-				result["token"] = public.GetWaitPayToken(orderid, order_money, ordercode, pay_way, userId)
-				result["res"] = res
-			}
-		} else {
-			log.Println("生成微信预支付交易单失败", ret)
-		}
-	}
-	p.ServeJson(result)
-}
-
 //
 //
 func (p *DataExportPayAction) SacnPay_WaitPay() error {
 func (p *DataExportPayAction) SacnPay_WaitPay() error {
 	defer util.Catch()
 	defer util.Catch()
@@ -327,6 +132,10 @@ func (p *DataExportPayAction) SacnPay_CreateOrder() {
 	}
 	}
 	user_mail_status := util.ObjToString(p.GetSession("DataExportVerifyEmail_val")) != ""
 	user_mail_status := util.ObjToString(p.GetSession("DataExportVerifyEmail_val")) != ""
 	pay_way := p.GetString("pay_way")
 	pay_way := p.GetString("pay_way")
+	tradeclass := "b"
+	if pay_way == "wx_js" {
+		tradeclass = "c"
+	}
 	user_mail, _ := p.GetSession("DataExportVerifyEmail_val").(string)
 	user_mail, _ := p.GetSession("DataExportVerifyEmail_val").(string)
 	user_phone, _ := p.GetSession("DataExportVerifyPhone").(string)
 	user_phone, _ := p.GetSession("DataExportVerifyPhone").(string)
 	if user_phone != "" {
 	if user_phone != "" {
@@ -367,12 +176,12 @@ func (p *DataExportPayAction) SacnPay_CreateOrder() {
 		//价格支付方式变动需要重新生成订单二维码(关闭之前的订单)
 		//价格支付方式变动需要重新生成订单二维码(关闭之前的订单)
 		if (data_spec != "" && data_spec != util.ObjToString((*oldOrder)["data_spec"])) || pay_way != util.ObjToString((*oldOrder)["pay_way"]) {
 		if (data_spec != "" && data_spec != util.ObjToString((*oldOrder)["data_spec"])) || pay_way != util.ObjToString((*oldOrder)["pay_way"]) {
 			//在此关闭之前的订单
 			//在此关闭之前的订单
-			if !CloseDataExportOrder(util.ObjToString((*oldOrder)["pay_way"]), util.ObjToString((*oldOrder)["out_trade_no"]), util.ObjToString((*oldOrder)["prepay_time"])) {
-				p.ServeJson(map[string]interface{}{
-					"status": "n",
-				})
-				return
-			}
+			//			if !CloseDataExportOrder(util.ObjToString((*oldOrder)["pay_way"]), util.ObjToString((*oldOrder)["out_trade_no"]), util.ObjToString((*oldOrder)["prepay_time"])) {
+			//				p.ServeJson(map[string]interface{}{
+			//					"status": "n",
+			//				})
+			//				return
+			//			}
 			data_count := public.GetDataExportSearchCountUseId(id)
 			data_count := public.GetDataExportSearchCountUseId(id)
 			if data_count > public.ExConf.MsgMaxCount {
 			if data_count > public.ExConf.MsgMaxCount {
 				data_count = public.ExConf.MsgMaxCount
 				data_count = public.ExConf.MsgMaxCount
@@ -394,21 +203,76 @@ func (p *DataExportPayAction) SacnPay_CreateOrder() {
 			original_price := int(original_price_ * 100)
 			original_price := int(original_price_ * 100)
 			prepayTime, _ := time.ParseInLocation(util.Date_Full_Layout, util.ObjToString((*oldOrder)["prepay_time"]), time.Local)
 			prepayTime, _ := time.ParseInLocation(util.Date_Full_Layout, util.ObjToString((*oldOrder)["prepay_time"]), time.Local)
 			surplus_second := prepayTime.Unix() - 7200 //价格变动  支付二维码更新成超时状态--重新生订单二维码
 			surplus_second := prepayTime.Unix() - 7200 //价格变动  支付二维码更新成超时状态--重新生订单二维码
-			public.Mysql.Update("dataexport_order", map[string]interface{}{
-				"user_id":   userId,
-				"filter_id": id,
-			}, map[string]interface{}{
-				"data_spec":      data_spec,
-				"order_money":    order_money,
-				"original_price": original_price,
-				"prepay_time":    util.FormatDateByInt64(&surplus_second, util.Date_Full_Layout),
-				"pay_way":        pay_way,
-			})
+			//价格变动需重新生预支付订单 更新原来得库的信息
+			var res map[string]interface{}
+			if pay_way == "wx_js" {
+				now := time.Now()
+				tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), tradeclass, p.IP(), openId, "", order_money)
+				nonceStr := util.GetRandom(16) + util.GetLetterRandom(16)
+				timestamp := time.Now().Unix()
+				sign := util.WxSign(fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%d&key=%s", public.WxStruct.Appid, nonceStr, "prepay_id="+util.ObjToString((*ret)["prepayid"]), "MD5", timestamp, public.WxStruct.Key))
+				res = map[string]interface{}{
+					"appId":     public.WxStruct.Appid,
+					"timestamp": fmt.Sprint(timestamp),
+					"signType":  "MD5",
+					"sign":      sign,
+					"nonceStr":  nonceStr,
+					"prepayId":  "prepay_id=" + util.ObjToString((*ret)["prepayid"]),
+				}
+				public.Mysql.Update("dataexport_order", map[string]interface{}{
+					"user_id":   userId,
+					"filter_id": id,
+				}, map[string]interface{}{
+					"data_spec":      data_spec,
+					"order_money":    order_money,
+					"original_price": original_price,
+					"prepay_time":    util.FormatDate(&now, util.Date_Full_Layout),
+					"pay_way":        pay_way,
+					"prepay_id":      (*ret)["prepayid"],
+					"code_url":       (*ret)["codeurl"],
+					"out_trade_no":   tradeno,
+				})
+			} else {
+				public.Mysql.Update("dataexport_order", map[string]interface{}{
+					"user_id":   userId,
+					"filter_id": id,
+				}, map[string]interface{}{
+					"data_spec":      data_spec,
+					"order_money":    order_money,
+					"original_price": original_price,
+					"prepay_time":    util.FormatDateByInt64(&surplus_second, util.Date_Full_Layout),
+					"pay_way":        pay_way,
+				})
+			}
 			(*oldOrder)["order_money"] = order_money
 			(*oldOrder)["order_money"] = order_money
 			p.ServeJson(map[string]interface{}{
 			p.ServeJson(map[string]interface{}{
 				"status": "y",
 				"status": "y",
 				"code":   util.ObjToString((*oldOrder)["order_code"]),
 				"code":   util.ObjToString((*oldOrder)["order_code"]),
 				"token":  public.GetWaitPayToken(util.Int64All((*oldOrder)["id"]), util.IntAll((*oldOrder)["order_money"]), util.ObjToString((*oldOrder)["order_code"]), "微信", userId),
 				"token":  public.GetWaitPayToken(util.Int64All((*oldOrder)["id"]), util.IntAll((*oldOrder)["order_money"]), util.ObjToString((*oldOrder)["order_code"]), "微信", userId),
+				"res":    res,
+			})
+			return
+		} else if data_spec != "" && data_spec == util.ObjToString((*oldOrder)["data_spec"]) {
+			//微信端 价格没有变动则无需重新生预支付订单
+			var res map[string]interface{}
+			if pay_way == "wx_js" {
+				nonceStr := util.GetRandom(16) + util.GetLetterRandom(16)
+				timestamp := time.Now().Unix()
+				sign := util.WxSign(fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%d&key=%s", public.WxStruct.Appid, nonceStr, "prepay_id="+util.ObjToString((*oldOrder)["prepay_id"]), "MD5", timestamp, public.WxStruct.Key))
+				res = map[string]interface{}{
+					"appId":     public.WxStruct.Appid,
+					"timestamp": fmt.Sprint(timestamp),
+					"signType":  "MD5",
+					"sign":      sign,
+					"nonceStr":  nonceStr,
+					"prepayId":  "prepay_id=" + util.ObjToString((*oldOrder)["prepay_id"]),
+				}
+			}
+			p.ServeJson(map[string]interface{}{
+				"status": "y",
+				"code":   util.ObjToString((*oldOrder)["order_code"]),
+				"token":  public.GetWaitPayToken(util.Int64All((*oldOrder)["id"]), util.IntAll((*oldOrder)["order_money"]), util.ObjToString((*oldOrder)["order_code"]), "微信", userId),
+				"res":    res,
 			})
 			})
 			return
 			return
 		}
 		}
@@ -469,9 +333,8 @@ func (p *DataExportPayAction) SacnPay_CreateOrder() {
 	}
 	}
 	if isPass {
 	if isPass {
 		//创建微信预支付订单
 		//创建微信预支付订单
-		if pay_way == "wx_pc" {
-			tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), "b", p.IP(), openId, "", order_money)
-			log.Println("支付交易单", openId, ret)
+		if pay_way == "wx_pc" || pay_way == "wx_js" {
+			tradeno, ret := public.WxStruct.CreatePrepayOrder(config.Sysconfig["weixinrpc"].(string), tradeclass, p.IP(), openId, "", order_money)
 			if ret != nil && (*ret)["status"] == "1" {
 			if ret != nil && (*ret)["status"] == "1" {
 				now := time.Now()
 				now := time.Now()
 				ordercode := public.GetOrderCode(openId)
 				ordercode := public.GetOrderCode(openId)
@@ -501,10 +364,25 @@ func (p *DataExportPayAction) SacnPay_CreateOrder() {
 					"user_id":            userId, //20190719 移动端数据导出 生订单添加用户id
 					"user_id":            userId, //20190719 移动端数据导出 生订单添加用户id
 				})
 				})
 				if orderid > 0 {
 				if orderid > 0 {
+					var res map[string]interface{}
+					if pay_way == "wx_js" {
+						nonceStr := util.GetRandom(16) + util.GetLetterRandom(16)
+						timestamp := time.Now().Unix()
+						sign := util.WxSign(fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%d&key=%s", public.WxStruct.Appid, nonceStr, "prepay_id="+util.ObjToString((*ret)["prepayid"]), "MD5", timestamp, public.WxStruct.Key))
+						res = map[string]interface{}{
+							"appId":     public.WxStruct.Appid,
+							"timestamp": fmt.Sprint(timestamp),
+							"signType":  "MD5",
+							"sign":      sign,
+							"nonceStr":  nonceStr,
+							"prepayId":  "prepay_id=" + util.ObjToString((*ret)["prepayid"]),
+						}
+					}
 					p.SetSession("DataExportVerifyPhone_val", user_phone)
 					p.SetSession("DataExportVerifyPhone_val", user_phone)
 					result["status"] = "y"
 					result["status"] = "y"
 					result["code"] = ordercode
 					result["code"] = ordercode
 					result["token"] = public.GetWaitPayToken(orderid, order_money, ordercode, pay_way, userId)
 					result["token"] = public.GetWaitPayToken(orderid, order_money, ordercode, pay_way, userId)
+					result["res"] = res
 				}
 				}
 			} else {
 			} else {
 				log.Println("生成微信预支付交易单失败", ret)
 				log.Println("生成微信预支付交易单失败", ret)

+ 2 - 2
src/jfw/pay/weixin.go

@@ -173,7 +173,7 @@ func (p *WxPayAction) WxPay() error {
 	return nil
 	return nil
 }
 }
 
 
-//pc端扫码支付才会回调,微信端直接支付并没有
+//微信支付成功后回调
 func (p *WxPayAction) PayCallback() {
 func (p *WxPayAction) PayCallback() {
 	defer util.Catch()
 	defer util.Catch()
 	by := p.Body()
 	by := p.Body()
@@ -226,7 +226,7 @@ func (p *WxPayAction) PayCallback() {
 							"pay_money":    util.IntAll(ret["cash_fee"]),
 							"pay_money":    util.IntAll(ret["cash_fee"]),
 							"pay_time":     pay_time,
 							"pay_time":     pay_time,
 							"order_status": 1,
 							"order_status": 1,
-							"pay_way":      "微信",
+							//"pay_way":      "微信",
 							"download_url": download_url,
 							"download_url": download_url,
 						})
 						})
 						if flag_1 {
 						if flag_1 {

+ 2 - 2
src/web/templates/weixin/dataExport/dataExport_payOrder.html

@@ -528,11 +528,11 @@
 					canpay=false;
 					canpay=false;
 					
 					
 					var param={};
 					var param={};
-					param.pay_way="M-微信";
+					param.pay_way="wx_js";
 					param.data_spec=dataType==2?"senior":"standard";
 					param.data_spec=dataType==2?"senior":"standard";
 					param.id={{.T._id}}
 					param.id={{.T._id}}
 					//先去创建预支付订单
 					//先去创建预支付订单
-					$.post("/dataExport/wx_sacnPay/createOrder",param,function(data){
+					$.post("/dataExport/sacnPay/createOrder",param,function(data){
 						//alert(JSON.stringify(data))
 						//alert(JSON.stringify(data))
 						if(data.status=="y"){
 						if(data.status=="y"){
 							sessionStorage.setItem("order_spec"+{{.T._id}}, dataType);
 							sessionStorage.setItem("order_spec"+{{.T._id}}, dataType);