GiftController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-02-17
  10. // +—————————————————————————————————————————————————————————————————————
  11. /**
  12. * 礼物
  13. */
  14. namespace app\admin\controller;
  15. use cmf\controller\AdminBaseController;
  16. use think\Db;
  17. class GiftController extends AdminbaseController {
  18. protected function getTypes($k=''){
  19. $type=[
  20. '0'=>'普通礼物',
  21. '1'=>'豪华礼物',
  22. ];
  23. if($k==''){
  24. return $type;
  25. }
  26. return isset($type[$k]) ? $type[$k]: '';
  27. }
  28. protected function getMark($k=''){
  29. $mark=[
  30. '0'=>'普通',
  31. '1'=>'热门',
  32. ];
  33. if($k==''){
  34. return $mark;
  35. }
  36. return isset($mark[$k]) ? $mark[$k]: '';
  37. }
  38. protected function getSwftype($k=''){
  39. $swftype=[
  40. '0'=>'GIF',
  41. '1'=>'SVGA',
  42. ];
  43. if($k==''){
  44. return $swftype;
  45. }
  46. return isset($swftype[$k]) ? $swftype[$k]: '';
  47. }
  48. public function index(){
  49. $lists = Db::name("gift")
  50. ->where('type!=2')
  51. ->order("list_order asc,id desc")
  52. ->paginate(20);
  53. $lists->each(function($v,$k){
  54. $v['gifticon']=get_upload_path($v['gifticon']);
  55. $v['swf']=get_upload_path($v['swf']);
  56. return $v;
  57. });
  58. $page = $lists->render();
  59. $this->assign('lists', $lists);
  60. $this->assign("page", $page);
  61. $this->assign("type", $this->getTypes());
  62. $this->assign("mark", $this->getMark());
  63. $this->assign("swftype", $this->getSwftype());
  64. return $this->fetch();
  65. }
  66. public function del(){
  67. $id = $this->request->param('id', 0, 'intval');
  68. $rs = DB::name('gift')->where("id={$id}")->delete();
  69. if(!$rs){
  70. $this->error("删除失败!");
  71. }
  72. $this->resetcache();
  73. $this->success("删除成功!");
  74. }
  75. //排序
  76. public function listOrder() {
  77. $model = DB::name('gift');
  78. parent::listOrders($model);
  79. $this->resetcache();
  80. $this->success("排序更新成功!");
  81. }
  82. public function add(){
  83. $this->assign("type", $this->getTypes());
  84. $this->assign("mark", $this->getMark());
  85. $this->assign("swftype", $this->getSwftype());
  86. return $this->fetch();
  87. }
  88. public function addPost(){
  89. if ($this->request->isPost()) {
  90. $data = $this->request->param();
  91. $giftname=$data['giftname'];
  92. if($giftname == ''){
  93. $this->error('请输入名称');
  94. }else{
  95. $check = Db::name('gift')->where("giftname='{$giftname}'")->find();
  96. if($check){
  97. $this->error('名称已存在');
  98. }
  99. }
  100. $needcoin=$data['needcoin'];
  101. $gifticon=$data['gifticon'];
  102. if($needcoin==''){
  103. $this->error('请输入价格');
  104. }
  105. if(!is_numeric($needcoin)){
  106. $this->error('价格必须为数字');
  107. }
  108. if($needcoin<1){
  109. $this->error('价格必须为大于1的整数');
  110. }
  111. if(!$needcoin){
  112. $this->error('价格必须为大于1的整数');
  113. }
  114. if(floor($needcoin)!=$needcoin){
  115. $this->error('价格必须为大于1的整数');
  116. }
  117. if($gifticon==''){
  118. $this->error('请上传图片');
  119. }
  120. $swftype=$data['swftype'];
  121. $data['swf']=$data['gif'];
  122. if($swftype==1){
  123. $data['swf']=$data['svga'];
  124. }
  125. if($data['type']==1 && $data['swf']==''){
  126. $this->error('请上传动画效果');
  127. }
  128. $data['gifticon']=set_upload_path($data['gifticon']);
  129. if($data['gif']){
  130. $data['gif']=set_upload_path($data['gif']);
  131. }
  132. if($data['svga']){
  133. $data['svga']=set_upload_path($data['svga']);
  134. }
  135. $data['addtime']=time();
  136. unset($data['gif']);
  137. unset($data['svga']);
  138. $id = DB::name('gift')->insertGetId($data);
  139. if(!$id){
  140. $this->error("添加失败!");
  141. }
  142. $this->resetcache();
  143. $this->success("添加成功!");
  144. }
  145. }
  146. public function edit(){
  147. $id = $this->request->param('id', 0, 'intval');
  148. $data=Db::name('gift')
  149. ->where("id={$id}")
  150. ->find();
  151. if(!$data){
  152. $this->error("信息错误");
  153. }
  154. $this->assign("type", $this->getTypes());
  155. $this->assign("mark", $this->getMark());
  156. $this->assign("swftype", $this->getSwftype());
  157. $this->assign('data', $data);
  158. return $this->fetch();
  159. }
  160. public function editPost(){
  161. if ($this->request->isPost()) {
  162. $data = $this->request->param();
  163. $id=$data['id'];
  164. $giftname=$data['giftname'];
  165. if($giftname == ''){
  166. $this->error('请输入名称');
  167. }else{
  168. $check = Db::name('gift')->where("giftname='{$giftname}' and id!={$id}")->find();
  169. if($check){
  170. $this->error('名称已存在');
  171. }
  172. }
  173. $needcoin=$data['needcoin'];
  174. $gifticon=$data['gifticon'];
  175. if($needcoin==''){
  176. $this->error('请输入价格');
  177. }
  178. if(!is_numeric($needcoin)){
  179. $this->error('价格必须为数字');
  180. }
  181. if($needcoin<1){
  182. $this->error('价格必须为大于1的整数');
  183. }
  184. if(!$needcoin){
  185. $this->error('价格必须为大于1的整数');
  186. }
  187. if(floor($needcoin)!=$needcoin){
  188. $this->error('价格必须为大于1的整数');
  189. }
  190. if($gifticon==''){
  191. $this->error('请上传图片');
  192. }
  193. $gifticon_old=$data['gifticon_old'];
  194. if($gifticon!=$gifticon_old){
  195. $data['gifticon']=set_upload_path($gifticon);
  196. }
  197. $gif=$data['gif'];
  198. if($gif){
  199. $gif_old=$data['gif_old'];
  200. if($gif!=$gif_old){
  201. $data['gif']=set_upload_path($gif);
  202. }
  203. }
  204. $svga=$data['svga'];
  205. if($svga){
  206. $svga_old=$data['svga_old'];
  207. if($svga!=$svga_old){
  208. $data['svga']=set_upload_path($svga);
  209. }
  210. }
  211. $swftype=$data['swftype'];
  212. $data['swf']=$data['gif'];
  213. if($swftype==1){
  214. $data['swf']=$data['svga'];
  215. }
  216. if($data['type']==1 && $data['swf']==''){
  217. $this->error('请上传动画效果');
  218. }
  219. unset($data['gif']);
  220. unset($data['svga']);
  221. unset($data['gifticon_old']);
  222. unset($data['gif_old']);
  223. unset($data['svga_old']);
  224. $rs = DB::name('gift')->update($data);
  225. if($rs===false){
  226. $this->error("修改失败!");
  227. }
  228. $this->resetcache();
  229. $this->success("修改成功!");
  230. }
  231. }
  232. public function resetcache(){
  233. $key='getGiftList';
  234. $rs=DB::name('gift')
  235. ->field("id,type,mark,giftname,needcoin,gifticon,swftime")
  236. ->where('type!=2')
  237. ->order("list_order asc,id desc")
  238. ->select();
  239. if($rs){
  240. setcaches($key,$rs);
  241. }else{
  242. delcache($key);
  243. }
  244. return 1;
  245. }
  246. }