123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Models\City;
- use App\Models\Custom;
- use App\Models\Product;
- use App\Facades\Servers\WechatMini\Mini;
- use App\Http\Requests\Admin\Redpacket\Active as Request;
- use App\Models\Redpacket\Active as Model;
- use App\Models\Redpacket\ActiveReward;
- use App\Models\WeiBan\Taglist as WeiBanTagList;
- /**
- * 优惠券自动发放规则
- *
- * @author 刘相欣
- *
- */
- class RedpacketActive extends Auth{
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','红包活动');
- $this->assign('breadcrumb2','余额红包');
- }
- /**
- * 列表页
- *
- * */
- public function index(Model $Model){
- // 接收参数
- $name = request('name','');
- // 查询条件
- $map = [];
- // 组合条件
- if( $name ) $map[] = ['name','=',$name];
- // 查询数据
- $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10))->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 小程序链接
- $value['mp_urllink']= $this->getUrlLink($value['id']);
- // 重组
- $list[$key] = $value;
- }
- // 分配数据
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $this->assign('list',$list);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 获取小程序链接
- *
- */
- private function getUrlLink($id){
- // 结果数据
- $link = cache('admin:redpacket:active:urllink:'.$id);
- // 不存在数据
- if ( is_null($link) ) {
- // 从数据库获取数据
- $link = Mini::getUrlLink('pages/redpacket/active','id='.$id);
- // 存起来
- cache(['admin:redpacket:active:urllink:'.$id=>$link],$link ? now()->addDays(28) : now()->addMinutes(3));
- }
- // 返回结果
- return $link;
- }
- /**
- * 添加
- *
- * */
- public function add(Request $request,Model $Model,City $City,WeiBanTagList $WeiBanTagList,Product $Product,Custom $Custom){
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 接收数据
- $data['logo'] = request('logo','');
- $data['name'] = request('name','');
- $data['rule'] = request('rule','');
- $data['freq'] = request('freq',0);
- $data['lucky_num'] = request('lucky_num',0);
- $data['is_arrive'] = request('is_arrive',0);
- $data['max_reward'] = request('max_reward',0);
- $data['start_time'] = request('start_time','');
- $data['end_time'] = request('end_time','');
- $data['tag_scope_type'] = request('tag_scope_type',0);
- $data['custom_type'] = request('custom_type',0);
- $data['order_start_time']= request('order_start_time','');
- $data['order_end_time'] = request('order_end_time','');
- $data['custom_start_time']= request('custom_start_time','');
- $data['custom_end_time']= request('custom_end_time','');
- $cityIds = request('city_ids',[]);
- $tagScope = request('tag_scope',[]);
- $tagExcept = request('tag_except',[]);
- $productScope = request('product_scope','');
- $customScope = request('custom_scope','');
- $phoneScope = request('phone_scope','');
-
- // 时间
- $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
- $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
- // 报单
- $data['is_order'] = request('is_order',0);
- $data['order_start_time']= $data['order_start_time'] ? strtotime($data['order_start_time']) : 0;
- $data['order_end_time'] = $data['order_end_time'] ? strtotime($data['order_end_time']) : 0;
- // 客户注册
- $data['custom_start_time']= $data['custom_start_time'] ? strtotime($data['custom_start_time']) : 0;
- $data['custom_end_time']= $data['custom_end_time'] ? strtotime($data['custom_end_time']) : 0;
- // 城市和标签
- $data['city_ids'] = implode(',',$cityIds);
- $data['tag_scope'] = implode(',',$tagScope);
- $data['tag_except'] = implode(',',$tagExcept);
- // 范围
- $productScope = scope_to_array($productScope);
- $phoneScope = scope_to_array($phoneScope);
- $customScope = scope_to_array($customScope);
- // 产品编码批量循环转ID
- foreach ($productScope as $key => $code) {
- // 获取产品ID
- $code = $Product->codeToId($code);
- // 如果存在ID
- if ( !$code ) {
- // 删除
- unset($productScope[$key]);
- continue;
- }
- // 重组
- $productScope[$key] = $code;
- }
- // 产品编码批量循环转ID
- foreach ($customScope as $key => $code) {
- // 获取产品ID
- $code = $Custom->codeToId($code);
- // 如果存在ID
- if ( !$code ) {
- // 删除
- unset($customScope[$key]);
- continue;
- }
- // 重组
- $customScope[$key] = $code;
- }
- // 转字符串存储
- $data['product_scope'] = implode(',',$productScope);
- $data['phone_scope'] = implode(',',$phoneScope);
- $data['custom_scope'] = implode(',',$customScope);
- // 状态
- $data['status'] = 1;
- // 如果要下单的话,判断订单时间
- if( $data['custom_type'] ) {
- if( !$data['custom_start_time'] || !$data['custom_end_time'] ) return json_send(['code'=>'error','msg'=>'限定注册时间']);
- }
- // 如果要下单的话,判断订单时间
- if( $data['product_scope'] ) {
- if( !$data['is_order'] ) return json_send(['code'=>'error','msg'=>'限定下单产品时,是否报单请选择是,并限定下单时间']);
- }
- // 如果要下单的话,判断订单时间
- if( $data['is_order'] ) {
- if( !$data['order_start_time'] || !$data['order_end_time'] ) return json_send(['code'=>'error','msg'=>'下单时间不能为空']);
- }
- // 如果操作失败
- if( $data['max_reward'] > $data['lucky_num'] ) return json_send(['code'=>'error','msg'=>'中奖次数不能大于抽奖次数']);
- // 写入数据表
- $id = $Model->add($data);
- // 如果操作失败
- if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
- }
- // 获取列表
- $cityList = $City->getCityList();
- // 标签列表
- $tagData = $WeiBanTagList->query()->get(['tag_group_name as group','tag_name as name'])->toArray();
- // 标签列表
- $tagList = [];
- // 循环数据
- foreach ($tagData as $value) {
- $tagList[$value['group']][] = $value['name'];
- }
- // 分配数据
- $this->assign('cityList',$cityList);
- $this->assign('tagList',$tagList);
- // 分配数据
- $this->assign('crumbs','新增');
- // 加载模板
- return $this->fetch();
- }
- /**
- * 修改
- *
- * */
- public function edit(Request $request,Model $Model,City $City,WeiBanTagList $WeiBanTagList,Product $Product,Custom $Custom){
- // 接收参数
- $id = request('id',0);
- // 查询用户
- $oldData = $Model->where(['id'=>$id])->first();
- // 修改
- if(request()->isMethod('post')){
- // 验证参数
- $request->scene('edit')->validate();
- /// 接收数据
- $data['logo'] = request('logo','');
- $data['name'] = request('name','');
- $data['rule'] = request('rule','');
- $data['freq'] = request('freq',0);
- $data['lucky_num'] = request('lucky_num',0);
- $data['is_arrive'] = request('is_arrive',0);
- $data['max_reward'] = request('max_reward',0);
- $data['start_time'] = request('start_time','');
- $data['end_time'] = request('end_time','');
- $data['tag_scope_type'] = request('tag_scope_type',0);
- $data['custom_type'] = request('custom_type',0);
- $data['order_start_time']= request('order_start_time','');
- $data['order_end_time'] = request('order_end_time','');
- $data['custom_start_time']= request('custom_start_time','');
- $data['custom_end_time']= request('custom_end_time','');
- $cityIds = request('city_ids',[]);
- $tagScope = request('tag_scope',[]);
- $tagExcept = request('tag_except',[]);
- $productScope = request('product_scope','');
- $customScope = request('custom_scope','');
- $phoneScope = request('phone_scope','');
-
- // 时间
- $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
- $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
- // 报单
- $data['is_order'] = request('is_order',0);
- $data['order_start_time']= $data['order_start_time'] ? strtotime($data['order_start_time']) : 0;
- $data['order_end_time'] = $data['order_end_time'] ? strtotime($data['order_end_time']) : 0;
- // 客户注册
- $data['custom_start_time']= $data['custom_start_time'] ? strtotime($data['custom_start_time']) : 0;
- $data['custom_end_time']= $data['custom_end_time'] ? strtotime($data['custom_end_time']) : 0;
- // 城市和标签
- $data['city_ids'] = implode(',',$cityIds);
- $data['tag_scope'] = implode(',',$tagScope);
- $data['tag_except'] = implode(',',$tagExcept);
- // 范围
- $productScope = scope_to_array($productScope);
- $phoneScope = scope_to_array($phoneScope);
- $customScope = scope_to_array($customScope);
- // 产品编码批量循环转ID
- foreach ($productScope as $key => $code) {
- // 获取产品ID
- $code = $Product->codeToId($code);
- // 如果存在ID
- if ( !$code ) {
- // 删除
- unset($productScope[$key]);
- continue;
- }
- // 重组
- $productScope[$key] = $code;
- }
- // 产品编码批量循环转ID
- foreach ($customScope as $key => $code) {
- // 获取产品ID
- $code = $Custom->codeToId($code);
- // 如果存在ID
- if ( !$code ) {
- // 删除
- unset($customScope[$key]);
- continue;
- }
- // 重组
- $customScope[$key] = $code;
- }
- // 转字符串存储
- $data['product_scope'] = implode(',',$productScope);
- $data['phone_scope'] = implode(',',$phoneScope);
- $data['custom_scope'] = implode(',',$customScope);
- // 如果要下单的话,判断订单时间
- if( $data['custom_type'] ) {
- if( !$data['custom_start_time'] || !$data['custom_end_time'] ) return json_send(['code'=>'error','msg'=>'限定注册时间']);
- }
- // 如果要下单的话,判断订单时间
- if( $data['product_scope'] ) {
- if( !$data['is_order'] ) return json_send(['code'=>'error','msg'=>'限定下单产品时,是否报单请选择是,并限定下单时间']);
- }
- // 如果要下单的话,判断订单时间
- if( $data['is_order'] ) {
- if( !$data['order_start_time'] || !$data['order_end_time'] ) return json_send(['code'=>'error','msg'=>'下单时间不能为空']);
- }
- // 如果操作失败
- if( $data['max_reward'] > $data['lucky_num'] ) return json_send(['code'=>'error','msg'=>'中奖次数不能大于抽奖次数']);
- // 写入数据表
- $result = $Model->edit($id,$data);
- // 如果操作失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
- }
- // 错误告知
- if( !$oldData ) return $this->error('查无数据');
- // 获取城市ID
- $oldData['city_ids'] = explode(',',$oldData['city_ids']);
- $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
- $oldData['tag_except'] = explode(',',$oldData['tag_except']);
- $productScope = explode(',',$oldData['product_scope']);
- $phoneScope = explode(',',$oldData['phone_scope']);
- $customScope = explode(',',$oldData['custom_scope']);
- // 产品编码批量循环转ID
- foreach ($productScope as $key => $value) {
- // 重组
- $productScope[$key] = $Product->idToCode($value);
- }
- // 产品编码批量循环转ID
- foreach ($customScope as $key => $value) {
- // 重组
- $customScope[$key] = $Custom->idToCode($value);
- }
- // 转数据
- $oldData['product_scope'] = implode("\n",$productScope);
- $oldData['phone_scope'] = implode("\n",$phoneScope);
- $oldData['custom_scope'] = implode("\n",$customScope);
- // 获取列表
- $cityList = $City->getCityList();
- // 标签列表
- $tagData = $WeiBanTagList->query()->get(['tag_group_name as group','tag_name as name'])->toArray();
- // 标签列表
- $tagList = [];
- // 循环数据
- foreach ($tagData as $value) {
- $tagList[$value['group']][] = $value['name'];
- }
- // 分配数据
- $this->assign('cityList',$cityList);
- $this->assign('tagList',$tagList);
- $this->assign('oldData',$oldData);
- $this->assign('crumbs','修改');
- // 加载模板
- return $this->fetch();
- }
- /**
- * 修改状态
- *
- * */
- public function set_status(Request $request,Model $Model,ActiveReward $ActiveReward){
- // 验证参数
- $request->scene('set_status')->validate();
- // 设置状态
- $id = request('id',0);
- $status = request('status',0);
- // 查询用户
- $oldData = $Model->where(['id'=>$id])->first();
- // 如果用户不存在
- if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
- // 如果设置上架
- if( !$status ) {
- // 判断奖品个数
- $count = $ActiveReward->query()->where([['active_id','=',$id],['status','=',0]])->count();
- // 获取统计数量
- if( !$count ) return json_send(['code'=>'error','msg'=>'请设置奖项再启用']);
- }
- // 执行修改
- $result = $Model->edit($id,['status'=>$status]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
- }
|