|
|
@@ -0,0 +1,145 @@
|
|
|
+package com.uas.platform.b2c.common.weixin.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.platform.b2c.common.account.dao.UserDao;
|
|
|
+import com.uas.platform.b2c.common.account.model.User;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.MessageModel;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.TemplateData;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.TemplateMessage;
|
|
|
+import com.uas.platform.b2c.common.weixin.service.WeChatService;
|
|
|
+import com.uas.platform.b2c.common.weixin.util.WeChatUtil;
|
|
|
+import com.uas.platform.core.exception.IllegalOperatorException;
|
|
|
+import com.uas.sso.common.util.HttpUtil;
|
|
|
+import com.uas.sso.entity.UserView;
|
|
|
+import com.uas.sso.util.AccountUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.ui.ModelMap;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author liusw
|
|
|
+ * @date 2018-05-30 9:24
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class WeChatServiceImpl implements WeChatService{
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserDao userDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap getWxUserInfo(String code, String state) {
|
|
|
+ ModelMap result = new ModelMap();
|
|
|
+ if (!StringUtils.isEmpty(code)) {
|
|
|
+ JSONObject userInfo = getAccessTokenByCode(code);
|
|
|
+ result.put("nickname", userInfo.getString("nickname"));
|
|
|
+ result.put("headimgurl", userInfo.getString("headimgurl"));
|
|
|
+ String openid = userInfo.getString("openid");
|
|
|
+ result.put("openid", openid);
|
|
|
+ // todo 检查当前用户是否绑定
|
|
|
+ result.put("status", 0);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap sendTemplateMessage(List<MessageModel> messages) {
|
|
|
+ ModelMap result = new ModelMap();
|
|
|
+ String url = WeChatUtil.GET_ACCESS_TOKEN;
|
|
|
+ url = url.replace("APPID", WeChatUtil.APPID);
|
|
|
+ url = url.replace("SECRET", WeChatUtil.APPSECRET);
|
|
|
+ try {
|
|
|
+ HttpUtil.ResponseWrap res = HttpUtil.doGet(url);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(res.getContent());
|
|
|
+ String access_token = jsonObject.getString("access_token");
|
|
|
+ String sendTemplateUrl = WeChatUtil.GET_ACCESS_TOKEN.replace("ACCESS_TOKEN", access_token);
|
|
|
+ TemplateMessage templateMessage = new TemplateMessage();
|
|
|
+ templateMessage.setTemplateId(WeChatUtil.INQUIRY_TEMPLATE_ID);
|
|
|
+ for (MessageModel messageModel : messages) {
|
|
|
+ User user= userDao.findOne(messageModel.getReceiverUu());
|
|
|
+// String jsonstr = array.toString();
|
|
|
+// HttpUtil.doPost(sendTemplateUrl, jsonstr);
|
|
|
+ }
|
|
|
+ result.put("success", true);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ result.put("success", false);
|
|
|
+ result.put("message", e.getMessage());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap bindUser(User user) {
|
|
|
+ ModelMap result = new ModelMap();
|
|
|
+ // 账户中心校验手机号和密码是否正确
|
|
|
+ if (StringUtils.isEmpty(user) || StringUtils.isEmpty(user.getUserTel()) || StringUtils.isEmpty(user.getUserPwd())) {
|
|
|
+ throw new IllegalOperatorException("信息不完整!");
|
|
|
+ }
|
|
|
+ // 是否存在用户信息
|
|
|
+ List<User> oldUsers = userDao.findUserByUserTel(user.getUserTel());
|
|
|
+ if (CollectionUtils.isEmpty(oldUsers)) {
|
|
|
+ throw new IllegalOperatorException("未找到用户信息");
|
|
|
+ }
|
|
|
+ UserView userView = new UserView();
|
|
|
+ userView.setMobile(user.getUserTel());
|
|
|
+ userView.setPassword(user.getUserPwd());
|
|
|
+ boolean pass = false;
|
|
|
+ try {
|
|
|
+ pass = AccountUtils.fuzzyCheckPassword(userView);
|
|
|
+ if (!pass) {
|
|
|
+ throw new IllegalOperatorException("手机号或密码不正确");
|
|
|
+ }
|
|
|
+ user = oldUsers.get(0);
|
|
|
+ result.put("success", true);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ result.put("success", false);
|
|
|
+ result.put("message", e.getMessage());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过code获取用户openId
|
|
|
+ * @param code
|
|
|
+ */
|
|
|
+ private JSONObject getAccessTokenByCode(String code) {
|
|
|
+ String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
|
|
|
+ url = url.replace("APPID", WeChatUtil.APPID);
|
|
|
+ url = url.replace("SECRET", WeChatUtil.APPSECRET);
|
|
|
+ url = url.replace("CODE", code);
|
|
|
+ try {
|
|
|
+ HttpUtil.ResponseWrap res = HttpUtil.doGet(url);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(res.getContent());
|
|
|
+ String accessToken = jsonObject.getString("access_token");
|
|
|
+ String openId = jsonObject.getString("openid");
|
|
|
+ String refreshToken = jsonObject.getString("refresh_token");
|
|
|
+ JSONObject userInfo = getUserInfo(accessToken, openId);
|
|
|
+ return userInfo;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject getUserInfo(String accessToken, String openId) {
|
|
|
+ String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
|
|
|
+ url = url.replace("ACCESS_TOKEN", accessToken);
|
|
|
+ url = url.replace("OPENID", openId);
|
|
|
+ try {
|
|
|
+ HttpUtil.ResponseWrap res = HttpUtil.doGet(url);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(res.getContent());
|
|
|
+ return jsonObject;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|