Resident.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\common\model\ResidentLog as ResidentLogModel;
  5. use app\common\model\User as UserModel;
  6. use app\common\model\Worker as WorkerModel;
  7. use app\common\model\Broker as BrokerModel;
  8. use app\common\model\Resident as ResidentModel;
  9. use app\common\validate\Resident as ResidentValidate;
  10. use think\exception\ValidateException;
  11. class Resident extends BaseController
  12. {
  13. public function residentlist()
  14. {
  15. $workerlist = WorkerModel::order(['id' => 'desc'])->select();
  16. return view('resident/residentlist', [
  17. 'workerlist' => $workerlist,
  18. ]);
  19. }
  20. public function residentForm()
  21. {
  22. $id = input('id/d, 0');
  23. $resident = ResidentModel::findOrEmpty($id);
  24. return view('resident/residentform', [
  25. 'resident' => $resident,
  26. ]);
  27. }
  28. public function editresident()
  29. {
  30. $id = input('id/d');
  31. $data = [
  32. 'title' => input('title/s', ""),
  33. 'mobile' => input('mobile/s', ""),
  34. 'weixin' => input('weixin/s', ""),
  35. 'qq' => input('qq/s', ""),
  36. 'province' => input('province/s', ""),
  37. 'city' => input('city/s', ""),
  38. 'district' => input('district/s', ""),
  39. 'details' => input('details/s', ""),
  40. 'status' => input('status/d') == 1 ? 1 : 2,
  41. ];
  42. if (empty($id)) {
  43. $vdata = [
  44. 'id' => $id,
  45. 'mobile' => input('mobile/s'),
  46. ];
  47. try {
  48. validate(ResidentValidate::class)->check($vdata);
  49. } catch (ValidateException $e) {
  50. exit(json_encode([
  51. 'code' => 1,
  52. 'msg' => $e->getError(),
  53. ]));
  54. }
  55. $user = UserModel::where(['mobile' => input('usermobile/s', '')])->findOrEmpty();
  56. if ($user->isEmpty()) {
  57. exit(json_encode([
  58. 'code' => 1,
  59. 'msg' => "关联的用户不存在。",
  60. ]));
  61. }
  62. $broker = BrokerModel::where('userid', $user['id'])->findOrEmpty();
  63. if (empty($broker)) {
  64. exit(json_encode([
  65. 'code' => 1,
  66. 'msg' => "只有经纪人才能关联。",
  67. ]));
  68. }
  69. $resident_user = ResidentModel::where('userid', $user->id)->find();
  70. if (!empty($resident_user)) {
  71. exit(json_encode([
  72. 'code' => 1,
  73. 'msg' => "该用户已是经纪人。",
  74. ]));
  75. }
  76. $data['userid'] = $user->id;
  77. $data['workerid'] = $broker['workerid'];
  78. $data['agentid'] = $broker['agentid'];
  79. $data['createtime'] = time();
  80. ResidentModel::create($data);
  81. } else {
  82. $resident = ResidentModel::find($id);
  83. $resident->save($data);
  84. }
  85. exit(json_encode([
  86. 'code' => 0,
  87. ]));
  88. }
  89. public function fieldresident()
  90. {
  91. $id = input('id/d', 0);
  92. $resident = ResidentModel::findOrEmpty($id);
  93. if ($resident->isEmpty()) {
  94. exit(json_encode([
  95. 'code' => 1,
  96. 'msg' => "信息不存在",
  97. ]));
  98. } else {
  99. $resident->save([
  100. input('field/s') => input('value'),
  101. ]);
  102. }
  103. exit(json_encode([
  104. 'code' => 0,
  105. ]));
  106. }
  107. public function delresident()
  108. {
  109. $access_admin = session('access_admin');
  110. $password = input('password');
  111. if ($access_admin['password'] !== md5($password)) {
  112. exit(json_encode([
  113. 'code' => 1,
  114. 'msg' => "操作密码验证失败",
  115. ]));
  116. }
  117. $idarr = input('idarr/a');
  118. $result = ResidentModel::whereIn('id', $idarr)->delete();
  119. if ($result) {
  120. exit(json_encode([
  121. 'code' => 0,
  122. 'msg' => "",
  123. ]));
  124. }
  125. exit(json_encode([
  126. 'code' => 1,
  127. 'msg' => "删除失败,请稍后重试",
  128. ]));
  129. }
  130. public function listresident()
  131. {
  132. $limit = input('limit/d', 20);
  133. $page = input('page/d', 1);
  134. $map = [];
  135. $keywords = input('keywords/s');
  136. if (!empty($keywords)) {
  137. $map[] = ['title', 'like', '%' . $keywords . '%'];
  138. }
  139. $status = input('status/d');
  140. if (!empty($status)) {
  141. $map[] = ['status', '=', $status];
  142. }
  143. $workerid = input('workerid/d');
  144. if (!empty($workerid)) {
  145. $map[] = ['workerid', '=', $workerid];
  146. }
  147. $list = ResidentModel::with(['worker', 'user'])->where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text'])->select();
  148. $count = ResidentModel::where($map)->count();
  149. if ($count == 0) {
  150. exit(json_encode([
  151. 'code' => 1,
  152. 'msg' => "未查询到数据",
  153. ]));
  154. }
  155. exit(json_encode([
  156. 'code' => 0,
  157. 'msg' => "",
  158. 'count' => $count,
  159. 'data' => $list,
  160. ]));
  161. }
  162. // 用户跟进记录
  163. public function follow()
  164. {
  165. return view('resident/follow');
  166. }
  167. public function listfollow()
  168. {
  169. $limit = input('limit/d', 20);
  170. $page = input('page/d', 1);
  171. $map = [];
  172. $mobile = input('mobile/s');
  173. if (!empty($mobile)) {
  174. $resident = ResidentModel::where('mobile',$mobile)->findOrEmpty();
  175. if (empty($resident)) {
  176. $map[] = ['id','=',0];
  177. } else {
  178. $map[] = ['residentid', '=', $resident['id']];
  179. }
  180. }
  181. $list = ResidentLogModel::with(['resident', 'worker'])->where($map)->order('createtime', 'DESC')->limit($limit)->page($page)->select();
  182. $count = ResidentLogModel::where($map)->count();
  183. if ($count == 0) {
  184. exit(json_encode([
  185. 'code' => 1,
  186. 'msg' => "未查询到数据",
  187. ]));
  188. }
  189. exit(json_encode([
  190. 'code' => 0,
  191. 'msg' => "",
  192. 'count' => $count,
  193. 'data' => $list,
  194. ]));
  195. }
  196. }