CommonController.php 48 KB

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