ExpertController.php 15.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2019/1/6
 * Time: 14:45
 */

namespace app\user\controller;


use app\user\model\InquiryModel;
use app\user\model\TransferModel;
use app\user\model\UserModel;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\Image;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Db;
use think\Log;

class ExpertController extends WeChatBaseController
{

    public function inquiryAnswer(){
        $where=[];
        $userId=cmf_get_current_user_id();
        $model=new InquiryModel();
        $where['expert_id|user_id']=$userId;

        $count0=$model->where($where)->where('status',0)->count('id');
        $count1=$model->where($where)->where(['status'=>['neq',0]])->count('id');
        $this->assign('count0',$count0);
        $this->assign('count1',$count1);

        $list0=$model->where($where)->where('status',0)->select()->toArray();
        $list1=$model->where($where)->where(['status'=>['neq',0]])->select()->toArray();

        $this->assign('list0',$list0);
        $this->assign('list1',$list1);
        return $this->fetch();
    }

    public function transferAnswer(){
        $where=[];
        $userId=cmf_get_current_user_id();
        $model=new TransferModel();
        $where['expert_id|user_id']=$userId;

        $count0=$model->where($where)->where('status',0)->count('id');
        $count1=$model->where($where)->where(['status'=>['neq',0]])->count('id');
        $this->assign('count0',$count0);
        $this->assign('count1',$count1);

        $list0=$model->where($where)->where('status',0)->select()->toArray();
        $list1=$model->where($where)->where(['status'=>['neq',0]])->select()->toArray();

        $this->assign('list0',$list0);
        $this->assign('list1',$list1);

        return $this->fetch();
    }

    /**
     * 获取进修提交列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function engageAnswer(){
        $user_id=cmf_get_current_user_id();
        $map['user_id']=$user_id;
        $map['state']='0';
        /*未处理信息*/
        $undo=db('engage')->where($map)->select()->toArray();
        $count0=count($undo);
        $map['state']=['in','1,2'];
        $redo=db('engage')->where($map)->select()->toArray();
        $count1=count($redo);
        $this->assign('count0',$count0);
        $this->assign('count1',$count1);
        $this->assign('undo',$undo);
        $this->assign('redo',$redo);
        return $this->fetch();
    }

    /**
     * 提交信息细节
     */
    public function engageDetail(){
        $id=input('id');
        $info=db('engage')->where('id',$id)->find();
        if (empty($info)){
            $this->error('信息错误');
        }
        $User=new UserModel();
        $user=$User->getUser($info['user_id'])->toArray();
        $this->assign('user',$user['0']);
        $this->assign('info',$info);
        return $this->fetch();
    }


    public function inquiryDetail(){
        $config=config('wechat_config');
        $Wechat=new Application($config);
        $js = $Wechat->js;
        $api[]='startRecord';
        $api[]='stopRecord';
        $api[]='onVoiceRecordEnd';
        $api[]='playVoice';
        $api[]='pauseVoice';
        $api[]='stopVoice';
        $api[]='onVoicePlayEnd';
        $api[]='uploadVoice';
        $api[]='downloadVoice';
        $api[]='chooseImage';
        $api[]='previewImage';
        $api[]='uploadImage';
        $api[]='downloadImage';
        $api[]='translateVoice';
        $sdk=$js->config($api, false);
        $this->assign('sdk',$sdk);

        $id=$this->request->param('id',0,'intval');
        $model=new InquiryModel();
        $info=$model->getInfo($id);
        $info['report']=empty($info['report'])?[]:explode(',',$info['report']);
        $this->assign('info',$info);

        $user_type=Db::name('user')->where(['id'=>cmf_get_current_user_id()])->value('user_type');
        if($user_type==3){
            return $this->fetch();//专家
        }else{
            return $this->fetch('inquirydetail2');//用户
        }

    }

    public function replyInquiry(){
        $data=$this->request->param();
        $reply=$this->getSaveContent($data,'reply');

        $model=new InquiryModel();
        $info=$model->getInfo($data['id']);
        if($info['stage']==1){
            $update['stage']=2;
            $update['reply']=$reply;
        }elseif($info['stage']==3){
            $update['reply_again']=$reply;
            $update['stage']=4;
        }else{
            $this->success('您已经回复过了',url('inquiryAnswer'));
        }
        $update['id']=$data['id'];
        $update['status']=1;

        $model=new InquiryModel();
        $model->adminEdit($update);

        $this->success('提交成功',url('inquiryAnswer'));
    }

    /**
     * 再提问
     */
    public function problemInquiry(){
        $data=$this->request->param();
        $update['problem_again']=$this->getSaveContent($data,'problem_again');
        $update['id']=$data['id'];
        $update['status']=0;
        $update['stage']=3;

        $model=new InquiryModel();
        $model->adminEdit($update);

        $this->success('提交成功',url('inquiryAnswer'));
    }

    public function transferDetail(){
        $config=config('wechat_config');
        $Wechat=new Application($config);
        $js = $Wechat->js;
        $api[]='startRecord';
        $api[]='stopRecord';
        $api[]='onVoiceRecordEnd';
        $api[]='playVoice';
        $api[]='pauseVoice';
        $api[]='stopVoice';
        $api[]='onVoicePlayEnd';
        $api[]='uploadVoice';
        $api[]='downloadVoice';
        $api[]='chooseImage';
        $api[]='previewImage';
        $api[]='uploadImage';
        $api[]='downloadImage';
        $api[]='translateVoice';
        $sdk=$js->config($api, false);
        $this->assign('sdk',$sdk);

        $id=$this->request->param('id',0,'intval');
        $model=new TransferModel();
        $info=$model->getInfo($id);
        $this->assign('info',$info);

        $user_type=Db::name('user')->where(['id'=>cmf_get_current_user_id()])->value('user_type');

        if($user_type==3){
            return $this->fetch();//专家
        }else{
            return $this->fetch('transferdetail2');//用户
        }
    }

    /**
     * 转诊疑问
     */
    public function replyTransfer(){
        $data=$this->request->param();
        $update['reply']=$this->getSaveContent($data,'reply');
        $update['id']=$data['id'];
        $model=new InquiryModel();

        $info=$model->getInfo($data['id']);
        if($info['stage']==1){
            $update['stage']=2;
        }else{
            $this->success('您已经提交过了',url('inquiryAnswer'));
        }

        $model=new TransferModel();
        $model->adminEdit($update);
        $this->success('提交成功',url('transferAnswer'));
    }

    /**
     * 再提问
     */
    public function problemTransfer(){
        $data=$this->request->param();
        $update['problem_again']=$this->getSaveContent($data,'problem_again');
        $update['id']=$data['id'];
        $update['status']=0;
        $update['stage']=3;

        $model=new TransferModel();
        $model->adminEdit($update);

        $this->success('提交成功',url('transferAnswer'));
    }

    /**
     * 同意转诊
     */
    public function agreeTransfer(){
        $data=$this->request->param();
        $update['reply_again']=$this->getSaveContent($data,'reply_again');
        $update['id']=$data['id'];
        $update['receive_hospital']=$data['receive_hospital'];
        $update['receive_expert']=$data['receive_expert'];
        $update['receive_address']=$data['receive_address'];
        $update['receive_time']=strtotime($data['receive_time']);
        $update['status']=1;
        $update['stage']=4;
        $model=new TransferModel();
        $model->adminEdit($update);
        $info=$model->getInfo($data['id']);
        //todo 生成卡片
        $this->createTransferCard($info);
        $this->success('操作成功',url('transferAnswer'));
    }

    /**
     * 生成转诊电子卡
     * @param $info
     */
    protected function createTransferCard($info){
        $filename='transfer_'.$info['id'].'.png';
        $savePath='./upload/transfer/';
        if (!file_exists($savePath)){
            mkdir($savePath, 0777,true);
        }
        $savePathName=$savePath.$filename;

        $background='./static/images/transfer_card.png';
        $image = \think\Image::open($background);
        $font='./static/font-pingfang/PingFangMedium.ttf';

        $clinical='';
        if(!empty($info['clinical'])&&$info['clinical']['type']==2){
            $clinical=$info['clinical']['content'];
            $clinical=mb_substr($clinical,0,16,'utf-8');
        }
        $sex='';
        if($info['sex']==1){
            $sex='男';
        }elseif ($info['sex']==2){
            $sex='女';
        }
        $image->text($info['name'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[140,205])
            ->text($sex,$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[360,205])
            ->text($info['age'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[580,205])
            ->text($info['mobile'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[140,265])
            ->text($info['address'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[140,325])
            ->text($clinical,$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[190,385])
            ->text($info['hospital'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[190,540])
            ->text($info['user_nickname'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[190,600])
            ->text($info['mobile'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[350,660])
            ->text($info['receive_hospital'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[190,825])
            ->text($info['receive_expert'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[190,885])
            ->text($info['receive_address'],$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[190,945])
            ->text(date('Y-m-d',$info['receive_time']),$font,20,'#1A1A1A',\think\Image::WATER_NORTHWEST,[190,1005])
            ->save($savePathName);
        $openid=Db::name('third_party_user')->where(['user_id'=>$info['user_id']])->value('openid');
        $this->sendTransferCard($savePathName,$openid);

    }

    /**
     * 驳回转诊
     */
    public function refuseTransfer(){
        $id=$this->request->param('id');
        $model=new TransferModel();
        $update['stage']=4;
        $update['status']=2;
        $row=$model->where(['id'=>$id])->update($update);
        if($row){
            $this->success('驳回成功',url('transferAnswer'));
        }else{
            $this->error('驳回失败');
        }
    }





    /**
     * 内容处理
     * @param $input
     * @param $name
     * @return mixed
     */
    protected function getSaveContent($input,$name){

        $return['type']=$input[$name.'_type'];
        if ($return['type']==1){
            $return['content']=explode(',',$input[$name]);
            $dir=[];
            foreach ($return['content'] as $k=>$v) {
                $dir[$k]=$this->saveWxVoice($v);
            }
            $return['dir']=$dir;
        }else{
            $return['content']=$input[$name];
        }
        return $return;
    }

    /**
     * 获取微信语音到本地
     * @param $audioID
     */
    protected function saveWxVoice($mediaId){
        $config=config('wechat_config');
        $Wechat=new Application($config);
        $temporary = $Wechat->material_temporary;
        $dir=ROOT_PATH.'public/'.'upload/inquery'.'/';
        $name=date('YmdHis',time()).rand(1000,9999);
        $file=$name.'.amr';
        $data=$temporary->download($mediaId,"$dir", "$file");
        $filePath=$dir.$file;

        $result=$this->upChange($filePath,$name);
        if($result['err']==0){
            return $result['data'];
        }else{
            $this->error($result['msg']);
        }
    }

    /**
     * 录音文件amr转mp3
     * @param $filePath 文件绝对路径
     * @param $name 文件名不含后缀
     * @return mixed
     */
    protected function upChange($filePath,$name){

        //转码时使用的队列名称
        $pipeline = 'jyht_list';

        //要进行转码的转码操作
        $fops = "avthumb/mp3/ab/320k/ar/44100/acodec/libmp3lame";

        //可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定文件会默认命名并保存在当间
        $savekey = \Qiniu\base64_urlSafeEncode("jyht:$name.mp3");//目标Bucket_Name:自定义文件key
        $fops = $fops.'|saveas/'.$savekey;

        $policy = array(
            'persistentOps' => $fops,
            'persistentPipeline' => $pipeline
        );
        $key = "$name.mp3";
        $ret=$this->uploadToQiniu($filePath,$key,$policy);
        return $ret;
    }

    /**
     * @return mixed
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
     */
    public function upload_wx_pic_mul($mediaId)
    {
        $config=config('wechat_config');
        $Wechat=new Application($config);
        $temporary = $Wechat->material_temporary;
        $dir=ROOT_PATH.'public/'.'upload/friend'.'/';
        $name=date('YmdHis',time()).rand(1000,9999).'.jpg';
        $data=$temporary->download($mediaId,"$dir", "$name");
        $filePath=$dir.$name;
        $result=$this->uploadToQiniu($filePath,$name);
        if($result['err']===0){
            return $result['data'];
        }else{
            $this->error($result['msg']);
        }
        return $data;
    }

    /**
     * @param $filePath
     * @param $save_name
     * @param $policy
     * @return array
     */
    protected function uploadToQiniu($filePath,$save_name,$policy=null){
        $plugin=Db::name('plugin')->field('config')->where(['name'=>'Qiniu'])->find();
        $config=json_decode($plugin['config'],true);

        // 上传到七牛后保存的文件名
        $key =$save_name;

        require_once VENDOR_PATH . 'qiniu/php-sdk/autoload.php';

        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = $config['accessKey'];
        $secretKey = $config['secretKey'];
        // 构建鉴权对象
        $auth = new Auth($accessKey,$secretKey);
        // 要上传的空间
        $bucket = $config['bucket'];
        $token = $auth->uploadToken($bucket, null, 3600, $policy);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err"=>1,"msg"=>$err,"data"=>""];
        } else {
            //unlink($filePath);
            return ["err"=>0,"msg"=>"上传完成","data"=>cmf_get_asset_url($ret['key'])];
        }
    }

    protected function sendTransferCard($imagePath,$openId){
        $config=config('wechat_config');
        $app=new Application($config);
        $temporary = $app->material_temporary;
        $result = $temporary->uploadImage($imagePath);
        $mediaId=$result['media_id'];
        $text = new Image(['media_id' => $mediaId]);
        $result = $app->staff->message($text)->to($openId)->send();
        var_dump($result);
        Log::info($result);
    }


}