| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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
- }
- /**
- * 修改头像
- * @param $userId
- * @param $avatar
- * @return mixed
- */
- public function saveAvatar($userId, $avatar)
- {
- return $this->model->where(['id'=> $userId])->update(['avatar'=> $avatar,'update_time'=> time()]);
- }
- /**
- * 重置密码
- * @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()]);
- }
- }
|