CommonController.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
  914. die();
  915. // 开始json
  916. $req = request()->post();
  917. // 首先创建记录
  918. try {
  919. $data = [];
  920. $data['call_time'] = $req[0]['call_time'];
  921. $data['call_type'] = $req[0]['call_type'];
  922. $data['aphone'] = $req[0]['phone_no'];
  923. $data['bphone'] = $req[0]['peer_no'];
  924. $data['call_id'] = $req[0]['call_id'];
  925. $data['secret_no'] = $req[0]['called_display_no'];
  926. $data['sub_id'] = $req[0]['sub_id'];
  927. CallLog::create($data);
  928. }catch (\Exception $e){
  929. var_dump($e->getFile().$e->getLine().$e->getMessage());
  930. }catch (\PDOException $e){
  931. var_dump($e->getFile().$e->getLine().$e->getMessage());
  932. }
  933. return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
  934. }
  935. /**
  936. * 电话挂断时候回调数据!
  937. * @return false|string
  938. * 回调数据
  939. * [{
  940. "phone_no": "18831138292", 主叫号码
  941. "pool_key": "FC100000115024469", 对应的号池Key。
  942. "city": "昆明",
  943. "sub_id": 1000027052283144, 通话对应的三元组的绑定关系ID。
  944. "unconnected_cause": 0, 0表示正常通话,1表示黑名单拦截,2表示无绑定关系,3表示呼叫限制,4表示其他
  945. "call_time": "2020-12-21 17:23:56", 主叫拨打时间
  946. "call_out_time": "2020-12-21 17:23:56", 呼叫由X送给B端局的时间
  947. "peer_no": "15222021008", AXB中的B号码或者N号码
  948. "called_display_no": "17052201941", 被叫显号
  949. "release_dir": 2, 通话释放方向。0表示平台释放,1表示主叫挂断,2表示被叫挂断
  950. "ring_time": "2020-12-21 17:23:56", 呼叫送被叫端局时,被叫端局响应的时间。
  951. "call_id": "31343639616333323735", 唯一标识一通通话记录的ID。
  952. "start_time": "2020-12-21 17:24:05", 被叫接听时间
  953. "free_ring_time": "2020-12-21 17:24:03",被叫手机真实的振铃时间。free_ring_time 大于call_out_time表示被叫真实发生了振铃事件。free_ring_time 和call_out_time相等表示未振铃。
  954. "partner_key": "FC100000115024469",
  955. "control_msg": "OK",
  956. "id": 1007221453890,
  957. "secret_no": "17052201941", AXB中的X号码
  958. "call_type": 0, 呼叫类型,包括:0:主叫,即phone_no打给peer_no。1:被叫,即peer_no打给phone_no。2:短信发送。3:短信接收。4:呼叫拦截5:短信收发拦截
  959. "release_cause": 16, 释放原因。请根据编号在释放原因中查看。
  960. "control_type": "CONTINUE",
  961. "release_time": "2020-12-21 17:24:09" 被叫挂断时间。release_time和start_time之差表示通话时长, 如果结果为0,说明呼叫未接通
  962. }]
  963. *
  964. */
  965. public function SecretReport(){
  966. return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
  967. die();
  968. $req = request()->post();
  969. $callids = CallLog::where('call_id',$req[0]['call_id'])->first();
  970. try {
  971. if ($callids){
  972. if($req[0]['release_dir']==0||(strtotime($req[0]['release_time'])-strtotime($req[0]['start_time']))==0||$req[0]['unconnected_cause']!=0){
  973. CallLog::where('call_id',$req[0]['call_id'])->delete();
  974. }else{
  975. // 修改信息
  976. $axbId = Axb::where(['xphone'=>$req[0]['called_display_no'],'subs_id'=>$req[0]['sub_id']])->first();
  977. if($axbId){
  978. $where['docter_id'] = $axbId['docter_id'];
  979. $where['user_id'] = $axbId['user_id'];
  980. $where['product_type'] =1;
  981. $where['order_status'] =3;
  982. $where['payment_status'] =2;
  983. $order_id = Order::where($where)->first();
  984. if ($order_id){
  985. $order_id = $order_id->id;
  986. $save_data = [];
  987. $save_data['order_id'] = $order_id;
  988. $save_data['ring_time'] = $req[0]['release_time'];
  989. $save_data['docter_id'] = $axbId['docter_id'];
  990. $save_data['release_dir'] = $req[0]['release_dir'];
  991. $save_data['talk_time'] = strtotime($req[0]['release_time'])-strtotime($req[0]['start_time']);
  992. $save_data['text'] = json_encode($req,JSON_UNESCAPED_UNICODE);
  993. // 解除号码绑定,并且删除数据库绑定信息
  994. // $this->unLokPhone($req[0]['called_display_no'],$req[0]['sub_id']);
  995. // Axb::where(['subs_id'=>$req[0]['sub_id']])->delete();
  996. CallLog::where('call_id',$req[0]['call_id'])->update($save_data);
  997. return json_encode(['code'=>0,'msg'=>"成功"],JSON_UNESCAPED_UNICODE);
  998. }
  999. }
  1000. }
  1001. }
  1002. }catch (\Exception $e){
  1003. CallLog::create(['text'=>json_encode($e->getFile().$e->getCode().$e->getMessage(),JSON_UNESCAPED_UNICODE)]);
  1004. }catch (\PDOException $e){
  1005. CallLog::create(['text'=>json_encode($e->getFile().$e->getCode().$e->getMessage(),JSON_UNESCAPED_UNICODE)]);
  1006. }
  1007. }
  1008. /**
  1009. * goEasy聊天记录存入数据库
  1010. * * @return false|string
  1011. */
  1012. public function easyMessage(){
  1013. $req = request()->post();
  1014. try {
  1015. $data = json_decode($req['content'],true);
  1016. $ImList = ImMessage::get();
  1017. $newList=[];
  1018. if ($ImList){
  1019. foreach ($ImList as $k=>$v){
  1020. $newList[$k] = $v['messageId'];
  1021. }
  1022. }
  1023. $list = [];
  1024. if($data){
  1025. foreach ($data as $k=>$v){
  1026. if(!in_array($v['messageId'],$newList)){
  1027. $list[$k]['messageId'] = $v['messageId'];
  1028. $list[$k]['type'] = $v['type'];
  1029. $list[$k]['senderId'] = $v['senderId'];
  1030. $list[$k]['receiverId'] = $v['receiverId'];
  1031. $list[$k]['timestamp'] = $v['timestamp'];
  1032. $list[$k]['payload'] = $v['payload'];
  1033. $list[$k]['text'] = $req['content'];
  1034. $list[$k]['create_time'] = time();
  1035. }
  1036. }
  1037. ImMessage::insert($list);
  1038. }else{
  1039. $list['text'] = $req['content'];
  1040. ImMessage::create($list);
  1041. }
  1042. return json_encode(['code'=>200,'content'=>'success']);
  1043. }catch (\Exception $e){
  1044. ImMessage::create(['text'=>json_encode($e->getFile().'的第 '.$e->getLine().'行报错:'.$e->getMessage(),true)]);
  1045. }catch (\PDOException $e){
  1046. ImMessage::create(['text'=>json_encode($e->getFile().'的第 '.$e->getLine().'行报错:'.$e->getMessage(),true)]);
  1047. }
  1048. }
  1049. }