CommonController.php 43 KB

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