Broker.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $data = [
  36. 'agentid' => input('agentid/d', 0),
  37. 'title' => input('title/s', ""),
  38. 'mobile' => input('mobile/s', ""),
  39. 'weixin' => input('weixin/s', ""),
  40. 'qq' => input('qq/s', ""),
  41. 'province' => input('province/s', ""),
  42. 'city' => input('city/s', ""),
  43. 'district' => input('district/s', ""),
  44. 'details' => input('details/s', ""),
  45. 'powerreport' => input('powerreport/d', 0)==1 ? 1 : 2,
  46. 'status' => input('status/d', 0)==1 ? 1 : 2
  47. ];
  48. if (empty($id)){
  49. $id = input('id/d', 0);
  50. $vdata = array(
  51. 'id' => $id,
  52. 'mobile' => input('mobile/s')
  53. );
  54. try {
  55. validate(BrokerValidate::class)->check($vdata);
  56. } catch (ValidateException $e) {
  57. exit(json_encode(array(
  58. 'code' => 1,
  59. 'msg' => $e->getError()
  60. )));
  61. }
  62. $muser = UserModel::where(['mobile'=>input('musermobile/s', '')])->findOrEmpty();
  63. if ($muser->isEmpty()){
  64. exit(json_encode(array(
  65. 'code' => 1,
  66. 'msg' => "关联的用户不存在。"
  67. )));
  68. }
  69. $broker_user = BrokerModel::where('userid',$muser->id)->find();
  70. if (!empty($broker_user)) {
  71. exit(json_encode(array(
  72. 'code' => 1,
  73. 'msg' => "该用户已是经纪人。"
  74. )));
  75. }
  76. $data['userid'] = $muser->id;
  77. $data['workerid'] = $workerid;
  78. $data['createtime'] = time();
  79. $broker = BrokerModel::create($data);
  80. event('brokerAdd',$broker);
  81. }else{
  82. $broker = BrokerModel::find($id);
  83. $broker->save($data);
  84. }
  85. $muser->save([
  86. 'brokerid' => $broker->id
  87. ]);
  88. exit(json_encode(array(
  89. 'code' => 0
  90. )));
  91. }
  92. public function fieldBroker()
  93. {
  94. $workerid = $this->access_worker['id'];
  95. $id = input('id/d',0);
  96. $broker = BrokerModel::where('workerid','=',$workerid)->findOrEmpty($id);
  97. if ($broker->isEmpty()){
  98. exit(json_encode(array(
  99. 'code' => 1,
  100. 'msg' => "信息不存在"
  101. )));
  102. }else{
  103. $broker->save([
  104. input('field/s') => input('value/s', "")
  105. ]);
  106. }
  107. exit(json_encode(array(
  108. 'code' => 0
  109. )));
  110. }
  111. public function delBroker()
  112. {
  113. $workerid = $this->access_worker['id'];
  114. $password = input('password');
  115. if ( $this->access_worker['password'] !== md5($password) ){
  116. exit(json_encode(array(
  117. 'code' => 1,
  118. 'msg' => "操作密码验证失败"
  119. )));
  120. }
  121. $idarr = input('idarr/a');
  122. $user_check = UserModel::whereIn('brokerid',$idarr)->find();
  123. if (!empty($user_check)) {
  124. exit(json_encode(array(
  125. 'code' => 1,
  126. 'msg' => "该经纪人还有下线,请先转移再删除"
  127. )));
  128. }
  129. $broker = BrokerModel::where('workerid','=',$workerid)->whereIn('id',$idarr)->select();
  130. $result = $broker->delete();
  131. if ($result){
  132. exit(json_encode(array(
  133. 'code' => 0,
  134. 'msg' => ""
  135. )));
  136. }
  137. exit(json_encode(array(
  138. 'code' => 1,
  139. 'msg' => "删除失败,请稍后重试"
  140. )));
  141. }
  142. public function listBroker()
  143. {
  144. $workerid = $this->access_worker['id'];
  145. $limit = input('limit/d',20);
  146. $page = input('page/d',1);
  147. $map = array();
  148. $map[] = ['workerid', '=', $workerid];
  149. $keywords = input('keywords/s');
  150. if (!empty($keywords)){
  151. $map[] =['title', 'like', '%'.$keywords.'%'];
  152. }
  153. $status = input('status/d');
  154. if (!empty($status)){
  155. $map[] = ['status', '=', $status];
  156. }
  157. $agentid = input('agentid/d');
  158. if (!empty($agentid)){
  159. $map[] = ['agentid', '=', $agentid];
  160. }
  161. $list = BrokerModel::with(['muser','worker','agent'])->withCount(['user'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['status_text','powerreport_text'])->select();
  162. $count = BrokerModel::where($map)->count();
  163. if ($count==0){
  164. exit(json_encode(array(
  165. 'code' => 1,
  166. 'msg' => "未查询到数据"
  167. )));
  168. }
  169. exit(json_encode(array(
  170. 'code' => 0,
  171. 'msg' => "",
  172. 'count' => $count,
  173. 'data' => $list
  174. )));
  175. }
  176. }