Recall.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <div id="recall">
  3. <!-- 点击下载提示 -->
  4. <DownLoadInfo @onShowControl="onShowControl"></DownLoadInfo>
  5. <!-- safari添加到桌面提示 -->
  6. <SafariTips v-if="showSafariTips" @onShowControl="onShowControl" />
  7. <!-- 退出页面提示 -->
  8. <ExitGame></ExitGame>
  9. <!-- 页面悬浮球 -->
  10. <Float @openInnerBox="openInnerBox"></Float>
  11. <!-- 游戏iframe -->
  12. <GameIframe></GameIframe>
  13. <!-- 已登录 -->
  14. <InnerBox v-show="isLogin && showInnerBox"></InnerBox>
  15. <!-- 未 显示登录框 -->
  16. <UdbBox v-if="!isLogin"></UdbBox>
  17. </div>
  18. </template>
  19. <script>
  20. import { recallConfig } from "../../config";
  21. import { mapState, mapActions } from "vuex";
  22. import DownLoadInfo from "@/components/Inner/DownLoadInfo"; // 点击下载到桌面提示
  23. import ExitGame from "@/components/Inner/ExitGame.vue"; // 退出页面提示
  24. import Float from "@/components/Inner/Float"; // 页面悬浮球
  25. import GameIframe from "@/components/Inner/GameIframe"; // 游戏iframe
  26. import InnerBox from "@/components/Inner/InnerBox"; // 已登录显示的内容
  27. import UdbBox from "@/components/UDB/UdbBox"; // 登录框
  28. import SafariTips from "@/components/Inner/SafariTips"; // safari添加到桌面提示
  29. export default {
  30. name: "Recall",
  31. components: {
  32. DownLoadInfo,
  33. ExitGame,
  34. Float,
  35. GameIframe,
  36. InnerBox,
  37. UdbBox,
  38. SafariTips,
  39. },
  40. data() {
  41. return {
  42. queryTimeout: null, // 订单查询 定时器
  43. wechatAppid: "wx2c4f9f89c572aeb5", // 公众号appid
  44. showSafariTips: false, // 显示safari组件
  45. };
  46. },
  47. provide() {
  48. return {
  49. // 校验返回值
  50. checkCode: this.checkCode,
  51. // 支付校验返回值
  52. checkCodeHandler: this.checkCodeHandler,
  53. // 刷新登录态
  54. refresh: this.refresh,
  55. // 获取用户信息
  56. getUserInfo: this.getUserInfo,
  57. // 获取鸿币余额
  58. getFinance: this.getFinance,
  59. // 支付
  60. payHandler: this.payHandler,
  61. // 订单查询
  62. queryOrder: this.queryOrder,
  63. // 微信内支付
  64. wechatPay: this.wechatPay,
  65. // 获取微信code
  66. getWechatCode: this.getWechatCode,
  67. };
  68. },
  69. computed: {
  70. ...mapState(["CONFIG", "isLogin", "showInnerBox", "isPay", "query","appid"]),
  71. userInfo() {
  72. return this.$store.state.userInfo;
  73. },
  74. },
  75. watch: {
  76. // ios url带token 直接登录进去
  77. userInfo: {
  78. handler(newValue) {
  79. if (newValue) {
  80. const token = this.$utils.getQueryString("token");
  81. if (token) {
  82. window.location.href = this.$utils.delQueStr(
  83. window.location.href,
  84. "token"
  85. );
  86. }
  87. }
  88. },
  89. deep: true,
  90. immediate: true,
  91. },
  92. },
  93. created() {
  94. // 携带token 直接登录
  95. const token = this.$utils.getQueryString("token");
  96. if (token) {
  97. localStorage.setItem("x-token", token);
  98. }
  99. },
  100. mounted() {
  101. clearTimeout(this.queryTimeout);
  102. // 设置页面基础配置信息
  103. this.setConfigAction(recallConfig);
  104. // 初始化页面信息
  105. this.init();
  106. },
  107. methods: {
  108. ...mapActions([
  109. "setConfigAction",
  110. "showInnerAction",
  111. "userInfoAction",
  112. "isLoginAction",
  113. "financeAction",
  114. "isPayAction",
  115. "tokenAction",
  116. "isShowExitGameAction",
  117. "isWeChatAction",
  118. ]),
  119. onShowControl(showSafariTips) {
  120. this.showSafariTips = showSafariTips;
  121. },
  122. // 初始化页面信息
  123. async init() {
  124. // 微信内
  125. const _this = this;
  126. document.addEventListener(
  127. "WeixinJSBridgeReady",
  128. function onBridgeReady() {
  129. _this.isWeChatAction(true);
  130. // const wechat_open_id = localStorage.getItem("wechat_open_id");
  131. const wechat_open_id = _this.$utils.getCookie("wechat_open_id");
  132. const wechat_code = _this.$utils.getQueryString("code");
  133. if (!wechat_open_id && !wechat_code) {
  134. _this.$toast.text("授权过期,请重新授权");
  135. _this.getWechatCode();
  136. return;
  137. }
  138. // 有code, 没有openid 用code换openid
  139. if (!wechat_open_id && wechat_code) {
  140. _this.wechatOpenId();
  141. }
  142. },
  143. false
  144. );
  145. // userInfo
  146. // const userInfo = localStorage.getItem("userInfo");
  147. // if (!userInfo) {
  148. // this.isLoginAction(false);
  149. // return;
  150. // }
  151. // 校验登录态
  152. await this.refresh();
  153. // 获取用户信息
  154. await this.getUserInfo();
  155. // 获取鸿币余额
  156. await this.getFinance();
  157. // 订单查询
  158. await this.queryOrder();
  159. },
  160. // 换openid
  161. wechatOpenId() {
  162. const wechat_code = this.$utils.getQueryString("code");
  163. const pay_type = this.$utils.payType("WECHATPAY").h5;
  164. this.$api
  165. .wechatOpenId({
  166. wechat_code,
  167. pay_type,
  168. })
  169. .then((res) => {
  170. this.checkCode(res);
  171. const { data, code, msg } = res.data;
  172. // 错误
  173. if (code) {
  174. return;
  175. }
  176. // 成功
  177. this.$utils.setCookie("wechat_open_id", data.openId, 30);
  178. });
  179. },
  180. // 获取用户信息
  181. getUserInfo() {
  182. this.$api
  183. .info()
  184. .then((res) => {
  185. this.checkCode(res);
  186. const { data, code, msg } = res.data;
  187. // 错误
  188. if (code) {
  189. return;
  190. }
  191. // 成功
  192. this.isLoginAction(true);
  193. localStorage.setItem("userInfo", JSON.stringify(data));
  194. this.userInfoAction(data);
  195. const token = localStorage.getItem("token");
  196. this.tokenAction(token);
  197. })
  198. .then(() => {
  199. // 携带token 直接登录
  200. const token = this.$utils.getQueryString("token");
  201. if (token) {
  202. // window.location.href = this.$utils.delQueStr(
  203. // window.location.href,
  204. // "token"
  205. // );
  206. return;
  207. }
  208. });
  209. },
  210. // 查询鸿币余额
  211. getFinance() {
  212. this.$api.finance().then((res) => {
  213. this.checkCode(res);
  214. const { data, code, msg } = res.data;
  215. // 错误
  216. if (code) {
  217. return;
  218. }
  219. // 成功
  220. this.financeAction(data.hb / 100);
  221. });
  222. },
  223. // 微信获取微信code
  224. getWechatCode() {
  225. const { wechatAppid } = this;
  226. let redirect_uri = "";
  227. redirect_uri = escape(window.location.href);
  228. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${wechatAppid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
  229. },
  230. /**
  231. * 支付宝 微信支付
  232. * @param {Object} payInfo 支付信息
  233. */
  234. payHandler(payInfo) {
  235. const {
  236. server_id,
  237. server_name,
  238. cp_order_id,
  239. role_name,
  240. role_id,
  241. goods_id,
  242. goods_name,
  243. amount,
  244. pay_type,
  245. pay_platform,
  246. extra,
  247. } = payInfo;
  248. const { appid } = this;
  249. return this.$api
  250. .order({
  251. server_id,
  252. server_name,
  253. cp_order_id,
  254. app_id: appid,
  255. role_name,
  256. role_id,
  257. goods_id,
  258. goods_name,
  259. amount,
  260. pay_type,
  261. pay_platform,
  262. return_url: window.location.href,
  263. extra,
  264. })
  265. .then((res) => {
  266. this.checkCodeHandler(res);
  267. const { data, code, msg } = res.data;
  268. // 错误
  269. if (code) {
  270. return;
  271. }
  272. // 成功
  273. this.isPayAction(true);
  274. // 鸿币支付不需要回调
  275. if (pay_platform === "HB") {
  276. return;
  277. }
  278. const time = new Date().getTime();
  279. const orderItem = {
  280. order_id: data.order_id,
  281. time,
  282. };
  283. let orderList = JSON.parse(localStorage.getItem("orderList")) || [];
  284. orderList.push(orderItem);
  285. localStorage.setItem("orderList", JSON.stringify(orderList));
  286. // 微信内支付
  287. const { isWeChat } = this;
  288. if (isWeChat) {
  289. this.wechatPay(data);
  290. return;
  291. }
  292. window.location.href = data.url;
  293. });
  294. },
  295. // 微信内支付
  296. wechatPay(res) {
  297. if (typeof WeixinJSBridge == "undefined") {
  298. if (document.addEventListener) {
  299. document.addEventListener(
  300. "WeixinJSBridgeReady",
  301. this.onBridgeReady(res),
  302. false
  303. );
  304. } else if (document.attachEvent) {
  305. document.attachEvent("WeixinJSBridgeReady", this.onBridgeReady(res));
  306. document.attachEvent(
  307. "onWeixinJSBridgeReady",
  308. this.onBridgeReady(res)
  309. );
  310. }
  311. } else {
  312. this.onBridgeReady(res);
  313. }
  314. },
  315. // 微信内支付
  316. onBridgeReady(res) {
  317. const { appId, timeStamp, nonceStr, signType, paySign, openId } = res;
  318. // localStorage.setItem("wechat_open_id", openId);
  319. this.$utils.setCookie("wechat_open_id", openId, 30);
  320. const _this = this;
  321. WeixinJSBridge.invoke(
  322. "getBrandWCPayRequest",
  323. {
  324. appId, //公众号ID,由商户传入
  325. timeStamp, //时间戳,自1970年以来的秒数
  326. nonceStr, //随机串
  327. package: res.package,
  328. signType, //微信签名方式:
  329. paySign, //微信签名
  330. },
  331. function (res) {
  332. if (res.err_msg == "get_brand_wcpay_request:ok") {
  333. _this.$toast.text("支付成功");
  334. // 使用以上方式判断前端返回,微信团队郑重提示:
  335. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  336. return;
  337. }
  338. if (res.err_msg == "get_brand_wcpay_request:cancel") {
  339. _this.$toast.text("取消支付");
  340. return;
  341. }
  342. _this.$toast.text("支付失败");
  343. }
  344. );
  345. },
  346. // 订单查询
  347. queryOrder() {
  348. // 延迟执行定义定时器
  349. this.queryTimeout = setTimeout(() => {
  350. let orderList = JSON.parse(localStorage.getItem("orderList")) || [];
  351. if (orderList.length == 0) {
  352. clearTimeout(this.queryTimeout);
  353. return;
  354. }
  355. const maxTime = 10000;
  356. const nowTime = new Date().getTime();
  357. let _orderList = JSON.parse(JSON.stringify(orderList));
  358. orderList.map((ele, index) => {
  359. const flag = nowTime - ele.time > maxTime;
  360. if (!flag) {
  361. return;
  362. }
  363. // 大于10s 订单查询
  364. this.$api
  365. .query({
  366. order_id: ele.order_id,
  367. })
  368. .then((res) => {
  369. _orderList.splice(index, 1);
  370. localStorage.setItem("orderList", JSON.stringify(_orderList));
  371. });
  372. });
  373. this.queryOrder();
  374. }, 5000);
  375. },
  376. // 显示innerBox
  377. openInnerBox() {
  378. this.showInnerAction(true);
  379. },
  380. // 校验返回值
  381. checkCode(res) {
  382. // 请求超时
  383. if (!res) {
  384. this.$toast.text("当前网络拥堵, 请重试");
  385. return;
  386. }
  387. this.checkCodeHandler(res);
  388. },
  389. // 刷新登录态
  390. refresh() {
  391. return this.$api.refresh().then((res) => {
  392. this.checkCode(res);
  393. const { data, code, msg } = res.data;
  394. // 错误
  395. if (code) {
  396. this.isLoginAction(false);
  397. this.showInnerAction(false);
  398. localStorage.removeItem("userInfo");
  399. return;
  400. }
  401. const userInfo = localStorage.getItem("userInfo");
  402. this.isLoginAction(true);
  403. this.userInfoAction(JSON.parse(userInfo));
  404. });
  405. },
  406. // 校验返回值====all
  407. checkCodeHandler(res) {
  408. const { code, msg } = res.data;
  409. if (!code) {
  410. return;
  411. }
  412. switch (code) {
  413. // 2 未登录
  414. case 2:
  415. localStorage.removeItem("userInfo");
  416. this.showInnerAction(false);
  417. this.isLoginAction(false);
  418. this.userInfoAction({});
  419. this.$toast.text(msg);
  420. break;
  421. // 7 微信支付code 过期
  422. case 7:
  423. this.getWechatCode();
  424. this.$toast.text(msg);
  425. break;
  426. default:
  427. this.$toast.text(msg);
  428. }
  429. },
  430. },
  431. };
  432. </script>
  433. <style lang="less" scoped>
  434. .focusState {
  435. position: absolute;
  436. top: 0;
  437. left: 0;
  438. right: 0;
  439. bottom: 0;
  440. }
  441. </style>