shop.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. selectCity();
  20. }
  21. }
  22. getProductInfo = function (pid) {
  23. var parameter = urlEncode(["SelectProduct", pid]);
  24. $.ajax({
  25. type: "post",
  26. url: "/new/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: "/new/php/index.php",
  143. data: {mode: parameter},
  144. dataType: "json",
  145. success: function (data) {
  146. if (data != -1 && data[0].OrderId) {
  147. /*
  148. console.log(data);
  149. $("#PageLoading").css({"display": "none"});
  150. $("#PageWarp").css({"display": "none"});
  151. $("#PageWarp").remove();
  152. $("#ts_OrderNum").html(data[0].OrderNum);
  153. $("#ts_UserReallyName").html(data[0].UserReallyName);
  154. $("#ts_ProductName").html(ProductInfo.ProductName);
  155. $("#ts_Payment").html(data[0].Payment);
  156. $("#ts_UserPhoneNum").html(data[0].UserPhoneNum);
  157. $("#ts_Address").html(data[0].Address);
  158. $("#TS").css({"display": "block"});
  159. $("#ts_back")[0].onclick = function () {
  160. window.location.href = "goods.html?pid=" + getUrlParam("pid");
  161. }
  162. $("#ts_lx")[0].onclick = function () {
  163. window.location.href = " https://www.facebook.com/pg/MinimatterTimes";
  164. }
  165. */
  166. insertUserIpAddress(ordernum, function () {
  167. console.log(data);
  168. $("#PageLoading").css({"display": "none"});
  169. $("#PageWarp").css({"display": "none"});
  170. $("#PageWarp").remove();
  171. $("#ts_OrderNum").html(data[0].OrderNum);
  172. $("#ts_UserReallyName").html(data[0].UserReallyName);
  173. $("#ts_ProductName").html(ProductInfo.ProductName);
  174. $("#ts_Payment").html(data[0].Payment);
  175. $("#ts_UserPhoneNum").html(data[0].UserPhoneNum);
  176. $("#ts_Address").html(data[0].Address);
  177. $("#TS").css({"display": "block"});
  178. $("#ts_back")[0].onclick = function () {
  179. window.location.href = "goods.html?pid=" + getUrlParam("pid");
  180. }
  181. $("#ts_lx")[0].onclick = function () {
  182. window.location.href = " https://www.facebook.com/pg/MinimatterTimes";
  183. }
  184. });
  185. }
  186. else {
  187. alert("ซื้อล้มเหลวกรุณาติดต่อฝ่ายบริการลูกค้า ต้องขอโทษด้วยครับไม่สะดวก!!!");
  188. }
  189. }
  190. });
  191. //facebook推广购物事件,请不要删除!!!!!
  192. fbq('track', 'Purchase', {
  193. value: ProductInfo.ProductPrice,
  194. currency: 'THB',
  195. content_ids: ProductInfo.ProductRemarks,
  196. content_type: 'product_group'
  197. });
  198. }
  199. /*
  200. * 获取下单用户ip地址插入数据库
  201. * */
  202. insertUserIpAddress = function (num, cb) {
  203. var IPAddress = returnCitySN.cip,
  204. country = returnCitySN.cname,
  205. city = '',
  206. parameter = urlEncode(["InsertOrderIPAddress", num, IPAddress, country, city]);
  207. $.ajax({
  208. type: "post",
  209. url: "/new/php/index.php",
  210. data: {mode: parameter},
  211. dataType: "json",
  212. success: function (data) {
  213. if (data == 1) {
  214. cb && cb();
  215. }
  216. }
  217. });
  218. }
  219. /*
  220. * 根据当前时间,生成订单号
  221. * */
  222. orderNum = function () {
  223. var vNow = new Date(), sNow = "";
  224. sNow += String(vNow.getFullYear());
  225. sNow += String(vNow.getMonth() + 1);
  226. sNow += String(vNow.getDate());
  227. sNow += String(vNow.getHours());
  228. sNow += String(vNow.getMinutes());
  229. sNow += String(vNow.getSeconds());
  230. sNow += String(vNow.getMilliseconds());
  231. return sNow;//返回订单号
  232. }
  233. /*
  234. * 输入泰国指定的邮政编码,生成指定的泰国地址。
  235. * */
  236. selectCity = function () {
  237. $('#tha_postal').change(function () {
  238. var id = $(this).val();
  239. ajaxLocked = true;
  240. $.ajax({
  241. url: '/new/php/Forward.php',
  242. type: 'post',
  243. data: {'postName': id},
  244. dataType: 'json',
  245. success: function (ret) {
  246. if (ret) {
  247. if (ret.length != 0) {
  248. var option = "";
  249. for (var i in ret) {
  250. option += '<option value="' + ret[i].name + '" data-city="' + ret[i].parent_name + '">' + ret[i].name + '</option>';
  251. $('.regions').html(ret[0].parent_name);
  252. $('input[name="city"]').val(ret[0].parent_name);
  253. }
  254. $(".tha_district").html('').append(option);
  255. ajaxLocked = false;
  256. } else {
  257. alert('รหัสไปรษณีย์ ผิด!');
  258. $('#tha_postal').val('');
  259. $('.tha_district').html('');
  260. $('.regions').html('');
  261. $('input[name="city"]').val('');
  262. ajaxLocked = true;
  263. }
  264. } else {
  265. alert('รหัสไปรษณีย์ ผิด!');
  266. $('#tha_postal').val('');
  267. $('.tha_district').html('');
  268. $('.regions').html('');
  269. $('input[name="city"]').val('');
  270. ajaxLocked = true;
  271. }
  272. },
  273. error: function (xhr, ajaxOptions, thrownError) {
  274. }
  275. });
  276. });
  277. }
  278. /*
  279. * 获取url参数
  280. * @param {string} 需要获取参数的名字
  281. * */
  282. getUrlParam = function (name) {
  283. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  284. var r = window.location.search.substr(1).match(reg); //匹配目标参数
  285. if (r != null) return unescape(r[2]);
  286. return null; //返回参数值
  287. }
  288. /*
  289. * 将字符串拼接成数组,并且修改成指定格式。用于ajax上传。
  290. * @param {array}
  291. * */
  292. urlEncode = function (arr) {
  293. var i; //循环初始化
  294. for (i = 0; i < arr.length; i++) {//循环对数组各元素进行加密
  295. arr[i] = encodeURIComponent(encodeURIComponent(arr[i]));
  296. }
  297. return arr.join();//转换为字符串返回
  298. }
  299. /*
  300. * 页面跳转指定的404页面
  301. * */
  302. go404Html = function () {
  303. window.location.href = "../404.html";
  304. }