RedpacketActive.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\City;
  3. use App\Models\Custom;
  4. use App\Models\Product;
  5. use App\Facades\Servers\WechatMini\Mini;
  6. use App\Http\Requests\Admin\Redpacket\Active as Request;
  7. use App\Models\Redpacket\Active as Model;
  8. use App\Models\Redpacket\ActiveReward;
  9. use App\Models\WeiBan\Taglist as WeiBanTagList;
  10. /**
  11. * 优惠券自动发放规则
  12. *
  13. * @author 刘相欣
  14. *
  15. */
  16. class RedpacketActive extends Auth{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','红包活动');
  20. $this->assign('breadcrumb2','余额红包');
  21. }
  22. /**
  23. * 列表页
  24. *
  25. * */
  26. public function index(Model $Model){
  27. // 接收参数
  28. $name = request('name','');
  29. // 查询条件
  30. $map = [];
  31. // 组合条件
  32. if( $name ) $map[] = ['name','=',$name];
  33. // 查询数据
  34. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10))->appends(request()->all());
  35. // 循环处理数据
  36. foreach ($list as $key => $value) {
  37. // 小程序链接
  38. $value['mp_urllink']= $this->getUrlLink($value['id']);
  39. // 重组
  40. $list[$key] = $value;
  41. }
  42. // 分配数据
  43. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  44. $this->assign('list',$list);
  45. // 加载模板
  46. return $this->fetch();
  47. }
  48. /**
  49. * 获取小程序链接
  50. *
  51. */
  52. private function getUrlLink($id){
  53. // 结果数据
  54. $link = cache('admin:redpacket:active:urllink:'.$id);
  55. // 不存在数据
  56. if ( is_null($link) ) {
  57. // 从数据库获取数据
  58. $link = Mini::getUrlLink('pages/redpacket/active','id='.$id);
  59. // 存起来
  60. cache(['admin:redpacket:active:urllink:'.$id=>$link],$link ? now()->addDays(28) : now()->addMinutes(3));
  61. }
  62. // 返回结果
  63. return $link;
  64. }
  65. /**
  66. * 添加
  67. *
  68. * */
  69. public function add(Request $request,Model $Model,City $City,WeiBanTagList $WeiBanTagList,Product $Product,Custom $Custom){
  70. if( request()->isMethod('post') ){
  71. // 验证参数
  72. $request->scene('add')->validate();
  73. // 接收数据
  74. $data['logo'] = request('logo','');
  75. $data['name'] = request('name','');
  76. $data['rule'] = request('rule','');
  77. $data['freq'] = request('freq',0);
  78. $data['lucky_num'] = request('lucky_num',0);
  79. $data['is_arrive'] = request('is_arrive',0);
  80. $data['max_reward'] = request('max_reward',0);
  81. $data['start_time'] = request('start_time','');
  82. $data['end_time'] = request('end_time','');
  83. $data['tag_scope_type'] = request('tag_scope_type',0);
  84. $data['custom_type'] = request('custom_type',0);
  85. $data['order_start_time']= request('order_start_time','');
  86. $data['order_end_time'] = request('order_end_time','');
  87. $data['custom_start_time']= request('custom_start_time','');
  88. $data['custom_end_time']= request('custom_end_time','');
  89. $cityIds = request('city_ids',[]);
  90. $tagScope = request('tag_scope',[]);
  91. $tagExcept = request('tag_except',[]);
  92. $productScope = request('product_scope','');
  93. $customScope = request('custom_scope','');
  94. $phoneScope = request('phone_scope','');
  95. // 时间
  96. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  97. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  98. // 报单
  99. $data['is_order'] = request('is_order',0);
  100. $data['order_start_time']= $data['order_start_time'] ? strtotime($data['order_start_time']) : 0;
  101. $data['order_end_time'] = $data['order_end_time'] ? strtotime($data['order_end_time']) : 0;
  102. // 客户注册
  103. $data['custom_start_time']= $data['custom_start_time'] ? strtotime($data['custom_start_time']) : 0;
  104. $data['custom_end_time']= $data['custom_end_time'] ? strtotime($data['custom_end_time']) : 0;
  105. // 城市和标签
  106. $data['city_ids'] = implode(',',$cityIds);
  107. $data['tag_scope'] = implode(',',$tagScope);
  108. $data['tag_except'] = implode(',',$tagExcept);
  109. // 范围
  110. $productScope = scope_to_array($productScope);
  111. $phoneScope = scope_to_array($phoneScope);
  112. $customScope = scope_to_array($customScope);
  113. // 产品编码批量循环转ID
  114. foreach ($productScope as $key => $code) {
  115. // 获取产品ID
  116. $code = $Product->codeToId($code);
  117. // 如果存在ID
  118. if ( !$code ) {
  119. // 删除
  120. unset($productScope[$key]);
  121. continue;
  122. }
  123. // 重组
  124. $productScope[$key] = $code;
  125. }
  126. // 产品编码批量循环转ID
  127. foreach ($customScope as $key => $code) {
  128. // 获取产品ID
  129. $code = $Custom->codeToId($code);
  130. // 如果存在ID
  131. if ( !$code ) {
  132. // 删除
  133. unset($customScope[$key]);
  134. continue;
  135. }
  136. // 重组
  137. $customScope[$key] = $code;
  138. }
  139. // 转字符串存储
  140. $data['product_scope'] = implode(',',$productScope);
  141. $data['phone_scope'] = implode(',',$phoneScope);
  142. $data['custom_scope'] = implode(',',$customScope);
  143. // 状态
  144. $data['status'] = 1;
  145. // 如果要下单的话,判断订单时间
  146. if( $data['custom_type'] ) {
  147. if( !$data['custom_start_time'] || !$data['custom_end_time'] ) return json_send(['code'=>'error','msg'=>'限定注册时间']);
  148. }
  149. // 如果要下单的话,判断订单时间
  150. if( $data['product_scope'] ) {
  151. if( !$data['is_order'] ) return json_send(['code'=>'error','msg'=>'限定下单产品时,是否报单请选择是,并限定下单时间']);
  152. }
  153. // 如果要下单的话,判断订单时间
  154. if( $data['is_order'] ) {
  155. if( !$data['order_start_time'] || !$data['order_end_time'] ) return json_send(['code'=>'error','msg'=>'下单时间不能为空']);
  156. }
  157. // 如果操作失败
  158. if( $data['max_reward'] > $data['lucky_num'] ) return json_send(['code'=>'error','msg'=>'中奖次数不能大于抽奖次数']);
  159. // 写入数据表
  160. $id = $Model->add($data);
  161. // 如果操作失败
  162. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  163. // 记录行为
  164. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  165. // 告知结果
  166. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  167. }
  168. // 获取列表
  169. $cityList = $City->getCityList();
  170. // 标签列表
  171. $tagData = $WeiBanTagList->query()->get(['tag_group_name as group','tag_name as name'])->toArray();
  172. // 标签列表
  173. $tagList = [];
  174. // 循环数据
  175. foreach ($tagData as $value) {
  176. $tagList[$value['group']][] = $value['name'];
  177. }
  178. // 分配数据
  179. $this->assign('cityList',$cityList);
  180. $this->assign('tagList',$tagList);
  181. // 分配数据
  182. $this->assign('crumbs','新增');
  183. // 加载模板
  184. return $this->fetch();
  185. }
  186. /**
  187. * 修改
  188. *
  189. * */
  190. public function edit(Request $request,Model $Model,City $City,WeiBanTagList $WeiBanTagList,Product $Product,Custom $Custom){
  191. // 接收参数
  192. $id = request('id',0);
  193. // 查询用户
  194. $oldData = $Model->where(['id'=>$id])->first();
  195. // 修改
  196. if(request()->isMethod('post')){
  197. // 验证参数
  198. $request->scene('edit')->validate();
  199. /// 接收数据
  200. $data['logo'] = request('logo','');
  201. $data['name'] = request('name','');
  202. $data['rule'] = request('rule','');
  203. $data['freq'] = request('freq',0);
  204. $data['lucky_num'] = request('lucky_num',0);
  205. $data['is_arrive'] = request('is_arrive',0);
  206. $data['max_reward'] = request('max_reward',0);
  207. $data['start_time'] = request('start_time','');
  208. $data['end_time'] = request('end_time','');
  209. $data['tag_scope_type'] = request('tag_scope_type',0);
  210. $data['custom_type'] = request('custom_type',0);
  211. $data['order_start_time']= request('order_start_time','');
  212. $data['order_end_time'] = request('order_end_time','');
  213. $data['custom_start_time']= request('custom_start_time','');
  214. $data['custom_end_time']= request('custom_end_time','');
  215. $cityIds = request('city_ids',[]);
  216. $tagScope = request('tag_scope',[]);
  217. $tagExcept = request('tag_except',[]);
  218. $productScope = request('product_scope','');
  219. $customScope = request('custom_scope','');
  220. $phoneScope = request('phone_scope','');
  221. // 时间
  222. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  223. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  224. // 报单
  225. $data['is_order'] = request('is_order',0);
  226. $data['order_start_time']= $data['order_start_time'] ? strtotime($data['order_start_time']) : 0;
  227. $data['order_end_time'] = $data['order_end_time'] ? strtotime($data['order_end_time']) : 0;
  228. // 客户注册
  229. $data['custom_start_time']= $data['custom_start_time'] ? strtotime($data['custom_start_time']) : 0;
  230. $data['custom_end_time']= $data['custom_end_time'] ? strtotime($data['custom_end_time']) : 0;
  231. // 城市和标签
  232. $data['city_ids'] = implode(',',$cityIds);
  233. $data['tag_scope'] = implode(',',$tagScope);
  234. $data['tag_except'] = implode(',',$tagExcept);
  235. // 范围
  236. $productScope = scope_to_array($productScope);
  237. $phoneScope = scope_to_array($phoneScope);
  238. $customScope = scope_to_array($customScope);
  239. // 产品编码批量循环转ID
  240. foreach ($productScope as $key => $code) {
  241. // 获取产品ID
  242. $code = $Product->codeToId($code);
  243. // 如果存在ID
  244. if ( !$code ) {
  245. // 删除
  246. unset($productScope[$key]);
  247. continue;
  248. }
  249. // 重组
  250. $productScope[$key] = $code;
  251. }
  252. // 产品编码批量循环转ID
  253. foreach ($customScope as $key => $code) {
  254. // 获取产品ID
  255. $code = $Custom->codeToId($code);
  256. // 如果存在ID
  257. if ( !$code ) {
  258. // 删除
  259. unset($customScope[$key]);
  260. continue;
  261. }
  262. // 重组
  263. $customScope[$key] = $code;
  264. }
  265. // 转字符串存储
  266. $data['product_scope'] = implode(',',$productScope);
  267. $data['phone_scope'] = implode(',',$phoneScope);
  268. $data['custom_scope'] = implode(',',$customScope);
  269. // 如果要下单的话,判断订单时间
  270. if( $data['custom_type'] ) {
  271. if( !$data['custom_start_time'] || !$data['custom_end_time'] ) return json_send(['code'=>'error','msg'=>'限定注册时间']);
  272. }
  273. // 如果要下单的话,判断订单时间
  274. if( $data['product_scope'] ) {
  275. if( !$data['is_order'] ) return json_send(['code'=>'error','msg'=>'限定下单产品时,是否报单请选择是,并限定下单时间']);
  276. }
  277. // 如果要下单的话,判断订单时间
  278. if( $data['is_order'] ) {
  279. if( !$data['order_start_time'] || !$data['order_end_time'] ) return json_send(['code'=>'error','msg'=>'下单时间不能为空']);
  280. }
  281. // 如果操作失败
  282. if( $data['max_reward'] > $data['lucky_num'] ) return json_send(['code'=>'error','msg'=>'中奖次数不能大于抽奖次数']);
  283. // 写入数据表
  284. $result = $Model->edit($id,$data);
  285. // 如果操作失败
  286. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  287. // 记录行为
  288. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  289. // 告知结果
  290. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  291. }
  292. // 错误告知
  293. if( !$oldData ) return $this->error('查无数据');
  294. // 获取城市ID
  295. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  296. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  297. $oldData['tag_except'] = explode(',',$oldData['tag_except']);
  298. $productScope = $oldData['product_scope'] ? explode(',',$oldData['product_scope']) : [];
  299. $phoneScope = $oldData['phone_scope'] ? explode(',',$oldData['phone_scope']) : [];
  300. $customScope = $oldData['custom_scope'] ? explode(',',$oldData['custom_scope']) : [];
  301. // 产品编码批量循环转ID
  302. foreach ($productScope as $key => $value) {
  303. // 重组
  304. $productScope[$key] = $Product->idToCode($value);
  305. }
  306. // 产品编码批量循环转ID
  307. foreach ($customScope as $key => $value) {
  308. // 重组
  309. $customScope[$key] = $Custom->idToCode($value);
  310. }
  311. // 转数据
  312. $oldData['product_scope'] = implode("\n",$productScope);
  313. $oldData['phone_scope'] = implode("\n",$phoneScope);
  314. $oldData['custom_scope'] = implode("\n",$customScope);
  315. // 获取列表
  316. $cityList = $City->getCityList();
  317. // 标签列表
  318. $tagData = $WeiBanTagList->query()->get(['tag_group_name as group','tag_name as name'])->toArray();
  319. // 标签列表
  320. $tagList = [];
  321. // 循环数据
  322. foreach ($tagData as $value) {
  323. $tagList[$value['group']][] = $value['name'];
  324. }
  325. // 分配数据
  326. $this->assign('cityList',$cityList);
  327. $this->assign('tagList',$tagList);
  328. $this->assign('oldData',$oldData);
  329. $this->assign('crumbs','修改');
  330. // 加载模板
  331. return $this->fetch();
  332. }
  333. /**
  334. * 修改状态
  335. *
  336. * */
  337. public function set_status(Request $request,Model $Model,ActiveReward $ActiveReward){
  338. // 验证参数
  339. $request->scene('set_status')->validate();
  340. // 设置状态
  341. $id = request('id',0);
  342. $status = request('status',0);
  343. // 查询用户
  344. $oldData = $Model->where(['id'=>$id])->first();
  345. // 如果用户不存在
  346. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  347. // 如果设置上架
  348. if( !$status ) {
  349. // 判断奖品个数
  350. $count = $ActiveReward->query()->where([['active_id','=',$id],['status','=',0]])->count();
  351. // 获取统计数量
  352. if( !$count ) return json_send(['code'=>'error','msg'=>'请设置奖项再启用']);
  353. }
  354. // 执行修改
  355. $result = $Model->edit($id,['status'=>$status]);
  356. // 提示新增失败
  357. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  358. // 记录行为
  359. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  360. // 告知结果
  361. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  362. }
  363. }