CommonController.php 43 KB

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