User.php 10.0 KB

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