|
@@ -1,146 +0,0 @@
|
|
|
-package com.dgtly.api.controller;
|
|
|
-
|
|
|
-import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
|
-import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.dgtly.common.annotation.ApiNoCheckSign;
|
|
|
-import com.dgtly.common.core.controller.BaseController;
|
|
|
-import com.dgtly.common.core.domain.AjaxResult;
|
|
|
-import com.dgtly.common.core.domain.ResultType;
|
|
|
-import com.dgtly.common.utils.security.EncryptPassWordClass;
|
|
|
-import com.dgtly.system.domain.SysUser;
|
|
|
-import com.dgtly.system.service.ISysUserService;
|
|
|
-import com.dgtly.wxsystem.domain.WxappSysuser;
|
|
|
-import com.dgtly.wxsystem.service.IWxappSysuserService;
|
|
|
-import io.swagger.annotations.Api;
|
|
|
-import io.swagger.annotations.ApiImplicitParam;
|
|
|
-import io.swagger.annotations.ApiOperation;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
-
|
|
|
-import java.util.Map;
|
|
|
-@ApiNoCheckSign
|
|
|
-@Api(tags = "微信api登录相关接口")
|
|
|
-@RestController
|
|
|
-public class WxAuthController extends BaseController {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private WxMaService wxService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private IWxappSysuserService wxappSysuserService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ISysUserService sysUserService;
|
|
|
- /**
|
|
|
- * 微信登录
|
|
|
- */
|
|
|
- @ApiOperation("微信登录")
|
|
|
- @ApiImplicitParam(name = "json",paramType="body",required = true
|
|
|
- ,value = "请求的json示例{code:1},返回值301未绑定用户")
|
|
|
- @PostMapping("login_by_weixin")
|
|
|
- public Object loginByWeixin() {
|
|
|
- try {
|
|
|
- Map<String,Object> param = this.getJsonMap();
|
|
|
- if(!param.containsKey("code")){
|
|
|
- return AjaxResult.error(ResultType.LESSPARAM);
|
|
|
- }
|
|
|
- String code = param.get("code").toString();
|
|
|
- String sessionKey = null;
|
|
|
- String openId = null;
|
|
|
- WxMaJscode2SessionResult resultData = this.wxService.getUserService().getSessionInfo(code);
|
|
|
- sessionKey = resultData.getSessionKey();
|
|
|
- openId = resultData.getOpenid();
|
|
|
- if(sessionKey == null || openId == null){
|
|
|
- return AjaxResult.error();
|
|
|
- }
|
|
|
- //利用微信openid查询相关用户信息。
|
|
|
- WxappSysuser wxappSysuser = wxappSysuserService.selectWxappSysuserById(openId);
|
|
|
- //若微信用户不存在
|
|
|
- if(wxappSysuser==null||wxappSysuser.getUserId()==null){
|
|
|
- //新增微信小程序用户,并返回前端要求绑定账户密码
|
|
|
- return AjaxResult.error(301,"微信用户未绑定相关人员").putKV("openId",openId);
|
|
|
- }
|
|
|
- SysUser sysUser =sysUserService.selectUserById(wxappSysuser.getUserId());
|
|
|
-
|
|
|
- try{
|
|
|
- if(param.containsKey("userInfo")){
|
|
|
- WxappSysuser paramWxuser = ((JSONObject)param.get("userInfo")).toJavaObject(WxappSysuser.class);
|
|
|
- wxappSysuser.setAvatarUrl(paramWxuser.getAvatarUrl());
|
|
|
- wxappSysuser.setGender(paramWxuser.getGender());
|
|
|
- wxappSysuser.setNickname(paramWxuser.getNickname());
|
|
|
- if(sysUser.getAvatar()==null||sysUser.getAvatar().equals("")){
|
|
|
- sysUser.setAvatar(wxappSysuser.getAvatarUrl());
|
|
|
- sysUserService.updateUserInfo(sysUser);
|
|
|
- }
|
|
|
- wxappSysuserService.updateWxappSysuser(wxappSysuser);
|
|
|
-
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- logger.error("微信登录时更新用户信息发生异常",e);
|
|
|
- }
|
|
|
- return AjaxResult.success(sysUser);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- logger.error("微信登录发生错误",e);
|
|
|
- }
|
|
|
- return AjaxResult.error();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @ApiOperation("微信信息与系统信息绑定")
|
|
|
- @ApiImplicitParam(name = "json",paramType="body",required = true
|
|
|
- ,value = "请求的json示例:" +
|
|
|
- "{username:13311111111,password:13311111111,openId:,userInfo}" +
|
|
|
- "错误码303 账号不存在 302账号密码错误")
|
|
|
- @PostMapping("bind_by_weixin")
|
|
|
- public Object bindByWeixin() {
|
|
|
- try {
|
|
|
- Map<String,Object> param = this.getJsonMap();
|
|
|
- if(!param.containsKey("openId")&&
|
|
|
- !param.containsKey("username")&&
|
|
|
- !param.containsKey("password")&&
|
|
|
- !param.containsKey("userInfo")){
|
|
|
- return AjaxResult.error(ResultType.LESSPARAM);
|
|
|
- }
|
|
|
- String username=param.get("username").toString();
|
|
|
- String password=param.get("password").toString();
|
|
|
- SysUser sysUser = sysUserService.selectUserByLoginName(username);
|
|
|
- if(sysUser==null){
|
|
|
- return AjaxResult.error(303,"账号不存在");
|
|
|
- }
|
|
|
- if(!sysUser.getPassword().equals(EncryptPassWordClass.encryptPassword(username,password,sysUser.getSalt()))){
|
|
|
- return AjaxResult.error(302,"账号或密码错误");
|
|
|
- }
|
|
|
- String openid=param.get("openId").toString();
|
|
|
- WxappSysuser wxappSysuser = new WxappSysuser();
|
|
|
- wxappSysuser.setOpenId(openid);
|
|
|
- wxappSysuser.setUserId(sysUser.getUserId());
|
|
|
- wxappSysuserService.insertWxappSysuser(wxappSysuser);
|
|
|
-
|
|
|
-
|
|
|
- try{
|
|
|
- if(param.containsKey("userInfo")){
|
|
|
- WxappSysuser paramWxuser = ((JSONObject)param.get("userInfo")).toJavaObject(WxappSysuser.class);
|
|
|
- wxappSysuser.setAvatarUrl(paramWxuser.getAvatarUrl());
|
|
|
- wxappSysuser.setGender(paramWxuser.getGender());
|
|
|
- wxappSysuser.setNickname(paramWxuser.getNickname());
|
|
|
- if(sysUser.getAvatar()==null||sysUser.getAvatar().equals("")){
|
|
|
- sysUser.setAvatar(wxappSysuser.getAvatarUrl());
|
|
|
- sysUserService.updateUserInfo(sysUser);
|
|
|
- }
|
|
|
- wxappSysuserService.updateWxappSysuser(wxappSysuser);
|
|
|
-
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- logger.error("微信登录时更新用户信息发生异常",e);
|
|
|
- }
|
|
|
-
|
|
|
- return AjaxResult.success(sysUser);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- logger.error("微信登录发生错误",e);
|
|
|
- }
|
|
|
- return AjaxResult.error();
|
|
|
- }
|
|
|
-}
|