// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Helpers\Jwt; use App\Models\AccountModel; use App\Models\ActionLogModel; use App\Models\MemberModel; use App\Models\ShopModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; use phpQrcode\QRcode; /** * 会员管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class MemberService * @package App\Services\Common */ class MemberService extends BaseService { protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MemberService constructor. */ public function __construct() { $this->model = new MemberModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取资料详情 * @param $where * @param array $field */ public function getInfo($where, array $field = []) { $field = $field ? $field : ['id', 'username', 'realname','mobile', 'nickname','is_trade','login_shop_id','code','parent_id', 'openid','score_rate', 'idcard', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'member_level', 'bonus','bonus_total','score', 'status', 'avatar']; if (is_array($where)) { $info = $this->model->where($where)->select($field)->first(); } else { $info = $this->model->where(['id' => (int)$where])->select($field)->first(); } $info = $info ? $info->toArray() : []; if ($info) { $info['version'] = env('VERSION','v1.1.20'); $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : ''; $info['bonus'] = round($info['bonus'],0); $info['mobile_text'] = $info['mobile']; $info['mobile'] = $info['mobile']? format_mobile($info['mobile']):''; // 积分 $scoreRate = isset($info['score_rate'])? $info['score_rate'] : 1; $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1; $info['real_score'] = intval($scoreRate * $info['score']); // 二维码 $inviteUrl = env('WEB_URL').'h5/#/pages/register/index?code='.$info['code']; $qrcode = $this->makeQrcode($inviteUrl); $info['qrcode'] = $qrcode? get_image_url($qrcode):''; $info['invite_url'] = $inviteUrl; $info['shop_info'] = []; if(isset($info['login_shop_id']) && $info['login_shop_id']){ $shopInfo = ShopService::make()->getInfo($info['login_shop_id']); $snapTime = ConfigService::make()->getConfigByCode('snap_time'); $snapTime = $snapTime? $snapTime : 5; $curTime = strtotime(date('H:i:s')); $startTime = isset($shopInfo['start_time'])&&$shopInfo['start_time']? strtotime($shopInfo['start_time']) : 0; $endTime = isset($shopInfo['end_time'])&&$shopInfo['end_time']? strtotime($shopInfo['end_time']) : 0; $timeLock = $startTime - $curTime>0? $startTime - $curTime : 0; $shopInfo['timeData'] = [ 'hours'=> 0, 'minutes'=> 0, 'seconds'=> 0, ]; $shopInfo['snap_time'] = $snapTime; $shopInfo['cur_time'] = $curTime; $shopInfo['snap_time_lock'] = max(0,$curTime - $startTime); $shopInfo['start_time_num'] = $startTime; $shopInfo['end_time_lock'] = max(0,$endTime - $startTime); $shopInfo['time_lock'] = 0; $shopInfo['trade_status'] = 2; // if($timeLock ){ if($timeLock && $timeLock<= $snapTime*60){ $shopInfo['time_lock'] = $timeLock; $shopInfo['timeData']['hours'] = intval($timeLock/3600); $shopInfo['timeData']['minutes'] = intval($timeLock%3600/60); $shopInfo['timeData']['seconds'] = intval($timeLock%3600%60); $shopInfo['trade_status'] = 1; }else if($endTime>=$curTime && $curTime>=$startTime){ $shopInfo['trade_status'] = 1; } $info['shop_info'] = $shopInfo; } $info['parent_info'] = ['nickname'=>'无','code'=>'无']; if(isset($info['parent_id']) && $info['parent_id']){ $info['parent_info'] = $this->model->where(['id'=>$info['parent_id'],'mark'=>1]) ->select(['id','nickname','username','code']) ->first(); } $type = request()->post('type', 0); if($type == 3){ // 银行卡信息 $info['bank_info'] = MemberBankService::make()->getBindInfo($info['id']); } if($type == 1){ // 交易订单统计 $info['orderCounts'] = TradeService::make()->getNewTradeCountByStatus($info['id'],$info['login_shop_id'],[1,2,3]); // 积分订单统计 $info['scoreOrderCount'] = OrderService::make()->getNewTradeCount($info['id'],[1,2,3,4,5]); } } return $info; } /** * 分销中心信息 * @param $where * @param array $field * @return array */ public function getMarketInfo($where, array $field = []) { $field = $field ? $field : ['id', 'username', 'realname','mobile', 'nickname','is_trade','login_shop_id','code','parent_id', 'openid','score_rate','merits_count','merits_total','merits_time', 'idcard', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'member_level', 'bonus','bonus_total','score', 'status', 'avatar']; if (is_array($where)) { $info = $this->model->where($where)->select($field)->first(); } else { $info = $this->model->where(['id' => (int)$where])->select($field)->first(); } $info = $info ? $info->toArray() : []; if ($info) { $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : ''; $info['bonus'] = round($info['bonus'],0); $info['bonus_total'] = round($info['bonus_total'],0); $info['mobile'] = $info['mobile']? format_mobile($info['mobile']):''; // 今日是否有拍过商品(业绩有入账) $tradeCount = TradeService::make()->getCountByDay($info['id']); $showBonusLimitCount = ConfigService::make()->getConfigByCode('show_bonus_limit'); $showBonusLimitCount = $showBonusLimitCount>0? $showBonusLimitCount : 1; $info['show_bonus'] = $tradeCount>=$showBonusLimitCount?1:0; $info['shop_info'] = []; if(isset($info['login_shop_id']) && $info['login_shop_id']) { $shopInfo = ShopService::make()->getInfo($info['login_shop_id']); $info['shop_info'] = $shopInfo; } if(isset($info['parent_id']) && $info['parent_id']) { $parentInfo = MemberModel::where(['id'=> $info['parent_id']])->select(['id','nickname','mobile','code'])->first(); $info['parent_info'] = $parentInfo; $info['parent_mobile'] = isset($parentInfo['mobile'])? format_mobile($parentInfo['mobile']) : ''; } // 团队人数 $info['team_num'] = MemberService::make()->getInviteNums($info['id']); // // 本人业绩 // $info['merits_count'] = TradeService::make()->getUserTradeTotal($info['id'],[3,4]); // // // 总业绩 // $info['merits_total'] = TradeService::make()->getTeamTradeTotal($info['id'],[3,4]); } return $info; } /** * 用户缓存信息 * @param $id * @param string $field * @return array|mixed */ public function getCacheInfo($id, $field='') { $cacheKey = "caches:member:info:u_{$id}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $field = $field? $field : ['id','is_trade','status','parent_id','code']; $info = $this->model->where(['id' => $id,'mark'=>1])->select($field)->first(); $info = $info? $info->toArray() : []; if($info){ RedisService::set($cacheKey, $info, rand(5, 10)); } return $info; } /** * 用户登录 * @param $params * @return array|false */ public function login($params) { $mobile = isset($params['mobile']) ? $params['mobile'] : ''; $password = isset($params['password']) ? $params['password'] : ''; $shopCode = isset($params['shop_code']) ? $params['shop_code'] : ''; if (empty($mobile) || empty($password)) { $this->error = 2009; return false; } if(empty($shopCode)){ $this->error = 2010; return false; } // 验证店铺 $shopInfo = ShopModel::where(['code'=> $shopCode,'status'=>1,'mark'=>1])->first(); $shopId = isset($shopInfo['id'])? $shopInfo['id'] : 0; $shopUserId = isset($shopInfo['user_id'])? $shopInfo['user_id'] : 0; if(!$shopId){ $this->error = 2011; return false; } // 用户验证 $info = $this->model->where(['mobile'=>$mobile,'mark'=>1]) ->select(['id','username','nickname','mobile','password','code','parent_id','login_count','login_shop_id','status']) ->first(); if (!$info) { $this->error = 2001; return false; } $userId = isset($info['id'])? $info['id'] : 0; $loginShopId = isset($info['login_shop_id'])? $info['login_shop_id'] : 0; if(($shopUserId != $userId) && $loginShopId>0 && ($loginShopId != $shopId)){ $this->error = 2022; return false; } // 密码校验 $password = get_password($password); if ($password != $info['password']) { $this->error = 2002; return false; } // 使用状态校验 if ($info['status'] != 1) { $this->error = 2012; return false; } // 设置日志标题 ActionLogModel::setTitle("会员登录"); ActionLogModel::record($info); // JWT生成token $jwt = new Jwt('jwt_app'); $token = $jwt->getToken($info['id']); RedisService::set("stores:auths:info:{$info['id']}", $info->toArray(), 5, 10); // 登录 $updateData = ['login_time' => time(),'login_shop_id'=> $shopId,'login_count'=>$info['login_count']+1, 'login_ip' => get_client_ip()]; $this->model->where(['id' => $info['id']])->update($updateData); // 登录数据 return [ 'token' => $token, 'user_id' => $info['id'], 'shop_id' => $shopId, ]; } /** * 用户注册 * @param $params * @return bool */ public function register($params) { // 检测账号是否存在 if ($this->checkExists('mobile', $params['mobile'],'id',[1,2])) { $this->error = '2005'; return false; } $mobile = isset($params['mobile']) ? trim($params['mobile']) : ''; $nickname = isset($params['nickname']) ? trim($params['nickname']) : ''; $password = isset($params['password']) ? trim($params['password']) : ''; $safePassword = isset($params['safe_password']) ? trim($params['safe_password']) : ''; $inviteCode = isset($params['invite_code']) ? trim($params['invite_code']) : ''; $inviteInfo = $this->model->where(['code'=> $inviteCode,'mark'=>1])->select(['id','code','username','parents'])->first(); if(empty($inviteInfo)){ $this->error = '2013'; return false; } $parentId = isset($inviteInfo['id'])? $inviteInfo['id'] : 0; $parents = isset($inviteInfo['parents'])? $inviteInfo['parents'] : ''; $parents = $parents? rtrim($parents,',').",{$parentId}," : "{$parentId},"; $id = $this->model->max('id')+1; $data = [ 'nickname' => $nickname? $nickname : get_random_code(6,'用户u'), 'username' => strtoupper(get_random_code(7,'U')), 'password' => get_password($password), 'safe_password' => get_password($safePassword), 'code'=> strtoupper(get_random_code(8,'Q', "{$id}")), 'mobile' => $mobile, 'parents' => $parents, 'parent_id' => $parentId, 'status' => 1, 'score' => 0, 'mark' => 1, 'merits_time' => date('Y-m-d H:i:s'), 'create_time' => time(), ]; if ($id = $this->model->edit($data)) { //$this->model->where(['id'=> $id])->update(['code'=> strtoupper(get_random_code('Q',8, "{$id}"))]); $this->error = 2008; return true; } $this->error = 2007; return false; } /** * 修改保存用户资料 * @param $userId * @param $params * @return mixed */ public function saveInfo($userId, $params) { $nickname = isset($params['nickname'])? trim($params['nickname']) : ''; if($nickname){ if($this->model->where(['nickname'=> $nickname,'mark'=> 1])->whereNotIn('id',[$userId])->value('id')){ $this->error = '2028'; return false; } } $data = [ 'nickname' => $nickname, 'update_time' => time(), ]; if($this->model->where(['id'=> $userId])->update($data)){ $this->error = '1020'; return true; } $this->error = '1021'; return false; } /** * 列表 * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $parentId = isset($params['parent_id'])? $params['parent_id'] : 0; $time = isset($params['time'])&&$params['time']? $params['time'] : 0; // 默认今日 if($parentId>0){ $where['a.parent_id'] = $parentId; } $loginShopId = isset($params['login_shop_id'])? $params['login_shop_id'] : 0; if($loginShopId>0){ $where['a.login_shop_id'] = $loginShopId; } if($status>0){ $where['a.status'] = $status; } $list = $this->model->from('member as a') // ->leftJoin('shop as b', 'b.user_id', '=', 'a.id') ->leftJoin('member as c', 'c.id', '=', 'a.parent_id') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->where('a.username','like',"%{$keyword}%")->orWhere('a.mobile','like',"%{$keyword}%"); } $keyword1 = isset($params['keyword1'])? $params['keyword1'] : ''; if($keyword1){ $query->where('a.nickname','like',"%{$keyword1}%")->orWhere('a.mobile','like',"%{$keyword1}%"); } }) ->select(['a.*','c.code as parent_code','c.nickname as parent_name']) ->orderBy('a.create_time','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['selected'] = false; $item['nickname'] = trim($item['nickname']); $item['rank_num'] = $item['id']%10+1; $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : ''; $item['login_time'] = $item['login_time']? datetime($item['login_time'],'Y-m-d H.i.s') : ''; $item['avatar'] = isset($item['avatar']) && $item['avatar']? get_image_url($item['avatar']) : ''; $item['parent_code'] = isset($item['parent_code']) && $item['parent_code']? $item['parent_code'] : '无'; $showType = isset($params['show_type'])? $params['show_type'] : 1; if($showType==1){ $item['invite_num'] = MemberService::make()->getInviteNums($item['id']); $item['goods_num'] = GoodsService::make()->getCount($item['id']); }else if($showType == 3){ $item['bonus_total'] = TradeService::make()->getTradeBonusTotal($item['id'], $time); $item['profit_total'] = TradeService::make()->getTradeProfitTotal($item['id'], $time); } } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 直推用户数 * @param $userId * @return array|mixed */ public function getInviteNums($userId) { $cacheKey = "caches:member:inviteNums"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model->where(['parent_id'=> $userId, 'mark'=> 1,'status'=>1])->count('id'); if($data){ RedisService::set($cacheKey, $data, rand(3, 5)); } return $data; } /** * 用户选项 * @return array */ public function options() { // 获取参数 $param = request()->all(); // 用户ID $keyword = getter($param, "keyword"); $parentId = getter($param, "parent_id"); $userId = getter($param, "user_id"); $datas = $this->model->where(function($query) use($parentId){ if($parentId){ $query->where(['id'=> $parentId,'mark'=>1]); }else{ $query->where(['status'=> 1,'mark'=>1]); } }) ->where(function($query) use($userId){ if($userId){ $query->whereNotIn('id', [$userId]); } }) ->where(function($query) use($keyword){ if($keyword){ $query->where('nickname','like',"%{$keyword}%")->orWhere('mobile','like',"%{$keyword}%"); } }) ->select(['id','username','mobile','code','nickname','status']) ->get(); return $datas? $datas->toArray() : []; } /** * 上级用户列表 * @return array */ public function parents() { // 获取参数 $param = request()->all(); // 用户ID $keyword = getter($param, "keyword"); $parentId = getter($param, "parent_id"); $userId = getter($param, "user_id"); $datas = $this->model->where(function($query) use($parentId){ if($parentId){ $query->where(['id'=> $parentId,'mark'=>1]); }else{ $query->where(['status'=> 1,'mark'=>1]); } }) ->where(function($query) use($userId){ if($userId){ $query->whereNotIn('id', [$userId]); } }) ->where(function($query) use($keyword){ if($keyword){ $query->where('username','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%"); } }) ->select(['id','username','mobile','code','nickname','status']) ->get(); return $datas? $datas->toArray() : []; } /** * 生成普通参数二维码 * @param $str 参数 * @param bool $refresh 是否重新生成 * @return bool */ public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2) { $qrFile = '/images/qrcode/'; if (!is_dir('/uploads' . $qrFile)) { @mkdir('./uploads' . $qrFile, 0755, true); } $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size . $margin . $level)) . '.png'; $cacheKey = "caches:qrcodes:member_" . md5($str); if (RedisService::get($cacheKey) && is_file('/uploads' . $qrFile) && !$refresh) { //return $qrFile; } QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin); if (!file_exists('./uploads' . $qrFile)) { return false; } RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600); return $qrFile; } /** * 推荐树 * @return array|false|mixed */ public function getTree() { // 请求参数 $keyword = request()->post('keyword',''); $cacheKey = "caches:member:trees:".md5('t'.$keyword); $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model->where(['status'=>1,'mark'=>1]) ->select(['id','username','nickname','mobile','parent_id','status']) ->get()->keyBy('id'); $datas = $datas? $datas->toArray() : []; $pid = 0; if($keyword){ $data = $this->model->where(function($query) use($keyword){ $query->where('nickname','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%"); }) ->where(['status'=>1,'mark'=>1]) ->orderBy('parent_id','asc') ->select(['id','parent_id','nickname','mobile','username']) ->first(); $nickname = isset($data['nickname'])? $data['nickname'] : ''; $username = isset($data['username'])? $data['username'] : ''; $mobile = isset($data['mobile'])? $data['mobile'] : ''; if($data){ $pid = isset($data['id'])? $data['id'] : 0; $data['label'] = $nickname.($mobile?"({$mobile})":""); unset($data['nickname']); unset($data['username']); } } $datas = get_tree($datas, $pid); if($datas){ if($pid){ $data['children'] = $datas; $newDatas[0] = $data; $datas = $newDatas; } RedisService::set($cacheKey, $datas, rand(3,5)); } return $datas; } /** * 添加会编辑会员 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 请求参数 $data = request()->all(); // 头像处理 $avatar = isset($data['avatar'])? trim($data['avatar']) : ''; if ($avatar && strpos($avatar, "temp")) { $data['avatar'] = save_image($avatar, 'member'); } else if($avatar){ $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']); } if(!isset($data['merits_time']) && empty($data['merits_time'])){ $data['merits_time'] = date('Y-m-d', time() - 2*86400); } return parent::edit($data); // TODO: Change the autogenerated stub } /** * 删除 * @return mixed */ public function delete() { $this->model->where(['mark'=>0])->where('update_time','<', time() - 300)->delete(); return parent::delete(); } /** * 修改头像 * @param $userId * @param $avatar * @return mixed */ public function saveAvatar($userId, $avatar) { $oldAvatar = $this->model->where(['id'=> $userId])->value('avatar'); if($this->model->where(['id'=> $userId])->update(['avatar'=> $avatar,'update_time'=> time()])){ if($oldAvatar && file_exists(ATTACHMENT_PATH.$oldAvatar)){ @unlink(ATTACHMENT_PATH.$oldAvatar); } return true; } return false; } /** * 重置密码 * @return array * @since 2020/11/14 * @author laravel开发员 */ public function resetPwd() { // 获取参数 $param = request()->all(); // 用户ID $userId = getter($param, "id"); if (!$userId) { return message("用户ID不能为空", false); } $userInfo = $this->model->getInfo($userId); if (!$userInfo) { return message("用户信息不存在", false); } // 设置新密码 $password = '123456'; $userInfo['password'] = get_password($password); $result = $this->model->edit($userInfo); if (!$result) { return message("重置密码失败", false); } return message("重置密码成功"); } /** * 修改账号 * @param $userId * @param $params * @return bool */ public function modify($userId, $params) { $username = isset($params['username']) ? $params['username'] : ''; $newUsername = isset($params['new_username']) ? $params['new_username'] : ''; $password = isset($params['password']) ? $params['password'] : ''; if (empty($username) || empty($password)) { $this->error = 1013; return false; } // 用户验证 $info = $this->model->getOne([['username', '=', $username]]); if (!$info || $info['id'] != $userId) { $this->error = 2001; return false; } // 使用状态校验 if ($info['status'] != 1) { $this->error = 2009; return false; } // 密码校验 $password = get_password($password); if ($password != $info['password']) { $this->error = 2002; return false; } $checkInfo = $this->model->getOne([['username', '=', $newUsername]]); if ($checkInfo && $checkInfo['id'] != $info['id']) { $this->error = 2005; return false; } if (!$this->model->where(['id' => $info['id']])->update(['username' => $newUsername, 'update_time' => time()])) { $this->error = 2021; return false; } $this->error = 2020; return true; } /** * 修改更新登录密码 * @param $userId * @param $params * @return bool */ public function updatePassword($userId, $params) { $password = isset($params['password']) ? $params['password'] : ''; $oldPassword = isset($params['old_password']) ? $params['old_password'] : ''; if (empty($oldPassword)) { $this->error = 2014; return false; } if (empty($password)) { $this->error = 2015; return false; } // 用户验证 $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]); if (!$info) { $this->error = 2001; return false; } // 使用状态校验 if ($info['status'] != 1) { $this->error = 2012; return false; } // 更新登录密码 $passwordStr = get_password($password); $oldPasswordStr = get_password($oldPassword); if($info['password'] != $oldPasswordStr){ $this->error = 2002; return false; } if($oldPassword == $password){ $this->error = 2016; return false; } if (!$this->model->where(['id' => $info['id']])->update(['password' => $passwordStr, 'update_time' => time()])) { $this->error = 2025; return false; } $this->error = 2024; return true; } /** * 修改更新支付密码 * @param $userId * @param $params * @return bool */ public function updateSafePassword($userId, $params) { $password = isset($params['password']) ? $params['password'] : ''; $safePassword = isset($params['safe_password']) ? $params['safe_password'] : ''; if (empty($safePassword)) { $this->error = 2017; return false; } if (empty($password)) { $this->error = 2018; return false; } // 用户验证 $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]); if (!$info) { $this->error = 2001; return false; } // 使用状态校验 if ($info['status'] != 1) { $this->error = 2012; return false; } // 更新登录密码 $safePassword = get_password($safePassword); $oldPasswordStr = get_password($password); if($info['password'] != $oldPasswordStr){ $this->error = 2002; return false; } if (!$this->model->where(['id' => $info['id']])->update(['safe_password' => $safePassword, 'update_time' => time()])) { $this->error = 2025; return false; } $this->error = 2024; return true; } /** * 转换佣金 * @param $userId * @param int $shopId * @param int $catchUid * @return bool */ public function switchBonus($userId, $shopId=0, $catchUid=0) { if($userId>0){ $info = $this->model->where(['id'=> $userId,'mark'=>1]) ->select(['id','bonus','login_shop_id','bonus','score','status']) ->first(); $score = isset($info['score'])? $info['score'] : 0; $bonus = isset($info['bonus'])? $info['bonus'] : 0; $status = isset($info['status'])? $info['status'] : 0; if(empty($info) || $status != 1){ $this->error = 2019; return false; } if($bonus<=0){ $this->error = 2026; return false; } DB::beginTransaction(); $scoreRate = ConfigService::make()->getConfigByCode('score_rate'); $scoreRate = $scoreRate? $scoreRate : 1; $switchScore = intval($bonus*$scoreRate); if(!$this->model->where(['id'=> $userId])->update(['bonus'=> 0,'score'=> intval($score + $switchScore), 'update_time'=> time()])){ DB::rollBack(); return false; } $logDatas[0] = [ 'user_id'=> $userId, 'shop_id'=> $info['login_shop_id'], 'type'=> 5, 'coin_type'=> 2, 'money'=> -$info['bonus'], 'balance'=> $info['bonus'], 'create_time'=>time(), 'update_time'=>time(), 'remark'=> '佣金转换', 'status'=>1, 'mark'=> 1, ]; $logDatas[1] = [ 'user_id'=> $userId, 'shop_id'=> $info['login_shop_id'], 'type'=> 5, 'coin_type'=> 3, 'money'=> $switchScore, 'balance'=> $info['score'], 'create_time'=>time(), 'update_time'=>time(), 'remark'=> '佣金转换【¥'.$bonus.'】', 'status'=>1, 'mark'=> 1, ]; if(!AccountModel::insert($logDatas)){ DB::rollBack(); return false; } DB::commit(); $this->error = 1002; return true; } // 店铺一键转换 else if ($shopId>0){ $datas = $this->model->where(['login_shop_id'=> $shopId,'mark'=>1,'status'=>1]) ->where('bonus','>', 0) ->select(['id','bonus','login_shop_id','bonus','score']) ->get(); if($datas){ $logDatas = []; $scoreRate = ConfigService::make()->getConfigByCode('score_rate'); $scoreRate = $scoreRate? $scoreRate : 1; foreach($datas as $v){ // 佣金 $logDatas[] = [ 'user_id'=> $v['id'], 'shop_id'=> $v['login_shop_id'], 'type'=> 5, 'coin_type'=> 2, 'money'=> -$v['bonus'], 'balance'=> $v['bonus'], 'create_time'=>time(), 'update_time'=>time(), 'remark'=> '佣金转换', 'status'=>1, 'mark'=> 1, ]; // 积分 $logDatas[] = [ 'user_id'=> $v['id'], 'shop_id'=> $v['login_shop_id'], 'type'=> 5, 'coin_type'=> 3, 'money'=> $v['bonus']*$scoreRate, 'balance'=> $v['score'], 'create_time'=>time(), 'update_time'=>time(), 'remark'=> '佣金转换', 'status'=>1, 'mark'=> 1, ]; } $sql = "UPDATE lev_member set `score`=`score`+(`bonus`)*{$scoreRate},`bonus`=0,update_time=".time()." where login_shop_id={$shopId} and status=1 and mark=1 and bonus >0"; DB::beginTransaction(); if(!DB::update(DB::raw($sql))){ DB::rollBack(); return false; } if(!AccountModel::insert($logDatas)){ DB::rollBack(); return false; } DB::commit(); $this->error = 1002; return true; } } return false; } /** * 设置抢拍状态 * @param $userId * @param $shopId * @param int $status * @return mixed */ public function setTrade($userId, $shopId, $status=1) { if($userId>0){ return $this->model->where(['id'=> $userId])->update(['is_trade'=>$status,'update_time'=> time()]); }else if($shopId){ return $this->model->where(['login_shop_id'=> $shopId,'mark'=>1])->update(['is_trade'=>$status,'update_time'=> time()]); } } /** * 删除用户 * @param $userId * @return mixed */ public function setLock() { $id = request()->post('id', 0); if(!$id){ $this->error =1013; return false; } return $this->model->where(['id'=> $id])->update(['status'=> 2,'update_time'=> time()]); } /** * 修改登录店铺 * @param $userId * @return mixed */ public function modifyShop() { $ids = request()->post('ids', 0); $shopCode = request()->post('shop_code',''); if(empty($ids)){ $this->error =2401; return false; } if(empty($shopCode)){ $this->error = 2402; return false; } $shopInfo = ShopModel::where(['code'=>$shopCode,'mark'=>1,'status'=>1])->first(); $shopId = isset($shopInfo['id'])? $shopInfo['id'] : 0; if(empty($shopInfo) && empty($shopId)){ $this->error = 2403; return false; } return $this->model->whereIn('id', $ids)->update(['login_shop_id'=> $shopId,'update_time'=> time()]); } }