User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_end = $timerang[1];
  100. if (time() > strtotime($ticket->appointment_daytime . ' ' . $period_end)) {
  101. $this->json_error("该时段已经过期,请重新选择时间");
  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. // 'childs_num' => $post['childs_num']??0,
  135. // 'childs_age1' => $post['childs_age1']??0,
  136. // 'childs_age2' => $post['childs_age2']??0,
  137. // 'childs_age3' => $post['childs_age3']??0,
  138. // 'childs_sex1' => $post['childs_sex1']??0,
  139. // 'childs_sex2' => $post['childs_sex2']??0,
  140. // 'childs_sex3' => $post['childs_sex3']??0,
  141. ];
  142. if (false === $model->allowField(true)->save($data)) {
  143. $this->json_error("预约入库失败");
  144. } else {
  145. $this->json_success("预约成功");
  146. }
  147. }
  148. //取消预约
  149. public function cancelApplication()
  150. {
  151. $post = $this->request->param();
  152. $validate = new \think\Validate([
  153. ['id', 'require|number'],
  154. ]);
  155. if (!$validate->check($post)) {
  156. $this->json_error('提交失败:' . $validate->getError());
  157. }
  158. $application = (new AppointmentApplication())->where(['id' => $post['id'], 'user_id' => $this->getUserId()])->find();
  159. if (!$application) {
  160. $this->json_error("预约不存在");
  161. }
  162. if ($application->finish_time > 0) {
  163. $this->json_error("已完成,不可取消");
  164. }
  165. // n 小时后不可取消
  166. $cancel_appointment_time = Webconfig::getValue('cancel_appointment_time');
  167. if ($cancel_appointment_time && time() > ($application->getData('create_time') + $cancel_appointment_time * 3600)) {
  168. $this->json_error("超过{$cancel_appointment_time}小时后,不可取消");
  169. }
  170. if (false === $application->save(['status' => AppointmentApplication::STATUS_CANCEL, 'finish_time' => time()])) {
  171. $this->json_error("提交失败");
  172. } else {
  173. $this->json_success("提交成功");
  174. }
  175. }
  176. //我的预约列表
  177. public function applicationList()
  178. {
  179. $post = $this->request->param();
  180. $validate = new \think\Validate([
  181. ['status', 'number'],
  182. ['page', 'number'],
  183. ['pagenum', 'number|<=:1000']
  184. ]);
  185. if (!$validate->check($post)) {
  186. $this->json_error('提交失败:' . $validate->getError());
  187. }
  188. $where = ['user_id' => $this->getUserId()];
  189. $status = $this->request->param('status', 0, 'intval');
  190. if ($status == 1) {
  191. $where['finish_time'] = 0;
  192. } elseif ($status == 2) {
  193. $where['finish_time'] = ['>', 0];
  194. }
  195. $pagenum = $this->request->param('pagenum', 20, 'intval');
  196. $datalist = (new AppointmentApplication())->where($where)->order('create_time desc')->paginate($pagenum, true);
  197. foreach ($datalist as $key => $item) {
  198. //专家信息
  199. $item->address;
  200. $item->specialist;
  201. $item->specialist->head_pic = geturl($item->specialist->head_pic, '', true);
  202. $datalist[$key] = $item;
  203. }
  204. if (empty($datalist)) {
  205. $this->json_error("没有数据");
  206. }
  207. $this->json_success("查询成功", $datalist);
  208. }
  209. //提交反馈接口
  210. public function feedback()
  211. {
  212. $post = $this->request->param();
  213. //验证
  214. $validate = new \think\Validate([
  215. ['content|内容', 'require|max:500'],
  216. ]);
  217. if (!$validate->check($post)) {
  218. $this->json_error('提交失败:' . $validate->getError());
  219. }
  220. $model = new Feedback();
  221. $data = [
  222. 'user_id' => $this->getUserId(),
  223. 'content' => $this->request->param('content', '', 'htmlspecialchars')
  224. ];
  225. if (false === $model->allowField(true)->save($data)) {
  226. $this->json_error("提交失败");
  227. } else {
  228. $this->json_success("提交成功");
  229. }
  230. }
  231. }