Agent.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\worker\controller;
  3. use app\worker\BaseController;
  4. use app\common\model\Worker as WorkerModel;
  5. use app\common\model\Agent as AgentModel;
  6. use app\common\model\Broker as BrokerModel;
  7. use app\common\model\User as UserModel;
  8. use app\common\validate\Agent as AgentValidate;
  9. use think\exception\ValidateException;
  10. use think\facade\Session;
  11. use think\facade\Db;
  12. use think\facade\Request;
  13. class Agent extends BaseController
  14. {
  15. public function agentList()
  16. {
  17. return view('agent/agentlist');
  18. }
  19. public function agentForm()
  20. {
  21. $workerid = $this->access_worker['id'];
  22. $id = input('id/d, 0');
  23. $agent = AgentModel::where('workerid','=',$workerid)->findOrEmpty($id);
  24. return view('agent/agentform',[
  25. 'agent' => $agent,
  26. 'worker' => $this->access_worker
  27. ]);
  28. }
  29. public function editAgent()
  30. {
  31. $workerid = $this->access_worker['id'];
  32. $id = input('id/d');
  33. $vdata = array(
  34. 'id' => $id,
  35. 'loginname' => input('loginname/s', ""),
  36. 'idnumber' => input('idnumber/s', "")
  37. );
  38. try {
  39. validate(AgentValidate::class)->check($vdata);
  40. } catch (ValidateException $e) {
  41. exit(json_encode(array(
  42. 'code' => 1,
  43. 'msg' => $e->getError()
  44. )));
  45. }
  46. $muser = UserModel::where(['mobile'=>input('musermobile/s', '')])->findOrEmpty();
  47. if ($muser->isEmpty()){
  48. exit(json_encode(array(
  49. 'code' => 1,
  50. 'msg' => "关联的用户不存在。"
  51. )));
  52. }
  53. $data = [
  54. 'userid' => $muser->id,
  55. 'workerid' => $workerid,
  56. 'loginname' => input('loginname/s', ""),
  57. 'idnumber' => input('idnumber/s', ""),
  58. 'title' => input('title/s', ""),
  59. 'tilpic' => input('tilpic/s', ""),
  60. 'picall' => input('picall/a', array()),
  61. 'realname' => input('realname/s', ""),
  62. 'mobile' => input('mobile/s', ""),
  63. 'telephone' => input('telephone/s', ""),
  64. 'latitude' => input('latitude/f', 0.00),
  65. 'longitude' => input('longitude/f', 0.00),
  66. 'province' => input('province/s', ""),
  67. 'city' => input('city/s', ""),
  68. 'district' => input('district/s', ""),
  69. 'address' => input('address/s', ""),
  70. 'details' => input('details/s', ""),
  71. 'priority' => input('priority/d', 0),
  72. 'remark' => input('remark/s', ""),
  73. 'status' => input('status/d', 0)
  74. ];
  75. $password = input('password/s', "");
  76. if (empty($id)){
  77. $data['password'] = empty($password) ? md5("123456789") : md5($password);
  78. $data['createtime'] = time();
  79. $agent = AgentModel::create($data);
  80. }else{
  81. if (!empty($password)){
  82. $data['password'] = md5($password);
  83. }
  84. $agent = AgentModel::find($id);
  85. $agent->save($data);
  86. }
  87. BrokerModel::update([
  88. 'workerid' => $workerid
  89. ],['agentid'=>$agent->id]);
  90. exit(json_encode(array(
  91. 'code' => 0
  92. )));
  93. }
  94. public function agent_join()
  95. {
  96. if(Request::isAjax()){
  97. $data['realname'] = trim(input('post.realname/s'));
  98. $data['mobile'] = trim(input('post.mobile/s'));
  99. $data['address'] = trim(input('post.province/s')).trim(input('post.city/s')).trim(input('post.district/s')).trim(input('post.address/s'));
  100. $data['idcard'] = trim(input('post.idcard/s'));
  101. $data['recommender'] = trim(input('post.recommender/s'));
  102. $data['createtime'] = time();
  103. $data['status'] = (int)1;
  104. $res = Db::name('agent_form')->insert($data);
  105. if($res){
  106. $rtn['code'] = 0;
  107. $rtn['msg'] = '您的申请已提交,请静心等待';
  108. }else{
  109. $rtn['code'] = 1;
  110. $rtn['msg'] = '对不起,您的申请失败,请重新申请';
  111. }
  112. return $rtn;
  113. }else{
  114. return view('agent/agent_join');
  115. }
  116. }
  117. public function fieldAgent()
  118. {
  119. $workerid = $this->access_worker['id'];
  120. $id = input('id/d',0);
  121. $agent = AgentModel::where('workerid','=',$workerid)->findOrEmpty($id);
  122. if ($agent->isEmpty()){
  123. exit(json_encode(array(
  124. 'code' => 1,
  125. 'msg' => "信息不存在"
  126. )));
  127. }else{
  128. $agent->save([
  129. input('field/s') => input('value')
  130. ]);
  131. }
  132. exit(json_encode(array(
  133. 'code' => 0
  134. )));
  135. }
  136. public function delAgent()
  137. {
  138. $workerid = $this->access_worker['id'];
  139. $password = input('password');
  140. if ( $this->access_worker['password'] !== md5($password) ){
  141. exit(json_encode(array(
  142. 'code' => 1,
  143. 'msg' => "操作密码验证失败"
  144. )));
  145. }
  146. $idarr = input('idarr/a');
  147. $agent = AgentModel::where('workerid','=',$workerid)->whereIn('id',$idarr)->select();
  148. $result = $agent->delete();
  149. if ($result){
  150. exit(json_encode(array(
  151. 'code' => 0,
  152. 'msg' => ""
  153. )));
  154. }
  155. exit(json_encode(array(
  156. 'code' => 1,
  157. 'msg' => "删除失败,请稍后重试"
  158. )));
  159. }
  160. public function listAgent()
  161. {
  162. $workerid = $this->access_worker['id'];
  163. $limit = input('limit');
  164. $page = input('page');
  165. $map = array();
  166. $map[] = ['workerid', '=', $workerid];
  167. $keywords = input('keywords/s');
  168. if (!empty($keywords)){
  169. $map[] =['idnumber', 'like', '%'.$keywords.'%'];
  170. }
  171. $status = input('status/d');
  172. if (!empty($status)){
  173. $map[] = ['status', '=', $status];
  174. }
  175. $list = AgentModel::with(['muser'])->where($map)->order(['priority'=>'DESC','id'=>'DESC'])->limit($limit)->page($page)->append(['status_text'])->select();
  176. $count = AgentModel::where($map)->count();
  177. if ($count==0){
  178. exit(json_encode(array(
  179. 'code' => 1,
  180. 'msg' => "未查询到数据"
  181. )));
  182. }
  183. exit(json_encode(array(
  184. 'code' => 0,
  185. 'msg' => "",
  186. 'count' => $count,
  187. 'data' => $list
  188. )));
  189. }
  190. }