MemberService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\api\controller\members;
  3. use app\api\controller\Controller;
  4. use app\api\model\Coupon;
  5. use app\api\model\Goods as GoodsModel;
  6. use app\api\model\member\MemberCardOrder;
  7. use app\api\model\member\MemberCards;
  8. use app\api\model\member\MemberGoods;
  9. use app\api\model\member\MemberWelfare;
  10. use app\api\model\member\GoldRice;
  11. use app\api\model\member\MemberWelfareDraw;
  12. use app\api\model\shop\Shops;
  13. use app\api\model\User;
  14. use app\api\model\UserCoupon;
  15. use app\api\service\User as UserService;
  16. use app\common\library\helper;
  17. use app\store\model\CouponGood;
  18. use think\Exception;
  19. use think\facade\Db;
  20. class MemberService extends Controller
  21. {
  22. // public function tj(){
  23. // $otj = new MemberTj;
  24. // $day = $otj->day(1);
  25. // $month = $otj->month();
  26. // $week = $otj->week();
  27. // echo 'success';
  28. // }
  29. /**
  30. * 会员专区
  31. */
  32. public function memberZone(){
  33. $userId = UserService::getCurrentLoginUserId();
  34. $info = User::detail(['user_id'=>$userId]);
  35. $data['duration'] = 1;//年卡
  36. $data['curr_month'] = intval(date('m'));//年卡
  37. $data['member_expire_time'] = $info['member_expire_time']>0?date('Y-m-d',$info['member_expire_time']):'';
  38. $data['gold_rice'] = $info['gold_rice'];
  39. $data['welfare'] = MemberWelfare::currMonthWelfare($userId);
  40. return $this->renderSuccess($data);
  41. }
  42. /**
  43. * 推荐商品
  44. * @return array|\think\response\Json
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function vipGoodsList(){
  50. $model = new GoodsModel;
  51. $list = $model->getList([],15,true);
  52. foreach ($list as &$item){
  53. $dis = MemberGoods::where(['goods_id'=>$item['goods_id'],'status'=>0])
  54. ->field('goods_id,discount')->find();
  55. if ($dis){
  56. //$t = helper::bcmul($item['goods_price_min'],$dis['discount'],3);
  57. //$item['member_goods_price'] = helper::bcadd(ceil($t)/100,0,2);
  58. $item['member_goods_dis_price'] = helper::bcsub($item['goods_price_min'],$item['member_price'],2);
  59. }
  60. }
  61. return $this->renderSuccess(compact('list'));
  62. }
  63. /**
  64. * 优惠券适用商品
  65. * @return array|\think\response\Json
  66. * @throws \think\db\exception\DbException
  67. */
  68. public function vipCouponGoodsList(){
  69. $couponId = $this->request->get('coupon_id');
  70. $model = new GoodsModel;
  71. $exGoodsId = CouponGood::getExceptGoodsId($couponId,1);
  72. $params['not_goods_id_arr'] = $exGoodsId;
  73. $list = $model->getList($params,15,false);
  74. return $this->renderSuccess(compact('list'));
  75. }
  76. /**
  77. * 会员开通记录
  78. * @return array|\think\response\Json
  79. * @throws \app\common\exception\BaseException
  80. */
  81. public function vipOpenRecords(){
  82. $userId = UserService::getCurrentLoginUserId();
  83. $list = MemberCardOrder::getList($userId);
  84. return $this->renderSuccess(compact('list'));
  85. }
  86. /**
  87. * 校验会员时效
  88. * @return array|\think\response\Json
  89. * @throws \app\common\exception\BaseException
  90. */
  91. public function checkMemberExpire(){
  92. $user = UserService::getCurrentLoginUser(true);
  93. if ($user['member_expire_time'] - time() > 63072000){//大于两年
  94. $end_date = date('Y-m-d',$user['member_expire_time']).'后方可进行续费操作';
  95. return $this->renderError('您的会员有效期天数已达上限啦~',['hint'=>$end_date]);
  96. }
  97. return $this->renderSuccess();
  98. }
  99. /**
  100. * 会员卡详情
  101. * @return array|\think\response\Json
  102. */
  103. public function memberCardInfo(){
  104. $user = UserService::getCurrentLoginUser();
  105. $member_status = 0;
  106. $expire_at = '';
  107. if ($user){
  108. if ($user['member_expire_time'] == 0){
  109. $member_status = 0;//未开通
  110. }elseif($user['member_expire_time'] < time()){
  111. $member_status = 1;//已过期
  112. }else{
  113. $member_status = 2;//生效中
  114. $expire_at = date('Y-m-d',$user['member_expire_time']);
  115. }
  116. }
  117. $card = MemberCards::findCard();
  118. return $this->renderSuccess(compact('card','member_status','expire_at'));
  119. }
  120. /**
  121. * 金米粒收入/支出列表
  122. * @param string $dataType in收入 out 支出
  123. * @return array
  124. * @throws \app\common\exception\BaseException
  125. * @throws \think\db\exception\DbException
  126. * @author: zjwhust
  127. * @Time: 2022/6/7 11:58
  128. */
  129. public function listGoldRice(string $dataType = GoldRice::LIST_TYPE_IN)
  130. {
  131. $user = UserService::getCurrentLoginUser();
  132. $model = new GoldRice;
  133. $param = [];
  134. $param['userId'] = $user['user_id'];
  135. $list = $model->getList($dataType,$param);
  136. return $this->renderSuccess(compact('list'));
  137. }
  138. /**
  139. * 领取福利
  140. * @return array|\think\response\Json
  141. * @throws \app\common\exception\BaseException
  142. */
  143. public function drawMemberWelfare(){
  144. $user = UserService::getCurrentLoginUser(true);
  145. if ($user['member_expire_time'] < time()){
  146. return $this->renderError('您的会员已过期');
  147. }
  148. $id = $this->request->get('id');
  149. $m = new MemberWelfareDraw();
  150. $flag = $m->drawAction($id,$user);
  151. if ($flag){
  152. return $this->renderSuccess();
  153. }
  154. return $this->renderError($m->getError());
  155. }
  156. /**
  157. * 福利领取记录
  158. * @return array|\think\response\Json
  159. * @throws \app\common\exception\BaseException
  160. * @throws \think\db\exception\DbException
  161. */
  162. public function myWelfareRecords(){
  163. $user = UserService::getCurrentLoginUser(true);
  164. if ($user['member_expire_time'] < time()){
  165. return $this->renderError('您的会员已过期');
  166. }
  167. $m = new MemberWelfareDraw();
  168. $list = $m->welfareRecords($user->user_id);
  169. return $this->renderSuccess(compact('list'));
  170. }
  171. /**
  172. * 我领取的福利详情
  173. * @return array|\think\response\Json
  174. * @throws \app\common\exception\BaseException
  175. */
  176. public function myWelfareDetail(){
  177. $user = UserService::getCurrentLoginUser(true);
  178. $id = $this->request->get('id');
  179. $m = new MemberWelfareDraw();
  180. $list = $m->myWelfareDetail($id);
  181. if($user['user_id']!=$list['user_id']){
  182. return $this->renderError('不是你的福利');
  183. }
  184. //todo 需根据第三方接口联调更新
  185. if($list['target_type']==2){
  186. $list['hx_qrcode_url'] = url('/api/order/getHxWelQrcode',['id'=>$list['id']], true, true)->build();
  187. }
  188. return $this->renderSuccess(compact('list'));
  189. }
  190. /**
  191. * 用户优惠券详情
  192. * @param int $user_coupon_id
  193. * @return array|\think\response\Json
  194. */
  195. public function VipCouponDetail(int $user_coupon_id){
  196. $list = UserCoupon::showDetail($user_coupon_id);
  197. return $this->renderSuccess(compact('list'));
  198. }
  199. /**
  200. * v1.4.1开通/续费会员后的会员开通礼优惠券列表弹窗
  201. * @author: zjwhust
  202. * @Time: 2022/6/24 14:44
  203. */
  204. public function vipRechargeCouponList(){
  205. $list = (new Coupon)->getMemList();
  206. return $this->renderSuccess(compact('list'));
  207. }
  208. /**
  209. * v1.4.1是否有待领取的会员开通礼
  210. * @author: zjwhust
  211. * @Time: 2022/6/24 15:31
  212. */
  213. public function isReceive(){
  214. $user = UserService::getCurrentLoginUser(true);
  215. $count = MemberCardOrder::where('user_id',$user['user_id'])
  216. ->where('pay_status',1)
  217. ->where('order_status',30)
  218. ->where('is_del',0)
  219. ->where('is_receive',0)
  220. ->count();
  221. return $this->renderSuccess(compact('count'));
  222. }
  223. /**
  224. * v1.4.1领取会员开通礼
  225. * @author: zjwhust
  226. * @Time: 2022/6/24 15:34
  227. */
  228. public function receive(){
  229. $user = UserService::getCurrentLoginUser(true);
  230. if($user['member_expire_time']<time()){
  231. return $this->renderError('不是会员');
  232. }
  233. $memberOrder = MemberCardOrder::where('user_id',$user['user_id'])
  234. ->where('pay_status',1)
  235. ->where('order_status',30)
  236. ->where('is_del',0)
  237. ->where('is_receive',0)
  238. ->select();
  239. if(!$memberOrder){
  240. return $this->renderError('您已领取,请在优惠券卡包查看');
  241. }
  242. Db::startTrans();
  243. try{
  244. $list = (new Coupon)->getMemList();
  245. foreach ($list as $coupon){
  246. for ($i=0;$i<$coupon['limit_receive_cnt'];$i++){
  247. $model = new UserCoupon();
  248. $model->receiveLogin($coupon,$user);
  249. }
  250. }
  251. //会员订单修改为已处理
  252. MemberCardOrder::where('id',$memberOrder[0]['id'])->update(['is_receive'=>1]);
  253. Db::commit();
  254. return $this->renderSuccess('领取成功');
  255. }catch (Exception $e) {
  256. Db::rollback();
  257. return $this->renderError($e->getMessage());
  258. }
  259. }
  260. /**
  261. * 异业门店核销详情接口
  262. * @author: zjwhust
  263. * @Time: 2022/7/21 18:08
  264. */
  265. public function hxWelfareDetail($id){
  266. // $id = $this->request->get('id');
  267. $user = UserService::getCurrentLoginUser(true);
  268. $shop = Shops::find($user['shop_id']);
  269. if(!$shop || $shop['is_virtual']!=3){
  270. return $this->renderError('不是异业门店,不能核销');
  271. }
  272. $m = new MemberWelfareDraw();
  273. $list = $m->myWelfareDetail($id);
  274. if($list['deploy_type']!=2){
  275. return $this->renderError('不是自制券码');
  276. }
  277. return $this->renderSuccess(compact('list'));
  278. }
  279. /**
  280. * 核销异业门店核销
  281. * @author: zjwhust
  282. * @Time: 2022/7/21 18:14
  283. */
  284. public function hxWelfare($id){
  285. $user = UserService::getCurrentLoginUser(true);
  286. $shop = Shops::find($user['shop_id']);
  287. if(!$shop || $shop['is_virtual']!=3){
  288. return $this->renderError('不是异业门店,不能核销');
  289. }
  290. $m = new MemberWelfareDraw();
  291. $list = $m->myWelfareDetail($id);
  292. if($list['deploy_type']!=2){
  293. return $this->renderError('不是自制券码');
  294. }
  295. if($list['use_end_time']<date('Y-m-d')){
  296. return $this->renderError('已过使用有效期');
  297. }
  298. $list->check_status = 1;
  299. $list->check_time = date('Y-m-d H:i:s');
  300. if($list->save()){
  301. return $this->renderSuccess('核销成功');
  302. }
  303. return $this->renderError('核销失败');
  304. }
  305. }