微信小程序 提审时隐藏功能;微信小程序添加微信快捷登录、随便看看(登的user用户)
This commit is contained in:
@@ -25,4 +25,5 @@ public class User implements Serializable {
|
||||
private String doorplate;
|
||||
private String permission;
|
||||
private LocalDateTime permissionTime;
|
||||
private String wxcode;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.cxyxiaomo.epp.gateway.controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
@RestController
|
||||
public class Controller {
|
||||
|
||||
@@ -10,4 +13,35 @@ public class Controller {
|
||||
public String error() {
|
||||
return "[ERROR] 500 Internal Server Error";
|
||||
}
|
||||
|
||||
/**
|
||||
* 为了通过微信的小程序审核所特别处理的
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/getConfig")
|
||||
public HashMap<String, Object> WxMiniProgramAuditSpecialHandle() {
|
||||
boolean showCode = true;
|
||||
boolean showShop = true;
|
||||
LinkedList<String> tabbarItem = new LinkedList<>();
|
||||
tabbarItem.push("pages/index/index");
|
||||
if (showCode) tabbarItem.push("pages/residents/code");
|
||||
tabbarItem.push("pages/residents/report");
|
||||
if (showShop) tabbarItem.push("pages/shop/shop");
|
||||
tabbarItem.push("pages/person/person");
|
||||
|
||||
LinkedList<String> indexItem = new LinkedList<>();
|
||||
indexItem.push("/pages/index/login");
|
||||
if (showCode) indexItem.push("/pages/residents/code");
|
||||
if (showCode) indexItem.push("scanQRCode");
|
||||
indexItem.push("/pages/residents/report");
|
||||
if (showShop) indexItem.push("/pages/shop/shop");
|
||||
indexItem.push("/pages/person/person");
|
||||
indexItem.push("/pages/person/updpwd");
|
||||
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("tabbarItem", tabbarItem);
|
||||
map.put("indexItem", indexItem);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/access/wechat")
|
||||
public class WeChatTokenController {
|
||||
@@ -47,4 +49,18 @@ public class WeChatTokenController {
|
||||
|
||||
return responseBody.bytes();
|
||||
}
|
||||
|
||||
@GetMapping(value = "/rpc/getOpenIdFromApi")
|
||||
@ResponseBody
|
||||
@SneakyThrows
|
||||
public Res getOpenIdFromApi(@RequestParam(value = "code", required = true) String code) {
|
||||
if (Objects.isNull(code) || "".equals(code)) {
|
||||
return Res.error("参数错误");
|
||||
}
|
||||
String openId = weChatTokenService.getOpenIdFromApi(code);
|
||||
if (openId == null) {
|
||||
return Res.error("微信OpenId接口调用失败,请刷新重试");
|
||||
}
|
||||
return Res.success(openId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,8 +97,8 @@ public class WeChatTokenServiceImpl implements WeChatTokenService {
|
||||
params.put("check_path", unlimitedQRCodeParam.getCheckPath());
|
||||
params.put("env_version", unlimitedQRCodeParam.getEnvVersion());
|
||||
params.put("width", unlimitedQRCodeParam.getWidth());
|
||||
params.put("auto_color",unlimitedQRCodeParam.getAutoColor());
|
||||
params.put("is_hyaline",unlimitedQRCodeParam.getIsHyaline());
|
||||
params.put("auto_color", unlimitedQRCodeParam.getAutoColor());
|
||||
params.put("is_hyaline", unlimitedQRCodeParam.getIsHyaline());
|
||||
String paramsString = JSON.toJSONString(params);
|
||||
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
@@ -108,11 +108,40 @@ public class WeChatTokenServiceImpl implements WeChatTokenService {
|
||||
.post(RequestBody.create(paramsString, MediaType.parse("application/json; charset=utf-8")))
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
// String result = response.body()response.body().string();
|
||||
// String result = response.body().string();
|
||||
|
||||
// System.out.println("headers: " + response.headers());
|
||||
// System.out.println("body: " + response.body());
|
||||
System.out.println("paramsString: " + paramsString);
|
||||
return response.body();
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public String getOpenIdFromApi(String code) {
|
||||
// refer:
|
||||
// https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
|
||||
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
|
||||
String url = String.format("https://api.weixin.qq.com/sns/jscode2session?" +
|
||||
"appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", APPID, APPSECRET, code);
|
||||
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.get()
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
ResponseBody body = response.body();
|
||||
if (body != null) {
|
||||
String jsonString = body.string();
|
||||
// {"session_key":"7DnwgV6kUALcO+j65k5Gcw==","openid":"oFzuC4pvbPzY7vI6vmP6_57iTk-U","unionid":"oZxJH1kLie37EpC4FzslnCcWDEA4"}
|
||||
|
||||
JSONObject result = JSONObject.parseObject(jsonString);
|
||||
String openId = result.getString("openid");
|
||||
System.out.println("body: " + body + ", result: " + result);
|
||||
return openId;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +82,16 @@
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- OpenFeign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 分页 -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
|
||||
@@ -3,11 +3,13 @@ package com.cxyxiaomo.epp.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;
|
||||
|
||||
// 启动类
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
public class UserProvider {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserProvider.class, args);
|
||||
|
||||
@@ -15,6 +15,7 @@ import com.cxyxiaomo.epp.common.pojo.Role;
|
||||
import com.cxyxiaomo.epp.common.pojo.User;
|
||||
import com.cxyxiaomo.epp.common.response.Res;
|
||||
import com.cxyxiaomo.epp.common.vo.UserVO;
|
||||
import com.cxyxiaomo.epp.user.rpc.WeChatTokenServiceFeign;
|
||||
import com.cxyxiaomo.epp.user.service.RoleService;
|
||||
import com.cxyxiaomo.epp.user.service.UserService;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
@@ -25,10 +26,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/user")
|
||||
@@ -40,6 +38,9 @@ public class UserController {
|
||||
@Resource
|
||||
private RoleService roleService;
|
||||
|
||||
@Resource
|
||||
private WeChatTokenServiceFeign weChatTokenService;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
@@ -50,6 +51,12 @@ public class UserController {
|
||||
@PostMapping("/login")
|
||||
@ResponseBody
|
||||
public Res login(@RequestParam("username") String username, @RequestParam("password") String password) {
|
||||
// 微信小程序 随便看看
|
||||
if (Objects.equals(username, "#fastLogin#") && Objects.equals(password, "#fastLogin#")) {
|
||||
username = "user";
|
||||
password = "user";
|
||||
}
|
||||
|
||||
User user = userService.getUserByUsername(username);
|
||||
if (user != null) {
|
||||
String passwordHash = DigestUtils.sha512Hex(password);
|
||||
@@ -65,6 +72,49 @@ public class UserController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信小程序用户登录
|
||||
*
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/wxlogin")
|
||||
@ResponseBody
|
||||
public Res wxlogin(@RequestParam("code") String code) {
|
||||
// 微信小程序 登录页面 随便看看
|
||||
if (Objects.isNull(code)) {
|
||||
return Res.error("参数错误");
|
||||
}
|
||||
|
||||
String jsonString = weChatTokenService.getOpenIdFromApi(code);
|
||||
JSONObject jsonObject = JSONObject.parseObject(jsonString);
|
||||
String openId = jsonObject.getString("data");
|
||||
if (Objects.isNull(openId) || "".equals(openId)) {
|
||||
return Res.error("微信接口异常,请重试");
|
||||
}
|
||||
|
||||
User user = userService.getUserByWxcode(openId);
|
||||
if (user == null) {
|
||||
// 创建一个新用户
|
||||
user = new User();
|
||||
user.setId(null);
|
||||
user.setUsername(UUID.randomUUID().toString().replace("-", "").substring(0, 10));
|
||||
user.setPassword(UUID.randomUUID().toString());
|
||||
user.setRealname("微信用户" + user.getUsername().substring(0, 5));
|
||||
user.setRoleId(3);
|
||||
user.setPermission("1");
|
||||
user.setWxcode(openId);
|
||||
userService.addUser(user);
|
||||
// 添加之后 user.getId() 可以获取到用户id
|
||||
// System.out.println("userId: " + user.getId());
|
||||
}
|
||||
|
||||
// 完成登录
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("userInfo", UserVO.convertFrom(user));
|
||||
return Res.success(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过用户ID获取用户信息
|
||||
*
|
||||
|
||||
@@ -19,6 +19,8 @@ public interface UserDao {
|
||||
|
||||
User getUserByUsername(String username);
|
||||
|
||||
User getUserByWxcode(String wxcode);
|
||||
|
||||
public List<User> getUserList(UserVO userVO);
|
||||
|
||||
public boolean deleteUserById(Integer userId);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.cxyxiaomo.epp.user.rpc;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient("microservice-provider-access")
|
||||
public interface WeChatTokenServiceFeign {
|
||||
/**
|
||||
* 返回值、参数要对应,方法名随意
|
||||
* 在 OpenFeign 中方法参数前如果没有注解,默认添加 @RequestBody 注解,最多只能存在一个不带注解的参数
|
||||
* 普通表单参数必须添加 @Requestparam 注解。如果变量名和参数名称对应可以不写name
|
||||
*/
|
||||
@GetMapping("/access/wechat/rpc/getOpenIdFromApi")
|
||||
String getOpenIdFromApi(@RequestParam("code") String code);
|
||||
}
|
||||
@@ -18,6 +18,10 @@ public class UserService {
|
||||
return userDao.getUserByUsername(username);
|
||||
}
|
||||
|
||||
public User getUserByWxcode(String wxcode) {
|
||||
return userDao.getUserByWxcode(wxcode);
|
||||
}
|
||||
|
||||
public User getUserById(Integer id) {
|
||||
return userDao.getUserById(id);
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cxyxiaomo.epp.user.dao.UserDao">
|
||||
<insert id="addUser" parameterType="com.cxyxiaomo.epp.common.pojo.User">
|
||||
<insert id="addUser" parameterType="com.cxyxiaomo.epp.common.pojo.User" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO user (username, `password`, realname, id_number, phone_number, role_id, building_id, doorplate,
|
||||
permission, permission_time)
|
||||
permission, permission_time, wx_code)
|
||||
VALUES (#{username}, #{password}, #{realname}, #{idNumber}, #{phoneNumber}, #{roleId}, #{buildingId},
|
||||
#{doorplate}, #{permission}, #{permissionTime})
|
||||
#{doorplate}, #{permission}, #{permissionTime}, #{wxcode})
|
||||
</insert>
|
||||
<update id="updateUser" parameterType="com.cxyxiaomo.epp.common.pojo.User">
|
||||
UPDATE user
|
||||
@@ -42,6 +42,9 @@
|
||||
<if test="permissionTime != null">
|
||||
permission_time = #{permissionTime},
|
||||
</if>
|
||||
<if test="wxcode != null and wxcode != ''">
|
||||
wx_code = #{doorplate},
|
||||
</if>
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
@@ -55,6 +58,11 @@
|
||||
from user
|
||||
where username = #{username}
|
||||
</select>
|
||||
<select id="getUserByWxcode" resultType="com.cxyxiaomo.epp.common.pojo.User">
|
||||
select *
|
||||
from user
|
||||
where wx_code = #{wxcode}
|
||||
</select>
|
||||
<select id="getUserList" resultType="com.cxyxiaomo.epp.common.pojo.User">
|
||||
select *
|
||||
from user
|
||||
|
||||
Reference in New Issue
Block a user