Favorite.php 4.46 KB
<?php
/**
 * Public 收藏 
 * User: elkan
 * Date: 2014-8-19
 * Time: PM1:08
 */
class YHMApi_App_V1_Favorite extends YHMApi_App_V1_Base {

    /**
     * 获取收藏的商品
     * @param array $params
     * @param string $fields
     */
    public static function getGoodsByUid(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        $offset = !empty($params['offset']) ? (int) $params['offset'] : '0';
        $num = !empty($params['num']) ? (int) $params['num'] : "10";
        $data = YHMPassport_Models_Favorite_Goods_Client::getListByUid($params['uid'], $offset, $num);
        $goodlist = array();
        foreach ($data as $k => $v) {
            $goodlist[] = self::_getGoodsForList($v["product_skc"]);
        }
        return self::result(200, '收藏的商品', $goodlist);
    }

    /**
     * 获取收藏的卖家
     * @param array $params
     * @param string $fields
     */
    public static function getStoreByUid(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        $offset = !empty($params['offset']) ? (int) $params['offset'] : '0';
        $num = !empty($params['num']) ? (int) $params['num'] : "10";
        $data = YHMPassport_Models_Favorite_Store_Client::getListByUid($params['uid'], $offset, $num);
        $storeList = array();
        foreach ($data as $k => $v) {
            $storeList[] = self::_getStoreForList($v['store_id']);
        }
        return self::result(200, '获取收藏的店铺', $storeList);
    }

    /**
     * 收藏商品
     * @param array $params
     * @param type $fields
     * @return type
     */
    public static function addGoods(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        if (empty($params['product_skc'])) {
            return self::result(400, '商品skc不能为空');
        }
        $goods_info = YHMProduct_Models_Goods_Client::getOneByProductSkc($params['product_skc']);
        $id = YHMPassport_Models_Favorite_Goods_Client::add($params['uid'], $params['product_skc'], $goods_info['sale_price']);
        if ($id < 0) {
            return self::result(500,'收藏失败');
        }
        return self::result(200, "添加成功", $id);
    }

    /**
     * 取消收藏商品
     * @param array $params
     * @param type $fields
     * @return type
     */
    public static function cancelFavoriteGoods(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        if (empty($params['product_skc'])) {
            return self::result(400, '收藏id不能为空');
        }
        $status = YHMPassport_Models_Favorite_Goods_Client::del($params['uid'], $params['product_skc']);
        return self::result(200, "已取消收藏");
    }

    /**
     * 收藏店铺
     * @param array $params
     * @param type $fields
     * @return type
     */
    public static function addStore(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        if (empty($params['store_id'])) {
            return self::result(400, '店铺id不能为空');
        }
        $arr=YHMPassport_Models_Favorite_Store_Client:: findIsFavoriteByStoreId($params['uid'], $params['store_id']);
        if (isset($arr['id'])&&($arr['id']>0))
        {
           return self::result(200, "已收藏", $arr['id']); 
        }
        $id = YHMPassport_Models_Favorite_Store_Client::add($params['uid'], $params['store_id']);
        if ($id < 0) {
            return 0;
        }
        return self::result(200, "添加成功", $id);
    }

    /**
     * 取消收藏店铺
     * @param array $params
     * @param type $fields
     * @return type
     */
    public static function cancelFavoriteStore(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        if (empty($params['store_id'])) {
            return self::result(400, '收藏店铺id不能为空');
        }
        $status = YHMPassport_Models_Favorite_Store_Client::del($params['uid'], $params['store_id']);
        if (empty($status)) {
            return self::reuslt(400, "删除失败");
        }
        return self::result(200, "已取消收藏");
    }

}