Appointment.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Sms as SmsLib;
  5. use app\common\model\AppointmentEmailLog as AppointmentEmailLogModel;
  6. use app\common\model\AppointmentRecord as AppointmentRecordModel;
  7. use app\common\model\AppointmentSmsLog as AppointmentSmsLogModel;
  8. use think\facade\Db;
  9. use think\facade\Event;
  10. use think\facade\Validate;
  11. class Appointment extends Backend
  12. {
  13. protected $modelClass = null;
  14. protected $modelValidate = false;
  15. protected function initialize()
  16. {
  17. parent::initialize();
  18. $this->modelClass = new AppointmentRecordModel();
  19. $this->assign('statusList', $this->modelClass->getStatusList());
  20. $this->assign('submitSmsStatusList', $this->modelClass->getSubmitSmsStatusList());
  21. $this->assign('batchSmsStatusList', $this->modelClass->getBatchSmsStatusList());
  22. $this->assign('notifyChannelList', $this->modelClass->getNotifyChannelList());
  23. $activityList = Db::name('cms_yuyue')->field('id,title')->order('id desc')->select()->toArray();
  24. $this->assign('activityList', $activityList);
  25. }
  26. protected function getTranslatedActivity(int $activityId, string $lang): array
  27. {
  28. $lang = strtolower(trim($lang));
  29. if ($lang === '') {
  30. $lang = 'cn';
  31. }
  32. $query = Db::name('cms_yuyue_data')->where('did', $activityId);
  33. $row = (clone $query)->where('lang', $lang)->find();
  34. if (!$row && $lang !== 'cn') {
  35. $row = (clone $query)->where('lang', 'cn')->find();
  36. }
  37. return is_array($row) ? $row : [];
  38. }
  39. protected function loadActivityConfig(int $activityId, string $lang): array
  40. {
  41. $activity = Db::name('cms_yuyue')->where('id', $activityId)->find();
  42. if (!is_array($activity) || empty($activity)) {
  43. return [];
  44. }
  45. $translatedActivity = $this->getTranslatedActivity($activityId, $lang);
  46. if (is_array($translatedActivity) && !empty($translatedActivity)) {
  47. $activity = array_merge($activity, $translatedActivity);
  48. }
  49. return $activity;
  50. }
  51. protected function sendAppointmentEmail(string $email, string $subject, string $content): array
  52. {
  53. try {
  54. $config = get_addon_config('phpmailer');
  55. if (!$config) {
  56. return [false, 'NO_CONFIG', '未找到 phpmailer 插件配置'];
  57. }
  58. $mailer = new \addons\phpmailer\library\Mailer($config);
  59. $result = $mailer->to($email)->subject($subject)->message($content, true)->send();
  60. if ($result) {
  61. return [true, 'SUCCESS', '邮件发送成功'];
  62. }
  63. return [false, 'FAIL', $mailer->getError() ?: '邮件发送失败'];
  64. } catch (\Throwable $e) {
  65. return [false, 'EXCEPTION', $e->getMessage()];
  66. }
  67. }
  68. public function index()
  69. {
  70. if ($this->request->isAjax()) {
  71. $page = (int)$this->request->param('page/d', 1);
  72. $limit = (int)$this->request->param('limit/d', 20);
  73. $sort = (string)$this->request->param('sort/s', 'id');
  74. $order = (string)$this->request->param('order/s', 'desc');
  75. $activityId = (int)$this->request->param('activity_id/d', 0);
  76. $mobile = trim((string)$this->request->param('mobile/s', ''));
  77. $email = trim((string)$this->request->param('email/s', ''));
  78. $lang = trim((string)$this->request->param('lang/s', ''));
  79. $notifyChannel = trim((string)$this->request->param('notify_channel/s', ''));
  80. $status = $this->request->param('status/s', '');
  81. $appointmentAutoSms = $this->request->param('appointment_auto_sms/s', '');
  82. $submitSmsStatus = $this->request->param('submit_sms_status/s', '');
  83. $batchSmsStatus = $this->request->param('batch_sms_status/s', '');
  84. $query = $this->modelClass;
  85. if ($activityId > 0) {
  86. $query = $query->where('activity_id', $activityId);
  87. }
  88. if ($appointmentAutoSms !== '') {
  89. $activityIds = Db::name('cms_yuyue')->where('appointment_auto_sms', (int)$appointmentAutoSms)->column('id');
  90. $activityIds = $activityIds ?: [0];
  91. $query = $query->where('activity_id', 'in', $activityIds);
  92. }
  93. if ($mobile !== '') {
  94. $query = $query->where('mobile', 'like', '%' . $mobile . '%');
  95. }
  96. if ($email !== '') {
  97. $query = $query->where('email', 'like', '%' . $email . '%');
  98. }
  99. if ($lang !== '') {
  100. $query = $query->where('lang', $lang);
  101. }
  102. if ($notifyChannel !== '') {
  103. $query = $query->where('notify_channel', $notifyChannel);
  104. }
  105. if ($status !== '') {
  106. $query = $query->where('status', (int)$status);
  107. }
  108. if ($submitSmsStatus !== '') {
  109. $query = $query->where('submit_sms_status', (int)$submitSmsStatus);
  110. }
  111. if ($batchSmsStatus !== '') {
  112. $query = $query->where('batch_sms_status', (int)$batchSmsStatus);
  113. }
  114. $count = $query->count();
  115. $data = $query->order($sort, $order)->page($page, $limit)->select();
  116. $activityMap = Db::name('cms_yuyue')->column('appointment_auto_sms', 'id');
  117. foreach ($data as $item) {
  118. $item->appointment_auto_sms = isset($activityMap[$item->activity_id]) ? (int)$activityMap[$item->activity_id] : 0;
  119. }
  120. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  121. }
  122. return $this->fetch();
  123. }
  124. public function sendSms()
  125. {
  126. if (!$this->request->isPost()) {
  127. $this->error('请求方式错误');
  128. }
  129. $ids = $this->request->post('ids/a', []);
  130. if (!$ids) {
  131. $this->error('请先选择预约记录');
  132. }
  133. if (!Event::hasListener('sms_notice')) {
  134. $this->error('未检测到短信通知插件监听 sms_notice');
  135. }
  136. $rows = $this->modelClass->where('id', 'in', $ids)->where('notify_channel', 'sms')->select();
  137. if ($rows->isEmpty()) {
  138. $this->error('未找到可发送的短信预约记录');
  139. }
  140. $success = 0;
  141. $failed = 0;
  142. $skipped = 0;
  143. foreach ($rows as $row) {
  144. if ((int)$row->batch_sms_status === 1) {
  145. $skipped++;
  146. continue;
  147. }
  148. $now = time();
  149. $activityConfig = $this->loadActivityConfig((int)$row->activity_id, (string)$row->lang);
  150. if (empty($activityConfig)) {
  151. AppointmentSmsLogModel::create([
  152. 'record_id' => $row->id,
  153. 'activity_id' => $row->activity_id,
  154. 'mobile' => $row->mobile,
  155. 'sms_type' => 'appointment_batch',
  156. 'template_code' => '',
  157. 'content' => '',
  158. 'status' => 2,
  159. 'response_code' => 'NO_ACTIVITY',
  160. 'response_msg' => '活动配置不存在,无法发送统一短信',
  161. 'retry_count' => 0,
  162. 'send_time' => 0,
  163. 'createtime' => $now,
  164. 'updatetime' => $now,
  165. ]);
  166. $row->batch_sms_status = 2;
  167. $row->updatetime = $now;
  168. $row->save();
  169. $failed++;
  170. continue;
  171. }
  172. $activityTitle = trim((string)($activityConfig['title'] ?? $row->activity_title ?? ''));
  173. $batchTpl = trim((string)($activityConfig['batch_sms_content'] ?? ''));
  174. $content = $batchTpl !== ''
  175. ? str_replace('{title}', $activityTitle, $batchTpl)
  176. : '您预约的【' . $activityTitle . '】已有最新动态,欢迎及时关注官方发布信息。';
  177. $sendResult = SmsLib::notice($row->mobile, $content, null);
  178. AppointmentSmsLogModel::create([
  179. 'record_id' => $row->id,
  180. 'activity_id' => $row->activity_id,
  181. 'mobile' => $row->mobile,
  182. 'sms_type' => 'appointment_batch',
  183. 'template_code' => '',
  184. 'content' => $content,
  185. 'status' => $sendResult ? 1 : 2,
  186. 'response_code' => $sendResult ? 'SUCCESS' : 'FAIL',
  187. 'response_msg' => $sendResult ? '后台批量短信发送成功' : '后台批量短信发送失败',
  188. 'retry_count' => 0,
  189. 'send_time' => $sendResult ? $now : 0,
  190. 'createtime' => $now,
  191. 'updatetime' => $now,
  192. ]);
  193. $row->activity_title = $activityTitle;
  194. $row->batch_sms_status = $sendResult ? 1 : 2;
  195. $row->batch_sms_time = $sendResult ? $now : $row->batch_sms_time;
  196. $row->updatetime = $now;
  197. $row->save();
  198. $sendResult ? $success++ : $failed++;
  199. }
  200. $this->success('统一发送短信完成:成功 ' . $success . ' 条,失败 ' . $failed . ' 条,跳过 ' . $skipped . ' 条');
  201. }
  202. public function sendEmail()
  203. {
  204. if (!$this->request->isPost()) {
  205. $this->error('请求方式错误');
  206. }
  207. $ids = $this->request->post('ids/a', []);
  208. if (!$ids) {
  209. $this->error('请先选择预约记录');
  210. }
  211. $rows = $this->modelClass->where('id', 'in', $ids)->where('notify_channel', 'email')->select();
  212. if ($rows->isEmpty()) {
  213. $this->error('未找到可发送的邮件预约记录');
  214. }
  215. $success = 0;
  216. $failed = 0;
  217. $skipped = 0;
  218. foreach ($rows as $row) {
  219. if ((int)$row->batch_sms_status === 1) {
  220. $skipped++;
  221. continue;
  222. }
  223. $now = time();
  224. $sendResult = false;
  225. $responseCode = 'FAIL';
  226. $responseMsg = '';
  227. $subject = '';
  228. $content = '';
  229. try {
  230. if (!$row->email || !Validate::is($row->email, 'email')) {
  231. throw new \RuntimeException('邮箱为空或格式不正确');
  232. }
  233. $activityConfig = $this->loadActivityConfig((int)$row->activity_id, (string)$row->lang);
  234. if (empty($activityConfig)) {
  235. throw new \RuntimeException('活动配置不存在,无法发送统一邮件');
  236. }
  237. $activityTitle = trim((string)($activityConfig['title'] ?? $row->activity_title ?? ''));
  238. $batchTpl = trim((string)($activityConfig['batch_sms_content'] ?? ''));
  239. $content = $batchTpl !== ''
  240. ? str_replace('{title}', $activityTitle, $batchTpl)
  241. : 'You reserved [' . $activityTitle . ']. We have updates for you. Please stay tuned.';
  242. $subject = $activityTitle . ' Update Notification';
  243. [$sendResult, $responseCode, $responseMsg] = $this->sendAppointmentEmail($row->email, $subject, $content);
  244. $row->activity_title = $activityTitle;
  245. } catch (\Throwable $e) {
  246. $sendResult = false;
  247. $responseCode = 'EXCEPTION';
  248. $responseMsg = $e->getMessage();
  249. }
  250. AppointmentEmailLogModel::create([
  251. 'record_id' => $row->id,
  252. 'activity_id' => $row->activity_id,
  253. 'email' => $row->email,
  254. 'mail_type' => 'appointment_batch',
  255. 'subject' => $subject,
  256. 'content' => $content,
  257. 'status' => $sendResult ? 1 : 2,
  258. 'response_code' => $responseCode,
  259. 'response_msg' => $responseMsg ?: ($sendResult ? '后台批量邮件发送成功' : '后台批量邮件发送失败'),
  260. 'retry_count' => 0,
  261. 'send_time' => $sendResult ? $now : 0,
  262. 'createtime' => $now,
  263. 'updatetime' => $now,
  264. ]);
  265. $row->batch_sms_status = $sendResult ? 1 : 2;
  266. $row->batch_sms_time = $sendResult ? $now : $row->batch_sms_time;
  267. $row->updatetime = $now;
  268. $row->save();
  269. $sendResult ? $success++ : $failed++;
  270. }
  271. $this->success('统一发送邮件完成:成功 ' . $success . ' 条,失败 ' . $failed . ' 条,跳过 ' . $skipped . ' 条');
  272. }
  273. public function resendSms()
  274. {
  275. if (!$this->request->isPost()) {
  276. $this->error('请求方式错误');
  277. }
  278. $id = (int)$this->request->post('id/d', 0);
  279. if ($id <= 0) {
  280. $this->error('参数错误');
  281. }
  282. $row = $this->modelClass->where('id', $id)->find();
  283. if (!$row) {
  284. $this->error('预约记录不存在');
  285. }
  286. $activity = $this->loadActivityConfig((int)$row->activity_id, (string)$row->lang);
  287. if (empty($activity)) {
  288. $this->error('活动配置不存在,无法补发通知');
  289. }
  290. $activityTitle = trim((string)($activity['title'] ?? $row->activity_title ?? ''));
  291. $submitTpl = trim((string)($activity['submit_sms_content'] ?? ''));
  292. $now = time();
  293. if ($row->notify_channel === 'email') {
  294. $content = $submitTpl !== ''
  295. ? str_replace('{title}', $activityTitle, $submitTpl)
  296. : 'You have successfully reserved [' . $activityTitle . ']. We will notify you by email when updates are available.';
  297. $subject = $activityTitle . ' Reservation Confirmation';
  298. [$sendResult, $responseCode, $responseMsg] = $this->sendAppointmentEmail($row->email, $subject, $content);
  299. AppointmentEmailLogModel::create([
  300. 'record_id' => $row->id,
  301. 'activity_id' => $row->activity_id,
  302. 'email' => $row->email,
  303. 'mail_type' => 'appointment_resend',
  304. 'subject' => $subject,
  305. 'content' => $content,
  306. 'status' => $sendResult ? 1 : 2,
  307. 'response_code' => $responseCode,
  308. 'response_msg' => $responseMsg ?: ($sendResult ? '后台单条补发邮件成功' : '后台单条补发邮件失败'),
  309. 'retry_count' => 0,
  310. 'send_time' => $sendResult ? $now : 0,
  311. 'createtime' => $now,
  312. 'updatetime' => $now,
  313. ]);
  314. } else {
  315. if (!Event::hasListener('sms_notice')) {
  316. $this->error('未检测到短信通知插件监听 sms_notice');
  317. }
  318. $content = $submitTpl !== ''
  319. ? str_replace('{title}', $activityTitle, $submitTpl)
  320. : '您已成功预约【' . $activityTitle . '】,新品动态发布后我们将第一时间短信通知您。';
  321. $sendResult = SmsLib::notice($row->mobile, $content, null);
  322. AppointmentSmsLogModel::create([
  323. 'record_id' => $row->id,
  324. 'activity_id' => $row->activity_id,
  325. 'mobile' => $row->mobile,
  326. 'sms_type' => 'appointment_resend',
  327. 'template_code' => '',
  328. 'content' => $content,
  329. 'status' => $sendResult ? 1 : 2,
  330. 'response_code' => $sendResult ? 'SUCCESS' : 'FAIL',
  331. 'response_msg' => $sendResult ? '后台单条补发短信成功' : '后台单条补发短信失败',
  332. 'retry_count' => 0,
  333. 'send_time' => $sendResult ? $now : 0,
  334. 'createtime' => $now,
  335. 'updatetime' => $now,
  336. ]);
  337. }
  338. $row->activity_title = $activityTitle;
  339. $row->submit_sms_status = $sendResult ? 1 : 2;
  340. $row->submit_sms_time = $sendResult ? $now : $row->submit_sms_time;
  341. $row->updatetime = $now;
  342. $row->save();
  343. if ($sendResult) {
  344. $this->success($row->notify_channel === 'email' ? '补发邮件成功' : '补发短信成功');
  345. }
  346. $this->error($row->notify_channel === 'email' ? '补发邮件失败' : '补发短信失败');
  347. }
  348. public function del()
  349. {
  350. if (!$this->request->isPost()) {
  351. $this->error('请求方式错误');
  352. }
  353. $ids = $this->request->post('ids/a', []);
  354. if (!$ids) {
  355. $this->error('请先选择要删除的预约记录');
  356. }
  357. $rows = $this->modelClass->where('id', 'in', $ids)->select();
  358. if ($rows->isEmpty()) {
  359. $this->error('未找到可删除的预约记录');
  360. }
  361. Db::startTrans();
  362. try {
  363. AppointmentSmsLogModel::where('record_id', 'in', $ids)->delete();
  364. AppointmentEmailLogModel::where('record_id', 'in', $ids)->delete();
  365. $count = $this->modelClass->where('id', 'in', $ids)->delete();
  366. Db::commit();
  367. } catch (\Throwable $e) {
  368. Db::rollback();
  369. $this->error('删除失败:' . $e->getMessage());
  370. }
  371. $this->success('已删除 ' . $count . ' 条预约记录');
  372. }
  373. public function smslog()
  374. {
  375. $smsModel = new AppointmentSmsLogModel();
  376. if ($this->request->isAjax()) {
  377. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  378. $mobile = trim((string)$this->request->param('mobile/s', ''));
  379. $smsType = trim((string)$this->request->param('sms_type/s', ''));
  380. $status = $this->request->param('status/s', '');
  381. $query = $smsModel->where($where);
  382. if ($mobile !== '') {
  383. $query->where('mobile', 'like', '%' . $mobile . '%');
  384. }
  385. if ($smsType !== '') {
  386. $query->where('sms_type', 'like', '%' . $smsType . '%');
  387. }
  388. if ($status !== '') {
  389. $query->where('status', (int)$status);
  390. }
  391. $count = $query->order($sort, $order)->count();
  392. $data = $query->order($sort, $order)->page($page, $limit)->select();
  393. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  394. }
  395. $this->assign('smsLogStatusList', $smsModel->getStatusList());
  396. return $this->fetch('appointment/smslog');
  397. }
  398. public function emaillog()
  399. {
  400. $emailModel = new AppointmentEmailLogModel();
  401. if ($this->request->isAjax()) {
  402. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  403. $email = trim((string)$this->request->param('email/s', ''));
  404. $recordId = (int)$this->request->param('record_id/d', 0);
  405. $activityId = (int)$this->request->param('activity_id/d', 0);
  406. $mailType = trim((string)$this->request->param('mail_type/s', ''));
  407. $status = $this->request->param('status/s', '');
  408. $sendTimeStart = trim((string)$this->request->param('send_time_start/s', ''));
  409. $sendTimeEnd = trim((string)$this->request->param('send_time_end/s', ''));
  410. $query = $emailModel->where($where);
  411. if ($email !== '') {
  412. $query->where('email', 'like', '%' . $email . '%');
  413. }
  414. if ($recordId > 0) {
  415. $query->where('record_id', $recordId);
  416. }
  417. if ($activityId > 0) {
  418. $query->where('activity_id', $activityId);
  419. }
  420. if ($mailType !== '') {
  421. $query->where('mail_type', 'like', '%' . $mailType . '%');
  422. }
  423. if ($status !== '') {
  424. $query->where('status', (int)$status);
  425. }
  426. if ($sendTimeStart !== '') {
  427. $query->where('send_time', '>=', strtotime($sendTimeStart));
  428. }
  429. if ($sendTimeEnd !== '') {
  430. $query->where('send_time', '<=', strtotime($sendTimeEnd));
  431. }
  432. $count = $query->order($sort, $order)->count();
  433. $data = $query->order($sort, $order)->page($page, $limit)->select();
  434. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  435. }
  436. $this->assign('emailLogStatusList', $emailModel->getStatusList());
  437. return $this->fetch('appointment/emaillog');
  438. }
  439. public function delSmslog()
  440. {
  441. if (!$this->request->isPost()) {
  442. $this->error('请求方式错误');
  443. }
  444. $ids = $this->request->post('ids/a', []);
  445. if (!$ids) {
  446. $this->error('请先选择要删除的短信日志');
  447. }
  448. $rows = AppointmentSmsLogModel::where('id', 'in', $ids)->select();
  449. if ($rows->isEmpty()) {
  450. $this->error('未找到可删除的短信日志');
  451. }
  452. $count = AppointmentSmsLogModel::where('id', 'in', $ids)->delete();
  453. $this->success('已删除 ' . $count . ' 条短信日志');
  454. }
  455. public function delEmaillog()
  456. {
  457. if (!$this->request->isPost()) {
  458. $this->error('请求方式错误');
  459. }
  460. $ids = $this->request->post('ids/a', []);
  461. if (!$ids) {
  462. $this->error('请先选择要删除的邮件日志');
  463. }
  464. $rows = AppointmentEmailLogModel::where('id', 'in', $ids)->select();
  465. if ($rows->isEmpty()) {
  466. $this->error('未找到可删除的邮件日志');
  467. }
  468. $count = AppointmentEmailLogModel::where('id', 'in', $ids)->delete();
  469. $this->success('已删除 ' . $count . ' 条邮件日志');
  470. }
  471. }