mixinNew.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. import {
  2. postBuy,
  3. orderList,
  4. searchResult,
  5. changeBackUserid,
  6. changeUseridToPhone,
  7. memberGive,
  8. getCode,
  9. login,
  10. seniorStrategy,
  11. strategyGetID,
  12. postByte
  13. } from '../api'
  14. import util from '../util'
  15. import addR from "../common/addRecord";
  16. import {
  17. Dialog
  18. } from 'vant';
  19. export default {
  20. data() {
  21. return {
  22. iframeUrl: process.env.VUE_APP_IFAMRE,
  23. param: {
  24. cpid: '',
  25. spid: '',
  26. channel: "boyuan",
  27. channeltype: "1",
  28. timestamp: "",
  29. clienttype: "3",
  30. sign: "",
  31. phone: "",
  32. theme: "",
  33. version: "1.0.0",
  34. servicephone: "",
  35. },
  36. loginForm: {
  37. phone: "",
  38. vcode: "",
  39. },
  40. codeState: {
  41. state: false,
  42. time: 60,
  43. task: null,
  44. },
  45. payState: false,
  46. iframe: {},
  47. showZhezhao: false,
  48. showTimeout: false,
  49. callback: "",
  50. // 临时实现
  51. bgc: '#ffffff',
  52. showVcode: false,
  53. dateTime: new Date()
  54. }
  55. },
  56. computed: {
  57. user() {
  58. return this.$store.state.user
  59. },
  60. pageData() {
  61. this.param.cpid = this.$store.state.pageData.mainProduct.cpid
  62. this.param.spid = this.$store.state.pageData.mainProduct.spid
  63. return this.$store.state.pageData
  64. }
  65. },
  66. created() {
  67. addR.openPage() // 页面打开事件
  68. let {
  69. mappid,
  70. callback,
  71. userid,
  72. tid
  73. } = util.query2obj(location.search)
  74. if (mappid) {
  75. changeBackUserid({
  76. userid: decodeURIComponent(mappid),
  77. cpid: this.pageData.mainProduct.cpid
  78. }).then(res => {
  79. this.$sensors.registerPage({
  80. attr3: "盲盒活动"
  81. });
  82. localStorage.setItem('beCome', '盲盒活动')
  83. this.$store.commit('setUser', {
  84. userid: res.data.userid,
  85. phone: res.data.phone
  86. })
  87. addR.loginSuccess()
  88. })
  89. }
  90. if (userid) {
  91. changeUseridToPhone({
  92. userid: decodeURIComponent(userid)
  93. }).then(res => {
  94. this.$store.commit('setUser', {
  95. userid: decodeURIComponent(userid),
  96. phone: res.data
  97. })
  98. addR.loginSuccess()
  99. })
  100. }
  101. if (callback) {
  102. this.callback = callback;
  103. }
  104. if (tid) { // 任务id获取
  105. this.$sensors.registerPage({
  106. taskid: tid
  107. });
  108. // addr相关埋点也需要增加该参数 旭哥在改接口 今天更新
  109. this.$store.commit('setTid', tid)
  110. }
  111. },
  112. mounted() {
  113. var that = this
  114. let channelKey = that.pageData.channl.channelKey
  115. if (!channelKey) {
  116. localStorage.setItem('key', channelKey)
  117. }
  118. // 统一认证监听
  119. window.addEventListener('message', e => {
  120. document.body.style.overflow = '';
  121. if (e.data.type === 'onActiveCallback') {} else if (e.data.type === 'onBuyCallback') {
  122. window.console.log(e.data.data.msg)
  123. // this.$toast(e.data.data.msg)
  124. } else {
  125. try {
  126. var data = JSON.parse(e.data);
  127. switch (data.resultcode) {
  128. case "0": //处理成功
  129. changeUseridToPhone({
  130. userid: decodeURIComponent(data.userid)
  131. }).then(res => {
  132. this.$store.commit('setUser', {
  133. userid: decodeURIComponent(data.userid),
  134. phone: res.data
  135. })
  136. this.$sensors.login(res.data)
  137. this.onMessageSuccess(data);
  138. })
  139. break;
  140. case "0001": //auth_no无效
  141. case "0003": //client_id不存在
  142. case "4444": //服务暂时不可用(系统忙/系统队列满)
  143. case "9001": //关闭H5认证页面
  144. that.showZhezhao = false
  145. that.iframe.remove();
  146. that.$toast(res.data);
  147. break;
  148. default:
  149. that.$toast(res.data)
  150. break;
  151. }
  152. } catch (e) {}
  153. }
  154. }, false);
  155. // 关闭页面监听
  156. if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
  157. window.addEventListener("pagehide", e => this.unloadHandler(e));
  158. } else {
  159. window.addEventListener("unload", e => this.unloadHandler(e));
  160. }
  161. },
  162. methods: {
  163. onSearchResult() {
  164. let fromSource = localStorage.getItem('beCome')
  165. searchResult({
  166. anonymousId: this.$sensors.store._state.$device_id, //cap接口新增神策id
  167. testId: this.$store.state.testId,
  168. testGroupId: this.$store.state.testGroupId,
  169. productId: this.pageData.mainProduct.id,
  170. cpid: this.param.cpid,
  171. spid: this.param.spid,
  172. userid: this.user.userid,
  173. fromSource: fromSource,
  174. needJoinActivity: this.pageData.remarks.isZhic === '1' ? 1 : 0,
  175. })
  176. this.showTimeout = true
  177. this.getIsBuy()
  178. },
  179. checkVcode() {
  180. if (this.loginForm.vcode.length > 6) {
  181. this.loginForm.vcode = this.loginForm.vcode.slice(0, 6);
  182. }
  183. },
  184. openIframe() {
  185. document.body.style.overflow = 'hidden';
  186. var that = this
  187. // if (!localStorage.getItem('key')) {
  188. // this.$toast.fail('渠道无效')
  189. // return false
  190. // }
  191. this.$toast.loading({
  192. message: '请求中...'
  193. });
  194. let now = Date.now()
  195. that.param.timestamp = now
  196. if (that.phone) {
  197. that.param.phone = that.phone
  198. }
  199. that.param.sign = that.$md5(this.param.cpid + now + '!QAZ2wsx')
  200. let iframe = document.createElement('iframe');
  201. let bodyDom = document.getElementById('temp');
  202. iframe.id = 'iframe'
  203. iframe.classList.add('iframe')
  204. iframe.src = that.iframeUrl
  205. bodyDom.insertBefore(iframe, bodyDom.lastChild)
  206. this.iframe = iframe
  207. addR.buyClick()
  208. iframe.onload = function () {
  209. iframe.contentWindow.postMessage(JSON.stringify(that.param), that.iframeUrl)
  210. if (that.pageData.remarks.isMask === '1') {
  211. that.showZhezhao = true
  212. }
  213. that.$toast.clear();
  214. }
  215. },
  216. async onMessageSuccess(data) {
  217. this.iframe.remove();
  218. this.showZhezhao = false
  219. // this.$sensors.login(decodeURIComponent(data.userid))
  220. addR.getToken()
  221. let params = {
  222. anonymousId: this.$sensors.store._state.$device_id,
  223. channleKey: localStorage.getItem("key"),
  224. spId: this.param.spid,
  225. userId: decodeURIComponent(data.userid),
  226. };
  227. let item = await seniorStrategy(params);
  228. if (item.data !== null) {
  229. // 判断高级策略是否开启遮罩;
  230. if (item.data.enableMask !== null) {
  231. this.pageData.remarks.isMask = String(item.data.enableMask);
  232. // 判断高级策略是否有遮罩文案 并替换
  233. if (item.data.maskText !== null && item.data.maskText !== "") {
  234. this.pageData.remarks.maskText = item.data.maskText;
  235. }
  236. }
  237. // 判断高级策略是否开启有推荐
  238. if (
  239. item.data.recommendStrategy !== "" &&
  240. item.data.recommendStrategy !== null
  241. ) {
  242. let itemList = item.data.recommendStrategy.split(",");
  243. this.pageData.recommend = [];
  244. itemList.forEach((v) => {
  245. strategyGetID({
  246. id: v,
  247. }).then((resp) => {
  248. this.pageData.recommend.push(resp.data);
  249. });
  250. });
  251. }
  252. if (item.data.cancelRule !== "" && item.data.cancelRule !== null) {
  253. this.pageData.strategyInfo.unsubscribeStrategyList =
  254. item.data.cancelRule.split(",");
  255. }
  256. if (item.data.interceptRule === "1") {
  257. this.pageData.interceptRule = item.data.interceptRule;
  258. }
  259. if (item.data.location !== "" && item.data.location !== null) {
  260. this.pageData.location = item.data.location.split(",");
  261. }
  262. }
  263. if (this.pageData.interceptRule !== "1") {
  264. let fromSource = localStorage.getItem('beCome')
  265. let updata = {
  266. cpid: this.param.cpid,
  267. spid: this.param.spid,
  268. productId: this.pageData.mainProduct.id,
  269. channelKey: this.pageData.channl.channelKey,
  270. token: data.token,
  271. type: '0',
  272. userid: decodeURIComponent(data.userid),
  273. needJoinActivity: this.pageData.remarks.isZhic === '1' ? 1 : 0,
  274. fromSource: fromSource,
  275. anonymousId: this.$sensors.store._state.$device_id,
  276. testId: this.$store.state.testId,
  277. testGroupId: this.$store.state.testGroupId,
  278. }
  279. await postBuy(updata).then(res => {
  280. if (res.code == 10000) {
  281. this.isBuy = '已订购'
  282. this.payState = true
  283. addR.buySuccess()
  284. this.$toast.success('订购成功');
  285. this.postApi()
  286. if (this.pageData.remarks.isZhic === '1') {
  287. setTimeout(() => {
  288. this.giveMember()
  289. }, 1500)
  290. }
  291. if (this.pageData.mainProduct.cpid === 'mangtv16') {
  292. setTimeout(() => {
  293. window.location.href = this.pageData.mainProduct.flowJumpUrl
  294. }, 1500)
  295. }
  296. // if (this.pageData.mainProduct.threshold > 0 && this.pageData.mainProduct.threshold !== null) {
  297. // if (res.data.toMember === 1) {
  298. // setTimeout(() => {
  299. // this.giveMember()
  300. // }, 1500)
  301. // }
  302. // } else {
  303. // if (this.pageData.remarks.isZhic === '1') {
  304. // setTimeout(() => {
  305. // this.giveMember()
  306. // }, 1500)
  307. // }
  308. // }
  309. }
  310. }, response => {
  311. if (response.code !== 500) {
  312. // this.onSearchResult()
  313. this.showTimeout = true
  314. this.postApi()
  315. } else {
  316. if (response.message != "指定时间内重复请求") {
  317. if (response.message == "您已订购,请不要重复订购!" && this.pageData.remarks.orderRepeat && this.pageData.remarks.orderRepeat !== '' && this.pageData.remarks.orderRepeat !== null) {
  318. this.repeatShow = true
  319. } else {
  320. this.$toast.fail({
  321. message: response.message,
  322. duration: 5000
  323. })
  324. }
  325. }
  326. addR.buyFail(response)
  327. this.getIsBuy()
  328. }
  329. }).catch(res => {
  330. console.log(res)
  331. })
  332. } else {
  333. Dialog({
  334. message: '您不能订购当前产品!',
  335. theme: 'round-button',
  336. })
  337. }
  338. },
  339. unloadHandler(e) {
  340. // addR.closePage() //页面关闭事件
  341. let startTime = this.dateTime.getTime()
  342. let endTime = new Date().getTime()
  343. let duration = endTime - startTime
  344. localStorage.setItem('duration', duration)
  345. addR.pageBrowseTime()
  346. localStorage.removeItem('duration')
  347. },
  348. // 短信认证
  349. toBuy() {
  350. let {
  351. phone,
  352. vcode
  353. } = this.loginForm;
  354. if (!/^1[23456789]\d{9}$/.test(phone)) {
  355. return this.$toast("请输入正确手机号");
  356. }
  357. if (!vcode) {
  358. return this.$toast("请输入验证码");
  359. }
  360. addR.buyClick()
  361. return login({
  362. phone,
  363. vcode
  364. }).then(res => {
  365. clearInterval(this.codeState.task);
  366. addR.getToken()
  367. this.codeState.task = null;
  368. this.codeState.state = false;
  369. this.codeState.time = 60;
  370. let flag = this.user.userid === ''
  371. this.$store.commit('setUser', {
  372. userid: res.data.userid,
  373. phone: this.loginForm.phone
  374. })
  375. if (flag) {
  376. addR.loginSuccess()
  377. }
  378. this.loginForm = {}
  379. let params = {
  380. anonymousId: this.$sensors.store._state.$device_id,
  381. channleKey: localStorage.getItem("key"),
  382. spId: this.param.spid,
  383. userId: res.data.userid,
  384. };
  385. seniorStrategy(params).then(item => {
  386. if (item.data !== null) {
  387. // 判断高级策略是否开启遮罩;
  388. if (item.data.enableMask !== null) {
  389. this.pageData.remarks.isMask = String(item.data.enableMask);
  390. // 判断高级策略是否有遮罩文案 并替换
  391. if (item.data.maskText !== null && item.data.maskText !== "") {
  392. this.pageData.remarks.maskText = item.data.maskText;
  393. }
  394. }
  395. // 判断高级策略是否开启有推荐
  396. if (
  397. item.data.recommendStrategy !== "" &&
  398. item.data.recommendStrategy !== null
  399. ) {
  400. let itemList = item.data.recommendStrategy.split(",");
  401. this.pageData.recommend = [];
  402. itemList.forEach((v) => {
  403. strategyGetID({
  404. id: v,
  405. }).then((resp) => {
  406. this.pageData.recommend.push(resp.data);
  407. });
  408. });
  409. }
  410. if (item.data.cancelRule !== "" && item.data.cancelRule !== null) {
  411. this.pageData.strategyInfo.unsubscribeStrategyList =
  412. item.data.cancelRule.split(",");
  413. }
  414. if (item.data.interceptRule === "1") {
  415. this.pageData.interceptRule = item.data.interceptRule;
  416. }
  417. if (item.data.location !== "" && item.data.location !== null) {
  418. this.pageData.location = item.data.location.split(",");
  419. }
  420. }
  421. })
  422. if (this.pageData.interceptRule !== "1") {
  423. let fromSource = localStorage.getItem('beCome')
  424. let updata = {
  425. cpid: this.param.cpid,
  426. spid: this.param.spid,
  427. productId: this.pageData.mainProduct.id,
  428. channelKey: this.pageData.channl.channelKey,
  429. token: res.data.token ? res.data.token : "0",
  430. type: '0',
  431. userid: res.data.userid,
  432. needJoinActivity: this.pageData.remarks.isZhic === '1' ? 1 : 0,
  433. fromSource: fromSource,
  434. anonymousId: this.$sensors.store._state.$device_id,
  435. testId: this.$store.state.testId,
  436. testGroupId: this.$store.state.testGroupId,
  437. }
  438. postBuy(updata).then(res => {
  439. if (res.code == 10000) {
  440. this.isBuy = '已订购'
  441. this.payState = true
  442. addR.buySuccess()
  443. this.$toast.success('订购成功');
  444. this.postApi()
  445. if (this.pageData.remarks.isZhic === '1') {
  446. setTimeout(() => {
  447. this.giveMember()
  448. }, 1500)
  449. }
  450. if (this.pageData.mainProduct.cpid === 'mangtv16') {
  451. setTimeout(() => {
  452. window.location.href = this.pageData.mainProduct.flowJumpUrl
  453. }, 1500)
  454. }
  455. // if (this.pageData.mainProduct.threshold > 0 && this.pageData.mainProduct.threshold !== null) {
  456. // if (res.data.toMember === 1) {
  457. // setTimeout(() => {
  458. // this.giveMember()
  459. // }, 1500)
  460. // }
  461. // } else {
  462. // if (this.pageData.remarks.isZhic === '1') {
  463. // setTimeout(() => {
  464. // this.giveMember()
  465. // }, 1500)
  466. // }
  467. // }
  468. }
  469. }, response => {
  470. if (response.code !== 500) {
  471. // this.onSearchResult()
  472. this.showTimeout = true
  473. this.postApi()
  474. } else {
  475. if (response.message == "您已订购,请不要重复订购!" && this.pageData.remarks.orderRepeat && this.pageData.remarks.orderRepeat !== '' && this.pageData.remarks.orderRepeat !== null) {
  476. this.repeatShow = true
  477. } else {
  478. this.$toast.fail({
  479. message: response.message,
  480. duration: 5000
  481. })
  482. }
  483. // this.$toast.fail({
  484. // message: response.message
  485. // })
  486. addR.buyFail(response)
  487. this.getIsBuy()
  488. }
  489. }).catch(res => {
  490. console.log(res)
  491. })
  492. } else {
  493. Dialog({
  494. message: '您不能订购当前产品!',
  495. theme: 'round-button',
  496. })
  497. }
  498. }, res => {
  499. this.$toast(res.message);
  500. })
  501. },
  502. getCode() {
  503. if (this.codeState.state) return;
  504. let {
  505. phone
  506. } = this.loginForm;
  507. if (!/^1[23456789]\d{9}$/.test(phone)) {
  508. return this.$toast("请输入正确手机号");
  509. }
  510. if (this.checked === false) {
  511. return this.$toast.fail("请先勾选隐私声明");
  512. }
  513. getCode({
  514. phone
  515. }).then(res => {
  516. this.$toast("验证码已发送");
  517. this.codeState.state = true;
  518. this.codeState.task = setInterval(() => {
  519. if (this.codeState.time <= 0) {
  520. clearInterval(this.codeState.task);
  521. this.codeState.task = null;
  522. this.codeState.state = false;
  523. this.codeState.time = 60;
  524. } else {
  525. this.codeState.time--;
  526. }
  527. }, 1000);
  528. }, res => {
  529. this.$toast(res.message);
  530. })
  531. },
  532. // 直接领取会员
  533. giveMember() {
  534. let updata = {
  535. activeType: this.pageData.mainProduct.activeType,
  536. userid: this.user.userid,
  537. channelKey: this.pageData.channl.channelKey,
  538. isCompositeProduct: this.pageData.mainProduct.isCompositeProduct,
  539. };
  540. addR.memberClick();
  541. memberGive(updata)
  542. .then(
  543. (res) => {
  544. addR.memberSuccess();
  545. this.$toast.success('会员领取成功');
  546. },
  547. (response) => {
  548. addR.memberFail(response);
  549. this.$toast(response.message);
  550. }
  551. )
  552. .catch(() => {
  553. this.$toast.fail('会员领取失败,请手动领取')
  554. });
  555. },
  556. // 刷新订购状态
  557. getIsBuy() {
  558. orderList({
  559. userid: this.user.userid,
  560. cpid: this.param.cpid,
  561. spid: this.param.spid,
  562. }).then((res) => {
  563. let data = res.data;
  564. if (data.length === 0) {
  565. this.isBuy = "未订购";
  566. } else {
  567. for (let i = 0; i < data.length; i++) {
  568. if (data[i].STATUS === "0") {
  569. this.isBuy = "已订购";
  570. } else {
  571. this.isBuy = "已退订";
  572. }
  573. }
  574. }
  575. });
  576. },
  577. // 字节埋点回传
  578. postApi() {
  579. let apiQuery = util.query2obj(location.search)
  580. if (apiQuery.clickid) {
  581. let nowTime = new Date().getTime()
  582. let params = {
  583. event_type: "form",
  584. context: {
  585. ad: {
  586. callback: apiQuery.clickid
  587. },
  588. },
  589. timestamp: nowTime
  590. }
  591. postByte(params)
  592. }
  593. },
  594. }
  595. }