美文网首页
微信小程序开发流程图

微信小程序开发流程图

作者: Albert新荣 | 来源:发表于2021-05-11 13:44 被阅读0次
    image.png

    图里其实说的很清楚了,清理下流程:

    1.前端调用wx.login()获取code值

    2.前端通过调用wx.getUserInfo获取iv、rawData、signature、encryptedData等加密数据,传递给后端

    3.服务器通过code请求api换回session_key和openid

    4.服务器通过前端给的rawData 加获取的session_key使用sha1加密,计算出signature1

    5.比对前端传的signature和自己算出来的signature1是否一致(防止数据不一致)

    6.用AES算法解密encryptedData里的敏感数据

    7.拿着敏感数据后做自己的逻辑

    8.通知前端登陆成功

    ** 这里只是想拿到用户的openid,则直接1,3就可以做到了。如下:
    第一步:
    通过wx.login(微信前端--小程序)接口获取code,将code传到后台

    注意:

    code的来源:是用户打开小程序的时候,随机生成的,是腾讯生成的,每个code只能使用一次,因此,理论上这个code是安全的

    
    package cn.wmyskxz.springboot.model.user;
     
    /**
     * @Author: Yangke
     * @Date: 2019/3/31 15:52
     **/
    public class WeChatLoginModel {
        String code;
     
        public String getCode() {
            return code;
        }
     
        public void setCode(String code) {
            this.code = code;
        }
    

    第二步:
    后台通过code访问微信(腾讯)接口,微信(腾讯)接口返回当前登录的信息:session_key及openid

    返回的openid是每个用户唯一的,通过这个 可以匹配 微信(腾讯)的用户 跟 我们的用户,就是我们后台通过openid来判断这个人是谁,

    UserController.java 微信小程序登录

    /**
         * 微信小程序登录
         *
         * 登录成功后,将用户身份信息及session_key存入token
         * @param model
         * @return
         */
        @ResponseBody
        @PostMapping("/weChatLogin")
        public SingleResult<String> weChatLogin(@RequestBody WeChatLoginModel model){
     
            /**
             * 登录日志:
             * id\ userid\ date\ wx_code\ createTime
             * create table loginLog (
                    id varchar(50) primary key,
                    userId varchar(50),
                    logindate date,
                    wxcode varchar(100),
                    createtime datetime
               );
             */
     
            SingleResult<String> result = new SingleResult<String>();
            //第三步:调用service.weChatLogin(model):后台检查openid是否存在,返回openid对应的用户
            WeChatLoginResult<UserAccount> loginResult = service.weChatLogin(model);
            
            //第四步:
            UserAccount user = loginResult.getUser();
            if(user == null ){
                result.setCode(0);
                result.setMessage("登录失败");
            }
            else {
                User u = new User();
                u.setId(user.getId());
                u.setPassword(user.getPassword() == null ? user.getWxopenid() : user.getPassword());
                u.setSessionKey(loginResult.getSession_key());
                String token = getToken(u);
                result.setToken(token);
                result.setCode(1);
                result.setMessage("登陆成功");
            }
     
            return result;
    
    其中:就是下面的第三步
    
    //调用service.weChatLogin(model)
    WeChatLoginResult<UserAccount> loginResult = service.weChatLogin(model);
    
    

    第三步:
    后台检查openid是否存在,
    去UserService.java

    
    @Override
        public WeChatLoginResult<UserAccount> weChatLogin(WeChatLoginModel model){
            WeChatLoginResult<UserAccount> result = null;
            try {
     
                // code  -> openid
                String urlFormat = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
                String url = String.format(urlFormat, WeChat.appId, WeChat.secret, model.getCode());
                String json = WeChat.sendGet(url);
     
                //将json字符串转化成对象
                result = JSON.parseObject(json, WeChatLoginResult.class);
     
                if(result.getErrcode() == null){
                    // 去数据库 检查 openId 是否存在 不存在就新建用户
                    UserAccount user = userAccount.wechatOpenIdIsExists(result.getOpenid());
                    if(user == null || user.getId() == null){
                        // 不存在,就是第一次登录:新建用户信息
                        user = new UserAccount();
                        user.setId(UUID.randomUUID().toString());
                        user.setWxopenid(result.getOpenid());
                        user.setLasttime(new Date());
                        userAccount.insert(user);
                    }
                    else {
                        //如果存在,就不是第一次登录,更新最后登录时间
                        user.setLasttime(new Date());
                        userAccount.updateByPrimaryKeySelective(user);
                    }
                    result.setUser(user);
     
                    // 保存登录日志
                    LoginLog log = new LoginLog();
                    log.setId(UUID.randomUUID().toString());
                    log.setCreatetime(new Date());
                    log.setLogindate(new Date());
                    log.setUserid(user.getId());
                    log.setWxcode(model.getCode());
                    loginLog.insert(log);
                }
                else {
                    System.out.println(json);
                }
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
     
            return result;
        }
    
    

    去数据库中检查openid是否存在:
    UserAccountMapper.java
    后面几部可以去看原文,转载来自[https://blog.csdn.net/qq_39474604/article/details/100019809]

    相关文章

      网友评论

          本文标题:微信小程序开发流程图

          本文链接:https://www.haomeiwen.com/subject/nrwgdltx.html