User.php 11 KB

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