index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import * as compnt from "../../public/js/compnt.js";
  2. import * as unicom from "../../public/js/unicom.js";
  3. import * as baidu from "../../sina/wb/js/weibo.js";
  4. import "../scss/main.scss";
  5. compnt.unlockClick();
  6. $(function () {
  7. var isPageHide = false;
  8. window.addEventListener('pageshow', function () {
  9. if (isPageHide) {
  10. window.location.reload();
  11. }
  12. });
  13. window.addEventListener('pagehide', function () {
  14. isPageHide = true;
  15. });
  16. })
  17. init();
  18. function init() {
  19. try {
  20. var message = localStorage.getItem('message');
  21. if (message != null && message != '') {
  22. if (message.indexOf("您已经订购") >= 0) {
  23. showAlertDialog('orderFailure', message);
  24. } else {
  25. compnt.showAlert(message);
  26. }
  27. localStorage.removeItem('message');
  28. }
  29. } catch (e) {
  30. // alert('na ni ?')
  31. }
  32. compnt.showLoading();
  33. unicom.init().then(function(response) {
  34. compnt.hideLoading();
  35. unicom.product = response.product[0];
  36. unicom.fesname = response.festival;
  37. unicom.loadUserInfo();
  38. if (location.href.indexOf("index.html") >= 0) {
  39. if (unicom.user.userid) {
  40. openPage("flow");
  41. } else {
  42. showOrderPage();
  43. }
  44. } else {
  45. openPage();
  46. }
  47. if (location.href.indexOf('/cancel') >= 0) {
  48. unicom.burypointLogId('110002');// 退订页面pv, pv去重得到uv
  49. } else if (location.href.indexOf('/draw') >= 0) {
  50. unicom.burypointLogId('110003');// 领取页面pv, pv去重得到uv
  51. } else if (location.href.indexOf('/flow') >= 0) {
  52. unicom.burypointLogId('110004');// 流量查询pv, pv去重得到uv
  53. } else if (location.href.indexOf('/active') >= 0) {
  54. unicom.burypointLogId('110005');// 激活页面pv, pv去重得到uv
  55. } else if (location.href.indexOf('/refund') >= 0) {
  56. unicom.burypointLogId('110006');// 退款
  57. } else {
  58. // location.href.indexOf('/index') >= 0
  59. // location.href.indexOf('/order') >= 0
  60. unicom.burypointLogId('110001');// 订购页面pv, pv去重得到uv
  61. }
  62. })
  63. }
  64. function openPage(pageName) {
  65. if (pageName == null) {
  66. if (location.href.indexOf("index.html") >= 0) pageName = "order";
  67. if (location.href.indexOf("cancel.html") >= 0) pageName = "cancel";
  68. if (location.href.indexOf("active.html") >= 0) pageName = "active";
  69. if (location.href.indexOf("flow.html") >= 0) pageName = "flow";
  70. }
  71. switch (pageName) {
  72. case "active":
  73. showActivePage();
  74. break;
  75. case "cancel":
  76. showCancelPage();
  77. break;
  78. case "draw":
  79. alert('参数错误')
  80. break;
  81. case "flow":
  82. showFlowPage();
  83. break;
  84. default:
  85. showOrderPage();
  86. break;
  87. }
  88. }
  89. $('#btn-cancel').unbind('click').on('click', function () {
  90. showCancelPage();
  91. })
  92. $('#btn-active').unbind('click').on('click', function () {
  93. // if (!checkEquip('active')) return;
  94. // showActivePage();
  95. })
  96. function showOrderPage() {
  97. if ($('.section-order').css('display') == 'none') {
  98. bindOrderBtn();
  99. } else {
  100. $('.btn-order').unbind('click').on('click', function (event) {
  101. bindOrderBtn();
  102. })
  103. }
  104. function bindOrderBtn() {
  105. if (!compnt.lockClick()) {
  106. return;
  107. }
  108. unicom.showCapPage(null, null, false, true).then(function (data) {
  109. compnt.unlockClick();
  110. order(data.userid, data.token, data.usermob);
  111. }).fail(function(response) {
  112. compnt.unlockClick();
  113. })
  114. }
  115. }
  116. function showFlowPage() {
  117. if (location.href.indexOf("flow.html") < 0) {
  118. window.location.href = "flow.html?fes="
  119. + unicom.query.fes + "&sou=" + unicom.query.sou
  120. + "&userid=" + unicom.user.userid + "&usermob=" + unicom.user.usermob
  121. + "&usertime=" + (unicom.user.usertime||(new Date().getTime()));
  122. return;
  123. }
  124. var $container = $('.section-flow');
  125. var usermob = unicom.user.usermob;
  126. $container.find('.phone').html(usermob.substring(0,3) + 'xxxx' + usermob.substring(7,11));
  127. $container.find('.btn').unbind('click').on('click', function() {
  128. //if (!compnt.lockClick()) {
  129. // return;
  130. //}
  131. queryFlow().then(function(message) {
  132. compnt.unlockClick()
  133. if (message != null && message != "") {
  134. compnt.showAlert(message);
  135. }
  136. }).fail(function() {
  137. compnt.unlockClick();
  138. });
  139. })
  140. queryFlow();
  141. }
  142. function showActivePage() {
  143. if (location.href.indexOf("active.html") < 0) {
  144. window.location.href = "active.html?fes=" + unicom.query.fes + "&sou="
  145. + unicom.query.sou + "&F=" + unicom.query.F + "&page=active";
  146. return;
  147. }
  148. var $container = $('.section-login').find('.form-group');
  149. $container.find('.sendcode').unbind('click').on('click', function () {
  150. if (!checkEquip()) return;
  151. unicom.sendSms($container, 'activelogin');
  152. })
  153. $container.find('.login').unbind('click').on('click', function (event) {
  154. if (!checkEquip()) return;
  155. if (!compnt.lockClick()) {
  156. return;
  157. }
  158. unicom.checkCode($container).then(function() {
  159. compnt.unlockClick()
  160. active()
  161. }).fail(function() {
  162. compnt.unlockClick();
  163. });
  164. })
  165. window.scrollTo(0, 0)
  166. }
  167. function showCancelPage() {
  168. if (location.href.indexOf("cancel.html") < 0) {
  169. window.location.href = "cancel.html?fes=" + unicom.query.fes + "&sou=" + unicom.query.sou;
  170. return;
  171. }
  172. var $container = $('.section-login').find('.form-group');
  173. $container.find('.sendcode').unbind('click').on('click', function () {
  174. unicom.sendSms($container, 'cancellogin');
  175. })
  176. $container.find('.login').unbind('click').on('click', function () {
  177. compnt.showLoading();
  178. unicom.checkCode($container).done(function() {
  179. unicom.cancel().done(function(response) {
  180. compnt.hideLoading();
  181. switch (response.resultCode) {
  182. case "0000":
  183. showAlertDialog('cancelSuccess','您已成功退订该商品,退订次月生效', function() {
  184. showFlowPage();
  185. });
  186. //compnt.showAlert('退订成功').then(function(message) {
  187. // showFlowPage();
  188. //});
  189. break;
  190. case "7605": //应该是已经退订了吧,因此不需要在重复退订啦
  191. //compnt.showAlert(response.errorInfo).then(function(message) {
  192. // showFlowPage();
  193. //});
  194. showAlertDialog('cancelFailure', response.errorInfo, function() {
  195. showFlowPage();
  196. });
  197. break;
  198. default:
  199. var prefix = '';
  200. if (response.errorInfo.indexOf('退订失败') < 0) {
  201. prefix = '退订失败,';
  202. }
  203. //compnt.showAlert(prefix + response.errorInfo);
  204. showAlertDialog('cancelFailure', prefix + response.errorInfo);
  205. break;
  206. }
  207. }).fail(function(data) {
  208. compnt.hideLoading();
  209. })
  210. }).fail(function(data) {
  211. compnt.hideLoading();
  212. })
  213. })
  214. window.scrollTo(0, 0)
  215. }
  216. function checkEquip() {
  217. /*if (!baidu.app.baidu) {
  218. compnt.showAlert('请到百度产品客户端中激活免流~')
  219. return false;
  220. }
  221. if (!baidu.checkWeiboVersion()) {
  222. compnt.showAlert('请到升级到最新版百度产品客户端后激活免流~')
  223. return false;
  224. }*/
  225. return true;
  226. }
  227. /**
  228. * 订购
  229. */
  230. function order(userid, token, usermob) {
  231. compnt.showLoading('处理中,请稍后...');
  232. unicom.order(userid, token, usermob).then(function(response) {
  233. compnt.hideLoading();
  234. switch (response.resultCode) {
  235. case "0000": //订购成功!跳转到页面
  236. unicom.saveUserInfo();
  237. showFlowPage();
  238. //compnt.showAlert('订购成功!').then(function() {
  239. // showFlowPage();
  240. //})
  241. //showAlertDialog('orderSuccess', '订购成功!', function() {
  242. // showFlowPage();
  243. //});
  244. return;
  245. case "0013":
  246. showAlertDialog('orderFailure','该号码归属省分2/3G暂未开通此产品!');
  247. break;
  248. case "6611":
  249. showAlertDialog('orderFailure','错误代码(6611),' + '订购失败,请重试!');
  250. break;
  251. case "6615":
  252. showAlertDialog('orderFailure', '订购失败,请确定用户套餐!');
  253. break;
  254. case "6622": // 用户已订购过此产品!跳转到流量查页面
  255. localStorage.setItem('message', '您已经订购了本产品~');
  256. unicom.saveUserInfo();
  257. showFlowPage();
  258. break;
  259. default:
  260. showAlertDialog('orderFailure', response.errorInfo + ',订购失败,请确定用户套餐!');
  261. break;
  262. }
  263. }).fail(function (error) {
  264. console.log(error);
  265. });
  266. }
  267. function showAlertDialog(action, message, callback) {
  268. var className = '';
  269. switch (action) {
  270. case 'orderSuccess':
  271. className = 'dialog-message-order-success';
  272. break;
  273. case 'orderFailure':
  274. className = 'dialog-message-order-failure';
  275. break;
  276. case 'activeSuccess':
  277. className = 'dialog-message-active-success';
  278. break;
  279. case 'activeFailure':
  280. className = 'dialog-message-active-failure';
  281. break;
  282. case 'cancelSuccess':
  283. className = 'dialog-message-cancel-success';
  284. break;
  285. case 'cancelFailure':
  286. className = 'dialog-message-cancel-failure';
  287. break;
  288. default:
  289. break;
  290. }
  291. var $overlay = $('<div class="overlay"></div>');
  292. var $dialog = $(
  293. '<div class="dialog dialog-message ' + className + '">' +
  294. ' <div class="title"></div>' +
  295. ' <div class="content">' + message + '</div>' +
  296. ' <div class="actions">' +
  297. ' <div class="btn">确定</div>' +
  298. ' </div>' +
  299. '</div>');
  300. $overlay.appendTo($(document.body))
  301. $dialog.appendTo($(document.body))
  302. $dialog.find('.btn').unbind('click').on('click', function () {
  303. $dialog.close()
  304. })
  305. $dialog.close = function () {
  306. $overlay.remove();
  307. $dialog.remove();
  308. $dialog = null;
  309. callback && callback()
  310. }
  311. return $dialog;
  312. }
  313. /**
  314. * 查询用户剩余流量
  315. */
  316. function queryFlow() {
  317. var $container = $('.section-flow');
  318. compnt.showLoading('处理中,请稍后...');
  319. //return new Promise(function(resolve, reject) {
  320. var def = $.Deferred();
  321. unicom.queryFlow().then(function(response) {
  322. compnt.hideLoading();
  323. switch (response.resultCode) {
  324. case "0000":
  325. //compnt.showAlert(JSON.stringify(response.data));
  326. //console.log(response.data)
  327. //console.log(JSON.parse(response.data).surplusFlow)
  328. var surplus = compnt.returnFloat(JSON.parse(response.data).surplusFlow/1024/1024);
  329. $container.find('.surplus').html(surplus + 'GB');
  330. var usedFlowKb = parseInt(JSON.parse(response.data).usedFlow);
  331. if (usedFlowKb > 0 && usedFlowKb < 1024) {
  332. def.resolve("剩余流量更新成功,为您免流了" + usedFlowKb + "KB~");
  333. } else if (usedFlowKb > 1024) {
  334. var useedFlow = compnt.returnFloat(usedFlowKb/1024);
  335. def.resolve("剩余流量更新成功,为您免流了" + useedFlow + "MB~");
  336. } else {
  337. def.resolve("剩余流量更新成功~");
  338. }
  339. break;
  340. case "9201": //{"resultCode":"9201","errorInfo":"查询失败,请检查是否有订购关系!"}
  341. def.resolve("您未订购百度定向流量包~")
  342. $container.find('.phone').html('用户未订购此产品!');
  343. break;
  344. case "9203": //{"resultCode":"9203","errorInfo":"流量查询失败,没有获取到用户所使用流量信息!"}
  345. //compnt.showAlert("没有获取到用户所使用流量信息!");
  346. def.resolve("剩余流量更新成功~")
  347. $container.find('.suplus').html('本月剩余<br>8.00GB');
  348. break;
  349. default:
  350. def.resolve("剩余流量更新成功~")
  351. break;
  352. }
  353. })
  354. return def;
  355. //})
  356. }
  357. function active() {
  358. baidu.active().then(function() {
  359. //checkActiveResult();
  360. compnt.showAlert('激活成功').then(function() {
  361. unicom.saveUserInfo()
  362. showFlowPage();
  363. });
  364. })
  365. }
  366. $('.section-agr').find('.footer').on('click',function() {
  367. if ($($('.section-agr .scope').find('li')[2]).hasClass('hidden')) {
  368. $('.section-agr .scope').find('li').each(function(i, ele) {
  369. $(ele).removeClass('hidden');
  370. })
  371. // $('.section-agr').find('.footer').html('隐藏<span class="arrow">&gt;');
  372. } else {
  373. $('.section-agr .scope').find('li').each(function(i, ele) {
  374. if (i >= 0) {
  375. $(ele).addClass('hidden');
  376. }
  377. })
  378. // $('.section-agr').find('.footer').html('更多适用范围<span class="arrow">&gt;');
  379. }
  380. });