java实现微信小程序登录授权 (第2版_tourism)

小程序用户表

CREATE TABLE `wx_user` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `openid` varchar(28) DEFAULT NULL COMMENT '小程序的openid',
  `nick_name` varchar(100) DEFAULT NULL COMMENT '昵称',
  `real_name` varchar(100) DEFAULT NULL COMMENT '姓名',
  `headimg_url` varchar(100) DEFAULT NULL COMMENT '头像',
  `gender` tinyint(1) DEFAULT NULL COMMENT '性别  0-男、1-女',
  `country` varchar(100) DEFAULT NULL COMMENT '所在国家',
  `province` varchar(100) DEFAULT NULL COMMENT '省份',
  `city` varchar(100) DEFAULT NULL COMMENT '城市',
  `language` varchar(100) DEFAULT NULL COMMENT '语种',
  `mobile` varchar(50) DEFAULT NULL COMMENT '手机号码',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `login_time` datetime DEFAULT NULL COMMENT '登录时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='小程序用户表';

pom.xml

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>3.3.0</version>
</dependency>

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-pay</artifactId>
    <version>3.3.0</version>
</dependency>

application.yml (只作参考, 不一定用得上…)

#############微信公共接口调用###########################
WeChat:
    jscode2session: https://api.weixin.qq.com/sns/jscode2session
    appid:  wxc299.....0d0f72
    secret:  391c014566141.....f7a697a86087
    grantType:  authorization_code

#微信支付回调url
wechat.notify.url: http://....

applet:
  # 开发者应该设置成自己的wx相关信息
  wx:
    app-id: wxcaf0d...4923b
    app-secret: 6b70e4a33c00...bcf008b6bdd071
    mch-id:
    mch-key:
    notify-url:
    # 商户证书文件路径
    # 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
    key-path: xxxxx
  redis:
    #搜索次数 redis缓存有效时间 单位s
    searchlistcount: 300

UserController.java

package com.yymt.controller.applet;

import cn.hutool.core.bean.BeanUtil;
import com.yymt.common.form.LoginForm;
import com.yymt.common.utils.R;
import com.yymt.controller.applet.base.BaseController;
import com.yymt.entity.faceswap.FaceSwapAppUserEntity;
import com.yymt.service.faceswap.AppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;


/**
 * 用户表
 */
@RestController
@RequestMapping("applet/user")
@Api(tags = "App用户管理")
public class UserController extends BaseController {
   
    @Autowired
    private AppUserService appUserService;

    /**
     * 登录,主要是用来创建给后台其他平台登录,所以没有添加密码加密
     */
    @PostMapping("/authLogin")
    @ApiOperation("用户登录")
    public R authLogin(@RequestBody LoginForm loginForm) {
   
        String openId = appUserService.getOpenId(loginForm.getCode());
        if (StringUtils.isEmpty(openId)){
   
            return R.error("授权失败");
        }
        FaceSwapAppUserEntity userEntity = appUserService.queryByOpenId(openId);
        if (userEntity == null){
   
            userEntity = new FaceSwapAppUserEntity();
            userEntity.setOpenId(openId);
            userEntity.setNickName(UUID.randomUUID().toString());
        }
        BeanUtil.copyProperties(loginForm, userEntity);
        // 登录时间
        Date loginDate = new Date();
        userEntity.setLoginTime(loginDate);
        appUserService.insertOrUpdate(userEntity);
        // 生成app端token
        String token = generateToken(userEntity);
        HashMap<String, Object> result = new HashMap<>(2);
        result.put("Authorization", token);
        result.put("userInfo", userEntity);
        return R.ok().put("data", result);
    }
}

AppUserService.java

package com.yymt.service.faceswap;

import com.baomidou.mybatisplus.service.IService;
import com.yymt.entity.faceswap.FaceSwapAppUserEntity;

/**
 * 用户表
 */
public interface AppUserService extends IService<FaceSwapAp
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值