<?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\BestbaoCard;
use app\bestbao\model\BestbaoEngineer;
use app\bestbao\model\BestbaoInformation;

class Card extends Base{

     /**
     * 首页
     **/
    public function index(){
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $rel = BestbaoCard::where(['member_miniapp_id' => $this->miniapp_id])->find();
        if($rel){
            $map = bdMap_to_txMap($rel['latitude'],$rel['longitude']);
            $rel['content'] = str_replace('<img', '<img class="img"',dehtml($rel['content']));
            $rel['latitude']  = $map['lat'];
            $rel['longitude'] = $map['lng'];
            return enjson(200,'成功',$rel);
        }else{
            return enjson(204);
        }
    }

     /**
     * 公司动态
     **/
    public function news(){
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $rel = BestbaoInformation::where(['member_miniapp_id' => $this->miniapp_id])->limit(10)->select();
        if($rel){
            return enjson(200,'成功',$rel);
        }else{
            return enjson(204);
        }
    }

     /**
     * 公司动态
     **/
    public function reNews(){
        $param['id'] = $this->request->param('id/d',0);
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $rel = BestbaoInformation::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
        if($rel){
            $rel['content'] = str_replace('<img', '<img class="img"',dehtml($rel['content']));
            return enjson(200,'成功',$rel);
        }else{
            return enjson(204);
        }
    }
 

    /**
     * 获取工程师列表
     * @return void
     */
    public function engineer(){
        $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'],'签名验证失败');
        }
        $rel = BestbaoEngineer::with(['user' => function($query){
            $query->field('nickname,phone_uid,id,face');
        }])->where(['member_miniapp_id' => $this->miniapp_id])->order('sort desc,id desc')->paginate(10)->toArray();
        if(empty($rel['data'])){
            return enjson(204);
        }
        return enjson(200, '成功', $rel['data']);
    }

    /**
     * 不包含自己的的工程师列表
     * @return void
     */
    public function engineerNotMe(){
        $this->isUserAuth();
        $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'],'签名验证失败');
        }
        $rel = BestbaoEngineer::with(['user' => function($query){
            $query->field('nickname,phone_uid,id,face');
        }])->where([['member_miniapp_id','=',$this->miniapp_id],['uid','<>',$this->user->id]])->order('sort desc,id desc')->paginate(10)->toArray();
        if(empty($rel['data'])){
            return enjson(204);
        }
        return enjson(200, '成功', $rel['data']);
    }
}