CommonController.php 48 KB

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