微信登录、商品浏览

一、HttpClient

1.1、介绍

微信登录、商品浏览_第1张图片

1.2、入门案例

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
public class HttpClientTest {

    /**
     * 测试通过HttpClientTest发送GET方式的请求
     */
    @Test
    public void testGet() throws Exception{
        //创建HttpClientTest对象
        CloseableHttpClient httpClient = HttpClients.createDefault ();
        //创建请求对象
        HttpGet httpGet = new HttpGet ("http://localhost:8080/user/shop/status");

        //发送请求,接受响应结果
        CloseableHttpResponse response = httpClient.execute ( httpGet );
        //获取服务端返回的状态码并解析
        int statusCode = response.getStatusLine ().getStatusCode ();
        System.out.println ("服务端返回的状态码:"+statusCode);
        HttpEntity entity = response.getEntity ();
        String body = EntityUtils.toString ( entity );
        System.out.println ("服务端返回的数据为:"+body);
        //关闭资源
        response.close ();
        httpClient.close ();
    }

    /**
     * 测试通过HttpClientTest发送POST方式的请求
     */
    @Test
    public void testPost() throws Exception{
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault ();
        //创建请求对象
        HttpPost httpPost = new HttpPost ("http://localhost:8080/admin/employee/login");
        JSONObject jsonObject = new JSONObject ();
        jsonObject.put ( "username","admin" );
        jsonObject.put ( "password","123456" );
        StringEntity entity = new StringEntity (jsonObject.toString ());
        //指定请求的编码方式
        entity.setContentEncoding ( "utf-8" );
        ///数据格式
        entity.setContentType ( "application/json" );
        httpPost.setEntity ( entity );
        //发送请求
        CloseableHttpResponse response = httpClient.execute ( httpPost );
        //解析返回结果
        int statusCode = response.getStatusLine ().getStatusCode ();//获得响应码
        System.out.println ("响应码为:"+statusCode);
        //解析响应的数据
        HttpEntity entity1 = response.getEntity ();
        String body = EntityUtils.toString ( entity1 );
        //关闭资源
        response.close ();
        httpClient.close ();
    }
}

二、微信小程序开发

2.1、介绍

微信登录、商品浏览_第2张图片

微信登录、商品浏览_第3张图片

微信登录、商品浏览_第4张图片

2.2、准备工作

微信登录、商品浏览_第5张图片

2.3、入门案例

微信登录、商品浏览_第6张图片

微信登录、商品浏览_第7张图片

三、微信登录

3.1、需求分析和设计

微信登录、商品浏览_第8张图片

微信登录、商品浏览_第9张图片

微信登录、商品浏览_第10张图片

3.2、代码开发

微信登录、商品浏览_第11张图片

微信登录、商品浏览_第12张图片

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.exception.LoginFailedException;
import com.sky.mapper.UserMapper;
import com.sky.properties.WeChatProperties;
import com.sky.service.UserService;
import com.sky.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private WeChatProperties weChatProperties;
    @Autowired
    private UserMapper userMapper;
    public  static final String WX_LOGIN ="https://api.weixin.qq.com/sns/jscode2session";//微信服务接口地址
    /**
     * 微信登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public User wxLogin(UserLoginDTO userLoginDTO) {
        String openid = getOpenId ( userLoginDTO.getCode () );
        //调用微信接口服务,获得当前微信用户的openid
//        Map map = new HashMap<> ();
//        map.put ( "appid",weChatProperties.getAppid () );
//        map.put ( "secret",weChatProperties.getSecret () );
//        map.put ( "js_code",userLoginDTO.getCode () );
//        map.put ( "grant_type","authorization_code" );
//        String json = HttpClientUtil.doGet ( WX_LOGIN, map );
//        JSONObject jsonObject = JSON.parseObject ( json );
//        String openid = jsonObject.getString ( "openid" );
        //判断openid是否为空,如果为空表示登陆失败,抛出业务异常
        if(openid==null){
            throw new LoginFailedException ( MessageConstant.LOGIN_FAILED );
        }

        //判断当前用户是否为新用户
        User user = userMapper.getByOpenid ( openid );
        //如果是新用户,自动完成注册
        if (user==null){
             user = User.builder ()
                    .openid ( openid )
                    .createTime ( LocalDateTime.now () )
                    .build ();
            userMapper.insert(user);

        }

        //返回用户对象
        return user;


    }
    //调用微信接口服务,获取用户的openid
    private String getOpenId(String code){
        //调用微信接口服务,获得当前微信用户的openid
        Map map = new HashMap<> ();
        map.put ( "appid",weChatProperties.getAppid () );
        map.put ( "secret",weChatProperties.getSecret () );
        map.put ( "js_code",code );
        map.put ( "grant_type","authorization_code" );
        String json = HttpClientUtil.doGet ( WX_LOGIN, map );
        JSONObject jsonObject = JSON.parseObject ( json );
        String openid = jsonObject.getString ( "openid" );
        return openid;
    }

}

你可能感兴趣的:(微信)