LuckdrawConfig.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\data\controller;
  3. use app\wechat\service\WechatService;
  4. use think\admin\Controller;
  5. use think\admin\extend\CodeExtend;
  6. /**
  7. * 抽奖活动配置
  8. * Class LuckdrawConfig
  9. * @package app\data\controller
  10. */
  11. class LuckdrawConfig extends Controller
  12. {
  13. /**
  14. * 绑定数据表
  15. * @var string
  16. */
  17. protected $table = 'ActivityLuckdrawConfig';
  18. /**
  19. * 抽奖活动配置
  20. * @auth true
  21. * @menu true
  22. * @throws \think\Exception
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. * @throws \think\exception\PDOException
  27. */
  28. public function index()
  29. {
  30. if ($this->request->get('action') === 'qrc') try {
  31. [$wechat, $code] = [WechatService::WeChatQrcode(), $this->request->get('code', '')];
  32. $short = $wechat->shortUrl(url("@data/app.luckdraw/index/code/{$code}", [], true, true)->build());
  33. $result = $wechat->create("reply#text:活动地址:\n{$short['short_url']}");
  34. $this->success('生成二维码成功!', "javascript:$.previewImage('{$wechat->url($result['ticket'])}')");
  35. } catch (\think\exception\HttpResponseException $exception) {
  36. throw $exception;
  37. } catch (\Exception $exception) {
  38. $this->error("生成二维码失败,请稍候再试!<br> {$exception->getMessage()}");
  39. }
  40. $this->title = '抽奖活动管理';
  41. $query = $this->_query($this->table)->like('code,name')->equal('status');
  42. $query->dateBetween('create_at')->where(['deleted' => 0])->order('sort desc,id desc')->page();
  43. }
  44. /**
  45. * 添加抽奖活动
  46. * @auth true
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function add()
  52. {
  53. $this->title = '添加抽奖活动';
  54. $this->_form($this->table, 'form', 'code');
  55. }
  56. /**
  57. * 编辑抽奖活动
  58. * @auth true
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function edit()
  64. {
  65. $this->title = '编辑抽奖活动';
  66. $this->_form($this->table, 'form', 'code');
  67. }
  68. /**
  69. * 表单数据处理
  70. * @param array $vo
  71. * @throws \think\Exception
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. * @throws \think\exception\PDOException
  76. */
  77. protected function _form_filter(array &$vo)
  78. {
  79. $vo['code'] = $vo['code'] ?? CodeExtend::uniqidDate(16, 'A');
  80. if ($this->request->isGet()) {
  81. $this->prizes = $this->app->db->name('ActivityLuckdrawPrize')->where(['deleted' => 0, 'status' => 1])->select()->toArray();
  82. $this->selectPrizes = $this->app->db->name('ActivityLuckdrawConfigRecord')->where(['code' => $vo['code']])->select()->toArray();
  83. } elseif ($this->request->isPost()) {
  84. [$post, $records] = [$this->request->post(), []];
  85. if (empty($post['cover'])) $this->error('活动图片不能为空!');
  86. if (empty($post['prize_code']) || !is_array($post['prize_code'])) $this->error('请配置奖品信息!');
  87. $prizes = $this->app->db->name('ActivityLuckdrawPrize')->whereIn('code', $post['prize_code'])->select();
  88. foreach (array_keys($post['prize_code']) as $key) foreach ($prizes as $pz) {
  89. if (intval($pz['code']) === intval($post['prize_code'][$key])) $records[] = [
  90. 'code' => $vo['code'],
  91. 'prize_code' => $pz['code'],
  92. 'prize_name' => $pz['name'],
  93. 'prize_cover' => $pz['cover'],
  94. 'prize_num' => $post['prize_num'][$key],
  95. 'prize_rate' => $post['prize_rate'][$key],
  96. 'prize_level' => $post['prize_level'][$key],
  97. ];
  98. }
  99. $this->app->db->name('ActivityLuckdrawConfigRecord')->where(['code' => $vo['code']])->delete();
  100. $this->app->db->name('ActivityLuckdrawConfigRecord')->insertAll($records);
  101. }
  102. }
  103. /**
  104. * 保存成功后的处理
  105. * @param boolean $result
  106. */
  107. protected function _form_result(bool $result)
  108. {
  109. if ($result) {
  110. $this->success('活动配置成功!', 'javascript:history.back()');
  111. }
  112. }
  113. /**
  114. * 修改活动状态
  115. * @auth true
  116. * @throws \think\db\exception\DbException
  117. */
  118. public function state()
  119. {
  120. $this->_save($this->table, $this->_vali([
  121. 'status.in:0,1' => '状态值范围异常!',
  122. 'status.require' => '状态值不能为空!',
  123. ]));
  124. }
  125. /**
  126. * 删除抽奖活动
  127. * @auth true
  128. * @throws \think\db\exception\DbException
  129. */
  130. public function remove()
  131. {
  132. $this->_delete($this->table);
  133. }
  134. }