12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283 |
- <?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\SystemConfig;
- 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,
- 'is_open' => 1,
- ];
- $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,
- 'is_open' => 1,
- ];
- // 查询医生表有没有记录
- $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,
- 'is_open' => 1,
- ];
- $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,
- 'is_open' => 1,
- ];
- $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 && (isset($req[0]['called_display_no'])&&!empty($req[0]['called_display_no'])) ) {
- 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 'callLog/' . $rename . "." . $ext;
- } catch (\Exception $e) {
- trace(['转换失败' => '1', '错误' => $e->getFile() . '文件' . $e->getLine() . '行' . $e->getMessage() . '错误!'], 'info');
- }
- }
- /**
- * goEasy聊天记录存入数据库
- * * @return false|string
- */
- public function easyMessage()
- {
- $req = request()->post();
- $data = json_decode($req['content'], true);
- if ($data) {
- try {
- $list = [];
- $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) == 'member') {
- // 说明是用户给医生发的, 就给医生端发消息
- $docter_id = substr($v['receiverId'], 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 {
- // 说明是医生给用户发的, 就给用户端发消息
- $this->ReplyReminder(substr($v['senderId'], 7), substr($v['receiverId'], 7));
- }
- }
- }
- if ($list){
- $add = ImMessage::insert($list);
- order_trace(['聊天信息入库'=>$add, '请求参数' => $list ?? ''], 'info');
- }
- return json_encode(['code' => 200, 'content' => 'success']);
- } catch (\Exception $e) {
- order_trace(['聊天信息报错'=>$e->getFile() . '的第 ' . $e->getLine() . '行报错:' . $e->getMessage(), '请求参数' => $data ?? '','返回参数'=>$list], 'error');
- } catch (\PDOException $e) {
- order_trace(['聊天信息报错'=>$e->getFile() . '的第 ' . $e->getLine() . '行报错:' . $e->getMessage(), '请求参数' => $data ?? '','返回参数'=>$list], 'error');
- }
- }
- return json_encode(['code' => 200, 'content' => 'success']);
- }
- /**
- * 消息回复提醒(医生)
- * @param $order_id
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function ReplyReminder($docter_id,$user_id){
- $list = Docter::with(['DocterOrganization','organization'])->where(['id'=>$docter_id])->first();
- $user = User::where('id',$user_id)->first();
- if ($list){
- order_trace(['消息回复2', '请求参数' => $docter_id.'-'.$user_id ?? '', 'list数据' => $list->toArray() ?? ''], 'info');
- order_trace(['消息回复3', '请求参数' => $docter_id.'-'.$user_id ?? '', 'list数据' => $user ?? ''], 'info');
- if ($user['openid']){
- $send = send_wechatSubscription_message('reply_reminder',[
- $user['openid'],
- "pages/index/index",
- $list['organization'][0]['name'],
- $list['name'],
- $user['nickname'],
- ],'wechat_small_program');
- }
- }
- }
- /**
- * 删除图片
- * @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();
- }
- public function contactUsPhone(){
- $phone = SystemConfig::where('key','contact_us_phone')->value('value');
- return out($phone);
- }
- }
|