const fetch = require('node-fetch'); // Node.js 18以下版本需要安装 node-fetch const tools = require('../../../tools'); const config = require('../../../etc/config.json'); const redis_help = require('../../use_redis'); const helper = require('../../helper'); const { urlencoded } = require('express'); const mysql = require('mysql2/promise'); const mldbConfig = config.isDebug?config.debug_mysql:config.release_mysql const CMD = {} CMD.search_id = async function(bookId) { try{ let timestamp = helper.getCurrentUnixTimestamp() let token = await require('../zy_mf/get_zy_mf_token').get_token() let app_key = "shiyuestory_mini" let user_id = 7850 let clinet = tools.getOneNewClinet() let param = `access_token=${token}&app_key=${app_key}&user_id=${user_id}&book_id=${bookId}&page=1&size=10` let response = await clinet.get(`https://openapi-gaia.zhangyue.com/opendata/reseller/free/book/list?${param}`) let list = null if(response.data.code!=0){ console.error("search_id error:",response) throw response.data }else{ list = response.data.body.list } if(list.length<=0){ throw list } console.log('知乎书籍::响应状态:', response.status); let book_info = list[0] let info = {} info.words = book_info.word_count info.book_name = book_info.book_name info.book_id = book_info.book_id info.publish_time = book_info.create_time info.genre = 3; return info; }catch(error){ console.error('掌阅书籍::请求错误:', error); return null; } } CMD.new_search_id = async function(bookId) { try{ let authorization = await redis_help.getKeyValue("zy_mf_token") console.log("authorization:",authorization) let clinet = tools.getOneNewClinet({ // "cookie":"acw_tc=1a0c639417498037411406820e006fb57d572ffdc9c83cd8a46aaf8d19ca00", "authorization": authorization, }) let list = null let response = await clinet.get(`https://gaia.zhangyue.com/gaia/v1/open_api/pay/book/getBookInfoList?reseller_id=9886&application_key=shiyuestory_mini&book_ids=${bookId}&book_content_type=&author=©right_type=&category_sex=&is_complete=&total=0&page=1&size=10&sort=&order=&psize=10&scheme_id=38`) if(response.data.code!=0){ console.error("new_search_id error:",response) throw response.data }else{ list = response.data.body.list } if(list.length<0){ throw response.data } let book_info = list[0] let info = {} // console.log("book_info:",book_info) info.words = book_info.word_count info.book_name = book_info.book_name info.book_id = book_info.book_id info.publish_time = book_info.create_time info.totalChapterNum = book_info.chapter_count info.default_pay_section = book_info.default_pay_section //默认付费章节 info.default_price = book_info.default_price // 加个 info.fee_unit = book_info.fee_unit //10 = 整本, 20 = 千字 info.author = book_info.author if(book_info.word_count<100000){ info.genre = 3; }else{ info.genre = 1; } return info; }catch(e){ return null; } } CMD.search_name_by_lib = async function(bookName) { let connection = null try{ //先搜索库里是否存在 connection = await mysql.createConnection({ ...mldbConfig, multipleStatements: true }); let sql = `SELECT * FROM zy_mf_lib WHERE product_name ="${bookName}" LIMIT 1` let [info] = await connection.execute(sql) if(info.length<=0){ throw 0 } return info[0] }catch(e){ console.log(e) return null }finally{ if(connection!=null){ connection.end() } } } CMD.new_search_name = async function(bookName) { try{ let lib_book_info = await CMD.search_name_by_lib(bookName) if(bookName!=null){ return {book_name:lib_book_info.product_name,book_id:lib_book_info.product_id} } let authorization = await redis_help.getKeyValue("zy_mf_token") console.log("authorization:",authorization) let clinet = tools.getOneNewClinet({ // "cookie":"acw_tc=1a0c639417498037411406820e006fb57d572ffdc9c83cd8a46aaf8d19ca00", "authorization": authorization, }) let list = null let response = await clinet.get(`https://gaia.zhangyue.com/gaia/v1/open_api/search/pay/book/channel/price/getBookNameList?application_uniq_id=shiyuestory_mini&name=${encodeURIComponent(bookName)}&page=1&size=10&scheme_id=38&business_model=free`) if(response.data.code!=0){ console.error("new_search_id error:",response) throw response.data }else{ list = response.data.body.items } if(list.length<0){ throw response.data } let book_info = list[0] let info = {} info.book_name = book_info.name info.book_id = book_info.id return info; }catch(e){ return null } } module.exports = CMD;