PHP code example of jobsys / starter-module
1. Go to this page and download the library: Download jobsys/starter-module library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jobsys / starter-module example snippets
"Starter" => [
"route_prefix" => "manager", // 路由前缀
]
//性别类型
if (!Dictionary::where('name', 'gender_type')->exists()) {
$dict = Dictionary::create(['display_name' => '性别类型', 'name' => 'gender_type']);
$dict->items()->insert([
['dictionary_id' => $dict->id, 'display_name' => '男', 'value' => '男'],
['dictionary_id' => $dict->id, 'display_name' => '女', 'value' => '女'],
]);
}
log_access('查看xxx资源', $id);
// 基础状态码
enum State: string
{
const SUCCESS = 'STATE_CODE_SUCCESS';
const FAIL = 'STATE_CODE_FAIL';
const DB_FAIL = 'STATE_CODE_DB_FAIL';
const NOT_LOGIN = 'STATE_CODE_NOT_LOGIN';
const NOT_FOUND = 'STATE_CODE_NOT_FOUND';
const NOT_ALLOWED = 'STATE_CODE_NOT_ALLOWED';
const INVALID_PARAMETERS = 'STATE_CODE_INVALID_PARAMETERS';
const DUPLICATION = 'STATE_CODE_DUPLICATION';
const USER_INVALID = 'STATE_CODE_USER_INVALID';
const USER_INFO_INCOMPLETE = 'STATE_CODE_USER_INFO_INCOMPLETE';
const TOO_FREQUENTLY = 'STATE_CODE_TOO_FREQUENTLY';
const CAPTCHA_ERROR = 'STATE_CODE_CAPTCHA_ERROR';
const VERIFY_ERROR = 'STATE_CODE_VERIFY_ERROR';
const MOBILE_ERROR = 'STATE_CODE_MOBILE_ERROR';
const EMAIL_ERROR = 'STATE_CODE_EMAIL_ERROR';
const RISKY_CONTENT = 'STATE_CODE_RISKY_CONTENT';
const RISKY_IMAGE = 'STATE_CODE_RISKY_IMAGE';
}
/**
* 获取用户相关内容 [菜单, 权限, 部门, 个人信息, 是否超级管理员]
* @return array
*/
function starter_setup_user(): array
/**
* 获取用户菜单
* @param $user
* @param array $permissions
* @return array
*/
function starter_get_user_menu($user = null, array $permissions = []): array
/**
* 获取字典
* @param string $name
* @param bool $only_options
* @return array|BaseModel|Dictionary|null
*/
function dict_get(string $name, bool $only_options = true): BaseModel|array|Dictionary|null
/**
* @param string $action 动作
* @param string $object 对象
* @param string $effect 结果
* @param int $user_id 用户ID
* @param string $user_type 用户类型
* @param string $brief 摘要
* @return void
*/
function log_access(string $action, string $object = '', string $effect = '', string $brief = '', int $user_id = 0, string $user_type = 'user'): void
// `json` 方法结果与状态都是自定的
public function json($result = null, $status = State::SUCCESS): JsonResponse
// `message` 方法状态为 `State::FAIL`
public function message($message): JsonResponse
// `success` 方法状态为 `State::SUCCESS`
public function success($result): JsonResponse
//返回结果示例
[
'status' => 'SUCCESS',
'result' => [
'id' => 1,
'name' => 'name',
'created_at' => '2021-08-17 16:22:49',
'updated_at' => '2021-08-17 16:22:49',
],
]
bash
# 安装依赖
composer module:enable Starter && php artisan module:publish-migration Starter && php artisan migrate
bash
php artisan db:seed --class=DatabaseSeeder
bash
2014_10_12_000001_create_dict_tables.php # 字典表
2022_08_17_000002_create_access_logs_table.php # 日志表
2023_04_07_162249_create_notifications_table.php # 消息通知表