Usermanage.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. /**
  5. * @title 会员管理
  6. * @controller Usermanage
  7. * @group base
  8. */
  9. class Usermanage extends Base
  10. {
  11. public function initialize()
  12. {
  13. parent::initialize();
  14. parent::check_login();
  15. }
  16. /**
  17. * @title 发放(领取)优惠券
  18. * @desc 发放(领取)优惠券
  19. * @author qc
  20. * @url /api/Usermanage/sendCoupon
  21. * @method POST
  22. * @header name:Authorization require:1 desc:Token
  23. * @param name:coupon_id type:int require:1 default:0 desc:券id
  24. * @return name:int_id type: int default:0 desc:发放(领取)id
  25. */
  26. public function sendCoupon()
  27. {
  28. $coupon_id = input("post.coupon_id");
  29. if(!$coupon_id) $this->error('请选择优惠券');
  30. $coupon_info = Db::table('store_coupon_config')
  31. ->where(['status'=>1,'is_deleted'=>0,'id'=>$coupon_id])
  32. ->find();
  33. if(empty($coupon_info)) $this->error('优惠券已过期或禁用');
  34. $has_get = Db::table('user_coupon_list')
  35. ->where(['user_id'=>$this->uid])
  36. ->count();
  37. if($coupon_info['user_num'] <= $has_get) $this->error('已达到领取上线');
  38. $coupon_data = [
  39. 'user_id'=>$this->uid,
  40. 'coupon_id'=>$coupon_id,
  41. 'low_amount'=>$coupon_info['low_amount'],
  42. 'amount'=>$coupon_info['amount'],
  43. 'low_day'=>$coupon_info['low_day'],
  44. 'create_at'=>date("Y-m-d 00:00:00"),
  45. 'past_at'=>date("Y-m-d 00:00:00",strtotime('+'.$coupon_info['low_day'].' days')),
  46. ];
  47. Db::table('user_coupon_list')->insert($coupon_data);
  48. $this->success('领取成功',['int_id'=>Db::getLastInsID()]);
  49. }
  50. /**
  51. * @title 获取我的优惠券
  52. * @desc 获取我的优惠券
  53. * @author qc
  54. * @url /api/Usermanage/getMyCouponByStatus
  55. * @method GET
  56. * @header name:Authorization require:1 desc:Token
  57. * @param name:sel_status type:int : default:0 desc:0全部,1正常,2已使用,3过期
  58. * @param name:page type:int : default:1 desc:页数
  59. * @param name:page_num type:int : default:20 desc:每页数
  60. * @return name:coupon_id type:int default:-- desc:券id
  61. * @return name:low_amount type:float default:-- desc:最低消费
  62. * @return name:amount type:float default:-- desc:抵扣金额
  63. * @return name:create_at type:float default:-- desc:领取(发放时间)
  64. * @return name:status type:int default:-- desc:1正常,2已使用,3过期
  65. * @return name:past_at type:string default:-- desc:到期时间
  66. * @return name:used_time type:string default:-- desc:使用时间
  67. */
  68. public function getMyCouponByStatus()
  69. {
  70. $sel_status = input('sel_status');
  71. $where['user_id'] = $this->uid;
  72. if(in_array($sel_status,[1,2,3])) $where['status'] = $sel_status;
  73. $list = Db::table('user_coupon_list')->alias('l')
  74. ->field('l.*,c.title')
  75. ->join('store_coupon_config c',"l.coupon_id = c.id","LEFT")
  76. ->where($where)
  77. ->order('id desc')
  78. ->limit($this->off_set,$this->page_num)
  79. ->select();
  80. $this->success('获取成功',['list'=>$list]);
  81. }
  82. /**
  83. * @title 收藏商品
  84. * @desc 收藏商品
  85. * @author qc
  86. * @url /api/Usermanage/goodsCollect
  87. * @method POST
  88. * @header name:Authorization require:1 desc:Token
  89. * @param name:goods_id type:int require:1 default:0 desc:商品id
  90. */
  91. public function goodsCollect()
  92. {
  93. $goods_id = input('post.goods_id');
  94. if(!$goods_id) $this->error('请选择商品');
  95. $check_collect = Db::table('goods_collect')
  96. ->where(['goods_id'=>$goods_id,'user_id'=>$this->uid])
  97. ->value('id');
  98. if($check_collect) $this->error('商品已收藏');
  99. Db::table('goods_collect')->insert(['goods_id'=>$goods_id,'user_id'=>$this->uid,'create_at'=>date('Y-m-d H:i:s')]);
  100. $this->success('商品已收藏');
  101. }
  102. /**
  103. * @title 取消收藏
  104. * @desc 取消收藏
  105. * @author qc
  106. * @url /api/Usermanage/cancelCollect
  107. * @method POST
  108. * @header name:Authorization require:1 desc:Token
  109. * @param name:id type:int default:0 desc:收藏记录id(两个参数不能同时为空)
  110. * @param name:goods_id type:int default:0 desc:商品id(两个参数不能同时为空)
  111. */
  112. public function cancelCollect()
  113. {
  114. $id = input('post.id');
  115. $goods_id = input('post.goods_id');
  116. if(!$id && !$goods_id) $this->error('参数错误');
  117. $del_where = [];
  118. $del_where['user_id'] = $this->uid;
  119. if($id) $del_where['id'] = $id;
  120. if($goods_id) $del_where['goods_id'] = $goods_id;
  121. Db::table('goods_collect')->where($del_where)->delete();
  122. $this->success('取消成功');
  123. }
  124. /**
  125. * @title 获取我的收藏列表
  126. * @desc 获取我的收藏列表
  127. * @author qc
  128. * @url /api/Usermanage/getMyGoodsCollect
  129. * @method GET
  130. * @header name:Authorization require:1 desc:Token
  131. * @param name:page type:int default:1 desc:页数
  132. * @param name:page_num type:int default:20 desc:每页数
  133. * @return name:id type:int default:-- desc:收藏记录id
  134. * @return name:goods_id type:int default:-- desc:商品id
  135. * @return name:name type:string default:-- desc:商品名称
  136. * @return name:cover type:string default:-- desc:商品封面图
  137. * @return name:desc type:string default:-- desc:商品简述
  138. * @return name:floor_price type:float default:-- desc:最低价
  139. * @return name:month_sell type:int default:-- desc:月销量
  140. */
  141. public function getMyGoodsCollect()
  142. {
  143. $list = Db::table('goods_collect')->alias('c')
  144. ->field('c.id,c.goods_id,g.name,g.cover,g.desc,g.floor_price')
  145. ->join('store_goods g','c.goods_id = g.id','INNER')
  146. ->where(['c.user_id'=>$this->uid])
  147. ->order('c.id desc')
  148. ->limit($this->off_set,$this->page_num)
  149. ->select();
  150. foreach ($list as &$v) {
  151. $v['month_sell'] = Db::table('goods_sell_info')
  152. ->where(['goods_id'=>$v['goods_id']])
  153. ->where('create_at','> time',date('Y-m-d H:i:s',strtotime('-1 month')))
  154. ->count();
  155. }
  156. $this->success('获取成功',['list'=>$list]);
  157. }
  158. /**
  159. * @title 设置收货地址(新增或修改)
  160. * @desc 设置收货地址(新增或修改)
  161. * @author qc
  162. * @url /api/Usermanage/setDeliveryAddress
  163. * @method POST
  164. * @header name:Authorization require:1 desc:Token
  165. * @param name:id type:int : default:0 desc:收货地址记录id(不传值是添加)
  166. * @param name:pro_name type:int require:1 default:-- desc:省名
  167. * @param name:city_name type:int require:1 default:-- desc:市名
  168. * @param name:county_name type:int require:1 default:-- desc:县区名
  169. * @param name:detail type:string require:1 default:-- desc:详细地址
  170. * @param name:phone type:string require:1 default:-- desc:联系电话
  171. * @param name:name type:string require:1 default:-- desc:联系人
  172. * @param name:is_mr type:int require:0 default:0 desc:是否是默认(0不默认,1默认)
  173. */
  174. public function setDeliveryAddress()
  175. {
  176. $pro_name = input('post.pro_name');// 省
  177. $city_name = input('post.city_name');// 市
  178. $county_name = input('post.county_name');// 县区
  179. $pro_id = Db::table('store_area')->where('name',$pro_name)->value('id');
  180. $city_id = Db::table('store_area')->where('name',$city_name)->value('id');
  181. $county_id = Db::table('store_area')->where('name',$county_name)->value('id');
  182. $detail = input('post.detail');// 详细地址
  183. $phone = input('post.phone');
  184. $name = input('post.name');
  185. $id = input('post.id');
  186. $is_mr = input('post.is_mr',0);
  187. $set_data = compact(['pro_id','city_id','county_id','detail','phone','name','is_mr']);
  188. $set_data['user_id'] = $this->uid;
  189. $set_data['pro_name'] = $pro_name;
  190. $set_data['city_name'] = $city_name;
  191. $set_data['county_name']= $county_name;
  192. $set_data['mer_name']=$pro_name . $city_name. $county_name;
  193. if($id) {
  194. Db::table('delivery_address')->where(['id'=>$id])->update($set_data);
  195. }else{
  196. Db::table('delivery_address')->insert($set_data);
  197. $id = Db::getLastInsID();
  198. }
  199. if($is_mr) Db::table('delivery_address')->where('user_id','=',$this->uid)->where('id','<>',$id)->where('is_mr','=',1)->update(['is_mr'=>0]);
  200. $this->success('编辑成功');
  201. }
  202. /**
  203. * @title 删除收货地址
  204. * @desc 删除收货地址
  205. * @author qc
  206. * @url /api/Usermanage/delDeliveryAddress
  207. * @method POST
  208. * @header name:Authorization require:1 desc:Token
  209. * @param name:id type:int require:1 default:1 desc:收货地址记录id
  210. */
  211. public function delDeliveryAddress()
  212. {
  213. $id = input('post.id');
  214. if(!$id)$this->error('请选择要删除的收货地址');
  215. Db::table('delivery_address')->where(['user_id'=>$this->uid,'id'=>$id])->delete();
  216. $this->success('删除成功!');
  217. }
  218. /**
  219. * @title 获取收货地址列表
  220. * @desc 获取收货地址列表
  221. * @author qc
  222. * @url /api/Usermanage/getDeliveryAddressList
  223. * @method GET
  224. * @header name:Authorization require:1 desc:Token
  225. * @param name:page type:int default:1 desc:页数
  226. * @param name:page_num type:int default:20 desc:每页数
  227. * @return name:pro_id type:int default:-- desc:省id
  228. * @return name:city_id type:int default:-- desc:市id
  229. * @return name:county_id type:int default:-- desc:县区id
  230. * @return name:pro_name type:string default:-- desc:省名
  231. * @return name:city_name type:string default:-- desc:市名
  232. * @return name:county_name type:string default:-- desc:县区名
  233. * @return name:detail type:string default:-- desc:详细地址
  234. * @return name:phone type:string default:-- desc:联系电话
  235. * @return name:name type:string default:-- desc:联系人
  236. * @return name:is_mr type:int default:0 desc:是否是默认(0不默认,1默认)
  237. */
  238. public function getDeliveryAddressList()
  239. {
  240. $list = Db::table('delivery_address')
  241. ->where(['user_id'=>$this->uid])
  242. ->order('is_mr desc ,id desc')
  243. ->limit($this->off_set,$this->page_num)
  244. ->select();
  245. $this->success('ok',['list'=>$list]);
  246. }
  247. /**
  248. * @title 获取收货地址详情
  249. * @desc 获取收货地址详情
  250. * @author qc
  251. * @url /api/Usermanage/getDeliveryAddressDetail
  252. * @method GET
  253. * @header name:Authorization require:1 desc:Token
  254. * @param name:id type:int default:1 desc:收货地址记录id
  255. * @return name:pro_id type:int default:-- desc:省id
  256. * @return name:city_id type:int default:-- desc:市id
  257. * @return name:county_id type:int default:-- desc:县区id
  258. * @return name:pro_name type:string default:-- desc:省名
  259. * @return name:city_name type:string default:-- desc:市名
  260. * @return name:county_name type:string default:-- desc:县区名
  261. * @return name:detail type:string default:-- desc:详细地址
  262. * @return name:phone type:string default:-- desc:联系电话
  263. * @return name:name type:string default:-- desc:联系人
  264. * @return name:is_mr type:int default:0 desc:是否是默认(0不默认,1默认)
  265. */
  266. public function getDeliveryAddressDetail()
  267. {
  268. $detail = Db::table('delivery_address')->find(input('id'));
  269. $this->success('ok',$detail);
  270. }
  271. /**
  272. * @title 获取消息未读数量
  273. * @desc 获取消息未读数量
  274. * @author qc
  275. * @url /api/Usermanage/getMsgCalNum
  276. * @method GET
  277. * @header name:Authorization require:1 desc:Token
  278. * @return name:integral_num type:int default:0 desc:积分消息未读数量
  279. * @return name:lev_num type:int default:0 desc:等级变更消息未读数量
  280. * @return name:live_num type:int default:0 desc:直播消息未读数量
  281. * @return name:growth_num type:int default:0 desc:成长值消息未读数量
  282. */
  283. public function getMsgCalNum()
  284. {
  285. $integral_num = Db::table('integral_info')
  286. ->where(['user_id'=>$this->uid,'is_deleted'=>0,'is_read'=>0])
  287. ->count();
  288. $lev_num = Db::table('member_level_log')
  289. ->where(['user_id'=>$this->uid,'is_read'=>0])
  290. ->count();
  291. $live_num = Db::table('live_app_msg')
  292. ->where(['user_id'=>$this->uid,'is_read'=>0])
  293. ->count();
  294. $growth_num = Db::table('member_level_growth')
  295. ->where(['user_id'=>$this->uid,'is_read'=>0])
  296. ->count();
  297. $this->success('ok',['integral_num'=>$integral_num ? $integral_num:0,'lev_num'=>$lev_num ? $lev_num:0,'live_num'=>$live_num ? $live_num:0,'growth_num'=>$growth_num?$growth_num : 0 ]);
  298. }
  299. /**
  300. * @title 消息变更为全部已读
  301. * @desc 消息变更为全部已读
  302. * @author qc
  303. * @url /api/Usermanage/msgReadByType
  304. * @method POST
  305. * @header name:Authorization require:1 desc:Token
  306. * @param name:type type:int default:1 desc:类型(1积分,2等级变更,3直播通知,4成长值)
  307. */
  308. public function msgReadByType()
  309. {
  310. $type = input('post.type',1);
  311. switch ($type) {
  312. case 1:
  313. Db::table('integral_info')
  314. ->where(['user_id'=>$this->uid,'is_deleted'=>0,'is_read'=>0])
  315. ->update(['is_read'=>1]);
  316. break;
  317. case 2:
  318. Db::table('member_level_log')
  319. ->where(['user_id'=>$this->uid,'is_read'=>0])
  320. ->update(['is_read'=>1]);
  321. break;
  322. case 3:
  323. Db::table('live_app_msg')
  324. ->where(['user_id'=>$this->uid,'is_read'=>0])
  325. ->update(['is_read'=>1]);
  326. break;
  327. case 4:
  328. Db::table('member_level_growth')
  329. ->where(['user_id'=>$this->uid,'is_read'=>0])
  330. ->update(['is_read'=>1]);
  331. break;
  332. }
  333. $this->success('ok');
  334. }
  335. /**
  336. * @title 获取积分变更日志
  337. * @desc 获取积分变更日志
  338. * @author qc
  339. * @url /api/Usermanage/getIntegralLog
  340. * @method GET
  341. * @header name:Authorization require:1 desc:Token
  342. * @param name:page type:int default:1 desc:页数
  343. * @param name:page_num type:int default:20 desc:每页数
  344. * @return name:create_at type:string default:-- desc:时间
  345. * @return name:integral type:int default:-- desc:积分变更数
  346. * @return name:desc type:string default:-- desc:描述
  347. * @return name:is_read type:int default:-- desc:消息是否已读(0未读,1已读)
  348. */
  349. public function getIntegralLog()
  350. {
  351. $list = Db::table('integral_info')
  352. ->field('id,create_at,integral,desc,is_read')
  353. ->where(['user_id'=>$this->uid,'is_deleted'=>0])
  354. ->order('id desc')
  355. ->limit($this->off_set,$this->page_num)
  356. ->select();
  357. $this->success('ok',['list'=>$list]);
  358. }
  359. /**
  360. * @title 获取等级变更日志
  361. * @desc 获取等级变更日志
  362. * @author qc
  363. * @url /api/Usermanage/getMemberLevLog
  364. * @method GET
  365. * @header name:Authorization require:1 desc:Token
  366. * @param name:page type:int default:1 desc:页数
  367. * @param name:page_num type:int default:20 desc:每页数
  368. * @return name:create_at type:string default:-- desc:时间
  369. * @return name:before_lev type:int default:-- desc:变更前等级id
  370. * @return name:after_lev type:int default:-- desc:变更后等级id
  371. * @return name:lev_name type:int default:-- desc:变更后等级名称
  372. * @return name:lev_logo type:int default:-- desc:变更后等级logo
  373. * @return name:desc type:string default:-- desc:描述
  374. * @return name:is_read type:int default:-- desc:消息是否已读(0未读,1已读)
  375. */
  376. public function getMemberLevLog()
  377. {
  378. $lev_set = Db::table('member_level')
  379. ->select();
  380. $lev_arr = array_column($lev_set,null,'id');
  381. $list = Db::table('member_level_log')
  382. ->field('id,create_at,before_lev,after_lev,is_read,desc')
  383. ->where(['user_id'=>$this->uid,'is_deleted'=>0])
  384. ->order('id desc')
  385. ->limit($this->off_set,$this->page_num)
  386. ->select();
  387. array_walk($list,function (&$val)use ($lev_arr){
  388. $val['lev_name'] = $lev_arr[$val['after_lev']]['name'];
  389. $val['lev_logo'] = $lev_arr[$val['after_lev']]['logo'];
  390. });
  391. $this->success('ok',['list'=>$list]);
  392. }
  393. /**
  394. * @title 获取直播预告通知
  395. * @desc 获取直播预告通知
  396. * @author qc
  397. * @url /api/Usermanage/getLiveAppMsg
  398. * @method GET
  399. * @header name:Authorization require:1 desc:Token
  400. * @param name:page type:int default:1 desc:页数
  401. * @param name:page_num type:int default:20 desc:每页数
  402. * @return name:title type:string default:-- desc:预约直播标题
  403. * @return name:show_time type:string default:-- desc:预约直播开始时间
  404. * @return name:create_at type:string default:-- desc:消息提醒时间
  405. * @return name:url type:string default:-- desc:跳转链接
  406. * @return name:is_read type:int default:-- desc:消息是否已读(0未读,1已读)
  407. */
  408. public function getLiveAppMsg()
  409. {
  410. $list = Db::table('live_app_msg')
  411. ->field('id,title,show_time,create_at,url,is_read')
  412. ->where(['user_id'=>$this->uid])
  413. ->order('id desc')
  414. ->limit($this->off_set,$this->page_num)
  415. ->select();
  416. $this->success('ok',['list'=>$list]);
  417. }
  418. /**
  419. * @title 获取成长值变更日志
  420. * @desc 获取成长值变更日志
  421. * @author qc
  422. * @url /api/Usermanage/getGrowthLog
  423. * @method GET
  424. * @header name:Authorization require:1 desc:Token
  425. * @param name:page type:int default:1 desc:页数
  426. * @param name:page_num type:int default:20 desc:每页数
  427. * @return name:create_at type:string default:-- desc:时间
  428. * @return name:growth type:int default:-- desc:成长值变更数
  429. * @return name:desc type:string default:-- desc:描述
  430. * @return name:is_read type:int default:-- desc:消息是否已读(0未读,1已读)
  431. */
  432. public function getGrowthLog()
  433. {
  434. $list = Db::table('member_level_growth')
  435. ->field('id,create_at,growth,desc,is_read')
  436. ->where(['user_id'=>$this->uid])
  437. ->order('id desc')
  438. ->limit($this->off_set,$this->page_num)
  439. ->select();
  440. $this->success('ok',['list'=>$list]);
  441. }
  442. }