Broker.php 5.2 KB

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