FriendsController.php 7.0 KB
<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2018/12/20
 * Time: 16:55
 */

namespace app\portal\controller;


use app\portal\model\GroupModel;
use app\user\model\UserModel;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Log;
use think\Request;

class FriendsController extends WeChatBaseController
{
    protected $GroupModel;

    public function __construct(GroupModel $GroupModel)
    {
        parent::__construct();
        $this->GroupModel = $GroupModel;
    }


    public function index(){
        $User=new UserModel();
        $user=$User->getUserInfo();
        if (empty($user['user_login'])){
            $targetUrl=url('portal/index/register');
            header('location:'. $targetUrl);
        }
        $group=$this->GroupModel->getMyGroup();
        $this->assign('group',$group);
        return    $this->fetch();
    }

    /**
     * 圈子详情页
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function friendList(){

        $config=config('wechat_config');
        $Wechat=new Application($config);
        $js = $Wechat->js;
        $api[]='chooseImage';
        $api[]='previewImage';
        $api[]='uploadImage';
        $api[]='downloadImage';
        $sdk=$js->config($api, false);


        $group_id=input('id');
        /*检查权限*/
        $user_id=cmf_get_current_user_id();
        $User=new UserModel();
        $user=$User->getMyself()->toArray();
        $isin=$this->GroupModel->checkUser($user_id,$group_id);
        if (empty($isin)){
            $this->error();
        }
        $silence=$this->GroupModel->silenceCheck($user_id,$group_id);

        $group=$this->GroupModel->getGroup($group_id);
        $info=$this->GroupModel->getFriendsInfo($group_id);
        $article=$this->GroupModel->getArticleList($group_id)->toArray();
        foreach ($article as $k=>$v){
            $article[$k]['pic']=cmf_get_image_url(json_decode($v['amore'],true)['thumbnail']);
        }
        $this->assign('sdk',$sdk);
        $this->assign('silence',$silence);
        $this->assign('info',$info);
        $this->assign('userinfo',$user);
        $this->assign('group',$group);
        $this->assign('article',$article);
        return $this->fetch();
    }

    public function addTalk(){
        $group_id=input('gid');
        $config=config('wechat_config');
        $Wechat=new Application($config);
        $js = $Wechat->js;
        $api[]='chooseImage';
        $api[]='previewImage';
        $api[]='uploadImage';
        $api[]='downloadImage';
        $sdk=$js->config($api, false);
        $this->assign(cmf_get_option('site_info'));
        $this->assign('sdk',$sdk);
        $this->assign('group_id',$group_id);
        return $this->fetch();
    }

    /**
     * 微信上传照片
     */
    public function upload_wx_pic_mul()
    {
        $config=config('wechat_config');
        $Wechat=new Application($config);
        $js = $Wechat->js;
        $access_token = $js->getAccessToken();
        $img_str = $this->request->param('media', '');
        $img_str = rtrim($img_str, ',');
        $img_arr = explode(',', $img_str);
        $temporary = $Wechat->material_temporary;
        $dir=ROOT_PATH.'public/'.'upload/friend'.'/';
        foreach ($img_arr as $k => $v) {
            $name=date('YmdHis',time()).rand(1000,9999).'.jpg';
             $data[$k]=$temporary->download($v, "$dir", "$name");
        }
        return $data;
    }

//    public function getUser(){
//        require_once VENDOR_PATH . "jssdk/jssdk.php";
//        $jssdk = new \JSSDK(Config('WX_APPID'), Config('WX_APP_SECRET'));
//        $access_token = $jssdk->getAccessToken();
//        $this->redirect('https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid=o1FU71IKa-M6q5vNF9sTd2tv-GEg&lang=zh_CN');
//    }

    /**
     * 根据media_id下载微信图片
     * @param $access_token
     * @param $media_id
     * @param $foldername
     * @return string
     */
    private function getmedia($access_token, $media_id, $foldername)
    {
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" . $access_token . "&media_id=" . $media_id;
        if (!file_exists("./upload/" . $foldername)) {
            mkdir("./upload/" . $foldername, 0777, true);
        }
        $file_name = date('YmdHis') . rand(1000, 9999) . '.jpg';
        $targetName = './upload/' . $foldername . '/' . $file_name;
        $saveName = $foldername . '/' . $file_name;
        $ch = curl_init($url); // 初始化
        $fp = fopen($targetName, 'wb'); // 打开写入
        curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置,值是一个资源类型
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
        return $saveName;
    }


    //发布文章提交
    public function addPost()
    {

//        Log::init([
//            'type'  =>  'File',
//            'path'  =>  APP_PATH.'logs/'
//        ]);
        $param = $this->request->param();
        if (empty($param['media'])&&empty($param['post_content'])){
            return 0;
        }
        if (!empty($param['media'])) {
            $param['more']['photos'] = [];
            foreach ($this->upload_wx_pic_mul($param['media']) as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($param['more']['photos'], $photoUrl);
                $insert['more'] = json_encode($param['more']);
                unset($param['picture']);
            }
        }
     /*   Log::write($param);
        log('11111');*/

        $insert['user_id'] = cmf_get_current_user_id();
        $insert['group_id'] = input('group_id');
        $insert['pic_num']=$param['num'];
        $insert['add_time'] = time();
        $insert['content'] = input('post_content');

        $re = db('group_text')->insert($insert);
        if ($re==1){
            $site=cmf_get_option('site_info');
            addScore($insert['user_id'],$site['friend'],'发布圈子动态');
        }
        return $re;
    }


    /**
     * 点击赞
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function postLike(){
        $id=input('text_id');
        $user_id=cmf_get_current_user_id();
        $result=$this->GroupModel->postLike($user_id,$id);
        if ($result==1){
            $this->success('');
        }else{
            $this->error();
        }
    }

    /**
     * 发布提交内容
     */
    public function postContent(){
        $id=input('id');
        $content=input('content');
        $user_id=cmf_get_current_user_id();
        $result=$this->GroupModel->postContent($user_id,$content,$id);
        if ($result==1){
            $this->success('');
        }else{
            $this->error();
        }
    }


}