shop.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. var ProductInfo = "";
  2. var number = "";
  3. $(document).ready(function () {
  4. init();
  5. });
  6. /*
  7. * 页面初始化函数
  8. * */
  9. init = function () {
  10. var pid = getUrlParam("pid"),
  11. attr = getUrlParam("attr");
  12. // num = getUrlParam("num");
  13. if (pid === "" || attr === "") {
  14. go404Html();
  15. }
  16. else {
  17. $(".goback").attr("href", "goods.html?pid=" + pid);
  18. getProductInfo(pid);
  19. initialize();
  20. }
  21. }
  22. getProductInfo = function (pid) {
  23. var parameter = urlEncode(["SelectProduct", pid]);
  24. $.ajax({
  25. type: "post",
  26. url: "/taiwan/php/index.php",
  27. data: {mode: parameter},
  28. dataType: "json",
  29. success: function (data) {
  30. printProductInfo(data);
  31. }
  32. });
  33. }
  34. printProductInfo = function (data) {
  35. if (data == -1 || data[0].length === 0) {
  36. go404Html();
  37. }
  38. else {
  39. $("#PageLoading").css({"display": "none"});
  40. $("#PageWarp").css({"display": "block"});
  41. ProductInfo = data[0][0];
  42. // $("#goodName").html(data[0][0].ProductIntroduce);
  43. var attr = JSON.parse(unescape(getUrlParam("attr")));
  44. $(".m-orderItem").html("");
  45. for (var i in attr) {
  46. if (i == 0) {
  47. $("#payment_amount").html(parseInt(attr[0].Money) * parseInt(attr[0].Number));
  48. $("#total").html(parseInt(attr[0].Money) * parseInt(attr[0].Number));
  49. number = attr[0].Number;
  50. ProductInfo.ProductPrice = parseInt(attr[0].Money) * parseInt(attr[0].Number);
  51. $(".number").html(attr[0].Number);//数量
  52. }
  53. if (i != 0) {
  54. var a = '<div class="row-2 flexbox">' +
  55. '<div class="goodImg">' +
  56. '<img src="' + attr[i]['img'] + '" width="100%"/>' +
  57. '</div>' +
  58. ' <div class="goodInfo flex">\n' +
  59. ' <div class="goodName">' + ProductInfo.ProductName + '</div>\n' +
  60. ' <div class="goodName ti">\n' +
  61. ' <div class="goodStatus f-fr"><span>顏色分類: ' + attr[i]['color'] + '</span></div>' +
  62. (attr[i]['size'] ? '<div class="goodStatus f-fr"><span>ขนาด:' + attr[i]["size"] + '</span></div>' : '') +
  63. '<div class="goodStatus f-fr"><span>數量: ' + attr[0].Number + '</span></div>' +
  64. ' </div>' +
  65. '</div>' +
  66. '</div>';
  67. $(".m-orderItem").append(a);
  68. }
  69. }
  70. }
  71. }
  72. submit = function () {
  73. var lid = "",
  74. ordernum = orderNum(),
  75. pid = getUrlParam("pid"),
  76. pcid = escape(getUrlParam("attr")),
  77. attr = "",
  78. ppr = ProductInfo.ProductPrice,
  79. pn = number,
  80. urn = $("#UserReallyName").val().trim(),
  81. upn = $("#thaphone").val().trim(),
  82. uem = $("#UserEMail").val().trim(),
  83. pc = $("#tha_postal").val().trim(),
  84. p = $("#Province").html(),
  85. c = "",
  86. a = $("#Area").val().trim(),
  87. address = $("#Address").val().trim(),
  88. rem = $("#Remarks").val().trim(),
  89. fr = 0,
  90. am = ProductInfo.ProductPrice,
  91. pay = ProductInfo.ProductPrice,
  92. state = 1;
  93. if (urn == "") {
  94. // alert("请输入姓名");
  95. alert("請輸入姓名");
  96. return;
  97. }
  98. else if (upn == "") {
  99. // alert("请输入手机号码");
  100. alert("請輸入手機號碼");
  101. return;
  102. }
  103. /*
  104. else if (uem == "") {
  105. // alert("请输入电子邮件");
  106. alert("請輸入電子郵件");
  107. return;
  108. }
  109. */
  110. else if (pc == "") {
  111. // alert("请输入邮编");
  112. alert("請輸入郵政編碼");
  113. return;
  114. }
  115. else if (p == "") {
  116. // alert("请输入省份");
  117. alert("請輸入省份");
  118. return;
  119. }
  120. else if (a == "") {
  121. // alert("请输入直辖区");
  122. alert("請輸入直轄區");
  123. return;
  124. }
  125. else if (address == "") {
  126. // alert("请输入详细地址");
  127. alert("請輸入詳細地址");
  128. return;
  129. }
  130. /*
  131. else if (rem == "") {
  132. // alert("请输入留言");
  133. alert("請輸入留言");
  134. return;
  135. }
  136. */
  137. $("#PageLoading").css({"display": "block"});
  138. $("#PageWarp").css({"display": "none"});
  139. var parameter = urlEncode(["InsertOrder", lid, ordernum, pid, pcid, attr, ppr, pn, urn, upn, uem, pc, p, c, a, address, rem, fr, am, pay, state]);
  140. $.ajax({
  141. type: "post",
  142. url: "/taiwan/php/index.php",
  143. data: {mode: parameter},
  144. dataType: "json",
  145. success: function (data) {
  146. if (data != -1 && data[0].OrderId) {
  147. $("#PageLoading").css({"display": "none"});
  148. $("#PageWarp").css({"display": "block"});
  149. alert("購買成功!");
  150. window.location.href = "goods.html?pid=" + getUrlParam("pid");
  151. // insertUserIpAddress(ordernum, function () {
  152. //
  153. // });
  154. }
  155. else {
  156. alert("對不起,您購買失敗,請聯繫客服!!!");
  157. }
  158. }
  159. });
  160. //facebook推广购物事件,请不要删除!!!!!
  161. fbq('track', 'Purchase', {
  162. value: ProductInfo.ProductPrice,
  163. currency: 'TWD',
  164. content_ids: ProductInfo.ProductRemarks,
  165. content_type: 'product_group'
  166. });
  167. }
  168. /*
  169. * 获取下单用户ip地址插入数据库
  170. * */
  171. insertUserIpAddress = function (num, cb) {
  172. var IPAddress = ipaddress.traits.ip_address,
  173. country = ipaddress.country.names["zh-CN"],
  174. city = ipaddress.city.names["zh-CN"],
  175. parameter = urlEncode(["InsertOrderIPAddress", num, IPAddress, country, city]);
  176. $.ajax({
  177. type: "post",
  178. url: "/taiwan/php/index.php",
  179. data: {mode: parameter},
  180. dataType: "json",
  181. success: function (data) {
  182. if (data == 1) {
  183. cb && cb();
  184. }
  185. }
  186. });
  187. }
  188. /*
  189. * 根据当前时间,生成订单号
  190. * */
  191. orderNum = function () {
  192. var vNow = new Date(), sNow = "";
  193. sNow += String(vNow.getFullYear());
  194. sNow += String(vNow.getMonth() + 1);
  195. sNow += String(vNow.getDate());
  196. sNow += String(vNow.getHours());
  197. sNow += String(vNow.getMinutes());
  198. sNow += String(vNow.getSeconds());
  199. sNow += String(vNow.getMilliseconds());
  200. return sNow;//返回订单号
  201. }
  202. /*
  203. * 输入泰国指定的邮政编码,生成指定的泰国地址。
  204. * */
  205. // selectCity = function () {
  206. // $('#tha_postal').change(function () {
  207. // var id = $(this).val();
  208. // ajaxLocked = true;
  209. // $.ajax({
  210. // url: '/taiwan/php/Forward.php',
  211. // type: 'post',
  212. // data: {'postName': id},
  213. // dataType: 'json',
  214. // success: function (ret) {
  215. // if (ret) {
  216. // if (ret.length != 0) {
  217. // var option = "";
  218. // for (var i in ret) {
  219. // option += '<option value="' + ret[i].name + '" data-city="' + ret[i].parent_name + '">' + ret[i].name + '</option>';
  220. // $('.regions').html(ret[0].parent_name);
  221. // $('input[name="city"]').val(ret[0].parent_name);
  222. // }
  223. // $(".tha_district").html('').append(option);
  224. // ajaxLocked = false;
  225. // } else {
  226. // alert('郵政編碼不存在!');
  227. // $('#tha_postal').val('');
  228. // $('.tha_district').html('');
  229. // $('.regions').html('');
  230. // $('input[name="city"]').val('');
  231. // ajaxLocked = true;
  232. // }
  233. // } else {
  234. // alert('郵政編碼不存在!');
  235. // $('#tha_postal').val('');
  236. // $('.tha_district').html('');
  237. // $('.regions').html('');
  238. // $('input[name="city"]').val('');
  239. // ajaxLocked = true;
  240. // }
  241. // },
  242. // error: function (xhr, ajaxOptions, thrownError) {
  243. // }
  244. // });
  245. // });
  246. // }
  247. /*
  248. * 選擇台灣的城市
  249. */
  250. //province(城市類类)
  251. function provinceList()
  252. {
  253. this.length=24;
  254. this[0] = new Option("臺北市","0");
  255. this[1] = new Option("高雄市","1");
  256. this[2] = new Option("基隆市","2");
  257. this[3] = new Option("新竹市","3");
  258. this[4] = new Option("臺中市","4");
  259. this[5] = new Option("嘉儀市","5");
  260. this[6] = new Option("臺南市","6");
  261. this[7] = new Option("臺北縣","7");
  262. this[8] = new Option("宜蘭縣","8");
  263. this[9] = new Option("桃園縣","9");
  264. this[10] = new Option("新竹縣","10");
  265. this[11] = new Option("苗栗縣","11");
  266. this[12] = new Option("臺中縣","12");
  267. this[13] = new Option("彰化縣","13");
  268. this[14] = new Option("南投縣","14");
  269. this[15] = new Option("雲林縣","15");
  270. this[16] = new Option("嘉義縣","16");
  271. this[17] = new Option("臺南縣","17");
  272. this[18] = new Option("高雄縣","18");
  273. this[19] = new Option("屏東縣","19");
  274. this[20] = new Option("臺東縣","20");
  275. this[21] = new Option("花蓮縣","21");
  276. this[22] = new Option("澎湖縣","22");
  277. this[23] = new Option("金門縣","23");
  278. this[24] = new Option("連江縣","24");
  279. return this;
  280. }
  281. //创建provincelist实例
  282. var provinceOb=new provinceList();
  283. //定义province、city变量,用于select元素
  284. var province;
  285. //初始化地区下拉菜单
  286. function initialize(privinceId){
  287. //获取select元素
  288. province=document.getElementById("Area");
  289. //循环添加省份到province
  290. for(var i=0;i<provinceOb.length;i++){
  291. try{
  292. province.add(provinceOb[i]);
  293. }catch(e){
  294. province.add(provinceOb[i],null);
  295. }
  296. }
  297. //初始化privinceId
  298. if(privinceId==undefined){
  299. privinceId=0;
  300. }
  301. //设置province默认选项
  302. province.options[privinceId].selected=true;
  303. }
  304. /*
  305. * 获取url参数
  306. * @param {string} 需要获取参数的名字
  307. * */
  308. getUrlParam = function (name) {
  309. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  310. var r = window.location.search.substr(1).match(reg); //匹配目标参数
  311. if (r != null) return unescape(r[2]);
  312. return null; //返回参数值
  313. }
  314. /*
  315. * 将字符串拼接成数组,并且修改成指定格式。用于ajax上传。
  316. * @param {array}
  317. * */
  318. urlEncode = function (arr) {
  319. var i; //循环初始化
  320. for (i = 0; i < arr.length; i++) {//循环对数组各元素进行加密
  321. arr[i] = encodeURIComponent(encodeURIComponent(arr[i]));
  322. }
  323. return arr.join();//转换为字符串返回
  324. }
  325. /*
  326. * 页面跳转指定的404页面
  327. * */
  328. go404Html = function () {
  329. window.location.href = "../404.html";
  330. }