Browse Source

短视频小程序授权

半月无霜 4 years ago
parent
commit
28ed48d560

+ 99 - 23
ymall/src/main/java/com/liangjian11/ymall/controller/common/LoginController.java

@@ -12,15 +12,21 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.liangjian11.ymall.config.WxMaConfiguration;
 import com.liangjian11.ymall.handle.WxMpHandle;
+import com.liangjian11.ymall.mapper.DoctorMapper;
 import com.liangjian11.ymall.mapper.DomainConfigMapper;
+import com.liangjian11.ymall.model.Doctor;
 import com.liangjian11.ymall.model.DomainConfig;
 import com.liangjian11.ymall.model.UserAccount;
+import com.liangjian11.ymall.model.UserRole;
 import com.liangjian11.ymall.service.UserAccountService;
+import com.liangjian11.ymall.service.UserRoleService;
 import com.liangjian11.ymall.utils.DataChangeUtil;
+import com.liangjian11.ymall.utils.MongoDBUtil;
 import com.liangjian11.ymall.utils.ResultInfo;
 import com.liangjian11.ymall.utils.ResultUtil;
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang.StringUtils;
+import org.bson.Document;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -33,7 +39,9 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.util.Calendar;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.UUID;
 
 @RestController
@@ -51,6 +59,12 @@ public class LoginController {
 	@Resource
 	private WxMpHandle wxMpHandle;
 	
+	@Autowired
+	private DoctorMapper doctorMapper;
+	
+	@Autowired
+	private UserRoleService userRoleService;
+	
 	@GetMapping("/login")
 	public String login(@RequestParam String redirectUrl, @RequestParam Long userId,
 							HttpServletRequest request, HttpServletResponse response) throws IOException {
@@ -111,27 +125,32 @@ public class LoginController {
 		WxMaUserService userService = wxService.getUserService();
 		try {
 			WxMaJscode2SessionResult info = userService.getSessionInfo(code);
-			logger.debug(info.toString());
 			String sessionKey = info.getSessionKey();
 			WxMaUserInfo userInfo = userService.getUserInfo(sessionKey, encryptedData, iv);
 			logger.debug("小程序登录用户信息:" + userInfo.toString());
 
 			JSONObject user = uaService.queryByUnionid(userInfo.getUnionId());
+			JSONObject resultObj = null;
+			String token = UUID.randomUUID().toString();
+			Calendar calendar = Calendar.getInstance();
+			calendar.add(Calendar.DAY_OF_YEAR,30);
 			if(user!=null){
 				logger.info("用户存在:"+user.toString());
+				MongoDBUtil.insertVLoginToken(calendar.getTime(), user.getString("id"), token, 1);
+				resultObj = JSONObject.parseObject(JSON.toJSONString(user));
+				resultObj.put("Token", token);
+				return ResultUtil.createSuccess("成功", resultObj);
+			}else{
+				logger.info("用户不存在,将创建用户");
 				UserAccount userAccount = new UserAccount(userInfo);
-				userAccount.setCreatetime(null);
-				QueryWrapper<UserAccount> wrapper = new QueryWrapper<>();
-				wrapper.eq("unionid", userInfo.getUnionId());
-				uaService.update(userAccount, wrapper);
-				return ResultUtil.createSuccess("成功", user);
+				uaService.create(userAccount);
+				MongoDBUtil.insertVLoginToken(calendar.getTime(), userAccount.getId(), token, 1);
+				resultObj = JSONObject.parseObject(JSON.toJSONString(userAccount));
+				resultObj.put("userRoles", new LinkedList<>());
+				resultObj.put("Token", token);
+				return ResultUtil.createSuccess("成功", resultObj);
 			}
-			logger.info("用户不存在,将创建用户");
-			UserAccount userAccount = new UserAccount(userInfo);
-			uaService.create(userAccount);
-			JSONObject jsonObj = JSONObject.parseObject(JSON.toJSONString(userAccount));
-			jsonObj.put("userRoles", new LinkedList<>());
-			return ResultUtil.createSuccess("成功", jsonObj);
+			
 		} catch (Exception e) {
 			logger.error("小程序认证失败", e);
 			return ResultUtil.createFail("服务器繁忙,请稍后再试!");
@@ -139,23 +158,80 @@ public class LoginController {
 	}
 	
 	@PostMapping("/phone")
-	public ResultInfo getPhone(@RequestBody JSONObject json){
-		logger.info(json.toJSONString());
-		String sessionKey = json.getString("sessionKey");
-		String signature = json.getString("signature");
-		String rawData = json.getString("rawData");
+	public ResultInfo getPhone(@RequestBody JSONObject json, HttpServletRequest request){
+		logger.debug("小程序手机认证:"+json.toJSONString());
+		String code = json.getString("code");
 		String encryptedData = json.getString("encryptedData");
 		String iv = json.getString("iv");
+		String token = request.getHeader("token");
+		if(StringUtils.isBlank(code))
+			return ResultUtil.createFail("code不能为空");
+		if(StringUtils.isBlank(encryptedData))
+			return ResultUtil.createFail("encryptedData不能为空");
+		if(StringUtils.isBlank(iv))
+			return ResultUtil.createFail("iv不能为空");
+		if(StringUtils.isBlank(token))
+			return ResultUtil.createFail("token不能为空");
+		Document document = MongoDBUtil.queryVLoginTokenByToken(token);
+		if(document==null){
+			logger.debug("用户不存在,请确认!");
+			return ResultUtil.createFail("用户不存在,请确认!");
+		}
 		
 		WxMaService wxService = WxMaConfiguration.getMaService(WxMaConfiguration.MINIAPP_APPID);
 		WxMaUserService userService = wxService.getUserService();
-		if(!userService.checkUserInfo(sessionKey, rawData, signature))
-			return ResultUtil.createFail("检查失败,sessionKey异常");
-		WxMaPhoneNumberInfo phoneNoInfo = userService.getPhoneNoInfo(sessionKey, encryptedData, iv);
-		logger.info(phoneNoInfo.toString());
-		// TODO 将手机号存入数据库
+		String id = (String) document.get("AccountId");
+		try {
+			WxMaJscode2SessionResult info = userService.getSessionInfo(code);
+			String sessionKey = info.getSessionKey();
+			WxMaPhoneNumberInfo phoneInfo = userService.getPhoneNoInfo(sessionKey, encryptedData, iv);
+			logger.debug("小程序手机认证:", phoneInfo.toString());
+			
+			// 加密手机号
+			String enPhone = DataChangeUtil.ljEncrypt(phoneInfo.getPurePhoneNumber());
+			QueryWrapper<Doctor> wrapperD = new QueryWrapper(){{
+				eq("phone", enPhone);
+				ne("\"AccountID\"::text", id);
+			}};
+			UserAccount userAccount = uaService.getAccountByCookie(id);
+			List<Doctor> doctorList = doctorMapper.selectList(wrapperD);
+			
+			if(doctorList.size()>0){
+				UpdateWrapper<UserAccount> wp = new UpdateWrapper<>();
+				wp.setSql("mobile = null ");
+				wp.eq("mobile", enPhone);
+				UserAccount user = new UserAccount();
+				user.setMobile(null);
+				uaService.update(user, wp);
+				
+				Doctor doctor = new Doctor();
+				doctor.setAccountID(id);
+				doctor.setWxOpenID(userAccount.getWxopenid());
+				doctor.setUnionid(userAccount.getUnionid());
+				doctor.setPhone(enPhone);
+				doctorMapper.updateOne(doctor);
+				
+				QueryWrapper<UserRole> userRoleQuery = new QueryWrapper<>();
+				userRoleQuery.eq("user_role_type", 1);
+				userRoleQuery.eq("mobile", enPhone);
+				if (userRoleService.getOne(userRoleQuery) == null)
+					userRoleService.addUserRole(userAccount, enPhone, userAccount.getNickname(), userAccount.getHeadimg(), doctorList.get(0).getId(), 1, 1);
+			}
+			UserAccount account = new UserAccount();
+			account.setMobile(enPhone);
+			UpdateWrapper<UserAccount> wrapper = new UpdateWrapper<>();
+			wrapper.eq("id::text", id);
+			uaService.update(account, wrapper);
+			
+			userAccount.setMobile(enPhone);
+			return ResultUtil.createSuccess("成功", userAccount);
+		} catch (Exception e) {
+			logger.error("小程序手机认证失败", e);
+			return ResultUtil.createFail("服务器繁忙,请稍后再试!");
+		}
+		
+		
 		
-		return ResultUtil.createSuccess("成功", phoneNoInfo);
 	}
 	
 	

+ 2 - 0
ymall/src/main/java/com/liangjian11/ymall/service/impl/UserAccountServiceImpl.java

@@ -84,6 +84,8 @@ public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserA
 	public JSONObject queryByUnionid(String unionid) {
 		Map<String, Object> map = userAccountMapper.queryByUnionid(unionid);
 		JSONObject json = JSONObject.parseObject(JSON.toJSONString(map));
+		if(json==null)
+			return null;
 		Integer roleType = json.getInteger("roleType");
 		if(roleType != null){
 			json.put("userRoles", new LinkedList<Integer>(){{ add(roleType); }});

+ 1 - 1
ymall/src/main/resources/application-pgdev.properties

@@ -89,6 +89,6 @@ phone.doctorRegister= 15207700220
 
 mongodb.host=192.168.50.41
 mongodb.port=27018
-mongodb.database=
+mongodb.database=B2BMall
 mongodb.username=
 mongodb.password=

+ 2 - 2
ymall/src/main/resources/application-pgtest.properties

@@ -47,8 +47,8 @@ ymall.path.doctorFlow= https://mem.360lj.com/ui2/#/doctorFlow
 ymall.path.personalInfo= https://mem.360lj.com/ui2/#/doctorMine
 ymall.path.queryExpress= https://m.360lj.com/nhome/express
 ymall.path.guideFlow= https://mem.360lj.com/ui2/#/h5Flow
-ymall.path.activityhelpPage= https://mem.360lj.com/ui2/#/friendsHelp
-ymall.path.activityIndex= https://mem.360lj.com/ui2/#/myHelp
+ymall.path.activityhelpPage= https://mem.360lj.com/ui/#/friendsHelp
+ymall.path.activityIndex= https://mem.360lj.com/ui/#/myHelp
 ymall.path.specialist= https://mem.360lj.com/ui2/#/specialist
 ymall.path.shortVideo=https://mem.360lj.com/ui2/#/shortVideo
 ymall.path.selectRoles= https://mem.360lj.com/ui2/#/selectRoles