CommonController.php 42 KB

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