Plantask.php 28 KB

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