CommonController.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. $list = [];
  1094. $ImList = ImMessage::select('messageId')->groupBy('messageId')->get();
  1095. $newList = [];
  1096. if ($ImList) {
  1097. foreach ($ImList as $k => $v) {
  1098. $newList[$k] = $v['messageId'];
  1099. }
  1100. }
  1101. foreach ($data as $k => $v) {
  1102. if (!in_array($v['messageId'], $newList)) {
  1103. $list[$k]['messageId'] = $v['messageId'];
  1104. $list[$k]['type'] = $v['type'];
  1105. $list[$k]['senderId'] = $v['senderId'];
  1106. $list[$k]['receiverId'] = $v['receiverId'];
  1107. $list[$k]['timestamp'] = $v['timestamp'];
  1108. $list[$k]['payload'] = $v['payload'];
  1109. $list[$k]['text'] = $req['content'];
  1110. $list[$k]['create_time'] = time();
  1111. if (substr($v['senderId'], 0, 6) == 'member') {
  1112. // 说明是用户给医生发的, 就给医生端发消息
  1113. $docter_id = substr($v['receiverId'], 7);
  1114. $docter = Docter::where('id', $docter_id)->first()->toArray();
  1115. $user_id = substr($v['senderId'], 7);
  1116. $user = User::where('id', $user_id)->first()->toArray();
  1117. $send_time = date('Y-m-d H:i', round($v['timestamp'] / 1000));
  1118. if ($v['type'] != 'text') {
  1119. $text = '图片语音类消息';
  1120. } else {
  1121. $payload = json_decode($v['payload'], true);
  1122. $text = !empty($payload['text']) ? $payload['text'] : '文字类消息';
  1123. }
  1124. $official_arr = [$docter['openid'], $user['nickname'], $text, $send_time];
  1125. send_wechat_message_to_docter(12, $official_arr);
  1126. } else {
  1127. // 说明是医生给用户发的, 就给用户端发消息
  1128. $this->ReplyReminder(substr($v['senderId'], 7), substr($v['receiverId'], 7));
  1129. }
  1130. }
  1131. }
  1132. if ($list){
  1133. $add = ImMessage::insert($list);
  1134. order_trace(['聊天信息入库'=>$add, '请求参数' => $list ?? ''], 'info');
  1135. }
  1136. return json_encode(['code' => 200, 'content' => 'success']);
  1137. } catch (\Exception $e) {
  1138. order_trace(['聊天信息报错'=>$e->getFile() . '的第 ' . $e->getLine() . '行报错:' . $e->getMessage(), '请求参数' => $data ?? '','返回参数'=>$list], 'error');
  1139. } catch (\PDOException $e) {
  1140. order_trace(['聊天信息报错'=>$e->getFile() . '的第 ' . $e->getLine() . '行报错:' . $e->getMessage(), '请求参数' => $data ?? '','返回参数'=>$list], 'error');
  1141. }
  1142. }
  1143. return json_encode(['code' => 200, 'content' => 'success']);
  1144. }
  1145. /**
  1146. * 消息回复提醒(医生)
  1147. * @param $order_id
  1148. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  1149. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  1150. * @throws \GuzzleHttp\Exception\GuzzleException
  1151. */
  1152. public function ReplyReminder($docter_id,$user_id){
  1153. $list = Docter::with(['DocterOrganization','organization'])->where(['id'=>$docter_id])->first();
  1154. $user = User::where('id',$user_id)->first();
  1155. if ($list){
  1156. order_trace(['消息回复2', '请求参数' => $docter_id.'-'.$user_id ?? '', 'list数据' => $list->toArray() ?? ''], 'info');
  1157. order_trace(['消息回复3', '请求参数' => $docter_id.'-'.$user_id ?? '', 'list数据' => $user ?? ''], 'info');
  1158. if ($user['openid']){
  1159. $send = send_wechatSubscription_message('reply_reminder',[
  1160. $user['openid'],
  1161. "pages/index/index",
  1162. $list['organization'][0]['name'],
  1163. $list['name'],
  1164. $user['nickname'],
  1165. ],'wechat_small_program');
  1166. }
  1167. }
  1168. }
  1169. /**
  1170. * 删除图片
  1171. * @return \Illuminate\Http\JsonResponse
  1172. * @throws \Illuminate\Validation\ValidationException
  1173. */
  1174. public function delFile()
  1175. {
  1176. $req = request()->post();
  1177. $this->validate(request(), [
  1178. 'url' => 'required|url'
  1179. ]);
  1180. $tem = parse_url($req['url']);
  1181. $allPath = public_path() . $tem['path'];
  1182. unlink($allPath);
  1183. return out();
  1184. }
  1185. public function contactUsPhone(){
  1186. $phone = SystemConfig::where('key','contact_us_phone')->value('value');
  1187. return out($phone);
  1188. }
  1189. }