No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shop_address.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. if (typeof __isMobile__ == 'undefined') {
  2. // 如果未定义则通过JQ判断是否为移动端
  3. var __isMobile__ = navigator.userAgent.match(/mobile/i) ? 1 : 0;
  4. }
  5. // 收货地址url
  6. var shop_add_address = eyou_basefile + "?m=user&c=Shop&a=shop_add_address&_ajax=1";
  7. var shop_edit_address = eyou_basefile + "?m=user&c=Shop&a=shop_edit_address&_ajax=1";
  8. // 显示地址选择框
  9. function showAddressWindow() {
  10. $('.address-select-box').css('display', 'block');
  11. if (0 === parseInt($('#city').val())) {
  12. showAddressList('province');
  13. } else if (0 === parseInt($('#district').val())) {
  14. showAddressList('city');
  15. } else if (0 !== parseInt($('#district').val())) {
  16. showAddressList('district');
  17. $(".address-selectd .gray").text('');
  18. }
  19. }
  20. // 显示地址列表
  21. function showAddressList(type) {
  22. // 隐藏全部地址列表
  23. $('.address-list-province, .address-list-city, .address-list-district').css('display', 'none');
  24. // 显示对应地址列表
  25. if (type) $('.address-list-' + type).css('display', 'block');
  26. }
  27. // 查询选中地址下级地址列表
  28. function showSelectAddress(obj, type) {
  29. var parent_id = $(obj).val();
  30. if (!parent_id) parent_id = $(obj).data('id');
  31. if (!parent_id) return false;
  32. $.ajax({
  33. url : eyou_basefile + "?m=user&c=Shop&a=get_region_data&_ajax=1",
  34. data: {parent_id: parent_id},
  35. type: 'post',
  36. dataType: 'json',
  37. success: function(res) {
  38. // 选择省份地址处理
  39. if ('province' == type) {
  40. if (1 === parseInt(__isMobile__)) {
  41. var options = '<option value="0">请选择城市</option>';
  42. $.each(res.data, function(k,e) {
  43. options += "<option value='"+e.id+"'>"+e.name+"</option>";
  44. });
  45. $('#city').empty().html(options);
  46. $('#district').empty().html('<option value="0">请选择县/区/镇</option>');
  47. } else {
  48. // 处理页面显示
  49. var text = $(obj).text();
  50. if (0 < parseInt($(".address-selectd .province").length)) {
  51. $(".address-selectd .province").text(text);
  52. } else {
  53. $(".address-selectd").prepend("<span class='province' onclick=\"showAddressList('province');\">"+text+"</span>");
  54. }
  55. $(".address-selectd .city").remove();
  56. $(".address-selectd .district").remove();
  57. $(".address-selectd .gray").text('选择城市/地区');
  58. // 加载地址列表
  59. var html = '';
  60. $.each(res.data, function(k,e) {
  61. html += "<span data-id='"+e.id+"' onclick=\"showSelectAddress(this, 'city')\">"+e.name+"</span>";
  62. });
  63. showAddressList('city');
  64. $('#city, #district').val(0);
  65. $('#province').val(parent_id);
  66. $('.address-list-city').empty().html(html);
  67. // 加载选中地址名
  68. $('#address-title').val(text);
  69. }
  70. }
  71. // 选择市区地址处理
  72. else if ('city' == type) {
  73. if (1 === parseInt(__isMobile__)) {
  74. var options = '<option value="0">请选择县/区/镇</option>';
  75. $.each(res.data, function(k,e) {
  76. options += "<option value='"+e.id+"'>"+e.name+"</option>";
  77. });
  78. $('#district').empty().html(options);
  79. } else {
  80. // 处理页面显示
  81. var text = $(obj).text();
  82. if (0 < parseInt($(".address-selectd .city").length)) {
  83. $(".address-selectd .city").text(text);
  84. } else {
  85. $(".address-selectd .province").after("<span class='city' onclick=\"showAddressList('city');\">"+text+"</span>");
  86. }
  87. $(".address-selectd .district").remove();
  88. $(".address-selectd .gray").text('选择区县');
  89. // 加载地址列表
  90. var html = '';
  91. $.each(res.data, function(k,e) {
  92. html += "<span data-id='"+e.id+"' onclick=\"showSelectAddress(this, 'district')\">"+e.name+"</span>";
  93. });
  94. showAddressList('district');
  95. $('#district').val(0);
  96. $('#city').val(parent_id);
  97. $('.address-list-district').empty().html(html);
  98. // 加载选中地址名
  99. $('#address-title').val($(".address-selectd .province").text() + ' ' + text);
  100. }
  101. }
  102. // 选择县区地址处理
  103. else if ('district' == type) {
  104. if (0 === parseInt(__isMobile__)) {
  105. // 处理页面显示
  106. var text = $(obj).text();
  107. if (0 < parseInt($(".address-selectd .district").length)) {
  108. $(".address-selectd .district").text(text);
  109. } else {
  110. $(".address-selectd .city").after("<span class='district' onclick=\"showAddressList('district');\">"+text+"</span>");
  111. }
  112. showAddressList();
  113. $('#district').val(parent_id);
  114. $('.address-select-box').css('display', 'none');
  115. // 加载选中地址名
  116. $('#address-title').val($(".address-selectd .province").text() + ' ' + $(".address-selectd .city").text() + ' ' + text);
  117. }
  118. handleParam(false);
  119. }
  120. },
  121. error: function(e) {
  122. layer.closeAll();
  123. if (1 === parseInt(__isMobile__)) {
  124. showLayerAlert(e.responseText);
  125. } else {
  126. showErrorAlert(e.responseText);
  127. }
  128. }
  129. });
  130. }
  131. // 添加收货地址(PC端)
  132. function addPcAddress(types) {
  133. if (!handleParam(true)) return false;
  134. $.ajax({
  135. url : shop_add_address,
  136. data: $('#theForm').serialize(),
  137. type: 'post',
  138. dataType: 'json',
  139. success: function(res) {
  140. layer.closeAll();
  141. if (res.code == 1) {
  142. var loadContent = addressLoadContent(res.data);
  143. addressLoadHandle(loadContent, 'add', types);
  144. } else {
  145. showErrorMsg(res.msg);
  146. }
  147. },
  148. error: function(e) {
  149. layer.closeAll();
  150. showErrorAlert(e.responseText);
  151. }
  152. });
  153. }
  154. // 编辑收货地址(PC端)
  155. function editPcAddress() {
  156. if (!handleParam(true)) return false;
  157. $.ajax({
  158. url : shop_edit_address,
  159. data: $('#theForm').serialize(),
  160. type: 'post',
  161. dataType: 'json',
  162. success: function(res) {
  163. layer.closeAll();
  164. if (res.code == 1) {
  165. addressLoadHandle(res.data, 'edit');
  166. } else {
  167. showErrorMsg(res.msg);
  168. }
  169. },
  170. error: function(e) {
  171. layer.closeAll();
  172. showErrorAlert(e.responseText);
  173. }
  174. });
  175. }
  176. // 添加收货地址(移动端)
  177. function addMoveAddress() {
  178. if (!handleParam(true)) return false;
  179. $.ajax({
  180. url : shop_add_address,
  181. data: $('#theForm').serialize(),
  182. type: 'post',
  183. dataType: 'json',
  184. success: function(res) {
  185. layer.closeAll();
  186. if (res.code == 1) {
  187. var _parent = parent;
  188. var parentObj = parent.layer.getFrameIndex(window.name);
  189. parent.layer.close(parentObj);
  190. parent.showLayerMsg(res.msg, 2, function() {
  191. _parent.returnUrl(res.data.url);
  192. });
  193. } else {
  194. showLayerMsg(res.msg);
  195. }
  196. },
  197. error: function(e) {
  198. layer.closeAll();
  199. showLayerAlert(e.responseText);
  200. }
  201. });
  202. }
  203. // 添加收货地址(移动端)
  204. function editMoveAddress() {
  205. if (!handleParam(true)) return false;
  206. $.ajax({
  207. url : shop_edit_address,
  208. data: $('#theForm').serialize(),
  209. type: 'post',
  210. dataType: 'json',
  211. success: function(res) {
  212. layer.closeAll();
  213. if (res.code == 1) {
  214. var _parent = parent;
  215. var parentObj = parent.layer.getFrameIndex(window.name);
  216. parent.layer.close(parentObj);
  217. parent.showLayerMsg(res.msg, 2, function() {
  218. _parent.returnUrl(res.data.url);
  219. });
  220. } else {
  221. showLayerMsg(res.msg);
  222. }
  223. },
  224. error: function(e) {
  225. layer.closeAll();
  226. showLayerAlert(e.responseText);
  227. }
  228. });
  229. }
  230. // 地址管理页追加地址html
  231. function addressLoadContent(data) {
  232. var divhtml = $('#divhtml').html();
  233. var strings = '';
  234. // 替换ID值
  235. if (1 === parseInt(parent.pointsShop)) {
  236. strings = divhtml.replace('#ul_li_id#', 'address-list-' + data.addr_id);
  237. } else {
  238. strings = divhtml.replace('#ul_li_id#', data.addr_id + "_ul_li");
  239. }
  240. strings = strings.replace('#consigneeid#', data.addr_id + "_consignee");
  241. strings = strings.replace('#mobileid#', data.addr_id + "_mobile");
  242. strings = strings.replace('#infoid#', data.addr_id + "_info");
  243. strings = strings.replace('#addressid#', data.addr_id + "_address");
  244. // 替换地址内容信息
  245. strings = strings.replace('#consignee#', data.consignee);
  246. strings = strings.replace('#mobile#', data.mobile);
  247. strings = strings.replace('#info#', data.country+" "+data.province+" "+data.city+" "+data.district);
  248. strings = strings.replace('#address#', data.address);
  249. // 替换JS方法
  250. if (1 === parseInt(parent.pointsShop)) {
  251. strings = strings.replace('#selected#', "selectAddress('" + data.addr_id + "');");
  252. strings = strings.replace('#shopeditaddr#', "editAddress('" + data.addr_id + "');");
  253. strings = strings.replace('#shopdeladdr#', "delAddress('" + data.addr_id + "');");
  254. } else {
  255. strings = strings.replace('#selected#', "SelectEd('addr_id','" + data.addr_id + "');");
  256. strings = strings.replace('#setdefault#', "SetDefault(this, '" + data.addr_id + "');\" data-is_default=\"0\" id=\"" + data.addr_id + "_color\" data-setbtn=\"1\" data-attr_id=\"" + data.addr_id + "\"");
  257. strings = strings.replace('#shopeditaddr#', "ShopEditAddress('" + data.addr_id + "');");
  258. strings = strings.replace('#shopdeladdr#', "ShopDelAddress('" + data.addr_id + "');");
  259. }
  260. // 隐藏域,下单页第一次添加收货地址则出现一次,存在则替换数据
  261. strings = strings.replace('#name#', "addr_id");
  262. strings = strings.replace('#id#', "addr_id");
  263. strings = strings.replace('#value#', data.addr_id);
  264. return strings;
  265. }
  266. // 收货地址加载处理
  267. function addressLoadHandle(loadContent, action, types) {
  268. if ('add' == action) {
  269. // 加载指定收货地址信息
  270. if ('list' == types) {
  271. parent.$('#UlHtml').find('div.address-item:last').after(loadContent);
  272. parent.$('#address-list-all').find('div.address-item:last').after(loadContent);
  273. } else if ('order' == types) {
  274. if (3 <= parseInt(parent.$('#UlHtml div.address-item').length) || 3 <= parseInt(parent.$('#address-list-all div.address-item').length)) {
  275. parent.$('#addressShowHide').attr('data-showhide', 'hide').show().click();
  276. }
  277. parent.$('#UlHtml').find('div.address-item:last').before(loadContent);
  278. parent.$('#address-list-all').find('div.address-item:last').before(loadContent);
  279. }
  280. } else if ('edit' == action) {
  281. // 更新指定收货地址信息
  282. parent.$('#'+loadContent.addr_id+'_mobile').html(loadContent.mobile);
  283. parent.$('#'+loadContent.addr_id+'_address').html(loadContent.address);
  284. parent.$('#'+loadContent.addr_id+'_consignee').html(loadContent.consignee);
  285. parent.$('#'+loadContent.addr_id+'_info').html(loadContent.province +' '+ loadContent.city +' '+ loadContent.district);
  286. }
  287. parent.layer.closeAll();
  288. }
  289. // 参数处理
  290. function handleParam(showMsg) {
  291. if (!$("#consignee").val()) {
  292. $("#consignee").focus();
  293. if (showMsg && 1 === parseInt(__isMobile__)) showLayerMsg('请输入联系人');
  294. return false;
  295. }
  296. if (!$("#mobile").val()) {
  297. $("#mobile").focus();
  298. if (showMsg && 1 === parseInt(__isMobile__)) showLayerMsg('请输入联系电话');
  299. return false;
  300. }
  301. if ($("#province").val() == 0 || $("#city").val() == 0 || $("#district").val() == 0) {
  302. if (showMsg && 1 === parseInt(__isMobile__)) showLayerMsg('请选择完整省市区');
  303. return false;
  304. }
  305. if (!$("#address").val()) {
  306. $("#address").focus();
  307. if (showMsg && 1 === parseInt(__isMobile__)) showLayerMsg('请输入详细地址');
  308. return false;
  309. }
  310. if (showMsg) {
  311. if (1 === parseInt(__isMobile__)) {
  312. showLayerLoad();
  313. } else {
  314. layer_loading('正在处理');
  315. }
  316. }
  317. return true;
  318. }
  319. // 地址选择框
  320. function layerColse() {
  321. var parentObj = parent.layer.getFrameIndex(window.name);
  322. parent.layer.close(parentObj);
  323. }