CommonController.php 45 KB

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