7.java项目-尚医通(7)

前端代码:https://github.com/wyj41/yygh_html.git
后端代码:https://github.com/wyj41/yygh_parent.git

登录需求

1,登录采取弹出层的形式

2,登录方式:
(1)手机号码+手机验证码
(2)微信扫描

3,无注册界面,第一次登录根据手机号判断系统是否存在,如果不存在则自动注册

4,微信扫描登录成功必须绑定手机号码,即:第一次扫描成功后绑定手机号,以后登录扫描直接登录成功

5,网关统一判断登录状态,如何需要登录,页面弹出登录层

1.登录

1.1 搭建service_user模块

在service模块下创建service_user子模块

1.1.1 修改配置

修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>service</artifactId>
        <groupId>com.myproject</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service_user</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.myproject</groupId>
            <artifactId>service_cmn_client</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

添加配置文件application.yml

#服务端口
server:
  port: 8203

spring:
  application:
    name: service-user # 服务名
  profiles:
    active: dev # 环境设置:dev、test、prod
  # mysql数据库连接
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.50.224/yygh_user?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
  #返回json的全局时间格式
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  # nacos服务地址
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
  redis:
    host: 192.168.50.224
    port: 6379
    database: 0
    timeout: 1800000
    lettuce:
      pool:
        max-active: 10
        max-wait: -1
        max-idle: 5
        min-idle: 0

#配置mapper xml文件的路径
mybatis-plus:
  mapper-locations: classpath:com/myproject/yygh/user/mapper/xml/*.xml

1.1.2 启动类

在service_user模块中创建com.myproject.yygh.user包

创建com.myproject.yygh.user.ServiceUserApplication启动类

package com.myproject.yygh.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = "com.myproject")
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.myproject")
public class ServiceUserApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ServiceUserApplication.class, args);
    }
}

1.1.3 配置网关

在service_gateway模块的配置文件中添加

- id: service-user
  uri: lb://service-user
  predicates:
    - name: Path
      args:
        - /*/user/**

1.1.4 添加用户基础类

说明:由于实体对象没有逻辑,我们已经统一导入
model模块中的com.myproject.yygh.model.user.UserInfo

1.添加Mapper
添加com.myproject.yygh.user.mapper.UserInfoMapper

package com.myproject.yygh.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.myproject.yygh.model.user.UserInfo;

public interface UserInfoMapper extends BaseMapper<UserInfo> {
    
    
}

添加com/myproject/yygh/user/mapper/xml/UserInfoMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.myproject.yygh.user.mapper.UserInfoMapper">

</mapper>

2.添加service接口及实现类
添加com.myproject.yygh.user.service.UserInfoService接口

package com.myproject.yygh.user.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.myproject.yygh.model.user.UserInfo;

public interface UserInfoService extends IService<UserInfo> {
    
    
}

添加com.myproject.yygh.user.service.impl.UserInfoServiceImpl接口实现

package com.myproject.yygh.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myproject.yygh.model.user.UserInfo;
import com.myproject.yygh.user.mapper.UserInfoMapper;
import com.myproject.yygh.user.service.UserInfoService;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    
    
}

3.添加controller
添加com.myproject.yygh.user.api.UserInfoApiController类

package com.myproject.yygh.user.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    
    
}

4.添加配置类
com.myproject.yygh.user.config.UserConfig

package com.myproject.yygh.user.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.myproject.yygh.user.mapper")
public class UserConfig {
    
    
}

1.2 登录api接口

添加service接口与实现
在UserInfoService类添加接口

//用户手机号登录接口
Map<String, Object> loginUser(LoginVo loginVo);

在UserInfoServiceImpl类实现接口

//用户手机号登录接口
@Override
public Map<String, Object> loginUser(LoginVo loginVo) {
    
    
    //从loginVo获取输入的手机号和验证码
    String phone = loginVo.getPhone();
    String code = loginVo.getCode();

    //判断手机号和验证码是否为空
    if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
    
    
        throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }

    //TODO 判断手机验证码和输入的验证码是否一致

    //判断是否第一次登录,根据手机号查询数据库,如果不存在想用手机号就是第一次登录
    QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
    wrapper.eq("phone",phone);
    UserInfo userInfo = baseMapper.selectOne(wrapper);
    if(userInfo == null){
    
    //第一次使用这个手机号登录
        //添加信息到数据库
        userInfo = new UserInfo();
        userInfo.setName("");
        userInfo.setPhone(phone);
        userInfo.setStatus(1);
        baseMapper.insert(userInfo);
    }

    //校验是否被禁用
    if(userInfo.getStatus() == 0){
    
    
        throw new YyghException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
    }

    //不是第一次,直接登录
    //返回登录信息
    //返回登录用户名
    //返回token信息
    Map<String,Object> map = new HashMap<>();
    String name = userInfo.getName();
    if(StringUtils.isEmpty(name)){
    
    
        name = userInfo.getNickName();
    }
    if(StringUtils.isEmpty(name)){
    
    
        name = userInfo.getPhone();
    }
    map.put("name",name);

    //TODO token生成
    map.put("token","");
    return map;
}

说明:
1、验证码先注释,后续校验
2、登录成功生成token,后续讲解

添加controller接口
在UserInfoApiController类添加方法

@Autowired
private UserInfoService userInfoService;

//用户手机号登录接口
@PostMapping("login")
public Result login(@RequestBody LoginVo loginVo){
    
    
    Map<String,Object> info  = userInfoService.loginUser(loginVo);
    return Result.ok(info);
}

1.3 生成token

1.3.1 JWT介绍

JWT(Json Web Token)是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准。

JWT的声明一般被用来在身份提供者和服务提供者间传递被认证的用户身份信息,以便于从资源服务器获取资源。比如用在用户登录上

JWT最重要的作用就是对 token信息的防伪作用。

JWT的原理,
一个JWT由三个部分组成:公共部分、私有部分、签名部分。最后由这三者组合进行base64编码得到JWT。

1、 公共部分
主要是该JWT的相关配置参数,比如签名的加密算法、格式类型、过期时间等等。
Key=ATGUIGU

2、 私有部分
用户自定义的内容,根据实际需要真正要封装的信息。
userInfo{用户的Id,用户的昵称nickName}

3、 签名部分
SaltiP: 当前服务器的Ip地址!{linux 中配置代理服务器的ip}
主要用户对JWT生成字符串的时候,进行加密{盐值}

最终组成 key+salt+userInfo ==> token!
base64编码,并不是加密,只是把明文信息变成了不可见的字符串。但是其实只要用一些工具就可以把base64编码解成明文,所以不要在JWT中放入涉及私密的信息。

1.3.2 集成JWT

在common-util模块pom.xml添加依赖(已添加)

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
</dependency>

版本已在yygh-parent父模块pom.xml添加

在common-util模块编写JwtHelper类
com.myproject.yygh.common.helper.JwtHelper

package com.myproject.yygh.common.helper;

import com.alibaba.excel.util.StringUtils;
import io.jsonwebtoken.*;

import java.util.Date;

public class JwtHelper {
    
    
    //过期时间
    private static long tokenExpiration = 24*60*60*1000;

    //签名密钥
    private static String tokenSignKey = "123456";

    //根据参数生成token
    public static String createToken(Long userId, String userName) {
    
    
        String token = Jwts.builder()
                .setSubject("YYGH-USER")
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                .claim("userId", userId)
                .claim("userName", userName)
                .signWith(SignatureAlgorithm.HS512, tokenSignKey)
                .compressWith(CompressionCodecs.GZIP)
                .compact();
        return token;
    }

    //根据token字符串得到用户id
    public static Long getUserId(String token) {
    
    
        if(StringUtils.isEmpty(token)) return null;
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        Integer userId = (Integer)claims.get("userId");
        return userId.longValue();
    }

    //根据token字符串得到用户名称
    public static String getUserName(String token) {
    
    
        if(StringUtils.isEmpty(token)) return "";
        Jws<Claims> claimsJws
                = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        return (String)claims.get("userName");
    }

    public static void main(String[] args) {
    
    
        String token = JwtHelper.createToken(1L, "55");
        System.out.println(token);
        System.out.println(JwtHelper.getUserId(token));
        System.out.println(JwtHelper.getUserName(token));
    }
}

说明:执行main方法测试

1.3.3 完善登录service接口

修改UserInfoServiceImpl类登录方法

//用户手机号登录接口
    @Override
    public Map<String, Object> loginUser(LoginVo loginVo) {
    
    
        ...

        //jwtshen生成token
        String token = JwtHelper.createToken(userInfo.getId(), name);
        map.put("token",token);
        return map;
    }

使用Swagger测试接口
http://localhost:8203/swagger-ui.html

请求体:

{
    
    
  "code": "123456",
  "phone": "15970422532"
}

1.4 阿里云短信

获取阿里云短信服务api(测试用)
参考:https://blog.csdn.net/weixin_46931860/article/details/124512435

1.4.1 搭建service-msm模块

在service模块中创建service_msm模块

修改配置
修改pom.xml

<dependencies>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
    </dependency>
</dependencies>

添加配置文件application.yml
其中accessKeyId和secret填写自己的

# 服务端口
server:
  port: 8204

spring:
  application:
    name: service-msm
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  redis:
    host: 192.168.50.224
    port: 6379
    database: 0
    timeout: 1800000
    lettuce:
      pool:
        max-active: 10
        max-wait: -1
        max-idle: 5
        min-idle: 0
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848

aliyun:
  sms:
    regionId: default
    accessKeyId: ***
    secret: ***

启动类

package com.myproject.yygh.msm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
@EnableDiscoveryClient
@ComponentScan(basePackages = {
    
    "com.myproject"})
public class ServiceMsmApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ServiceMsmApplication.class, args);
    }
}

配置网关

- id: service-msm
  uri: lb://service-msm
  predicates:
    - name: Path
      args:
        - /*/msm/**

1.4.2 封装注册短信验证码接口

1.添加配置类和工具类
com.myproject.yygh.msm.utils.ConstantPropertiesUtils

package com.myproject.yygh.msm.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConstantPropertiesUtils implements InitializingBean {
    
    

    @Value("${aliyun.sms.regionId}")
    private String regionId;

    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.sms.secret}")
    private String secret;

    public static String REGION_Id;
    public static String ACCESS_KEY_ID;
    public static String SECRECT;

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        REGION_Id=regionId;
        ACCESS_KEY_ID=accessKeyId;
        SECRECT=secret;
    }
}

com.myproject.yygh.msm.utils.RandomUtil

package com.myproject.yygh.msm.utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class RandomUtil {
    
    

    private static final Random random = new Random();

    private static final DecimalFormat fourdf = new DecimalFormat("0000");

    private static final DecimalFormat sixdf = new DecimalFormat("000000");

    public static String getFourBitRandom() {
    
    
        return fourdf.format(random.nextInt(10000));
    }

    public static String getSixBitRandom() {
    
    
        return sixdf.format(random.nextInt(1000000));
    }

    /**
     * 给定数组,抽取n个数据
     * @param list
     * @param n
     * @return
     */
    public static ArrayList getRandom(List list, int n) {
    
    

        Random random = new Random();

        HashMap<Object, Object> hashMap = new HashMap<Object, Object>();

        // 生成随机数字并存入HashMap
        for (int i = 0; i < list.size(); i++) {
    
    

            int number = random.nextInt(100) + 1;

            hashMap.put(number, i);
        }

        // 从HashMap导入数组
        Object[] robjs = hashMap.values().toArray();

        ArrayList r = new ArrayList();

        // 遍历数组并打印数据
        for (int i = 0; i < n; i++) {
    
    
            r.add(list.get((int) robjs[i]));
            System.out.print(list.get((int) robjs[i]) + "\t");
        }
        System.out.print("\n");
        return r;
    }
}

2. 封装service接口和实现类
com.myproject.yygh.msm.service.MsmService

package com.myproject.yygh.msm.service;

public interface MsmService {
    
    
    //发送手机验证码
    boolean send(String phone, String code);
}

com.myproject.yygh.msm.service.impl.MsmServiceImpl
模板code填自己的

package com.myproject.yygh.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.myproject.yygh.msm.service.MsmService;
import com.myproject.yygh.msm.utils.ConstantPropertiesUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    
    
    //发送手机验证码
    @Override
    public boolean send(String phone, String code) {
    
    
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)){
    
    
            return false;
        }
        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "阿里云短信测试");
        //模板code
        request.putQueryParameter("TemplateCode", "***");
        //验证码  使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
    
    
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
    
    
            e.printStackTrace();
        } catch (ClientException e) {
    
    
            e.printStackTrace();
        }
        return false;
    }
}

** 封装controller接口**

package com.myproject.yygh.msm.controller;

import com.myproject.yygh.common.result.Result;
import com.myproject.yygh.msm.service.MsmService;
import com.myproject.yygh.msm.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/msm")
public class MsmApiController {
    
    
    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //发送手机验证码
    @GetMapping("send/{phone}")
    public Result sendCode(@PathVariable String phone){
    
    
        //从redis获取验证码,如果获取到,返回ok
        //key 手机号 value 验证码
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
    
    
            return Result.ok();
        }

        //如果从redis获取不到
        //生成验证码
        code = RandomUtil.getSixBitRandom();
        //调用service方法,通过整合短信服务进行发送
        boolean isSend = msmService.send(phone,code);
        //生成验证码放到redis里面,设置有效时间
        if(isSend){
    
    
            redisTemplate.opsForValue().set(phone,code,2, TimeUnit.MINUTES);
            return Result.ok();
        }else {
    
    
            return Result.fail().message("发送短信失败");
        }

    }
}

完善登录service接口
UserInfoServiceImpl

@Autowired
private RedisTemplate<String,String> redisTemplate;

//用户手机号登录接口
@Override
public Map<String, Object> loginUser(LoginVo loginVo) {
    
    
    ...

    //判断手机验证码和输入的验证码是否一致
    String redisCode = redisTemplate.opsForValue().get(phone);
    if(!code.equals(redisCode)){
    
    
        throw new YyghException(ResultCodeEnum.CODE_ERROR);
    }

    ...
}

测试短信发送功能
http://localhost:8204/swagger-ui.html
手机号填自己授权的手机号

1.5 登录前端

1.5.1 封装api请求

创建/api/userInfo.js

import request from '@/utils/request'

const api_name = `/api/user`

export default {
    
    
  login(userInfo) {
    
    
    return request({
    
    
      url: `${
      
      api_name}/login`,
      method: `post`,
      data: userInfo
    })
  }
}

创建/api/msm.js

import request from '@/utils/request'

const api_name = `/api/msm`

export default {
    
    
  sendCode(phone) {
    
    
    return request({
    
    
      url: `${
      
      api_name}/send/${
      
      phone}`,
      method: `get`
    })
  }
}

1.5.2 安装cookie

登录成功,我们要把用户信息记录在cookie里面
命令行执行:

npm install js-cookie
1.5.3 添加登录组件

登录层是一个公共层,因此我们把它放在头部组件里面
修改layouts/myheader.vue文件

<template>
  <div class="header-container">
    <div class="wrapper">
      <!-- logo -->
      <div class="left-wrapper v-link selected">
        <img style="width: 50px" width="50" height="50" src="~assets/images/logo.png">
        <span class="text">尚医通 预约挂号统一平台</span>
      </div>
      <!-- 搜索框 -->
      <div class="search-wrapper">
        <div class="hospital-search animation-show">
          <el-autocomplete
            class="search-input small"
            prefix-icon="el-icon-search"
            v-model="state"
            :fetch-suggestions="querySearchAsync"
            placeholder="点击输入医院名称"
            @select="handleSelect"
          >
            <span slot="suffix" class="search-btn v-link highlight clickable selected">搜索 </span>
          </el-autocomplete>
        </div>
      </div>
      <!-- 右侧 -->
      <div class="right-wrapper">
        <span class="v-link clickable">帮助中心</span>
        <span v-if="name == ''" class="v-link clickable" @click="showLogin()" id="loginDialog">登录/注册</span>
        <el-dropdown v-if="name != ''" @command="loginMenu">
        <span class="el-dropdown-link">
          {
    
    {
    
     name }}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
          <el-dropdown-menu class="user-name-wrapper" slot="dropdown">
            <el-dropdown-item command="/user">实名认证</el-dropdown-item>
            <el-dropdown-item command="/order">挂号订单</el-dropdown-item>
            <el-dropdown-item command="/patient">就诊人管理</el-dropdown-item>
            <el-dropdown-item command="/logout" divided>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <!-- 登录弹出层 -->
    <el-dialog :visible.sync="dialogUserFormVisible" style="text-align: left;" top="50px" :append-to-body="true"  width="960px" @close="closeDialog()">
      <div class="container">

        <!-- 手机登录 #start -->
        <div class="operate-view" v-if="dialogAtrr.showLoginType === 'phone'">
          <div class="wrapper" style="width: 100%">
            <div class="mobile-wrapper" style="position: static;width: 70%">
              <span class="title">{
    
    {
    
     dialogAtrr.labelTips }}</span>
              <el-form>
                <el-form-item>
                  <el-input v-model="dialogAtrr.inputValue" :placeholder="dialogAtrr.placeholder" :maxlength="dialogAtrr.maxlength" class="input v-input">
                    <span slot="suffix" class="sendText v-link" v-if="dialogAtrr.second > 0">{
    
    {
    
     dialogAtrr.second }}s </span>
                    <span slot="suffix" class="sendText v-link highlight clickable selected" v-if="dialogAtrr.second == 0" @click="getCodeFun()">重新发送 </span>
                  </el-input>
                </el-form-item>
              </el-form>
              <div class="send-button v-button" @click="btnClick()"> {
    
    {
    
     dialogAtrr.loginBtn }}</div>
            </div>
            <div class="bottom">
              <div class="wechat-wrapper" @click="weixinLogin()"><span
                class="iconfont icon"></span></div>
              <span class="third-text"> 第三方账号登录 </span></div>
          </div>
        </div>
        <!-- 手机登录 #end -->

        <!-- 微信登录 #start -->
        <div class="operate-view"  v-if="dialogAtrr.showLoginType === 'weixin'" >
          <div class="wrapper wechat" style="height: 400px">
            <div>
              <div id="weixinLogin"></div>
            </div>
            <div class="bottom wechat" style="margin-top: -80px;">
              <div class="phone-container">
                <div class="phone-wrapper"  @click="phoneLogin()"><span
                  class="iconfont icon"></span></div>
                <span class="third-text"> 手机短信验证码登录 </span></div>
            </div>
          </div>
        </div>
        <!-- 微信登录 #end -->

        <div class="info-wrapper">
          <div class="code-wrapper">
            <div><img src="//img.114yygh.com/static/web/code_login_wechat.png" class="code-img">
              <div class="code-text"><span class="iconfont icon"></span>微信扫一扫关注
              </div>
              <div class="code-text"> “快速预约挂号”</div>
            </div>
            <div class="wechat-code-wrapper"><img
              src="//img.114yygh.com/static/web/code_app.png"
              class="code-img">
              <div class="code-text"> 扫一扫下载</div>
              <div class="code-text"> “预约挂号”APP</div>
            </div>
          </div>
          <div class="slogan">
            <div>xxxxxx官方指定平台</div>
            <div>快速挂号 安全放心</div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import cookie from 'js-cookie'
import Vue from 'vue'

import userInfoApi from '@/api/userInfo'
import smsApi from '@/api/msm'
import hospitalApi from '@/api/hosp'

const defaultDialogAtrr = {
    
    
  showLoginType: 'phone', // 控制手机登录与微信登录切换

  labelTips: '手机号码', // 输入框提示

  inputValue: '', // 输入框绑定对象
  placeholder: '请输入您的手机号', // 输入框placeholder
  maxlength: 11, // 输入框长度控制

  loginBtn: '获取验证码', // 登录按钮或获取验证码按钮文本

  sending: true,      // 是否可以发送验证码
  second: -1,        // 倒计时间  second>0 : 显示倒计时 second=0 :重新发送 second=-1 :什么都不显示
  clearSmsTime: null  // 倒计时定时任务引用 关闭登录层清除定时任务
}
export default {
    
    
  data() {
    
    
    return {
    
    
      userInfo: {
    
    
        phone: '',
        code: '',
        openid: ''
      },

      dialogUserFormVisible: false,
      // 弹出层相关属性
      dialogAtrr:defaultDialogAtrr,

      name: '' // 用户登录显示的名称
    }
  },
  created() {
    
    
    this.showInfo()
  },
  methods: {
    
    
    // 绑定登录或获取验证码按钮
    btnClick() {
    
    
      // 判断是获取验证码还是登录
      if(this.dialogAtrr.loginBtn == '获取验证码') {
    
    
        this.userInfo.phone = this.dialogAtrr.inputValue

        // 获取验证码
        this.getCodeFun()
      } else {
    
    
        // 登录
        this.login()
      }
    },
    // 绑定登录,点击显示登录层
    showLogin() {
    
    
      this.dialogUserFormVisible = true

      // 初始化登录层相关参数
      this.dialogAtrr = {
    
     ...defaultDialogAtrr }
    },
    // 登录
    login() {
    
    
      this.userInfo.code = this.dialogAtrr.inputValue

      if(this.dialogAtrr.loginBtn == '正在提交...') {
    
    
        this.$message.error('重复提交')
        return;
      }
      if (this.userInfo.code == '') {
    
    
        this.$message.error('验证码必须输入')
        return;
      }
      if (this.userInfo.code.length != 6) {
    
    
        this.$message.error('验证码格式不正确')
        return;
      }
      this.dialogAtrr.loginBtn = '正在提交...'
      userInfoApi.login(this.userInfo).then(response => {
    
    
        console.log(response.data)
        // 登录成功 设置cookie
        this.setCookies(response.data.name, response.data.token)
      }).catch(e => {
    
    
        this.dialogAtrr.loginBtn = '马上登录'
      })
    },

    setCookies(name, token) {
    
    
      cookie.set('token', token, {
    
     domain: 'localhost' })
      cookie.set('name', name, {
    
     domain: 'localhost' })
      window.location.reload()
    },
    // 获取验证码
    getCodeFun() {
    
    
      if (!(/^1[34578]\d{9}$/.test(this.userInfo.phone))) {
    
    
        this.$message.error('手机号码不正确')
        return;
      }

      // 初始化验证码相关属性
      this.dialogAtrr.inputValue = ''
      this.dialogAtrr.placeholder = '请输入验证码'
      this.dialogAtrr.maxlength = 6
      this.dialogAtrr.loginBtn = '马上登录'

      // 控制重复发送
      if (!this.dialogAtrr.sending) return;

      // 发送短信验证码
      this.timeDown();
      this.dialogAtrr.sending = false;
      smsApi.sendCode(this.userInfo.phone).then(response => {
    
    
        this.timeDown();
      }).catch(e => {
    
    
        this.$message.error('发送失败,重新发送')
        // 发送失败,回到重新获取验证码界面
        this.showLogin()
      })
    },
    // 倒计时
    timeDown() {
    
    
      if(this.clearSmsTime) {
    
    
        clearInterval(this.clearSmsTime);
      }
      this.dialogAtrr.second = 60;

      this.dialogAtrr.labelTips = '验证码已发送至' + this.userInfo.phone
      this.clearSmsTime = setInterval(() => {
    
    
        --this.dialogAtrr.second;
        if (this.dialogAtrr.second < 1) {
    
    
          clearInterval(this.clearSmsTime);
          this.dialogAtrr.sending = true;
          this.dialogAtrr.second = 0;
        }
      }, 1000);
    },
    // 关闭登录层
    closeDialog() {
    
    
      if(this.clearSmsTime) {
    
    
        clearInterval(this.clearSmsTime);
      }
    },

    showInfo() {
    
    
      let token = cookie.get('token')
      if (token) {
    
    
        this.name = cookie.get('name')
        console.log(this.name)
      }
    },
    loginMenu(command) {
    
    
      if('/logout' == command) {
    
    
        cookie.set('name', '', {
    
    domain: 'localhost'})
        cookie.set('token', '', {
    
    domain: 'localhost'})

        //跳转页面
        window.location.href = '/'
      } else {
    
    
        window.location.href = command
      }
    },
    handleSelect(item) {
    
    
      window.location.href = '/hospital/' + item.hoscode
    },
    weixinLogin() {
    
    
      this.dialogAtrr.showLoginType = 'weixin'
    },
    phoneLogin() {
    
    
      this.dialogAtrr.showLoginType = 'phone'
      this.showLogin()
    }
  }
}
</script>

1.6 登录全局事件

目前登录层在myheader组件里面,登录按钮也在同一个组件里面,我们点击登录,调用showLogin()方法即可

目前的问题是,我们在预约挂号页面,选择科室去挂号时我们需要判断当前是否登录,如果登录可以进入下一个页面;如果没有登录需要显示登录层,那么这个问题怎么解决呢,我们不能直接调用头部登录方法,我们目前的组件是包含在nuxt里面的

问题总是能够解决的,其实很简单,我们可以注册一个全局登录事件,当需要登录层是,我们发送一个登录事件,头部监听登录事件,然后我们触发登录按钮的点击事件即可打开登录层,下面我们来试试

1.6.1 头部注册和监听登录事件

修改myheader.vue组件

1.引入Vue

import Vue from 'vue'

2.注册与监听事件

mounted() {
    window.loginEvent = new Vue();
    // 监听登录事件
    loginEvent.$on('loginDialogEvent', function () {
      document.getElementById("loginDialog").click();
    })
    // 触发事件,显示登录层:loginEvent.$emit('loginDialogEvent')
},

1.6.2 预约挂号页面调整

修改/pages/hosp/_hoscode.vue组件

1.引入cookie

import cookie from 'js-cookie'

2.修改方法

schedule(depcode) {
  // 登录判断
  let token = cookie.get('token')
  if (!token) {
    loginEvent.$emit('loginDialogEvent')
    return
  }
  window.location.href = '/hospital/schedule?hoscode=' + this.hospital.hoscode + "&depcode="+ depcode
}

说明:清除cookie,点击科室测试

2.用户认证与网关整合

思路:

  1. 所有请求都会经过服务网关,服务网关对外暴露服务,在网关进行统一用户认证;
  2. 既然要在网关进行用户认证,网关得知道对哪些url进行认证,所以我们得对ur制定规则
  3. Api接口异步请求的,我们采取url规则匹配,如:/api/**/auth/**,如凡是满足该规则的都必须用户认证

2.1 调整server-gateway模块

在服务网关添加fillter
com.myproject.yygh.filter.AuthGlobalFilter

package com.myproject.yygh.filter;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.myproject.yygh.common.helper.JwtHelper;
import com.myproject.yygh.common.result.Result;
import com.myproject.yygh.common.result.ResultCodeEnum;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 全局Filter,统一处理会员登录与外部不允许访问的服务
 * </p>
 *
 * @author qy
 * @since 2019-11-21
 */
@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {
    
    

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    
    
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getURI().getPath();
        System.out.println("==="+path);

        //内部服务接口,不允许外部访问
        if(antPathMatcher.match("/**/inner/**", path)) {
    
    
            ServerHttpResponse response = exchange.getResponse();
            return out(response, ResultCodeEnum.PERMISSION);
        }

        Long userId = this.getUserId(request);
        //api接口,异步请求,校验用户必须登录
        if(antPathMatcher.match("/api/**/auth/**", path)) {
    
    
            if(StringUtils.isEmpty(userId)) {
    
    
                ServerHttpResponse response = exchange.getResponse();
                return out(response, ResultCodeEnum.LOGIN_AUTH);
            }
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
    
    
        return 0;
    }

    /**
     * api接口鉴权失败返回数据
     * @param response
     * @return
     */
    private Mono<Void> out(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
    
    
        Result result = Result.build(null, resultCodeEnum);
        byte[] bits = JSONObject.toJSONString(result).getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = response.bufferFactory().wrap(bits);
        //指定编码,否则在浏览器中会中文乱码
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }

    /**
     * 获取当前登录用户id
     * @param request
     * @return
     */
    private Long getUserId(ServerHttpRequest request) {
    
    
        String token = "";
        List<String> tokenList = request.getHeaders().get("token");
        if(null  != tokenList) {
    
    
            token = tokenList.get(0);
        }
        if(!StringUtils.isEmpty(token)) {
    
    
            return JwtHelper.getUserId(token);
        }
        return null;
    }
}

2.2 调整前端yygh-site

请求服务器端接口时我们默认带上token,需要登录的接口如果token没有或者token过期,服务器端会返回208状态,然后发送登录事件打开登录弹出层登录

修改utils/request.js文件

import axios from 'axios'
import {
    
     MessageBox, Message } from 'element-ui'
import cookie from 'js-cookie'

// 创建axios实例
const service = axios.create({
    
    
  baseURL: 'http://localhost:81',
  timeout: 15000 // 请求超时时间
})
// http request 拦截器
service.interceptors.request.use(
  config => {
    
    
    //判断cookie是否有token值
    if(cookie.get('token')){
    
    
      //token值放到headers里面
      config.headers['token']=cookie.get('token')
    }
    return config
  },
  err => {
    
    
    return Promise.reject(err)
  })
// http response 拦截器
service.interceptors.response.use(
  response => {
    
    
    //状态码是208
    if (response.data.code === 208){
    
    
      //弹出登录输入框
      loginEvent.$emit('loginDialogEvent')
      return
    }
    if (response.data.code !== 200) {
    
    
      Message({
    
    
        message: response.data.message,
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(response.data)
    } else {
    
    
      return response.data
    }
  },
  error => {
    
    
    return Promise.reject(error.response)
  })
export default service

3.微信登录

3.1 OAuth2

OAuth2针对特定问题解决方案:
1.开放系统间授权问题
2.单点登录问题

OAuth2的应用
1.微服务安全
现代微服务中系统微服务化以及应用的形态和设备类型增多,不能用传统的登录方式
核心的技术不是用户名和密码,而是token,由AuthServer颁发token,用户使用token进行登录

2.社交登录

3.2 前期准备

(老师已经提供,因此不需要操作)
1、注册
微信开放平台:https://open.weixin.qq.com
2、邮箱激活
3、完善开发者资料
4、开发者资质认证
准备营业执照,1-2个工作日审批、300元
5、创建网站应用
提交审核,7个工作日审批
6、内网穿透
ngrok的使用

*授权流程
参考文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=e547653f995d8f402704d5cb2945177dc8aa4e7e&lang=zh_CN

3.3 服务器端开发

操作模块:service-user
说明:微信登录二维码我们是以弹出层的形式打开,不是以页面形式,所以做法是不一样的,参考如下链接,上面有相关弹出层的方式
https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

因此我们的操作步骤为:
第一步我们通过接口把对应参数返回页面;
第二步在头部页面启动打开微信登录二维码;
第三步处理登录回调接口;
第四步回调返回页面通知微信登录层回调成功
第五步如果是第一次扫描登录,则绑定手机号码,登录成功
接下来我们根据步骤,一步一步实现

3.4 返回微信登录参数

操作模块:service-user

添加配置
在application.yml中添加配置

wx:
  open:
    app_id: wxed9954c01bb89b47
    app_secret: a7482517235173ddb4083788de60b90e
    redirect_url: http://guli.shop/api/ucenter/wx/callback
yygh:
  baseUrl: http://localhost:3000

添加配置类
com.myproject.yygh.user.utils.ConstantWxPropertiesUtil

package com.myproject.yygh.user.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConstantWxPropertiesUtil implements InitializingBean {
    
    

    @Value("${wx.open.app_id}")
    private String appId;

    @Value("${wx.open.app_secret}")
    private String appSecret;

    @Value("${wx.open.redirect_url}")
    private String redirectUrl;

    @Value("${yygh.baseUrl}")
    private String yyghBaseUrl;

    public static String WX_OPEN_APP_ID;
    public static String WX_OPEN_APP_SECRET;
    public static String WX_OPEN_REDIRECT_URL;

    public static String YYGH_BASE_URL;

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        WX_OPEN_APP_ID = appId;
        WX_OPEN_APP_SECRET = appSecret;
        WX_OPEN_REDIRECT_URL = redirectUrl;
        YYGH_BASE_URL = yyghBaseUrl;
    }
}

添加接口
com.myproject.yygh.user.controller.WeixinApiController

package com.myproject.yygh.user.controller;

import com.myproject.yygh.common.result.Result;
import com.myproject.yygh.user.utils.ConstantWxPropertiesUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

//微信操作的接口
@Controller
@RequestMapping("/api/ucenter/wx")
public class WeixinApiController {
    
    
    //1.生成微信扫描二维码
    //返回生成二维码需要参数
    @GetMapping("getLoginParam")
    @ResponseBody
    public Result genQrConnect(){
    
    
        try {
    
    
            Map<String,Object> map = new HashMap<>();
            map.put("appid", ConstantWxPropertiesUtil.WX_OPEN_APP_ID);
            map.put("scope","snsapi_login");
            String wxOpenRedirectUrl = ConstantWxPropertiesUtil.WX_OPEN_REDIRECT_URL;
            wxOpenRedirectUrl = URLEncoder.encode(wxOpenRedirectUrl, "utf-8");
            map.put("redirect_uri",wxOpenRedirectUrl);
            map.put("state",System.currentTimeMillis()+"");
            return Result.ok(map);
        } catch (UnsupportedEncodingException e) {
    
    
            throw new RuntimeException(e);
        }
    }

    //2.回调的方法,得到扫描人信息
}

3.5 前端显示登录二维码

3.5.1 封装api请求

创建/api/wexin.js文件

import request from "~/utils/request";

const api_name = `/api/ucenter/wx`
export default {
    
    
  getLoginParam(dictCode){
    
    
    return request({
    
    
      url:`${
      
      api_name}/getLoginParam`,
      method:'get'
    })
  }
}

3.5.2 修改组件

修改layouts/myheader.vue文件,添加微信二维码登录逻辑
1.引入api

import weixinApi from '@/api/weixin'

2.引入微信js
修改mounted

mounted() {
    
    
    window.loginEvent = new Vue();
    // 监听登录事件
    loginEvent.$on('loginDialogEvent', function () {
    
    
      document.getElementById("loginDialog").click();
    })
    // 触发事件,显示登录层:loginEvent.$emit('loginDialogEvent')

    //初始化微信js
    const script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = 'https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js'
    document.body.appendChild(script)
},

3.实例化微信JS对象
修改weixinLogin方法

weixinLogin() {
    
    
  this.dialogAtrr.showLoginType = 'weixin'
  //初始化微信相关参数
  weixinApi.getLoginParam().then(response => {
    
    
    var obj = new WxLogin({
    
    
      self_redirect: true,
      id: 'weixinLogin', // 需要显示的容器id
      appid: response.data.appid, // 公众号appid wx*******
      scope: response.data.scope, // 网页默认即可
      redirect_uri: response.data.redirect_uri, // 授权成功后回调的url
      state: response.data.state, // 可设置为简单的随机数加session用来校验
      style: 'black', // 提供"black"、"white"可选。二维码的样式
      href: '' // 外部css文件url,需要https
    })
  })
},

4.添加网关
在service-user的参数中增加 /*/ucenter/**

- id: service-user
  uri: lb://service-user
  predicates:
    - name: Path
      args:
        - /*/user/**
        - /*/ucenter/**

测试
出现问题:redirect_uri参数错误
解决方法:
修改service_user的配置文件application.yml

#端口号改为8160
server:
  port: 8160

#修改redirect_url 
wx:
  open:
    app_id: wxed9954c01bb89b47
    app_secret: a7482517235173ddb4083788de60b90e
    redirect_url: http://localhost:8160/api/ucenter/wx/callback

3.6 处理微信回调

3.6.1 添加httpclient工具类

添加com.myproject.yygh.user.utils.HttpClientUtils类

package com.myproject.yygh.user.utils;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HttpClientUtils {
    
    

    public static final int connTimeout=10000;
    public static final int readTimeout=10000;
    public static final String charset="UTF-8";
    private static HttpClient client = null;

    static {
    
    
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(128);
        cm.setDefaultMaxPerRoute(128);
        client = HttpClients.custom().setConnectionManager(cm).build();
    }

    public static String postParameters(String url, String parameterStr) throws ConnectTimeoutException, SocketTimeoutException, Exception{
    
    
        return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout);
    }

    public static String postParameters(String url, String parameterStr,String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception{
    
    
        return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout);
    }

    public static String postParameters(String url, Map<String, String> params) throws ConnectTimeoutException,
            SocketTimeoutException, Exception {
    
    
        return postForm(url, params, null, connTimeout, readTimeout);
    }

    public static String postParameters(String url, Map<String, String> params, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException,
            SocketTimeoutException, Exception {
    
    
        return postForm(url, params, null, connTimeout, readTimeout);
    }

    public static String get(String url) throws Exception {
    
    
        return get(url, charset, null, null);
    }

    public static String get(String url, String charset) throws Exception {
    
    
        return get(url, charset, connTimeout, readTimeout);
    }

    /**
     * 发送一个 Post 请求, 使用指定的字符集编码.
     *
     * @param url
     * @param body RequestBody
     * @param mimeType 例如 application/xml "application/x-www-form-urlencoded" a=1&b=2&c=3
     * @param charset 编码
     * @param connTimeout 建立链接超时时间,毫秒.
     * @param readTimeout 响应超时时间,毫秒.
     * @return ResponseBody, 使用指定的字符集编码.
     * @throws ConnectTimeoutException 建立链接超时异常
     * @throws SocketTimeoutException  响应超时
     * @throws Exception
     */
    public static String post(String url, String body, String mimeType,String charset, Integer connTimeout, Integer readTimeout)
            throws ConnectTimeoutException, SocketTimeoutException, Exception {
    
    
        HttpClient client = null;
        HttpPost post = new HttpPost(url);
        String result = "";
        try {
    
    
            if (StringUtils.isNotBlank(body)) {
    
    
                HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset));
                post.setEntity(entity);
            }
            // 设置参数
            RequestConfig.Builder customReqConf = RequestConfig.custom();
            if (connTimeout != null) {
    
    
                customReqConf.setConnectTimeout(connTimeout);
            }
            if (readTimeout != null) {
    
    
                customReqConf.setSocketTimeout(readTimeout);
            }
            post.setConfig(customReqConf.build());

            HttpResponse res;
            if (url.startsWith("https")) {
    
    
                // 执行 Https 请求.
                client = createSSLInsecureClient();
                res = client.execute(post);
            } else {
    
    
                // 执行 Http 请求.
                client = HttpClientUtils.client;
                res = client.execute(post);
            }
            result = IOUtils.toString(res.getEntity().getContent(), charset);
        } finally {
    
    
            post.releaseConnection();
            if (url.startsWith("https") && client != null&& client instanceof CloseableHttpClient) {
    
    
                ((CloseableHttpClient) client).close();
            }
        }
        return result;
    }


    /**
     * 提交form表单
     *
     * @param url
     * @param params
     * @param connTimeout
     * @param readTimeout
     * @return
     * @throws ConnectTimeoutException
     * @throws SocketTimeoutException
     * @throws Exception
     */
    public static String postForm(String url, Map<String, String> params, Map<String, String> headers, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException,
            SocketTimeoutException, Exception {
    
    

        HttpClient client = null;
        HttpPost post = new HttpPost(url);
        try {
    
    
            if (params != null && !params.isEmpty()) {
    
    
                List<NameValuePair> formParams = new ArrayList<NameValuePair>();
                Set<Entry<String, String>> entrySet = params.entrySet();
                for (Entry<String, String> entry : entrySet) {
    
    
                    formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
                post.setEntity(entity);
            }

            if (headers != null && !headers.isEmpty()) {
    
    
                for (Entry<String, String> entry : headers.entrySet()) {
    
    
                    post.addHeader(entry.getKey(), entry.getValue());
                }
            }
            // 设置参数
            Builder customReqConf = RequestConfig.custom();
            if (connTimeout != null) {
    
    
                customReqConf.setConnectTimeout(connTimeout);
            }
            if (readTimeout != null) {
    
    
                customReqConf.setSocketTimeout(readTimeout);
            }
            post.setConfig(customReqConf.build());
            HttpResponse res = null;
            if (url.startsWith("https")) {
    
    
                // 执行 Https 请求.
                client = createSSLInsecureClient();
                res = client.execute(post);
            } else {
    
    
                // 执行 Http 请求.
                client = HttpClientUtils.client;
                res = client.execute(post);
            }
            return IOUtils.toString(res.getEntity().getContent(), "UTF-8");
        } finally {
    
    
            post.releaseConnection();
            if (url.startsWith("https") && client != null
                    && client instanceof CloseableHttpClient) {
    
    
                ((CloseableHttpClient) client).close();
            }
        }
    }

    /**
     * 发送一个 GET 请求
     */
    public static String get(String url, String charset, Integer connTimeout,Integer readTimeout)
            throws ConnectTimeoutException,SocketTimeoutException, Exception {
    
    

        HttpClient client = null;
        HttpGet get = new HttpGet(url);
        String result = "";
        try {
    
    
            // 设置参数
            Builder customReqConf = RequestConfig.custom();
            if (connTimeout != null) {
    
    
                customReqConf.setConnectTimeout(connTimeout);
            }
            if (readTimeout != null) {
    
    
                customReqConf.setSocketTimeout(readTimeout);
            }
            get.setConfig(customReqConf.build());

            HttpResponse res = null;

            if (url.startsWith("https")) {
    
    
                // 执行 Https 请求.
                client = createSSLInsecureClient();
                res = client.execute(get);
            } else {
    
    
                // 执行 Http 请求.
                client = HttpClientUtils.client;
                res = client.execute(get);
            }

            result = IOUtils.toString(res.getEntity().getContent(), charset);
        } finally {
    
    
            get.releaseConnection();
            if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) {
    
    
                ((CloseableHttpClient) client).close();
            }
        }
        return result;
    }

    /**
     * 从 response 里获取 charset
     */
    @SuppressWarnings("unused")
    private static String getCharsetFromResponse(HttpResponse ressponse) {
    
    
        // Content-Type:text/html; charset=GBK
        if (ressponse.getEntity() != null  && ressponse.getEntity().getContentType() != null && ressponse.getEntity().getContentType().getValue() != null) {
    
    
            String contentType = ressponse.getEntity().getContentType().getValue();
            if (contentType.contains("charset=")) {
    
    
                return contentType.substring(contentType.indexOf("charset=") + 8);
            }
        }
        return null;
    }

    /**
     * 创建 SSL连接
     * @return
     * @throws GeneralSecurityException
     */
    private static CloseableHttpClient createSSLInsecureClient() throws GeneralSecurityException {
    
    
        try {
    
    
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
    
    
                public boolean isTrusted(X509Certificate[] chain,String authType) throws CertificateException {
    
    
                    return true;
                }
            }).build();

            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {
    
    

                @Override
                public boolean verify(String arg0, SSLSession arg1) {
    
    
                    return true;
                }

                @Override
                public void verify(String host, SSLSocket ssl)
                        throws IOException {
    
    
                }

                @Override
                public void verify(String host, X509Certificate cert)
                        throws SSLException {
    
    
                }

                @Override
                public void verify(String host, String[] cns,
                                   String[] subjectAlts) throws SSLException {
    
    
                }
            });
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();

        } catch (GeneralSecurityException e) {
    
    
            throw e;
        }
    }
}

3.6.2 添加service方法

UserInfoService

//根据openid获取userinfo
UserInfo selectWxInfoOpenId(String openid);

UserInfoServiceImpl

//根据openid获取userinfo
@Override
public UserInfo selectWxInfoOpenId(String openid) {
    
    
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("openid",openid);
    UserInfo userInfo = baseMapper.selectOne(queryWrapper);
    return userInfo;
}

3.6.3 添加回调接口,获取用户信息

@Autowired
private UserInfoService userInfoService;

//2.回调的方法,得到扫描人信息
@GetMapping("callback")
public String callback(String code,String state){
    
    
    //第一步 获取临时票据code
    System.out.println("code:" + code);
    //第二步 拿着code和微信id和密钥,请求微信固定地址,得到两个值
    //使用code和appid以及appscrect换区access_token
    StringBuffer baseAccessTokenUrl = new StringBuffer()
            .append("https://api.weixin.qq.com/sns/oauth2/access_token")
            .append("?appid=%s")
            .append("&secret=%s")
            .append("&code=%s")
            .append("&grant_type=authorization_code");

    String accessTokenUrl = String.format(baseAccessTokenUrl.toString(),
            ConstantWxPropertiesUtil.WX_OPEN_APP_ID,
            ConstantWxPropertiesUtil.WX_OPEN_APP_SECRET,
            code);

    //使用httpclient请求这个地址
    try {
    
    
        String accesstokenInfo = HttpClientUtils.get(accessTokenUrl);
        System.out.println("accesstokenInfo:" + accesstokenInfo);
        //从返回的字符串获取两个值 openid 和 access_token
        JSONObject jsonObject = JSONObject.parseObject(accesstokenInfo);
        String access_token = jsonObject.getString("access_token");
        String openid = jsonObject.getString("openid");

        //判断数据库是否存在微信的扫码人信息
        //根据openid判断
        UserInfo userInfo = userInfoService.selectWxInfoOpenId(openid);
        if(userInfo == null){
    
    //数据库不存在
            //第三步 使用openid 和 access_token请求微信地址,得到扫码人信息
            String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
                    "?access_token=%s" +
                    "&openid=%s";
            String userInfoUrl = String.format(baseUserInfoUrl, access_token, openid);
            String resultInfo = HttpClientUtils.get(userInfoUrl);
            System.out.println("resultInfo:" + resultInfo);
            JSONObject resultUserInfoJson = JSONObject.parseObject(resultInfo);
            //解析用户信息
            //用户昵称
            String nickname = resultUserInfoJson.getString("nickname");
            //用户头像
            String headimgurl = resultUserInfoJson.getString("headimgurl");

            //获取扫码人信息添加数据库
            userInfo = new UserInfo();
            userInfo.setNickName(nickname);
            userInfo.setOpenid(openid);
            userInfo.setStatus(1);
            userInfoService.save(userInfo);
        }

        //返回name和token的字符串
        Map<String, Object> map = new HashMap<>();
        String name = userInfo.getName();
        if(StringUtils.isEmpty(name)) {
    
    
            name = userInfo.getNickName();
        }
        if(StringUtils.isEmpty(name)) {
    
    
            name = userInfo.getPhone();
        }
        map.put("name", name);
        //判断userInfo是否有手机号,如果手机号为空,返回openid
        //如果手机号不为空,返回openid值为空字符串
        //前端判断,如果openid不为空,绑定手机号,如果openid为空,不需要绑定手机号
        if(StringUtils.isEmpty(userInfo.getPhone())) {
    
    
            map.put("openid", userInfo.getOpenid());
        } else {
    
    
            map.put("openid", "");
        }
        //使用jwt生成token字符串
        String token = JwtHelper.createToken(userInfo.getId(), name);
        map.put("token", token);
        //跳转到前端页面
        return "redirect:" + ConstantWxPropertiesUtil.YYGH_BASE_URL + "/weixin/callback?token="+map.get("token")+"&openid="+map.get("openid")+"&name="+URLEncoder.encode((String) map.get("name"),"utf-8");
    } catch (Exception e) {
    
    
        throw new RuntimeException(e);
    }
}

3.6.4 回调返回页面

根据返回路径/weixin/callback,我们创建组件/weixin/callback.vue

<template>
  <!-- header -->
  <div>
  </div>
  <!-- footer -->
</template>
<script>
export default {
  data() {
    return {
    }
  },
  mounted() {
    let token = this.$route.query.token
    let name = this.$route.query.name
    let openid = this.$route.query.openid
    // 调用父vue方法
    window.parent['loginCallback'](name, token, openid)
  }
}
</script>

说明:在页面我们就能够接收到返回来的参数

3.6.5 父组件定义回调方法

在myheader.vue添加方法

mounted() {
    
    
    window.loginEvent = new Vue();
    // 监听登录事件
    loginEvent.$on('loginDialogEvent', function () {
    
    
      document.getElementById("loginDialog").click();
    })
    // 触发事件,显示登录层:loginEvent.$emit('loginDialogEvent')

    //初始化微信js
    const script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = 'https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js'
    document.body.appendChild(script)

    // 微信登录回调处理
    let self = this;
    window["loginCallback"] = (name,token, openid) => {
    
    
      self.loginCallback(name, token, openid);
    }
  },
//微信回调的方法
loginCallback(name, token, openid) {
    
    
  // 打开手机登录层,绑定手机号,改逻辑与手机登录一致
  if(openid != '') {
    
    
    this.userInfo.openid = openid
    this.showLogin()
  } else {
    
    
    this.setCookies(name, token)
  }
},

3.6.6 服务器绑定手机号码

页面绑定手机号码会把openid传递过来,我们根据openid找到用户信息,然后绑定手机号码

修改UserInfoServiceImpl类登录方法

//用户手机号登录接口
@Override
public Map<String, Object> loginUser(LoginVo loginVo) {
    
    
    //从loginVo获取输入的手机号和验证码
    String phone = loginVo.getPhone();
    String code = loginVo.getCode();

    //判断手机号和验证码是否为空
    if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
    
    
        throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }
    //判断手机验证码和输入的验证码是否一致
    String redisCode = redisTemplate.opsForValue().get(phone);
    if(!code.equals(redisCode)){
    
    
        throw new YyghException(ResultCodeEnum.CODE_ERROR);
    }

    //绑定手机号
    UserInfo userInfo = null;
    if(!StringUtils.isEmpty(loginVo.getOpenid())) {
    
    
        userInfo = this.selectWxInfoOpenId(loginVo.getOpenid());
        if(null != userInfo) {
    
    
            userInfo.setPhone(loginVo.getPhone());
            this.updateById(userInfo);
        } else {
    
    
            throw new YyghException(ResultCodeEnum.DATA_ERROR);
        }
    }

    //如果userInfo为空,则进行正常手机登录
    if(userInfo == null){
    
    
        //判断是否第一次登录,根据手机号查询数据库,如果不存在想用手机号就是第一次登录
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        userInfo = baseMapper.selectOne(wrapper);
        if(userInfo == null){
    
    //第一次使用这个手机号登录
            //添加信息到数据库
            userInfo = new UserInfo();
            userInfo.setName("");
            userInfo.setPhone(phone);
            userInfo.setStatus(1);
            baseMapper.insert(userInfo);
        }
    }
    ...
}

测试微信登录功能
微信扫码登录后还需手机绑定

猜你喜欢

转载自blog.csdn.net/hutc_Alan/article/details/126128962