Преглед на файлове

电话号码一键登录

chejianzheng преди 4 месеца
родител
ревизия
0cdd6a66bd
променени са 1 файла, в които са добавени 66 реда и са изтрити 0 реда
  1. 66 0
      src/main/java/com/wxxcx/index/WxxcxIndexController.java

+ 66 - 0
src/main/java/com/wxxcx/index/WxxcxIndexController.java

@@ -286,6 +286,72 @@ public class WxxcxIndexController<AesException extends Throwable> {
         return r;
     }
 
+    @GetMapping("/loginByPhone")
+    @ApiOperation(value = "一键获取手机号登录")
+    public R loginByPhone(String code, String openId, String unionId) {
+        R r = JSONUtil.getSuccessMsg(null);
+        if (StringUtils.isNotEmpty(code)) {
+            String phoneNumber = "";
+            // 获取 access_token
+            String accessToken = getAccessToken();
+            // 检查 access_token 是否有效
+            if (StringUtils.isEmpty(accessToken)) {
+                return JSONUtil.getErrorMsg("获取 access_token 失败,请检查配置");
+            }
+            // 请求参数
+            JSONObject param = new JSONObject();
+            param.put("code", code);
+            // 请求获取手机号的接口
+            String res = HttpUtil.request("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken, param.toJSONString(), false, null, null);
+            // 处理返回的结果
+            if (StringUtils.isNotEmpty(res)) {
+                try {
+                    JSONObject resJson = JSONObject.parseObject(res);
+                    if (resJson.containsKey("phone_info") && resJson.getJSONObject("phone_info").containsKey("phoneNumber")) {
+                        phoneNumber = resJson.getJSONObject("phone_info").getString("phoneNumber");
+
+                        MiniUserVO user = new MiniUserVO();
+                        List<MiniUserVO> users = baseDAO.queryAllByCondition(MiniUserVO.class, " and phone='" + phoneNumber + "' ", null);
+                        if (users.size() == 0) {
+                            user.setOpenid(openId);
+                            user.setPhone(phoneNumber);
+                            user.setDelete_tag(0);
+                            baseDAO.insertAll(user);
+
+                            // 查询是否存在该 openid
+                            List<WxRelatiion> list = baseDAO.getJdbcTemplate().query(
+                                    "SELECT * FROM wx_relation WHERE openid = ?",
+                                    new Object[]{openId},
+                                    new BeanPropertyRowMapper<>(WxRelatiion.class)
+                            );
+                            if (list.size() <= 0) {
+                                String insertSql = "INSERT INTO wx_relation (openid, unionid,fwhopenid) VALUES (?, ?,?)";
+                                baseDAO.getJdbcTemplate().update(insertSql, openId, unionId, null);
+                            }
+                        } else {
+                            r = JSONUtil.getErrorMsg("该号码已注册");
+                        }
+
+                    } else {
+                        phoneNumber = "未返回手机号";
+                    }
+                } catch (Exception e) {
+                    phoneNumber = "解析失败,异常:" + e.getMessage();
+                }
+            } else {
+                phoneNumber = "接口调用失败,未获取到手机号";
+            }
+
+            // 将获取到的手机号放入返回数据中
+            r.data.put("phoneNumber", phoneNumber);
+        } else {
+            r.data.put("phoneNumber", "无效的 code 参数");
+        }
+
+        return r;
+    }
+
+
     /**
      * 检测用户是否拥有设备
      *