CommonController.php 44 KB

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