vip_renew.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. var renew = {
  2. oldRules: Calculation.PriceRule.old,
  3. newRules: Calculation.PriceRule.new,
  4. requestRules: {},
  5. price: '', // 单位分
  6. timeSelect: [1, 2], // 续费周期(默认选中1个月)
  7. timeCheckOk: true,
  8. buyInfo: { // 购买的信息
  9. startTime: 0,
  10. endTime: 0,
  11. area: {},
  12. industry: [],
  13. buyset: {
  14. areacount: 0,
  15. newcitys: [],
  16. buyerclasscount: 0,
  17. upgrade: 0
  18. },
  19. isTrial: false,
  20. renewList: []
  21. },
  22. init: function () {
  23. this.initEvents()
  24. this.recoverState()
  25. this.getPrice()
  26. this.getBuySetInfo()
  27. this.setTimePickerSelected(this.timeSelect)
  28. },
  29. setAreaAndIndustry: function () {
  30. var buySet = this.buyInfo.buyset
  31. var result = {
  32. area: '',
  33. industry: ''
  34. }
  35. // 区域
  36. if (buySet.areacount === -1) {
  37. result.area = '全国'
  38. } else {
  39. var strArr = []
  40. if (buySet.areacount) {
  41. strArr.push(buySet.areacount + '个省')
  42. }
  43. if (buySet.newcitys && buySet.newcitys.length !== 0) {
  44. var count = 0
  45. buySet.newcitys.forEach(function (item) {
  46. count += item
  47. })
  48. strArr.push(count + '个市(分布在' + buySet.newcitys.length + '个省内')
  49. }
  50. result.area = strArr.join('、')
  51. }
  52. // 行业
  53. if (buySet.buyerclasscount === -1) {
  54. result.industry = '全部行业'
  55. } else {
  56. result.industry = buySet.buyerclasscount + '个行业'
  57. }
  58. $('.area .item-r').text(result.area)
  59. $('.industry .item-r').text(result.industry)
  60. return result
  61. },
  62. initEvents: function () {
  63. this.initClickEvents()
  64. this.initPickerEvents()
  65. },
  66. getBuySetInfo: function () {
  67. var _this = this
  68. $.ajax({
  69. url: '/subscribepay/vipsubscribe/getSubBuyMsg',
  70. type: 'POST',
  71. success: function (r) {
  72. if (r.success && r.data) {
  73. Object.assign(_this.buyInfo, r.data)
  74. _this.setAreaAndIndustry()
  75. _this.checkShowDueTip()
  76. // 初始化默认续费周期
  77. _this.calcDuration(true)
  78. // 初始化价格计算类
  79. Calculation.Init(
  80. _this.buyInfo.isTrial,
  81. new Buyset(
  82. _this.buyInfo.buyset.areacount,
  83. _this.buyInfo.buyset.newcitys,
  84. _this.buyInfo.buyset.buyerclasscount,
  85. _this.buyInfo.buyset.upgrade || 0
  86. )
  87. )
  88. // 初始化默认金额
  89. _this.setPrice(_this.calcPrice())
  90. }
  91. }
  92. })
  93. },
  94. getPrice: function () {
  95. var _this = this
  96. $.ajax({
  97. url: '/subscribepay/vipsubscribe/getPrice',
  98. type: 'POST',
  99. success: function (r) {
  100. if (r) {
  101. _this.requestRules = r
  102. }
  103. }
  104. })
  105. },
  106. // 计算价格
  107. calcPrice: function (timeSelect) {
  108. return Calculation.GetTotal(undefined, timeSelect ? timeSelect : this.timeSelect)
  109. },
  110. // 设置价格
  111. setPrice: function (price) {
  112. this.price = price
  113. coupon.updatePrice(this.price / 100)
  114. },
  115. // 根据选中结果得到续费了多长时间的字符串
  116. getDuration: function (timeSelect) {
  117. var durationStr = ''
  118. if (timeSelect[1] === 1) {
  119. // 年
  120. durationStr = timeSelect[0] + '年'
  121. } else if (timeSelect[1] === 2) {
  122. // 月
  123. durationStr = timeSelect[0] + '个月'
  124. }
  125. return durationStr
  126. },
  127. // 计算续费后的日期区间
  128. calcDuration: function (needSet) {
  129. // 根据
  130. var endTime = this.buyInfo.endTime
  131. if (!endTime) {
  132. $('.select_cycle .duration').hide()
  133. return
  134. }
  135. var durationStamp = getVipEndDate(this.timeSelect[1], this.timeSelect[0], endTime) - endTime // 续费时间段时间戳
  136. var durationStr = this.getDuration(this.timeSelect) // 续费时间段字符串
  137. var buyEndTimeString = new Date(endTime * 1000).pattern('yyyy年MM月dd日') // svip到期时间
  138. var renewEndTimeString = new Date((endTime + durationStamp) * 1000).pattern('yyyy年MM月dd日') //续费后到期时间
  139. if (needSet) {
  140. $('.select_cycle .month').text(durationStr)
  141. $('.select_cycle .duration').text(buyEndTimeString + ' - ' + renewEndTimeString)
  142. } else {
  143. return {
  144. duration: durationStr, // 续费时间段字符串
  145. buyEndTimeString: buyEndTimeString, // svip到期时间
  146. renewEndTimeString: renewEndTimeString, //续费后到期时间
  147. }
  148. }
  149. },
  150. // 月份转年份(超过1年才会转)
  151. monthToYear: function (num) {
  152. var year = Math.floor(num / 12)
  153. var month = num % 12
  154. var str = ''
  155. if (year) {
  156. str += (year + '年')
  157. }
  158. if (month) {
  159. str += (month + '个月')
  160. }
  161. return {
  162. str: str,
  163. year: year,
  164. month: month
  165. }
  166. },
  167. checkOver3Years: function (timeSelect) {
  168. // 根据
  169. var endTime = this.buyInfo.endTime
  170. if (!endTime) {
  171. return
  172. }
  173. var maxEndMonth = 36 // 最大续费36个月
  174. var durationStamp = getVipEndDate(timeSelect[1], timeSelect[0], endTime) - endTime // 续费时间段时间戳
  175. // 最长订阅周期不可超过3年 ------------
  176. var nowStamp = Math.floor(Date.now() / 1000)
  177. // 已购买的套餐剩余月数
  178. var lastMonthStamp = endTime - nowStamp
  179. var lastMonth = Math.round(lastMonthStamp / (60 * 60 * 24 * 30))
  180. // 续费后套餐剩余月数(需要加上原剩余月数lastMonth)
  181. var renewMonthTotalStamp = lastMonthStamp + durationStamp
  182. var renewMonthTotal = Math.round(renewMonthTotalStamp / (60 * 60 * 24 * 30))
  183. if (renewMonthTotal > maxEndMonth) {
  184. var showTipText = '<div style="white-space: nowrap;">最长订阅周期不可超过3年</div>剩余周期:' + this.monthToYear(lastMonth).str
  185. weui.toast(showTipText, {
  186. duration: 3000,
  187. className: 'jy-toast',
  188. })
  189. // 确认按钮置灰
  190. $('#enter_period').prop('disabled', true)
  191. } else {
  192. $('#enter_period').prop('disabled', false)
  193. }
  194. },
  195. // 是否展示即将到期
  196. checkShowDueTip () {
  197. var endTime = this.buyInfo.endTime * 1000
  198. var timeSub = endTime - Date.now()
  199. if (timeSub > 0 && timeSub < 60 * 60 * 24 * 1000 * 3) {
  200. $('.vip-header').show()
  201. }
  202. },
  203. setTimePickerSelected: function (timeSelect) {
  204. if (timeSelect[1] == 1) {
  205. // 年
  206. var cButton = $('.year_number.j-button-select[data-id=' + timeSelect[0] + ']')
  207. if (cButton) {
  208. cButton.trigger('click')
  209. } else {
  210. $('input[value=year]').trigger('click')
  211. }
  212. } else if (timeSelect[1] == 2) {
  213. // 月
  214. $('input[value=month]').trigger('click')
  215. $('#number_box_month .month_number').text(timeSelect[0] - 1)
  216. $('#number_box_month button.add').trigger('click')
  217. }
  218. },
  219. getTimePickerSelected: function () {
  220. var timeSelectPicker = $('#time_cycle')
  221. var $checkInput = timeSelectPicker.find('input[name=time]:checked')
  222. if (!$checkInput) return
  223. // year/month(按年或者按月购买)
  224. var checkedType = $checkInput.val()
  225. // timeSelectedArr[0] 代表选择的内容
  226. // timeSelectedArr[1] 代表选择的类型 1年份 2月份
  227. var timeSelectedArr = []
  228. if (checkedType === 'month') {
  229. timeSelectedArr = [$('#number_box_month .month_number').text() - 0, 2]
  230. } else if (checkedType === 'year') {
  231. timeSelectedArr = [$('#number_box_year .year_number.active').attr('data-id') - 0, 1]
  232. }
  233. return timeSelectedArr
  234. },
  235. onTimePickerChange: function () {
  236. var timeSelect = this.getTimePickerSelected()
  237. var price = this.calcPrice(timeSelect)
  238. $('.computed_price').text(formatMoney(price / 100))
  239. this.checkOver3Years(timeSelect)
  240. },
  241. // 恢复数据
  242. recoverState: function () {
  243. var vipSubSelectTime = sessionStorage.getItem('vipSubSelectTime')
  244. if (vipSubSelectTime) {
  245. this.timeSelect = JSON.parse(vipSubSelectTime)
  246. }
  247. this.flushSubmit()
  248. },
  249. // 保存数据
  250. saveState: function () {
  251. sessionStorage.setItem('vipSubSelectTime', JSON.stringify(this.timeSelect))
  252. },
  253. // 是否可提交
  254. flushSubmit: function () {
  255. if (this.timeCheckOk && sessionStorage.getItem("vipSub_read") === "true") {
  256. $('.vip-footer.renew .confirm').prop('disabled', false)
  257. coupon.checkboxStatus = true
  258. coupon.$refs.couponRef.submitStatus = false
  259. } else {
  260. $('.vip-footer.renew .confirm').prop('disabled', true)
  261. coupon.$refs.couponRef.submitStatus = true
  262. }
  263. },
  264. initClickEvents: function () {
  265. var _this = this
  266. /*------ 关闭弹窗事件 点击取消或遮罩层 -----*/
  267. $('.weui-mask').click(hideDialog);
  268. $('.cancel').click(hideDialog);
  269. // 确认订阅周期
  270. $('#enter_period').on('click', function () {
  271. $('.weui-mask').trigger('click')
  272. _this.timeSelect = _this.getTimePickerSelected()
  273. _this.calcDuration(true)
  274. _this.setPrice(_this.calcPrice())
  275. _this.saveState()
  276. })
  277. $('.vip-footer.renew .confirm').on('click', function () {
  278. $('.vip-footer.renew .confirm').prop('disabled', true)
  279. //支付请求
  280. var data = {
  281. userLotteryId :coupon.$refs.couponRef.coupon.userLotteryId,
  282. lotteryId : coupon.$refs.couponRef.coupon.lotteryId,
  283. time : _this.getDuration(_this.timeSelect).trim(),
  284. orderType: 5,
  285. disWord :GetQueryString('disWord'),
  286. price: _this.price
  287. }
  288. $.ajax({
  289. // url: '/subscribepay/vipsubscribe/createOrder',
  290. url: '/subscribepay/vipsubscribe/renew',
  291. type: 'POST',
  292. data: data,
  293. success: function (r) {
  294. if (r.success) {
  295. clearSessionStorage()
  296. try {
  297. history.replaceState({}, '', '/front/vipsubscribe/toOrderDetailPage?orderCode=' + r.data.code);
  298. window.location.href = "/weixin/pay/checkout_subvip?t=1&orderCode=" + r.data.code;
  299. } catch (e) {
  300. showToast(e)
  301. }
  302. } else {
  303. showToast(r.errMsg || '请求失败')
  304. }
  305. },
  306. complete: function () {
  307. $('.vip-footer.renew .confirm').prop('disabled', false)
  308. }
  309. })
  310. })
  311. },
  312. initPickerEvents: function () {
  313. var _this = this
  314. /* ----- 订阅周期弹窗弹出事件 -------*/
  315. // 选择订阅周期
  316. $('.select_cycle a').click(function (e) {
  317. _this.onTimePickerChange()
  318. $('#time_cycle').show(200);
  319. });
  320. // 对订阅时间选择的input绑定点击事件
  321. $('#time_cycle input:radio[name="time"]').on('click', function (e) {
  322. // 解除确认按钮的锁定
  323. if ($(e.target).hasClass('monthly') !== $('#number_box_month').hasClass('active')) {
  324. $('#time_cycle .form-btn button').removeAttr('disabled');
  325. }
  326. if ($(e.target).hasClass('monthly')) {
  327. // 按月订阅
  328. $('#number_box_month').addClass('active');
  329. $('#number_box_year span').removeClass('active');
  330. } else {
  331. // 按年订阅
  332. $('#number_box_month').removeClass('active');
  333. }
  334. });
  335. /* --------控制月份number_box的事件 点击加减号触发的事件------- */
  336. $('#number_box_month').on('click', 'button', function (e) {
  337. // 点击加减号让input radio选中
  338. $('#monthly').prop('checked', true);
  339. $('#yearly').prop('checked', false);
  340. $('.number_box span').removeClass('active');
  341. $('#number_box_month').addClass('active');
  342. var $number = $('#number_box_month span.month_number');
  343. var $monthlyInput = $('#monthly');
  344. // 未整理的data数组,里面的值都是字符串
  345. var preData = e.delegateTarget.dataset;
  346. var currentNum = parseInt($number.text());
  347. var data = {};
  348. // 把字符串转换成数字
  349. for (var i in preData) {
  350. data[i] = preData[i] - 0
  351. }
  352. if (!$monthlyInput.prop('checked')) {
  353. return
  354. }
  355. // 判断是点击的是+ 还是-
  356. if ($(e.target).hasClass('add') || $(e.currentTarget).hasClass('add')) {
  357. if (currentNum == 11) { //12个月自动跳转1年
  358. $("#number_box_year .year_number:eq(0)").trigger("click");
  359. return
  360. }
  361. // 点的+
  362. currentNum = currentNum >= data.numboxMax ? data.numboxMax : (currentNum + data.numboxStep);
  363. } else {
  364. // 点的-
  365. currentNum = currentNum <= data.numboxMin ? data.numboxMin : (currentNum - data.numboxStep);
  366. }
  367. $number.text(currentNum);
  368. _this.onTimePickerChange()
  369. var firstButton = $('#number_box_month button:first');
  370. var lastButton = $('#number_box_month button:last');
  371. // 如果为操作后的结果为1,则锁定减号按钮
  372. if (currentNum === data.numboxMin) {
  373. firstButton.attr('disabled', true)
  374. } else {
  375. firstButton.removeAttr('disabled')
  376. }
  377. // 如果为操作后的结果为12,则锁定加号按钮
  378. if (currentNum === data.numboxMax) {
  379. lastButton.attr('disabled', true)
  380. } else {
  381. lastButton.removeAttr('disabled')
  382. }
  383. });
  384. /* -------- 控制年份number_box的事件 点击1年 2年 3年触发的事件------- */
  385. $('#number_box_year').on('click', 'span', function (e) {
  386. $('#number_box_month').removeClass('active');
  387. var id = e.target.dataset.id;
  388. $(this).addClass('active').siblings().removeClass('active');
  389. $('#yearly').prop('checked', true);
  390. $("#monthly").prop('checked', false);
  391. _this.onTimePickerChange()
  392. });
  393. /* -------- 选择按月订阅 radio触发的事件------- */
  394. $('#monthly').on('change', function (e) {
  395. var isChecked = $(this).is(':checked');
  396. var val = $('.month_number').text();
  397. if (!isChecked) {
  398. $('#number_box_month button').attr('disabled', true)
  399. }
  400. _this.onTimePickerChange()
  401. });
  402. /* -------- 选择按年订阅 radio触发的事件------- */
  403. $('#yearly').on('change', function (e) {
  404. var isChecked = $(this).is(':checked');
  405. // 按年订阅默认选择1年
  406. var val = 1;
  407. if (isChecked) {
  408. $('#number_box_year span:eq(0)').addClass('active').siblings().removeClass('active')
  409. }
  410. _this.onTimePickerChange()
  411. });
  412. // 计价标准
  413. $('.billing-list-btn').on('click', function () {
  414. pickerShow('.billing-list-container', true)
  415. })
  416. // 计算标准隐藏
  417. $('.billing-list-container .dialog_hd__close').on('click', function () {
  418. pickerShow('.billing-list-container', false)
  419. })
  420. $('.checkbox').on('click', function () {
  421. $(this).toggleClass('checked')
  422. var state = $(this).hasClass('checked')
  423. sessionStorage.setItem("vipSub_read", state)
  424. renew.flushSubmit()
  425. });
  426. }
  427. }
  428. $(function () {
  429. renew.init()
  430. })
  431. //获取url中"?"符后的字符串并正则匹配
  432. function GetQueryString(name) {
  433. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  434. var r = window.location.search.substr(1).match(reg);
  435. var context = "";
  436. if (r != null)
  437. context = r[2];
  438. reg = null;
  439. r = null;
  440. return context == null || context == "" || context == "undefined" ? "" : context;
  441. }
  442. // 隐藏dialog选择框
  443. function hideDialog() {
  444. $('#pay_way').hide(200);
  445. $('#time_cycle').hide(200);
  446. $('.discount-details').hide()
  447. $('.discount-button .icon-arrow').removeClass('up')
  448. }
  449. function clearSessionStorage() {
  450. sessionStorage.removeItem("vipSubSelectTime");
  451. sessionStorage.removeItem("vipSub_read");
  452. sessionStorage.removeItem("historypushDataCache");
  453. sessionStorage.removeItem("endTime");
  454. sessionStorage.To_introducePage = 2;
  455. //订阅首页
  456. sessionStorage.removeItem("sub_vip_state");
  457. sessionStorage.removeItem("vip_index_read");
  458. sessionStorage.removeItem("vip_change_time");
  459. sessionStorage.removeItem("pay_read_cache")
  460. sessionStorage.removeItem("liveActiveCode_renew");
  461. sessionStorage.removeItem("liveActiveCode_new");
  462. }
  463. function showToast(text) {
  464. weui.toast(text, {
  465. duration: 3000,
  466. className: 'jy-toast',
  467. })
  468. }
  469. function pickerShow(selector, f) {
  470. if (f) {
  471. // 添加进场动画,并显示
  472. $(selector).find('.weui-mask').removeClass('weui-animate-fade-out').addClass('weui-animate-fade-in')
  473. $(selector).find('.weui-picker').removeClass('weui-animate-slide-down').addClass('weui-animate-slide-up')
  474. $(selector).show(300);
  475. } else {
  476. // 添加离场动画,并隐藏
  477. $(selector).find('.weui-mask').removeClass('weui-animate-fade-in').addClass('weui-animate-fade-out')
  478. $(selector).find('.weui-picker').removeClass('weui-animate-slide-up').addClass('weui-animate-slide-down')
  479. $(selector).hide(300);
  480. }
  481. }
  482. /* vue 代码 start */
  483. var coupon = new Vue({
  484. el: '#coupon-vue',
  485. data: function () {
  486. return {
  487. type: 'vip',
  488. initPrice: 0, // 原价
  489. realPrice: 0, // 实付价
  490. checkboxStatus: false, // checkbox状态
  491. submitStatus: true, // 提交按钮状态
  492. links: [
  493. {
  494. text: '《剑鱼标讯线上购买与服务条款》',
  495. url: '/front/staticPage/wx-serviceterms.html',
  496. event: this.readEvent
  497. }
  498. ],
  499. buttons: {
  500. submit: this.submitOrder
  501. }
  502. }
  503. },
  504. mounted () {
  505. this.init()
  506. },
  507. methods: {
  508. // 更新价格相关
  509. updatePrice: function (before) {
  510. console.log( '原价:' + before)
  511. this.initPrice = before;
  512. // 调用子组件查询最优卡券
  513. this.$refs.couponRef.getCoupon();
  514. },
  515. // 初始化及回显相关
  516. init: function () {},
  517. // 勾选阅读协议
  518. updateS: function(data) {
  519. data.callback(this.checkSubmitStatus(data.check))
  520. },
  521. // 校验checkbox
  522. checkSubmitStatus: function (checkStatus) {
  523. sessionStorage.setItem("vipSub_read", checkStatus)
  524. renew.flushSubmit()
  525. return renew.timeCheckOk && sessionStorage.getItem("vipSub_read") === "true"
  526. },
  527. saveEvent: function () {
  528. renew.saveState()
  529. sessionStorage.setItem("pay_read_cache", "0");
  530. },
  531. // 阅读协议点击事件
  532. readEvent: function () {
  533. renew.saveState()
  534. toRead()
  535. },
  536. // 订单提交事件
  537. submitOrder: function () {
  538. this.$refs.couponRef.submitStatus = true
  539. $('.vip-footer.renew .confirm').trigger('click')
  540. this.$refs.couponRef.submitStatus = false
  541. }
  542. }
  543. })
  544. /* vue 代码 end */