func-tbook-isbn.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Cloud Code test
  3. curl -X POST -H "Content-Type: application/json" -H 'X-Parse-Application-Id: edu-textbook' -d '{ "isbn": "9787302609865" }' http://127.0.0.1:61337/parse/functions/tbookISBN
  4. */
  5. /**
  6. *
  7. */
  8. export function defineTbookISBN(){
  9. Parse.Cloud.define("tbookISBN", async (request) => {
  10. let isbn = request.params.isbn;
  11. if(isbn) {
  12. try{
  13. let url = "https://www.tbook.com.cn/api/api/public/index/search/book_isbn_info";
  14. let response = await fetch(url,{
  15. method:"POST",
  16. json:true,
  17. body:JSON.stringify({isbn:isbn})
  18. });
  19. let text = await response?.text()
  20. try{
  21. let result = JSON.parse(text)
  22. // console.log(result)
  23. if(result?.data?.[0]){
  24. return result?.data?.[0];
  25. }
  26. }catch(err){}
  27. }catch(err){
  28. console.error(err)
  29. }
  30. }
  31. throw new Parse.Error(404,"未找到该图书信息")
  32. },{
  33. fields : {
  34. isbn:{
  35. required:true
  36. }
  37. }
  38. });
  39. }