<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 小程序公共API服务
 */
namespace app\bestbao\controller\api\v1;
use app\bestbao\controller\api\Base;
use app\bestbao\model\BestbaoProduct;

class Product extends Base{

     /**
     * 获取分类
     **/
    public function index(){
        $param['cate_id'] = $this->request->param('cate_id');
        $param['keyword'] = $this->request->param('keyword');
        $param['page']    = $this->request->param('page/d',1);
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $condition['member_miniapp_id'] = $this->miniapp_id;
        if($param['cate_id']){
            $condition['category_id']   = $param['cate_id'];
        }
        $rel = BestbaoProduct::where($condition)->withSearch(['title'],['title' => $param['keyword']])->order('sort desc,id desc')->paginate(10)->toArray();
        if($rel){
            return enjson(200,'成功',$rel['data']);
        }else{
            return enjson(204);
        }
    }

    /**
     * 获取某个产品
     */
    public function getView(){
        $this->isUserAuth();
        $data['product_id']   = $this->request->param('product_id/d',0);
        $data['product_code'] = $this->request->param('product_code');
        $data['sign']         = $this->request->param('sign');
        $rel = $this->apiSign($data);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $condition['member_miniapp_id'] = $this->miniapp_id;
        if(empty($data['product_id']) && empty($data['product_code'])){
            return enjson(204);
        }
        if(!empty($data['product_id'])){
            $condition['id'] = $data['product_id'];
        }
        if(!empty($data['product_code'])){
            $condition['code'] = $data['product_code'];
        }
        $rel = BestbaoProduct::where($condition)->field('id,code,images,note,title')->find();
        if($rel){
            return enjson(200,'成功',$rel);
        }else{
            return enjson(204);
        }
    }    
}