Couponarea.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\model\SystemAdmin;
  4. use app\common\model\CouponAreaModel;
  5. use app\common\model\CouponPlanModel;
  6. use app\common\model\ShopCategory;
  7. use app\admin\traits\Curd;
  8. use app\common\controller\AdminController;
  9. use app\common\model\ThirddataLogModel;
  10. use app\common\model\ThirddataModel;
  11. use EasyAdmin\annotation\ControllerAnnotation;
  12. use EasyAdmin\annotation\NodeAnotation;
  13. use think\App;
  14. use think\facade\Db;
  15. use think\Model;
  16. /**
  17. * Class Admin
  18. * @package app\admin\controller\system
  19. * @ControllerAnnotation(title="消费券分类管理")
  20. */
  21. class Couponarea extends AdminController
  22. {
  23. public function __construct(App $app)
  24. {
  25. parent::__construct($app);
  26. $this->model = new CouponAreaModel();
  27. }
  28. use Curd;
  29. protected $allowModifyFields = [
  30. 'can_buy',
  31. 'status'
  32. ];
  33. public function index ()
  34. {
  35. if ($this->request->isAjax()) {
  36. if (input('selectFields')) {
  37. return $this->selectList();
  38. }
  39. list($page, $limit, $where) = $this->buildTableParames();
  40. $count = $this->model
  41. ->where($where)
  42. ->count();
  43. // return 11;
  44. // return json_decode('[{"price":10, "fee":1},{"price":50, "fee":0.5},{"price":100, "fee":0}]');
  45. $list = $this->model
  46. ->withoutField('password')
  47. ->withAttr('buy_time', function ($val, $data){
  48. if (!$val){return '还未配置时间';}
  49. $curdata = json_decode($val);
  50. $cu_data['day'] = sr_getcurtime($curdata->day, 'Y-m-d');
  51. $arr_time = $curdata->time;
  52. for ($i = 0; $i < count($arr_time); $i++){
  53. $val1 = $curdata->time[$i];
  54. $key = 'time'.($i+1);
  55. $cu_data[$key] = (sr_getcurtime($val1->start_time, 'H:i:s') . ' - ' . sr_getcurtime($val1->end_time, 'H:i:s'));
  56. }
  57. return ($cu_data['day']. '日'. ($cu_data['time1']?('【'.$cu_data['time1'].'】'):'' ). '|'. (isset($cu_data['time2'])?('【'.$cu_data['time2'].'】'):''). '|' .(isset($cu_data['time3'])?('【'.$cu_data['time3'].'】'):''));
  58. })
  59. ->where($where)
  60. ->page($page, $limit)
  61. ->order($this->sort)
  62. ->select();
  63. $data = [
  64. 'code' => 0,
  65. 'msg' => '',
  66. 'count' => $count,
  67. 'data' => $list,
  68. ];
  69. return json($data);
  70. }
  71. return $this->fetch();
  72. }
  73. /**
  74. * @NodeAnotation(title="添加")
  75. */
  76. public function add()
  77. {
  78. if ($this->request->isAjax()){
  79. $row = request()->post();
  80. $row['create_time'] = sr_getcurtime(time());
  81. unset($row['file']);
  82. $modelplan = new CouponPlanModel();
  83. Db::startTrans();
  84. try {
  85. $a_id = $this->model->insertGetId($row);
  86. $modelplan->insert([
  87. 'area_id'=>$a_id,
  88. 'target_num'=>$row['begin_num'],
  89. 'create_time'=>sr_getcurtime(time()),
  90. 'less_num'=>$row['begin_num'],
  91. 'buy_least'=>10,
  92. 'buy_most'=>1000
  93. ]);
  94. Db::commit();
  95. }catch (\Exception $e){
  96. Db::rollback();
  97. $this->error('添加失败'. $e->getMessage());
  98. }
  99. $this->success('添加成功');
  100. }
  101. return $this->fetch();
  102. }
  103. /**
  104. * @NodeAnotation(title="修改")
  105. */
  106. public function edit ($id)
  107. {
  108. $row = $this->model->find($id);
  109. empty($row) && $this->error('数据不存在');
  110. if ($this->request->isAjax()) {
  111. $post = $this->request->post();
  112. $rule = [];
  113. $this->validate($post, $rule);
  114. try {
  115. if (!empty($post['content'])) {
  116. $post['content'] = htmlspecialchars_decode($post['content']);
  117. }
  118. $save = $row->save($post);
  119. } catch (\Exception $e) {
  120. $this->error('保存失败');
  121. }
  122. $save ? $this->success('保存成功') : $this->error('保存失败');
  123. }
  124. $this->assign('row', $row);
  125. return $this->fetch();
  126. }
  127. /**
  128. * @NodeAnotation(title="配置时间")
  129. */
  130. public function addtime($id){
  131. $row = $this->model->find($id);
  132. empty($row) && $this->error('数据不存在');
  133. if ($this->request->isAjax()) {
  134. $post = $this->request->post();
  135. $day = strtotime($post['day']);
  136. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  137. $endToday=mktime(24,0,0,date('m'),date('d'),date('Y'));
  138. $is_today = false;
  139. if ($beginToday<=$day && $endToday>$day){
  140. $is_today = true;
  141. }
  142. $timearr = [];
  143. if ($post['time1']){
  144. $arrtime1 = explode(' - ', $post['time1']);
  145. // sr_log('aaa'.$post['day'].' '. $arrtime1[0]);
  146. $arrtime1 = [strtotime($post['day'].' '. $arrtime1[0]), strtotime($post['day'].' '. $arrtime1[1])];
  147. array_push($timearr, ['start_time' => $arrtime1[0], 'end_time' => $arrtime1[1]]);
  148. }
  149. if ($post['time2']){
  150. $arrtime2 = explode(' - ', $post['time2']);
  151. $arrtime2 = [strtotime($post['day'].' '.$arrtime2[0]), strtotime($post['day'].' '.$arrtime2[1])];
  152. array_push($timearr, ['start_time' => $arrtime2[0], 'end_time' => $arrtime2[1]]);
  153. }
  154. if ($post['time3']){
  155. $arrtime3 = explode(' - ', $post['time3']);
  156. $arrtime3 = [strtotime($post['day'].' '.$arrtime3[0]), strtotime($post['day'].' '.$arrtime3[1])];
  157. array_push($timearr, ['start_time' => $arrtime3[0], 'end_time' => $arrtime3[1]]);
  158. }
  159. if (count($timearr) == 0){
  160. $this->error('请选择时间段');
  161. }
  162. foreach ($timearr as $key=>$value){
  163. if ($timearr[$key]['start_time'] > $timearr[$key]['end_time']){
  164. $this->error('时间段开始时间比如小于结束时间');
  165. }
  166. }
  167. if (!compareDate($timearr)){
  168. $this->error('时间段交叉,保存失败');
  169. }
  170. try {
  171. $save = $row->save([
  172. 'buy_time' => json_encode(['day'=>$day, 'time'=>$timearr]),
  173. 'is_today'=>($is_today?1:0)
  174. ]);
  175. } catch (\Exception $e) {
  176. $this->error('保存失败');
  177. }
  178. $save ? $this->success('保存成功') : $this->error('保存失败');
  179. }
  180. $curdata = json_decode($row['buy_time']);
  181. if ($curdata){
  182. $cu_data['day'] = sr_getcurtime($curdata->day, 'Y-m-d');
  183. $arr_time = $curdata->time;
  184. // dd($arr_time);
  185. for ($i = 0; $i < count($arr_time); $i++){
  186. $val = $curdata->time[$i];
  187. $key = 'time'.($i+1);
  188. $cu_data[$key] = (sr_getcurtime($val->start_time, 'H:i:s') . ' - ' . sr_getcurtime($val->end_time, 'H:i:s'));
  189. }
  190. $this->assign('row', $cu_data);
  191. }
  192. return $this->fetch();
  193. }
  194. }