Plantask.php 27 KB

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