Plantask.php 27 KB

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