CommonController.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-23
  6. * Time: 上午11:26
  7. */
  8. namespace App\Http\Controllers\Api\V2;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\Article;
  11. use App\Models\Order;
  12. use App\Models\User;
  13. use App\Models\Docter;
  14. use EasyWeChat\Factory;
  15. use http\Env\Request;
  16. use Illuminate\Support\Facades\DB;
  17. use AlibabaCloud\Client\Exception\ClientException;
  18. use Alibabaloud\Client\Exception\ServerException;
  19. use AlibabaCloud\Client\AlibabaCloud;
  20. use AlibabaCloud\Dybaseapi\MNS\Requests\BatchReceiveMessage;
  21. use AlibabaCloud\Dybaseapi\MNS\Requests\BatchDeleteMessage;
  22. use App\Models\CallLog;
  23. use App\Models\Axb;
  24. use App\Models\ImMessage;
  25. use Cache;
  26. /**
  27. * 公共方法类
  28. * Class CommonController
  29. * @package App\Http\Controllers\Api\V2
  30. */
  31. class CommonController extends Controller
  32. {
  33. public function wxLogin()
  34. {
  35. $req = request()->post();
  36. $this->validate(request(), [
  37. 'wechat_code' => 'required',
  38. 'nickname|昵称' => 'max:50',
  39. 'avatar|头像' => 'url',
  40. 'latitude|纬度' => 'numeric',
  41. 'longitude|纬度' => 'numeric',
  42. ]);
  43. $app = Factory::miniProgram(config('config.docter_wechat_small_program'));
  44. $data = $app->auth->session($req['wechat_code']);
  45. if (empty($data['openid'])){
  46. return out(null, 10001, '微信登录code错误');
  47. }
  48. $session_key = !empty($data['session_key']) ? $data['session_key'] : '';
  49. $user = User::select(['id', 'status', 'phone', 'nickname', 'avatar'])->where('openid', $data['openid'])->first();
  50. if (empty($user)){
  51. $user = User::create([
  52. 'openid' => $data['openid'],
  53. 'nickname' => $req['nickname'] ?? '',
  54. 'avatar' => $req['avatar'] ?? '',
  55. 'latitude' => $req['latitude'] ?? 0,
  56. 'longitude' => $req['longitude'] ?? 0,
  57. 'session_key' => $session_key,
  58. 'is_docter' => 1
  59. ]);
  60. }
  61. else {
  62. if ($user['status'] == 0) {
  63. return out(null, 10002, '该账号已被冻结');
  64. }
  65. User::where('id', $user['id'])->update([
  66. 'nickname' => $req['nickname'] ?? '',
  67. 'avatar' => $req['avatar'] ?? '',
  68. 'latitude' => $req['latitude'] ?? 0,
  69. 'longitude' => $req['longitude'] ?? 0,
  70. 'session_key' => $session_key
  71. ]);
  72. }
  73. // 查询医生表有没有记录
  74. $doct = User::where('openid',$data['openid'])->orderBy('id','desc')->first();
  75. if ($doct['phone']!=''){
  76. $list = Docter::where('phone',$doct['phone'])->orderBy('id','desc')->first();
  77. if ($list){
  78. if ($list['status'] == 0) {
  79. return out(null, 10002, '该账号已被冻结');
  80. }
  81. $datas = [
  82. 'avatar' => $list['avatar'],
  83. 'name' => $list['name'],
  84. 'flag' => 'doctor_'.$list['id'],
  85. ];
  86. $token = aes_encrypt(['doctor_id' => $list['id'], 'time' => time()]);
  87. return out(['token' => $token,'data'=>$datas]);
  88. }else{
  89. return out(['session_key'=>$session_key,'openid'=>$data['openid']]);
  90. }
  91. }else{
  92. return out(['session_key'=>$session_key,'openid'=>$data['openid']]);
  93. }
  94. }
  95. /**
  96. * 获取手机号!
  97. * Auth:Yuanhang-Liu
  98. * Date:2020/10/18 17:17 *
  99. * @return \Illuminate\Http\JsonResponse
  100. */
  101. public function getPhoneNumber(){
  102. $req = request()->post();
  103. $this->validate(request(), [
  104. 'iv' => 'required',
  105. 'encryptedData' => 'required',
  106. 'session_key' => 'required',
  107. 'openid' => 'required',
  108. ]);
  109. $app = Factory::miniProgram(config('config.docter_wechat_small_program'));
  110. // $data = $app->auth->session($req['code']);
  111. // if (empty($data['openid'])){
  112. // return out(null, 10001, '微信登录code错误');
  113. // }
  114. $session_key = !empty($req['session_key']) ? $req['session_key'] : '';
  115. $user = User::select(['id','sex', 'status', 'phone', 'birthday','nickname', 'avatar','status'])->where('openid', $req['openid'])->first();
  116. if (!$user){
  117. return out(['status'=>false,'msg' => '用户不存在!']);
  118. }
  119. $decryptedData = $app->encryptor->decryptData($session_key, $req['iv'], $req['encryptedData']);
  120. if (!isset($decryptedData['phoneNumber']) || empty($decryptedData['phoneNumber'])){
  121. return out(['status'=>false,'msg' => '手机号解密失败!']);
  122. }
  123. $docter_list = [
  124. 'type' => 1,
  125. 'name' => $user['nickname'],
  126. 'phone' => $decryptedData['phoneNumber'],
  127. 'sex' => $user['sex'],
  128. 'birthday' => $user['birthday'],
  129. 'avatar' => $user['avatar'],
  130. 'status' => 1,
  131. 'label' => '无',
  132. 'sign' => '无',
  133. 'intro' => 0,
  134. 'office_id' => 0,
  135. 'qualification_id' => 0,
  136. 'score' => 0,
  137. 'service_persons' => 0,
  138. 'eva_num' => 0,
  139. 'service_days' => 0,
  140. 'phone_minutes' => 0,
  141. 'chat_price' => 0,
  142. 'phone_price' => 0,
  143. 'appoint_price' => 0,
  144. 'is_chat' => 0,
  145. 'is_phone' => 0,
  146. 'is_appoint' => 0,
  147. 'latitude' => 0,
  148. 'longitude' => 0,
  149. 'is_then' => 0,
  150. 'practice' => 0,
  151. 'card_photo' => 0,
  152. 'is_quail' => 0,
  153. 'card_id' => 0,
  154. ];
  155. // 查询医生表有没有记录
  156. $doct = Docter::where('phone',$decryptedData['phoneNumber'])->orderBy('id','desc')->first();
  157. if ($doct){
  158. if ($doct['status'] == 0) {
  159. return out(null, 10002, '该账号已被冻结');
  160. }
  161. $datas = [
  162. 'avatar' => $doct['avatar'],
  163. 'name' => $doct['name'],
  164. 'flag' => 'doctor_'.$doct['id'],
  165. ];
  166. $token = aes_encrypt(['doctor_id' => $doct['id'], 'time' => time()]);
  167. return out(['token' => $token,'data'=>$datas]);
  168. }
  169. $list=[];
  170. if ($user){
  171. DB::beginTransaction();
  172. try {
  173. User::where('openid', $req['openid'])->update([
  174. 'phone' => $decryptedData['phoneNumber'] ?? '','is_docter'=>1,
  175. ]);
  176. // 注册医生
  177. $list = Docter::create($docter_list);
  178. DB::commit();
  179. }catch (\Exception $e){
  180. DB::rollback();
  181. return out(null, 500, $e->getMessage());
  182. }catch (\PDOException $e){
  183. DB::rollback();
  184. return out(null, 500, $e->getMessage());
  185. }
  186. }
  187. $datas = [
  188. 'avatar' => $list['avatar'],
  189. 'name' => $list['name'],
  190. 'flag' => 'doctor_'.$list['id'],
  191. ];
  192. $token = aes_encrypt(['doctor_id' => $list['id'], 'time' => time()]);
  193. return out(['token' => $token,'data'=>$datas]);
  194. }
  195. /**
  196. * 手机号登陆
  197. * Auth:Yuanhang-Liu
  198. * Date:2020/10/18 19:17 *
  199. * @return \Illuminate\Http\JsonResponse
  200. */
  201. public function phoneLogin(){
  202. $req = request()->post();
  203. $this->validate(request(), [
  204. 'phone|手机号' => 'required|integer',
  205. ]);
  206. $req['verify'] = 0;
  207. $find = Docter::where('phone','=',$req['phone'])->first();
  208. if (empty($find)){
  209. $docter_list = [
  210. 'type' => 1,
  211. 'name' => '用户名',
  212. 'phone' => $req['phone'],
  213. 'sex' => 0,
  214. 'birthday' => 0,
  215. 'avatar' => '../../static/login/moren.png',
  216. 'status' => 1,
  217. 'label' => '无',
  218. 'sign' => '无',
  219. 'intro' => 0,
  220. 'office_id' => 0,
  221. 'qualification_id' => 0,
  222. 'score' => 0,
  223. 'service_persons' => 0,
  224. 'eva_num' => 0,
  225. 'service_days' => 0,
  226. 'phone_minutes' => 0,
  227. 'chat_price' => 0,
  228. 'phone_price' => 0,
  229. 'appoint_price' => 0,
  230. 'is_chat' => 0,
  231. 'is_phone' => 0,
  232. 'is_appoint' => 0,
  233. 'latitude' => 0,
  234. 'longitude' => 0,
  235. 'is_then' => 0,
  236. 'practice' => 0,
  237. 'card_photo' => 0,
  238. 'is_quail' => 0,
  239. 'card_id' => 0,
  240. ];
  241. $list = Docter::create($docter_list)->toArray();
  242. if (!empty($list)){
  243. $datas = [
  244. 'avatar' => $list['avatar'],
  245. 'name' => $list['name'],
  246. 'flag' => 'doctor_'.$list['id'],
  247. ];
  248. $token = aes_encrypt(['doctor_id' => $list['id'], 'time' => time()]);
  249. return out(['token' => $token,'data'=>$datas]);
  250. }else{
  251. return out('',401,'用户不存在,注册失败!');
  252. }
  253. }
  254. $find = $find->toArray();
  255. if ($find['status'] == 0) {
  256. return out(null, 10002, '该账号已被冻结');
  257. }
  258. if ($req['verify']==0){
  259. $datas = [
  260. 'avatar' => $find['avatar'],
  261. 'name' => $find['name'],
  262. 'flag' => 'doctor_'.$find['id'],
  263. ];
  264. // 验证是否正确
  265. $token = aes_encrypt(['doctor_id' => $find['id'], 'time' => time()]);
  266. return out(['token' => $token,'data'=>$datas]);
  267. }else{
  268. return out(null, 401, '验证码错误');
  269. }
  270. }
  271. /**
  272. * 账号密码登陆
  273. * Auth:Yuanhang-Liu
  274. * Date:2020/10/18 19:17 *
  275. * @return \Illuminate\Http\JsonResponse
  276. */
  277. public function passLogin(){
  278. $req = request()->post();
  279. $this->validate(request(), [
  280. 'phone|手机号' => 'required|integer',
  281. 'password|密码' => 'required',
  282. ]);
  283. $find = Docter::where('phone','=',$req['phone'])->first();
  284. if (empty($find)){
  285. return out(null, 401, '账号不存在');
  286. }
  287. if ($find['status'] == 0) {
  288. return out(null, 10002, '该账号已被冻结');
  289. }
  290. $find = $find->toArray();
  291. // 验证密码
  292. $password = md5(md5(md5($req['password'])));
  293. if ($password==$find['password']){
  294. $datas = [
  295. 'avatar' => $find['avatar'],
  296. 'name' => $find['name'],
  297. 'flag' => 'doctor_'.$find['id'],
  298. ];
  299. $token = aes_encrypt(['doctor_id' => $find['id'], 'time' => time()]);
  300. return out(['token' => $token,'data'=>$datas]);
  301. }else{
  302. return out(null, 401, '密码错误');
  303. }
  304. }
  305. /**
  306. * 获取验证码
  307. * @return \Illuminate\Http\JsonResponse
  308. * @author Liu-Yh
  309. * Create By 2020/11/6 10:45
  310. */
  311. public function putverfiy(){
  312. //防止恶意刷验证码接口,一分钟最多10次
  313. check_repeat_request(60, 10);
  314. $req = request()->post();
  315. $this->validate(request(), [
  316. 'phone|手机号' => 'required|integer',
  317. ]);
  318. $mobile =$req['phone']; //获取传入的手机号
  319. $verify_code = generate_code();
  320. $result = send_sms($mobile,'verify_template_code',['code'=>$verify_code]);
  321. if (empty($result['Code']) || $result['Code'] != 'OK'){
  322. return out(null, 30010, '验证码发送失败,请稍后重试');
  323. }
  324. Cache::set($req['phone'].'-', $verify_code, config('config.aly_sms.sms_verify_code_expire'));
  325. return out();
  326. }
  327. /**
  328. * 手机号注册
  329. * Auth:Yuanhang-Liu
  330. * Date:2020/10/18 20:17 *
  331. * @return \Illuminate\Http\JsonResponse
  332. */
  333. public function phoneRegister(){
  334. $req = request()->post();
  335. $this->validate(request(), [
  336. 'phone|手机号' => 'required|integer',
  337. 'password|密码' => 'required',
  338. ]);
  339. // 查询是否注册过!
  340. $docters = Docter::where('phone','=',$req['phone'])->first();
  341. if (!empty($docters)){
  342. return out('',500,'此手机号已被注册!');
  343. }
  344. $password = md5(md5(md5($req['password'])));
  345. $docter_list = [
  346. 'type' => 1,
  347. 'name' => '用户名',
  348. 'phone' => $req['phone'],
  349. 'sex' => 0,
  350. 'birthday' => 0,
  351. 'avatar' => '无',
  352. 'status' => 1,
  353. 'label' => '无',
  354. 'sign' => '',
  355. 'intro' => '',
  356. 'office_id' => 0,
  357. 'qualification_id' => 0,
  358. 'score' => 0,
  359. 'service_persons' => 0,
  360. 'eva_num' => 0,
  361. 'service_days' => 0,
  362. 'phone_minutes' => 0,
  363. 'chat_price' => 0,
  364. 'phone_price' => 0,
  365. 'appoint_price' => 0,
  366. 'is_chat' => 0,
  367. 'is_phone' => 0,
  368. 'is_appoint' => 0,
  369. 'latitude' => 0,
  370. 'longitude' => 0,
  371. 'password' => $password,
  372. 'is_then' => 0,
  373. 'practice' => 0,
  374. 'card_photo' => 0,
  375. 'is_quail' => 0,
  376. 'card_id' => 0,
  377. ];
  378. $list = Docter::create($docter_list)->toArray();
  379. if (!empty($list)){
  380. $datas = [
  381. 'avatar' => $list['avatar'],
  382. 'name' => $list['name'],
  383. 'flag' => 'doctor_'.$list['id'],
  384. ];
  385. $token = aes_encrypt(['doctor_id' => $list['id'], 'time' => time()]);
  386. return out(['token' => $token,'data'=>$datas]);
  387. }else{
  388. return out('',500,'注册失败!');
  389. }
  390. // return out();
  391. }
  392. public function articleList()
  393. {
  394. $data = Article::orderBy('id', 'desc')->paginate();
  395. return out($data);
  396. }
  397. public function getUserIdByDoctorId($phone=null){
  398. $list = Docter::where('phone', $phone)->first();
  399. if ($list){
  400. return $list->id;
  401. }else{
  402. return false;
  403. }
  404. }
  405. public function uploadFile()
  406. {
  407. $file = request()->file('file');
  408. if (empty($file)) {
  409. return out(null, 10001, '文件不能为空');
  410. }
  411. $path = $file->store('upload/docter'.date('Ymd'));
  412. // $url = request()->getScheme().'://'.request()->getHost().'/'.$path;
  413. return out(['url' => $path]);
  414. }
  415. public function doc()
  416. {
  417. $database = env('DB_DATABASE');
  418. $prefix = env('DB_PREFIX');
  419. $exclude_tables = "'bm_password_resets','bm_admin_menu','bm_admin_users','bm_failed_jobs','bm_migrations'";
  420. $sql = "select TABLE_NAME name,TABLE_COMMENT comment from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='".$database."' and TABLE_NAME not in (".$exclude_tables.")";
  421. $tables = \DB::select($sql);
  422. $map1 = $map2 = [];
  423. $i = round(count($tables)/2);
  424. foreach ($tables as $k => $v) {
  425. $name = str_replace($prefix, '', $v->name);
  426. if ($k >= $i) {
  427. $map1[$v->name] = $name.'('.$v->comment.')';
  428. }
  429. else {
  430. $map2[$v->name] = $name.'('.$v->comment.')';
  431. }
  432. }
  433. $data1 = [];
  434. foreach ($map1 as $k => $v){
  435. $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
  436. $comment = \DB::select($sql);
  437. $data1[$v] = $comment;
  438. }
  439. $data2 = [];
  440. foreach ($map2 as $k => $v){
  441. $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
  442. $comment = \DB::select($sql);
  443. $data2[$v] = $comment;
  444. }
  445. return view('doc', ['data1' => $data1, 'data2' => $data2]);
  446. }
  447. /**
  448. * 绑定号码池子
  449. * @param string $phone1 医生id
  450. * @param string $phone2 用户id
  451. * @param array $data 参数
  452. * @return mixed
  453. */
  454. public function BindAxb($phone1,$phone2,$data=[]){
  455. $config = config('config.axb');
  456. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  457. ->regionId('cn-kunming')
  458. ->asDefaultClient();
  459. try {
  460. $result = AlibabaCloud::rpc()
  461. ->product('Dyplsapi')
  462. ->version('2017-05-25')
  463. ->action('BindAxb')
  464. ->method('POST')
  465. ->host('dyplsapi.aliyuncs.com')
  466. ->options([
  467. 'query' => [
  468. "Expiration" => date("Y-m-d H:i:s",strtotime("+1 day")),
  469. 'RegionId' => "cn-kunming",
  470. 'PhoneNoA' => $phone1,
  471. 'PhoneNoB' => $phone2,
  472. ],
  473. ])
  474. ->request();
  475. return $result->toArray();
  476. } catch (ClientException $e) {
  477. echo $e->getErrorMessage() . PHP_EOL;
  478. } catch (ServerException $e) {
  479. echo $e->getErrorMessage() . PHP_EOL;
  480. }
  481. }
  482. /**
  483. * 释放虚拟号码
  484. * @param int $phone
  485. */
  486. public function ReleaseSecretNo($phone){
  487. $config = config('config.axb');
  488. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  489. ->regionId('cn-kunming')
  490. ->asDefaultClient();
  491. try {
  492. $result = AlibabaCloud::rpc()
  493. ->product('Dyplsapi')
  494. // ->scheme('https') // https | http
  495. ->version('2017-05-25')
  496. ->action('ReleaseSecretNo')
  497. ->method('POST')
  498. ->host('dyplsapi.aliyuncs.com')
  499. ->options([
  500. 'query' => [
  501. 'RegionId' => "cn-kunming",
  502. "SecretNo" => $phone,
  503. "PoolKey" => $config['PoolKey'],
  504. ],
  505. ])
  506. ->request();
  507. $res = $result->toArray();
  508. return $res;
  509. } catch (ClientException $e) {
  510. echo $e->getErrorMessage() . PHP_EOL;
  511. } catch (ServerException $e) {
  512. echo $e->getErrorMessage() . PHP_EOL;
  513. }
  514. }
  515. /**
  516. * 查询号码关系
  517. * @param $phone
  518. * @return mixed
  519. */
  520. public function QuerySubscriptionDetail($phone,$SubsId){
  521. $config = config('config.axb');
  522. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  523. ->regionId('cn-kunming')
  524. ->asDefaultClient();
  525. try {
  526. $result = AlibabaCloud::rpc()
  527. ->product('Dyplsapi')
  528. // ->scheme('https') // https | http
  529. ->version('2017-05-25')
  530. ->action('QuerySubscriptionDetail')
  531. ->method('POST')
  532. ->host('dyplsapi.aliyuncs.com')
  533. ->options([
  534. 'query' => [
  535. 'RegionId' => "cn-hangzhou",
  536. "PoolKey" => $config['PoolKey'],
  537. "SubsId" => $SubsId,
  538. "PhoneNoX" => $phone,
  539. ],
  540. ])
  541. ->request();
  542. $res = $result->toArray();
  543. return $res;
  544. } catch (ClientException $e) {
  545. echo $e->getErrorMessage() . PHP_EOL;
  546. } catch (ServerException $e) {
  547. echo $e->getErrorMessage() . PHP_EOL;
  548. }
  549. }
  550. /**
  551. * 解除绑定关系
  552. * @param $phone
  553. * @return mixed
  554. */
  555. public function UnbindSubscription($phone,$subId){
  556. $config = config('config.axb');
  557. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  558. ->regionId('cn-kunming')
  559. ->asDefaultClient();
  560. try {
  561. $result = AlibabaCloud::rpc()
  562. ->product('Dyplsapi')
  563. // ->scheme('https') // https | http
  564. ->version('2017-05-25')
  565. ->action('UnbindSubscription')
  566. ->method('POST')
  567. ->host('dyplsapi.aliyuncs.com')
  568. ->options([
  569. 'query' => [
  570. 'RegionId' => "cn-kunming",
  571. 'SubsId' => $subId,
  572. "SecretNo" => $phone,
  573. "PoolKey" => $config['PoolKey'],
  574. ],
  575. ])
  576. ->request();
  577. $res = $result->toArray();
  578. return $res;
  579. } catch (ClientException $e) {
  580. echo $e->getErrorMessage() . PHP_EOL;
  581. } catch (ServerException $e) {
  582. echo $e->getErrorMessage() . PHP_EOL;
  583. }
  584. }
  585. /**
  586. * 调用接口QuerySubsId查询绑定唯一标识SubsId
  587. * @param $phone X号码
  588. * @return mixed
  589. */
  590. public function QuerySubsId($phone){
  591. $config = config('config.axb');
  592. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  593. ->regionId('cn-kunming')
  594. ->asDefaultClient();
  595. try {
  596. $result = AlibabaCloud::rpc()
  597. ->product('Dyplsapi')
  598. // ->scheme('https') // https | http
  599. ->version('2017-05-25')
  600. ->action('QuerySubsId')
  601. ->method('POST')
  602. ->host('dyplsapi.aliyuncs.com')
  603. ->options([
  604. 'query' => [
  605. 'RegionId' => "cn-hangzhou",
  606. 'Action' => "QuerySubsId",
  607. "PhoneNoX" => $phone,
  608. "PoolKey" => $config['PoolKey'],
  609. ],
  610. ])
  611. ->request();
  612. $res = $result->toArray();
  613. return $res;
  614. } catch (ClientException $e) {
  615. echo $e->getErrorMessage() . PHP_EOL;
  616. } catch (ServerException $e) {
  617. echo $e->getErrorMessage() . PHP_EOL;
  618. }
  619. }
  620. /**
  621. * 解锁号码
  622. * @return \Illuminate\Http\JsonResponse
  623. * @author Liu-Yh
  624. * Create By 2020/11/25 18:36
  625. */
  626. public function unLokPhone($phone,$SubsId){
  627. $unlok = $this->UnbindSubscription($phone,$SubsId);
  628. if ($unlok['Code']!='OK'){
  629. return out($unlok);
  630. }else{
  631. return 1;
  632. }
  633. }
  634. /**
  635. * 测试解除绑定电话的方法
  636. */
  637. public function testunlockphone(){
  638. $phone = 17052201940;
  639. $sub_id = 1000027059330181;
  640. var_dump($this->unLokPhone($phone,$sub_id));
  641. }
  642. /**
  643. * 调用接口QueryCallStatus查询呼叫状态。
  644. * @param $phone
  645. * @author Liu-Yh
  646. * Create By 2020/11/26 10:51
  647. */
  648. public function QueryCallStatus($phone,$SubsId){
  649. $config = config('config.axb');
  650. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  651. ->regionId('cn-kunming')
  652. ->asDefaultClient();
  653. try {
  654. $result = AlibabaCloud::rpc()
  655. ->product('Dyplsapi')
  656. // ->scheme('https') // https | http
  657. ->version('2017-05-25')
  658. ->action('QueryCallStatus')
  659. ->method('POST')
  660. ->host('dyplsapi.aliyuncs.com')
  661. ->options([
  662. 'query' => [
  663. 'RegionId' => "cn-hangzhou",
  664. "PoolKey" => $config['PoolKey'],
  665. "SubsId" => $SubsId,
  666. ],
  667. ])
  668. ->request();
  669. $res = $result->toArray();
  670. return $res;
  671. } catch (ClientException $e) {
  672. echo $e->getErrorMessage() . PHP_EOL;
  673. } catch (ServerException $e) {
  674. echo $e->getErrorMessage() . PHP_EOL;
  675. }
  676. }
  677. /**
  678. * 接收通话发起时的通话记录报告内容,可以在呼叫发起时立即获取到通话记录信息,包括通话开始时间、主被叫号码等,便于平台进行预判处理
  679. * @author Liu-Yh
  680. * Create By 2020/11/25 14:44
  681. */
  682. public function StartSecretReport(){
  683. $config = config('config.axb');
  684. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  685. ->regionId('cn-kunming')
  686. ->asDefaultClient();
  687. $queueName = $config['StartReport']; // 队列名称
  688. $messageType = "SecretStartReport"; // 需要接收的消息类型
  689. $response = null;
  690. $token = null;
  691. $i = 0;
  692. do {
  693. try {
  694. if (null == $token || strtotime($token['ExpireTime']) - time() > 2 * 60) {
  695. $response = AlibabaCloud::rpcRequest()
  696. ->product('Dybaseapi')
  697. ->version('2017-05-25')
  698. ->action('QueryTokenForMnsQueue')
  699. ->method('POST')
  700. ->host("dybaseapi.aliyuncs.com")
  701. ->options([
  702. 'query' => [
  703. 'MessageType' => $messageType,
  704. 'QueueName' => $queueName,
  705. ],
  706. ])
  707. ->request()
  708. ->toArray();
  709. }
  710. $token = $response['MessageTokenDTO'];
  711. $mnsClient = new \AlibabaCloud\Dybaseapi\MNS\MnsClient(
  712. "http://1943695596114318.mns.cn-hangzhou.aliyuncs.com",
  713. $token['AccessKeyId'],
  714. $token['AccessKeySecret'],
  715. $token['SecurityToken']
  716. );
  717. $mnsRequest = new BatchReceiveMessage(10, 5);
  718. $mnsRequest->setQueueName($queueName);
  719. $mnsResponse = $mnsClient->sendRequest($mnsRequest);
  720. $receiptHandles = Array();
  721. foreach ($mnsResponse->Message as $message) {
  722. // 用户逻辑:
  723. // 入库
  724. // var_dump(base64_decode($message->MessageBody));
  725. // var_dump(json_decode(base64_decode($message->MessageBody),true));
  726. // $receiptHandles[] = $message->ReceiptHandle; // 加入$receiptHandles数组中的记录将会被删除
  727. $messageBody = json_decode(base64_decode($message->MessageBody),true); // base64解码后的JSON字符串
  728. echo "进来了";
  729. var_dump($messageBody);
  730. }
  731. if (count($receiptHandles) > 0) {
  732. $deleteRequest = new BatchDeleteMessage($queueName, $receiptHandles);
  733. $mnsClient->sendRequest($deleteRequest);
  734. }
  735. } catch (ClientException $e) {
  736. echo $e->getErrorMessage() . PHP_EOL;
  737. } catch (ServerException $e) {
  738. if ($e->getCode() == 404) {
  739. $i++;
  740. }
  741. echo $e->getErrorMessage() . PHP_EOL;
  742. }
  743. } while ($i < 3);
  744. }
  745. /**
  746. * 接收通话结束时的通话记录报告内容,可以在呼叫结束后获取通话记录信息,包括通话开始时间、通话结束时间、主被叫号码等,
  747. * @author Liu-Yh
  748. * Create By 2020/11/25 12:29
  749. */
  750. public function SecretPullReport(){
  751. $config = config('config.axb');
  752. AlibabaCloud::accessKeyClient($config['appid'], $config['appscret'])
  753. ->regionId('cn-kunming')
  754. ->asDefaultClient();
  755. $queueName = $config['Report']; // 队列名称
  756. $messageType = "SecretReport"; // 需要接收的消息类型
  757. $response = null;
  758. $token = null;
  759. $i = 0;
  760. do {
  761. try {
  762. if (null == $token || strtotime($token['ExpireTime']) - time() > 2 * 60) {
  763. $response = AlibabaCloud::rpcRequest()
  764. ->product('Dybaseapi')
  765. ->version('2017-05-25')
  766. ->action('QueryTokenForMnsQueue')
  767. ->method('POST')
  768. ->host("dybaseapi.aliyuncs.com")
  769. ->options([
  770. 'query' => [
  771. 'MessageType' => $messageType,
  772. 'QueueName' => $queueName,
  773. ],
  774. ])
  775. ->request()
  776. ->toArray();
  777. }
  778. $token = $response['MessageTokenDTO'];
  779. $mnsClient = new \AlibabaCloud\Dybaseapi\MNS\MnsClient(
  780. "http://1943695596114318.mns.cn-hangzhou.aliyuncs.com",
  781. $token['AccessKeyId'],
  782. $token['AccessKeySecret'],
  783. $token['SecurityToken']
  784. );
  785. $mnsRequest = new BatchReceiveMessage(10, 5);
  786. $mnsRequest->setQueueName($queueName);
  787. $mnsResponse = $mnsClient->sendRequest($mnsRequest);
  788. $receiptHandles = Array();
  789. $getList = CallLog::get();
  790. if ($getList){
  791. $getList = $getList->toArray();
  792. }
  793. $msgs = $mnsResponse->Message;
  794. if (!is_array($msgs)){
  795. $messageBody = json_decode(base64_decode($msgs->MessageBody),true); // base64解码后的JSON字符
  796. if (count($getList)==0){
  797. CallLog::create([
  798. 'call_time'=>$messageBody['call_time'],
  799. 'ring_time'=>$messageBody['release_time'],
  800. 'release_dir'=>$messageBody['release_dir'],
  801. 'call_type'=>$messageBody['call_type'],
  802. 'aphone'=>$messageBody['phone_no'],
  803. 'bphone'=>$messageBody['peer_no'],
  804. 'call_id'=>$messageBody['call_id'],
  805. 'secret_no'=>$messageBody['secret_no'],
  806. 'sub_id'=>$messageBody['sub_id'],
  807. 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
  808. ]);
  809. }else{
  810. foreach ($getList as $k=>$v){
  811. if ($v['call_id']==$messageBody['call_id']){
  812. unset($messageBody);
  813. }else{
  814. CallLog::create([
  815. 'call_time'=>$messageBody['call_time'],
  816. 'ring_time'=>$messageBody['release_time'],
  817. 'release_dir'=>$messageBody['release_dir'],
  818. 'call_type'=>$messageBody['call_type'],
  819. 'aphone'=>$messageBody['phone_no'],
  820. 'bphone'=>$messageBody['peer_no'],
  821. 'call_id'=>$messageBody['call_id'],
  822. 'secret_no'=>$messageBody['secret_no'],
  823. 'sub_id'=>$messageBody['sub_id'],
  824. 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
  825. ]);
  826. }
  827. }
  828. }
  829. }else{
  830. foreach ($msgs as $message) {
  831. // 用户逻辑:
  832. $messageBody = json_decode(base64_decode($message->MessageBody),true); // base64解码后的JSON字符串
  833. if (count($getList)==0) {
  834. CallLog::create([
  835. 'call_time'=>$messageBody['call_time'],
  836. 'ring_time'=>$messageBody['release_time'],
  837. 'release_dir'=>$messageBody['release_dir'],
  838. 'call_type'=>$messageBody['call_type'],
  839. 'aphone'=>$messageBody['phone_no'],
  840. 'bphone'=>$messageBody['peer_no'],
  841. 'call_id'=>$messageBody['call_id'],
  842. 'secret_no'=>$messageBody['secret_no'],
  843. 'sub_id'=>$messageBody['sub_id'],
  844. 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
  845. ]);
  846. }else{
  847. foreach ($getList as $k=>$v){
  848. if ($v['call_id']==$messageBody['call_id']){
  849. unset($messageBody);
  850. }else{
  851. CallLog::create([
  852. 'call_time'=>$messageBody['call_time'],
  853. 'ring_time'=>$messageBody['release_time'],
  854. 'release_dir'=>$messageBody['release_dir'],
  855. 'call_type'=>$messageBody['call_type'],
  856. 'aphone'=>$messageBody['phone_no'],
  857. 'bphone'=>$messageBody['peer_no'],
  858. 'call_id'=>$messageBody['call_id'],
  859. 'secret_no'=>$messageBody['secret_no'],
  860. 'sub_id'=>$messageBody['sub_id'],
  861. 'talk_time'=> strtotime($messageBody['release_time'])-strtotime($messageBody['start_time']),
  862. ]);
  863. }
  864. }
  865. }
  866. $receiptHandles[] = $message->ReceiptHandle; // 加入$receiptHandles数组中的记录将会被删除
  867. }
  868. }
  869. if (count($receiptHandles) > 0) {
  870. $deleteRequest = new BatchDeleteMessage($queueName, $receiptHandles);
  871. $mnsClient->sendRequest($deleteRequest);
  872. }
  873. } catch (ClientException $e) {
  874. echo $e->getErrorMessage() . PHP_EOL;
  875. } catch (ServerException $e) {
  876. if ($e->getCode() == 404) {
  877. $i++;
  878. }
  879. echo $e->getErrorMessage() . PHP_EOL;
  880. }
  881. } while ($i < 3);
  882. }
  883. /**
  884. * 通话开始时候回调数据
  885. * @return false|string
  886. * 返回参数
  887. * [{
  888. "phone_no": "18831138292",
  889. "pool_key": "FC100000115024469", 对应的号池Key。
  890. "city": "昆明",
  891. "sub_id": 1000027052283144, 通话对应的三元组的绑定关系ID。
  892. "unconnected_cause": 0,
  893. "call_time": "2020-12-21 17:23:56", 主叫拨打时间。
  894. "peer_no": "15222021008", AXB中的B号码或者N号码。
  895. "called_display_no": "17052201941", 被叫显号X号码
  896. "call_id": "31343639616333323735", 唯一标识一通通话记录的ID。
  897. "partner_key": "FC100000115024469",
  898. "control_msg": "OK",
  899. "id": 1007221453890,
  900. "secret_no": "17052201941",
  901. "call_type": 0,0:主叫(phone_no打给peer_no);1:被叫(peer_no打给phone_no);2:短信发送;3:短信接收;4:呼叫拦截;5:短信收发拦截;
  902. "control_type": "CONTINUE"
  903. }]
  904. */
  905. public function SecretStartReport(){
  906. // 开始json
  907. $req = request()->post();
  908. // 首先创建记录
  909. try {
  910. $data = [];
  911. $data['call_time'] = $req[0]['call_time'];
  912. $data['call_type'] = $req[0]['call_type'];
  913. $data['aphone'] = $req[0]['phone_no'];
  914. $data['bphone'] = $req[0]['peer_no'];
  915. $data['call_id'] = $req[0]['call_id'];
  916. $data['secret_no'] = $req[0]['called_display_no'];
  917. $data['sub_id'] = $req[0]['sub_id'];
  918. CallLog::create($data);
  919. }catch (\Exception $e){
  920. var_dump($e->getFile().$e->getLine().$e->getMessage());
  921. }catch (\PDOException $e){
  922. var_dump($e->getFile().$e->getLine().$e->getMessage());
  923. }
  924. return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
  925. }
  926. /**
  927. * 电话挂断时候回调数据!
  928. * @return false|string
  929. * 回调数据
  930. * [{
  931. "phone_no": "18831138292", 主叫号码
  932. "pool_key": "FC100000115024469", 对应的号池Key。
  933. "city": "昆明",
  934. "sub_id": 1000027052283144, 通话对应的三元组的绑定关系ID。
  935. "unconnected_cause": 0, 0表示正常通话,1表示黑名单拦截,2表示无绑定关系,3表示呼叫限制,4表示其他
  936. "call_time": "2020-12-21 17:23:56", 主叫拨打时间
  937. "call_out_time": "2020-12-21 17:23:56", 呼叫由X送给B端局的时间
  938. "peer_no": "15222021008", AXB中的B号码或者N号码
  939. "called_display_no": "17052201941", 被叫显号
  940. "release_dir": 2, 通话释放方向。0表示平台释放,1表示主叫挂断,2表示被叫挂断
  941. "ring_time": "2020-12-21 17:23:56", 呼叫送被叫端局时,被叫端局响应的时间。
  942. "call_id": "31343639616333323735", 唯一标识一通通话记录的ID。
  943. "start_time": "2020-12-21 17:24:05", 被叫接听时间
  944. "free_ring_time": "2020-12-21 17:24:03",被叫手机真实的振铃时间。free_ring_time 大于call_out_time表示被叫真实发生了振铃事件。free_ring_time 和call_out_time相等表示未振铃。
  945. "partner_key": "FC100000115024469",
  946. "control_msg": "OK",
  947. "id": 1007221453890,
  948. "secret_no": "17052201941", AXB中的X号码
  949. "call_type": 0, 呼叫类型,包括:0:主叫,即phone_no打给peer_no。1:被叫,即peer_no打给phone_no。2:短信发送。3:短信接收。4:呼叫拦截5:短信收发拦截
  950. "release_cause": 16, 释放原因。请根据编号在释放原因中查看。
  951. "control_type": "CONTINUE",
  952. "release_time": "2020-12-21 17:24:09" 被叫挂断时间。release_time和start_time之差表示通话时长, 如果结果为0,说明呼叫未接通
  953. }]
  954. *
  955. */
  956. public function SecretReport(){
  957. $req = request()->post();
  958. $callids = CallLog::where('call_id',$req[0]['call_id'])->first();
  959. try {
  960. if ($callids){
  961. if($req[0]['release_dir']==0||(strtotime($req[0]['release_time'])-strtotime($req[0]['start_time']))==0||$req[0]['unconnected_cause']!=0){
  962. CallLog::where('call_id',$req[0]['call_id'])->delete();
  963. }else{
  964. // 修改信息
  965. $axbId = Axb::where(['xphone'=>$req[0]['called_display_no'],'subs_id'=>$req[0]['sub_id']])->first();
  966. if($axbId){
  967. $where['docter_id'] = $axbId['docter_id'];
  968. $where['user_id'] = $axbId['user_id'];
  969. $where['product_type'] =1;
  970. $where['order_status'] =3;
  971. $where['payment_status'] =2;
  972. $order_id = Order::where($where)->first();
  973. if ($order_id){
  974. $order_id = $order_id->id;
  975. $save_data = [];
  976. $save_data['order_id'] = $order_id;
  977. $save_data['ring_time'] = $req[0]['release_time'];
  978. $save_data['docter_id'] = $axbId['docter_id'];
  979. $save_data['release_dir'] = $req[0]['release_dir'];
  980. $save_data['talk_time'] = strtotime($req[0]['release_time'])-strtotime($req[0]['start_time']);
  981. $save_data['text'] = json_encode($req,JSON_UNESCAPED_UNICODE);
  982. // 解除号码绑定,并且删除数据库绑定信息
  983. // $this->unLokPhone($req[0]['called_display_no'],$req[0]['sub_id']);
  984. // Axb::where(['subs_id'=>$req[0]['sub_id']])->delete();
  985. CallLog::where('call_id',$req[0]['call_id'])->update($save_data);
  986. return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
  987. }
  988. }
  989. }
  990. }
  991. }catch (\Exception $e){
  992. CallLog::create(['text'=>json_encode($e->getFile().$e->getCode().$e->getMessage(),JSON_UNESCAPED_UNICODE)]);
  993. }catch (\PDOException $e){
  994. CallLog::create(['text'=>json_encode($e->getFile().$e->getCode().$e->getMessage(),JSON_UNESCAPED_UNICODE)]);
  995. }
  996. }
  997. /**
  998. * goEasy聊天记录存入数据库
  999. * * @return false|string
  1000. */
  1001. public function easyMessage(){
  1002. $req = request()->post();
  1003. try {
  1004. $data = json_decode($req['content'],true);
  1005. $ImList = ImMessage::get();
  1006. $newList=[];
  1007. if ($ImList){
  1008. foreach ($ImList as $k=>$v){
  1009. $newList[$k] = $v['messageId'];
  1010. }
  1011. }
  1012. $list = [];
  1013. if($data){
  1014. foreach ($data as $k=>$v){
  1015. if(!in_array($v['messageId'],$newList)){
  1016. $list[$k]['messageId'] = $v['messageId'];
  1017. $list[$k]['type'] = $v['type'];
  1018. $list[$k]['senderId'] = $v['senderId'];
  1019. $list[$k]['receiverId'] = $v['receiverId'];
  1020. $list[$k]['timestamp'] = $v['timestamp'];
  1021. $list[$k]['payload'] = $v['payload'];
  1022. $list[$k]['text'] = $req['content'];
  1023. $list[$k]['create_time'] = time();
  1024. }
  1025. }
  1026. ImMessage::insert($list);
  1027. }else{
  1028. $list['text'] = $req['content'];
  1029. ImMessage::create($list);
  1030. }
  1031. return json_encode(['code'=>200,'content'=>'success']);
  1032. }catch (\Exception $e){
  1033. ImMessage::create(['text'=>json_encode($e->getFile().'的第 '.$e->getLine().'行报错:'.$e->getMessage(),true)]);
  1034. }catch (\PDOException $e){
  1035. ImMessage::create(['text'=>json_encode($e->getFile().'的第 '.$e->getLine().'行报错:'.$e->getMessage(),true)]);
  1036. }
  1037. }
  1038. }