recharge.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. var vm = new Vue({
  2. delimiters: ['${', '}'],
  3. el: '#app',
  4. data: function () {
  5. return {
  6. sessKey: '$data-data-pack-recharge',
  7. userInfo: {
  8. phone: ''
  9. },
  10. priceInfo: {},
  11. price: {
  12. before: 0, // 优惠前价格
  13. after: 0 // 优惠后价格
  14. },
  15. specActive: 1,
  16. charge: {
  17. discount: 1,
  18. count: 1000,
  19. duration: 2, // 单位年
  20. },
  21. specList: [
  22. {
  23. label: '标准字段包',
  24. price: 0.45,
  25. id: 0
  26. },
  27. {
  28. label: '高级字段包',
  29. price: 0.9,
  30. id: 1
  31. }
  32. ],
  33. chargeCount: {
  34. value: 1000, // 选择器暂存数据
  35. pickerShow: false,
  36. infoList: [
  37. {
  38. label: '1000条',
  39. value: 1000,
  40. normal_discount: 1,
  41. senior_discount: 1
  42. },
  43. {
  44. label: '3000条',
  45. value: 3000,
  46. normal_discount: 1,
  47. senior_discount: 1
  48. },
  49. {
  50. label: '5000条',
  51. value: 5000,
  52. normal_discount: 1,
  53. senior_discount: 1
  54. }
  55. ],
  56. },
  57. dialog: {
  58. question: false
  59. },
  60. bottomConf: {
  61. type: 'dataPack',
  62. initPrice: 0,
  63. realPrice: 0,
  64. disPrice: 0,
  65. checkboxStatus: false,
  66. hideCoupon: false,
  67. links: [
  68. {
  69. text: '《剑鱼标讯线上购买与服务条款》',
  70. url: '',
  71. event: this.readEvent
  72. }
  73. ],
  74. buttons: {
  75. submit: this.submitOrder
  76. }
  77. }
  78. }
  79. },
  80. computed: {
  81. // 手机号校验是否通过
  82. validatorPhonePass: function () {
  83. var phone = this.userInfo.phone
  84. if (!phone) return true
  85. return /^1\d{10}$/.test(phone)
  86. },
  87. },
  88. watch: {
  89. 'charge.count': function () {
  90. this.sortPrice()
  91. this.calcPrice()
  92. },
  93. specActive: function () {
  94. this.sortPrice()
  95. this.calcPrice()
  96. },
  97. validatorPhonePass: function () {
  98. this.checkStatus()
  99. },
  100. },
  101. created: function () {
  102. var restore = this.restoreState()
  103. if (!restore) {
  104. this.getUserInfo()
  105. this.getGoodsList()
  106. }
  107. },
  108. mounted: function () {
  109. this.checkConfirmButtonDisabled()
  110. utils.iosBackRefresh()
  111. },
  112. methods: {
  113. showLoading: function () {
  114. return this.$toast.loading({
  115. duration: 0,
  116. forbidClick: true,
  117. message: 'loading...',
  118. })
  119. },
  120. showToast: function (message) {
  121. return this.$toast({
  122. duration: 1500,
  123. forbidClick: true,
  124. message: message,
  125. })
  126. },
  127. showDialog: function (conf) {
  128. var defaultConf = {
  129. title: '提示',
  130. message: 'message',
  131. className: 'j-confirm-dialog',
  132. showConfirmButton: true,
  133. showCancelButton: true,
  134. confirmButtonColor: '#2abed1'
  135. }
  136. if (conf) {
  137. Object.assign(defaultConf, conf)
  138. }
  139. return this.$dialog.confirm(defaultConf)
  140. },
  141. getGoodsList: function () {
  142. var _this = this
  143. var loading = this.showLoading()
  144. $.ajax({
  145. url: '/subscribepay/dataExportPack/goodsList',
  146. type: 'POST',
  147. success: function (res) {
  148. loading && loading.clear()
  149. if (res) {
  150. Object.assign(_this.priceInfo, res)
  151. _this.initPageInfo()
  152. _this.sortPrice()
  153. _this.calcPrice()
  154. }
  155. },
  156. error: function () {
  157. loading && loading.clear()
  158. }
  159. })
  160. },
  161. getUserInfo: function () {
  162. var _this = this
  163. $.ajax({
  164. url: '/jypay/user/getAccountInfo?t=' + Date.now(),
  165. type: 'GET',
  166. success: function (res) {
  167. if (res && res.error_code === 0) {
  168. Object.assign(_this.userInfo, res.data)
  169. }
  170. }
  171. })
  172. },
  173. // 根据请求返回值初始化页面参数
  174. initPageInfo: function () {
  175. var info = this.priceInfo
  176. // 总折扣
  177. this.charge.discount = info.discount
  178. // 有效时间
  179. this.charge.duration = info.packs_validityYear
  180. var list = []
  181. if (Array.isArray(info.packs_showList)) {
  182. info.packs_showList.forEach(function (item) {
  183. list.push({
  184. label: item.packNum + '条',
  185. value: item.packNum,
  186. normal_discount: item.normal_discount,
  187. senior_discount: item.senior_discount
  188. })
  189. })
  190. this.chargeCount.infoList = list
  191. this.chargeCount.value = this.chargeCount.infoList[0].value
  192. this.charge.count = this.chargeCount.value
  193. }
  194. },
  195. // 计算卡片展示金额
  196. sortPrice: function () {
  197. var info = this.priceInfo
  198. // 获取被选中的条数info
  199. var countItem = this.getCountItem()
  200. // 条数折扣
  201. var cDiscount = this.specActive == 1 ? countItem.senior_discount : countItem.normal_discount
  202. // 普通字段包单价,单位元
  203. this.specList[0].before = info.unitPrice_normal
  204. this.specList[0].price = info.unitPrice_normal * cDiscount * info.discount
  205. // 高级字段包单价,单位元
  206. this.specList[1].before = info.unitPrice_senior
  207. this.specList[1].price = info.unitPrice_senior * cDiscount * info.discount
  208. },
  209. // 计算卡片展示金额 和 支付总金额
  210. calcPrice: function () {
  211. // 获取被选中的规格卡片info
  212. var specItem = this.getSpecItem()
  213. // 获取被选中的条数info
  214. var countItem = this.getCountItem()
  215. // 数据包折扣
  216. var discount = this.charge.discount
  217. // 条数折扣
  218. var cDiscount = this.specActive == 1 ? countItem.senior_discount : countItem.normal_discount
  219. // 优惠前价格
  220. var beforePrice = specItem.before * 1 * countItem.value * 1
  221. // 优惠后价格
  222. var afterPrice = specItem.before * discount * countItem.value * cDiscount
  223. this.price.before = beforePrice.toFixed(2)
  224. this.price.after = afterPrice.toFixed(2)
  225. this.updatePrice(this.price.after, this.price.before)
  226. },
  227. // 获取被选中的规格卡片info
  228. getSpecItem: function () {
  229. var _this = this
  230. var t = {}
  231. this.specList.some(function (item) {
  232. var gotThis = item.id == _this.specActive
  233. if (gotThis) {
  234. t = item
  235. }
  236. return gotThis
  237. })
  238. return t
  239. },
  240. // 获取被选中的条数info
  241. getCountItem: function () {
  242. var _this = this
  243. var t = {}
  244. this.chargeCount.infoList.some(function (item) {
  245. var gotThis = item.value == _this.charge.count
  246. if (gotThis) {
  247. t = item
  248. }
  249. return gotThis
  250. })
  251. return t
  252. },
  253. chargeCountPopupClosed: function () {
  254. this.chargeCount.value = this.charge.count
  255. },
  256. clickSpec: function (item) {
  257. this.specActive = item.id
  258. },
  259. showQuestion: function () {
  260. this.dialog.question = true
  261. },
  262. updateS: function (data) {
  263. var check = data.check
  264. var callback = data.callback
  265. callback(this.checkSubmitStatus(check))
  266. },
  267. checkSubmitStatus: function (checkStatus) {
  268. this.bottomConf.checkboxStatus = checkStatus
  269. return checkStatus && this.validatorPhonePass
  270. },
  271. // 保证mounted之后执行
  272. checkConfirmButtonDisabled: function () {
  273. this.$refs.couponRef.submitStatus = !this.bottomConf.checkboxStatus
  274. },
  275. // 调用此方法,即可检测当前页面表单是否满足条件(可计算出提交按钮是否可用)
  276. checkStatus: function () {
  277. // 此处取反,因为在调用couponRef.chooseCheckbox时候,会对checkbox再次取反
  278. this.bottomConf.checkboxStatus = !this.bottomConf.checkboxStatus
  279. this.$refs.couponRef.chooseCheckbox()
  280. },
  281. readEvent: function () {
  282. this.savePageState()
  283. if (utils.isWeiXinBrowser) {
  284. location.href = '/front/staticPage/wx-serviceterms.html'
  285. } else {
  286. location.href = '/jyapp/front/staticPage/dataExport_serviceterms.html'
  287. }
  288. },
  289. getPickId: function () {
  290. var specMap = {
  291. 0: 'normal',
  292. 1: 'senior'
  293. }
  294. return [specMap[this.specActive], this.charge.count].join('_')
  295. },
  296. submitOrder: function () {
  297. var loading = this.showLoading()
  298. var packId = this.getPickId()
  299. var data = {
  300. packId: packId,
  301. lotteryId: this.$refs.couponRef.coupon.userLotteryId,
  302. order_phone: this.userInfo.phone
  303. }
  304. $.ajax({
  305. url: '/subscribepay/dataExportPack/createOrder',
  306. type: 'POST',
  307. data: data,
  308. success: function (res) {
  309. loading && loading.clear()
  310. if (res.error_code === 0 && res.data) {
  311. if (utils.isWeiXinBrowser) {
  312. history.replaceState({}, '', '/weixin/common/dataPack/orderDetail?order_code=' + res.data.orderCode);
  313. location.href = '/weixin/pay/checkout_dataPack?orderCode=' + res.data.orderCode
  314. } else {
  315. history.replaceState({}, '', '/jyapp/common/dataPack/orderDetail?order_code=' + res.data.orderCode);
  316. location.href = "/jyapp/pay/checkout_dataPack?orderCode=" + res.data.orderCode + '&from=buy'
  317. }
  318. }
  319. },
  320. error: function () {
  321. loading && loading.clear()
  322. }
  323. })
  324. },
  325. updatePrice: function (after, before) {
  326. console.log('原价:' + before, '折扣价:' + after)
  327. this.bottomConf.disPrice = after;
  328. this.bottomConf.initPrice = before;
  329. this.$refs.couponRef.getCoupon()
  330. },
  331. restoreState: function () {
  332. var $data = sessionStorage.getItem(this.sessKey)
  333. if ($data) {
  334. $data = JSON.parse($data)
  335. Object.assign(this.priceInfo, $data.priceInfo)
  336. Object.assign(this.userInfo, $data.userInfo)
  337. Object.assign(this.price, $data.price)
  338. this.specActive = $data.specActive
  339. Object.assign(this.charge, $data.charge)
  340. Object.assign(this.specList, $data.specList)
  341. Object.assign(this.chargeCount, $data.chargeCount)
  342. Object.assign(this.bottomConf, $data.bottomConf)
  343. sessionStorage.removeItem(this.sessKey)
  344. }
  345. return !!$data
  346. },
  347. savePageState: function () {
  348. var data = {
  349. priceInfo: this.priceInfo,
  350. userInfo: this.userInfo,
  351. price: this.price,
  352. specActive: this.specActive,
  353. charge: this.charge,
  354. specList: this.specList,
  355. chargeCount: this.chargeCount,
  356. bottomConf: {
  357. initPrice: this.bottomConf.initPrice,
  358. realPrice: this.bottomConf.realPrice,
  359. disPrice: this.bottomConf.disPrice,
  360. checkboxStatus: this.bottomConf.checkboxStatus
  361. }
  362. }
  363. sessionStorage.setItem(this.sessKey, JSON.stringify(data))
  364. }
  365. }
  366. })