Agent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. 'header_image' => input('header_image/s', ""),
  83. ];
  84. $password = input('password/s', "");
  85. if (empty($id)) {
  86. $data['password'] = empty($password) ? md5("123456789") : md5($password);
  87. $data['createtime'] = time();
  88. $agent = AgentModel::create($data);
  89. } else {
  90. if (!empty($password)) {
  91. $data['password'] = md5($password);
  92. }
  93. $agent = AgentModel::find($id);
  94. $agent->save($data);
  95. }
  96. BrokerModel::update([
  97. 'workerid' => input('workerid/d', 0),
  98. ], ['agentid' => $agent->id]);
  99. exit(json_encode([
  100. 'code' => 0,
  101. ]));
  102. }
  103. public function fieldAgent()
  104. {
  105. $id = input('id/d', 0);
  106. $info = AgentModel::findOrEmpty($id);
  107. if ($info->isEmpty()) {
  108. exit(json_encode([
  109. 'code' => 1,
  110. 'msg' => "信息不存在",
  111. ]));
  112. } else {
  113. $info->save([
  114. input('field/s') => input('value/s', ""),
  115. ]);
  116. }
  117. exit(json_encode([
  118. 'code' => 0,
  119. ]));
  120. }
  121. public function delAgent()
  122. {
  123. $access_admin = session('access_admin');
  124. $password = input('password');
  125. if ($access_admin['password'] !== md5($password)) {
  126. exit(json_encode([
  127. 'code' => 1,
  128. 'msg' => "操作密码验证失败",
  129. ]));
  130. }
  131. $idarr = input('idarr/a');
  132. $result = Db::name('agent')->whereIn('id', $idarr)->update(['deletetime' => time()]);
  133. if ($result) {
  134. exit(json_encode([
  135. 'code' => 0,
  136. 'msg' => "",
  137. ]));
  138. }
  139. exit(json_encode([
  140. 'code' => 1,
  141. 'msg' => "删除失败,请稍后重试",
  142. ]));
  143. }
  144. public function listAgent()
  145. {
  146. $limit = input('limit');
  147. $page = input('page');
  148. $map = [];
  149. $keywords = input('keywords/s');
  150. if (!empty($keywords)) {
  151. $map[] = ['idnumber', 'like', '%' . $keywords . '%'];
  152. }
  153. $workerid = input('workerid/d');
  154. if (!empty($workerid)) {
  155. $map[] = ['workerid', '=', $workerid];
  156. }
  157. $status = input('status/d');
  158. if (!empty($status)) {
  159. $map[] = ['status', '=', $status];
  160. }
  161. $list = AgentModel::with(['worker', 'muser'])->where($map)->order(['priority' => 'DESC', 'id' => 'DESC'])->limit($limit)->page($page)->append(['status_text'])->select();
  162. $count = AgentModel::where($map)->count();
  163. if ($count == 0) {
  164. exit(json_encode([
  165. 'code' => 1,
  166. 'msg' => "未查询到数据",
  167. ]));
  168. }
  169. exit(json_encode([
  170. 'code' => 0,
  171. 'msg' => "",
  172. 'count' => $count,
  173. 'data' => $list,
  174. ]));
  175. }
  176. // 申请注册经纪人
  177. public function fagentList()
  178. {
  179. return view('agent/fagentlist');
  180. }
  181. public function fagentForm()
  182. {
  183. $id = input('id/d, 0');
  184. $fagent = AgentFormModel::findOrEmpty($id);
  185. return view('agent/fagentform', [
  186. 'fagent' => $fagent,
  187. ]);
  188. }
  189. public function editFagent()
  190. {
  191. $id = input('id/d');
  192. $fagent = AgentFormModel::findOrEmpty($id);
  193. $fagent->save([
  194. 'realname' => input('realname/s', ""),
  195. 'mobile' => input('mobile/s', ""),
  196. 'address' => input('address/s', ""),
  197. 'idcard' => input('idcard/s', ""),
  198. 'recommender' => input('recommender/s', ""),
  199. 'status' => input('status/d', 1),
  200. 'remark' => input('remark/s', ""),
  201. 'createtime' => input('createtime/s', ""),
  202. ]);
  203. exit(json_encode([
  204. 'code' => 0,
  205. ]));
  206. }
  207. public function fieldFagent()
  208. {
  209. $id = input('id/d', 0);
  210. $info = AgentFormModel::findOrEmpty($id);
  211. if ($info->isEmpty()) {
  212. exit(json_encode([
  213. 'code' => 1,
  214. 'msg' => "信息不存在",
  215. ]));
  216. } else {
  217. $info->save([
  218. input('field/s') => input('value'),
  219. ]);
  220. }
  221. exit(json_encode([
  222. 'code' => 0,
  223. ]));
  224. }
  225. public function delFagent()
  226. {
  227. $idarr = input('idarr/a');
  228. $fagent = AgentFormModel::whereIn('id', $idarr)->select();
  229. $result = $fagent->delete();
  230. if ($result) {
  231. exit(json_encode([
  232. 'code' => 0,
  233. 'msg' => "",
  234. ]));
  235. }
  236. exit(json_encode([
  237. 'code' => 1,
  238. 'msg' => "删除失败,请稍后重试",
  239. ]));
  240. }
  241. public function listFagent()
  242. {
  243. $limit = input('limit');
  244. $page = input('page');
  245. $map = [];
  246. $keywords = input('keywords/s');
  247. if (!empty($keywords)) {
  248. $map[] = ['realname|mobile', 'like', '%' . $keywords . '%'];
  249. }
  250. $status = input('status/d');
  251. if (!empty($status)) {
  252. $map[] = ['status', '=', $status];
  253. }
  254. $list = AgentFormModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text'])->select();
  255. $count = AgentFormModel::where($map)->count();
  256. if ($count == 0) {
  257. exit(json_encode([
  258. 'code' => 1,
  259. 'msg' => "未查询到数据",
  260. ]));
  261. }
  262. exit(json_encode([
  263. 'code' => 0,
  264. 'msg' => "",
  265. 'count' => $count,
  266. 'data' => $list,
  267. ]));
  268. }
  269. public function settleIncome()
  270. {
  271. $agentid = input('agentid');
  272. $value = input('value');
  273. if (empty($agentid) || $value <= 0) {
  274. exit(json_encode([
  275. 'code' => 1,
  276. 'msg' => "参数错误",
  277. ]));
  278. }
  279. $agent = AgentModel::find($agentid);
  280. if (empty($agent)) {
  281. exit(json_encode([
  282. 'code' => 1,
  283. 'msg' => "未查询到数据",
  284. ]));
  285. }
  286. if ($agent['money'] < $value) {
  287. exit(json_encode([
  288. 'code' => 1,
  289. 'msg' => "门店收益不足",
  290. ]));
  291. }
  292. $moneyService = new AgentMoneyService();
  293. $moneyService->settle($agentid, -$value, '门店结算', '平台与门店线下结算');
  294. exit(json_encode([
  295. 'code' => 0,
  296. 'msg' => "",
  297. ]));
  298. }
  299. public function moneyList()
  300. {
  301. $agentid = input('agentid');
  302. if (empty($agentid)) {
  303. exit("未查询到数据");
  304. }
  305. return view('agent/moneylist', [
  306. 'agentid' => $agentid,
  307. ]);
  308. }
  309. public function listMoney()
  310. {
  311. $agentid = input('agentid');
  312. if (empty($agentid)) {
  313. exit(json_encode([
  314. 'code' => 1,
  315. 'msg' => "未查询到数据",
  316. ]));
  317. }
  318. $limit = input('limit/d', 20);
  319. $page = input('page/d', 1);
  320. $map = [
  321. ['agentid', '=', $agentid],
  322. ];
  323. $list = AgentMoneyModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->select();
  324. $count = AgentMoneyModel::where($map)->count();
  325. if ($count == 0) {
  326. exit(json_encode([
  327. 'code' => 1,
  328. 'msg' => "未查询到数据",
  329. ]));
  330. }
  331. exit(json_encode([
  332. 'code' => 0,
  333. 'msg' => "",
  334. 'count' => $count,
  335. 'data' => $list,
  336. ]));
  337. }
  338. /**
  339. * 推广码
  340. */
  341. public function shengwaiqrcode()
  342. {
  343. $id = input('id/d', 0);
  344. error_reporting(E_ERROR);
  345. $url = request()->domain(true) . '/mobile/worker/index?id=' . $id;
  346. header('Content-Type: image/png');
  347. ob_clean();
  348. $errorCorrectionLevel = "L"; // 纠错级别:L、M、Q、H
  349. $matrixPointSize = "4"; //生成图片大小 :1到10
  350. \phpqrcode\QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize);
  351. exit();
  352. }
  353. }