xx-controller1
createAddressCtrl.js
1 /**
2 * Created on 2015/4/13.
3 */
4 /*global
5 VERSION_KEY_HK
6 customiseVer
7 commons
8 ENT_RSP_SUCCESS
9 */
10 define(["language/usercenter", "language/v1r2", "language/custom", "language/errMsg"],
11 function (i18user, i18v1r2,i18cus,i18Err) {
12 "use strict";
13
14 var createAddress = ["$scope", "$state", "$rootScope", "validator", "$stateParams", "invoiceService", "tiModalInstance", "tiValid", "csbMessage", "addressObj", "utilService",
15 function ($scope, $state, $rootScope, validator, $stateParams, invoiceService, tiModalInstance, tiValid, csbMessage, addressObj, utilService) {
16 if ($rootScope.i18user == null || $rootScope.i18user == undefined || $rootScope.i18user == "") {
17 $rootScope.i18user = i18user;
18 }
19 if ($rootScope.i18v1r2 == null || $rootScope.i18v1r2 == undefined || $rootScope.i18v1r2 == "") {
20 $rootScope.i18v1r2 = i18v1r2;
21 }
22 if ($rootScope.i18cus == null || $rootScope.i18cus == undefined || $rootScope.i18cus == "") {
23 $rootScope.i18cus = i18cus;
24 }
25 if ($rootScope.i18Err == null || $rootScope.i18Err == undefined || $rootScope.i18Err == "") {
26 $rootScope.i18Err = i18Err;
27 }
28
29 var addressItem = addressObj;
30 //==============自维护数据转接口数据适配代码;
31 if("香港" == addressItem.nationality || "Hong Kong" == addressItem.nationality) {
32 addressItem.province = addressItem.nationality;
33 }
34 //1.10.4版本:地址省、市、区由用户中心自维护数据转为调用合作伙伴域接口实现。两边的数据差异(即无法字符串匹配的数据)。key:用户中心;value:合作伙伴域
35 var addressTransfer = {
36 "Neimenggu":"Nei Mongol",
37 "Alashan":"Alxa Meng",
38 "Huhehaote":"Hohhot",
39 "Hulunbuir":"Hulun Buir",
40 "Erdos":"Ordos",
41 "Xilinguole":"Xilin Gol Meng",
42 "Alar":"Ala'er",
43 "Hetian":"Hotan Diqu",
44 "Yili":"Ili Kazak Zizhizhou",
45 "Kelamayi":"Karamay",
46 "Kashgar":"Kashi(Kaxgar) Diqu",
47 "Tumxuk":"Tumushuke"
48 };
49 //CBC接口无港澳台数据。自己拼接
50 var otherprovince = i18cus.other_province;
51 /*var zhixiashi = ["CN-11","CN-12","CN-31","CN-50"]; //直辖市*/
52
53 var districtDefault = {
54 id:-1,
55 label:i18v1r2.county_region
56 };
57 var cityDefault = {
58 id:-1,
59 label:i18v1r2.city_region
60 };
61
62 function selectedCheck(id) {
63 return !(id == "-1" || id == null || id == undefined || $.trim(id).length == 0);
64 }
65
66 function zipCodeCheck(id) {
67 return /^[0-9]{6}$/.test(id);
68 }
69
70 function mobilePhoneCheck(id) {
71 id = id.replace(/\s/g, "");
72 return id.length == 11 && validator.mobilePhoneCheck_R2(id);
73
74 }
75
76 //特殊字符限制
77 function specialCharCheck(id) {
78 if (id == null || id == undefined || $.trim(id).length == 0) {
79 return false;
80 }
81 return validator.specialCharCheckValidator(id); //tip_3_0
82
83 }
84
85 //特殊字符限制,不限制"- #"
86 function subSpecialCharCheck(id) {
87 if (id == null || id == undefined || $.trim(id).length == 0) {
88 return false;
89 }
90 return validator.subSpecialCharCheckValidator_2(id); //tip_3_2
91 }
92
93 //固话号码验证
94 function telePhoneCheck (value) {
95 return !(value && !(/^[0-9\-\+\(\) ]{1,32}$/.test(value)));
96 }
97
98 //默认区选项
99 function setDefaultDistrictOptions() {
100 $scope.districtMapArr = [districtDefault];
101 $scope.district.selectedId = -1;
102 $scope.selectedDistrict = $scope.districtMapArr[0];
103 }
104
105 //设置区县选项 TODO:还有个问题:区县貌似没有英文
106 function setDistrictOptions(cityCode) {
107 var districtList = [],district="",selectdIndex = 0,options=[];
108 if(-1 == cityCode) {
109 setDefaultDistrictOptions();
110 if(!$scope.initAreaTag) {
111 tiValid.check($("#district"));
112 }
113 return;
114 }
115
116 options = [districtDefault];
117 district = addressItem.district ? $.trim(addressItem.district) : "";
118 invoiceService.getDistrictList(cityCode).then(function(data) {
119 if(ENT_RSP_SUCCESS == data.error_code) {
120 districtList = data.result.countyList;
121 for(var i = 0; i < districtList.length; i++) {
122 if(district && $scope.initAreaTag) { //初次匹配默认值。
123 if(-1 != districtList[i].chineseName.indexOf(district) || (districtList[i].englishName && -1 != districtList[i].englishName.toLowerCase().indexOf(district.toLowerCase()))) {
124 selectdIndex = i + 1;
125 }
126 }
127 if($rootScope.currentLanguage !== 'en-us') {
128 options.push({
129 id:districtList[i].alpha2Code,
130 label:districtList[i].chineseName
131 });
132 }else if(districtList[i].englishName) {
133 options.push({
134 id:districtList[i].alpha2Code,
135 label:districtList[i].englishName
136 });
137 }
138 }
139 if(1 == options.length && $rootScope.currentLanguage === 'en-us') { //现网没有英文数据时,固定给一个全区
140 options.push({
141 id:0,
142 label:"All areas"
143 });
144 }
145 if(0 == selectdIndex) {
146 selectdIndex = 1;
147 }
148 }
149 }).finally(function() {
150 $scope.districtMapArr = options;
151 $scope.district.selectedId = options[selectdIndex].id;
152 $scope.selectedDistrict = options[selectdIndex];
153 if(!$scope.initAreaTag) {
154 tiValid.check($("#district"));
155 }
156 $scope.initAreaTag = false;
157 });
158 }
159
160 //默认市选项
161 function setDefaultCityOptions() {
162 $scope.cityMapArr = [cityDefault];
163 $scope.city.selectedId = -1;
164 $scope.selectedCity = $scope.cityMapArr[0];
165 }
166
167
168 //设置市/区选项
169 function setCityOptions(provinceCode) {
170 var cityList = [],city="",options=[],selectdIndex = 0;
171 if(-1 == provinceCode) {
172 setDefaultCityOptions();
173 setDistrictOptions(-1);
174 if(!$scope.initAreaTag) {
175 tiValid.check($("#city"));
176 }
177 return;
178 }
179 options = [cityDefault];
180 //差异数据适配为接口数据;
181 if(addressItem.city && addressTransfer[addressItem.city]) {
182 addressItem.city = addressTransfer[addressItem.city];
183 }
184 city = addressItem.city ? $.trim(addressItem.city):"";
185 //数据过渡,匹配港澳台数据
186 if("32" === provinceCode || "33" === provinceCode || "34" === provinceCode ) {
187 for(var i = 0; i < otherprovince.length; i++) {
188 if(otherprovince[i].alpha2Code == provinceCode) {
189 cityList = otherprovince[i].city;
190 for(var j = 0; j < cityList.length; j++) {
191 options.push({
192 id:cityList[j].alpha2Code,
193 label:$rootScope.currentLanguage === 'en-us'? cityList[j].englishName : cityList[j].chineseName
194 });
195 if(city && $scope.initAreaTag) {
196 if(-1 != cityList[j].chineseName.indexOf(city) ||
197 -1 != cityList[j].englishName.toLowerCase().indexOf(city.toLowerCase()) ||
198 -1 != city.indexOf(cityList[j].shortNameCn)
199 ) {
200 selectdIndex = j + 1; //有个默认选项。
201 }
202 }
203 }
204 }
205 }
206 if(0 == selectdIndex) { //
207 selectdIndex = 1; //默认选中第一个。
208 }
209 $scope.cityMapArr = options;
210 $scope.city.selectedId = options[selectdIndex].id;
211 $scope.selectedCity = options[selectdIndex];
212 if(!$scope.initAreaTag) {
213 tiValid.check($("#city"));
214 }
215 setDefaultDistrictOptions();
216 $scope.initAreaTag = false;
217 return;
218 }
219
220 invoiceService.getCityList(provinceCode).then(function(data) {
221 if("0" == data.code) {
222 cityList = data.result.cityList;
223 for(var k = 0; k < cityList.length; k++) {
224 if(city && $scope.initAreaTag) {
225 if(-1 != cityList[k].chineseName.indexOf(city) || -1 != cityList[k].englishName.toLowerCase().indexOf(city.toLowerCase())) {
226 selectdIndex = k + 1; //有个默认选项。
227 }
228 }
229 options.push({
230 id:cityList[k].alpha2Code,
231 label:$rootScope.currentLanguage === 'en-us'? cityList[k].englishName : cityList[k].chineseName
232 });
233 }
234 if(0 == selectdIndex) {
235 selectdIndex = 1;
236 }
237 }
238 }).finally(function() {
239 $scope.cityMapArr = options;
240 $scope.selectedCity = options[selectdIndex];
241 $scope.city.selectedId = options[selectdIndex].id;
242 if(!$scope.initAreaTag) {
243 tiValid.check($("#city"));
244 }
245 setDistrictOptions($scope.city.selectedId);
246 });
247 }
248
249 //设省/市选项
250 function setProviceOptions() {
251 var provinceList = [],province="",selectdIndex = 0,options=[];
252 //香港版本只展示香港,且不展示区县
253 if($scope.isHKStation) {
254 $scope.provinceMapArr = [{
255 id:otherprovince[0].alpha2Code,
256 label:$rootScope.currentLanguage === 'en-us'?otherprovince[0].englishName : otherprovince[0].chineseName,
257 checked:true
258 }];
259 $scope.province.selectedId = otherprovince[0].alpha2Code;
260 $scope.selectedProvince = $scope.provinceMapArr[0];
261 tiValid.check($("#province"));
262 setDefaultCityOptions();
263 setDefaultDistrictOptions();
264 return;
265 }
266 //非香港版本
267 options = [{
268 id:-1,
269 label:i18v1r2.province_city
270 }];
271 //差异数据适配为接口数据;
272 if(addressItem.province && addressTransfer[addressItem.province]) {
273 addressItem.province = addressTransfer[addressItem.province];
274 }
275 province = addressItem.province ? $.trim(addressItem.province):"";
276 invoiceService.getProvinceList().then(function(data) {
277 if("0" == data.code) {
278 provinceList = data.result.provinceList.concat(otherprovince);//数据过渡 匹配代码:拼接港澳台。
279 for(var i = 0; i < provinceList.length; i++) {
280 if(province && $scope.initAreaTag) {
281 if(-1 != provinceList[i].chineseName.indexOf(province) ||
282 -1 != provinceList[i].englishName.toLowerCase().indexOf(province.toLowerCase()) ||
283 -1 != province.indexOf(provinceList[i].shortNameCn)
284 ) {
285 selectdIndex = i + 1; //有个默认选项。
286 }
287 }
288 options.push({
289 id:provinceList[i].alpha2Code,
290 label:$rootScope.currentLanguage === 'en-us'? provinceList[i].englishName : provinceList[i].chineseName
291 });
292 }
293 }
294 }).finally(function() {
295 $scope.provinceMapArr = options;
296 $scope.province.selectedId = options[selectdIndex].id;
297 $scope.selectedProvince = options[selectdIndex];
298 if(!$scope.initAreaTag) {
299 tiValid.check($("#province"));
300 }
301
302 if(-1 != $scope.province.selectedId) {
303 setCityOptions($scope.province.selectedId);
304 }else{
305 setDefaultCityOptions();
306 setDefaultDistrictOptions();
307 if(!$scope.initAreaTag) {
308 tiValid.check($("#city"