MobileController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package com.qlm.controller.jinzai;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.UUID;
  6. import javax.servlet.http.HttpServletRequest;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.jfinal.aop.Clear;
  9. import com.jfinal.core.Controller;
  10. import com.jfinal.kit.HashKit;
  11. import com.jfinal.kit.HttpKit;
  12. import com.jfinal.kit.PropKit;
  13. import com.jfinal.plugin.activerecord.Db;
  14. import com.jfinal.plugin.activerecord.Record;
  15. import com.jfinal.weixin.sdk.api.ApiConfig;
  16. import com.jfinal.weixin.sdk.api.ApiConfigKit;
  17. import com.jfinal.weixin.sdk.api.JsTicket;
  18. import com.jfinal.weixin.sdk.api.JsTicketApi;
  19. import com.jfinal.weixin.sdk.api.JsTicketApi.JsApiType;
  20. import com.qlm.annotation.RequestUrl;
  21. import com.qlm.log.Log;
  22. import com.qlm.tools.WxUtil;
  23. import com.qlm.tools.ip.CityInfo;
  24. import com.qlm.tools.ip.IpAddressUtil;
  25. @RequestUrl("/mobile")
  26. public class MobileController extends Controller{
  27. @Clear
  28. public void trace(){
  29. setJsTicket();
  30. render("/mobile/index.jsp");
  31. }
  32. @Clear
  33. public void search(){
  34. String code = getPara("code","");
  35. code = code.toUpperCase();
  36. String latitude = getPara("latitude");
  37. String longitude = getPara("longitude");
  38. CityInfo locationInfo = getInfo(latitude,longitude);
  39. String ipAddress = IpAddressUtil.getIpAddress(getRequest());
  40. CityInfo ipInfo = IpAddressUtil.getCityInfoByMemorySearch(ipAddress);
  41. String ipProvince = "";
  42. String ipCity = "";
  43. if(ipInfo != null){
  44. ipProvince = ipInfo.getProvince();
  45. ipCity = ipInfo.getCity();
  46. }
  47. if(locationInfo == null){
  48. locationInfo = new CityInfo();
  49. }
  50. String locationCity = locationInfo.getCity();
  51. String locationProvince = locationInfo.getProvince();
  52. String compareCity = "";
  53. String compareProvice = "";
  54. if(locationInfo != null){
  55. compareCity = locationInfo.getCity();
  56. compareProvice = locationInfo.getProvince();
  57. }
  58. if(WxUtil.isNull(compareCity)){
  59. compareCity = ipCity;
  60. compareProvice = ipProvince;
  61. }
  62. boolean status = false;
  63. String msg ="";
  64. Record searchInfo = new Record();
  65. Long searchatCount = Db.queryLong("select count(*) from t_jz_searchrecord where child_code = ?",code);
  66. if(searchatCount == null){
  67. searchatCount = 0L;
  68. }
  69. searchatCount++;
  70. searchInfo.set("child_code", code).set("count", searchatCount).set("create_time", new Date())
  71. .set("ip_addr", ipProvince+ipCity)
  72. .set("ip", ipAddress).set("location", locationProvince+locationCity);
  73. Record result = new Record();
  74. boolean cuan = true;
  75. boolean isThird = false;
  76. if(code.length() ==14){//易码
  77. try{
  78. isThird = true;
  79. String jsonData = HttpKit.get("http://saas.ymbs.com.cn/ccn/sk-warehouse-out/scanCode/"+code);
  80. JSONObject parseObject = JSONObject.parseObject(jsonData);
  81. JSONObject data = parseObject.getJSONObject("data");
  82. String distributorName = data.getString("distributorName");
  83. String operateDate = data.getString("operateDate");
  84. String salesTerritory = data.getString("salesTerritory");
  85. String productName = data.getString("productName");
  86. result.set("pinxiang", productName);
  87. Record fahuoInfo = new Record();
  88. fahuoInfo.set("DeliveryTime", operateDate).set("CustomerName", distributorName);
  89. Record jxsInfo = Db.findFirst("select * from jinzai_jxs_area where jxs = ?",distributorName);
  90. if(jxsInfo != null){
  91. String saleCity = jxsInfo.getStr("city");
  92. String saleProvince = jxsInfo.getStr("province");
  93. if(saleCity.equals(compareCity)){
  94. cuan = false;
  95. }
  96. result.set("saleLocation", saleProvince+saleCity);
  97. searchInfo.set("kehu", saleProvince+saleCity);
  98. result.set("fahuoInfo", fahuoInfo);
  99. status = true;
  100. }
  101. }catch(Exception e){
  102. Log.info("em接口查询失败,"+code);
  103. e.printStackTrace();
  104. }
  105. }else if(code.length() == 21){
  106. try{
  107. isThird = true;
  108. Map<String,String> header =new HashMap<String,String>();
  109. header.put("content-type", "application/json;charset=UTF-8");
  110. JSONObject obj = new JSONObject();
  111. obj.put("code", code);
  112. obj.put("latitude", 34.16559);
  113. obj.put("longitude", 112.824936);
  114. String jsonData = HttpKit.post("https://jinzai.oklinklink.com/api/trace/inspect/customer/find",obj.toJSONString(),header);
  115. JSONObject parseObject = JSONObject.parseObject(jsonData);
  116. JSONObject data = parseObject.getJSONObject("data");
  117. String distributorName = data.getString("bizCorpName");
  118. String operateDate = data.getString("billTime");
  119. String productName = data.getString("productName");
  120. result.set("pinxiang", productName);
  121. Record fahuoInfo = new Record();
  122. fahuoInfo.set("DeliveryTime", operateDate).set("CustomerName", distributorName);
  123. Record jxsInfo = Db.findFirst("select * from jinzai_jxs_area where jxs = ?",distributorName);
  124. if(jxsInfo != null){
  125. String saleCity = jxsInfo.getStr("city");
  126. String saleProvince = jxsInfo.getStr("province");
  127. if(saleCity.equals(compareCity)){
  128. cuan = false;
  129. }
  130. result.set("saleLocation", saleProvince+saleCity);
  131. searchInfo.set("kehu", saleProvince+saleCity);
  132. result.set("fahuoInfo", fahuoInfo);
  133. status = true;
  134. }
  135. }catch(Exception e){
  136. Log.info("em接口查询失败,"+code);
  137. e.printStackTrace();
  138. }
  139. }else if(code.length() == 22){
  140. String master_code = code;
  141. Record masterInfo = Db.findFirst("select * from jinzai_upload_master where id = ?",master_code);
  142. String pinxiang = masterInfo.getStr("pinxiang");
  143. result.set("pinxiang", pinxiang);
  144. searchInfo.set("product_name", pinxiang);
  145. if(!WxUtil.isNull(master_code)){
  146. status = true;
  147. Record fahuoInfo = Db.findFirst("select * from deliveryorders where CaseCode =?",master_code);
  148. if(fahuoInfo != null){
  149. String CustomerNo = fahuoInfo.getStr("CustomerNo");
  150. String ProductName = fahuoInfo.getStr("ProductName");
  151. Record jxsInfo = Db.findFirst("select * from jinzai_jxs_area where code = ?",CustomerNo);
  152. if(jxsInfo != null){
  153. String saleCity = jxsInfo.getStr("city");
  154. String saleProvince = jxsInfo.getStr("province");
  155. if(saleCity.equals(compareCity)){
  156. cuan = false;
  157. }
  158. result.set("saleLocation", saleProvince+saleCity);
  159. searchInfo.set("kehu", saleProvince+saleCity);
  160. result.set("fahuoInfo", fahuoInfo);
  161. }
  162. }
  163. }
  164. }
  165. else{
  166. Record jinzai_upload_child = Db.findFirst("select * from jinzai_upload_child where child_code = ?",code);
  167. if(jinzai_upload_child!=null){
  168. String master_code = jinzai_upload_child.getStr("master_code");
  169. Record masterInfo = Db.findFirst("select * from jinzai_upload_master where id = ?",master_code);
  170. String pinxiang = masterInfo.getStr("pinxiang");
  171. result.set("pinxiang", pinxiang);
  172. searchInfo.set("product_name", pinxiang);
  173. if(!WxUtil.isNull(master_code)){
  174. status = true;
  175. Record fahuoInfo = Db.findFirst("select * from deliveryorders where CaseCode =?",master_code);
  176. if(fahuoInfo != null){
  177. String CustomerNo = fahuoInfo.getStr("CustomerNo");
  178. String ProductName = fahuoInfo.getStr("ProductName");
  179. Record jxsInfo = Db.findFirst("select * from jinzai_jxs_area where code = ?",CustomerNo);
  180. if(jxsInfo != null){
  181. String saleCity = jxsInfo.getStr("city");
  182. String saleProvince = jxsInfo.getStr("province");
  183. if(saleCity.equals(compareCity)){
  184. cuan = false;
  185. }
  186. result.set("saleLocation", saleProvince+saleCity);
  187. searchInfo.set("kehu", saleProvince+saleCity);
  188. result.set("fahuoInfo", fahuoInfo);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. if(cuan){
  195. searchInfo.set("status", 0);
  196. }else{
  197. searchInfo.set("status", 1);
  198. }
  199. if(!isThird){
  200. Db.save("t_jz_searchrecord", searchInfo);
  201. }
  202. result.set("status", status).set("msg", msg).set("cuan", cuan).set("location", compareProvice+compareCity);
  203. renderJson(result);
  204. }
  205. /**
  206. * 1是我们自己的
  207. * 2是易码
  208. * 3是爱创
  209. * @param code
  210. * @return
  211. */
  212. private int codeType(String code) {
  213. if(code.length() == 14){
  214. return 3;
  215. }
  216. if(code.length() == 22){
  217. }
  218. return 0;
  219. }
  220. private static CityInfo getInfo(String latitude,String longitude){
  221. if(latitude == null || longitude == null){
  222. return null;
  223. }
  224. String location = latitude+","+longitude;
  225. String key = "HTABZ-CBHRQ-WXT5C-BWAGZ-ARXY3-QMB5N";
  226. String url = "https://apis.map.qq.com/ws/geocoder/v1/?location="+location+"&key="+key+"&get_poi=0";
  227. System.out.println(url);
  228. String xmlStr = HttpKit.get(url);
  229. JSONObject parseObject = JSONObject.parseObject(xmlStr);
  230. String message = parseObject.getString("message");
  231. if("Success".equals(message)){
  232. JSONObject result = parseObject.getJSONObject("result");
  233. JSONObject adInfo = result.getJSONObject("ad_info");
  234. String province = adInfo.getString("province");
  235. String city = adInfo.getString("city");
  236. CityInfo ipInfo = new CityInfo();
  237. ipInfo.setCity(city);
  238. ipInfo.setProvince(province);
  239. return ipInfo;
  240. }
  241. return null;
  242. }
  243. public static void main(String[] args) {
  244. CityInfo info = getInfo(30.59276+"",114.30525+"");
  245. }
  246. public void setJsTicket() {
  247. ApiConfigKit.setThreadLocalApiConfig(getApiConfig());
  248. JsTicket jsApiTicket = JsTicketApi.getTicket(JsApiType.jsapi);
  249. String ticket = jsApiTicket.getTicket();
  250. String nonce_str = create_nonce_str();
  251. // 注意 URL 一定要动态获取,不能 hardcode.
  252. String authoUrl = PropKit.get("authoUrl");
  253. String url = authoUrl // 项目名称
  254. + getRequest().getServletPath();// 请求页面或其他地址
  255. String qs = getRequest().getQueryString(); // 参数
  256. if (qs != null) {
  257. url = url + "?" + (getRequest().getQueryString());
  258. }
  259. String timestamp = create_timestamp();
  260. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  261. //注意这里参数名必须全部小写,且必须有序
  262. String str = "jsapi_ticket=" + ticket +
  263. "&noncestr=" + nonce_str +
  264. "&timestamp=" + timestamp +
  265. "&url=" + url;
  266. String signature = HashKit.sha1(str);
  267. setAttr("appId", ApiConfigKit.getApiConfig().getAppId());
  268. setAttr("nonceStr", nonce_str);
  269. setAttr("timestamp", timestamp);
  270. setAttr("url", url);
  271. setAttr("signature", signature);
  272. setAttr("jsapi_ticket", ticket);
  273. }
  274. private static String create_timestamp() {
  275. return Long.toString(System.currentTimeMillis() / 1000);
  276. }
  277. private static String create_nonce_str() {
  278. return UUID.randomUUID().toString();
  279. }
  280. public ApiConfig getApiConfig() {
  281. ApiConfig ac = new ApiConfig();
  282. // 配置微信 API 相关常量
  283. ac.setToken("1");
  284. ac.setAppId(PropKit.get("appid"));
  285. ac.setAppSecret(PropKit.get("appsecret"));
  286. /**
  287. * 是否对消息进行加密,对应于微信平台的消息加解密方式: 1:true进行加密且必须配置 encodingAesKey
  288. * 2:false采用明文模式,同时也支持混合模式
  289. */
  290. ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
  291. ac.setEncodingAesKey(PropKit.get("encodingAesKey",
  292. "setting it in config file"));
  293. return ac;
  294. }
  295. }