User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\api\controller;
  9. use app\api\controller\base\Permissions;
  10. use app\common\model\AppointmentApplication;
  11. use app\common\model\AppointmentTicket;
  12. use app\common\model\Feedback;
  13. use app\common\model\Webconfig;
  14. class User extends Permissions
  15. {
  16. //个人资料
  17. public function info()
  18. {
  19. $user = $this->getUser();
  20. $info = [
  21. "nickname" => $user->nickname,
  22. "head_pic" => $user->head_pic,
  23. "sex" => $user->sex,
  24. "country" => $user->country,
  25. "province" => $user->province,
  26. "city" => $user->city
  27. ];
  28. $this->json_success('success', $info);
  29. }
  30. //预约申请
  31. public function application()
  32. {
  33. $post = $this->request->param();
  34. $validate = new \think\Validate([
  35. ['name|姓名', 'require|max:50'],
  36. ['age|年龄', 'number'],
  37. ['birthday|出生年月', 'max:50'],
  38. ['sex|性别', 'number|in:1,2'],
  39. ['phone|电话', 'require|max:50'],
  40. ['id_card|身份证', 'max:50'],
  41. ['problem_desc|问题描述', 'max:200'],
  42. ['requirement_desc|需求描述', 'max:200'],
  43. ['appointment_ticket_id', 'require|number'],
  44. ['appointment_period', 'require|number|in:1,2,3'],
  45. ['appointment_time|预约时间段', 'require|dateFormat:H:i - H:i'],
  46. ['city|来自城市', 'max:50'],
  47. ['job|职业', 'max:50'],
  48. ['education_level|受教育程度', 'number|in:0,1,2,3,4'],
  49. ['home_address|家庭住址', 'max:255'],
  50. ['marriage|婚姻情况', 'number|in:0,1,2'],
  51. ['problem_type|咨询问题类别', 'number|in:0,1,2,3,4,5'],
  52. ['childs_num_str|子女数量', 'max:255'],
  53. ]);
  54. if (!$validate->check($post)) {
  55. $this->json_error('提交失败:' . $validate->getError());
  56. }
  57. $model = new AppointmentApplication();
  58. //爽约多少次后,多少天内不能再预约
  59. $break_the_promise_day_range = Webconfig::getValue('break_the_promise_day_range');
  60. $break_the_promise_times = Webconfig::getValue('break_the_promise_times');
  61. $stop_appointment_day = Webconfig::getValue('stop_appointment_day');
  62. //
  63. if ($stop_appointment_day && $break_the_promise_times && $break_the_promise_day_range) {
  64. $break_the_promise_count = $model->where('status', AppointmentApplication::STATUS_NOT_COME)->whereTime('finish_time', "-$break_the_promise_day_range day")->count();
  65. if ($break_the_promise_count >= $break_the_promise_times) {
  66. $maxtime = $model->where('status', AppointmentApplication::STATUS_NOT_COME)->max('finish_time');
  67. if (time() < ($maxtime + $stop_appointment_day * 3600 * 24)) {
  68. $this->json_error("此账号 $break_the_promise_day_range 天内爽约 $break_the_promise_count 次, $stop_appointment_day 天内不能再预约");
  69. }
  70. }
  71. }
  72. //检测预约号源是否可预约
  73. $ticket = (new AppointmentTicket())->where(['id' => $post['appointment_ticket_id'], 'status' => AppointmentTicket::STATUS_OPEN])->find();
  74. if (!$ticket) {
  75. $this->json_error("获取预约号失败");
  76. }
  77. $appoint = $ticket->appointment;
  78. //判断该时间段,存在于时间列表中才行
  79. if ($post['appointment_period'] == 1) {
  80. $timePeriods = $appoint->morning_time_periods;
  81. } elseif ($post['appointment_period'] == 2) {
  82. $timePeriods = $appoint->afternoon_time_periods;
  83. } else {
  84. $timePeriods = [];//晚上没有
  85. }
  86. if (!in_array($post['appointment_time'], $timePeriods)) {
  87. $this->json_error("该时间段不存在,请重新选择时间");
  88. }
  89. //排除过期时段
  90. $timerang = explode(' - ', $post['appointment_time']);
  91. $period_start = $timerang[0];
  92. $period_end = $timerang[1];
  93. if (time() > strtotime($ticket->appointment_daytime . ' ' . $period_end)) {
  94. $this->json_error("该时段已经过期,请重新选择时间");
  95. }
  96. //临近几小时不可预约
  97. $appointment_time_limit = Webconfig::getValue('appointment_time_limit');
  98. if ($appointment_time_limit > 0) {
  99. if (time() > strtotime($ticket->appointment_daytime . ' ' . $period_start) - $appointment_time_limit * 3600) {
  100. $this->json_error("临近{$appointment_time_limit}小时内不可预约,请重新选择时间");
  101. }
  102. }
  103. //已经预约的时间段
  104. $ticketPeriods = (new AppointmentApplication())->where('appointment_ticket_id', $ticket->id)->where('finish_time', 0)->column('appointment_time');
  105. foreach ($ticketPeriods as $period) {
  106. if ($period == $post['appointment_time']) {
  107. $this->json_error("该时段已被预约,请重新选择时间");
  108. }
  109. }
  110. //入库
  111. $data = [
  112. 'name' => htmlspecialchars($post['name']),
  113. 'user_id' => $this->getUserId(),
  114. 'age' => $post['age']??0,
  115. 'birthday' => $this->request->param('birthday', '', 'htmlspecialchars'),
  116. 'sex' => $post['sex']??0,
  117. 'phone' => $post['phone'],
  118. 'id_card' => $this->request->param('id_card', '', 'htmlspecialchars'),
  119. 'problem_desc' => $this->request->param('problem_desc', '', 'htmlspecialchars'),
  120. 'requirement_desc' => $this->request->param('requirement_desc', '', 'htmlspecialchars'),
  121. 'address_id' => $ticket->address_id,
  122. 'provider_id' => $ticket->provider_id,
  123. 'appointment_ticket_id' => $post['appointment_ticket_id'],
  124. 'appointment_time' => $post['appointment_time'],
  125. 'appointment_period' => $post['appointment_period'],
  126. 'status' => AppointmentApplication::STATUS_NOT_SIGN,
  127. 'city' => $this->request->param('city', '', 'htmlspecialchars'),
  128. 'job' => $this->request->param('job', '', 'htmlspecialchars'),
  129. 'education_level' => $post['education_level']??0,
  130. 'home_address' => $this->request->param('home_address', '', 'htmlspecialchars'),
  131. 'marriage' => $post['marriage']??0,
  132. 'problem_type' => $post['problem_type']??0,
  133. 'childs_num_str' => $this->request->param('childs_num_str', '', 'htmlspecialchars'),
  134. ];
  135. if (false === $model->allowField(true)->save($data)) {
  136. $this->json_error("预约入库失败");
  137. } else {
  138. $this->json_success("预约成功");
  139. }
  140. }
  141. //取消预约
  142. public function cancelApplication()
  143. {
  144. $post = $this->request->param();
  145. $validate = new \think\Validate([
  146. ['id', 'require|number'],
  147. ]);
  148. if (!$validate->check($post)) {
  149. $this->json_error('提交失败:' . $validate->getError());
  150. }
  151. $application = (new AppointmentApplication())->where(['id' => $post['id'], 'user_id' => $this->getUserId()])->find();
  152. if (!$application) {
  153. $this->json_error("预约不存在");
  154. }
  155. if ($application->finish_time > 0) {
  156. $this->json_error("已完成,不可取消");
  157. }
  158. // n 小时后不可取消
  159. $cancel_appointment_time = Webconfig::getValue('cancel_appointment_time');
  160. if ($cancel_appointment_time && time() > ($application->getData('create_time') + $cancel_appointment_time * 3600)) {
  161. $this->json_error("超过{$cancel_appointment_time}小时后,不可取消");
  162. }
  163. if (false === $application->save(['status' => AppointmentApplication::STATUS_CANCEL, 'finish_time' => time()])) {
  164. $this->json_error("提交失败");
  165. } else {
  166. $this->json_success("提交成功");
  167. }
  168. }
  169. //我的预约列表
  170. public function applicationList()
  171. {
  172. $post = $this->request->param();
  173. $validate = new \think\Validate([
  174. ['status', 'number'],
  175. ['page', 'number'],
  176. ['pagenum', 'number|<=:1000']
  177. ]);
  178. if (!$validate->check($post)) {
  179. $this->json_error('提交失败:' . $validate->getError());
  180. }
  181. $where = ['user_id' => $this->getUserId()];
  182. $status = $this->request->param('status', 0, 'intval');
  183. if ($status == 1) {
  184. $where['finish_time'] = 0;
  185. } elseif ($status == 2) {
  186. $where['finish_time'] = ['>', 0];
  187. }
  188. $pagenum = $this->request->param('pagenum', 20, 'intval');
  189. $datalist = AppointmentApplication::with('ticket')->where($where)->order('create_time desc')->paginate($pagenum, true);
  190. foreach ($datalist as $key => $item) {
  191. //专家信息
  192. $item->address;
  193. $item->specialist;
  194. $item->specialist->head_pic = geturl($item->specialist->head_pic, '', true);
  195. $item->appointment_date = $item->appointment_date;
  196. $item->ticke_time = $item->ticket->appointment_daytime;
  197. $datalist[$key] = $item;
  198. }
  199. if (empty($datalist)) {
  200. $this->json_error("没有数据");
  201. }
  202. $this->json_success("查询成功", $datalist);
  203. }
  204. //提交反馈接口
  205. public function feedback()
  206. {
  207. $post = $this->request->param();
  208. //验证
  209. $validate = new \think\Validate([
  210. ['content|内容', 'require|max:500'],
  211. ]);
  212. if (!$validate->check($post)) {
  213. $this->json_error('提交失败:' . $validate->getError());
  214. }
  215. $model = new Feedback();
  216. $data = [
  217. 'user_id' => $this->getUserId(),
  218. 'content' => $this->request->param('content', '', 'htmlspecialchars')
  219. ];
  220. if (false === $model->allowField(true)->save($data)) {
  221. $this->json_error("提交失败");
  222. } else {
  223. $this->json_success("提交成功");
  224. }
  225. }
  226. }