/**
 * @Author: 嗨噜客(三亚)<fm453>
 * @Date: 2023-07-19 16:03:07
 * @FilePath: apis/ctms/news.js
 * @Description: 
 * @Email:  393213759@qq.com
 * Copyright (c) 2025 by www.hiluker.cn, All Rights Reserved. 
 */
import request from './_utils/request.js'
import apis from './apis.json'
import constant from './_utils/constant.js'
import storage from './_utils/storage.js'
export default {
    //查询运单
    search: function(formData = null, page = 0, psize = 0, ls = null) {
        page = page || 1;
        psize = psize || 10;

        var data = {
            'search[title]': formData.title,
            'search[cid]': formData.cid,
            page: page,
            psize: psize, //分页数据大小
        };
        var url = apis.NewsSearch;
        var header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            "headers": header,
            'params': {}
        }).then((res) => {
            if (!res) {
                storage.set(constant.newsList, {});
                return false;
            }
            switch (res.code) {
                default:
                    uni.showToast({
                        title: "没有查询到相关内容!",
                        icon: "fail"
                    });
                    break;
                case 0:
                    var news = {};
                    var totalCount = 0;
                    storage.set(constant.newsList, news, 'page-' + page);
                    break;
                case 200:
                    var news = res.data.news;
                    var totalCount = res.data.total;
                    storage.set(constant.newsList, totalCount, 'total');
                    storage.set(constant.newsList, news, 'page-' + page);
                    for (var x in news) {
                        var detail = news[x];
                        var id = detail.id;
                        storage.set(constant.newsDetail, detail, id);
                    }
                    break;
            }
            return res.data;
        })
    },

    //列表,从本地缓存中加载
    list: function(page = 0) {
        //取缓存
        var total = storage.get(constant.newsList, 'total');
        if (total) {
            var news = storage.get(constant.newsList, 'page-' + page);
            if (news) {
                return {
                    'total': total,
                    'news': news
                }
            }
        }
        return false;
    },

    //详情,网络更新
    checkDetail: function(id) {
        var data = {
            id: id
        };
        var url = apis.NewsDetail;
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            'params': {}
        }).then((res) => {
            if (res.code == 200) {
                var detail = res.data;
                storage.set(constant.newsDetail, detail, id);
                return res;
            } else {
                uni.showToast({
                    title: "信息获取失败!",
                    icon: "fail"
                });
            }
        })
    },

    //详情，从缓存中读取
    detail: function(id) {
        return storage.get(constant.newsDetail, id);
    },

    //AJAX操作
    ajax: function(id, op) {
        var data = {
            id: id,
            op: op,
        };
        var url = apis.NewsAjax;
        return request({
            'url': url,
            'method': 'POST',
            'data': data,
            'params': {}
        }).then((res) => {
            if (res.code == 200) {
                var detail = res.data;
                storage.set(constant.newsDetail, detail, id);
                return res;
            }
        })
    },

    // 取文章分类
    cats: function() {
        var url = apis.NewsCatList;
        var header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
        return request({
            'url': url,
            'method': 'GET',
            'data': {},
            "headers": header,
            'params': {}
        }).then((res) => {
            if (res.code == 200) {
                var cats = res.data.cats;
                return cats;
            }
        })
    },

    //标记文章浏览记录
    viewed: function(id) {
        var ls = constant.newsViewed;
        var res = uni.getStorageSync(ls) || {};
        if (!res[id]) {
            res[id] = id;
            uni.setStorageSync(ls, res);
            this.ajax(id, 'view');
            return true;
        }
    },

    //标记文章状态（纯缓存记录）(id,内容ID，op为具体操作-默认为取全部,type为状态类型)
    mark: function(id, type = null, op = false) {
        //不归入统一的缓存集合，以确保不在清除缓存时被误清除
        if (!type) {
            return false;
        }
        var ls, opp_set, opp_del;
        switch (type) {
            case 'reading':
                //在读
                ls = constant.newsReading;
                opp_set = 'read';
                opp_del = 'no_read';
                break;
            case 'liked':
                //喜欢
                ls = constant.newsLiked;
                opp_set = 'like';
                opp_del = 'no_like';
                break;
        }
        var res = uni.getStorageSync(ls) || {};
        if (!op) {
            return res;
        }
        var data = res,
            ret = false;
        switch (op) {
            case 'set':
                if (id && !data[id]) {
                    data[id] = id;
                    uni.setStorageSync(ls, data);
                    ret = true;
                    this.ajax(id, opp_set);
                }
                break;
            case 'get':
                ret = data[id] || false;
                break;
            case 'del':
                delete data[id];
                uni.setStorageSync(ls, data);
                ret = true;
                this.ajax(id, opp_del);
                break;
            default:
                ret = res;
        }
        return ret;
    }
}