User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 优惠券管理
  7. */
  8. namespace app\ais\controller\coupon;
  9. use app\ais\controller\Common;
  10. use app\ais\model\AisConfig;
  11. use app\ais\model\AisCoupon;
  12. use app\ais\model\AisCouponUser;
  13. use app\ais\model\AisCard;
  14. use app\common\facade\Inform;
  15. use think\facade\Request;
  16. use think\helper\Time;
  17. use app\ais\model\AisStore;
  18. class User extends Common{
  19. public function initialize() {
  20. parent::initialize();
  21. $this->assign('pathMaps', [['name'=>'优惠券管理','url'=>url("coupon/index")]]);
  22. }
  23. /**
  24. * 列表
  25. */
  26. public function index(int $types = 0,int $page = 0){
  27. $condition = [];
  28. $time = Request::param('time/d',0);
  29. $starttime = Request::param('starttime/s');
  30. $endtime = Request::param('endtime/s');
  31. if($time){
  32. switch ($time) {
  33. case 2:
  34. list($start, $end) = Time::yesterday();
  35. break;
  36. case 30:
  37. list($start, $end) = Time::month();
  38. break;
  39. case 60:
  40. list($start, $end) = Time::lastMonth();
  41. break;
  42. default:
  43. list($start, $end) = Time::today();
  44. break;
  45. }
  46. $condition[] = ['create_time','>=',$start];
  47. $condition[] = ['create_time','<=',$end];
  48. }else{
  49. if($starttime){
  50. $condition[] = ['create_time','>=',strtotime($starttime)];
  51. }
  52. if($endtime){
  53. $condition[] = ['create_time','<=',strtotime($endtime)];
  54. }
  55. }
  56. $store_id = Request::param('store_id/d');
  57. $store_name = Request::param('store_name');
  58. if($store_id){
  59. $condition[] = ['store_id','=',$store_id];
  60. }
  61. $keyword = Request::param('keyword');
  62. if(!empty($keyword)){
  63. $condition[] = ['name','like','%'.$keyword.'%'];
  64. }
  65. //统计
  66. $view['coupon_count'] = AisCoupon::where($this->mini_program)->where($condition)->where(['is_end' => 0,'is_lock' => 0])->count();
  67. $view['coupon_end_count'] = AisCoupon::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  68. $view['coupon_user_count'] = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();
  69. $view['coupon_user_end_count'] = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  70. switch ($types) {
  71. case 1:
  72. $condition[] = ['is_lock','=',1];
  73. $condition[] = ['is_end','=',0];
  74. break;
  75. case 2:
  76. $condition[] = ['is_lock','=',0];
  77. $condition[] = ['is_end','=',0];
  78. break;
  79. case 4:
  80. $condition[] = ['is_end','=',1];
  81. break;
  82. default;
  83. $condition[] = ['is_end','=',0];
  84. break;
  85. }
  86. $view['lists'] = AisCoupon::where($this->mini_program)->where($condition)->order('sort desc,is_lock desc,update_time desc')->paginate(10,false,['query' => ['types' => $types,'store_id' => $store_id,'store_name' => $store_name]]);
  87. $view['types'] = $types;
  88. $view['page'] = $page;
  89. $view['keyword'] = $keyword;
  90. $view['store_id'] = $store_id;
  91. $view['store_name'] = $store_name;
  92. $view['time'] = $time;
  93. $view['starttime'] = $starttime;
  94. $view['endtime'] = $endtime;
  95. return view()->assign($view);
  96. }
  97. //优惠券预览和统计
  98. public function review(int $id,int $types = 0){
  99. $info = AisCoupon::where($this->mini_program)->where(['id' => $id])->find();
  100. if(empty($info)){
  101. $this->error('内容不存在');
  102. }
  103. $condition = [];
  104. $condition['is_end'] = $types ?? 0;
  105. $condition['coupon_id'] = $id;
  106. $view['lists'] = AisCouponUser::where($this->mini_program)->where($condition)->order('id asc')->paginate(10,false,['query'=>['id' => $id,'types' => $types]]);
  107. $view['types'] = $types;
  108. $view['coupon_count'] = AisCouponUser::where($this->mini_program)->where(['coupon_id' => $id])->count();
  109. $view['coupon_end_count'] = AisCouponUser::where($this->mini_program)->where(['coupon_id' => $id,'is_end' => 1])->count();
  110. $view['info'] = $info;
  111. $config = AisConfig::getConfig($this->member_miniapp_id);
  112. $view['end_time'] = $config->coupon_longtime * 60 * 60;
  113. return view()->assign($view);
  114. }
  115. //编辑
  116. public function edit(){
  117. $id = Request::param('id/d');
  118. if(request()->isAjax()){
  119. $data = [
  120. 'id' => Request::param('id/d'),
  121. 'name' => Request::param('name/s'),
  122. 'price' => Request::param('price/f'),
  123. 'discount' => Request::param('discount/d',0),
  124. 'howmuch' => Request::param('howmuch/d',0),
  125. 'tips' => Request::param('tips/s'),
  126. 'types' => Request::param('types/d',0),
  127. ];
  128. $validate = $this->validate($data,'Coupon.edit');
  129. if(true !== $validate){
  130. return json(['code'=>0,'msg'=>$validate]);
  131. }
  132. if($data['types'] == 0){
  133. $data['discount'] = 0;
  134. }else{
  135. $data['price'] = 0;
  136. }
  137. if($id){
  138. $data['id'] = $id;
  139. $result = AisCoupon::where($this->mini_program)->where(['id' => $data['id']])->update($data);
  140. }else{
  141. $store_id = Request::param('store_id/d',0);
  142. $store = AisStore::where(['id' => $store_id])->find();
  143. if(empty($store)){
  144. return json(['code'=>0,'msg'=>'商家必须选择']);
  145. }
  146. $data['member_miniapp_id'] = $this->member_miniapp_id;
  147. $data['create_time'] = time();
  148. $data['update_time'] = time();
  149. $data['store_id'] = $store_id;
  150. $data['cate_id'] = $store->cate_id;
  151. $data['cate_sid'] = $store->cate_sid;
  152. $data['is_top'] = 0;
  153. $result = AisCoupon::create($data);
  154. }
  155. if($result){
  156. return json(['code'=>200,'url'=>url('coupon/index',['page' => input('get.page/d')]),'msg'=>'操作成功']);
  157. }else{
  158. return json(['code'=>0,'msg'=>'操作失败']);
  159. }
  160. }else{
  161. $info = AisCoupon::where($this->mini_program)->where(['id' => $id])->find();
  162. $view['info'] = $info;
  163. $view['page'] = input('get.page/d');
  164. return view()->assign($view);
  165. }
  166. }
  167. //删除
  168. public function delete(int $id){
  169. $result = AisCoupon::where($this->mini_program)->where(['id' => $id])->delete();
  170. if($result){
  171. return json(['code'=>200,'msg'=>'操作成功']);
  172. }else{
  173. return json(['code'=>403,'msg'=>'删除失败']);
  174. }
  175. }
  176. /**
  177. * 排序
  178. */
  179. public function sort(){
  180. if(request()->isAjax()){
  181. $data = [
  182. 'sort' => $this->request->param('sort/d'),
  183. 'id' => $this->request->param('id/d'),
  184. ];
  185. $validate = $this->validate($data,'Category.sort');
  186. if(true !== $validate){
  187. return json(['code'=>0,'msg'=>$validate]);
  188. }
  189. $result = AisCoupon::where($this->mini_program)->where(['id' => $data['id']])->update(['sort'=>$data['sort']]);
  190. if($result){
  191. return json(['code'=>200,'msg'=>'操作成功']);
  192. }else{
  193. return json(['code'=>0,'msg'=>'操作失败']);
  194. }
  195. }
  196. }
  197. /**
  198. * 置顶/取消
  199. * @param integer $id 用户ID
  200. */
  201. public function isTop(int $id){
  202. $rel = AisCoupon::where(['id' => $id])->field('is_top')->find();
  203. $rel->is_top = $rel->is_top ? 0 : 1;
  204. $rel->save();
  205. return json(['code'=>200,'msg'=>'操作成功']);
  206. }
  207. /**
  208. * 审核和取消
  209. * @param integer $id 用户ID
  210. */
  211. public function isLock(int $id){
  212. $result = AisCoupon::where(['id' => $id])->field('is_lock,store_id')->find();
  213. $result->is_lock = $result->is_lock ? 0 : 1;
  214. $rel = $result->save();
  215. if($rel){
  216. if($result->is_lock == 0){
  217. $store = AisStore::where('id','=',$result->store_id)->find();
  218. if($store){
  219. Inform::sms($store->manage_uid,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '优惠券申请','content' =>'您的优惠券申请已经通过审核','state' => '成功']);
  220. }
  221. }
  222. return json(['code'=>200,'msg'=>'操作成功']);
  223. }else{
  224. return json(['code'=>0,'msg'=>'操作失败']);
  225. }
  226. }
  227. /**
  228. * 用户已领优惠券管理
  229. *
  230. * @param integer $id 优惠券ID
  231. * @param integer $uid 用户ID
  232. * @return void
  233. */
  234. public function user(int $types = 0){
  235. $condition = [];
  236. $uid = Request::param('uid/d');
  237. if($uid){
  238. $condition[] = ['uid','=',$uid];
  239. }
  240. $store_id = Request::param('store_id/d');
  241. $store_name = Request::param('store_name/s');
  242. if($store_id){
  243. $condition[] = ['store_id','=',$store_id];
  244. }
  245. $time = Request::param('time/d',0);
  246. if($time){
  247. switch ($time) {
  248. case 2:
  249. list($start, $end) = Time::yesterday();
  250. break;
  251. case 30:
  252. list($start, $end) = Time::month();
  253. break;
  254. case 60:
  255. list($start, $end) = Time::lastMonth();
  256. break;
  257. default:
  258. list($start, $end) = Time::today();
  259. break;
  260. }
  261. $condition[] = ['create_time','>=',$start];
  262. $condition[] = ['create_time','<=',$end];
  263. }
  264. $starttime = Request::param('starttime/s');
  265. $endtime = Request::param('endtime/s');
  266. if($starttime){
  267. $condition[] = ['create_time','>=',strtotime($starttime)];
  268. }
  269. if($endtime){
  270. $condition[] = ['create_time','<=',strtotime($endtime)];
  271. }
  272. $view['coupon_count'] = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();
  273. $view['coupon_end_count'] = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  274. $view['lists'] = AisCouponUser::where($this->mini_program)->where($condition)->where(['is_end'=>$types ? 1 : 0])->order('id desc')->paginate(20,false,['query'=>['uid' => $uid,'types' => $types,'starttime' => $starttime,'endtime' => $endtime,'time'=>$time]]);
  275. $view['types'] = $types;
  276. $view['time'] = $time;
  277. $view['starttime'] = $starttime;
  278. $view['endtime'] = $endtime;
  279. $view['uid'] = $uid;
  280. $view['store_id'] = $store_id;
  281. $view['store_name'] = $store_name;
  282. $view['pathMaps'] = [['name'=>'已领优惠券','url'=>url("coupon/user")]];
  283. $config = AisConfig::getConfig($this->member_miniapp_id);
  284. $view['end_time'] = $config->coupon_longtime * 60 * 60;
  285. return view()->assign($view);
  286. }
  287. /**
  288. * 查看用户优惠券信息
  289. * @param integer $id
  290. * @return void
  291. */
  292. public function userCoupon(int $id = 0){
  293. $view['info'] = AisCouponUser::where($this->mini_program)->where(['id' => $id])->find();
  294. return view()->assign($view);
  295. }
  296. /**
  297. * 删除用户的优惠券
  298. * @return void
  299. */
  300. public function deleteUser(int $id){
  301. $result = AisCouponUser::where($this->mini_program)->where(['id' => $id])->delete();
  302. if($result){
  303. return json(['code'=>200,'msg'=>'操作成功']);
  304. }else{
  305. return json(['code'=>403,'msg'=>'删除失败']);
  306. }
  307. }
  308. }