<?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\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\allwin\model\AllwinAdwords;
use think\facade\Request;
use think\facade\Validate;

class Adwords extends Base{

    
    /**
     * 读取微信API
     * @param integer 读取ID
     * @return json
     */
    public function index(){
        $param['ids']  = $this->request->param('ids');
        $param['sign']  = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $group = explode('/',$param['ids']);
        $adword = [];
        foreach ($group as $value) {
            $adword[$value] = [];
        }
        $rel = AllwinAdwords::where(['member_miniapp_id' => $this->miniapp_id,'group' => $group])->field('title,picture,link,open_type,group')->order('sort desc,id desc')->select();
        foreach ($rel as $rs) {
            $adword[$rs['group']][] = $rs;
        }
        return enjson(200,'成功',$adword);
    }
    
    /**
     * 读取微信API
     * @param integer 读取ID
     * @return json
     */
    public function icons(){
        $api = $this->request->param('api/s');  //参数过滤
        $validate = Validate::alphaDash($api); 
        if(true !== $validate){
            return json(['code'=>404,'msg'=>'非法请求']);
        }
        $num   = $this->request->param('num/d',10);
        $limit = $this->request->param('limit/d',16);
        $where = [];
        $where['group']             = $api;
        $where['member_miniapp_id'] = $this->miniapp_id;
        $rel = AllwinAdwords::where($where)->cache(true)->field('title,picture,link,open_type')->limit($limit)->order('sort desc,id desc')->select()->toArray();
        if(empty($rel)){
            return enjson(204,['code'=>204,'msg'=>'空','data' => []]);
        }else{
            $nav = [];
            $i = -1;
            foreach($rel as $key => $value){
                if($key%$num == 0){
                    $i++;
                    $nav[$i][] = $value;
                }else{
                    $nav[$i][] = $value;
                }
            }
            return enjson(200,'成功',$nav);
        }
    }
}