Browse Source

promise finally implementation

Zhengxiaowei 5 years ago
parent
commit
fb4e16caba
5 changed files with 47 additions and 6 deletions
  1. 33 2
      src/api/index.js
  2. 8 0
      src/api/promise.finally.js
  3. 2 0
      src/api/utils.js
  4. 2 2
      src/helper/regenerator.js
  5. 2 2
      src/views/Home/index.ux

+ 33 - 2
src/api/index.js

@@ -6,6 +6,37 @@
  * @Description: api
  */
 import fly from "./fly";
-export const demoApi = () => {
-  return fly.get("/books/male/index");
+
+// 获取首页数据
+export const getHomeList = (sex) => {
+  return fly.get(`/books/${sex}/index`);
 };
+
+
+// 获取分类
+export const getCategory = () => {
+  return fly.get("/books/getCategory");
+}
+
+// 获取书库
+export const getBooksList = (params) => {
+  return fly.get("/books/library", params);
+}
+
+// 获取图书详情
+export const getBooksInfo = (bid) => {
+  return fly.get(`/book/${bid}`);
+}
+
+// 获取详情页相似数据
+export const getSimilarBooks = (category_id, bid) => {
+  return fly.get("/books/similar", {
+    category_id,
+    bid
+  })
+}
+
+// 获取充值列表
+export const getChargeList = () => {
+  return fly.get("/order/chargeList");
+}

+ 8 - 0
src/api/promise.finally.js

@@ -0,0 +1,8 @@
+// promise finally 实现
+Promise.prototype.finally = function (callback) {
+  let P = this.constructor;
+  return this.then(
+    value => P.resolve(callback()).then(() => value),
+    reason => P.resolve(callback()).then(() => { throw reason })
+  );
+};

+ 2 - 0
src/api/utils.js

@@ -1,3 +1,4 @@
+import "./promise.finally";
 import fly from "./fly";
 import qs from "qs";
 import storage from '@system.storage';
@@ -22,6 +23,7 @@ export const getToken = async () => {
   }
 
   getToken.promise = getToken.promise.finally(() => {
+    console.log("done");
     getToken.promise = null;
   }).then(r => {
     return setToken(r);

+ 2 - 2
src/helper/regenerator.js

@@ -1,8 +1,8 @@
 // 脚本:regenerator.js
 // 注意:仅用于注入类库函数,不允许存储页面组件等数据
-const injectRef = Object.getPrototypeOf(global) || global
+const injectRef = Object.getPrototypeOf(global) || global;
 
 // 注入regeneratorRuntime
-injectRef.regeneratorRuntime = require('@babel/runtime/regenerator')
+injectRef.regeneratorRuntime = require('@babel/runtime/regenerator');
 // 如果使用的 hap-toolkit 版本低于0.0.38(babel 版本低于 7),则按下面的方式引入
 // injectRef.regeneratorRuntime = require('babel-runtime/regenerator')

+ 2 - 2
src/views/Home/index.ux

@@ -99,7 +99,7 @@
 
 
 <script>
-import { demoApi } from "../../api";
+import { getHomeList } from "../../api";
 
 export default {
   props: {},
@@ -121,7 +121,7 @@ export default {
     }
   },
   onInit() {
-    demoApi().then(r => {
+    getHomeList().then(r => {
       console.log(r);
     })
   },