main.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. const API = require("./components/API/API");
  2. const api = API();
  3. if (typeof $dora == 'undefined') {
  4. console.error('This project runs only in Dora.js.')
  5. console.error('Please visit https://dorajs.com/ for more information.')
  6. process.exit(-1)
  7. }
  8. console.info('Congratulation, your addon runs successfully!')
  9. module.exports = {
  10. async getCookie() {
  11. var userlist = $storage.get("userlist");
  12. var order = $storage.get("order");
  13. this.top = $storage.get("top") == undefined ? false && $storage.put("top", false) : $storage.get("top")
  14. if (order == null) {
  15. $storage.put("order", "mtime");
  16. this.order = "mtime";
  17. } else {
  18. this.order = order;
  19. }
  20. var go = true, cookie = "", mid = 0, csrf = '';
  21. if (userlist == null) {
  22. userlist = [];
  23. $storage.put("userlist", userlist);
  24. }
  25. if (userlist.length > 0) {
  26. userlist.forEach(f => {
  27. if (f.is_login) {
  28. go = false;
  29. cookie = f.cookie;
  30. csrf = f.csrf;
  31. mid = f.mid;
  32. }
  33. });
  34. if (userlist.length >= 0 && go) {
  35. userlist[0].is_login = true;
  36. cookie = userlist[0].cookie;
  37. csrf = userlist[0].csrf;
  38. mid = userlist[0].mid;
  39. $storage.put("userlist", userlist);
  40. }
  41. }
  42. this.cookie = cookie;
  43. this.mid = mid;
  44. this.csrf = csrf;
  45. },
  46. top: false,
  47. cookie: "",
  48. mid: 0,
  49. csrf: '',
  50. order: '',
  51. formateTimeStamp(time) {
  52. var date = new Date();
  53. date.setTime(time);
  54. var year = date.getFullYear();
  55. var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  56. var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  57. var hour = date.getHours()< 10 ? "0" + date.getHours() : date.getHours();
  58. var minute = date.getMinutes()< 10 ? "0" + date.getMinutes() : date.getMinutes();
  59. var second = date.getSeconds()< 10 ? "0" + date.getSeconds() : date.getSeconds();
  60. return year + "-" + month + "-" + day+" "+hour+":"+minute+":"+second;
  61. },
  62. /**
  63. * 最新发布、最多播放、最多收藏
  64. * @param {Number} author_mid 作者mid
  65. */
  66. async pcs(author_mid) {
  67. let selected = await $input.select({
  68. title: 'UP视频排列顺序',
  69. options: [
  70. {value: 'pubdate', title: '最新发布: pubdate'},
  71. {value: 'click', title: '最多播放: click'},
  72. {value: 'stow', title: '最多收藏: stow'}
  73. ]
  74. })
  75. if (selected != null) {
  76. $router.to($route('list/space_video', {
  77. mid: author_mid, order: selected.value
  78. }))
  79. }
  80. },
  81. /**
  82. * 点赞、投币、收藏视频、稍后再看
  83. * @param {Number} aid 视频 aid
  84. * @param {String} bvid 视频 bvid
  85. * @param {Boolean} deal 是否收藏
  86. */
  87. async lad(aid, bvid, deal) {
  88. let options = []
  89. options.push({value: 'player', title: '播放'})
  90. options.push({value: 'like', title: '点赞'})
  91. options.push({value: 'add', title: '投币'})
  92. options.push(deal ? {value: 'deal', title: '收藏视频'} : {value: 'deal', title: '取消收藏'})
  93. options.push({value: 'history', title: '添加稍后再看'})
  94. selected = await $input.select({
  95. title: '更多操作',
  96. options: options
  97. })
  98. if (selected != null) {
  99. if (selected.value == 'player') {
  100. $router.to($route('play', {aid: aid, bvid: bvid}))
  101. } else if (selected.value == 'like') {
  102. api.archive_like(cookie, csrf, aid, bvid, 1).then(res => {
  103. $ui.toast(res.data.code == 0 ? "点赞成功" : res.data.message)
  104. })
  105. } else if (selected.value == 'add') {
  106. let pd = false, go = true
  107. await api.archive_relation(cookie, aid, bvid).then(async res => {
  108. if (res.data.code == 0 && res.data.data.coin < 2) {
  109. pd = await $input.confirm({
  110. title: "是否投币",
  111. message: res.data.data.coin>0 ? `当前视频已投 ${res.data.data.coin} 币` : "当前视频还未投币",
  112. okBtn: '确定'
  113. })
  114. } else {
  115. go = false
  116. $ui.toast(`超过投币上限啦~`)
  117. }
  118. })
  119. if (pd) {
  120. api.coin_add(cookie, csrf, aid, bvid, 1, 0).then(res => {
  121. $ui.toast(res.data.code == 0 ? "投币成功" : res.data.message)
  122. })
  123. } else if (go) {
  124. $ui.toast("取消投币")
  125. }
  126. } else if (selected.value == 'deal') {
  127. if (deal) {
  128. let list = await api.folder_created_list_all(mid, cookie, aid).then(res => {
  129. return res.data.data == null ? false : res.data.data.list;
  130. })
  131. let selected = await $input.select({
  132. title: '选择收藏位置(*添加了的)',
  133. options: list.map(m => {
  134. if (m.fav_state == 1) {
  135. m.title = `${m.title} *`
  136. return m
  137. } else {
  138. return m
  139. }
  140. })
  141. })
  142. if (selected != null) {
  143. if (selected.fav_state == 1) {
  144. $ui.toast("已经添加过了")
  145. await lad(aid, bvid, author_mid, deal_id, deal)
  146. } else {
  147. api.resource_deal(cookie, csrf, aid, selected.id, deal).then(res => {
  148. $ui.toast(res.data.code == 0 ? "收藏成功" : res.data.message)
  149. })
  150. }
  151. } else {
  152. $ui.toast("取消收藏")
  153. }
  154. } else {
  155. let pd = await $input.confirm({
  156. title: "取消收藏",
  157. message: "是否取消收藏",
  158. okBtn: '确定'
  159. })
  160. if (pd) {
  161. api.resource_deal(cookie, csrf, aid, deal_id, deal).then(res => {
  162. $ui.toast(res.data.code == 0 ? "取消收藏成功" : res.data.message)
  163. })
  164. } else {
  165. $ui.toast("取消 取消收藏")
  166. }
  167. }
  168. } else if (selected.value == 'history') {
  169. let pd = true
  170. await api.history_toview(cookie).then(res => {
  171. res.data.data.list.forEach(f => {
  172. if (f.aid == aid || f.bvid == bvid) {
  173. pd = false
  174. }
  175. })
  176. })
  177. if (pd) {
  178. api.history_toview_add(cookie, csrf, aid, bvid).then(res => {
  179. $ui.toast(res.data.code == 0 ? "添加成功" : res.data.message)
  180. })
  181. } else {
  182. $ui.toast("稍后再看 早已存在~")
  183. }
  184. }
  185. }
  186. },
  187. /**
  188. * 最新发布、最多播放、最多收藏、点赞、投币、收藏视频、添加稍后再看
  189. * @param {Number} aid 视频 aid
  190. * @param {String} bvid 视频 bvid
  191. * @param {Number} author_mid 用户mid
  192. * @param {Number} deal_id 收藏夹id
  193. * @param {Boolean} deal 是否收藏
  194. * @param {Boolean} toview 是否稍后再看
  195. */
  196. async pcslad(aid, bvid, author_mid, deal_id, deal, toview) {
  197. let options = []
  198. options.push({value: 'pubdate', title: '最新发布: pubdate'})
  199. options.push({value: 'click', title: '最多播放: click'})
  200. options.push({value: 'stow', title: '最多收藏: stow'})
  201. options.push({value: 'player', title: '播放'})
  202. options.push({value: 'like', title: '点赞'})
  203. options.push({value: 'add', title: '投币'})
  204. options.push(deal ? {value: 'deal', title: '收藏视频'} : {value: 'deal', title: '取消收藏'})
  205. options.push(toview ? {value: 'toview', title: '添加稍后再看'} : {value: 'toview', title: '删除稍后再看'})
  206. selected = await $input.select({
  207. title: '更多操作',
  208. options: options
  209. })
  210. if (selected != null) {
  211. if (selected.value == 'player') {
  212. $router.to($route('play', {aid: aid, bvid: bvid}))
  213. } else if (selected.value == 'pubdate' || selected.value == 'click' || selected.value == 'stow') {
  214. $router.to($route('list/space_video', {
  215. mid: author_mid, order: selected.value
  216. }))
  217. } else if(selected.value == 'like') {
  218. api.archive_like(cookie, csrf, aid, bvid, 1).then(res => {
  219. $ui.toast(res.data.code == 0 ? "点赞成功" : res.data.message)
  220. })
  221. } else if(selected.value == 'add') {
  222. let pd = false, go = true
  223. await api.archive_relation(cookie, aid, bvid).then(async res => {
  224. if (res.data.code == 0 && res.data.data.coin < 2) {
  225. pd = await $input.confirm({
  226. title: "是否投币",
  227. message: res.data.data.coin>0 ? `当前视频已投 ${res.data.data.coin} 币` : "当前视频还未投币",
  228. okBtn: '确定'
  229. })
  230. } else {
  231. go = false
  232. $ui.toast(`超过投币上限啦~`)
  233. }
  234. })
  235. if (pd) {
  236. api.coin_add(cookie, csrf, aid, bvid, 1, 0).then(res => {
  237. $ui.toast(res.data.code == 0 ? "投币成功" : res.data.message)
  238. })
  239. } else if (go) {
  240. $ui.toast("取消投币")
  241. }
  242. } else if (selected.value == 'deal') {
  243. if (deal) {
  244. let list = await api.folder_created_list_all(mid, cookie, aid).then(res => {
  245. return res.data.data == null ? false : res.data.data.list;
  246. })
  247. let selected = await $input.select({
  248. title: '选择收藏位置(*添加了的)',
  249. options: list.map(m => {
  250. if (m.fav_state == 1) {
  251. m.title = `${m.title} *`
  252. return m
  253. } else {
  254. return m
  255. }
  256. })
  257. })
  258. if (selected != null) {
  259. if (selected.fav_state == 1) {
  260. $ui.toast("已经添加过了")
  261. await pcslad(aid, bvid, author_mid, deal_id, deal, true)
  262. } else {
  263. api.resource_deal(cookie, csrf, aid, selected.id, deal).then(res => {
  264. $ui.toast(res.data.code == 0 ? "收藏成功" : res.data.message)
  265. })
  266. }
  267. } else {
  268. $ui.toast("取消收藏")
  269. }
  270. } else {
  271. let pd = await $input.confirm({
  272. title: "取消收藏",
  273. message: "是否取消收藏",
  274. okBtn: '确定'
  275. })
  276. if (pd) {
  277. api.resource_deal(cookie, csrf, aid, deal_id, deal).then(res => {
  278. $ui.toast(res.data.code == 0 ? "取消收藏成功" : res.data.message)
  279. })
  280. } else {
  281. $ui.toast("取消 取消收藏")
  282. }
  283. }
  284. } else if (selected.value == 'toview') {
  285. if (toview) {
  286. let pd = true
  287. await api.history_toview(cookie).then(res => {
  288. res.data.data.list.forEach(f => {
  289. if (f.aid == aid || f.bvid == bvid) {
  290. pd = false
  291. }
  292. })
  293. })
  294. if (pd) {
  295. api.history_toview_add(cookie, csrf, aid, bvid).then(res => {
  296. $ui.toast(res.data.code == 0 ? "添加成功" : res.data.message)
  297. })
  298. } else {
  299. $ui.toast("稍后再看 早已存在~")
  300. }
  301. } else {
  302. api.history_toview_del(cookie, this.csrf, aid, bvid).then(res => {
  303. $ui.toast(res.data.code == 0 ? "删除成功" : res.data.message)
  304. })
  305. }
  306. }
  307. }
  308. }
  309. }