Lessonslot.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\common\library\Email;
  6. /**
  7. * 每日课时
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Lessonslot extends Backend
  12. {
  13. protected $noNeedLogin = ['vue_index','vue_staff','slot_add','slot_info','slot_edit','cancel'];
  14. /**
  15. * Lessonslot模型对象
  16. * @var \app\admin\model\Lessonslot
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Lessonslot;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
  25. $this->view->assign("isShowList", $this->model->getIsShowList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. //按周翻页
  47. $week_current = input('week_current','');
  48. if($week_current !== ''){
  49. $starttime = strtotime('this week Monday') + (86400*7*$week_current);
  50. $endtime = $starttime + (86400*7);
  51. $where = ['lessonslot.starttime'=>['between',[$starttime,$endtime]]];
  52. $limit = 999999;
  53. }else{
  54. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  55. }
  56. $list = $this->model
  57. ->with(['coach','lesson','danceroom'])
  58. ->where($where)
  59. // ->order($sort, $order)
  60. ->order('lessonslot.starttime desc,lessonslot.id desc')
  61. // ->select(false);echo $list;exit;
  62. ->paginate($limit);
  63. foreach ($list as $row) {
  64. $row->getRelation('coach')->visible(['nickname']);
  65. $row->getRelation('lesson')->visible(['name','name_en','type']);
  66. $row->getRelation('danceroom')->visible(['name','name_en']);
  67. }
  68. $items = $list->items();
  69. foreach($items as $key => $val){
  70. $items[$key]['week'] = date('l',$val['starttime']);
  71. }
  72. //上-周,本周,下-周
  73. if(empty($week_current)){$week_current = 0;}
  74. $this_week = 0;
  75. $last_week = $week_current-1;
  76. $next_week = $week_current+1;
  77. $extend = [
  78. 'this_week'=>$this_week,
  79. 'last_week'=>$last_week,
  80. 'next_week'=>$next_week,
  81. ];
  82. $result = array("total" => $list->total(), "rows" => $items,'extend'=>$extend);
  83. return json($result);
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 预约
  89. */
  90. public function booking(){
  91. $id = input('id');
  92. $info = Db::name('lesson_slot')->where('id',$id)->find();
  93. if(!$info){
  94. $this->error('请刷新重试');
  95. }
  96. if($info['status'] != 0){
  97. $this->error('当前课程不能预约');//状态:0=报名中,20=已点名,30=已取消
  98. }
  99. if($this->request->isPost()){
  100. $user_id = input('user_id',0,'intval');
  101. //来自接口
  102. $slot_id = input('slot_id',0,'intval');
  103. $number = input('number',1,'intval');
  104. $remark = input('remark','','trim');
  105. $packageorder_id = input('packageorder_id',0,'intval');
  106. $trylessonorder_id = input('trylessonorder_id',0,'intval');
  107. $params = $this->request->post('row/a');
  108. $paytype = $params['paytype'];//支付类型:1=课程套餐,4=试课单
  109. if($number <= 0){
  110. $this->error('预约人数错误');
  111. }
  112. if($paytype == 4){
  113. $number = 1; //试课只能选一个人
  114. }
  115. if(empty($packageorder_id) && empty($trylessonorder_id)){
  116. $this->error('请选择任意一种支付方式');
  117. }
  118. //课时
  119. $info = Db::name('lesson_slot')->alias('slot')
  120. ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price,coach.nickname as coach_nickname,danceroom.name_en as danceroom_name_en')
  121. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  122. ->join('coach','slot.coach_ids = coach.id','LEFT')
  123. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  124. ->where('slot.id',$slot_id)->where('slot.status',0)->find();
  125. if(empty($info)){
  126. $this->error('课程可能已取消,请刷新重试');
  127. }
  128. if($info['endtime'] < time()){
  129. $this->error('课程已经结束了,不能再进行预约');
  130. }
  131. //报名人数不能超限
  132. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  133. $num_remain = $info['num_max'] - $pay_number;
  134. if($num_remain < 0){
  135. $num_remain = 0;
  136. }
  137. if($num_remain < $number){
  138. //$this->error(__('预约名额只剩N名',['number'=>$num_remain]));
  139. }
  140. $number_hours = bcmul($number,$info['hours'],1);//每日课时按小时扣
  141. $lesson_order = [
  142. 'order_no' => createUniqueNo('S',$user_id),
  143. 'user_id' => $user_id,
  144. 'slot_id' => $slot_id,
  145. 'lesson_id' => $info['lesson_id'],
  146. 'order_amount' => bcmul($info['price'],$number,2),
  147. 'order_status' => 0,
  148. 'paytype' => $paytype, //支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单
  149. 'paytime' => 0,
  150. 'createtime' => time(),
  151. // 'updatetime' => ,
  152. // 'finishtime' => ,
  153. 'usernumber' => $number,
  154. 'usernumber_hours' => $number_hours,
  155. 'userremark' => $remark,
  156. 'package_order_id' => 0,
  157. 'package_remark' => '',// 比如:5/30,5-7/30
  158. ];
  159. //
  160. Db::startTrans();
  161. if($paytype == 1){
  162. //检查已选择套餐
  163. $map = [
  164. 'user_id' =>$user_id,
  165. 'endtime' => ['gt',time()],
  166. 'remain' => ['gt',0],
  167. 'order_status' => 1,
  168. // 'use_status' => 1, //已激活的
  169. 'id' => $packageorder_id,
  170. ];
  171. $package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->lock(true)->find();
  172. if(!$package_order){
  173. Db::rollback();
  174. $this->error('配套信息不正确,请刷新重试');
  175. }
  176. //课时不足支撑报名人数
  177. if($package_order['remain'] < $number_hours){
  178. Db::rollback();
  179. $this->error('该配套余额不足,可以使用其他配套');
  180. }
  181. if($package_order['use_status'] != 1){
  182. Db::rollback();
  183. $this->error('该配套尚未激活');
  184. }
  185. //扣除一节
  186. $update = [
  187. 'remain' => bcsub($package_order['remain'],$number_hours,1),
  188. 'updatetime' => time(),
  189. ];
  190. $rs1 = Db::name('package_order')->where('id',$packageorder_id)->update($update);
  191. if($rs1 === false){
  192. Db::rollback();
  193. $this->error('扣除套餐余额失败');
  194. }
  195. //修改预约单数据
  196. $lesson_order['order_amount'] = 0;
  197. $lesson_order['order_status'] = 10;
  198. $lesson_order['paytime'] = time();
  199. $lesson_order['package_order_id'] = $packageorder_id;
  200. $lesson_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $number_hours) .'/'. $package_order['sessions'];
  201. //预约单写入
  202. $lesson_order_id = Db::name('lesson_order')->insertGetId($lesson_order);
  203. if(!$lesson_order_id){
  204. Db::rollback();
  205. $this->error('预约失败');
  206. }
  207. //更新已预约人数
  208. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  209. $rs_slot = Db::name('lesson_slot')->where('id',$slot_id)->update(['bookednum' => $pay_number]);
  210. if($rs_slot === false){
  211. Db::rollback();
  212. $this->error('预约失败');
  213. }
  214. }
  215. elseif($paytype == 4){
  216. //使用试课单
  217. $map = [
  218. 'id' => $trylessonorder_id,
  219. 'user_id' =>$user_id,
  220. 'endtime' => ['gt',time()],
  221. 'order_status' => 10,
  222. ];
  223. $trylesson_order = Db::name('trylesson_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->lock(true)->find();
  224. if(!$trylesson_order){
  225. Db::rollback();
  226. $this->error('未找到对应的试课');
  227. }
  228. //修改预约单数据
  229. $lesson_order['order_amount'] = 0;
  230. $lesson_order['order_status'] = 10;
  231. $lesson_order['paytime'] = time();
  232. $lesson_order['trylesson_order_id'] = $trylessonorder_id;
  233. //预约单写入
  234. $lesson_order_id = Db::name('lesson_order')->insertGetId($lesson_order);
  235. if(!$lesson_order_id){
  236. Db::rollback();
  237. $this->error('预约失败');
  238. }
  239. //改掉试课单状态
  240. $update = [
  241. 'order_status' => 20,
  242. 'updatetime' => time(),
  243. 'lesson_order_id' => $lesson_order_id,
  244. ];
  245. $rs1 = Db::name('trylesson_order')->where('id',$trylessonorder_id)->update($update);
  246. if($rs1 === false){
  247. Db::rollback();
  248. $this->error('使用试课失败');
  249. }
  250. //更新已预约人数
  251. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  252. $rs_slot = Db::name('lesson_slot')->where('id',$slot_id)->update(['bookednum' => $pay_number]);
  253. if($rs_slot === false){
  254. Db::rollback();
  255. $this->error('预约失败');
  256. }
  257. }
  258. Db::commit();
  259. //额外的通知
  260. $userinfo = Db::name('user')->where('id',$user_id)->find();
  261. $obj = new Email();
  262. try {
  263. $message =
  264. 'Hi,'.$userinfo['firstname']. ' ' .$userinfo['lastname'].'!<br/>
  265. Your booking is confirmed and you’re all set now to fly with us! Here are the details of your booking:
  266. Class:'.$info['name_en'].'<br/>
  267. Instructor:'.$info['coach_nickname'].'<br/>
  268. Location: '. $info['danceroom_name_en'] .'<br/>
  269. Date: '.date('d F Y',$info['starttime']).'<br/>
  270. Time: '.date('H:i a',$info['starttime']).'<br/>
  271. Please arrive 10 minutes before the class timing to prepare for the harness fitting!<br/>
  272. We are located at 450 Alexandra Road, #02-01,<br/>
  273. 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/>
  274. We look forward to seeing you for an amazing session! ❤<br/>
  275. Best Regards,<br/>
  276. Elin Dance Studio<br/>
  277. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  278. ';
  279. //给这些用户发邮件
  280. $result = $obj
  281. ->to($userinfo['email'])
  282. ->subject('You are now booked in for this class! ')
  283. ->message($message)
  284. ->send();
  285. //发whatsapp
  286. $parameters = [
  287. [
  288. 'type' => 'text',
  289. 'text' => $userinfo['firstname'].' '.$userinfo['lastname'],
  290. ],
  291. [
  292. 'type' => 'text',
  293. 'text' => $info['name_en'],
  294. ],
  295. [
  296. 'type' => 'text',
  297. 'text' => date('D l Y',$info['starttime']) .' at '.date('H:i a',$info['starttime']),
  298. ],
  299. [
  300. 'type' => 'text',
  301. 'text' => $info['danceroom_name_en'],
  302. ],
  303. ];
  304. $this->whatapp($userinfo['whatsapp'],'class_booked_confirmation','en_US',$parameters);
  305. } catch (Exception $e) {
  306. }
  307. //额外的通知
  308. $this->success('预约成功');
  309. }
  310. $this->view->assign('row',$info);
  311. return $this->view->fetch();
  312. }
  313. /**
  314. * 取消
  315. */
  316. public function cancel(){
  317. $id = input('id');
  318. $info = Db::name('lesson_slot')->where('id',$id)->find();
  319. if(!$info){
  320. $this->error('请刷新重试');
  321. }
  322. if($info['status'] != 0){
  323. $this->error('当前课程不能取消');
  324. }
  325. if($this->request->isPost()){
  326. $remark = input('remark','');
  327. $cancel_reason = input('cancel_reason','');
  328. $cancel_time = time();
  329. Db::startTrans();
  330. $info = Db::name('lesson_slot')->where('id',$id)->lock(true)->find();
  331. if(!$info){
  332. Db::rollback();
  333. $this->error('请刷新重试');
  334. }
  335. if($info['status'] != 0){
  336. Db::rollback();
  337. $this->error('当前课程不能取消');
  338. }
  339. $update = [
  340. 'status' => 30,
  341. 'remark' => $remark,
  342. 'cancel_reason' => $cancel_reason,
  343. 'cancel_time' => $cancel_time,
  344. 'bookednum' => 0,
  345. ];
  346. $rs1 = Db::name('lesson_slot')->where('id',$id)->update($update);
  347. if($rs1 === false){
  348. Db::rollback();
  349. $this->error('取消失败,请刷新重试');
  350. }
  351. //找到所有的已候补订单
  352. $update = [
  353. 'order_status' => 30,
  354. 'cancel_time' => $cancel_time,
  355. 'cancel_reason' => $cancel_reason,
  356. ];
  357. $rs2 = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',2)->where('order_status',0)->update($update);
  358. //找到所有的已报名订单
  359. $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',1)->where('order_status',10)->lock(true)->select();
  360. if(!empty($lesson_order_list)){
  361. foreach($lesson_order_list as $lesson_order){
  362. //套餐给加回去
  363. if($lesson_order['paytype'] == 1){
  364. $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find();
  365. $update = [
  366. 'remain' => bcadd($package_order['remain'],$lesson_order['usernumber_hours'],1),
  367. 'updatetime' => time(),
  368. ];
  369. $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update);
  370. if($rs_remain === false){
  371. Db::rollback();
  372. $this->error('取消失败');
  373. }
  374. }
  375. //试课给改回去
  376. if($lesson_order['paytype'] == 4){
  377. $update = [
  378. 'order_status' => 10,
  379. 'updatetime' => time(),
  380. 'lesson_order_id' => 0,
  381. ];
  382. $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update);
  383. if($rs_remain === false){
  384. Db::rollback();
  385. $this->error('取消失败');
  386. }
  387. }
  388. //现金支付不给退,线下处理
  389. //取消预约单
  390. $update = [
  391. 'order_status' => 30,
  392. 'cancel_time' => $cancel_time,
  393. 'cancel_reason' => $cancel_reason,
  394. ];
  395. if($lesson_order['paytype'] == 1 || $lesson_order['paytype'] == 4){
  396. $update['order_status'] = 40;
  397. }
  398. $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update);
  399. if($rs === false){
  400. Db::rollback();
  401. $this->error('取消失败');
  402. }
  403. }
  404. }
  405. Db::commit();
  406. $comefrom = input('comefrom','');
  407. if($comefrom == 'backend'){
  408. //后台来的
  409. $this->success('取消完成');
  410. }else{
  411. //接口来的
  412. $this->result('',1,'取消完成','json');
  413. }
  414. }
  415. $this->view->assign('row',$info);
  416. return $this->view->fetch();
  417. }
  418. /**
  419. * 复制本周的课程表
  420. */
  421. public function copyweek(){
  422. exit;
  423. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  424. $endtime = $starttime + 86400*7;
  425. $list = Db::name('lesson_slot')->where('is_show',1)->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  426. if(empty($list)){
  427. $this->error('本周还没有课程表');
  428. }
  429. foreach($list as $key => &$val){
  430. unset($val['id']);
  431. $val['starttime'] = $val['starttime'] + 86400*7;
  432. $val['endtime'] = $val['endtime'] + 86400*7;
  433. $val['status'] = 0;
  434. $val['notice_status'] = 0;
  435. $val['finishtime'] = 0;
  436. $val['cancel_reason'] = '';
  437. $val['cancel_time'] = 0;
  438. }
  439. Db::name('lesson_slot')->insertAll($list);
  440. $this->success('已复制到下周');
  441. }
  442. /**
  443. * 查看
  444. */
  445. public function vue_index()
  446. {
  447. $where = [
  448. 'slot.status' => ['NEQ',30],
  449. ];
  450. $coach_id = input('coach_id','');
  451. $lesson_id = input('lesson_id','');
  452. $danceroom_id = input('danceroom_id',0);
  453. $bookstatus = input('bookstatus',0);
  454. $starttime = input('starttime',0);//默认看今天
  455. $endtime = input('endtime',0);
  456. if(empty($starttime) || empty($endtime)){
  457. $starttime = strtotime(date('Y-m-d'));
  458. $endtime = $starttime + 86399;
  459. }
  460. if(!empty($coach_id)){
  461. $where['slot.coach_ids'] = ['IN',$coach_id];
  462. }
  463. if(!empty($lesson_id)){
  464. $where['slot.lesson_id'] = ['IN',$lesson_id];
  465. }
  466. if(!empty($danceroom_id)){
  467. $where['slot.danceroom_id'] = $danceroom_id;
  468. }
  469. $where['slot.starttime'] = ['BETWEEN',[$starttime,$endtime]];
  470. $wherenew = '';
  471. if(!empty($bookstatus)){
  472. if($bookstatus == 1){
  473. $wherenew = 'slot.bookednum = 0';
  474. }
  475. if($bookstatus == 2){
  476. $wherenew = 'slot.bookednum != 0 and slot.bookednum < num_max';
  477. }
  478. if($bookstatus == 3){
  479. $wherenew = 'slot.bookednum = num_max';
  480. }
  481. }
  482. $field = [
  483. 'slot.id','slot.starttime','slot.endtime','slot.num_max','slot.bookednum','coach_ids',
  484. 'coach.nickname as Staff','coach.bgcolor',
  485. 'lesson.name_en as title',
  486. 'danceroom.name_en as Location',
  487. ];
  488. $list = Db::name('lesson_slot')->alias('slot')
  489. ->join('coach','slot.coach_ids = coach.id','LEFT')
  490. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  491. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  492. ->field($field)
  493. ->where($where)
  494. ->where($wherenew)
  495. ->order('slot.id desc')
  496. ->select();
  497. if(!empty($list)){
  498. foreach($list as $key => $val){
  499. $val['resourceId'] = $val['coach_ids'];
  500. $val['start'] = date('Y-m-d H:i',$val['starttime']);
  501. $val['end'] = date('Y-m-d H:i',$val['endtime']);
  502. //无人约
  503. $val['backgroundColor'] = '#ffffff';
  504. $val['borderColor'] = $val['bgcolor'];
  505. $val['textColor'] = '#333333';
  506. //有人约
  507. if($val['bookednum'] > 0){
  508. $val['backgroundColor'] = $val['bgcolor'];
  509. $val['borderColor'] = $val['bgcolor'];
  510. $val['textColor'] = '#ffffff';
  511. }
  512. //详情
  513. $val['extendedProps'] = [
  514. 'Session' => $val['title'],
  515. 'time' => date('D, M d Y, H:i',$val['starttime']).' - '.date('H:i',$val['endtime']),
  516. 'Staff' => $val['Staff'],
  517. 'Location' => $val['Location'],
  518. 'Participar' => $val['bookednum'].'/'.$val['num_max'].' participants',
  519. 'id' => $val['id'],
  520. ];
  521. $list[$key] = $val;
  522. }
  523. }
  524. $this->result($list,1,'success','json');
  525. }
  526. //教练表头
  527. public function vue_staff()
  528. {
  529. $coach_id = input('coach_id','');
  530. //教练列表
  531. $coach_map = [];
  532. if(!empty($coach_id)){
  533. $coach_map['id'] = ['IN',$coach_id];
  534. }
  535. $coach_list = Db::name('coach')->where($coach_map)->order('id desc')->select();
  536. $result = [];
  537. if(!empty($coach_list)){
  538. foreach($coach_list as $ckey => $cval){
  539. $result[] = [
  540. 'id' => $cval['id'],
  541. 'title' => $cval['nickname'],
  542. 'extendedProps' => [
  543. 'name' => $cval['nickname'],
  544. 'avatar' => localpath_to_netpath($cval['avatar']),
  545. ],
  546. ];
  547. }
  548. }
  549. $this->result($result,1,'success','json');
  550. }
  551. //课程新增
  552. public function slot_add(){
  553. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show'];
  554. // $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  555. $data = request_post_hub($field/*,$require*/);
  556. if(empty($data['starttime'])){
  557. $data['starttime'] = time();
  558. }
  559. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  560. $id = Db::name('lesson_slot')->insertGetId($data);
  561. $this->result($id,1,'添加完成','json');
  562. }
  563. //课程详情
  564. public function slot_info(){
  565. $id = input('id',0);
  566. $info = Db::name('lesson_slot')->where('id',$id)->find();
  567. $name = Db::name('lesson')->where('id',$info['lesson_id'])->value('name_en');
  568. $info['lesson_name'] = $name;
  569. $this->result($info,1,'success','json');
  570. }
  571. //课程编辑
  572. public function slot_edit(){
  573. $id = input('id',0);
  574. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show'];
  575. /*$require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];*/
  576. $data = request_post_hub($field/*,$require*/);
  577. if(empty($data['starttime'])){
  578. $data['starttime'] = time();
  579. }
  580. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  581. Db::name('lesson_slot')->where('id',$id)->update($data);
  582. $this->result('',1,'修改完成','json');
  583. }
  584. //发送whatapp消息的方法
  585. private function whatapp($receive_mobile,$template,$code,$parameters){
  586. if(empty($receive_mobile)){return true;}
  587. $token = config('site.whatsapp_token');
  588. //发送者
  589. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  590. //发送
  591. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  592. $header = [
  593. 'Authorization: Bearer ' . $token,
  594. 'Content-Type: application/json',
  595. ];
  596. $body = [
  597. 'messaging_product' => 'whatsapp',
  598. 'recipient_type' => 'individual',
  599. 'to' => $receive_mobile,
  600. 'type' => 'template',
  601. 'template' => [
  602. 'name' => $template,
  603. 'language' => [
  604. 'code' => $code
  605. ],
  606. 'components' => [
  607. [
  608. 'type' => 'body',
  609. 'parameters' => $parameters
  610. ]
  611. ],
  612. ],
  613. ];
  614. $body = json_encode($body);
  615. $rs = curl_post($url,$body,$header);
  616. return true;
  617. }
  618. }