Plantask.php 27 KB

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