CommonController.php 44 KB

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