Couponuser.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-user
  9. */
  10. class Couponuser extends Backend
  11. {
  12. /**
  13. * Couponuser模型对象
  14. * @var \app\admin\model\Couponuser
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Couponuser;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. $this->view->assign("genderList", $this->model->getGenderList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. $coupon_id = input('coupon_id', 0, 'intval'); //优惠券id
  35. if (!$coupon_id) {
  36. $coupon_id = input('ids', 0, 'intval'); //从优惠券列表第一次跳转过来
  37. }
  38. $this->assignconfig('coupon_id', $coupon_id);
  39. //当前是否为关联查询
  40. $this->relationSearch = false;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. foreach ($list as $row) {
  54. $row->visible(['id','username','nickname','mobile','avatar','money','status','realname','gender','birthday','idcard','passport','emergencycontact','contactmobile','outdoorduration','invite_no','growthvalue','growthlevel','experiencelevel','experiencetime','maxlevel','freenumber']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 发放优惠券给用户
  63. */
  64. public function giveout() {
  65. $ids = input('ids', 0, 'intval'); //用户id
  66. $coupon_id = input('coupon_id', 0, 'intval'); //优惠券id
  67. if (!$ids || !$coupon_id) {
  68. $this->error('参数缺失');
  69. }
  70. $info = Db::name('coupon')->find($coupon_id);
  71. if (!$info) {
  72. $this->error('优惠券不存在');
  73. }
  74. if ($info['purpose'] != 3) {
  75. $this->error('优惠券类型错误');
  76. }
  77. $coupon_data = [
  78. 'user_id' => $ids,
  79. 'coupon_id' => $info['id'],
  80. 'title' => $info['title'],
  81. 'desc' => $info['desc'],
  82. 'type' => $info['type'],
  83. 'money' => $info['money'],
  84. 'minmoney' => $info['minmoney'],
  85. 'purpose' => $info['purpose'],
  86. 'starttime' => time(),
  87. 'endtime' => time() + $info['effectiveday'] * 86400,
  88. 'createtime' => time()
  89. ];
  90. $coupon_rs = Db::name('user_coupon')->insertGetId($coupon_data);
  91. if (!$coupon_rs) {
  92. $this->error('发放失败');
  93. }
  94. $this->success('发放成功');
  95. }
  96. }