CommonController.php 48 KB

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