AgentcodeController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\AgentCode;
  5. use app\model\Member;
  6. class AgentcodeController extends Base
  7. {
  8. function index()
  9. {
  10. $weid = weid();
  11. $keyword = input('post.keyword', '', 'serach_in');
  12. $query = AgentCode::where(['weid' => $weid]);
  13. if (!empty($keyword)) {
  14. $query->where('agent_code', 'like', '%' . $keyword . '%');
  15. }
  16. $datalist = $query->order('id asc')->select()->toArray();
  17. foreach ($datalist as &$vo) {
  18. $vo['uid'] = Member::get_name($vo['uid']);
  19. }
  20. $data['data'] = $datalist;
  21. return $this->json($data);
  22. }
  23. function listUpdate()
  24. {
  25. $data = only('id,status');
  26. if (!$data['id']) throw new ValidateException('参数错误');
  27. AgentCode::update($data);
  28. return $this->json(['msg' => '操作成功']);
  29. }
  30. public function update()
  31. {
  32. $id = $this->request->post('id');
  33. $data = input('post.');
  34. unset($data['create_time']);
  35. if (empty($id)) {
  36. $data['weid'] = weid();
  37. try {
  38. $res = AgentCode::create($data);
  39. } catch (\Exception $e) {
  40. throw new ValidateException($e->getMessage());
  41. }
  42. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  43. } else {
  44. $Agentdata = AgentCode::where(['agent_code' => $data['agent_code']])->find();
  45. if (!empty($Agentdata)) {
  46. return $this->json(['code' => 2001, 'msg' => '修改失败,这个邀请码已有人用']);
  47. }
  48. try {
  49. AgentCode::update($data);
  50. } catch (\Exception $e) {
  51. throw new ValidateException($e->getMessage());
  52. }
  53. return $this->json(['msg' => '修改成功']);
  54. }
  55. }
  56. function getInfo()
  57. {
  58. $id = $this->request->post('id', '', 'serach_in');
  59. if (!$id) throw new ValidateException('参数错误');
  60. $data = AgentCode::field('*')->find($id)->toArray();
  61. return $this->json(['data' => $data]);
  62. }
  63. function delete()
  64. {
  65. return $this->del(new AgentCode());
  66. }
  67. }