Agent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\common\model\User as UserModel;
  5. use app\common\model\Worker as WorkerModel;
  6. use app\common\model\Agent as AgentModel;
  7. use app\common\model\AgentForm as AgentFormModel;
  8. use app\common\model\Broker as BrokerModel;
  9. use app\common\model\AgentMoney as AgentMoneyModel;
  10. use app\common\service\AgentMoneyService;
  11. use app\common\validate\Agent as AgentValidate;
  12. use think\exception\ValidateException;
  13. use think\facade\Db;
  14. class Agent extends BaseController
  15. {
  16. public function agentList()
  17. {
  18. $workerlist = WorkerModel::order(['id' => 'desc'])->select();
  19. return view('agent/agentlist', [
  20. 'workerlist' => $workerlist,
  21. ]);
  22. }
  23. public function agentForm()
  24. {
  25. $id = input('id/d, 0');
  26. $agent = AgentModel::with(['muser'])->findOrEmpty($id);
  27. $workerlist = WorkerModel::order(['id' => 'desc'])->select();
  28. $brokerlist = BrokerModel::where('agentid', $id)->select();
  29. return view('agent/agentform', [
  30. 'workerlist' => $workerlist,
  31. 'agent' => $agent,
  32. 'brokerlist' => $brokerlist,
  33. ]);
  34. }
  35. public function editAgent()
  36. {
  37. $id = input('id/d');
  38. $vdata = [
  39. 'id' => $id,
  40. 'loginname' => input('loginname/s', ""),
  41. 'idnumber' => input('idnumber/s', ""),
  42. ];
  43. try {
  44. validate(AgentValidate::class)->check($vdata);
  45. } catch (ValidateException $e) {
  46. exit(json_encode([
  47. 'code' => 1,
  48. 'msg' => $e->getError(),
  49. ]));
  50. }
  51. $muser = UserModel::where(['mobile' => input('musermobile/s', '')])->findOrEmpty();
  52. if ($muser->isEmpty()) {
  53. exit(json_encode([
  54. 'code' => 1,
  55. 'msg' => "关联的用户不存在。",
  56. ]));
  57. }
  58. $data = [
  59. 'userid' => $muser->id,
  60. 'workerid' => input('workerid/d', 0),
  61. 'loginname' => input('loginname/s', ""),
  62. 'idnumber' => input('idnumber/s', ""),
  63. 'title' => input('title/s', ""),
  64. 'tilpic' => input('tilpic/s', ""),
  65. 'picall' => input('picall/a', []),
  66. 'realname' => input('realname/s', ""),
  67. 'mobile' => input('mobile/s', ""),
  68. 'telephone' => input('telephone/s', ""),
  69. 'latitude' => input('latitude/f', 0.00),
  70. 'longitude' => input('longitude/f', 0.00),
  71. 'province' => input('province/s', ""),
  72. 'city' => input('city/s', ""),
  73. 'district' => input('district/s', ""),
  74. 'address' => input('address/s', ""),
  75. 'details' => input('details/s', ""),
  76. 'priority' => input('priority/d', 0),
  77. 'remark' => input('remark/s', ""),
  78. 'status' => input('status/d', 1),
  79. 'is_settle' => input('is_settle/d', 1),
  80. 'type' => input('type/d', 1),
  81. 'default_broker_id' => input('default_broker_id/d', 0),
  82. ];
  83. $password = input('password/s', "");
  84. if (empty($id)) {
  85. $data['password'] = empty($password) ? md5("123456789") : md5($password);
  86. $data['createtime'] = time();
  87. $agent = AgentModel::create($data);
  88. } else {
  89. if (!empty($password)) {
  90. $data['password'] = md5($password);
  91. }
  92. $agent = AgentModel::find($id);
  93. $agent->save($data);
  94. }
  95. BrokerModel::update([
  96. 'workerid' => input('workerid/d', 0),
  97. ], ['agentid' => $agent->id]);
  98. exit(json_encode([
  99. 'code' => 0,
  100. ]));
  101. }
  102. public function fieldAgent()
  103. {
  104. $id = input('id/d', 0);
  105. $info = AgentModel::findOrEmpty($id);
  106. if ($info->isEmpty()) {
  107. exit(json_encode([
  108. 'code' => 1,
  109. 'msg' => "信息不存在",
  110. ]));
  111. } else {
  112. $info->save([
  113. input('field/s') => input('value/s', ""),
  114. ]);
  115. }
  116. exit(json_encode([
  117. 'code' => 0,
  118. ]));
  119. }
  120. public function delAgent()
  121. {
  122. $access_admin = session('access_admin');
  123. $password = input('password');
  124. if ($access_admin['password'] !== md5($password)) {
  125. exit(json_encode([
  126. 'code' => 1,
  127. 'msg' => "操作密码验证失败",
  128. ]));
  129. }
  130. $idarr = input('idarr/a');
  131. $result = Db::name('agent')->whereIn('id', $idarr)->update(['deletetime' => time()]);
  132. if ($result) {
  133. exit(json_encode([
  134. 'code' => 0,
  135. 'msg' => "",
  136. ]));
  137. }
  138. exit(json_encode([
  139. 'code' => 1,
  140. 'msg' => "删除失败,请稍后重试",
  141. ]));
  142. }
  143. public function listAgent()
  144. {
  145. $limit = input('limit');
  146. $page = input('page');
  147. $map = [];
  148. $keywords = input('keywords/s');
  149. if (!empty($keywords)) {
  150. $map[] = ['idnumber', 'like', '%' . $keywords . '%'];
  151. }
  152. $workerid = input('workerid/d');
  153. if (!empty($workerid)) {
  154. $map[] = ['workerid', '=', $workerid];
  155. }
  156. $status = input('status/d');
  157. if (!empty($status)) {
  158. $map[] = ['status', '=', $status];
  159. }
  160. $list = AgentModel::with(['worker', 'muser'])->where($map)->order(['priority' => 'DESC', 'id' => 'DESC'])->limit($limit)->page($page)->append(['status_text'])->select();
  161. $count = AgentModel::where($map)->count();
  162. if ($count == 0) {
  163. exit(json_encode([
  164. 'code' => 1,
  165. 'msg' => "未查询到数据",
  166. ]));
  167. }
  168. exit(json_encode([
  169. 'code' => 0,
  170. 'msg' => "",
  171. 'count' => $count,
  172. 'data' => $list,
  173. ]));
  174. }
  175. // 申请注册经纪人
  176. public function fagentList()
  177. {
  178. return view('agent/fagentlist');
  179. }
  180. public function fagentForm()
  181. {
  182. $id = input('id/d, 0');
  183. $fagent = AgentFormModel::findOrEmpty($id);
  184. return view('agent/fagentform', [
  185. 'fagent' => $fagent,
  186. ]);
  187. }
  188. public function editFagent()
  189. {
  190. $id = input('id/d');
  191. $fagent = AgentFormModel::findOrEmpty($id);
  192. $fagent->save([
  193. 'realname' => input('realname/s', ""),
  194. 'mobile' => input('mobile/s', ""),
  195. 'address' => input('address/s', ""),
  196. 'idcard' => input('idcard/s', ""),
  197. 'recommender' => input('recommender/s', ""),
  198. 'status' => input('status/d', 1),
  199. 'remark' => input('remark/s', ""),
  200. 'createtime' => input('createtime/s', ""),
  201. ]);
  202. exit(json_encode([
  203. 'code' => 0,
  204. ]));
  205. }
  206. public function fieldFagent()
  207. {
  208. $id = input('id/d', 0);
  209. $info = AgentFormModel::findOrEmpty($id);
  210. if ($info->isEmpty()) {
  211. exit(json_encode([
  212. 'code' => 1,
  213. 'msg' => "信息不存在",
  214. ]));
  215. } else {
  216. $info->save([
  217. input('field/s') => input('value'),
  218. ]);
  219. }
  220. exit(json_encode([
  221. 'code' => 0,
  222. ]));
  223. }
  224. public function delFagent()
  225. {
  226. $idarr = input('idarr/a');
  227. $fagent = AgentFormModel::whereIn('id', $idarr)->select();
  228. $result = $fagent->delete();
  229. if ($result) {
  230. exit(json_encode([
  231. 'code' => 0,
  232. 'msg' => "",
  233. ]));
  234. }
  235. exit(json_encode([
  236. 'code' => 1,
  237. 'msg' => "删除失败,请稍后重试",
  238. ]));
  239. }
  240. public function listFagent()
  241. {
  242. $limit = input('limit');
  243. $page = input('page');
  244. $map = [];
  245. $keywords = input('keywords/s');
  246. if (!empty($keywords)) {
  247. $map[] = ['realname|mobile', 'like', '%' . $keywords . '%'];
  248. }
  249. $status = input('status/d');
  250. if (!empty($status)) {
  251. $map[] = ['status', '=', $status];
  252. }
  253. $list = AgentFormModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text'])->select();
  254. $count = AgentFormModel::where($map)->count();
  255. if ($count == 0) {
  256. exit(json_encode([
  257. 'code' => 1,
  258. 'msg' => "未查询到数据",
  259. ]));
  260. }
  261. exit(json_encode([
  262. 'code' => 0,
  263. 'msg' => "",
  264. 'count' => $count,
  265. 'data' => $list,
  266. ]));
  267. }
  268. public function settleIncome()
  269. {
  270. $agentid = input('agentid');
  271. $value = input('value');
  272. if (empty($agentid) || $value <= 0) {
  273. exit(json_encode([
  274. 'code' => 1,
  275. 'msg' => "参数错误",
  276. ]));
  277. }
  278. $agent = AgentModel::find($agentid);
  279. if (empty($agent)) {
  280. exit(json_encode([
  281. 'code' => 1,
  282. 'msg' => "未查询到数据",
  283. ]));
  284. }
  285. if ($agent['money'] < $value) {
  286. exit(json_encode([
  287. 'code' => 1,
  288. 'msg' => "门店收益不足",
  289. ]));
  290. }
  291. $moneyService = new AgentMoneyService();
  292. $moneyService->settle($agentid, -$value, '门店结算', '平台与门店线下结算');
  293. exit(json_encode([
  294. 'code' => 0,
  295. 'msg' => "",
  296. ]));
  297. }
  298. public function moneyList()
  299. {
  300. $agentid = input('agentid');
  301. if (empty($agentid)) {
  302. exit("未查询到数据");
  303. }
  304. return view('agent/moneylist', [
  305. 'agentid' => $agentid,
  306. ]);
  307. }
  308. public function listMoney()
  309. {
  310. $agentid = input('agentid');
  311. if (empty($agentid)) {
  312. exit(json_encode([
  313. 'code' => 1,
  314. 'msg' => "未查询到数据",
  315. ]));
  316. }
  317. $limit = input('limit/d', 20);
  318. $page = input('page/d', 1);
  319. $map = [
  320. ['agentid', '=', $agentid],
  321. ];
  322. $list = AgentMoneyModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->select();
  323. $count = AgentMoneyModel::where($map)->count();
  324. if ($count == 0) {
  325. exit(json_encode([
  326. 'code' => 1,
  327. 'msg' => "未查询到数据",
  328. ]));
  329. }
  330. exit(json_encode([
  331. 'code' => 0,
  332. 'msg' => "",
  333. 'count' => $count,
  334. 'data' => $list,
  335. ]));
  336. }
  337. /**
  338. * 推广码
  339. */
  340. public function shengwaiqrcode()
  341. {
  342. $id = input('id/d', 0);
  343. error_reporting(E_ERROR);
  344. $url = request()->domain(true) . '/mobile/worker/recruit?id=' . $id;
  345. header('Content-Type: image/png');
  346. ob_clean();
  347. $errorCorrectionLevel = "L"; // 纠错级别:L、M、Q、H
  348. $matrixPointSize = "4"; //生成图片大小 :1到10
  349. \phpqrcode\QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize);
  350. exit();
  351. }
  352. }