yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
fanli/src/main/java/com/yeshi/fanli/util/ShamHongBaoUtil.java
@@ -2,7 +2,6 @@
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -15,10 +14,8 @@
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Component;
import com.yeshi.fanli.entity.bus.user.HongBaoMessage;
import com.yeshi.fanli.entity.bus.user.ShamUser;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.service.inter.hongbao.HongBaoMessageService;
import com.yeshi.fanli.service.inter.user.ShamUserService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -31,36 +28,33 @@
@Component
public class ShamHongBaoUtil {
   @Resource
   private ShamUserService shamUserService;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private HongBaoMessageService hongBaoMessageService;
   public void addShamUserInfo(){
      List<ShamUser> list =shamUserService.getAll();
   public void addShamUserInfo() {
      List<ShamUser> list = shamUserService.getAll();
      for (ShamUser shamUser : list) {
         UserInfo form = new UserInfo();
         form.setNickName(shamUser.getName());
         form.setPortrait(shamUser.getPicUrl());
         form.setLoginType(-1);
         form.setOpenid(UUID.randomUUID().toString().replaceAll("-", "")+"-1");
         form.setOpenid(UUID.randomUUID().toString().replaceAll("-", "") + "-1");
         userInfoService.addUser(form, "23649898");
      }
   }
   public void deleteShamUser(String path){
   public void deleteShamUser(String path) {
      try {
         Workbook workbook = Workbook.getWorkbook(new File(path));
         Sheet sheet = workbook.getSheet(0);
         for (int ii=0;ii<sheet.getRows();ii++) {
            Cell cell = sheet.getCell(0,ii);
            if(cell.getContents()==null || "".equals(cell.getContents().trim())){
               continue;
         for (int ii = 0; ii < sheet.getRows(); ii++) {
            Cell cell = sheet.getCell(0, ii);
            if (cell.getContents() == null || "".equals(cell.getContents().trim())) {
               continue;
            }
            shamUserService.delete(Long.parseLong(cell.getContents()));
         }
@@ -69,37 +63,20 @@
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   private void addHongBaoMessage(){
      List<ShamUser> all = shamUserService.getAll();
      List<HongBaoMessage> list = new ArrayList<HongBaoMessage>();
      HongBaoMessage message;
      DecimalFormat df = new DecimalFormat("######0.00");
      for (ShamUser shamUser : all) {
         message = new HongBaoMessage();
         message.setName(shamUser.getName());
         message.setPicUrl(shamUser.getPicUrl());
         double random = (Math.random()+0.01)*20;
         String format = df.format(random);
         message.setContent("悄悄领取了"+format+"元红包");
         list.add(message);
      }
      hongBaoMessageService.add(list);
   }
   public void addShamUser(String pid) throws Exception{
       HttpClient client = new HttpClient();
       HttpMethod method = getPostMethod(pid);
       client.executeMethod(method);
       java.lang.System.out.println(method.getStatusLine());   //打印结果页面
         String response=new String(method.getResponseBodyAsString().getBytes("UTF-8"));
         //打印返回的信息
         method.releaseConnection();
         List<ShamUser> list = parseQQresult(response);
         for (ShamUser shamUser : list) {
            shamUserService.addShamUser(shamUser);
   public void addShamUser(String pid) throws Exception {
      HttpClient client = new HttpClient();
      HttpMethod method = getPostMethod(pid);
      client.executeMethod(method);
      java.lang.System.out.println(method.getStatusLine()); // 打印结果页面
      String response = new String(method.getResponseBodyAsString().getBytes("UTF-8"));
      // 打印返回的信息
      method.releaseConnection();
      List<ShamUser> list = parseQQresult(response);
      for (ShamUser shamUser : list) {
         shamUserService.addShamUser(shamUser);
      }
   }
@@ -107,25 +84,25 @@
      JSONObject json = JSONObject.fromObject(response);
      List<ShamUser> list = new ArrayList<ShamUser>();
      String code = String.valueOf(json.opt("retcode"));
      if("0".equals(code)){
      if ("0".equals(code)) {
         JSONArray jsonArray = json.optJSONObject("result").optJSONObject("buddy").optJSONArray("info_list");
         ShamUser user = null;
         String patterns = "[^\u4e00-\u9fa5\\w\\s]+";
         if(jsonArray==null){
            return list;
         if (jsonArray == null) {
            return list;
         }
         for (Object object : jsonArray) {
            JSONObject  data = (JSONObject)object;
            JSONObject data = (JSONObject) object;
            String name = (String) data.opt("nick");
            name=name.replaceAll(patterns, "").replaceAll(" ", "");
            if(name.length() < 2){
            name = name.replaceAll(patterns, "").replaceAll(" ", "");
            if (name.length() < 2) {
               continue;
            }
            String picUrl =  (String) data.opt("url");
            String picUrl = (String) data.opt("url");
            user = new ShamUser();
            user.setName(new String(name.getBytes(),"utf-8"));
            if(picUrl.contains("pub.idqqimg.com")){
               picUrl="https://gw.alicdn.com/tps/i3/TB1yeWeIFXXXXX5XFXXuAZJYXXX-210-210.png_160x160.jpg";
            user.setName(new String(name.getBytes(), "utf-8"));
            if (picUrl.contains("pub.idqqimg.com")) {
               picUrl = "https://gw.alicdn.com/tps/i3/TB1yeWeIFXXXXX5XFXXuAZJYXXX-210-210.png_160x160.jpg";
            }
            user.setPicUrl(picUrl);
            list.add(user);
@@ -135,27 +112,32 @@
   }
   private static HttpMethod getPostMethod(String pid) {
       PostMethod post = new PostMethod( "http://cgi.find.qq.com/qqfind/buddy/search_v3" ); //recommendDetails voteGoods recommendReplys makePublic replys
        NameValuePair num = new NameValuePair( "num" , "100" );
        NameValuePair page = new NameValuePair( "page" , "0" );
        NameValuePair sessionid = new NameValuePair( "sessionid" , "0" );
        NameValuePair agerg = new NameValuePair( "agerg" , "13" );
        NameValuePair sex = new NameValuePair( "sex" , "0" );
        NameValuePair firston = new NameValuePair( "firston" , "0" );
        NameValuePair video = new NameValuePair( "video" , "0" );
        NameValuePair country = new NameValuePair( "country" , "1" );
        NameValuePair province = new NameValuePair( "province" , pid );
        NameValuePair city = new NameValuePair( "city" , "0" );
        NameValuePair district = new NameValuePair( "district" , "0" );
        NameValuePair hcountry = new NameValuePair( "hcountry" , "1" );
        NameValuePair hprovince = new NameValuePair( "hprovince" , "0" );
        NameValuePair hcity = new NameValuePair( "hcity" , "0" );
        NameValuePair hdistrict = new NameValuePair( "hdistrict" , "0" );
        NameValuePair online = new NameValuePair( "online" , "0" );
        NameValuePair ldw = new NameValuePair( "ldw" , "814539287" );
        post.setRequestHeader("Cookie", "RK=7dVSTqdaSf; pt2gguin=o0424539852; ptisp=ctc; ptcz=555762bb1a6cef860451fe47677c82eaf80a1388eee045b8cf2d546ed1c96534; _qpsvr_localtk=tk70; pgv_info=ssid=s852802188; pgv_pvid=9584824304; uin=o424539852; skey=Z9PWyiIOuI; itkn=2002088896");
        post.setRequestBody( new NameValuePair[] {num,page,sessionid,agerg,sex,firston,video,country,province,city,
              district,hcountry,hprovince,hcity,hdistrict,online,ldw});
      PostMethod post = new PostMethod("http://cgi.find.qq.com/qqfind/buddy/search_v3"); // recommendDetails
                                                                     // voteGoods
                                                                     // recommendReplys
                                                                     // makePublic
                                                                     // replys
      NameValuePair num = new NameValuePair("num", "100");
      NameValuePair page = new NameValuePair("page", "0");
      NameValuePair sessionid = new NameValuePair("sessionid", "0");
      NameValuePair agerg = new NameValuePair("agerg", "13");
      NameValuePair sex = new NameValuePair("sex", "0");
      NameValuePair firston = new NameValuePair("firston", "0");
      NameValuePair video = new NameValuePair("video", "0");
      NameValuePair country = new NameValuePair("country", "1");
      NameValuePair province = new NameValuePair("province", pid);
      NameValuePair city = new NameValuePair("city", "0");
      NameValuePair district = new NameValuePair("district", "0");
      NameValuePair hcountry = new NameValuePair("hcountry", "1");
      NameValuePair hprovince = new NameValuePair("hprovince", "0");
      NameValuePair hcity = new NameValuePair("hcity", "0");
      NameValuePair hdistrict = new NameValuePair("hdistrict", "0");
      NameValuePair online = new NameValuePair("online", "0");
      NameValuePair ldw = new NameValuePair("ldw", "814539287");
      post.setRequestHeader("Cookie",
            "RK=7dVSTqdaSf; pt2gguin=o0424539852; ptisp=ctc; ptcz=555762bb1a6cef860451fe47677c82eaf80a1388eee045b8cf2d546ed1c96534; _qpsvr_localtk=tk70; pgv_info=ssid=s852802188; pgv_pvid=9584824304; uin=o424539852; skey=Z9PWyiIOuI; itkn=2002088896");
      post.setRequestBody(new NameValuePair[] { num, page, sessionid, agerg, sex, firston, video, country, province,
            city, district, hcountry, hprovince, hcity, hdistrict, online, ldw });
      return post;
   }