<button class="confirm-btn" @click="wxlogin" :disabled="logining">登录</button>

//对应逻辑

methods: {

			wxlogin(){

			uni.getUserProfile({

					desc:"获取资料",

					success: (res) => {

						console.log(res)

						this.encryptedData=res.encryptedData

						this.rawData=res.rawData

						this.iv=res.iv

						this.signature=res.signature

						this.avatarUrl=res.userInfo.avatarUrl

						this.name=res.userInfo.nickName

					}

				});//获取用户资料

				uni.login({

				  provider: 'weixin',

				  success: (res) => {

					

					 this.code=res.code;

					// console.log(this.code);

					

				  }

				});

				console.log(this.name)

				console.log(this.avatarUrl)

				//发送请求

				uni.request({

					url:"http://localhost:8081/api/dsxs/company/token",

					method:"POST",

				 data: {

					// encryptedData:this.encryptedData,

					// rawData:this.rawData,

					// iv:this.iv,

					// signature:this.signature,

					code:this.code,

					img:this.avatarUrl,

					name:this.name

				    },

					success: (e) => {

						

						console.log("向后端请求成功");

					}

					

				})

			},

后端可以通过之前申请的appID、appSecret和前端传来的code获取到用户的openID与session_key

创建springboot项目,添加依赖

<dependency>

            <groupId>com.squareup.okhttp3</groupId>

            <artifactId>okhttp</artifactId>

            <version>4.7.2</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

        <!--mybatis-plus-->

        <dependency>

            <groupId>com.baomidou</groupId>

            <artifactId>mybatis-plus-boot-starter</artifactId>

            <version>3.3.1</version>

        </dependency>

        <!--mysql-->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

        </dependency>

        <!--lombok用来简化实体类-->

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>



        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>com.baomidou</groupId>

            <artifactId>mybatis-plus-generator</artifactId>

            <version>3.3.1</version>

        </dependency>



        <dependency>

            <groupId>org.apache.velocity</groupId>

            <artifactId>velocity-engine-core</artifactId>

            <version>2.0</version>

        </dependency>

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>fastjson</artifactId>

            <version>1.2.62</version>

        </dependency>

        <dependency>

            <groupId>io.jsonwebtoken</groupId>

            <artifactId>jjwt</artifactId>

            <version>0.9.1</version>

        </dependency>

        <dependency>

            <groupId>org.apache.httpcomponents</groupId>

            <artifactId>httpclient</artifactId>

            <version>4.3.1</version>

        </dependency>

        <dependency>

            <groupId>commons-io</groupId>

            <artifactId>commons-io</artifactId>

            <version>2.6</version>

        </dependency>

        <dependency>

            <groupId>commons-lang</groupId>

            <artifactId>commons-lang</artifactId>

            <version>2.6</version>

        </dependency>

配置好实体类与数据相关代码后,将自己的appID、appSecret放在配置文件中

wx.open.app_id=xxxxxxxx

wx.open.app_secret=xxxxxxxxx

创建获取配置信息类

@Component

//@PropertySource("classpath:application.properties")

public class ConstantPropertiesUtil implements InitializingBean {

    //读取配置文件并赋值

    @Value("${wx.open.app_id}")

    private String appId;

    @Value("${wx.open.app_secret}")

    private String appSecret;



    public static String WX_OPEN_APP_ID;

    public static String WX_OPEN_APP_SECRET;



    @Override

    public void afterPropertiesSet() throws Exception {

        WX_OPEN_APP_ID = appId;

        WX_OPEN_APP_SECRET = appSecret;

    }

}

编写用户登录控制层,这里我的实现逻辑是根据前端传来的code,获取用户openID作为用户的唯一标识。首先在数据库中查询有无当前用户,要有创建token返回给前端对应信息。因为前端写的是一次性将code与用户信息全传过来,用户点击登陆后会跳转到授权页面,用户若点击拒绝那么用户信息将不会传过来,只有code,这时我的处理逻辑是判断有无用户信息,若没有不存如数据库,这里由于用户点击授权会有时间响应所以做了一个短暂的休眠处理。

public class LoginController {

    @Autowired

    private UserService userService;



    @PostMapping("token")

    public R login(@RequestBody LoginBO loginBO) throws IOException, InterruptedException {

    //拼接对应信息

        StringBuffer baseAccessTokenUrl = new StringBuffer()

                .append("https://api.weixin.qq.com/sns/jscode2session")

                .append("?appid=%s")

                .append("&secret=%s")

                .append("&js_code=%s")

                .append("&grant_type=authorization_code");

        String accessTokenUrl = String.format(baseAccessTokenUrl.toString(),

                ConstantPropertiesUtil.WX_OPEN_APP_ID,

                ConstantPropertiesUtil.WX_OPEN_APP_SECRET,

                loginBO.getCode());

         //像网站发送请求

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder().url(accessTokenUrl).build();

        Response response = client.newCall(request).execute();

        //请求成功会返回对应信息,解析为json

        if (response.isSuccessful()){

            String body = response.body().string();

            JSONObject jsonObject = JSONObject.parseObject(body);

            String session_key = jsonObject.getString("session_key");

            String openid = jsonObject.getString("openid");

            HashMap<String, Object> map = new HashMap<>();

            Thread.sleep(1000);

            //判断数据中有无当前用户

            User userInfo = userService.getByOpenId(openid);

            if (userInfo==null){

                User user = new User();

                user.setOpenid(openid);



                if (loginBO.getName().equals("")){