java110
2021-03-22 4c40656795c0cfddbc32d5054debfc1879c6fc2e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package com.java110.front.smo.login.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.base.smo.front.AbstractFrontServiceSMO;
import com.java110.core.context.IPageData;
import com.java110.core.context.PageData;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.WechatFactory;
import com.java110.dto.owner.OwnerAppUserDto;
import com.java110.dto.smallWeChat.SmallWeChatDto;
import com.java110.dto.user.UserDto;
import com.java110.front.properties.WechatAuthProperties;
import com.java110.front.smo.login.IOwnerAppLoginSMO;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.constant.*;
import com.java110.utils.exception.SMOException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
/**
 * wx登录
 */
@Service("ownerAppLoginSMOImpl")
public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOwnerAppLoginSMO {
 
    private final static Logger logger = LoggerFactory.getLogger(OwnerAppLoginSMOImpl.class);
 
    private final static int expireTime = 7200;
 
    private final static int LOGIN_PAGE = 1;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    private RestTemplate outRestTemplate;
 
    @Autowired
    private WechatAuthProperties wechatAuthProperties;
 
    @Override
    public ResponseEntity<String> doLogin(IPageData pd) throws SMOException {
        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
        Assert.hasKeyAndValue(paramIn, "username", "请求报文中未包含用户名");
        Assert.hasKeyAndValue(paramIn, "password", "请求报文中未包含密码");
        logger.debug("doLogin入参:" + paramIn.toJSONString());
        ResponseEntity<String> responseEntity;
 
        JSONObject loginInfo = JSONObject.parseObject(pd.getReqData());
        boolean loginByPhone = false;
        if (paramIn.containsKey("loginByPhone")) {
            loginByPhone = paramIn.getBoolean("loginByPhone");
        }
 
        loginInfo.put("passwd", AuthenticationFactory.passwdMd5(loginInfo.getString("password")));
        UserDto userDto = new UserDto();
        userDto.setUserName(loginInfo.getString("username"));
        userDto.setPassword(loginInfo.getString("password"));
        userDto.setLoginByPhone(loginByPhone);
        userDto.setLevelCd("02");
        userDto = super.postForApi(pd, userDto, ServiceCodeConstant.SERVICE_CODE_USER_LOGIN, UserDto.class);
 
        if (userDto == null) {
            responseEntity = new ResponseEntity<>("用户名或密码错误", HttpStatus.BAD_REQUEST);
            return responseEntity;
        }
 
        //根据用户查询商户信息
        String userId = userDto.getUserId();
 
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setUserId(userId);
        List<OwnerAppUserDto> ownerAppUserDtos = super.getForApis(pd, ownerAppUserDto, ServiceCodeConstant.LIST_APPUSERBINDINGOWNERS, OwnerAppUserDto.class);
 
 
        if (ownerAppUserDtos == null || ownerAppUserDtos.size() < 1) {
            responseEntity = new ResponseEntity<>("用户未绑定业主", HttpStatus.BAD_REQUEST);
            return responseEntity;
        }
 
        JSONObject appUser = JSONObject.parseObject(JSONObject.toJSONString(ownerAppUserDtos.get(0)));
        appUser.put("userId", userId);
        appUser.put("userName", paramIn.getString("username"));
        JSONObject paramOut = new JSONObject();
        paramOut.put("result", 0);
        paramOut.put("owner", appUser);
        paramOut.put("token", userDto.getToken());
 
        userDto = new UserDto();
        userDto.setUserId(ownerAppUserDtos.get(0).getUserId());
        UserDto tmpUserDto = super.getForApi(pd, userDto, ServiceCodeConstant.QUERY_USER_SECRET, UserDto.class);
        paramOut.put("key", tmpUserDto.getKey());
 
        String appId = pd.getAppId();
 
        if ("992020061452450002".equals(appId)) { //公众号
            return wechat(pd, paramIn, paramOut, userId, ownerAppUserDtos);
        } else if ("992019111758490006".equals(appId)) { //小程序
            return mina(pd, paramIn, paramOut, userId, ownerAppUserDtos);
        } else {//app
            return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
        }
    }
 
    @Override
    public ResponseEntity<String> doLoginByKey(IPageData pd) throws SMOException {
        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
        Assert.hasKeyAndValue(paramIn, "key", "请求报文中未包含临时秘钥");
        logger.debug("doLogin入参:" + paramIn.toJSONString());
        ResponseEntity<String> responseEntity;
 
        JSONObject loginInfo = JSONObject.parseObject(pd.getReqData());
 
        UserDto userDto = new UserDto();
        userDto.setKey(paramIn.getString("key"));
        userDto = super.postForApi(pd, userDto, ServiceCodeConstant.SERVICE_CODE_USER_LOGIN, UserDto.class);
 
 
        //根据用户查询商户信息
        String userId = userDto.getUserId();
 
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setUserId(userId);
        List<OwnerAppUserDto> ownerAppUserDtos = super.getForApis(pd, ownerAppUserDto, ServiceCodeConstant.LIST_APPUSERBINDINGOWNERS, OwnerAppUserDto.class);
 
 
        if (ownerAppUserDtos == null || ownerAppUserDtos.size() < 1) {
            responseEntity = new ResponseEntity<>("用户未绑定业主", HttpStatus.BAD_REQUEST);
            return responseEntity;
        }
 
        JSONObject appUser = JSONObject.parseObject(JSONObject.toJSONString(ownerAppUserDtos.get(0)));
        appUser.put("userId", userId);
        appUser.put("userName", paramIn.getString("username"));
        JSONObject paramOut = new JSONObject();
        paramOut.put("code", 0);
        paramOut.put("msg", "成功");
        paramOut.put("owner", appUser);
        paramOut.put("token", userDto.getToken());
        paramOut.put("key", userDto.getKey());
        return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
 
    }
 
    @Override
    public ResponseEntity<String> getPageAccessToken(IPageData pd, HttpServletRequest request) throws SMOException {
        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
        String authCode = paramIn.getString("code");
        String state = paramIn.getString("state");
        String paramStr = CommonCache.getAndRemoveValue(paramIn.getString("urlCode"));
 
        if (StringUtil.isEmpty(paramStr)) {
            return ResultVo.redirectPage("/");
        }
 
        JSONObject param = JSONObject.parseObject(paramStr);
        String redirectUrl = param.getString("redirectUrl");
        String errorUrl = param.getString("errorUrl");
        String wId = paramIn.getString("wId");
        SmallWeChatDto smallWeChatDto = null;
        if (!StringUtil.isEmpty(wId)) {
            paramIn.put("appId", WechatFactory.getAppId(wId));
            smallWeChatDto = getSmallWechat(pd, paramIn);
        }
        if (smallWeChatDto == null) { //从配置文件中获取 小程序配置信息
            smallWeChatDto = new SmallWeChatDto();
            smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
            smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
        }
 
        String url = WechatConstant.APP_GET_ACCESS_TOKEN_URL.replace("APPID", smallWeChatDto.getAppId())
                .replace("SECRET", smallWeChatDto.getAppSecret())
                .replace("CODE", authCode);
 
        ResponseEntity<String> paramOut = outRestTemplate.getForEntity(url, String.class);
 
        logger.debug("调用微信换去openId " + paramOut);
        if (paramOut.getStatusCode() != HttpStatus.OK) {
            return ResultVo.redirectPage(errorUrl);
 
        }
 
        JSONObject paramObj = JSONObject.parseObject(paramOut.getBody());
 
        //获取 openId
        String openId = paramObj.getString("openid");
        String userinfo_url = WechatConstant.APP_GET_USER_INFO_URL
                .replace("ACCESS_TOKEN", paramObj.getString("access_token"))
                .replace("OPENID", openId);
 
        ResponseEntity<String> userinfo_paramOut = outRestTemplate.getForEntity(userinfo_url, String.class);
        logger.debug("调用微信换去openId ", userinfo_paramOut);
        if (userinfo_paramOut.getStatusCode() != HttpStatus.OK) {
            return ResultVo.redirectPage("/");
        }
        JSONObject userinfo_paramObj = JSONObject.parseObject(userinfo_paramOut.getBody());
 
        int loginFlag = paramIn.getInteger("loginFlag");
 
        //说明是登录页面,下发code 就可以,不需要下发key 之类
        if (loginFlag == LOGIN_PAGE) {
            //将openId放到redis 缓存,给前段下发临时票据
            String code = UUID.randomUUID().toString();
            CommonCache.setValue(code, openId, expireTime);
            CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime);
            CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime);
            if (errorUrl.indexOf("?") > 0) {
                errorUrl += ("&code=" + code);
            } else {
                errorUrl += ("?code=" + code);
            }
            return ResultVo.redirectPage(errorUrl);
        }
 
        //判断当前openId 是否绑定了业主
        pd = PageData.newInstance().builder("-1", "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setOpenId(openId);
        List<OwnerAppUserDto> ownerAppUserDtos = super.getForApis(pd, ownerAppUserDto, ServiceCodeConstant.LIST_APPUSERBINDINGOWNERS, OwnerAppUserDto.class);
 
        if (ownerAppUserDtos == null || ownerAppUserDtos.size() < 1) {
            //将openId放到redis 缓存,给前段下发临时票据
            String code = UUID.randomUUID().toString();
            CommonCache.setValue(code, openId, expireTime);
            CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime);
            CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime);
            if (errorUrl.indexOf("?") > 0) {
                errorUrl += ("&code=" + code);
            } else {
                errorUrl += ("?code=" + code);
            }
            return ResultVo.redirectPage(errorUrl);
        }
 
        // String accessToken = paramObj.getString("access_token");//暂时不用
        Map userMap = new HashMap();
        userMap.put(CommonConstant.LOGIN_USER_ID, ownerAppUserDtos.get(0).getUserId());
        userMap.put(CommonConstant.LOGIN_USER_NAME, ownerAppUserDtos.get(0).getAppUserName());
        String token = "";
        try {
            token = AuthenticationFactory.createAndSaveToken(userMap);
            pd.setToken(token);
        } catch (Exception e) {
            logger.error("创建token失败");
        }
        //查询用户key
        UserDto userDto = new UserDto();
        userDto.setUserId(ownerAppUserDtos.get(0).getUserId());
        UserDto tmpUserDto = super.getForApi(pd, userDto, ServiceCodeConstant.QUERY_USER_SECRET, UserDto.class);
 
        if (StringUtil.isEmpty(tmpUserDto.getKey())) {
            String code = UUID.randomUUID().toString();
            CommonCache.setValue(code, openId, expireTime);
            CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime);
            CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime);
            if (errorUrl.indexOf("?") > 0) {
                errorUrl += ("&code=" + code);
            } else {
                errorUrl += ("?code=" + code);
            }
            return ResultVo.redirectPage(errorUrl);
        }
        //如果参数中有key 直接用新的覆盖
        Map tempRedirectParam = super.urlToMap(redirectUrl);
        tempRedirectParam.put("key", tmpUserDto.getKey());
        if (redirectUrl.indexOf("?") > -1) {
            redirectUrl = redirectUrl.substring(0, redirectUrl.indexOf("?")) + super.mapToUrlParam(tempRedirectParam);
        } else {
            redirectUrl = redirectUrl + super.mapToUrlParam(tempRedirectParam);
        }
        //redirectUrl = redirectUrl + (redirectUrl.indexOf("?") > 0 ? "&key=" + tmpUserDto.getKey() : "?key=" + tmpUserDto.getKey());
        return ResultVo.redirectPage(redirectUrl);
 
    }
 
    /**
     * 刷新token
     *
     * @param pd
     * @param redirectUrl
     * @param request
     * @param response
     * @return
     * @throws SMOException
     */
    @Override
    public ResponseEntity<String> refreshToken(IPageData pd, String redirectUrl,
                                               String errorUrl,
                                               String loginFlag,
                                               HttpServletRequest request, HttpServletResponse response) throws SMOException {
        //分配urlCode
        String urlCode = UUID.randomUUID().toString();
        JSONObject param = new JSONObject();
        param.put("redirectUrl", redirectUrl);
        param.put("errorUrl", errorUrl);
        CommonCache.setValue(urlCode, param.toJSONString(), expireTime);
        String wAppId = request.getHeader("w-app-id");
        SmallWeChatDto smallWeChatDto = null;
        if (!StringUtil.isEmpty(wAppId)) {
            JSONObject paramIn = new JSONObject();
            paramIn.put("appId", wAppId);
            smallWeChatDto = getSmallWechat(pd, paramIn);
        }
        if (smallWeChatDto == null) { //从配置文件中获取 小程序配置信息
            smallWeChatDto = new SmallWeChatDto();
            smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
            smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
        }
 
        URL url = null;
        String openUrl = "";
        try {
            url = new URL(redirectUrl);
 
            String newUrl = url.getProtocol() + "://" + url.getHost();
            if (url.getPort() > 0) {
                newUrl += (":" + url.getPort());
            }
 
            openUrl = WechatConstant.OPEN_AUTH
                    .replace("APPID", smallWeChatDto.getAppId())
                    .replace("SCOPE", "snsapi_userinfo")
                    .replace(
                            "REDIRECT_URL",
                            URLEncoder
                                    .encode(
                                            (newUrl
                                                    + "/app/loginOwnerWechatAuth?appId=992020061452450002&urlCode=" +
                                                    urlCode + "&loginFlag=" + loginFlag + "&wId=" + WechatFactory.getWId(wAppId)),
                                            "UTF-8")).replace("STATE", "1");
 
        } catch (Exception e) {
            logger.error("微信公众号鉴权 redirectUrl 错误 " + redirectUrl, e);
            throw new SMOException(ResponseConstant.RESULT_CODE_ERROR, e.getLocalizedMessage());
        }
 
        JSONObject urlObj = new JSONObject();
        urlObj.put("openUrl", openUrl);
 
        return ResultVo.createResponseEntity(ResultVo.CODE_MACHINE_OK, ResultVo.MSG_OK, urlObj);
    }
 
    /**
     * 公众号登录
     *
     * @param pd
     * @param paramIn
     * @param paramOut
     * @param userId
     * @param ownerAppUserDtos
     * @return
     */
    private ResponseEntity<String> wechat(IPageData pd, JSONObject paramIn, JSONObject paramOut, String userId,
                                          List<OwnerAppUserDto> ownerAppUserDtos) {
 
        ResponseEntity<String> responseEntity = null;
        //查询微信信息
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
 
 
        String code = paramIn.getString("code");
 
        String openId = CommonCache.getValue(code);
        String nickname = CommonCache.getValue(code + "-nickname");
        String headimgurl = CommonCache.getValue(code + "-headimgurl");
 
        if (StringUtil.isEmpty(openId)) {
            responseEntity = new ResponseEntity<>("页面失效,请刷新后重试", HttpStatus.UNAUTHORIZED);
            return responseEntity;
        }
 
        OwnerAppUserDto curOwnerApp = judgeCurrentOwnerBind(ownerAppUserDtos, OwnerAppUserDto.APP_TYPE_WECHAT);
 
        //说明 当前的openId 就是最新的
        if (curOwnerApp != null && openId.equals(curOwnerApp.getOpenId())) {
            return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
        }
 
        JSONObject userOwnerInfo = new JSONObject();
        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setOpenId(openId);
        // ownerAppUserDto.setNickName(StringUtil.encodeEmoji(nickname));
        ownerAppUserDto.setNickName(nickname);
        ownerAppUserDto.setHeadImgUrl(headimgurl);
        ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
        if (curOwnerApp != null) {
            ownerAppUserDto.setAppUserId(curOwnerApp.getAppUserId());
            ownerAppUserDto.setCommunityId(curOwnerApp.getCommunityId());
        } else {
            ownerAppUserDto.setOldAppUserId(ownerAppUserDtos.get(0).getAppUserId());
            ownerAppUserDto.setAppUserId("-1");
            ownerAppUserDto.setCommunityId(ownerAppUserDtos.get(0).getCommunityId());
        }
 
        //查询微信信息
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
 
        super.postForApi(pd, ownerAppUserDto, ServiceCodeConstant.REFRESH_APP_USER_BINDING_OWNER_OPEN_ID,
                OwnerAppUserDto.class);
        return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
    }
 
    private ResponseEntity<String> mina(IPageData pd, JSONObject paramIn, JSONObject paramOut, String userId, List<OwnerAppUserDto> ownerAppUserDtos) {
 
        ResponseEntity<String> responseEntity = null;
        //查询微信信息
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        responseEntity = this.callCenterService(restTemplate, pd, "",
                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
                        + paramIn.getString("appId") + "&page=1&row=1&communityId=" + ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            return responseEntity;
        }
        JSONObject smallWechatObj = JSONObject.parseObject(responseEntity.getBody().toString());
        JSONArray smallWeChats = smallWechatObj.getJSONArray("smallWeChats");
        String appId = wechatAuthProperties.getAppId();
        String secret = wechatAuthProperties.getSecret();
        if (smallWeChats.size() > 0) {
            appId = smallWeChats.getJSONObject(0).getString("appId");
            secret = smallWeChats.getJSONObject(0).getString("appSecret");
        }
 
        String code = paramIn.getString("code");
        String urlString = "?appid={appId}&secret={secret}&js_code={code}&grant_type={grantType}";
        String response = outRestTemplate.getForObject(
                wechatAuthProperties.getSessionHost() + urlString, String.class,
                appId,
                secret,
                code,
                wechatAuthProperties.getGrantType());
 
        logger.debug("wechatAuthProperties:" + JSONObject.toJSONString(wechatAuthProperties));
 
        logger.debug("微信返回报文:" + response);
 
        //Assert.jsonObjectHaveKey(response, "errcode", "返回报文中未包含 错误编码,接口出错");
        JSONObject responseObj = JSONObject.parseObject(response);
 
        if (responseObj.containsKey("errcode") && !"0".equals(responseObj.getString("errcode"))) {
            throw new IllegalArgumentException("微信验证失败,可能是code失效" + responseObj);
        }
 
        String openId = responseObj.getString("openid");
 
        OwnerAppUserDto ownerAppUserDto = judgeCurrentOwnerBind(ownerAppUserDtos, OwnerAppUserDto.APP_TYPE_WECHAT_MINA);
 
        //说明 当前的openId 就是最新的
        if (ownerAppUserDto != null && openId.equals(ownerAppUserDto.getOpenId())) {
            return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
        }
 
        OwnerAppUserDto tmpOwnerAppUserDto = new OwnerAppUserDto();
        tmpOwnerAppUserDto.setOpenId(openId);
        tmpOwnerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT_MINA);
        if (ownerAppUserDto != null) {
            tmpOwnerAppUserDto.setAppUserId(ownerAppUserDto.getAppUserId());
            tmpOwnerAppUserDto.setCommunityId(ownerAppUserDto.getCommunityId());
        } else {
            tmpOwnerAppUserDto.setOldAppUserId(ownerAppUserDtos.get(0).getAppUserId());
            tmpOwnerAppUserDto.setAppUserId("-1");
            tmpOwnerAppUserDto.setCommunityId(ownerAppUserDtos.get(0).getCommunityId());
        }
        //查询微信信息
        pd = PageData.newInstance().builder(userId, "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
 
        super.postForApi(pd, tmpOwnerAppUserDto, ServiceCodeConstant.REFRESH_APP_USER_BINDING_OWNER_OPEN_ID,
                OwnerAppUserDto.class);
        return new ResponseEntity<>(paramOut.toJSONString(), HttpStatus.OK);
    }
 
    /**
     * 判断 绑定表里是否存在当前 端 绑定信息
     *
     * @param ownerAppUserDtos
     * @param appType
     * @return
     */
    private OwnerAppUserDto judgeCurrentOwnerBind(List<OwnerAppUserDto> ownerAppUserDtos, String appType) {
 
        for (OwnerAppUserDto ownerAppUserDto : ownerAppUserDtos) {
            if (appType.equals(ownerAppUserDto.getAppType())) {
                return ownerAppUserDto;
            }
        }
        return null;
    }
 
    private SmallWeChatDto getSmallWechat(IPageData pd, JSONObject paramIn) {
 
        ResponseEntity responseEntity = null;
 
        pd = PageData.newInstance().builder(pd.getUserId(), "", "", pd.getReqData(),
                "", "", "", "",
                pd.getAppId());
        responseEntity = this.callCenterService(restTemplate, pd, "",
                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
                        + paramIn.getString("appId") + "&page=1&row=1", HttpMethod.GET);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            return null;
        }
        JSONObject smallWechatObj = JSONObject.parseObject(responseEntity.getBody().toString());
        JSONArray smallWeChats = smallWechatObj.getJSONArray("smallWeChats");
 
        if (smallWeChats == null || smallWeChats.size() < 1) {
            return null;
        }
 
        return BeanConvertUtil.covertBean(smallWeChats.get(0), SmallWeChatDto.class);
    }
 
    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
 
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
 
}