wuxw
2020-01-18 762a48523ed1b53036ae4ba1a4d7c2e9e044a51f
AppFrontService/src/main/java/com/java110/app/smo/wxLogin/impl/WxLoginSMOImpl.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.app.properties.WechatAuthProperties;
import com.java110.app.smo.AppAbstractComponentSMO;
import com.java110.app.smo.wxLogin.IWxLoginSMO;
import com.java110.core.component.AbstractComponentSMO;
import com.java110.core.context.IPageData;
@@ -27,7 +28,7 @@
 * wx登录
 */
@Service("wxLoginSMOImpl")
public class WxLoginSMOImpl extends AbstractComponentSMO implements IWxLoginSMO {
public class WxLoginSMOImpl extends AppAbstractComponentSMO implements IWxLoginSMO {
    private final static Logger logger = LoggerFactory.getLogger(WxLoginSMOImpl.class);
@@ -65,13 +66,15 @@
                code,
                wechatAuthProperties.getGrantType());
        logger.debug("wechatAuthProperties:" + JSONObject.toJSONString(wechatAuthProperties));
        logger.debug("微信返回报文:" + response);
        Assert.jsonObjectHaveKey(response, "errcode", "返回报文中未包含 错误编码,接口出错");
        //Assert.jsonObjectHaveKey(response, "errcode", "返回报文中未包含 错误编码,接口出错");
        JSONObject responseObj = JSONObject.parseObject(response);
        if (!"0".equals(responseObj.getString("errcode"))) {
            throw new IllegalArgumentException("微信验证失败,可能是code失效");
        if (responseObj.containsKey("errcode") && !"0".equals(responseObj.getString("errcode"))) {
            throw new IllegalArgumentException("微信验证失败,可能是code失效" + responseObj);
        }
        String openId = responseObj.getString("openid");
@@ -87,36 +90,17 @@
        JSONObject userResult = JSONObject.parseObject(responseEntity.getBody());
        int total = userResult.getIntValue("total");
        JSONObject userInfo = paramIn.getJSONObject("userInfo");
        if (total == 0) {
            //保存用户信息
            JSONObject registerInfo = new JSONObject();
            JSONObject userInfo = paramIn.getJSONObject("userInfo");
            //设置默认密码
            String userDefaultPassword = MappingCache.getValue(MappingConstant.KEY_STAFF_DEFAULT_PASSWORD);
            Assert.hasLength(userDefaultPassword, "映射表中未设置员工默认密码,请检查" + MappingConstant.KEY_STAFF_DEFAULT_PASSWORD);
            userDefaultPassword = AuthenticationFactory.passwdMd5(userDefaultPassword);
            /**
             * {
             *      "userId": "-1",
             *      "name": "张三",
             *      "email": "928255095@qq.com",
             *      "address": "青海省西宁市城中区129号",
             *      "password": "ERCBHDUYFJDNDHDJDNDJDHDUDHDJDDKDK",
             *      "locationCd": "001",
             *      "age": 19,
             *      "sex": "0",
             *      "tel": "17797173943",
             *      "level_cd": "1",
             *      "businessUserAttr": [{
             *      "attrId":"-1",
             *      "specCd":"1001",
             *      "value":"01"
             *      }]
             *      }
             */
            registerInfo.put("userId", "-1");
            registerInfo.put("email", "");
            registerInfo.put("address", userInfo.getString("country") + userInfo.getString("province") + userInfo.getString("city"));
@@ -129,13 +113,13 @@
            registerInfo.put("password", userDefaultPassword);
            JSONArray userAttr = new JSONArray();
            JSONObject userAttrObj = new JSONObject();
            userAttrObj.put("attrId","-1");
            userAttrObj.put("specCd","100201911001");
            userAttrObj.put("value",openId);
            userAttrObj.put("attrId", "-1");
            userAttrObj.put("specCd", "100201911001");
            userAttrObj.put("value", openId);
            userAttr.add(userAttrObj);
            registerInfo.put("businessUserAttr", userAttr);
            responseEntity = this.callCenterService(restTemplate, pd, registerInfo.toJSONString(), ServiceConstant.SERVICE_API_URL + "/api/user.service.register", HttpMethod.POST);
            if(responseEntity.getStatusCode() != HttpStatus.OK){
            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                throw new IllegalArgumentException("保存用户信息失败");
            }
            responseEntity = super.getUserInfoByOpenId(pd, restTemplate, openId);
@@ -144,21 +128,27 @@
            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                throw new IllegalArgumentException("根绝openId 查询用户信息异常" + openId);
            }
             userResult = JSONObject.parseObject(responseEntity.getBody());
            userResult = JSONObject.parseObject(responseEntity.getBody());
        }
        JSONObject realUserInfo = userResult.getJSONArray("users").getJSONObject(0);
        userInfo.putAll(realUserInfo);
        userInfo.put("password", "");
        try {
            Map userMap = new HashMap();
            userMap.put(CommonConstant.LOGIN_USER_ID,userResult.getString("userId"));
            userMap.put(CommonConstant.LOGIN_USER_ID, userInfo.getString("userId"));
            userMap.put(CommonConstant.LOGIN_USER_NAME, userInfo.getString("name"));
            String token = AuthenticationFactory.createAndSaveToken(userMap);
            JSONObject paramOut = new JSONObject();
            paramOut.putAll(userResult);
            paramOut.put("token",token);
            paramOut.put("sessionKey",sessionKey);
            paramOut.put("result", 0);
            paramOut.put("userInfo", userInfo);
            paramOut.put("token", token);
            paramOut.put("sessionKey", sessionKey);
            pd.setToken(token);
            responseEntity = new ResponseEntity<String>(paramOut.toJSONString(), HttpStatus.OK);
        }catch (Exception e){
            logger.error("登录异常:",e);
        } catch (Exception e) {
            logger.error("登录异常:", e);
            throw new IllegalArgumentException("鉴权失败");
        }
        //根据openId 查询用户信息,是否存在用户