CommonController.php 43 KB

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