Recall.vue 11 KB

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