Lessonorder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 Lessonorder extends Backend
  12. {
  13. /**
  14. * Lessonorder模型对象
  15. * @var \app\admin\model\Lessonorder
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Lessonorder;
  22. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  23. $this->view->assign("paytypeList", $this->model->getPaytypeList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['user','slot','lesson','coach'])
  47. ->where($where)
  48. ->where('lessonorder.order_status','NEQ',0)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->getRelation('user')->visible(['firstname','lastname']);
  53. $row->getRelation('slot')->visible(['starttime','endtime']);
  54. $row->getRelation('lesson')->visible(['name','name_en']);
  55. $row->getRelation('coach')->visible(['nickname']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 取消
  64. */
  65. public function cancel(){
  66. $id = input('id');
  67. $info = Db::name('lesson_order')->where('id',$id)->where('order_status',10)->find();
  68. if(!$info){
  69. $this->error('请刷新重试');
  70. }
  71. if($this->request->isPost()){
  72. $cancel_reason = input('cancel_reason','');
  73. $cancel_time = strtotime(input('cancel_time',''));
  74. Db::startTrans();
  75. //找到所有的已报名订单
  76. $info = Db::name('lesson_order')->where('id',$id)->where('order_status',10)->lock(true)->find();
  77. if(empty($info)){
  78. Db::rollback();
  79. $this->error('请刷新重试');
  80. }
  81. //套餐给加回去
  82. if($info['paytype'] == 1){
  83. $package_order = Db::name('package_order')->where('id',$info['package_order_id'])->lock(true)->find();
  84. $update = [
  85. 'remain' => $package_order['remain'] + $info['usernumber'],
  86. 'updatetime' => time(),
  87. ];
  88. $rs_remain = Db::name('package_order')->where('id',$info['package_order_id'])->update($update);
  89. if($rs_remain === false){
  90. Db::rollback();
  91. $this->error('取消失败');
  92. }
  93. }
  94. if($info['paytype'] == 4){
  95. $update = [
  96. 'order_status' => 10,
  97. 'lesson_order_id' => 0,
  98. 'updatetime' => time(),
  99. ];
  100. $rs_remain = Db::name('trylesson_order')->where('id',$info['trylesson_order_id'])->update($update);
  101. if($rs_remain === false){
  102. Db::rollback();
  103. $this->error('取消失败');
  104. }
  105. }
  106. //现金支付不给退,线下处理
  107. //取消预约单
  108. $update2 = [
  109. 'order_status' => 30,
  110. 'cancel_time' => $cancel_time,
  111. 'cancel_reason' => $cancel_reason,
  112. ];
  113. if($info['paytype'] == 1 || $info['paytype'] == 4){
  114. $update2['order_status'] = 40;
  115. }
  116. $rs = Db::name('lesson_order')->where('id',$info['id'])->update($update2);
  117. if($rs === false){
  118. Db::rollback();
  119. $this->error('取消失败');
  120. }
  121. $slot_info = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
  122. $lesson_info = Db::name('lesson')->where('id',$slot_info['lesson_id'])->find();
  123. //给用户发通知
  124. $user_info = Db::name('user')->where('id',$info['user_id'])->find();
  125. if(!empty($user_info['email'])){
  126. try {
  127. $obj = new Email();
  128. $result = $obj
  129. ->to($user_info['email'])
  130. ->subject('Class is Cancelled!')
  131. ->message('Hi,'.$user_info['firstname']. ' ' .$user_info['lastname'].',您预约的'.$lesson_info['name'].'已取消')
  132. ->send();
  133. $coach_name = Db::name('coach')->where('id',$slot_info['coach_ids'])->value('nickname');
  134. //发whatsapp
  135. $parameters = [
  136. [
  137. 'type' => 'text',
  138. 'text' => $user_info['firstname'].' '.$user_info['lastname'],
  139. ],
  140. [
  141. 'type' => 'text',
  142. 'text' => $lesson_info['name_en'],
  143. ],
  144. [
  145. 'type' => 'text',
  146. 'text' => $coach_name,
  147. ],
  148. [
  149. 'type' => 'text',
  150. 'text' => date('Y-m-d H:i',$slot_info['starttime']),
  151. ],
  152. ];
  153. $this->whatapp($user_info['whatsapp'],'class_cancelled','en_US',$parameters);
  154. } catch (Exception $e) {
  155. }
  156. }
  157. Db::commit();
  158. $this->success('取消完成');
  159. }
  160. $this->view->assign('row',$info);
  161. return $this->view->fetch();
  162. }
  163. private function whatapp($receive_mobile,$template,$code,$parameters){
  164. if(empty($receive_mobile)){return true;}
  165. $token = config('site.whatsapp_token');
  166. //发送者
  167. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  168. //发送
  169. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  170. $header = [
  171. 'Authorization: Bearer ' . $token,
  172. 'Content-Type: application/json',
  173. ];
  174. $body = [
  175. 'messaging_product' => 'whatsapp',
  176. 'recipient_type' => 'individual',
  177. 'to' => $receive_mobile,
  178. 'type' => 'template',
  179. 'template' => [
  180. 'name' => $template,
  181. 'language' => [
  182. 'code' => $code
  183. ],
  184. 'components' => [
  185. [
  186. 'type' => 'body',
  187. 'parameters' => $parameters
  188. ]
  189. ],
  190. ],
  191. ];
  192. $body = json_encode($body);
  193. $rs = curl_post($url,$body,$header);
  194. return true;
  195. }
  196. }