CommonController.php 42 KB

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