1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 20-9-23
- * Time: 上午11:26
- */
- namespace App\Http\Controllers\Api\V2;
- use App\Http\Controllers\Controller;
- use App\Models\Article;
- use App\Models\Order;
- use App\Models\User;
- use App\Models\Docter;
- use EasyWeChat\Factory;
- use http\Env\Request;
- use Illuminate\Support\Facades\DB;
- use AlibabaCloud\Client\Exception\ClientException;
- use Alibabaloud\Client\Exception\ServerException;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Dybaseapi\MNS\Requests\BatchReceiveMessage;
- use AlibabaCloud\Dybaseapi\MNS\Requests\BatchDeleteMessage;
- use App\Models\CallLog;
- use App\Models\Axb;
- use App\Models\ImMessage;
- use Cache;
- /**
- * 公共方法类
- * Class CommonController
- * @package App\Http\Controllers\Api\V2
- */
- class CommonController extends Controller
- {
- public function wxLogin()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'wechat_code' => 'required',
- 'nickname|昵称' => 'max:50',
- 'phone|手机号' => 'max:20',
- 'avatar|头像' => 'url',
- 'latitude|纬度' => 'numeric',
- 'longitude|纬度' => 'numeric',
- ]);
- $app = Factory::miniProgram(config('config.docter_wechat_small_program'));
- $data = $app->auth->session($req['wechat_code']);
- if (empty($data['openid'])) {
- return out(null, 10001, '微信登录code错误');
- }
- $session_key = !empty($data['session_key']) ? $data['session_key'] : '';
- $user = Docter::select(['id', 'status', 'phone', 'name', 'avatar'])->where('openid', $data['openid'])->first();
- if (empty($user)) {
- $docter_list = [
- 'type' => 1,
- 'openid' => $data['openid'],
- 'session_key' => $session_key,
- 'phone' => $req['phone'],
- 'sex' => 0,
- 'birthday' => 0,
- 'status' => 1,
- 'label' => '无',
- 'sign' => '无',
- 'intro' => 0,
- 'office_id' => 0,
- 'qualification_id' => 0,
- 'score' => 0,
- 'service_persons' => 0,
- 'eva_num' => 0,
- 'service_days' => 0,
- 'phone_minutes' => 0,
- 'chat_price' => 0,
- 'phone_price' => 0,
- 'appoint_price' => 0,
- 'is_chat' => 0,
- 'is_phone' => 0,
- 'is_appoint' => 0,
- 'latitude' => $req['latitude'] ?? 0,
- 'longitude' => $req['longitude'] ?? 0,
- 'is_then' => 0,
- 'practice' => 0,
- 'card_photo' => 0,
- 'is_quail' => 0,
- 'card_id' => 0,
- ];
- $docter_list['name'] = $req['nickname'] ?? '';
- $docter_list['avatar'] = $req['avatar'] ?? request()->getScheme() . '://' . request()->getHost() . '/img/default-head.png';
- $user = Docter::create($docter_list);
- if (empty($docter_list['name'])) {
- $nickname = '用户' . base_convert($user['id'], 10, 16);
- Docter::where('id', $user['id'])->update(['name' => $nickname]);
- }
- } else {
- if ($user['status'] == 0) {
- return out(null, 10002, '该账号已被冻结');
- }
- Docter::where('id', $user['id'])->update([
- 'name' => $req['nickname'] ?? '',
- 'avatar' => $req['avatar'] ?? '',
- 'latitude' => $req['latitude'] ?? 0,
- 'longitude' => $req['longitude'] ?? 0,
- 'phone' => $req['phone'] ?? 0,
- 'session_key' => $session_key
- ]);
- }
- $token = aes_encrypt(['doctor_id' => $user['id'], 'time' => time()]);
- $datas = [
- 'avatar' => $user['avatar'],
- 'name' => $user['name'],
- 'flag' => 'doctor_' . $user['id'],
- ];
- return out(['token' => $token, 'data' => $datas]);
- }
- /**
- * 获取手机号!
- * Auth:Yuanhang-Liu
- * Date:2020/10/18 17:17 *
- * @return \Illuminate\Http\JsonResponse
- */
- public function getPhoneNumber()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'wechat_code' => 'required',
- 'iv' => 'required',
- 'encryptedData' => 'required',
- ]);
- $app = Factory::miniProgram(config('config.docter_wechat_small_program'));
- $data = $app->auth->session($req['wechat_code']);
- if (empty($data['openid']) || empty($data['session_key'])) {
- return out(null, 10001, '微信code错误');
- }
- $session_key = $data['session_key'];
- $decryptedData = $app->encryptor->decryptData($session_key, $req['iv'], $req['encryptedData']);
- if (!isset($decryptedData['phoneNumber']) || empty($decryptedData['phoneNumber'])) {
- return out(['status' => false, 'msg' => '手机号解密失败!']);
- }
- $docter_list = [
- 'type' => 1,
- 'openid' => $data['openid'],
- 'session_key' => $session_key,
- 'name' => '',
- 'phone' => $decryptedData['phoneNumber'],
- 'sex' => 0,
- 'birthday' => 0,
- 'avatar' => request()->getScheme() . '://' . request()->getHost() . '/img/default-head.png',
- 'status' => 1,
- 'label' => '无',
- 'sign' => '无',
- 'intro' => 0,
- 'office_id' => 0,
- 'qualification_id' => 0,
- 'score' => 0,
- 'service_persons' => 0,
- 'eva_num' => 0,
- 'service_days' => 0,
- 'phone_minutes' => 0,
- 'chat_price' => 0,
- 'phone_price' => 0,
- 'appoint_price' => 0,
- 'is_chat' => 0,
- 'is_phone' => 0,
- 'is_appoint' => 0,
- 'latitude' => 0,
- 'longitude' => 0,
- 'is_then' => 0,
- 'practice' => 0,
- 'card_photo' => 0,
- 'is_quail' => 0,
- 'card_id' => 0,
- ];
- // 查询医生表有没有记录
- $find = Docter::where('phone', $decryptedData['phoneNumber'])->first();
- if (!$find) {
- $user = Docter::create($docter_list);
- $nickname = '医生' . base_convert($user['id'], 10, 16);
- Docter::where('id', $user['id'])->update(['name' => $nickname]);
- }else{
- Docter::where('id', $find['id'])->update(['openid' => $data['openid'],'session_key'=>$session_key]);
- }
- return out($decryptedData);
- }
- /**
- * 手机号登陆
- * Auth:Yuanhang-Liu
- * Date:2020/10/18 19:17 *
- * @return \Illuminate\Http\JsonResponse
- */
- public function phoneLogin(){
- $req = request()->post();
- $this->validate(request(), [
- 'phone|手机号' => 'required|integer',
- 'verify|验证码' => 'required|integer',
- ]);
- $verify = (int)$req['verify'];
- $verifyCode = Cache::get($req['phone'].'-', $verify, config('config.aly_sms.sms_verify_code_expire'));
- if ($verifyCode!=$verify){
- return out('',401,'验证码错误!');
- }
- $find = Docter::where('phone','=',$req['phone'])->first();
- if (empty($find)){
- $docter_list = [
- 'type' => 1,
- 'name' => '医生' . base_convert($find['id'], 10, 16),
- 'phone' => $req['phone'],
- 'sex' => 0,
- 'birthday' => 0,
- 'avatar' => '../../static/login/moren.png',
- 'status' => 1,
- 'label' => '无',
- 'sign' => '无',
- 'intro' => 0,
- 'office_id' => 0,
- 'qualification_id' => 0,
- 'score' => 0,
- 'service_persons' => 0,
- 'eva_num' => 0,
- 'service_days' => 0,
- 'phone_minutes' => 0,
- 'chat_price' => 0,
- 'phone_price' => 0,
- 'appoint_price' => 0,
- 'is_chat' => 0,
- 'is_phone' => 0,
- 'is_appoint' => 0,
- 'latitude' => 0,
- 'longitude' => 0,
- 'is_then' => 0,
- 'practice' => 0,
- 'card_photo' => 0,
- 'is_quail' => 0,
- 'card_id' => 0,
- ];
- $list = Docter::create($docter_list)->toArray();
- if (!empty($list)){
- $datas = [
- 'avatar' => $list['avatar'],
- 'name' => $list['name'],
- 'flag' => 'doctor_'.$list['id'],
- ];
- $token = aes_encrypt(['doctor_id' => $list['id'], 'time' => time()]);
- return out(['token' => $token,'data'=>$datas]);
- }else{
- return out('',401,'用户不存在,注册失败!');
- }
- }
- $find = $find->toArray();
- if ($find['status'] == 0) {
- return out(null, 10002, '该账号已被冻结');
- }
- $datas = [
- 'avatar' => $find['avatar'],
- 'name' => $find['name'],
- 'flag' => 'doctor_'.$find['id'],
- ];
- // 验证是否正确
- $token = aes_encrypt(['doctor_id' => $find['id'], 'time' => time()]);
- Cache::delete($req['phone'] . '-', $verify);
- return out(['token' => $token,'data'=>$datas]);
- }
- /**
- * 账号密码登陆
- * Auth:Yuanhang-Liu
- * Date:2020/10/18 19:17 *
- * @return \Illuminate\Http\JsonResponse
- */
- public function passLogin(){
- $req = request()->post();
- $this->validate(request(), [
- 'phone|手机号' => 'required|integer',
- 'password|密码' => 'required',
- ]);
- $find = Docter::where('phone','=',$req['phone'])->first();
- if (empty($find)){
- return out(null, 401, '账号不存在');
- }
- if ($find['status'] == 0) {
- return out(null, 10002, '该账号已被冻结');
- }
- $find = $find->toArray();
- // 验证密码
- $password = md5(md5(md5($req['password'])));
- if ($password==$find['password']){
- $datas = [
- 'avatar' => $find['avatar'],
- 'name' => $find['name'],
- 'flag' => 'doctor_'.$find['id'],
- ];
- $token = aes_encrypt(['doctor_id' => $find['id'], 'time' => time()]);
- return out(['token' => $token,'data'=>$datas]);
- }else{
- return out(null, 401, '密码错误');
- }
- }
- /**
- * 获取验证码
- * @return \Illuminate\Http\JsonResponse
- * @author Liu-Yh
- * Create By 2020/11/6 10:45
- */
- public function putverfiy(){
- //防止恶意刷验证码接口,一分钟最多10次
- check_repeat_request(60, 10);
- $req = request()->post();
- $this->validate(request(), [
- 'phone|手机号' => 'required|integer',
- ]);
- $mobile =$req['phone']; //获取传入的手机号
- $verify_code = generate_code();
- $result = send_sms($mobile,'verify_template_code',['code'=>$verify_code]);
- if (empty($result['Code']) || $result['Code'] != 'OK'){
- return out(null, 30010, '验证码发送失败,请稍后重试');
- }
- Cache::set($req['phone'].'-', $verify_code, config('config.aly_sms.sms_verify_code_expire'));
- return out();
- }
- /**
- * 手机号注册
- * Auth:Yuanhang-Liu
- * Date:2020/10/18 20:17 *
- * @return \Illuminate\Http\JsonResponse
- */
- public function phoneRegister(){
- $req = request()->post();
- $this->validate(request(), [
- 'phone|手机号' => 'required|integer',
- 'password|密码' => 'required',
- 'verify|验证码' => 'required|integer',
- ]);
- $verify = (int)$req['verify'];
- $verifyCode = Cache::get($req['phone'].'-', $verify, config('config.aly_sms.sms_verify_code_expire'));
- if ($verifyCode!=$verify){
- return out('',401,'验证码错误!');
- }
- // 查询是否注册过!
- $docters = Docter::where('phone','=',$req['phone'])->first();
- if (!empty($docters)){
- return out('',500,'此手机号已被注册!');
- }
- $password = md5(md5(md5($req['password'])));
- $docter_list = [
- 'type' => 1,
- 'name' => '用户名',
- 'phone' => $req['phone'],
- 'sex' => 0,
- 'birthday' => 0,
- 'avatar' => '无',
- 'status' => 1,
- 'label' => '无',
- 'sign' => '',
- 'intro' => '',
- 'office_id' => 0,
- 'qualification_id' => 0,
- 'score' => 0,
- 'service_persons' => 0,
- 'eva_num' => 0,
- 'service_days' => 0,
- 'phone_minutes' => 0,
- 'chat_price' => 0,
- 'phone_price' => 0,
- 'appoint_price' => 0,
- 'is_chat' => 0,
- 'is_phone' => 0,
- 'is_appoint' => 0,
- 'latitude' => 0,
- 'longitude' => 0,
- 'password' => $password,
- 'is_then' => 0,
- 'practice' => 0,
- 'card_photo' => 0,
- 'is_quail' => 0,
- 'card_id' => 0,
- ];
- $list = Docter::create($docter_list)->toArray();
- if (!empty($list)){
- $datas = [
- 'avatar' => $list['avatar'],
- 'name' => $list['name'],
- 'flag' => 'doctor_'.$list['id'],
- ];
- $token = aes_encrypt(['doctor_id' => $list['id'], 'time' => time()]);
- return out(['token' => $token,'data'=>$datas]);
- }else{
- return out('',500,'注册失败!');
- }
- // return out();
- }
- public function articleList()
- {
- $data = Article::orderBy('id', 'desc')->paginate();
- return out($data);
- }
- public function getUserIdByDoctorId($phone=null){
- $list = Docter::where('phone', $phone)->first();
- if ($list){
- return $list->id;
- }else{
- return false;
- }
- }
- public function uploadFile()
- {
- $file = request()->file('file');
- if (empty($file)) {
- return out(null, 10001, '文件不能为空');
- }
- $path = $file->store('upload/docter/'.date('Ymd'));
- // $url = request()->getScheme().'://'.request()->getHost().'/'.$path;
- return out(['url' => $path]);
- }
- public function doc()
- {
- $database = env('DB_DATABASE');
- $prefix = env('DB_PREFIX');
- $exclude_tables = "'bm_password_resets','bm_admin_menu','bm_admin_users','bm_failed_jobs','bm_migrations'";
- $sql = "select TABLE_NAME name,TABLE_COMMENT comment from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='".$database."' and TABLE_NAME not in (".$exclude_tables.")";
- $tables = \DB::select($sql);
- $map1 = $map2 = [];
- $i = round(count($tables)/2);
- foreach ($tables as $k => $v) {
- $name = str_replace($prefix, '', $v->name);
- if ($k >= $i) {
- $map1[$v->name] = $name.'('.$v->comment.')';
- }
- else {
- $map2[$v->name] = $name.'('.$v->comment.')';
- }
- }
- $data1 = [];
- foreach ($map1 as $k => $v){
- $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
- $comment = \DB::select($sql);
- $data1[$v] = $comment;
- }
- $data2 = [];
- foreach ($map2 as $k => $v){
- $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
- $comment = \DB::select($sql);
- $data2[$v] = $comment;
- }
- return view('doc', ['data1' => $data1, 'data2' => $data2]);
- }
- /**
- * 绑定号码池子
- * @param string $phone1 医生id
- * @param string $phone2 用户id
- * @param array $data 参数
- * @return mixed
- */
- public function BindAxb($phone1,$phone2,$data=[]){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dyplsapi')
- ->version('2017-05-25')
- ->action('BindAxb')
- ->method('POST')
- ->host('dyplsapi.aliyuncs.com')
- ->options([
- 'query' => [
- "Expiration" => date("Y-m-d H:i:s",strtotime("+1 day")),
- 'RegionId' => "cn-kunming",
- 'PhoneNoA' => $phone1,
- 'PhoneNoB' => $phone2,
- // 修改
- 'IsRecordingEnabled' => true
- ],
- ])
- ->request();
- return $result->toArray();
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- /**
- * 释放虚拟号码
- * @param int $phone
- */
- public function ReleaseSecretNo($phone){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dyplsapi')
- // ->scheme('https') // https | http
- ->version('2017-05-25')
- ->action('ReleaseSecretNo')
- ->method('POST')
- ->host('dyplsapi.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-kunming",
- "SecretNo" => $phone,
- "PoolKey" => $config['PoolKey'],
- ],
- ])
- ->request();
- $res = $result->toArray();
- return $res;
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- /**
- * 查询号码关系
- * @param $phone
- * @return mixed
- */
- public function QuerySubscriptionDetail($phone,$SubsId){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dyplsapi')
- // ->scheme('https') // https | http
- ->version('2017-05-25')
- ->action('QuerySubscriptionDetail')
- ->method('POST')
- ->host('dyplsapi.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-hangzhou",
- "PoolKey" => $config['PoolKey'],
- "SubsId" => $SubsId,
- "PhoneNoX" => $phone,
- ],
- ])
- ->request();
- $res = $result->toArray();
- return $res;
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- /**
- * 解除绑定关系
- * @param $phone
- * @return mixed
- */
- public function UnbindSubscription($phone,$subId){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dyplsapi')
- // ->scheme('https') // https | http
- ->version('2017-05-25')
- ->action('UnbindSubscription')
- ->method('POST')
- ->host('dyplsapi.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-kunming",
- 'SubsId' => $subId,
- "SecretNo" => $phone,
- "PoolKey" => $config['PoolKey'],
- ],
- ])
- ->request();
- $res = $result->toArray();
- return $res;
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- /**
- * 调用接口QuerySubsId查询绑定唯一标识SubsId
- * @param $phone X号码
- * @return mixed
- */
- public function QuerySubsId($phone){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dyplsapi')
- // ->scheme('https') // https | http
- ->version('2017-05-25')
- ->action('QuerySubsId')
- ->method('POST')
- ->host('dyplsapi.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-hangzhou",
- 'Action' => "QuerySubsId",
- "PhoneNoX" => $phone,
- "PoolKey" => $config['PoolKey'],
- ],
- ])
- ->request();
- $res = $result->toArray();
- return $res;
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- /**
- * 解锁号码
- * @return \Illuminate\Http\JsonResponse
- * @author Liu-Yh
- * Create By 2020/11/25 18:36
- */
- public function unLokPhone($phone,$SubsId){
- $unlok = $this->UnbindSubscription($phone,$SubsId);
- if ($unlok['Code']!='OK'){
- return out($unlok);
- }else{
- return 1;
- }
- }
- /**
- * 测试解除绑定电话的方法
- */
- public function testunlockphone(){
- $phone = 17052201940;
- $sub_id = 1000027059330181;
- var_dump($this->unLokPhone($phone,$sub_id));
- }
- /**
- * 调用接口QueryCallStatus查询呼叫状态。
- * @param $phone
- * @author Liu-Yh
- * Create By 2020/11/26 10:51
- */
- public function QueryCallStatus($phone,$SubsId){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dyplsapi')
- // ->scheme('https') // https | http
- ->version('2017-05-25')
- ->action('QueryCallStatus')
- ->method('POST')
- ->host('dyplsapi.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-hangzhou",
- "PoolKey" => $config['PoolKey'],
- "SubsId" => $SubsId,
- ],
- ])
- ->request();
- $res = $result->toArray();
- return $res;
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- /**
- * 接收通话发起时的通话记录报告内容,可以在呼叫发起时立即获取到通话记录信息,包括通话开始时间、主被叫号码等,便于平台进行预判处理
- * @author Liu-Yh
- * Create By 2020/11/25 14:44
- */
- public function StartSecretReport(){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- $queueName = $config['StartReport']; // 队列名称
- $messageType = "SecretStartReport"; // 需要接收的消息类型
- $response = null;
- $token = null;
- $i = 0;
- do {
- try {
- if (null == $token || strtotime($token['ExpireTime']) - time() > 2 * 60) {
- $response = AlibabaCloud::rpcRequest()
- ->product('Dybaseapi')
- ->version('2017-05-25')
- ->action('QueryTokenForMnsQueue')
- ->method('POST')
- ->host("dybaseapi.aliyuncs.com")
- ->options([
- 'query' => [
- 'MessageType' => $messageType,
- 'QueueName' => $queueName,
- ],
- ])
- ->request()
- ->toArray();
- }
- $token = $response['MessageTokenDTO'];
- $mnsClient = new \AlibabaCloud\Dybaseapi\MNS\MnsClient(
- "http://1943695596114318.mns.cn-hangzhou.aliyuncs.com",
- $token['AccessKeyId'],
- $token['AccessKeySecret'],
- $token['SecurityToken']
- );
- $mnsRequest = new BatchReceiveMessage(10, 5);
- $mnsRequest->setQueueName($queueName);
- $mnsResponse = $mnsClient->sendRequest($mnsRequest);
- $receiptHandles = Array();
- foreach ($mnsResponse->Message as $message) {
- // 用户逻辑:
- // 入库
- // var_dump(base64_decode($message->MessageBody));
- // var_dump(json_decode(base64_decode($message->MessageBody),true));
- // $receiptHandles[] = $message->ReceiptHandle; // 加入$receiptHandles数组中的记录将会被删除
- $messageBody = json_decode(base64_decode($message->MessageBody),true); // base64解码后的JSON字符串
- echo "进来了";
- var_dump($messageBody);
- }
- if (count($receiptHandles) > 0) {
- $deleteRequest = new BatchDeleteMessage($queueName, $receiptHandles);
- $mnsClient->sendRequest($deleteRequest);
- }
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- if ($e->getCode() == 404) {
- $i++;
- }
- echo $e->getErrorMessage() . PHP_EOL;
- }
- } while ($i < 3);
- }
- /**
- * 接收通话结束时的通话记录报告内容,可以在呼叫结束后获取通话记录信息,包括通话开始时间、通话结束时间、主被叫号码等,
- * @author Liu-Yh
- * Create By 2020/11/25 12:29
- */
- public function SecretPullReport(){
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- $queueName = $config['Report']; // 队列名称
- $messageType = "SecretReport"; // 需要接收的消息类型
- $response = null;
- $token = null;
- $i = 0;
- do {
- try {
- if (null == $token || strtotime($token['ExpireTime']) - time() > 2 * 60) {
- $response = AlibabaCloud::rpcRequest()
- ->product('Dybaseapi')
- ->version('2017-05-25')
- ->action('QueryTokenForMnsQueue')
- ->method('POST')
- ->host("dybaseapi.aliyuncs.com")
- ->options([
- 'query' => [
- 'MessageType' => $messageType,
- 'QueueName' => $queueName,
- ],
- ])
- ->request()
- ->toArray();
- }
- $token = $response['MessageTokenDTO'];
- $mnsClient = new \AlibabaCloud\Dybaseapi\MNS\MnsClient(
- "http://1943695596114318.mns.cn-hangzhou.aliyuncs.com",
- $token['AccessKeyId'],
- $token['AccessKeySecret'],
- $token['SecurityToken']
- );
- $mnsRequest = new BatchReceiveMessage(10, 5);
- $mnsRequest->setQueueName($queueName);
- $mnsResponse = $mnsClient->sendRequest($mnsRequest);
- $receiptHandles = Array();
- $getList = CallLog::get();
- if ($getList){
- $getList = $getList->toArray();
- }
- $msgs = $mnsResponse->Message;
- if (!is_array($msgs)){
- $messageBody = json_decode(base64_decode($msgs->MessageBody),true); // base64解码后的JSON字符
- if (count($getList)==0){
- CallLog::create([
- 'call_time'=>$messageBody['call_time'],
- 'ring_time'=>$messageBody['release_time'],
- 'release_dir'=>$messageBody['release_dir'],
- 'call_type'=>$messageBody['call_type'],
- 'aphone'=>$messageBody['phone_no'],
- 'bphone'=>$messageBody['peer_no'],
- 'call_id'=>$messageBody['call_id'],
- 'secret_no'=>$messageBody['secret_no'],
- 'sub_id'=>$messageBody['sub_id'],
- 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
- ]);
- }else{
- foreach ($getList as $k=>$v){
- if ($v['call_id']==$messageBody['call_id']){
- unset($messageBody);
- }else{
- CallLog::create([
- 'call_time'=>$messageBody['call_time'],
- 'ring_time'=>$messageBody['release_time'],
- 'release_dir'=>$messageBody['release_dir'],
- 'call_type'=>$messageBody['call_type'],
- 'aphone'=>$messageBody['phone_no'],
- 'bphone'=>$messageBody['peer_no'],
- 'call_id'=>$messageBody['call_id'],
- 'secret_no'=>$messageBody['secret_no'],
- 'sub_id'=>$messageBody['sub_id'],
- 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
- ]);
- }
- }
- }
- }else{
- foreach ($msgs as $message) {
- // 用户逻辑:
- $messageBody = json_decode(base64_decode($message->MessageBody),true); // base64解码后的JSON字符串
- if (count($getList)==0) {
- CallLog::create([
- 'call_time'=>$messageBody['call_time'],
- 'ring_time'=>$messageBody['release_time'],
- 'release_dir'=>$messageBody['release_dir'],
- 'call_type'=>$messageBody['call_type'],
- 'aphone'=>$messageBody['phone_no'],
- 'bphone'=>$messageBody['peer_no'],
- 'call_id'=>$messageBody['call_id'],
- 'secret_no'=>$messageBody['secret_no'],
- 'sub_id'=>$messageBody['sub_id'],
- 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
- ]);
- }else{
- foreach ($getList as $k=>$v){
- if ($v['call_id']==$messageBody['call_id']){
- unset($messageBody);
- }else{
- CallLog::create([
- 'call_time'=>$messageBody['call_time'],
- 'ring_time'=>$messageBody['release_time'],
- 'release_dir'=>$messageBody['release_dir'],
- 'call_type'=>$messageBody['call_type'],
- 'aphone'=>$messageBody['phone_no'],
- 'bphone'=>$messageBody['peer_no'],
- 'call_id'=>$messageBody['call_id'],
- 'secret_no'=>$messageBody['secret_no'],
- 'sub_id'=>$messageBody['sub_id'],
- 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
- ]);
- }
- }
- }
- $receiptHandles[] = $message->ReceiptHandle; // 加入$receiptHandles数组中的记录将会被删除
- }
- }
- if (count($receiptHandles) > 0) {
- $deleteRequest = new BatchDeleteMessage($queueName, $receiptHandles);
- $mnsClient->sendRequest($deleteRequest);
- }
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- if ($e->getCode() == 404) {
- $i++;
- }
- echo $e->getErrorMessage() . PHP_EOL;
- }
- } while ($i < 3);
- }
- /**
- * 通话开始时候回调数据
- * @return false|string
- * 返回参数
- * [{
- "phone_no": "18831138292",
- "pool_key": "FC100000115024469", 对应的号池Key。
- "city": "昆明",
- "sub_id": 1000027052283144, 通话对应的三元组的绑定关系ID。
- "unconnected_cause": 0,
- "call_time": "2020-12-21 17:23:56", 主叫拨打时间。
- "peer_no": "15222021008", AXB中的B号码或者N号码。
- "called_display_no": "17052201941", 被叫显号X号码
- "call_id": "31343639616333323735", 唯一标识一通通话记录的ID。
- "partner_key": "FC100000115024469",
- "control_msg": "OK",
- "id": 1007221453890,
- "secret_no": "17052201941",
- "call_type": 0,0:主叫(phone_no打给peer_no);1:被叫(peer_no打给phone_no);2:短信发送;3:短信接收;4:呼叫拦截;5:短信收发拦截;
- "control_type": "CONTINUE"
- }]
- */
- public function SecretStartReport(){
- // 开始json
- $req = request()->post();
- // 首先创建记录
- if ($req){
- try {
- $data = [];
- $data['call_time'] = $req[0]['call_time'];
- $data['call_type'] = $req[0]['call_type'];
- $data['aphone'] = $req[0]['phone_no'];
- $data['bphone'] = $req[0]['peer_no'];
- $data['call_id'] = $req[0]['call_id'];
- $data['secret_no'] = $req[0]['called_display_no'];
- $data['sub_id'] = $req[0]['sub_id'];
- CallLog::create($data);
- }catch (\Exception $e){
- var_dump($e->getFile().$e->getLine().$e->getMessage());
- }catch (\PDOException $e){
- var_dump($e->getFile().$e->getLine().$e->getMessage());
- }
- }
- return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
- }
- /**
- * 电话挂断时候回调数据!
- * @return false|string
- * 回调数据
- * [{
- "phone_no": "18831138292", 主叫号码
- "pool_key": "FC100000115024469", 对应的号池Key。
- "city": "昆明",
- "sub_id": 1000027052283144, 通话对应的三元组的绑定关系ID。
- "unconnected_cause": 0, 0表示正常通话,1表示黑名单拦截,2表示无绑定关系,3表示呼叫限制,4表示其他
- "call_time": "2020-12-21 17:23:56", 主叫拨打时间
- "call_out_time": "2020-12-21 17:23:56", 呼叫由X送给B端局的时间
- "peer_no": "15222021008", AXB中的B号码或者N号码
- "called_display_no": "17052201941", 被叫显号
- "release_dir": 2, 通话释放方向。0表示平台释放,1表示主叫挂断,2表示被叫挂断
- "ring_time": "2020-12-21 17:23:56", 呼叫送被叫端局时,被叫端局响应的时间。
- "call_id": "31343639616333323735", 唯一标识一通通话记录的ID。
- "start_time": "2020-12-21 17:24:05", 被叫接听时间
- "free_ring_time": "2020-12-21 17:24:03",被叫手机真实的振铃时间。free_ring_time 大于call_out_time表示被叫真实发生了振铃事件。free_ring_time 和call_out_time相等表示未振铃。
- "partner_key": "FC100000115024469",
- "control_msg": "OK",
- "id": 1007221453890,
- "secret_no": "17052201941", AXB中的X号码
- "call_type": 0, 呼叫类型,包括:0:主叫,即phone_no打给peer_no。1:被叫,即peer_no打给phone_no。2:短信发送。3:短信接收。4:呼叫拦截5:短信收发拦截
- "release_cause": 16, 释放原因。请根据编号在释放原因中查看。
- "control_type": "CONTINUE",
- "release_time": "2020-12-21 17:24:09" 被叫挂断时间。release_time和start_time之差表示通话时长, 如果结果为0,说明呼叫未接通
- }]
- *
- */
- public function SecretReport(){
- $req = request()->post();
- $callids = CallLog::where('call_id',$req[0]['call_id'])->first();
- if ($req && $callids) {
- try {
- if ($callids){
- if($req[0]['release_dir']==0||(strtotime($req[0]['release_time'])-strtotime($req[0]['start_time']))==0||$req[0]['unconnected_cause']!=0){
- CallLog::where('call_id',$req[0]['call_id'])->delete();
- }else{
- // 修改信息
- $axbId = Axb::where(['xphone'=>$req[0]['called_display_no'],'subs_id'=>$req[0]['sub_id']])->first();
- if($axbId){
- $where['docter_id'] = $axbId['docter_id'];
- $where['user_id'] = $axbId['user_id'];
- $where['product_type'] =1;
- $where['order_status'] =3;
- $where['payment_status'] =2;
- $order_id = Order::where($where)->first();
- if ($order_id){
- $order_id = $order_id->id;
- $save_data = [];
- $save_data['order_id'] = $order_id;
- $save_data['ring_time'] = $req[0]['release_time'];
- $save_data['docter_id'] = $axbId['docter_id'];
- $save_data['release_dir'] = $req[0]['release_dir'];
- $save_data['talk_time'] = strtotime($req[0]['release_time'])-strtotime($req[0]['start_time']);
- $save_data['text'] = json_encode($req,JSON_UNESCAPED_UNICODE);
- // 解除号码绑定,并且删除数据库绑定信息
- // $this->unLokPhone($req[0]['called_display_no'],$req[0]['sub_id']);
- // Axb::where(['subs_id'=>$req[0]['sub_id']])->delete();
- CallLog::where('call_id',$req[0]['call_id'])->update($save_data);
- return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
- }
- }
- }
- }
- }catch (\Exception $e){
- CallLog::create(['text'=>json_encode($e->getFile().$e->getCode().$e->getMessage(),JSON_UNESCAPED_UNICODE)]);
- }catch (\PDOException $e){
- CallLog::create(['text'=>json_encode($e->getFile().$e->getCode().$e->getMessage(),JSON_UNESCAPED_UNICODE)]);
- }
- }
- return json_encode(['code' => 0, 'msg' => "成功"], JSON_UNESCAPED_UNICODE);
- }
- /**
- * 录音文件返回
- * @return false|string
- */
- public function Recording_status(){
- $req = request()->post();
- trace(['录音文件返回'=>'成功','入库数据'=>$req[0]],'info');
- $config = config('config.axb');
- AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
- ->regionId('cn-kunming')
- ->asDefaultClient();
- try {
- $result = AlibabaCloud::dyplsapi()
- ->v20170525()
- ->queryRecordFileDownloadUrl()
- ->withCallTime(date('Y-m-d H:i:s',substr($req[0]['call_time'], 0, 10)))
- ->withCallId($req[0]['call_id'])
- ->withPoolKey($req[0]['pool_key'])
- ->request()->toArray();
- trace(['录音文件返回1'=>'成功1','入库数据'=>$result],'info');
- if ($result['Message']=='OK'){
- $file = $this->downImgRar($result['DownloadUrl'],$req[0]['call_id']);
- CallLog::where('call_id',$req[0]['call_id'])->update(['files'=>$file]);
- trace(['录音文件完成'=>'成功','入库链接'=>$file],'info');
- }
- }catch (\Exception $e){
- trace(['录音文件返回'=>'失败','入库数据'=>$req,'录音文件返回'=>isset($result)?$result:''],'info');
- }
- return json_encode(['code' => 0, 'msg' => "成功"], JSON_UNESCAPED_UNICODE);
- }
- /**
- * 链接下载到本地
- * @param $url
- * @param $rename
- * @param $ext
- * @return string
- */
- public function downImgRar($url,$rename,$ext='mp3'){
- trace(['开始转换'=>'1','链接'=>$url],'info');
- try {
- $dir = iconv("UTF-8", "GBK", "upload/callLog/");
- if (!file_exists($dir)){
- mkdir ($dir,0777,true);
- }
- $file_path = 'upload/callLog/';
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
- $rawdata=curl_exec ($ch);
- curl_close ($ch);
- // 使用中文文件名需要转码
- $fp = fopen($file_path.iconv('UTF-8', 'GBK', $rename).".".$ext,'w');
- fwrite($fp, $rawdata);
- fclose($fp);
- // 返回路径
- trace(['转换成功!'=>'1','地址'=>$_SERVER['DOCUMENT_ROOT'].$file_path.$rename.".".$ext],'info');
- return $file_path.$rename.".".$ext;
- }catch (\Exception $e){
- trace(['转换失败'=>'1','错误'=>$e->getFile().'文件'.$e->getLine().'行'.$e->getMessage().'错误!'],'info');
- }
- }
- /**
- * goEasy聊天记录存入数据库
- * * @return false|string
- */
- public function easyMessage(){
- $req = request()->post();
- try {
- $data = json_decode($req['content'],true);
- $reminderController = new PatientController();
- $list = [];
- if($data){
- $ImList = ImMessage::select('messageId')->groupBy('messageId')->get();
- $newList=[];
- if ($ImList){
- foreach ($ImList as $k=>$v){
- $newList[$k] = $v['messageId'];
- }
- }
- foreach ($data as $k=>$v){
- if(!in_array($v['messageId'],$newList)){
- $list[$k]['messageId'] = $v['messageId'];
- $list[$k]['type'] = $v['type'];
- $list[$k]['senderId'] = $v['senderId'];
- $list[$k]['receiverId'] = $v['receiverId'];
- $list[$k]['timestamp'] = $v['timestamp'];
- $list[$k]['payload'] = $v['payload'];
- $list[$k]['text'] = $req['content'];
- $list[$k]['create_time'] = time();
- if (substr($v['senderId'],0,6)=='doctor'){
- // 说明是用户给医生发的, 就给医生端发消息
- $docter_id = substr($v['senderId'], 7);
- $docter = Docter::where('id', $docter_id)->first()->toArray();
- $user_id = substr($v['receiverId'],7);
- $user = User::where('id', $user_id)->first()->toArray();
- $send_time = date('Y-m-d H:i', round($v['timestamp']/1000));
- if ($v['type'] != 'text') {
- $text = '图片语音类消息';
- }
- else {
- $payload = json_decode($v['payload'], true);
- $text = !empty($payload['text']) ? $payload['text'] : '文字类消息';
- }
- $official_arr = [$docter['openid'], $user['nickname'], $text, $send_time];
- send_wechat_message_to_docter(12, $official_arr);
- }
- else {
- // 说明是医生给用户发的, 就给用户端发消息
- $reminderController->ReplyReminder(substr($v['receiverId'],7),substr($v['senderId'],7));
- }
- }
- }
- ImMessage::insert($list);
- }
- return json_encode(['code'=>200,'content'=>'success']);
- }catch (\Exception $e){
- ImMessage::create(['text'=>json_encode($e->getFile().'的第 '.$e->getLine().'行报错:'.$e->getMessage(),true)]);
- }catch (\PDOException $e){
- ImMessage::create(['text'=>json_encode($e->getFile().'的第 '.$e->getLine().'行报错:'.$e->getMessage(),true)]);
- }
- }
- /**
- * 删除图片
- * @return \Illuminate\Http\JsonResponse
- * @throws \Illuminate\Validation\ValidationException
- */
- public function delFile(){
- $req = request()->post();
- $this->validate(request(), [
- 'url' => 'required|url'
- ]);
- $tem = parse_url($req['url']);
- $allPath = public_path().$tem['path'];
- unlink($allPath);
- return out();
- }
- }
|