User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller;
  13. use app\api\model\OrderRefund;
  14. use app\api\model\user\AccumulatePointsLog;
  15. use app\api\model\user\CommissionsDetail;
  16. use app\api\model\user\UserIdcards;
  17. use app\api\model\UserCoupon;
  18. use app\api\service\passport\Login as LoginService;
  19. use app\common\enum\order\refund\RefundStatus as RefundStatusEnum;
  20. use app\common\enum\Setting as SettingEnum;
  21. use app\common\exception\BaseException;
  22. use app\api\model\UserCoupon as UserCouponModel;
  23. use app\api\service\User as UserService;
  24. use app\common\library\helper;
  25. use app\common\model\Shops;
  26. use app\api\model\user\BonusHistory;
  27. use app\api\model\user\CommissionSteps;
  28. use app\api\model\Setting as SettingModel;
  29. use app\api\model\Order as OrderModel;
  30. use app\api\model\Coupon as CouponModel;
  31. use think\cache\driver\Redis;
  32. use app\common\model\User as UserModel;
  33. use app\api\model\User as ApiUserModel;
  34. /**
  35. * 用户管理
  36. * Class User
  37. * @package app\api
  38. */
  39. class User extends Controller
  40. {
  41. /**
  42. * 当前用户详情
  43. * @return array|\think\response\Json
  44. * @throws BaseException
  45. */
  46. public function info()
  47. {
  48. // 当前用户信息
  49. $userInfo = UserService::getCurrentLoginUser(true);
  50. // 获取用户头像
  51. $userInfo['avatar'] = $userInfo['avatar'] ?? ['preview_url' => config('chef.user_default_avatar'), 'external_url' => config('chef.user_default_avatar')];
  52. // 获取会员等级
  53. $userInfo['grade'];
  54. // 今日收益
  55. $userInfo['today_profits'] = helper::bcadd(CommissionsDetail::getUserTodayProfits($userInfo->user_id), 0, 2);
  56. $userInfo['shop_name'] = '';
  57. $userInfo['shop_type'] = 0;
  58. $userInfo['is_pickup'] = 0;
  59. if($userInfo['shop_id'] && in_array($userInfo['role'],[2,3,4])){
  60. $shops = Shops::find($userInfo['shop_id']);
  61. $userInfo['shop_name'] = $shops['shop_name'];
  62. $userInfo['shop_type'] = $shops['is_virtual']; //门店类型 1虚拟门店 2自营店 3异业门店
  63. if($shops['is_pickup']){
  64. $userInfo['is_pickup'] = $shops['is_pickup'];//开启门店自提
  65. }
  66. }
  67. $userInfo['bind_shop_name'] = Shops::find($userInfo['bind_shop_id'])['shop_name'] ?? '';
  68. //待结算金额
  69. $djs_amount = CommissionsDetail::where('user_id', $userInfo->user_id)->where('clearing_status', 0)->sum('clearing_money') ?? 0;
  70. $userInfo['djs_amount'] = helper::bcadd($djs_amount, 0, 2);
  71. $userInfo['show_cash_box'] = false;
  72. $wait_clearing = CommissionsDetail::getUserWaitCommission($userInfo['user_id']);
  73. //if ($wait_clearing >0 || $userInfo['can_withdraw_money'] >0){
  74. if ($wait_clearing*100 >1 || $userInfo['ktxyj_amount']*100 >1){
  75. $userInfo['show_cash_box'] = true;
  76. }
  77. $userInfo['can_withdraw_money'] = helper::bcsub($userInfo['can_withdraw_money'],0,2); // 佣金已结算总金额
  78. $userInfo['have_withdrew_money'] = helper::bcsub($userInfo['have_withdrew_money'],0,2); // 已提现金额
  79. $userInfo['ktxyj_amount'] = helper::bcsub($userInfo['ktxyj_amount'],0,2); // 可提现金额
  80. // 上月达量奖励金
  81. $userInfo['last_month_bonus'] = BonusHistory::lastMonthBonus($userInfo['user_id']);
  82. // 阶梯奖励金计算
  83. $userInfo['calc_bonus_steps'] = CommissionSteps::calcBonusSteps($userInfo);
  84. // 阶梯奖励金计算
  85. $userInfo['sales_bonus_steps'] = $list['distributor_step'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_STEP)['distributor'];
  86. // 我的优惠券可用数量
  87. $userInfo['user_coupon_num'] = (new UserCoupon)->getCount($userInfo['user_id']);
  88. //是否开通会员,并且会员未过期
  89. $userInfo['is_member'] = 0;
  90. if($userInfo['member_expire_time']>0&&$userInfo['member_expire_time']>time()){
  91. $userInfo['is_member'] = 1;
  92. }
  93. //订单数大于0 ,则为老用户 否则为新用户
  94. $userInfo['is_new_user'] = (new OrderModel)->getCount()>0?false:true;
  95. // $userInfo['new_user_coupon_expire'] = $this->newUserCouponExpire(
  96. // $userInfo['user_id']);
  97. $receiveCouponState = 0;
  98. $key = (new UserCouponModel)->getUserCouponRdsKey($userInfo['user_id']);
  99. $rds = new Redis(config('cache.stores.redis'));
  100. $value = $rds->get($key);
  101. $userInfo['receive_coupon_state'] = intval($value??0);
  102. if($value==1){
  103. $rds->set($key,2);// 1 代表刚领,2代表领过了
  104. }
  105. return $this->renderSuccess(compact('userInfo'));
  106. }
  107. //新用户领的优惠券是不是都过期
  108. public function newUserCouponExpire($user_id){
  109. $couponIds = (new UserCoupon)->getUserCouponIds($user_id);
  110. $newUserCouponIds = (new CouponModel)->checkCouponIdBySendType($couponIds,20);
  111. $couponList = (new UserCoupon)->getUserCouponByCouponId($user_id,$newUserCouponIds);
  112. $now = Date("Y-m-d H:i:s",time());
  113. $new_user_coupon_expire = true;
  114. foreach($couponList as $row){
  115. $end_time = $row['end_time'];
  116. if($end_time > $now){
  117. $new_user_coupon_expire = false;
  118. break;
  119. }
  120. }
  121. return $new_user_coupon_expire;
  122. }
  123. /**
  124. * 更新用户信息,昵称、头像、性别
  125. * @return array
  126. * @throws BaseException
  127. * @author: zjwhust
  128. * @Time: 2022/3/2 16:15
  129. */
  130. public function upd()
  131. {
  132. $params = $this->request->post();
  133. $userInfo = UserService::getCurrentLoginUser(true);
  134. $LoginService = new LoginService;
  135. $ret = $LoginService->updateWxUserInfo($params, $userInfo);
  136. if ($ret === false) {
  137. return $this->renderError('更新失败');
  138. }
  139. return $this->renderSuccess([], '更新成功');
  140. }
  141. /**
  142. * 更新用户生日
  143. * @return array
  144. * @throws BaseException
  145. * @author: zjwhust
  146. * @Time: 2022/3/2 16:15
  147. */
  148. public function updBirthday()
  149. {
  150. $params = $this->request->post();
  151. $birthday = $params['birthday']??'';
  152. $userInfo = new ApiUserModel;
  153. $ret = $userInfo->updateBirthday($birthday);
  154. if ($ret === false) {
  155. return $this->renderError('更新失败');
  156. }
  157. return $this->renderSuccess([], '更新成功');
  158. }
  159. /**
  160. * 账户资产
  161. * @return array|\think\response\Json
  162. * @throws BaseException
  163. */
  164. public function assets()
  165. {
  166. // 当前用户信息
  167. $userInfo = UserService::getCurrentLoginUser(true);
  168. // 用户优惠券模型
  169. $model = new UserCouponModel;
  170. // 返回数据
  171. return $this->renderSuccess([
  172. 'assets' => [
  173. 'balance' => $userInfo['balance'], // 账户余额
  174. 'points' => $userInfo['points'], // 会员积分
  175. 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
  176. ]
  177. ]);
  178. }
  179. /**
  180. * 保存身份证和真实姓名
  181. * @return array|\think\response\Json
  182. */
  183. public function addIdcard(){
  184. $param = $this->postData();
  185. $patternIc = "/^[X0-9]{18}$/u";
  186. if(!preg_match($patternIc,$param['id_card'])){
  187. return $this->renderError('请输入正确的身份证号码');
  188. }
  189. $pattern = "/^[\x{4e00}-\x{9fa5}]+$/u";
  190. if(!preg_match($pattern,$param['real_name'])){
  191. return $this->renderError('姓名格式不正确');
  192. }
  193. $m = new UserIdcards();
  194. $m->addOne($param);
  195. return $this->renderSuccess([], '保存成功');
  196. }
  197. //下级推荐官
  198. public function upperuser(string $kw=''){
  199. $param = $this->postData();
  200. $userInfo = UserService::getCurrentLoginUser(true);
  201. $filter = [];
  202. //判断是否传递了日期
  203. if(isset($param['date']) && !empty($param['date'])){
  204. $start_time = strtotime($param['date']);
  205. $end_time = strtotime(date('Y-m-t 23:59:59',strtotime($param['date'])));//获取月份的最后一天
  206. $filter[] = ['created_time','>=',$start_time];
  207. $filter[] = ['created_time','<=',$end_time];
  208. }
  209. $user_id = $userInfo->user_id;
  210. // $user_id = 107731;
  211. // $user_id = 265;
  212. $list = UserModel::field("mobile,nick_name,user_id,avatar_id,create_time")->with(['avatar'])->where("upper_user_id",$user_id)->where(function($query) use ($kw){
  213. if(!empty($kw)){
  214. $query->whereOr('nick_name','like','%'.$kw.'%');
  215. $query->whereOr('mobile','like','%'.$kw.'%');
  216. }
  217. })->where($filter)
  218. ->where('role',99)->paginate(15)->each(function ($item){
  219. $res = CommissionsDetail::sumUserSalesVolume($item['user_id']);
  220. $item['order_cnt'] = $res['order_cnt'];
  221. $item['order_amount'] =$res['order_amount'];
  222. //推广金额
  223. $wheres[] = ['user_id','=',$item->user_id];
  224. $wheres[] = ['shop_id','=',0];
  225. $wheres[] = ['role','=',UserModel::COMMISSION_USER];
  226. $wheres[] = ['commission_level','=',1];
  227. $wheres[] = ['clearing_status','<',2];
  228. $item['inviter_sale_volume'] = CommissionsDetail::sumOrderSaleVolume($wheres);
  229. $item['user_cnt'] = UserModel::where("upper_user_id",$item->user_id)->whereIn('role',[UserModel::NORMAL_USER,UserModel::COMMISSION_USER])->count();
  230. if(strlen($item['mobile'])==11){
  231. $item['mobile'] = substr($item['mobile'],0,3).'****'.substr($item['mobile'],7);
  232. }
  233. }
  234. );
  235. return $this->renderSuccess(compact('list'));
  236. }
  237. /**
  238. * 在途金米粒更新到可用金米粒
  239. * @return array
  240. * @author: zjwhust
  241. * @Time: 2022/6/11 16:01
  242. */
  243. public function updGoldRice(){
  244. //获取有售后时间,并且已经过了售后时间,还有待结算的在途金米粒订单,并且没有售后中的订单
  245. $lists = OrderModel::field('order_id,user_id,gold_rice_amount_input')
  246. ->where('refund_time','>',0)
  247. ->where('refund_time','<',time())
  248. ->where('gold_rice_amount_input','>',0)
  249. ->where('gold_rice_is_pass',0)
  250. ->select();
  251. // return $this->renderSuccess(compact('lists'));
  252. $num = 0;
  253. foreach ($lists as $arr){
  254. //如果有正在退款中的订单
  255. if(OrderRefund::where('order_id',$arr['order_id'])->where('status','<=', RefundStatusEnum::REJECTED)->count()){
  256. continue;
  257. }
  258. //在途金米粒转到可用金米粒里
  259. UserModel::setIncDecByField($arr['user_id'],['gold_rice'=>(float)$arr['gold_rice_amount_input']],['gold_rice_road'=>(float)$arr['gold_rice_amount_input']]);
  260. //在途金米粒更改为已结算状态
  261. OrderModel::updateBase(['gold_rice_is_pass'=>1],['order_id'=>$arr['order_id']]);
  262. $num++;
  263. }
  264. //需要在售后成功后减掉在途的金米粒,并更新订单表里的gold_rice_amount_input字段数量
  265. return $this->renderSuccess('成功更新'.$num.'个订单');
  266. }
  267. public function accumulatePointsRecords(){
  268. $list = (new AccumulatePointsLog)->getList();
  269. return $this->renderSuccess(compact('list'));
  270. }
  271. }