| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- <?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\ActionLogModel;
- use App\Models\MemberModel;
- use App\Models\ShopModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- 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', 'nickname','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['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
- $info['bonus'] = round($info['bonus'],0);
- $info['show_bonus'] = GoodsService::make()->checkNewGoods($info['id']);
- // 二维码
- $qrcode = $this->makeQrcode(env('WEB_URL').'h5/#/pages/register/index?code='.$info['code']);
- $info['qrcode'] = $qrcode? get_image_url($qrcode):'';
- }
- 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;
- }
- // 验证店铺
- if(!$shopId = ShopModel::where(['code'=> $shopCode,'status'=>1,'mark'=>1])->value('id')){
- $this->error = 2011;
- return false;
- }
- // 用户验证
- $info = $this->model->getOne([['mobile', '=', $mobile]]);
- if (!$info) {
- $this->error = 2001;
- return false;
- }
- // 密码校验
- $password = get_password($password . md5($password . env('APP_NAME')));
- 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, 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'])) {
- $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},";
- $data = [
- 'nickname' => $nickname,
- 'username' => strtoupper('U'.get_random_code(7)),
- 'password' => get_password($password . md5($password . env('APP_NAME'))),
- 'safe_password' => get_password($safePassword . md5($safePassword . env('APP_NAME'))),
- 'mobile' => $mobile,
- 'parents' => $parents,
- 'parent_id' => $parentId,
- 'status' => 1,
- 'mark' => 1,
- 'create_time' => time(),
- ];
- if ($id = $this->model->edit($data)) {
- $this->model->where(['id'=> $id])->update(['code'=> strtoupper('Q'.get_random_code(8))]);
- $this->error = 2008;
- return true;
- }
- $this->error = 2007;
- 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;
- if($parentId>0){
- $where['a.parent_id'] = $parentId;
- }
- 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}%");
- }
- })
- ->select(['a.*','b.id as shop_id','b.name as shop_name','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['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'] : '无';
- $item['invite_num'] = MemberService::make()->getInviteNums($item['id']);
- }
- }
- 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])->count('id');
- if($data){
- RedisService::set($cacheKey, $data, rand(3, 5));
- }
- return $data;
- }
- /**
- * 上级用户列表
- * @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','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']);
- }
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 重置密码
- * @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 . md5($password.env('APP_NAME')));
- $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 . md5($password . 'otc'));
- 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)
- {
- $username = isset($params['username']) ? $params['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 . md5($password . 'otc'));
- if (!$this->model->where(['id' => $info['id']])->update(['password' => $password, 'update_time' => time()])) {
- $this->error = 2025;
- return false;
- }
- $this->error = 2024;
- return true;
- }
- }
|