shop.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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: "/malai/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>ColorClassification: ' + attr[i]['color'] + '</span></div>' +
  62. (attr[i]['size'] ? '<div class="goodStatus f-fr"><span>Size:' + attr[i]["size"] + '</span></div>' : '') +
  63. '<div class="goodStatus f-fr"><span>Quantity: ' + 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("Please type in your name");
  96. return;
  97. }
  98. else if (upn == "") {
  99. // alert("请输入手机号码");
  100. alert("Please enter the phone number");
  101. return;
  102. }
  103. /*
  104. else if (uem == "") {
  105. // alert("请输入电子邮件");
  106. alert("Please enter an email");
  107. return;
  108. }
  109. */
  110. else if (pc == "") {
  111. // alert("请输入邮编");
  112. alert("Please enter a zip code");
  113. return;
  114. }
  115. else if (p == "") {
  116. // alert("请输入省份");
  117. alert("Please enter the province");
  118. return;
  119. }
  120. else if (a == "") {
  121. // alert("请输入直辖区");
  122. alert("Please enter the territorial jurisdiction");
  123. return;
  124. }
  125. else if (address == "") {
  126. // alert("请输入详细地址");
  127. alert("Please enter the full address");
  128. return;
  129. }
  130. else if (rem == "") {
  131. // alert("请输入留言");
  132. alert("Please enter a message");
  133. return;
  134. }
  135. $("#PageLoading").css({"display": "block"});
  136. $("#PageWarp").css({"display": "none"});
  137. var parameter = urlEncode(["InsertOrder", lid, ordernum, pid, pcid, attr, ppr, pn, urn, upn, uem, pc, p, c, a, address, rem, fr, am, pay, state]);
  138. $.ajax({
  139. type: "post",
  140. url: "/malai/php/index.php",
  141. data: {mode: parameter},
  142. dataType: "json",
  143. success: function (data) {
  144. /*
  145. if (data != -1 && data[0].OrderId) {
  146. $("#PageLoading").css({"display": "none"});
  147. $("#PageWarp").css({"display": "block"});
  148. alert("Purchase Success!");
  149. window.location.href = "goods.html?pid=" + getUrlParam("pid");
  150. // insertUserIpAddress(ordernum, function () {
  151. //
  152. // });
  153. }
  154. */
  155. if (data != -1 && data[0].OrderId) {
  156. console.log(data);
  157. $("#PageLoading").css({"display": "none"});
  158. $("#PageWarp").css({"display": "none"});
  159. $("#PageWarp").remove();
  160. $("#ts_OrderNum").html(data[0].OrderNum);
  161. $("#ts_UserReallyName").html(data[0].UserReallyName);
  162. $("#ts_ProductName").html(ProductInfo.ProductName);
  163. $("#ts_Payment").html(data[0].Payment);
  164. $("#ts_UserPhoneNum").html(data[0].UserPhoneNum);
  165. $("#ts_Address").html(data[0].Address);
  166. $("#TS").css({"display": "block"});
  167. $("#ts_back")[0].onclick = function () {
  168. window.location.href = "goods.html?pid=" + getUrlParam("pid");
  169. }
  170. $("#ts_lx")[0].onclick = function () {
  171. window.location.href = " https://www.facebook.com/pg/MinimatterTimes";
  172. }
  173. /* insertUserIpAddress(ordernum, function () {
  174. });*/
  175. }
  176. else {
  177. alert("Sorry, your purchase failed, please contact customer service!!!");
  178. }
  179. }
  180. });
  181. //facebook推广购物事件,请不要删除!!!!!
  182. fbq('track', 'Purchase', {
  183. value: ProductInfo.ProductPrice,
  184. currency: 'MYR',
  185. content_ids: ProductInfo.ProductRemarks,
  186. content_type: 'product_group'
  187. });
  188. }
  189. /*
  190. * 获取下单用户ip地址插入数据库
  191. * */
  192. insertUserIpAddress = function (num, cb) {
  193. var IPAddress = ipaddress.traits.ip_address,
  194. country = ipaddress.country.names["zh-CN"],
  195. city = ipaddress.city.names["zh-CN"],
  196. parameter = urlEncode(["InsertOrderIPAddress", num, IPAddress, country, city]);
  197. $.ajax({
  198. type: "post",
  199. url: "/malai/php/index.php",
  200. data: {mode: parameter},
  201. dataType: "json",
  202. success: function (data) {
  203. if (data == 1) {
  204. cb && cb();
  205. }
  206. }
  207. });
  208. }
  209. /*
  210. * 根据当前时间,生成订单号
  211. * */
  212. orderNum = function () {
  213. var vNow = new Date(), sNow = "";
  214. sNow += String(vNow.getFullYear());
  215. sNow += String(vNow.getMonth() + 1);
  216. sNow += String(vNow.getDate());
  217. sNow += String(vNow.getHours());
  218. sNow += String(vNow.getMinutes());
  219. sNow += String(vNow.getSeconds());
  220. sNow += String(vNow.getMilliseconds());
  221. return sNow;//返回订单号
  222. }
  223. /*
  224. * 输入泰国指定的邮政编码,生成指定的泰国地址。
  225. * */
  226. // selectCity = function () {
  227. // $('#tha_postal').change(function () {
  228. // var id = $(this).val();
  229. // ajaxLocked = true;
  230. // $.ajax({
  231. // url: '/malai/php/Forward.php',
  232. // type: 'post',
  233. // data: {'postName': id},
  234. // dataType: 'json',
  235. // success: function (ret) {
  236. // if (ret) {
  237. // if (ret.length != 0) {
  238. // var option = "";
  239. // for (var i in ret) {
  240. // option += '<option value="' + ret[i].name + '" data-city="' + ret[i].parent_name + '">' + ret[i].name + '</option>';
  241. // $('.regions').html(ret[0].parent_name);
  242. // $('input[name="city"]').val(ret[0].parent_name);
  243. // }
  244. // $(".tha_district").html('').append(option);
  245. // ajaxLocked = false;
  246. // } else {
  247. // alert('郵政編碼不存在!');
  248. // $('#tha_postal').val('');
  249. // $('.tha_district').html('');
  250. // $('.regions').html('');
  251. // $('input[name="city"]').val('');
  252. // ajaxLocked = true;
  253. // }
  254. // } else {
  255. // alert('郵政編碼不存在!');
  256. // $('#tha_postal').val('');
  257. // $('.tha_district').html('');
  258. // $('.regions').html('');
  259. // $('input[name="city"]').val('');
  260. // ajaxLocked = true;
  261. // }
  262. // },
  263. // error: function (xhr, ajaxOptions, thrownError) {
  264. // }
  265. // });
  266. // });
  267. // }
  268. /*
  269. * 選擇马来西亚的州
  270. */
  271. //province(州類类)
  272. function provinceList()
  273. {
  274. this.length=24;
  275. this[0] = new Option("Pilih negeri anda...","0");
  276. this[1] = new Option("Johor","1");
  277. this[2] = new Option("Kedah","2");
  278. this[3] = new Option("Kelantan","3");
  279. this[4] = new Option("Melaka","4");
  280. this[5] = new Option("Negeri Sembilan","5");
  281. this[6] = new Option("Pahang","6");
  282. this[7] = new Option("Perak","7");
  283. this[8] = new Option("Perlis","8");
  284. this[9] = new Option("Pulau Pinang","9");
  285. this[10] = new Option("Selangor","10");
  286. this[11] = new Option("Terengganu","11");
  287. this[12] = new Option("Kuala Lumpur","12");
  288. this[13] = new Option("Putra Jaya","13");
  289. this[14] = new Option("Sarawak","14");
  290. this[15] = new Option("Sabah","15");
  291. this[16] = new Option("Labuan","16");
  292. return this;
  293. }
  294. //创建provincelist实例
  295. var provinceOb=new provinceList();
  296. //定义province、city变量,用于select元素
  297. var province;
  298. //初始化地区下拉菜单
  299. function initialize(privinceId){
  300. //获取select元素
  301. province=document.getElementById("Area");
  302. //循环添加省份到province
  303. for(var i=0;i<provinceOb.length;i++){
  304. try{
  305. province.add(provinceOb[i]);
  306. }catch(e){
  307. province.add(provinceOb[i],null);
  308. }
  309. }
  310. //初始化privinceId
  311. if(privinceId==undefined){
  312. privinceId=0;
  313. }
  314. //设置province默认选项
  315. province.options[privinceId].selected=true;
  316. }
  317. /*
  318. * 获取url参数
  319. * @param {string} 需要获取参数的名字
  320. * */
  321. getUrlParam = function (name) {
  322. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  323. var r = window.location.search.substr(1).match(reg); //匹配目标参数
  324. if (r != null) return unescape(r[2]);
  325. return null; //返回参数值
  326. }
  327. /*
  328. * 将字符串拼接成数组,并且修改成指定格式。用于ajax上传。
  329. * @param {array}
  330. * */
  331. urlEncode = function (arr) {
  332. var i; //循环初始化
  333. for (i = 0; i < arr.length; i++) {//循环对数组各元素进行加密
  334. arr[i] = encodeURIComponent(encodeURIComponent(arr[i]));
  335. }
  336. return arr.join();//转换为字符串返回
  337. }
  338. /*
  339. * 页面跳转指定的404页面
  340. * */
  341. go404Html = function () {
  342. window.location.href = "../404.html";
  343. }