服务器环境(插件)搭建

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 服务器环境(插件)搭建

PHP开发工具:neteasecloudmusic,phpstorm,postman,rdm,navicat for mysql,iterm,google chrome,dingtalk,wechat,youdao

正式和测试区别开有:域名,数据库,代码仓库分支(正式master,测试dev,开发app).

app-->合并->owen(自己)-->合并->app-->合并->dev

app-->合并->master

主流环境(插件):最笨就用宝塔面板,安全考虑使用以下:

  1. Grafana接口监控 www.jianshu.com/p/3527f4816…
  2. Jenkins代码部署
  3. Sentry,kibana日志查看
  4. GitLab代码仓库
  5. Jumpserver堡垒机:在线文件管理,命令执行
  6. sql审计平台
  7. 接口文档:yapi,swagger,小幺鸡,mindoc(github.com/lifei6671/m…
  8. 石墨在线办公文档
  9. 项目分工流程:tapd,禅道,钉钉蚂蚁分工,worktile
  10. 产品原型图,UI:蓝湖

PHP常用插件包:composter

packagist.org/

www.phpcomposer.com/

  1. topthink/framework ThinkPHP 核心框架库
  2. rmccue/requests 用PHP编写的HTTP库
  3. predis/predis **[Redis-
    Predis
    扩展](www.baidu.com/link?url=dD…)**
  4. lcobucci/jwt JWT 创建 Token
  5. phpmailer/phpmailer 实现PHP发邮件功能
  6. endroid/qr-code PHP-生成二维码
  7. codeitnowin/barcode PHP-生成条形码
  8. aliyuncs/oss-sdk-php 阿里云对象存储OSS(文件图片上传)
  9. topthink/think-queue 队列
  10. yansongda/pay 最优雅的微信和支付宝支付包
  11. symfony/event-dispatcher 通过分派事件并侦听事件来相互通信
  12. sentry/sdk sentry 日志查看,快速的发现故障问题

composer.json

{
    "name": "topthink/think",
    "description": "the new thinkphp framework",
    "type": "project",
    "keywords": [
        "lottery",
        "owenzhang"
    ],
    "homepage": "https://phper.owenzhang.com/",
    "license": "Apache-2.0",
    "authors": [
        {
            "name": "Owen Zhang",
            "email": "owen@owenzhang.com"
        }
    ],
    "require": {
        "php": ">=5.6.0",
        "topthink/framework": "5.1.*",
        "rmccue/requests": "^1.7",
        "predis/predis": "^1.1",
        "lcobucci/jwt": "^3.2",
        "phpmailer/phpmailer": "^6.0",
        "endroid/qr-code": "2.5.1",
        "aliyuncs/oss-sdk-php": "^2.3",
        "topthink/think-queue": "^2.0",
        "yansongda/pay": "2.6.0",
        "symfony/event-dispatcher": "3.4.32",
        "sentry/sdk": "2.0.3"
    },
    "autoload": {
        "psr-4": {
            "app\\": "application"
        }
    },
    "extra": {
        "think-path": "thinkphp"
    },
    "config": {
        "preferred-install": "dist"
    }
}

代码规范模块:

image.png

参数:DisablesendmsgListContext.php 用于列表参数格式化,避免参数过多不好维护

<?php
/**
 * 禁言列表参数格式化
 */
namespace app\sports\disablesendmsg\context;
use app\lucky\common\Context;
class DisablesendmsgListContext extends Context
{
    //页码
    public $page_no;
    //页数
    public $page_count;
    //操作者ID
    public $operatorId;
    //用户昵称
    public $user_nickname;
    //来源app1后台2
    public $sourceId;
    //是否删除1是0否
    public $isDel;
}

数据验证层DisablesendmsgValidate.php

<?php
/**
 * 禁言
 */
namespace app\sports\disablesendmsg\validate;
use app\lucky\common\InstanceTrait;
use think\Validate;
class DisablesendmsgValidate
{
    use InstanceTrait;
    public function checkAdd($data)
    {
        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $data[$key] = htmlentities($value);
            }
        }
        $rule = [
            "user_id" => "require|number",
            "disablesendmsg_reason" => "require|max:20",
            "disablesendmsg_duration" => "require|number|between:0,365"
        ];
        $msg = [
            "user_id.require" => "用户id必填",
            "name.number" => "用户id得是数字",
            "disablesendmsg_reason.require" => "禁言原因必填",
            "disablesendmsg_reason.max" => "禁言原因不得超过10字符",
            "disablesendmsg_duration.require" => "禁言时长必填",
            "disablesendmsg_duration.number" => "禁言时长得是数字",
            "disablesendmsg_duration.between" => "禁言时长必须在(0-365)之间"
        ];
        $validate = Validate::make($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => $validate->getError()];
        }
        return ["code" => _MSG_SYSTEM_SUCCESS, "msg" => "校验成功", "data" => $data];
    }
    public function checkUpdate($data)
    {
        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $data[$key] = htmlentities($value);
            }
        }
        $rule = [
            "id" => "require|number",
            "is_jiechu" => "require|number|between:0,1"
        ];
        $msg = [
            "id.require" => "禁言ID必须提供",
            "id.number" => "禁言ID必须是数字",
            "is_jiechu.require" => "禁言解除标识必须提供",
            "is_jiechu.number" => "禁言解除标识必须是数字",
            "is_jiechu.between" => "禁言解除标识必须在(0-1)之间"
        ];
        $validate = Validate::make($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => $validate->getError()];
        }
        return ["code" => _MSG_SYSTEM_SUCCESS, "msg" => "校验成功", "data" => $data];
    }
}

控制层:Disablesendmsg.php

<?php
/**
 * 禁言
 */
namespace app\sports\disablesendmsg\controller;
use app\common\BaseController;
use app\common\DataValidate;
use app\lucky\user\validate\UserValidate;
use app\sports\disablesendmsg\context\DisablesendmsgListContext;
use app\sports\disablesendmsg\service\DisablesendmsgService;
use app\sports\disablesendmsg\validate\DisablesendmsgValidate;
use think\App;
use think\Request;
class Disablesendmsg extends BaseController
{
    public $service = null;
    public $sys = '';
    public function __construct(App $app = null, Request $request = null)
    {
        parent::__construct($app, $request);
        $this->service = new DisablesendmsgService();
        $this->sys = \session('sys') ? \session('sys') : 343;
    }
    /**
     * 获取禁言列表
     */
    public function getDisablesendmsgList()
    {
        $postData = $this->request->post();
        $valiData = DataValidate::getInstance()->checkPager($postData);
        if ($valiData["code"] != _MSG_SYSTEM_SUCCESS) {
            $this->_jsonReturnV2($valiData["code"], '', $valiData["msg"]);
        }
        $context = new DisablesendmsgListContext($valiData['data']);
        $context->operatorId = $this->loginInfo['user_id'];
        $data = $this->service->getList($context,  $this->sys);
        $this->_jsonReturnV2(_MSG_SUCCESS, $data, '查询成功');
    }
    /**
     * 禁言添加
     */
    public function addDisablesendmsg()
    {
        $postData = $this->request->post();
        $valiData = DisablesendmsgValidate::getInstance()->checkAdd($postData);
        if ($valiData["code"] != _MSG_SYSTEM_SUCCESS) {
            $this->_jsonReturnV2($valiData["code"], '', $valiData["msg"]);
        }
        //operator_id操作员id
        $valiData['data']['operator_id'] = $this->loginInfo['user_id'];
        $data = $this->service->addDisablesendmsg($valiData['data'], $this->sys);
        if ($data) {
            $this->_jsonReturnV2(_MSG_SUCCESS, '', '添加成功');
        } else {
            $this->_jsonReturnV2(_MSG_AlREADY_EXIST, '', '添加失败');
        }
    }
}

服务层:DisablesendmsgService.php,服务接口,处理业务逻辑

<?php
/**
 * 禁言
 */
namespace app\sports\disablesendmsg\service;
use app\common\DataService;
use app\common\Redis;
use app\lucky\user\model\UserV2Model;
use app\lucky\user\service\UserService;
use app\sports\disablesendmsg\context\DisablesendmsgListContext;
use app\sports\disablesendmsg\model\DisablesendmsgModel;
class DisablesendmsgService
{
    public $model = '';
    public function __construct()
    {
        $this->model = new DisablesendmsgModel();
    }
    /**
     * 查询列表
     *
     * @access public
     * @param array $data 数据数组
     * @param string $sys 系统
     * @return array 返回类型
     */
    public function getList(DisablesendmsgListContext $context, $sys = 343)
    {
        $context->page_no = isset($context->page_no) ? $context->page_no : 1;
        $context->page_count = isset($context->page_count) ? $context->page_count : 10;
        //来源app1后台2
        $context->sourceId = isset($context->sourceId) ? $context->sourceId : 1;
        $context->operatorId = isset($context->operatorId) ? $context->operatorId : 0;
        $context->user_nickname = isset($context->user_nickname) ? $context->user_nickname : '';
        $context->isDel = isset($context->isDel) ? $context->isDel : 0;
        if ($context->user_nickname) {
            $userIdArr = UserService::getInstance()->getUserInfoByLikeUserNickname($context->user_nickname, $sys);
            $context->userIdArr = $userIdArr;
        }
        $result = $this->model->selectStatusListByPager($context, $sys);
        $data = $result['data'];
        //处理前端数据格式
        if ($data) {
            foreach ($data as $k => $v) {
                $userInfo = UserService::getInstance()->getUserInfo($v['user_id'], $sys);
                $data[$k]['nickname'] = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
                $data[$k]['headimgurl'] = isset($userInfo['headimgurl']) ? $userInfo['headimgurl'] : '';
                if ($context->sourceId == 1) {
                    //来源app1后台2 app时间格式
                    $data[$k]['disablesendmsg_date'] = DataService::getInstance()->_handleCreateTimeTwo(date('Y-m-d H:i:s', $v['disablesendmsg_time']));
                } else {
                    //后台时间格式
                    $data[$k]['disablesendmsg_date'] = date('Y-m-d H:i', $v['disablesendmsg_time']);
                }
            }
        }
        $pageTotal = (int)ceil($result['total'] / $context->page_count);
        $page_info = [
            'total' => $result['total'],//总记录
            'page_no' => $context->page_no,//页数
            'page_count' => $context->page_count,//每页显示数量
            'page_total' => $pageTotal,//总页数
        ];
        return [
            'page_info' => $page_info,
            'list' => $data,
        ];
    }
    /**
     * 添加禁言
     *
     * @access public
     * @param array $data 数据数组
     * @param string $sys 系统
     * @return array 返回类型
     */
    public function addDisablesendmsg($data, $sys = 343)
    {
        //防止重复添加
        $repetitionData = $this->model->getOneByUserId($data['user_id'], $sys);
        if ($repetitionData) {
            $updateData = [
                "operator_id" => isset($data['operator_id']) ? $data['operator_id'] : 0,
                "source_id" => isset($data['source_id']) ? $data['source_id'] : 1,
                "is_jiechu" => isset($data['is_jiechu']) ? $data['is_jiechu'] : 0,
                "disablesendmsg_reason" => isset($data['disablesendmsg_reason']) ? $data['disablesendmsg_reason'] : "",
                "disablesendmsg_duration" => isset($data['disablesendmsg_duration']) ? $data['disablesendmsg_duration'] : "",
                'disablesendmsg_time' => time(),
                'update_time' => time(),
                "sys" => $sys
            ];
            $result = $this->model->updateByUserId($updateData, $data['user_id'], $sys);
            if ($result) {
                $key = $this->_getDisablesendmsgUserListKey($sys);
                Redis::getInstance()->redisZadd($key, time(), $data['user_id']);
            }
            return $result;
        } else {
            $data = [
                "operator_id" => isset($data['operator_id']) ? $data['operator_id'] : 0,
                "user_id" => isset($data['user_id']) ? $data['user_id'] : 0,
                "source_id" => isset($data['source_id']) ? $data['source_id'] : 1,
                "disablesendmsg_reason" => isset($data['disablesendmsg_reason']) ? $data['disablesendmsg_reason'] : "",
                "disablesendmsg_duration" => isset($data['disablesendmsg_duration']) ? $data['disablesendmsg_duration'] : "",
                'disablesendmsg_time' => time(),
                "sys" => $sys
            ];
            $result = $this->model->insert($data, $sys);
            if ($result) {
                //用户的禁言列表
                //将被禁言的用户id 添加到redis/ key=Disablesendmsg:343:user
                $key = $this->_getDisablesendmsgUserListKey($sys);
                Redis::getInstance()->redisZadd($key, time(), $data['user_id']);
                return true;
            } else {
                return false;
            }
        }
    }
    /**
     * 获取用户的禁言列表的key
     */
    private function _getDisablesendmsgUserListKey($sys)
    {
        return "Disablesendmsg:{$sys}:users";
    }
}

数据层 DisablesendmsgModel.php 尽量简洁

<?php
/**
 * 禁言
 */
namespace app\sports\disablesendmsg\model;
use app\sports\disablesendmsg\context\DisablesendmsgListContext;
use think\Db;
class DisablesendmsgModel
{
    /**
     * @param $data
     * @param $sys
     * @return mixed
     * @description 添加禁言
     */
    public function insertGetId($data)
    {
        return Db::table("user_disablesendmsg")->insertGetId($data);
    }
    /**
     * @param $data
     * @return mixed
     * @description 修改信息
     */
    public function updateByUserId($data, $userId, $sys = 343)
    {
        return DB::table("user_disablesendmsg")
            ->where("user_id", $userId)
            ->where("sys", $sys)
            ->update($data);
    }
    /**
     * @param $data
     * @param $status
     * @return array
     * @description 分页查询
     */
    public function selectStatusListByPager(DisablesendmsgListContext $context, $sys)
    {
        $where = [];
        if ($context->operatorId) {
            $where['operator_id'] = $context->operatorId;
        }
        $op = Db::table('user_disablesendmsg')
            ->where(array('is_del' => $context->isDel, 'sys' => $sys, 'is_jiechu' => 0))
            ->where($where)
            ->field(array('id', 'user_id', 'disablesendmsg_reason', 'disablesendmsg_duration', 'disablesendmsg_time', 'is_jiechu'))
            ->order('disablesendmsg_time desc');
        if ($context->user_nickname) {
            $op = $op->whereIn('user_id', $context->userIdArr);
        }
        //总共记录
        $count = count($op->select());
        $list = $op->page($context->page_no, $context->page_count)->select();
        return [
            'data' => $list,
            'total' => $count
        ];
    }
    /**
     * @param $id
     * @return mixed
     * @description id 根据id查询
     */
    public function getOneById($id, $sys)
    {
        return DB::table("user_disablesendmsg")
            ->where("id", $id)
            ->where("sys", $sys)
            ->find();
    }
}


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
4天前
|
存储 网络协议 Ubuntu
Linux环境下的SVN服务器搭建并结合内网穿透实现远程连接
Linux环境下的SVN服务器搭建并结合内网穿透实现远程连接
|
4天前
|
运维 Ubuntu Linux
【服务器】安装Docker环境
【服务器】安装Docker环境
408 0
|
4天前
|
开发框架 JavaScript 中间件
node+express搭建服务器环境
node+express搭建服务器环境
node+express搭建服务器环境
|
4天前
|
PyTorch TensorFlow 算法框架/工具
【科研入门】搭建与配置云服务器的论文环境
本文介绍了如何搭建云服务器并配置论文代码环境,以AutoDL平台为例。首先,租用服务器并选择符合代码需求的镜像版本,如Python 3.7、TensorFlow 1.15和PyTorch。接着,启动服务器进入终端,克隆项目代码并使用Conda创建隔离的环境安装所需包。如果需在Pycharm中工作,还需在Pycharm内创建相同环境。最后,根据项目配置安装Tensorflow和PyTorch,遇到缺失包时通过`pip install`补充。完成配置后,可克隆服务器以备后续使用。遇到版本不兼容问题,可调整Conda环境的Python版本。
48 1
【科研入门】搭建与配置云服务器的论文环境
|
4天前
|
数据安全/隐私保护 Windows
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问-2
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问
|
4天前
|
存储 网络协议 文件存储
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问-1
使用Serv-U FTP服务器共享文件,实现无公网IP环境下远程访问
|
4天前
|
Java 应用服务中间件 网络安全
如何配置一个公司服务器的环境
如何配置一个公司服务器的环境
11 0
|
4天前
|
弹性计算 安全 Java
阿里云服务器配置、(xshell)远程连接、搭建环境、设置安全组、域名备案、申请ssl证书
以下是内容的摘要: 在阿里云购买服务器并进行基本配置的步骤如下: 1. **准备工作**: - 注册阿里云账号:访问阿里云官网并注册新账号,输入用户名、手机号和验证码。 - 实名认证:在个人中心进行实名认证,建议选择企业实名,因为个人实名可能无法索取企业发票。
|
4天前
|
安全 Java Linux
如何实现无公网IP及服务器实现公网环境企业微信网页应用开发调试
如何实现无公网IP及服务器实现公网环境企业微信网页应用开发调试
|
4天前
|
关系型数据库 MySQL Java
阿里云服务器搭建部署JavaWeb环境
以下是内容的摘要: 本文介绍了在阿里云上部署云服务器并配置Tomcat、JDK和MySQL的步骤。首先,需要注册阿里云账号并进行实名认证,然后选择合适的服务器配置并购买。接着,通过XShell和Xftp连接并上传Tomcat和JDK安装包到服务器,解压并配置环境变量。确保8080端口开放,并启动Tomcat。对于MySQL的安装,包括下载安装包、设置开机启动、修改密码、授权远程登录以及将本地数据库迁移至云服务器。最后,文章总结了整个过程并预告了后续关于项目部署的内容。

热门文章

最新文章