PHP开发工具:neteasecloudmusic,phpstorm,postman,rdm,navicat for mysql,iterm,google chrome,dingtalk,wechat,youdao
正式和测试区别开有:域名,数据库,代码仓库分支(正式master,测试dev,开发app).
app-->合并->owen(自己)-->合并->app-->合并->dev
app-->合并->master
主流环境(插件):最笨就用宝塔面板,安全考虑使用以下:
- Grafana接口监控 https://www.jianshu.com/p/3527f48165d7
- Jenkins代码部署
- Sentry,kibana日志查看
- GitLab代码仓库
- Jumpserver堡垒机:在线文件管理,命令执行
- sql审计平台
- 接口文档:yapi,swagger,小幺鸡,mindoc(https://github.com/lifei6671/mindoc)
- 石墨在线办公文档
- 项目分工流程:tapd,禅道,钉钉蚂蚁分工,worktile
- 产品原型图,UI:蓝湖
PHP常用插件包:composter
- topthink/framework ThinkPHP 核心框架库
- rmccue/requests 用PHP编写的HTTP库
- predis/predis **[Redis-
Predis
扩展](http://www.baidu.com/link?url=dDu-gyiL3eSHKTgJtqHCCLqgNxJ-ZzAC0ibIPeuqT7MmwMmyNUy4rYnyB9jMASWsJeluJSTCUVqbqLaE8RXSMGJAnSXMvHO6HpSk5QPw3)** - lcobucci/jwt JWT 创建 Token
- phpmailer/phpmailer 实现PHP发邮件功能
- endroid/qr-code PHP-生成二维码
- codeitnowin/barcode PHP-生成条形码
- aliyuncs/oss-sdk-php 阿里云对象存储OSS(文件图片上传)
- topthink/think-queue 队列
- yansongda/pay 最优雅的微信和支付宝支付包
- symfony/event-dispatcher 通过分派事件并侦听事件来相互通信
- 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": "[email protected]"
}
],
"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"
}
}
PHP代码规范模块:
参数: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();
}
}