原创作者:田超凡(程序员田宝宝)
版权所有,引用请注明原作者,严禁复制转载
TimpleShop关键代码说明书
1.短信验证码功能的代码:
2.js加入购物车的特效 抛物线
3.百度地图定位web
4.银联支付(接口api参考 银联api.txt)
5. 支付宝沙箱对接
6. QQ登录对接
7. IP定位
1.短信验证码功能的代码:
//web平台给的接口用户名
String name="13789998659";
//web平台给的接口密码
String pwd="1531F3D851DE8D880BB499491E0B";
// 传入进来的电话号码字符串,中间用英文逗号间隔
StringBuffer mobileString=new StringBuffer(phone);
//生成随机数的代码
int max=99999;
int min=1000;
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min; //将生成的验证码放进缓存里面 方便等下的验证 ActionContext.getContext().getSession().put("yzm", s);
// 将要发生内容字符串 可自定义
StringBuffer contextString=new StringBuffer("您注册的验证码:"+s+"如非本人操作,请忽略。【TimpleShop】");
// 签名
String sign="";
// 追加发送时间,可为空,为空为及时发送
String stime="";
// 扩展码,必须为数字 可为空
StringBuffer extno=new StringBuffer();
//然后去调用web平台给的接口方法 dopost
doPost(name, pwd, mobileString, contextString, sign, stime, extno);
//响应状态值说明
代码 | 说明 |
0 | 提交成功 |
1 | 含有敏感词汇 |
2 | 余额不足 |
3 | 没有号码 |
4 | 包含sql语句 |
10 | 账号不存在 |
11 | 账号注销 |
12 | 账号停用 |
13 | IP鉴权失败 |
14 | 格式错误 |
-1 | 系统异常 |
//传入以上的参数
public static String doPost(String name, String pwd,
StringBuffer mobileString, StringBuffer contextString,
String sign, String stime, StringBuffer extno) throws Exception {
StringBuffer param = new StringBuffer();
param.append("name="+name);
param.append("&pwd="+pwd);
param.append("&mobile=").append(mobileString);
param.append("&content=").append(URLEncoder.encode(contextString.toString(),"UTF-8"));
param.append("&stime="+stime);
param.append("&sign=").append(URLEncoder.encode(sign,"UTF-8"));
param.append("&type=pt");
param.append("&extno=").append(extno);
//去访问别人的web平台 就是web Service
//UTF-8编码地址为:http://web.cr6868.com/asmx/smsservice.aspx
GB2312编码地址为:http://web.cr6868.com/gbk/smsservice.aspx
URL localURL = new URL("http://web.cr6868.com/asmx/smsservice.aspx?");
//后面就是一些输出流之类的 访问的方式为post
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(param.length()));
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
String resultBuffer = "";
try {
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(param.toString());
outputStreamWriter.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
resultBuffer = convertStreamToString(inputStream);
} finally {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer;
}
快递查询功能的代码:
//物流跟踪也是采用web Service
kuaidi=doPost(快递英文名称,快递单号);
//返回值是一个string类型的xml格式的字符串
//需要把节点的信息取出来并且装在string[]里面
Document document = DocumentHelper.parseText(kuaidi);
List<String[]> lists=new ArrayList<String[]>();
for (int i = 1; i < document.getDocument().getRootElement().nodeCount(); i+=2) {
Node node=document.getDocument().getRootElement().node(i);
if(i>3){
String[] item=node.getText().split("-");
lists.add(item);
}
}
addlists=lists;
//这个方法是通过上面短信接口的方法进行改动的
//传入的两个参数
public static String doPost(String Compay, String OrderNo) throws Exception {
StringBuffer param = new StringBuffer();
param.append("Compay="+Compay);
param.append("&OrderNo="+OrderNo);
//Compay=string&OrderNo=string
//这个url是web Service 有两种访问的方式
//一种是http post 另一种是soap 方式访问
URL localURL = new URL("http://www.gpsso.com/webservice/kuaidi/kuaidi.asmx/KuaidiQuery");
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(param.length()));
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
String resultBuffer = "";
try {
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(param.toString());
outputStreamWriter.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
resultBuffer = convertStreamToString(inputStream);
} finally {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer;
}
2.js加入购物车的特效 抛物线
//导入一个js库 然后去调用里面的一些方法
// 元素以及其他一些变量
//首先要获取抛物线的落点 也就是侧边栏购物车的位置
var eleFlyElement = document.querySelector("#flyItem"), eleShopCart = document.querySelector(".right_gwd_1");
var numberItem = 0;
// 进行抛物线运动
var myParabola = funParabola(eleFlyElement, eleShopCart, {
speed: 300, //抛物线速度
curvature: 0.0010, //控制抛物线弧度
complete: function() {
eleFlyElement.style.visibility = "hidden";
}
});
// 绑定点击事件
if (eleFlyElement && eleShopCart) {
[].slice.call(document.getElementsByClassName("btnCart")).forEach(function(button) {
button.addEventListener("click", function(event) {
var ss=$(".goods_size_ul li");
var size=$("#ss").text();
//进行尺码判断是否为空
if(ss.length>0){
if(size==0){
alert("您还没有选择尺码!");
}else{
//不为空再去执行那个动画 获取当前的图片路径 赋给当前已经定义好的img
var src = $(".As_big1").attr("src");
$("#flyItem").find("img").attr("src", src);
// 滚动大小
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft || 0,
scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
eleFlyElement.style.left = event.clientX + scrollLeft + "px";
eleFlyElement.style.top = event.clientY + scrollTop + "px";
eleFlyElement.style.visibility = "visible";
// 让它抛物线完之后,还需要归还原位 目的是用户还会二次点击加入购物车
myParabola.position().move();
}
}else{
var src = $(".As_big1").attr("src");
$("#flyItem").find("img").attr("src", src);
// 抛物线的幅度大小
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft || 0,
scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
eleFlyElement.style.left = event.clientX + scrollLeft + "px";
eleFlyElement.style.top = event.clientY + scrollTop + "px";
eleFlyElement.style.visibility = "visible";
myParabola.position().move();
}
});
});
}
3.百度地图定位web:
//利用JavaScript的方式去访问
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=AEO1puS1989e6GtipTugtUyqRZ8NSjqu"></script>
//根据浏览器定位
<script type="text/javascript" >
//创建一个map地图 以下是方法以及参数,返回值介绍
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
else {
alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true})
//关于状态码
//BMAP_STATUS_SUCCESS 检索成功。对应数值“0”。
//BMAP_STATUS_CITY_LIST 城市列表。对应数值“1”。
//BMAP_STATUS_UNKNOWN_LOCATION 位置结果未知。对应数值“2”。
//BMAP_STATUS_UNKNOWN_ROUTE 导航结果未知。对应数值“3”。
//BMAP_STATUS_INVALID_KEY 非法密钥。对应数值“4”。
//BMAP_STATUS_INVALID_REQUEST 非法请求。对应数值“5”。
//BMAP_STATUS_PERMISSION_DENIED 没有权限。对应数值“6”。(自 1.1 新增)
//BMAP_STATUS_SERVICE_UNAVAILABLE 服务不可用。对应数值“7”。(自 1.1 新增)
//BMAP_STATUS_TIMEOUT 超时。对应数值“8”。(自 1.1 新增)
</script>
页面静态化:
//为了解决高并发的问题采用页面静态化处理
//并且有利于搜索引擎优化SEO
//这个项目采用的
private Logger logger = Logger.getLogger(Httpclient.class);
HttpClient httpClient = null; //HttpClient实例
GetMethod getMethod =null; //GetMethod实例
String page = null;
String webappname = null;
BufferedWriter fw = null;
BufferedReader br = null;
InputStream in = null;
StringBuffer sb = null;
String line = null;
//构造方法
public Httpclient(String webappname){
this.webappname = webappname;
}
/** 根据模版及参数产生静态页面 */
public boolean createHtmlPage(String url,String htmlFileName){
boolean status = false;
int statusCode = 0;
try{
//创建一个HttpClient实例充当模拟浏览器
httpClient = new HttpClient();
//设置httpclient读取内容时使用的字符集
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
//创建GET方法的实例
getMethod = new GetMethod(url);
//使用系统提供的默认的恢复策略,在发生异常时候将自动重试3次
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//设置Get方法提交参数时使用的字符集,以支持中文参数的正常传递
getMethod.addRequestHeader("Content-Type","text/html;charset=UTF-8");
//执行Get方法并取得返回状态码,200表示正常,其它代码为异常
statusCode = httpClient.executeMethod(getMethod);
if (statusCode!=200) {
logger.fatal("静态页面引擎在解析"+url+"产生静态页面"+htmlFileName+"时出错!");
}else{
//读取解析结果
sb = new StringBuffer();
in = getMethod.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
if(br!=null)br.close();
page = sb.toString();
//将页面中的相对路径替换成绝对路径,以确保页面资源正常访问
page = formatPage(page);
//将解析结果写入指定的静态HTML文件中,实现静态HTML生成
writeHtml(htmlFileName,page);
status = true;
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
//释放http连接
getMethod.releaseConnection();
}
return status;
}
//将解析结果写入指定的静态HTML文件中
private synchronized void writeHtml(String htmlFileName,String page) throws Exception{
fw = new BufferedWriter(new FileWriter(htmlFileName));
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(htmlFileName),"UTF-8");
fw.write(page);
if(fw!=null)fw.close();
}
//将页面中的相对路径替换成绝对路径,以确保页面资源正常访问
private String formatPage(String page){
page = page.replaceAll("\\.\\./\\.\\./\\.\\./", webappname+"/");
page = page.replaceAll("\\.\\./\\.\\./", webappname+"/");
page = page.replaceAll("\\.\\./", webappname+"/");
return page;
}
//测试方法
public static void main(String[] args){
Httpclient h = new Httpclient("webappname");
h.createHtmlPage("http://localhost:8080/TimpleShop/AS_home_getBrand.action","D:/TimpleShop/TimpleShop/WebRoot/jsp/Httpclient/home.html");
System.out.println("静态页面已经生成");
}
}
4.银联支付(接口api参考 银联api.txt)
基于Servlet的接口,接受用户传入参数生成自动跳转的Html表单,返回响应报文到回调地址;
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/**
* 请求银联接入地址,获取证书文件,证书路径等相关参数初始化到SDKConfig类中
* 在java main 方式运行时必须每次都执行加载
* 如果是在web应用开发里,这个方法可使用监听的方式写入缓存,无须在这出现
*/
//这里已经将加载属性文件的方法挪到了web/AutoLoadServlet.java中
//SDKConfig.getConfig().loadPropertiesFromSrc(); //从classpath加载acp_sdk.properties文件
//前台页面传过来的
String merId = req.getParameter("merId");
String txnAmt = req.getParameter("txnAmt");
String userid=req.getParameter("userid");
String addid=req.getParameter("addid");
Map<String, String> requestData = new HashMap<String, String>();
/***银联全渠道系统,产品参数,除了encoding自行选择外其他不需修改***/
requestData.put("version", DemoBase.version); //版本号,全渠道默认值
requestData.put("encoding", DemoBase.encoding_UTF8); //字符集编码,可以使用UTF-8,GBK两种方式
requestData.put("signMethod", "01"); //签名方法,只支持 01:RSA方式证书加密
requestData.put("txnType", "02"); //交易类型 ,02:预授权
requestData.put("txnSubType", "01"); //交易子类型, 01:预授权
requestData.put("bizType", "000201"); //业务类型,B2C网关支付,手机wap支付
requestData.put("channelType", "07"); //渠道类型,这个字段区分B2C网关支付和手机wap支付;07:PC,平板 08:手机
/***商户接入参数***/
requestData.put("merId", merId); //商户号码,请改成自己申请的正式商户号或者open上注册得来的777测试商户号
requestData.put("accessType", "0"); //接入类型,0:直连商户
requestData.put("orderId",DemoBase.getOrderId()); //商户订单号,8-40位数字字母,不能含“-”或“_”,可以自行定制规则
requestData.put("txnTime", DemoBase.getCurrentTime()); //订单发送时间,取系统时间,格式为YYYYMMDDhhmmss,必须取当前时间,否则会报txnTime无效
requestData.put("currencyCode", "156"); //交易币种(境内商户一般是156 人民币)
requestData.put("txnAmt", txnAmt); //交易金额,单位分,不要带小数点
requestData.put("reqReserved", userid+","+addid); //请求方保留域,如需使用请启用即可;透传字段(可以实现商户自定义参数的追踪)本交易的后台通知,对本交易的交易状态查询交易、对账文件中均会原样返回,商户可以按需上传,长度为1-1024个字节
//前台通知地址 (需设置为外网能访问 http https均可),支付成功后的页面 点击“返回商户”按钮的时候将异步通知报文post到该地址
//如果想要实现过几秒中自动跳转回商户页面权限,需联系银联业务(发邮件到operation@unionpay.com)申请开通自动返回商户权限
//异步通知参数详见open.unionpay.com帮助中心 下载 产品接口规范 网关支付产品接口规范 消费交易 商户通知
requestData.put("frontUrl", DemoBase.frontUrl);
//后台通知地址(需设置为【外网】能访问 http https均可),支付成功后银联会自动将异步通知报文post到商户上送的该地址,失败的交易银联不会发送后台通知
//后台通知参数详见open.unionpay.com帮助中心 下载 产品接口规范 网关支付产品接口规范 消费交易 商户通知
//注意:1.需设置为外网能访问,否则收不到通知 2.http https均可 3.收单后台通知后需要10秒内返回http200或302状态码
// 4.如果银联通知服务器发送通知后10秒内未收到返回状态码或者应答码非http200,那么银联会间隔一段时间再次发送。总共发送5次,每次的间隔时间为0,1,2,4分钟。
// 5.后台通知地址如果上送了带有?的参数,例如:http://abc/web?a=b&c=d 在后台通知处理程序验证签名之前需要编写逻辑将这些字段去掉再验签,否则将会验签失败
requestData.put("backUrl", DemoBase.backUrl);
//
//
// 报文中特殊用法请查看 PC wap网关跳转预授权特殊用法.txt
//
//
/**请求参数设置完毕,以下对请求参数进行签名并生成html表单,将表单写入浏览器跳转打开银联页面**/
Map<String, String> submitFromData = AcpService.sign(requestData,DemoBase.encoding_UTF8); //报文中certId,signature的值是在signData方法中获取并自动赋值的,只要证书配置正确即可。
String requestFrontUrl = SDKConfig.getConfig().getFrontRequestUrl(); //获取请求银联的前台地址:对应属性文件acp_sdk.properties文件中的acpsdk.frontTransUrl
String html = AcpService.createAutoFormHtml(requestFrontUrl, submitFromData,DemoBase.encoding_UTF8); //生成自动跳转的Html表单
LogUtil.writeLog("打印请求HTML,此为请求报文,为联调排查问题的依据:"+html);
//将生成的html写到浏览器中完成自动跳转打开银联支付页面;这里调用signData之后,将html写到浏览器跳转到银联页面之前均不能对html中的表单项的名称和值进行修改,如果修改会导致验签不通过
resp.getWriter().write(html);
}
交易成功的响应报文:
交易失败会自动回调error;
5.支付宝
配置支付宝的系统参数:
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String app_id = "2016080200150129";
// 商户私钥,您的PKCS8格式RSA2私钥
public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCT9Yi7zFul4bvcrvaj1ILMjOsUdThY5q2U/UDuCVkPlhlGmXws1Rtai9eJNSFbLWZnysya5faph5A8QaGzfBKFy8ej6bME6KCva2n5T4VKQkP3tDz1DQSN4pYB0BuMBBE8HNzCv2WZzf+bVU32WcVebAdhJRchOmQeyhVpGxEjGoiTgX0ziKh2E7/mcq0lSonksJorXOnZGQaPHvWiU2FH6BmKhOzz++WHQfz6ApZ2nrBhyYf8q/xQiryTZfEvZRDGS6VVWrR2c6CCBUlHYJ8pic0YBe0LfIdF+XVvLgG3kk9SFnsiYFroReDDAo+2+GcR5EA5Q0d4RV2wHdMpSyapAgMBAAECggEAJ0Rlqnmc5RRYmEPy/x3aLNFFXmORtb5a2QyxW7stYL6ULLF1TCyumsQ8SpTIKnuNDwS6cm2PngR1WhiNAqZuAAZKEI1oTmgypPub32ekV0Xo5ed2ug5fZAx7hTWm6ez2af669BB5tWz/ZkkobLX8dIe1GaoPTmlBww4cyo09ahQr7qCPZ2SXBxXI3UijxbSvPJzVUA2DCe6VDoULPWF6OQQmsbSDlX+pF6zHA6o+6KvhsGjYQtUmW+R5erICr0f4uAbRReciVycCv5nJQ7Jp3y4IS6MJK2YL5/mlqVglb2uWOKH3QicF5MeEhTBJwW87xMWSpYYU8GtJgwvWwVUkFQKBgQDUuUrzlTbwK/q9Hb9QxnT29QNGbXIRkSF5jWYJTUJMIXXWhk4t/pp2eOI7S//ffw45XjbyOS04FXnlmUIs27RP8i99uFYxiEdL288CKyqAppPUwzIt1XysNkm4D+6VeUfXr/OLaAjLwSIZ/l/vbolFE7QzdIh72nzhcMDV+KXNfwKBgQCyD0frdBO4FpIdsEKo8jCYK/LUS6UfJPyOJz0OJjjBRsrQHFlnZchd461qkZmWFkU0HSGB0yKNSSOhQHj3kppHKjhsuJvjaAXjIXUZPXNelmQHSlAuNkLdZrWPFRGqgl0qSLfrWtB8elo7EFXtsSRjkGWaxZKXl/OGAEJ0Uc7v1wKBgQCM0lE86WXiHqsxsNSq64YYymAtqlxeJr6LUkDfHYcrEOeKaYStzCODFElnycsMsHbBIY6sUY+O1iDJDukQ+yzl08T+rB5bUgUowc3LeLn9SskIi2PXlcGf25x3vgxKZWxa678FZYyVHjiuDoiB36H92apsedO0chJDx7nQrHdOjwKBgAe17bFeh3QTViR++4QBaOVOEefrlqzwlxc4z8YLFa4Y+LNmZuC5YaHfOCVDRNlhAYIuOMM8d2SS5D4ZYqOl75RhWawrmmSQnqClVELRjGtuK8q9BxfZNbAMwJP/n5I4rvwjxgMlHZ4mVj30iSQP5bmCCURYhe6Wn+8Pl9Edsc5lAoGAW614LCuioRJ2LSi/XpFfkWI79tOLWMX8NT+R61ii1l0pvXl4WOQ6sBnUDlT7is0yzdbLZjZhtDgJPojFDgLw7Occ0ul4v73f2drGTi7cCdv0SKDU8F6m6PnwP2hMcCPVNZDiCe1WPUnu6Ow+UM76wZ9kXs41rlGHw04aNm90EPc=";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtPP9ITQulLJjrQTP3EoYXDYig2UltHIipF/LnI6Ck1iko41sKyZIYdVTgur9EKQ/t0x1/843boxeYfvkXeCwzuPwdzDpk5U9fg7+I9XALCi1c7Ikscs5RzcHPU9nyoStgOblhbsEFkjhB92yWZmhyEMPX+7SH1cEGVoXdg5UQ2i5+wIncCfeWxbwV5P3wpNtRMxBb5lOxgxEGlFGllRXFlSwe1/p5j4jcbH6bghmLptjv7h2SF0sGbXkTs1eCoxPlXTxCkg0cMu8ryIJ9pfxz78jZvdmma1CAQgiGuYKSsT0eksAJF66EBoEJUvUygxMY6DKqFZvkZ50LW3iWDaCcQIDAQAB";
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://localhost:8080/TimpleShop/notify_url.jsp";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "http://localhost:8080/TimpleShop/return_url.jsp";
// 签名方式
public static String sign_type = "RSA2";
// 字符编码格式
public static String charset = "utf-8";
// 支付宝网关
public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
// 支付宝网关
public static String log_path = "C:\\";
获取用户的自定义参数加上封装好的系统参数,生成签名后的请求报文,请求支付宝接口:
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//商户订单号,商户网站订单系统中唯一订单号,必填
String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//付款金额,必填
String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
//订单名称,必填
String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
//商品描述,可空
String body = new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
// + "\"total_amount\":\""+ total_amount +"\","
// + "\"subject\":\""+ subject +"\","
// + "\"body\":\""+ body +"\","
// + "\"timeout_express\":\"10m\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
String result = alipayClient.pageExecute(alipayRequest).getBody();
//输出
out.println(result);
对应api参考
页面回调
提交订单的请求参数
6.QQ登录
首先到QQ开发平台注册应用等信息,获取appid
JS:
<script type="text/javascript"
src="http://qzonestyle.gtimg.cn/qzone/openapi/qc_loader.js" data-appid="101394710" data-redirecturi="http://localhost:8080/TimpleShop/qq.jsp" charset="utf-8"></script>
//通过QQjs,将指定的节点变成QQ登录按钮
QC.Login({
btnId:"qqLoginBtn" //插入按钮的节点id
});
//使用自定义的QQ登录按钮,绑定登录的功能
function qq(){
$("#qqLoginBtn a").trigger('click');
QQreg();
}
//实时获取登录状态
function QQreg(){
var image= $(".figure img").attr("src");
var name=$(".nickname").text();
if(image==undefined){
setTimeout(QQreg,1000);
}else{
jQuery.ajax({
url : 'As_user_login1.action',
data : {"image":image,"username":name},
type : "POST",
beforeSend : function() {
},
success : function(data) {
var js=$.parseJSON(data);
if(js.user_id!=0){
callback();
}else{
$(".userpwd").val("");
$(".userlogin").val("");
}
}
});
}
}
//注销登录
QC.Login.signOut();
7.IP定位
首先通过请求头得到访问者的ip;
public String addAddress() throws NumberFormatException, JSONException, UnsupportedEncodingException, UnknownHostException{
String ip = ServletActionContext.getRequest().getHeader("X-Real-IP");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
}
ip = ServletActionContext.getRequest().getHeader("X-Forwarded-For");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个IP值,第一个为真实IP。
int index = ip.indexOf(',');
if (index != -1) {
ip= ip.substring(0, index);
} else {
}
} else {
ip= ServletActionContext.getRequest().getRemoteAddr();
}
//判断IP和服务器地址是否一致,一致就返回服务器所在真实地址;
if (ip.equals(ServletActionContext.getRequest().getLocalAddr())||ip.equals("0:0:0:0:0:0:0:1")) {
addressSF="湖北省";
addressCS="武汉市";
addressDQ="洪山区";
}else{
//否则调用淘宝接口,查询IP对应的真实地址,查完后将省,市,县返回页面
String address=new AddressUtils().getAddresses("ip="+InetAddress.getLocalHost(), "utf-8");
String[] addresss=address.split("=");
addressSF=addresss[0];
addressCS=addresss[1];
addressDQ=addresss[2];
}
淘宝查地址接口:
/**
* 根据IP地址获取详细的地域信息
* @project:personGocheck
* @class:AddressUtils.java
* @author:heguanhua E-mail:37809893@qq.com
* @date:Nov 14, 2012 6:38:25 PM
*/
public class AddressUtils {
/**
*
* @param content
* 请求的参数 格式为:name=xxx&pwd=xxx
* @param encoding
* 服务器端请求编码。如GBK,UTF-8等
* @return
* @throws UnsupportedEncodingException
*/
public String getAddresses(String content, String encodingString)
throws UnsupportedEncodingException {
// 这里调用pconline的接口
String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
// 从http://whois.pconline.com.cn取得IP所在的省市区信息
String returnStr = this.getResult(urlStr, content, encodingString);
if (returnStr != null) {
// 处理返回的省市区信息
String[] temp = returnStr.split(",");
if(temp.length<3){
return "0";//无效IP,局域网测试
}
String region = (temp[5].split(":"))[1].replaceAll("\"", "");
region = decodeUnicode(region);// 省份
String country = "";
String area = "";
// String region = "";
String city = "";
String county = "";
String isp = "";
for (int i = 0; i < temp.length; i++) {
switch (i) {
case 1:
country = (temp[i].split(":"))[2].replaceAll("\"", "");
country = decodeUnicode(country);// 国家
break;
case 3:
area = (temp[i].split(":"))[1].replaceAll("\"", "");
area = decodeUnicode(area);// 地区
break;
case 5:
region = (temp[i].split(":"))[1].replaceAll("\"", "");
region = decodeUnicode(region);// 省份
break;
case 7:
city = (temp[i].split(":"))[1].replaceAll("\"", "");
city = decodeUnicode(city);// 市区
break;
case 9:
county = (temp[i].split(":"))[1].replaceAll("\"", "");
county = decodeUnicode(county);// 地区
break;
case 11:
isp = (temp[i].split(":"))[1].replaceAll("\"", "");
isp = decodeUnicode(isp); // ISP公司
break;
}
}
region=region+"="+city+"="+county;
return region;
}
return null;
}
/**
* @param urlStr
* 请求的地址
* @param content
* 请求的参数 格式为:name=xxx&pwd=xxx
* @param encoding
* 服务器端请求编码。如GBK,UTF-8等
* @return
*/
private String getResult(String urlStr, String content, String encoding) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();// 新建连接实例
connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
connection.setDoOutput(true);// 是否打开输出流 true|false
connection.setDoInput(true);// 是否打开输入流true|false
connection.setRequestMethod("POST");// 提交方法POST|GET
connection.setUseCaches(false);// 是否缓存true|false
connection.connect();// 打开连接端口
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());// 打开输出流往对端服务器写数据
out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx
out.flush();// 刷新
out.close();// 关闭输出流
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据
// ,以BufferedReader流来读取
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();// 关闭连接
}
}
return null;
}
/**
* unicode 转换成 中文
*
* @author fanhui 2007-3-15
* @param theString
* @return
*/
public static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't') {
aChar = '\t';
} else if (aChar == 'r') {
aChar = '\r';
} else if (aChar == 'n') {
aChar = '\n';
} else if (aChar == 'f') {
aChar = '\f';
}
outBuffer.append(aChar);
}
} else {
outBuffer.append(aChar);
}
}
return outBuffer.toString();
}
页面通过QQ提供的JS省市区三级联动
<script type="text/javascript" src="js/geo.js"></script>
//这个函数是必须的,因为在geo.js里每次更改地址时会调用此函数
function promptinfo()
{
var s1 = document.getElementById('s1');
var s2 = document.getElementById('s2');
var s3 = document.getElementById('s3');
/* $("#s2 option[value='武汉市']").attr("selected","selected"); */
}
$(function(){
$("form").addClass('animated lightSpeedIn');
var SF=$("#addresssf").text();
var CS=$("#addresscs").text();
var DQ=$("#addressdq").text();
setup();
preselect(SF,0);
for(var i=0;i<$("#s2 option").length;i++){
if($($("#s2 option").get(i)).val()==CS){
document.getElementById(s[1]).selectedIndex = i;
change(2);
}
}
for(var i=0;i<$("#s3 option").length;i++){
if($($("#s3 option").get(i)).val()==DQ){
document.getElementById(s[2]).selectedIndex = i;
change(3);
}
}
promptinfo();
});