微信小程序获取openid

微信小程序APPid对应自己的,否则获取不到openid
app.js

App({
 ROOT_URL: 'http://localhost:8080/',         这个是服务器路径
  appId:"这个位置要写自己的appid",
  secret:"这个位置是自己的小程序秘钥",
 })

index.js

//接口路径
var baseURL = getApp().ROOT_URL

        getOpenid: function() {
          var that = this;
          // 登录主页
          wx.login({
            success: res => {
              if (res.code) {
                wx.request({
                  url: baseURL + 'jsp/appSubscriber/getOpenId',//对应后台接口
                  data: {
                    appid: appId,
                    secret: secret,
                    js_code: res.code
                  },
                  method: 'GET',
                  success: function(result) {
                    //console.log(result.data.result);//打印后台返回的信息
                    wx.setStorageSync("openid", result.data.result.openid);
                   
                  }
                })
              } else {
                console.log('登录失败! ' + res.errMsg)
              }
            }
          })
        },
    })

后台代码

AppSubscriberController层

@Controller
@RequestMapping({ "/jsp/appSubscriber" })
public class AppSubscriberController {
	@Autowired
	private AppSubscriberService appSubscriberService;
/**
  *获取openID
 *@return
 */
@ResponseBody
@RequestMapping(value = "getOpenId", method = RequestMethod.GET)
public RestResult getOpenId(HttpServletRequest request, Subscriber subscriber) {
	RestResult restResult = null;
	
	Map<String, Object> result = appSubscriberService.getOpenId(request, subscriber);

	try {
		restResult = new RestResult(RestStatus.SUCCESS, "添加成功", result);
	} catch (Exception e) {
		e.printStackTrace();
		restResult = new RestResult(RestStatus.ERROR, "添加异常");
	}

	return restResult;
}
}

Service层

@Service
@Transactional	//事务处理
public class AppSubscriberService {
	@Autowired 
	private AppSubscriberMapper appSubscriberMapper;
/**
 * 通过微信接口获取openid
 * @param request
 * @param subscriber
 * @return
 */
public Map<String, Object> getOpenId(HttpServletRequest request, Subscriber subscriber) {
	Map<String, Object> result = new HashMap<String, Object>();
	String appid = request.getParameter("appid");	//获取小程序的appid
	String secret = request.getParameter("secret");	//获取小程序的秘钥
	String js_code = request.getParameter("js_code");	
	
	// 固定写法;获取微信的接口路径
	String url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
	String param = "appid=" + appid + "&secret=" + secret + "&js_code=" + js_code
			+ "&grant_type=authorization_code";
	// 接收传回来的值
	JSONObject resultJson = JSONObject.fromObject(UsrMngController.sendPost(url, param));
	// 获取openid
	String openid = resultJson.getString("openid");

	request.getSession().setAttribute("openid", openid);
	result.put("openid", openid);
	return result;

}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值