<?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\citys\controller\api\v1;
use app\citys\controller\api\Base;
use app\citys\model\Citys;
use app\citys\model\CitysFollow;
use app\citys\model\CitysMp;

class User extends Base{

    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
    }

    /**
     * 读取城市号信息
     */
    public function mp(){
        $this->apiSign();
        $rel = CitysMp::where(['uid' => $this->user->id])->find();
        if($rel){
            $rel->views     = Citys::where(['uid' => $rel->uid])->count();
            $rel->like      = CitysFollow::where(['uid' => $rel->uid])->count();
            $rel->vip_time  = empty($rel->vip_time)?'0':date('Y-m-d',$rel->vip_time);
            $rel->reg_time  = empty($rel->reg_time)?'0':date('Y-m-d',$rel->reg_time);
            $rel->is_follow = CitysFollow::where(['uid' => $this->user->id,'like_uid'=>$rel->uid])->count();
            return enjson(200,$rel);
        }
        return enjson(204);
    }

    /**
     * 读取城市号的信息
     * @param integer 读取ID
     * @return json
     */
    public function me(){
        $param['page']  = $this->request->param('page/d',1);
        $param['uid']   = $this->request->param('uid/d',0);
        $param['mid']   = $this->request->param('mid/d',0);
        $this->apiSign($param);
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $where['is_lock']           = 0;
        $data = Citys::apiLists($where,"is_top desc,id desc",$this->user);
        return enjson(empty($data)?204:200,$data);
    }
}
