package cn.simo.web.servlet;
import java.io.IOException;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils; import cn.simo.*;import cn.simo.service.UserService;/** * Servlet implementation class GetOpenId */public class BindWechatServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BindWechatServlet() { super(); }/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String code = request.getParameter("code");// HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?appid="+Configure.getAppID()+"&secret="+Configure.getSecret()+"&js_code="+code+"&grant_type=authorization_code");// //鐠佸墽鐤嗙拠閿嬬湴閸c劎娈戦柊宥囩枂// HttpClient httpClient = HttpClients.createDefault();// HttpResponse res = httpClient.execute(httpGet);// HttpEntity entity = res.getEntity();// String result = EntityUtils.toString(entity, "UTF-8"); response.setContentType("text/html;charset=utf-8"); String phone = request.getParameter("phone"); String openid = request.getParameter("openid");// String openid=result; String status=""; try { int i = new UserService().bindWechat(openid,phone); }catch (SQLException e) { status = "{status:2}"; response.getWriter().append(status); e.printStackTrace(); System.out.println("ERROR!"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * status: * 0 --- 缁戝畾澶辫触 * 1 --- 缁戝畾鎴愬姛 * 2 --- 缃戠粶寮傚父锛岃绋嶅悗鍐嶈瘯 */ if(openid!=null) { status = "{status:1}"; response.getWriter().append(status); } else { status = "{status:0}"; response.getWriter().append(status); // }}
}
微信小程序是以https方式提交数据到你的后台,如下:
wx.request({
url:
'test.php'
,
//你的后台地址
data: {
x:
''
,
y:
''
},
header: {
'content-type'
:
'application/json'
},
success:
function
(res) {
console.log(res.data)
}
})