johnswang
2022-01-15 22907b8763fc073011872b637dc23023ef7fb4f4
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
package com.yc.sdk.shopping.action;
 
import java.util.Set;
import java.util.List;
import java.util.Map.Entry;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.yc.action.BaseAction;
import com.yc.entity.DataSourceEntity;
import com.yc.multiData.MultiDataSource;
import com.yc.multiData.SpObserver;
import com.yc.sdk.shopping.action.api.ShopCcCode;
import com.yc.sdk.shopping.entity.InformationDetailEntity;
import com.yc.sdk.shopping.entity.InformationEntity;
import com.yc.sdk.shopping.entity.SettingEntity;
import com.yc.sdk.shopping.entity.ShopCcCodeEntity;
import com.yc.sdk.shopping.service.InformationIfc;
import com.yc.sdk.shopping.service.SettingIfc;
import com.yc.sdk.shopping.service.imagedata.ShoppingImageDataIfc;
import com.yc.sdk.shopping.util.SettingKey;
import com.yc.utils.SessionKey;
 
 
@Controller
@RequestMapping("/shopping/information.do")
public class Information extends BaseAction{
    @Autowired
    InformationIfc informationIfc ;
    @Autowired
    SettingIfc settingIfc ;
    @Autowired 
    ShoppingImageDataIfc imgData ;
 
    @RequestMapping(params = "m=getInformationByShowBannerOnHomePage")
    public void getInformationByShowBannerOnHomePage(HttpServletRequest request,HttpServletResponse response) {
        HttpSession session=request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
        //String outFormat = request.getParameter("OutFormat") ;
        //String  infoDocCode = request.getParameter(SettingKey.INFODOCCODE) ; 
        //String title = request.getParameter(SettingKey.TITLE) ;
        //InformationEntity info = null ;
        String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID)==null?"":(String)session.getAttribute(SessionKey.WEIXIN_OPENID)) ;
        SettingEntity settingEntity = null ;
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            settingEntity = settingIfc.getSettingEntity(request) ;
            
            String hostUrl = SettingKey.getHostUrl(request);
            //String hostUrl = SettingKey.getHostUrl(request) ;
            //将微信corpid组装成url
            //String wxQueryString = SettingKey.getQueryStringByWx(request);
            //boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
            
            //取网店 shopcccode
            ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
            
            JsonArray jsonArray = new JsonArray();
            List<InformationEntity> infoList =  informationIfc.getInformationsByShowBannerOnHomePage(shopCcCodeEntity.getShopCcCode());
            for(int k = 0 ;infoList !=null && k < infoList.size();k++) {
                InformationEntity info = infoList.get(k) ;
                JsonObject jsonObject = new JsonObject();
                request.setAttribute(SettingKey.FROMOPENID, openId);
                String description = imgData.getDescriptionByNewImgLink(info!=null?info.getDescription():"", request) ;
                jsonObject.addProperty("doccode", (info!=null? info.getDocCode():""));
                jsonObject.addProperty("title", (info!=null? info.getTitle():""));
            
                // BannerImage 格式: 790F20EF-270C-4869-BAAD-D9811587022C;6370;6371
                JsonArray bannerImageArray = new JsonArray();
                bannerImageArray.add(SettingKey.getUrl(hostUrl, info!=null&& info.getBannerImageUrl()!=null&&!"".equals(info.getBannerImageUrl())?info.getBannerImageUrl():"", dataSourceEntity.getDbId()+"",null)); 
                /*
                if (info.getBannerImage()!=null && ! info.getBannerImage().equals("")) {
                    String bnannerImages[] = info.getBannerImage().split(";") ;
                    String unid = bnannerImages[0] ;
                    //旧版上传控件上传的内容没有 seq 
                    if (bnannerImages.length == 1) {
                        String bnannerImageUrl = imgData.getImageUrl(unid,settingEntity.getImageBannerWidth(),settingEntity.getImageBannerHeight(),settingEntity.isShowBannerOrgImage(),settingEntity.isFromCached(),request) ;
                        bannerImageArray.add(bnannerImageUrl);
                    }else {
                        //新版上传控件上传的内容有 seq 
                        for (int i =0 ;i<bnannerImages.length;i++) {
                            if (i == 0) continue ;
                            String unids = unid + ";" + bnannerImages[i] ;   //新版上传控件上传的格式会带有 seq 
                            String bnannerImageUrl = imgData.getImageUrl(unids,settingEntity.getImageBannerWidth(),settingEntity.getImageBannerHeight(),settingEntity.isShowBannerOrgImage(),settingEntity.isFromCached(),request) ;
                            bannerImageArray.add(bnannerImageUrl);
                        }
                    }
                }*/
                
                jsonObject.add("bannerImages", bannerImageArray);
            
                jsonObject.addProperty("isShowBannerOnHomePage", (info!=null? info.isShowBannerOnHomePage():false));
                jsonObject.addProperty("isShowBannerOnDetailPage", (info!=null? info.isShowBannerOnDetailPage():false));
                jsonObject.addProperty("description", description);
                jsonArray.add(jsonObject);
            
            }
            json.add("list", jsonArray);
            this.printJson(response, json.toString());
            return ;
 
        }catch (DataAccessException e) {
            e.printStackTrace();
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }catch (Exception e ){
            e.printStackTrace();
            errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }finally {
            SpObserver.setDBtoInstance();
        }    
    }
    
    @RequestMapping(params = "m=get")
    public void getInformationEntity(HttpServletRequest request,HttpServletResponse response) {
        HttpSession session=request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
        String outFormat = request.getParameter("OutFormat") ;
        String  infoDocCode = request.getParameter(SettingKey.INFODOCCODE) ; 
        String title = request.getParameter(SettingKey.TITLE) ;
        InformationEntity info = null ;
        SettingEntity settingEntity = null ;
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID)==null?"":(String)session.getAttribute(SessionKey.WEIXIN_OPENID)) ;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            settingEntity = settingIfc.getSettingEntity(request) ;
            String hostUrl = SettingKey.getHostUrl(request);
            
            //取网店 shopcccode
            ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
            
            //内容
            JsonArray jsonArray = new JsonArray();
            List<InformationDetailEntity> informationDetailList =  informationIfc.getInformationDetail(infoDocCode,title,shopCcCodeEntity.getShopCcCode(),dataSourceEntity.getMiniAppId());
            for (int i = 0 ;informationDetailList != null && i < informationDetailList.size();i++) { 
                InformationDetailEntity informationDetailEntity = informationDetailList.get(i) ;
                if (informationDetailEntity.isShowBottom()) {
                    continue ;
                }
                JsonObject jsonObject=  getJsonObject( informationDetailEntity, request, settingEntity) ; 
                jsonArray.add(jsonObject);
            }
            
            
            
            info = informationIfc.getInformation(infoDocCode,title,shopCcCodeEntity.getShopCcCode());
 
            request.setAttribute(SettingKey.FROMOPENID, openId);
            String description = imgData.getDescriptionByNewImgLink(info!=null?info.getDescription():"", request) ;
            json.addProperty("title", (info!=null? info.getTitle():""));
            json.addProperty(SettingKey.INFODOCCODE, (info!=null? info.getDocCode():""));
            
            // BannerImage 格式: 790F20EF-270C-4869-BAAD-D9811587022C;6370;6371
            JsonArray bannerImageArray = new JsonArray();
            bannerImageArray.add(SettingKey.getUrl(hostUrl, info!=null&& info.getBannerImageUrl()!=null&&!"".equals(info.getBannerImageUrl())?info.getBannerImageUrl():"",  dataSourceEntity.getDbId()+"",null) ); 
            /*
            if (info != null && info.getBannerImage()!=null && ! info.getBannerImage().equals("")) {
                String bnannerImages[] = info.getBannerImage().split(";") ;
                String unid = bnannerImages[0] ;
                //旧版上传控件上传的内容没有 seq 
                if (bnannerImages.length == 1) {
                    String bnannerImageUrl = imgData.getImageUrl(unid,settingEntity.getImageBannerWidth(),settingEntity.getImageBannerHeight(),settingEntity.isShowBannerOrgImage(),settingEntity.isFromCached(),request) ;
                    bannerImageArray.add(bnannerImageUrl);
                }else {
                    //新版上传控件上传的内容有 seq 
                    for (int i =0 ;i<bnannerImages.length;i++) {
                        if (i == 0) continue ;
                        String unids = unid + ";" + bnannerImages[i] ;   //新版上传控件上传的格式会带有 seq 
                        String bnannerImageUrl = imgData.getImageUrl(unids,settingEntity.getImageBannerWidth(),settingEntity.getImageBannerHeight(),settingEntity.isShowBannerOrgImage(),settingEntity.isFromCached(),request) ;
                        bannerImageArray.add(bnannerImageUrl);
                    }
                }
            }*/
            
            json.add("bannerImages", bannerImageArray);
            
            json.addProperty("isShowBannerOnHomePage", (info!=null? info.isShowBannerOnHomePage():false));
            json.addProperty("isShowBannerOnDetailPage", (info!=null? info.isShowBannerOnDetailPage():false));
            json.addProperty("description", description);
            json.add("list", jsonArray);   //内容
            
            //页脚
            JsonArray bottomJsonArray = new JsonArray();
            for (int i = 0 ;informationDetailList != null && i < informationDetailList.size();i++) { 
                InformationDetailEntity informationDetailEntity = informationDetailList.get(i) ;
                if (! informationDetailEntity.isShowBottom()) {
                    continue ;
                }
                JsonObject jsonObject= getJsonObject( informationDetailEntity, request, settingEntity) ;
                bottomJsonArray.add(jsonObject) ;
            }
            json.add("bottomList", bottomJsonArray);   //页脚
            
            if (outFormat!=null&&"json".equalsIgnoreCase(outFormat)) {
                this.printJson(response, json.toString());
            }else {
                this.print(response, description);
            }
            return ;
 
        }catch (DataAccessException e) {
            e.printStackTrace();
            if (outFormat!=null&&"json".equalsIgnoreCase(outFormat)) {
                errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
                json.add("error", errJson);
                this.printJson(response, json.toString());
            }else {
                this.print(response, (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
            }
            return;
        }catch (Exception e ){
            e.printStackTrace();
            if (outFormat!=null&&"json".equalsIgnoreCase(outFormat)) {
                errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
                json.add("error", errJson);
                this.printJson(response, json.toString());
            }else {
                this.print(response, (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
            }
            return;
        }finally {
            SpObserver.setDBtoInstance();
        }    
    }
 
    private JsonObject getJsonObject(InformationDetailEntity informationDetailEntity,HttpServletRequest request,SettingEntity settingEntity) {
        if (informationDetailEntity == null) return null ;
        String hostUrl = SettingKey.getHostUrl(request) ;
        //将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
        
        JsonObject jsonObject= new JsonObject();
        String url = "" ,queryString = "";
        if (informationDetailEntity.getLink() != null && ! "".equals(informationDetailEntity.getLink())) { 
            String urlLink = informationDetailEntity.getLink(isMoblieBrowser) ;
            String flag = urlLink.indexOf("?") >= 0 ?"&":"?" ;
        
            url = (!urlLink.toLowerCase().trim().startsWith("http://")&&!urlLink.toLowerCase().trim().startsWith("https://")? hostUrl:"") + urlLink  + (wxQueryString == null||"".equals(wxQueryString)?"":flag + wxQueryString) ; 
        } 
 
        String imgUrl = imgData.getImageUrl(informationDetailEntity.getImage(),settingEntity.getImageBannerWidth(),settingEntity.getImageBannerHeight(),settingEntity.isShowBannerOrgImage(),settingEntity.isFromCached(),request) ;
        jsonObject.addProperty("url", url==null?"":url);
        jsonObject.addProperty("imgUrl", imgUrl);
        
        //拼接小程序文件路径
        if (informationDetailEntity.getLink()!=null&&!"".equals(informationDetailEntity.getLink())) {
            int pos = informationDetailEntity.getLink().indexOf("?") ;
            if (pos >= 0) {
                queryString = informationDetailEntity.getLink().substring(pos+1);
            }
        }
        String pathUrl = informationDetailEntity.getPathUrl() ;
        if (pathUrl!=null&&!"".equals(pathUrl) && queryString!=null && !"".equals(queryString)) {
            pathUrl +=  (pathUrl.indexOf("?")<0 ?  "?":"&") + queryString ;
        }
        
        if ((pathUrl == null || "".equals(pathUrl)) && informationDetailEntity.getLink() !=null && !"".equals(informationDetailEntity.getLink())) {
            if (informationDetailEntity.getLink().startsWith("http://") || informationDetailEntity.getLink().startsWith("https://")) {
                pathUrl = informationDetailEntity.getLink() ;
            }
        }
        
        jsonObject.addProperty("pathcode", informationDetailEntity.getPathCode());
        jsonObject.addProperty("pathurl", pathUrl);  //小程序路径 
        jsonObject.addProperty("isTabBarButton", informationDetailEntity.isTabBarButton());
        return jsonObject;
    }
    
    @RequestMapping(params = "m=saveenquiry")
    public void saveEnquiry(HttpServletRequest request,HttpServletResponse response) {
        //HttpSession session=request.getSession();
        //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID);
 
        String hostUrl = SettingKey.getHostUrl(request) ;
        //将微信corpid组装成url
        String wxQueryString = SettingKey.getQueryStringByWx(request);
        boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request);
        
        String cltCode = request.getParameter("cltcode");
        String cltName = request.getParameter("cltname");
        String telephone = request.getParameter("telephone");
        String email = request.getParameter("email");
        String enquiry = request.getParameter("enquiry") ;
        
        JsonObject json = new JsonObject() ;
        JsonObject errJson = new JsonObject() ;
        if (cltName== null || "".equals(cltName) || cltName.length()<2 ) {
            errJson.addProperty("cltname", "名字应在 3 至 32 个字符之间!");
        }
        if (telephone== null || "".equals(telephone) || telephone.length()<11 ) {
            errJson.addProperty("telephone", "电话应在 11 至 32 个字符之间!");
        }    
        if (enquiry== null || "".equals(enquiry) || enquiry.length()<10 ) {
            errJson.addProperty("enquiry", "咨询内容长度应在 10 至 3000 个字符之间!");
        }
        
        Set<Entry<String, JsonElement>> it = errJson.entrySet();
        if (! it.isEmpty() && it.size() != 0) {
            //{"error":{"firstname":"\u540d\u5b57\u5fc5\u987b\u5728 1 \u81f3 32 \u5b57\u7b26\u4e4b\u95f4\uff01","lastname":"\u59d3\u6c0f\u5fc5\u987b\u5728 1 \u81f3 32 \u5b57\u7b26\u4e4b\u95f4\uff01","email":"\u65e0\u6548\u7684\u90ae\u7bb1\u5730\u5740\uff01","telephone":"\u7535\u8bdd\u53f7\u7801\u5fc5\u987b\u4e3a 3 \u81f3 32 \u5b57\u8282\uff01","address_1":"\u5730\u5740\u957f\u5ea6\u5fc5\u987b\u5728 3 \u81f3 128 \u5b57\u7b26\u4e4b\u95f4\uff01","city":"\u57ce\u5e02\u540d\u5fc5\u987b\u5728 3 \u81f3 128 \u5b57\u8282\u4e4b\u95f4\uff01","zone":"\u8bf7\u9009\u62e9\u5730\u533a\/\u7701\u4efd\uff01","password":"\u5bc6\u7801\u5fc5\u987b\u5728 3 \u81f3 20 \u5b57\u7b26\u4e4b\u95f4\uff01","warning":"\u8b66\u544a\uff1a\u60a8\u672a\u540c\u610f\u6761\u6b3e Privacy Policy\uff01"}}
            //errJson.addProperty("warning", "警告:您未同意条款 Privacy Policy!");
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }
        SettingEntity settingEntity = null ;
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            settingEntity = settingIfc.getSettingEntity(request) ;
            
            //取网店 shopcccode
            ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request);
            
            informationIfc.saveEnquiry(cltCode, cltName, telephone, email, enquiry,shopCcCodeEntity.getShopCcCode());
            json.addProperty("redirect", hostUrl+"/shopping/" + (isMoblieBrowser?"mobile/":"") + "information/contactsuccess.jsp"+(wxQueryString == null||"".equals(wxQueryString)?"":"?" + wxQueryString));
            this.printJson(response, json.toString());
            return ;
        } catch( DataAccessException e) {
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }catch( Exception e) {
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage():e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return ;
        }finally {
            SpObserver.setDBtoInstance();
        }
        
        
    }
    
}