CommonController.php 42 KB

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