Plantask.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use app\common\library\Email;
  6. class Plantask extends Controller
  7. {
  8. //关于本文件里的计划任务
  9. //只有 public 方法,auto_开头的才是计划任务,其他private 方法都是工具方法
  10. ////////////////////////////////////////下面都是计划任务方法///////////////////////////////////////////////////////////////
  11. //明天有课,通知。计划任务10分钟执行一次
  12. public function auto_lesson_slot_notice(){
  13. $starttime = strtotime(date('Y-m-d')) + 86400;
  14. $endtime = $starttime + 86399;
  15. $map = [
  16. 'status' => 0,
  17. 'starttime' => ['BETWEEN',[$starttime,$endtime]],
  18. 'notice_status' => 0,
  19. ];
  20. $task_list = Db::name('lesson_slot')->where($map)->order('starttime asc')->limit(1)->select();
  21. if(empty($task_list)){
  22. echo 'empty';
  23. exit;
  24. }
  25. foreach($task_list as $slot){
  26. //找出这节课时的预约单
  27. $map = [
  28. 'order.order_status' => 10,
  29. 'order.slot_id' => $slot['id'],
  30. ];
  31. $order_list = Db::name('lesson_order')->alias('order')
  32. ->field('lesson.name,lesson.name_en,user.firstname,user.lastname,user.email,user.whatsapp')
  33. ->join('user','order.user_id = user.id','LEFT')
  34. ->join('lesson','order.lesson_id = lesson.id','LEFT')
  35. ->where($map)->order('order.id asc')->select();
  36. if(empty($order_list)){
  37. continue;
  38. }
  39. $coach_name = Db::name('coach')->where('id',$slot['coach_ids'])->value('nickname');
  40. //给这些预约单的用户发邮件
  41. try {
  42. $obj = new Email();
  43. foreach($order_list as $order){
  44. $message =
  45. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  46. We are looking forward to seeing you at the following class tomorrow!<br/>
  47. Class:'.$order['name_en'].'<br/>
  48. Date: '.date('D l Y',$slot['starttime']).'<br/>
  49. Time: '.date('H:i a',$slot['starttime']).'<br/>
  50. Please arrive 10 minutes before the class timing to prepare for the harness fitting!<br/>
  51. We are located at 450 Alexandra Road, #02-01,<br/>
  52. Singapore 119960. For those who are driving, parking is available on level 2 of the building. For those who are taking public transport, the nearest mrt is Labrador Park MRT (7-10 minutes walk) and there is also a bus stop available just outside the building!<br/>
  53. We look forward to seeing you for an amazing session! ❤<br/>
  54. Best Regards,<br/>
  55. Elin Dance Studio<br/>
  56. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:100px;height:100px">
  57. ';
  58. $result = $obj
  59. ->to($order['email'])
  60. ->subject('You have a class tomorrow!')
  61. ->message($message)
  62. ->send();
  63. //发whatsapp
  64. $parameters = [
  65. [
  66. 'type' => 'text',
  67. 'text' => $order['firstname'].' '.$order['lastname'],
  68. ],
  69. [
  70. 'type' => 'text',
  71. 'text' => $order['name_en'],
  72. ],
  73. [
  74. 'type' => 'text',
  75. 'text' => $coach_name,
  76. ],
  77. [
  78. 'type' => 'text',
  79. 'text' => date('D l Y',$slot['starttime']),
  80. ],
  81. [
  82. 'type' => 'text',
  83. 'text' => date('H:i a',$slot['starttime']),
  84. ],
  85. ];
  86. $this->whatapp($order['whatsapp'],'the_class_will_start_tomorrow','en_US',$parameters);
  87. }
  88. } catch (Exception $e) {
  89. }
  90. //这节课时,任务完成
  91. $update = [
  92. 'notice_status' => 1,
  93. ];
  94. Db::name('lesson_slot')->where('id',$slot['id'])->update($update);
  95. }
  96. }
  97. //课程取消,通知。计划任务5分钟执行一次
  98. public function auto_lesson_slot_cancel(){
  99. exit;
  100. $map = [
  101. 'slot.status' => 30,
  102. 'slot.cancel_notice_status' => 0,
  103. ];
  104. $task_list = Db::name('lesson_slot')->alias('slot')
  105. ->field('slot.*,coach.nickname as coach_nickname,coach.email as coach_email,coach.whatsapp as coach_whatsapp,lesson.name_en as lesson_name_en')
  106. ->join('coach','slot.coach_ids = coach.id','LEFT')
  107. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  108. ->where($map)->order('slot.starttime asc')->limit(1)->select();
  109. if(empty($task_list)){
  110. echo 'empty';
  111. exit;
  112. }
  113. foreach($task_list as $slot){
  114. //找出这节课时的预约单
  115. $map = [
  116. 'order.slot_id' => $slot['id'],
  117. ];
  118. $order_list = Db::name('lesson_order')->alias('order')
  119. ->field('user.firstname,user.lastname,user.email,user.whatsapp')
  120. ->join('user','order.user_id = user.id','LEFT')
  121. ->where('(order.jointype = 1 and order.order_status = 10) or (order.jointype = 2 and order.order_status = 0)') //已支付的 或 候补单
  122. ->where($map)->order('order.id asc')->select();
  123. //把教练也加入到发送列表里
  124. $coach_sender = [
  125. 'firstname' => $slot['coach_nickname'],
  126. 'lastname' => '',
  127. 'email' => $slot['coach_email'],
  128. 'whatsapp' => $slot['coach_whatsapp'],
  129. ];
  130. $order_list[] = $coach_sender;
  131. if(empty($order_list)){
  132. continue;
  133. }
  134. //给这些预约单的用户发邮件
  135. try {
  136. $obj = new Email();
  137. foreach($order_list as $order){
  138. $message =
  139. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  140. We regret to inform you that the following class has been cancelled.<br/>
  141. Class:'.$slot['lesson_name_en'].'<br/>
  142. Date: '.date('d F Y',$slot['starttime']).'<br/>
  143. Time: '.date('H:i a',$slot['starttime']).'<br/>
  144. Thank you for your kind understanding and look forward to seeing you in our studio soon!❤<br/>
  145. Best Regards,<br/>
  146. Elin Dance Studio<br/>
  147. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  148. ';
  149. if(!empty($order['email'])){
  150. $result = $obj
  151. ->to($order['email'])
  152. ->subject('Class is Cancelled!')
  153. ->message($message)
  154. ->send();
  155. }
  156. //$coach_name = Db::name('coach')->where('id',$slot['coach_ids'])->value('nickname');
  157. //发whatsapp
  158. $parameters = [
  159. [
  160. 'type' => 'text',
  161. 'text' => $order['firstname'].' '.$order['lastname'],
  162. ],
  163. [
  164. 'type' => 'text',
  165. 'text' => $slot['lesson_name_en'],
  166. ],
  167. /* [
  168. 'type' => 'text',
  169. 'text' => $coach_name,
  170. ],*/
  171. [
  172. 'type' => 'text',
  173. 'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
  174. ],
  175. ];
  176. if(!empty($order['whatsapp'])){
  177. $this->whatapp($order['whatsapp'],'class_cancelled','en_US',$parameters);
  178. }
  179. }
  180. } catch (Exception $e) {
  181. }
  182. //这节课时,任务完成
  183. $update = [
  184. 'cancel_notice_status' => 1,
  185. ];
  186. Db::name('lesson_slot')->where('id',$slot['id'])->update($update);
  187. }
  188. }
  189. //购买新套餐之后,通知。计划任务5分钟执行一次
  190. public function auto_after_buy_package(){
  191. $map = [
  192. 'order.order_status' => 1,
  193. 'order.buy_notice_status' => 0,
  194. 'order.is_gift' => 0,
  195. ];
  196. $task_list = Db::name('package_order')->alias('order')
  197. ->field('order.id,order.starttime,order.endtime,p.name,p.name_en,user.firstname,user.lastname,user.email,user.whatsapp')
  198. ->join('lesson_package p','order.package_id = p.id','LEFT')
  199. ->join('user','order.user_id = user.id','LEFT')
  200. ->where($map)->order('order.paytime asc')->limit(1)->select();
  201. if(empty($task_list)){
  202. echo 'empty';
  203. exit;
  204. }
  205. $obj = new Email();
  206. foreach($task_list as $order){
  207. try {
  208. $message =
  209. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  210. Thank you for your purchase of the '.$order['name_en'].' plan!<br/>
  211. <b>Please note</b>: Plans that are 5 hours and above need to be activated by us! This is so that you can use up your current plans first (if any)!<br/>
  212. When you wish to activate, please send a WhatsApp message to 8879-9689 to activate your package to start booking classes.<br/>
  213. Once activated, your plan’s validity period will begin.<br/>
  214. You can view the unactivated packages in the app.<br/>
  215. We look forward to seeing you soon!❤<br/>
  216. Best Regards,<br/>
  217. Elin Dance Studio<br/>
  218. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  219. ';
  220. //给这些用户发邮件
  221. $result = $obj
  222. ->to($order['email'])
  223. ->subject('Your plan purchase has been received!')
  224. ->message($message)
  225. ->send();
  226. //发whatsapp
  227. $parameters = [
  228. [
  229. 'type' => 'text',
  230. 'text' => $order['firstname'].' '.$order['lastname'],
  231. ],
  232. [
  233. 'type' => 'text',
  234. 'text' => $order['name_en'],
  235. ],
  236. /*[
  237. 'type' => 'text',
  238. 'text' => date('Y-m-d H:i',$order['starttime']),
  239. ],
  240. [
  241. 'type' => 'text',
  242. 'text' => date('Y-m-d H:i',$order['endtime']),
  243. ],
  244. [
  245. 'type' => 'text',
  246. 'text' => $order['firstname'].' '.$order['lastname'],
  247. ],*/
  248. ];
  249. $this->whatapp($order['whatsapp'],'buy_new_package','en_US',$parameters);
  250. } catch (Exception $e) {
  251. }
  252. //任务完成
  253. $update = [
  254. 'buy_notice_status' => 1,
  255. ];
  256. Db::name('package_order')->where('id',$order['id'])->update($update);
  257. }
  258. }
  259. //套餐将要到期,一个月,两周,一周,逐级通知,提醒三次。计划任务分别一小时执行一次
  260. public function auto_package_order_notice_mon(){
  261. $map = [
  262. 'order.order_status' => 1,
  263. 'order.use_status' => 1,
  264. 'order.remain' => ['gt',0],
  265. 'order.endtime' => ['lt',time()+(86400*30)],
  266. 'order.notice_status' => 0,
  267. ];
  268. $task_list = Db::name('package_order')->alias('order')
  269. ->field('order.id,order.endtime,p.name,p.name_en,order.remain,user.firstname,user.lastname,user.email,user.whatsapp')
  270. ->join('lesson_package p','order.package_id = p.id','LEFT')
  271. ->join('user','order.user_id = user.id','LEFT')
  272. ->where($map)->order('endtime asc')->limit(2)->select();
  273. if(empty($task_list)){
  274. echo 'empty';
  275. exit;
  276. }
  277. $obj = new Email();
  278. foreach($task_list as $order){
  279. try {
  280. $message =
  281. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  282. We wanted to let you know that the following plan is set to expire in one month’s time!<br/>
  283. Plan: '.$order['name_en'].'<br/>
  284. Expiry Date: '.date('d F Y',$order['endtime']).'<br/>
  285. Please note that any unutilised hours will automatically be forfeited in the system. Book your next session now and continue working towards those amazing goals you have set for yourself!<br/>
  286. If you would like to sign up for another plan, you can contact us at 8879-9689 or check on the app!❤<br/>
  287. Best Regards,<br/>
  288. Elin Dance Studio<br/>
  289. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  290. ';
  291. //给这些用户发邮件
  292. $result = $obj
  293. ->to($order['email'])
  294. ->subject('Your Membership Plan is Expiring in One Month’s Time!')
  295. ->message($message)
  296. ->send();
  297. //发whatsapp
  298. $parameters = [
  299. [
  300. 'type' => 'text',
  301. 'text' => $order['firstname'].' '.$order['lastname'],
  302. ],
  303. [
  304. 'type' => 'text',
  305. 'text' => $order['name_en'],
  306. ],
  307. [
  308. 'type' => 'text',
  309. 'text' => 'one month’s time',
  310. ],
  311. [
  312. 'type' => 'text',
  313. 'text' => date('D l Y',$order['endtime']),
  314. ],
  315. [
  316. 'type' => 'text',
  317. 'text' => ''.$order['remain'].'',
  318. ],
  319. ];
  320. $this->whatapp($order['whatsapp'],'package_expiration_reminder','en_US',$parameters);
  321. } catch (Exception $e) {
  322. }
  323. //任务完成
  324. $update = [
  325. 'notice_status' => 1,//月通知已发送
  326. ];
  327. Db::name('package_order')->where('id',$order['id'])->update($update);
  328. }
  329. }
  330. public function auto_package_order_notice_2week(){
  331. $map = [
  332. 'order.order_status' => 1,
  333. 'order.use_status' => 1,
  334. 'order.remain' => ['gt',0],
  335. 'order.endtime' => ['lt',time()+(86400*14)],
  336. 'order.notice_status' => 1,
  337. ];
  338. $task_list = Db::name('package_order')->alias('order')
  339. ->field('order.id,order.endtime,p.name,p.name_en,order.remain,user.firstname,user.lastname,user.email,user.whatsapp')
  340. ->join('lesson_package p','order.package_id = p.id','LEFT')
  341. ->join('user','order.user_id = user.id','LEFT')
  342. ->where($map)->order('endtime asc')->limit(2)->select();
  343. if(empty($task_list)){
  344. echo 'empty';
  345. exit;
  346. }
  347. $obj = new Email();
  348. foreach($task_list as $order){
  349. try {
  350. $message =
  351. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  352. We wanted to let you know that the following plan is set to expire in two week’s time!<br/>
  353. Plan: '.$order['name_en'].'<br/>
  354. Expiry Date: '.date('d F Y',$order['endtime']).'<br/>
  355. Please note that any unutilised hours will automatically be forfeited in the system. Book your next session now and continue working towards those amazing goals you have set for yourself!<br/>
  356. If you would like to sign up for another plan, you can contact us at 8879-9689 or check on the app!❤<br/>
  357. Best Regards,<br/>
  358. Elin Dance Studio<br/>
  359. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  360. ';
  361. //给这些用户发邮件
  362. $result = $obj
  363. ->to($order['email'])
  364. ->subject('Your Membership Plan is Expiring in Two Week’s Time!')
  365. ->message($message)
  366. ->send();
  367. //发whatsapp
  368. $parameters = [
  369. [
  370. 'type' => 'text',
  371. 'text' => $order['firstname'].' '.$order['lastname'],
  372. ],
  373. [
  374. 'type' => 'text',
  375. 'text' => $order['name_en'],
  376. ],
  377. [
  378. 'type' => 'text',
  379. 'text' => '2 week’s time',
  380. ],
  381. [
  382. 'type' => 'text',
  383. 'text' => date('D l Y',$order['endtime']),
  384. ],
  385. [
  386. 'type' => 'text',
  387. 'text' => ''.$order['remain'].'',
  388. ],
  389. ];
  390. $this->whatapp($order['whatsapp'],'package_expiration_reminder','en_US',$parameters);
  391. } catch (Exception $e) {
  392. }
  393. //任务完成
  394. $update = [
  395. 'notice_status' => 2,//2周通知已发送
  396. ];
  397. Db::name('package_order')->where('id',$order['id'])->update($update);
  398. }
  399. }
  400. public function auto_package_order_notice_1week(){
  401. $map = [
  402. 'order.order_status' => 1,
  403. 'order.use_status' => 1,
  404. 'order.remain' => ['gt',0],
  405. 'order.endtime' => ['lt',time()+(86400*7)],
  406. 'order.notice_status' => 2,
  407. ];
  408. $task_list = Db::name('package_order')->alias('order')
  409. ->field('order.id,order.endtime,p.name,p.name_en,order.remain,user.firstname,user.lastname,user.email,user.whatsapp')
  410. ->join('lesson_package p','order.package_id = p.id','LEFT')
  411. ->join('user','order.user_id = user.id','LEFT')
  412. ->where($map)->order('endtime asc')->limit(2)->select();
  413. if(empty($task_list)){
  414. echo 'empty';
  415. exit;
  416. }
  417. $obj = new Email();
  418. foreach($task_list as $order){
  419. try {
  420. $message =
  421. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  422. We wanted to let you know that the following plan is set to expire in one week’s time!<br/>
  423. Plan: '.$order['name_en'].'<br/>
  424. Expiry Date: '.date('d F Y',$order['endtime']).'<br/>
  425. Please note that any unutilised hours will automatically be forfeited in the system. Book your next session now and continue working towards those amazing goals you have set for yourself!<br/>
  426. If you would like to sign up for another plan, you can contact us at 8879-9689 or check on the app!❤<br/>
  427. Best Regards,<br/>
  428. Elin Dance Studio<br/>
  429. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  430. ';
  431. //给这些用户发邮件
  432. $result = $obj
  433. ->to($order['email'])
  434. ->subject('Your Membership Plan is Expiring in One Week’s Time!')
  435. ->message($message)
  436. ->send();
  437. //发whatsapp
  438. $parameters = [
  439. [
  440. 'type' => 'text',
  441. 'text' => $order['firstname'].' '.$order['lastname'],
  442. ],
  443. [
  444. 'type' => 'text',
  445. 'text' => $order['name_en'],
  446. ],
  447. [
  448. 'type' => 'text',
  449. 'text' => '1 week’s time',
  450. ],
  451. [
  452. 'type' => 'text',
  453. 'text' => date('D l Y',$order['endtime']),
  454. ],
  455. [
  456. 'type' => 'text',
  457. 'text' => ''.$order['remain'].'',
  458. ],
  459. ];
  460. $this->whatapp($order['whatsapp'],'package_expiration_reminder','en_US',$parameters);
  461. } catch (Exception $e) {
  462. }
  463. //任务完成
  464. $update = [
  465. 'notice_status' => 3,//1周通知已发送
  466. ];
  467. Db::name('package_order')->where('id',$order['id'])->update($update);
  468. }
  469. }
  470. /**
  471. * 复制本周的课程表
  472. */
  473. public function auto_slot_copyweek_old(){
  474. $week = date('w');
  475. if($week != 1){
  476. //exit;
  477. }
  478. //先查找 下周有没有数据
  479. $starttime = strtotime('this week Monday') + (86400*7);
  480. $endtime = $starttime + (86400*7);
  481. $check = Db::name('lesson_slot')->where('starttime','BETWEEN',[$starttime,$endtime])->find();
  482. if($check){
  483. echo '下周有数据了';
  484. exit;
  485. }
  486. echo '准备复制';
  487. //拿上周,复制到下周
  488. $starttime = strtotime('this week Monday') - (86400*7); // 获取本周一的时间戳
  489. $endtime = $starttime + (86400*7);
  490. $list = Db::name('lesson_slot')->where('is_show',1)->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  491. if(empty($list)){
  492. echo 'empty';
  493. exit;
  494. }
  495. foreach($list as $key => &$val){
  496. unset($val['id']);
  497. $val['starttime'] = $val['starttime'] + (86400*14);
  498. $val['endtime'] = $val['endtime'] + (86400*14);
  499. $val['bookednum'] = 0;
  500. $val['status'] = 0;
  501. $val['notice_status'] = 0;
  502. $val['finishtime'] = 0;
  503. $val['cancel_reason'] = '';
  504. $val['cancel_time'] = 0;
  505. $val['cancel_notice_status'] = 0;
  506. }
  507. Db::name('lesson_slot')->insertAll($list);
  508. echo '复制完成';
  509. //$this->success('已复制到下周');
  510. }
  511. //每两周一次
  512. //周一早上,复制本周+下周的课,到下下周+下下下周
  513. public function auto_slot_copyweek(){
  514. $week = date('w');
  515. if($week != 1){
  516. echo '不是周一';
  517. exit;
  518. }
  519. $check = Db::name('plantask')->where('key','auto_slot_copyweek')->whereTime('lasttime','week')->find();
  520. if($check){
  521. echo '本周执行过了';
  522. exit;
  523. }
  524. $check = Db::name('plantask')->where('key','auto_slot_copyweek')->whereTime('lasttime','last week')->find();
  525. if($check){
  526. echo '上周执行过了';
  527. exit;
  528. }
  529. //先查找 下周有没有数据
  530. /*$starttime = strtotime('this week Monday') + (86400*14);
  531. $endtime = $starttime + (86400*14);
  532. $check = Db::name('lesson_slot')->where('starttime','BETWEEN',[$starttime,$endtime])->find();
  533. if($check){
  534. echo '下下周和下下下周有数据了';
  535. exit;
  536. }*/
  537. echo '准备复制';
  538. //复制本周+下周的课
  539. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  540. $endtime = $starttime + (86400*14);
  541. $list = Db::name('lesson_slot')->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  542. if(empty($list)){
  543. echo 'empty';
  544. exit;
  545. }
  546. //到下下周+下下下周
  547. foreach($list as $key => &$val){
  548. unset($val['id']);
  549. $val['starttime'] = $val['starttime'] + (86400*14);
  550. $val['endtime'] = $val['endtime'] + (86400*14);
  551. $val['bookednum'] = 0;
  552. $val['status'] = 0;
  553. $val['notice_status'] = 0;
  554. $val['finishtime'] = 0;
  555. $val['cancel_reason'] = '';
  556. $val['cancel_time'] = 0;
  557. $val['is_show'] = 0;
  558. $val['cancel_notice_status'] = 0;
  559. }
  560. Db::startTrans();
  561. $rs1 = Db::name('plantask')->where('key','auto_slot_copyweek')->update(['lasttime'=>time()]);
  562. if($rs1 === false){
  563. Db::rollback();
  564. $this->error('同步失败');
  565. }
  566. $rs2 = Db::name('lesson_slot')->insertAll($list);
  567. if(!$rs2){
  568. Db::rollback();
  569. $this->error('复制失败');
  570. }
  571. Db::commit();
  572. echo '复制完成';
  573. }
  574. /////////////////////////////////////////下面都是工具方法////////////////////////////////////////////////
  575. //发送whatapp消息的方法
  576. private function whatapp($receive_mobile,$template,$code,$parameters){
  577. if(empty($receive_mobile)){return true;}
  578. $token = config('site.whatsapp_token');
  579. //发送者
  580. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  581. //发送
  582. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  583. $header = [
  584. 'Authorization: Bearer ' . $token,
  585. 'Content-Type: application/json',
  586. ];
  587. $body = [
  588. 'messaging_product' => 'whatsapp',
  589. 'recipient_type' => 'individual',
  590. 'to' => $receive_mobile,
  591. 'type' => 'template',
  592. 'template' => [
  593. 'name' => $template,
  594. 'language' => [
  595. 'code' => $code
  596. ],
  597. 'components' => [
  598. [
  599. 'type' => 'body',
  600. 'parameters' => $parameters
  601. ]
  602. ],
  603. ],
  604. ];
  605. $body = json_encode($body);
  606. $rs = curl_post($url,$body,$header);
  607. return true;
  608. }
  609. //结果集信息里,多个字段需要翻译
  610. private function list_lang($list,$field,$lang = ''){
  611. if(!$list || empty($list)){
  612. return $list;
  613. }
  614. foreach($list as $vo => $info){
  615. $list[$vo] = $this->info_lang($info,$field);
  616. }
  617. return $list;
  618. }
  619. //单条信息里,多个字段需要翻译
  620. private function info_lang($data,$field,$lang = ''){
  621. if(!$data || empty($data)){
  622. return $data;
  623. }
  624. foreach($data as $key => $val){
  625. if(in_array($key,$field)){
  626. if($lang == 'EN'){
  627. $data[$key] = $data[$key.'_en'];
  628. unset($data[$key.'_en']);
  629. }else{
  630. unset($data[$key.'_en']);
  631. }
  632. }
  633. }
  634. return $data;
  635. }
  636. }