App.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div id="app">
  3. <router-view />
  4. </div>
  5. </template>
  6. <script>
  7. import { mapState, mapActions } from "vuex";
  8. export default {
  9. name: "App",
  10. // 向子组件提供变量/方法, 子组件通过inject 注入变量
  11. provide() {
  12. return {
  13. // 校验返回值
  14. checkCode: this.checkCode,
  15. // 支付校验返回值
  16. checkCodeHandler: this.checkCodeHandler,
  17. // 刷新登录态
  18. refresh: this.refresh,
  19. // 获取用户信息
  20. getUserInfo: this.getUserInfo,
  21. // 获取鸿币余额
  22. getFinance: this.getFinance,
  23. // 支付
  24. payHandler: this.payHandler,
  25. // 订单查询
  26. queryOrder: this.queryOrder,
  27. // 路由跳转(携带路由参数)
  28. routeLink: this.routeLink,
  29. // 鸿币充值
  30. onRecharge: this.onRecharge,
  31. };
  32. },
  33. components: {},
  34. data() {
  35. return {
  36. wechatAppid: "wx2c4f9f89c572aeb5", // 公众号appid
  37. };
  38. },
  39. computed: {
  40. ...mapState(["query", "appid", "CONFIG"]),
  41. },
  42. watch: {
  43. // 监听路由变化 初始化路由
  44. $route(to, from) {
  45. this.initUrl();
  46. },
  47. },
  48. created() {
  49. this.initUrl();
  50. },
  51. mounted() {
  52. // 设置页面基础配置信息
  53. this.setConfigAction(this.$CONFIG);
  54. },
  55. methods: {
  56. ...mapActions([
  57. "queryAction",
  58. "userInfoAction",
  59. "financeAction",
  60. "appidAction",
  61. "setConfigAction",
  62. "platformAction",
  63. ]),
  64. /**
  65. * 初始化路由地址
  66. */
  67. initUrl() {
  68. // 获取url 参数
  69. const query = this.$utils.getAllQueryString();
  70. this.queryAction({ ...query });
  71. // 获取appid
  72. const _appid = this.$utils.getQueryString("appid");
  73. const appid = _appid || this.CONFIG.appid;
  74. this.appidAction(appid);
  75. // 获取当前android是哪个(针对特殊处理的包, 比如仙界情缘)
  76. const platform = this.$utils.getQueryString("platform");
  77. this.platformAction(platform);
  78. },
  79. // 获取用户信息
  80. getUserInfo() {
  81. this.$api.info().then((res) => {
  82. this.checkCode(res);
  83. const { data, code } = res.data;
  84. // 错误
  85. if (code) {
  86. return;
  87. }
  88. // 成功
  89. // localStorage.setItem("userInfo", JSON.stringify(data));
  90. this.$utils.writeStorage("", "userInfo", JSON.stringify(data));
  91. this.userInfoAction(data);
  92. });
  93. },
  94. // 查询鸿币余额
  95. getFinance() {
  96. this.$api.finance().then((res) => {
  97. this.checkCode(res);
  98. const { data, code } = res.data;
  99. // 错误
  100. if (code) {
  101. return;
  102. }
  103. // 成功
  104. this.financeAction(data.hb / 100);
  105. });
  106. },
  107. /**
  108. * 支付宝 微信支付
  109. * @param {Object} payInfo 支付信息
  110. */
  111. payHandler(payInfo) {
  112. const {
  113. server_id,
  114. server_name,
  115. cp_order_id,
  116. role_name,
  117. role_id,
  118. goods_id,
  119. goods_name,
  120. amount,
  121. pay_type,
  122. pay_platform,
  123. extra,
  124. } = payInfo;
  125. const { appid } = this;
  126. const returnUrlLeft =
  127. window.location.origin + window.location.pathname + "#/counter?";
  128. const returnUrlQuery = `pay=game&appid=${appid}&server_id=${server_id}&server_name=${server_name}&cp_order_id=${cp_order_id}&role_name=${role_name}&role_id=${role_id}&goods_id=${goods_id}&goods_name=${goods_name}&amount=${amount}&pay_type=${pay_type}&pay_platform=${pay_platform}`;
  129. return this.$api
  130. .order({
  131. server_id,
  132. server_name,
  133. cp_order_id,
  134. app_id: appid,
  135. role_name,
  136. role_id,
  137. goods_id,
  138. goods_name,
  139. amount,
  140. pay_type,
  141. pay_platform,
  142. return_url: returnUrlLeft + returnUrlQuery,
  143. extra,
  144. })
  145. .then((res) => {
  146. this.checkCodeHandler(res);
  147. const { data, code, msg } = res.data;
  148. // 错误
  149. if (code) {
  150. return;
  151. }
  152. // 鸿币支付不需要跳转页面
  153. if (pay_platform === "HB") {
  154. return;
  155. }
  156. window.location.href = data.url;
  157. });
  158. },
  159. /**
  160. * 鸿币充值
  161. */
  162. onRecharge(payInfo) {
  163. const { pay_platform, amount, goods_name, pay_type, extra } = payInfo;
  164. const { appid } = this;
  165. const returnUrlLeft =
  166. window.location.origin + window.location.pathname + "#/counter?";
  167. const returnUrlQuery = `pay=hb&appid=${appid}&amount=${amount}&goods_name=${goods_name}&pay_type=${pay_type}&pay_platform=${pay_platform}`;
  168. return this.$api
  169. .recharge({
  170. amount,
  171. goods_name,
  172. app_id: appid,
  173. pay_type,
  174. pay_platform,
  175. return_url: returnUrlLeft + returnUrlQuery,
  176. extra,
  177. })
  178. .then((res) => {
  179. this.checkCodeHandler(res);
  180. const { data, code } = res.data;
  181. // 错误
  182. if (code) {
  183. return res;
  184. }
  185. // 成功
  186. const time = new Date().getTime();
  187. const orderItem = {
  188. order_id: data.order_id,
  189. time,
  190. server_id: "",
  191. server_name: "",
  192. cp_order_id: "",
  193. app_id: appid,
  194. role_name: "",
  195. role_id: "",
  196. goods_id: "",
  197. goods_name,
  198. amount,
  199. pay_type,
  200. };
  201. // let orderList = JSON.parse(localStorage.getItem("orderList")) || [];
  202. let orderList = this.$utils.readStorage("", "orderList");
  203. orderList = JSON.parse(orderList) || [];
  204. orderList.push(orderItem);
  205. // localStorage.setItem("orderList", JSON.stringify(orderList));
  206. this.$utils.writeStorage("", "orderList", JSON.stringify(orderList));
  207. window.location.href = data.url;
  208. return res;
  209. });
  210. },
  211. // 订单查询
  212. queryOrder(order_id) {
  213. this.$api.query({
  214. order_id,
  215. });
  216. },
  217. // 校验返回值
  218. checkCode(res) {
  219. // 请求超时
  220. if (!res) {
  221. this.$toast.text("当前网络拥堵, 请重试");
  222. return;
  223. }
  224. this.checkCodeHandler(res);
  225. },
  226. // 刷新登录态
  227. refresh() {
  228. return this.$api.refresh().then((res) => {
  229. this.checkCode(res);
  230. const { code } = res.data;
  231. // 错误
  232. if (code) {
  233. // localStorage.removeItem("userInfo");
  234. this.$utils.deleteStorage("", "userInfo");
  235. return;
  236. }
  237. // const userInfo = localStorage.getItem("userInfo");
  238. const userInfo = this.$utils.readStorage("", "userInfo");
  239. this.userInfoAction(JSON.parse(userInfo));
  240. });
  241. },
  242. // 校验返回值====all
  243. checkCodeHandler(res) {
  244. const { code, msg } = res.data;
  245. if (!code) {
  246. return;
  247. }
  248. switch (code) {
  249. // 2 未登录
  250. case 2:
  251. // localStorage.removeItem("userInfo");
  252. this.$utils.deleteStorage("", "userInfo");
  253. this.userInfoAction({});
  254. // 跳转登录
  255. this.routeLink("Login");
  256. break;
  257. // 7 微信支付code 过期
  258. case 7:
  259. this.$toast.text(msg);
  260. break;
  261. default:
  262. this.$toast.text(msg);
  263. }
  264. },
  265. // 跳转页面
  266. routeLink(routeName, params) {
  267. const query = this.$utils.getAllQueryString();
  268. this.queryAction({ ...query });
  269. this.$utils.routeLink(routeName, query, params);
  270. },
  271. },
  272. };
  273. </script>
  274. <style lang="less">
  275. #app {
  276. font-family: "Avenir", Helvetica, Arial, sans-serif;
  277. -webkit-font-smoothing: antialiased;
  278. -moz-osx-font-smoothing: grayscale;
  279. text-align: center;
  280. // color: #eee;
  281. color: #555555;
  282. font-size: 28 / @rem;
  283. }
  284. /**横屏 */
  285. @media screen and (orientation: landscape),
  286. only screen and (min-device-width: 768px) and (min-device-height: 768px) and (orientation: portrait) {
  287. #app {
  288. font-size: 16 / @rem;
  289. }
  290. }
  291. </style>