|
@@ -0,0 +1,171 @@
|
|
|
+package com.caimei.controller.user;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.caimei.entity.CmOperationUser;
|
|
|
+import com.caimei.entity.WxJsonModel;
|
|
|
+import com.caimei.service.user.LoginService;
|
|
|
+import com.caimei.utils.HttpRequest;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 登录
|
|
|
+ */
|
|
|
+@Controller
|
|
|
+@RequestMapping("/login")
|
|
|
+public class LoginController {
|
|
|
+ @Autowired
|
|
|
+ private LoginService loginService;
|
|
|
+
|
|
|
+ @Value("${miniprogram.AppId}")
|
|
|
+ private String AppId;
|
|
|
+
|
|
|
+ @Value("${miniprogram.AppSecret}")
|
|
|
+ private String AppSecret;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否是游客
|
|
|
+ *
|
|
|
+ * @param code
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ResponseBody
|
|
|
+ @RequestMapping("/doLogin")
|
|
|
+ public WxJsonModel doLogin(@RequestParam(value = "code", required = true) String code,
|
|
|
+ @RequestParam(value = "userOrganizeID") Integer userOrganizeID,
|
|
|
+ HttpServletRequest request) {
|
|
|
+ WxJsonModel res = WxJsonModel.newInstance();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ String referer = request.getHeader("Referer"); //获取当前微信小程序的环境
|
|
|
+ map.put("referer", referer);
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+ Map<String, String> requestUrlParam = new HashMap<String, String>();
|
|
|
+ requestUrlParam.put("appid", AppId);//小程序appId
|
|
|
+ requestUrlParam.put("secret", AppSecret);//小程序appsecret
|
|
|
+ requestUrlParam.put("js_code", code);//小程序端返回的code
|
|
|
+ requestUrlParam.put("grant_type", "authorization_code");//默认参数
|
|
|
+ //发送post请求读取调用微信接口获取openid用户唯一标识
|
|
|
+ String infos;
|
|
|
+ try {
|
|
|
+ infos = HttpRequest.sendPost(requestUrl, requestUrlParam);
|
|
|
+ } catch (Exception e) {
|
|
|
+ res.setData(map);
|
|
|
+ return res.error("服务器内部异常");
|
|
|
+ }
|
|
|
+ //解析相应内容(转换成json对象)
|
|
|
+ JSONObject jsonObject = JSON.parseObject(infos);
|
|
|
+ String openid = jsonObject.getString("openid");
|
|
|
+ String session_key = jsonObject.getString("session_key");
|
|
|
+ String errcode = jsonObject.getString("errcode");
|
|
|
+ String errmsg = jsonObject.getString("errmsg");
|
|
|
+ if (!StringUtils.isEmpty(errcode) &&
|
|
|
+ (errcode.equals("-1") || errcode.equals("40029") || errcode.equals("45011"))) {
|
|
|
+ res.setMsg(errmsg);
|
|
|
+ res.setData(map);
|
|
|
+ map.put("sessionKey", session_key);
|
|
|
+ res.setCode("-1");
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ CmOperationUser operationUser = loginService.doLogin(openid, userOrganizeID);
|
|
|
+ if (operationUser == null) {
|
|
|
+ return res.error("-1", "游客,请登录");
|
|
|
+ }
|
|
|
+ return res.error("1", "登录成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断邀请码是否有效
|
|
|
+ *
|
|
|
+ * @param invitationCode 邀请码
|
|
|
+ * @param userOrganizeID 组织id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/isEnabled")
|
|
|
+ @ResponseBody
|
|
|
+ public WxJsonModel isEnabled(String invitationCode, Integer userOrganizeID) {
|
|
|
+ WxJsonModel res = WxJsonModel.newInstance();
|
|
|
+ CmOperationUser operationUser = loginService.isEnabled(invitationCode, userOrganizeID);
|
|
|
+ if (operationUser == null) {
|
|
|
+ return res.error("-1", "邀请码错误");
|
|
|
+ }
|
|
|
+ Date date = new Date();
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DATE, 2);
|
|
|
+ if (operationUser.getStatus().equals("1") && date.getTime() > calendar.getTime().getTime() && operationUser.getDelFlag().equals('0')) {
|
|
|
+ return res.error("-1", "邀请码已失效");
|
|
|
+ }
|
|
|
+ if (operationUser.getStatus().equals("2") && operationUser.getDelFlag().equals("0")) {
|
|
|
+ return res.error("-1", "邀请码已被使用");
|
|
|
+ }
|
|
|
+ if (!operationUser.getDelFlag().equals("0")) {
|
|
|
+ return res.error("-1", "您的账号已下线");
|
|
|
+ }
|
|
|
+ if (operationUser.getClubStatus().equals("91")) {
|
|
|
+ return res.error("-1", "您所在的会所已下线");
|
|
|
+ }
|
|
|
+ return res.error("1", "邀请码通过");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册并登录
|
|
|
+ *
|
|
|
+ * @param code
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/register")
|
|
|
+ @ResponseBody
|
|
|
+ public WxJsonModel register(@RequestParam(value = "code", required = true) String code,
|
|
|
+ CmOperationUser operationUser, HttpServletRequest request) {
|
|
|
+ WxJsonModel res = WxJsonModel.newInstance();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ String referer = request.getHeader("Referer"); //获取当前微信小程序的环境
|
|
|
+ map.put("referer", referer);
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+ Map<String, String> requestUrlParam = new HashMap<String, String>();
|
|
|
+ requestUrlParam.put("appid", AppId);//小程序appId
|
|
|
+ requestUrlParam.put("secret", AppSecret);//小程序appsecret
|
|
|
+ requestUrlParam.put("js_code", code);//小程序端返回的code
|
|
|
+ requestUrlParam.put("grant_type", "authorization_code");//默认参数
|
|
|
+ //发送post请求读取调用微信接口获取openid用户唯一标识
|
|
|
+ String infos;
|
|
|
+ try {
|
|
|
+ infos = HttpRequest.sendPost(requestUrl, requestUrlParam);
|
|
|
+ } catch (Exception e) {
|
|
|
+ res.setData(map);
|
|
|
+ return res.error("服务器内部异常");
|
|
|
+ }
|
|
|
+ //解析相应内容(转换成json对象)
|
|
|
+ JSONObject jsonObject = JSON.parseObject(infos);
|
|
|
+ String openid = jsonObject.getString("openid");
|
|
|
+ String session_key = jsonObject.getString("session_key");
|
|
|
+ String errcode = jsonObject.getString("errcode");
|
|
|
+ String errmsg = jsonObject.getString("errmsg");
|
|
|
+ if (!StringUtils.isEmpty(errcode) &&
|
|
|
+ (errcode.equals("-1") || errcode.equals("40029") || errcode.equals("45011"))) {
|
|
|
+ res.setMsg(errmsg);
|
|
|
+ res.setData(map);
|
|
|
+ map.put("sessionKey", session_key);
|
|
|
+ res.setCode("-1");
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ operationUser.setOpenid(openid);
|
|
|
+ loginService.update(operationUser);
|
|
|
+ return res.error("1", "注册并登录成功");
|
|
|
+ }
|
|
|
+}
|