User.php 12 KB

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