Broker.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Broker as BrokerValidate;
  9. use think\exception\ValidateException;
  10. class Broker extends BaseController
  11. {
  12. public function brokerList()
  13. {
  14. $workerid = $this->access_worker['id'];
  15. $agentlist = AgentModel::where('workerid','=',$workerid)->order(['id'=>'desc'])->select();
  16. return view('broker/brokerlist',[
  17. 'agentlist' => $agentlist
  18. ]);
  19. }
  20. public function brokerForm()
  21. {
  22. $workerid = $this->access_worker['id'];
  23. $id = input('id/d', 0);
  24. $broker = BrokerModel::where('workerid','=',$workerid)->findOrEmpty($id);
  25. $agentlist = AgentModel::where('workerid','=',$workerid)->order(['id'=>'desc'])->select();
  26. return view('broker/brokerform',[
  27. 'agentlist' => $agentlist,
  28. 'broker' => $broker,
  29. 'worker' => $this->access_worker
  30. ]);
  31. }
  32. public function editBroker()
  33. {
  34. $workerid = $this->access_worker['id'];
  35. $id = input('id/d', 0);
  36. $vdata = array(
  37. 'id' => $id,
  38. 'mobile' => input('mobile/s')
  39. );
  40. try {
  41. validate(BrokerValidate::class)->check($vdata);
  42. } catch (ValidateException $e) {
  43. exit(json_encode(array(
  44. 'code' => 1,
  45. 'msg' => $e->getError()
  46. )));
  47. }
  48. $muser = UserModel::where(['mobile'=>input('musermobile/s', '')])->findOrEmpty();
  49. if ($muser->isEmpty()){
  50. exit(json_encode(array(
  51. 'code' => 1,
  52. 'msg' => "关联的用户不存在。"
  53. )));
  54. }
  55. $data = [
  56. 'userid' => $muser->id,
  57. 'workerid' => $workerid,
  58. 'agentid' => input('agentid/d', 0),
  59. 'title' => input('title/s', ""),
  60. 'mobile' => input('mobile/s', ""),
  61. 'weixin' => input('weixin/s', ""),
  62. 'qq' => input('qq/s', ""),
  63. 'province' => input('province/s', ""),
  64. 'city' => input('city/s', ""),
  65. 'district' => input('district/s', ""),
  66. 'details' => input('details/s', ""),
  67. 'powerreport' => input('powerreport/d', 0)==1 ? 1 : 2,
  68. 'status' => input('status/d', 0)==1 ? 1 : 2
  69. ];
  70. if (empty($id)){
  71. $data['createtime'] = time();
  72. $broker = BrokerModel::create($data);
  73. }else{
  74. $broker = BrokerModel::find($id);
  75. $broker->save($data);
  76. }
  77. $muser->save([
  78. 'brokerid' => $broker->id
  79. ]);
  80. exit(json_encode(array(
  81. 'code' => 0
  82. )));
  83. }
  84. public function fieldBroker()
  85. {
  86. $workerid = $this->access_worker['id'];
  87. $id = input('id/d',0);
  88. $broker = BrokerModel::where('workerid','=',$workerid)->findOrEmpty($id);
  89. if ($broker->isEmpty()){
  90. exit(json_encode(array(
  91. 'code' => 1,
  92. 'msg' => "信息不存在"
  93. )));
  94. }else{
  95. $broker->save([
  96. input('field/s') => input('value/s', "")
  97. ]);
  98. }
  99. exit(json_encode(array(
  100. 'code' => 0
  101. )));
  102. }
  103. public function delBroker()
  104. {
  105. $workerid = $this->access_worker['id'];
  106. $password = input('password');
  107. if ( $this->access_worker['password'] !== md5($password) ){
  108. exit(json_encode(array(
  109. 'code' => 1,
  110. 'msg' => "操作密码验证失败"
  111. )));
  112. }
  113. $idarr = input('idarr/a');
  114. $broker = BrokerModel::where('workerid','=',$workerid)->whereIn('id',$idarr)->select();
  115. $result = $broker->delete();
  116. if ($result){
  117. exit(json_encode(array(
  118. 'code' => 0,
  119. 'msg' => ""
  120. )));
  121. }
  122. exit(json_encode(array(
  123. 'code' => 1,
  124. 'msg' => "删除失败,请稍后重试"
  125. )));
  126. }
  127. public function listBroker()
  128. {
  129. $workerid = $this->access_worker['id'];
  130. $limit = input('limit/d',20);
  131. $page = input('page/d',1);
  132. $map = array();
  133. $map[] = ['workerid', '=', $workerid];
  134. $keywords = input('keywords/s');
  135. if (!empty($keywords)){
  136. $map[] =['title', 'like', '%'.$keywords.'%'];
  137. }
  138. $status = input('status/d');
  139. if (!empty($status)){
  140. $map[] = ['status', '=', $status];
  141. }
  142. $agentid = input('agentid/d');
  143. if (!empty($agentid)){
  144. $map[] = ['agentid', '=', $agentid];
  145. }
  146. $list = BrokerModel::with(['muser','worker','agent'])->withCount(['user'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['status_text','powerreport_text'])->select();
  147. $count = BrokerModel::where($map)->count();
  148. if ($count==0){
  149. exit(json_encode(array(
  150. 'code' => 1,
  151. 'msg' => "未查询到数据"
  152. )));
  153. }
  154. exit(json_encode(array(
  155. 'code' => 0,
  156. 'msg' => "",
  157. 'count' => $count,
  158. 'data' => $list
  159. )));
  160. }
  161. }