林一峰
7 years ago
18 changed files with 469 additions and 102 deletions
-
42app/Common/JPush.php
-
133app/Common/ReturnData.php
-
105app/Common/Token.php
-
57app/Common/function.php
-
30app/Http/Controllers/Admin/GoodsController.php
-
34app/Http/Controllers/Admin/GoodstypeController.php
-
16app/Http/Controllers/Admin/LoginController.php
-
14app/Http/Controllers/Admin/UserController.php
-
10app/Http/Controllers/Admin/UserRoleController.php
-
30app/Http/Middleware/Token.php
-
3composer.json
-
6resources/views/admin/goods/add.blade.php
-
6resources/views/admin/goods/edit.blade.php
-
14resources/views/admin/goods/index.blade.php
-
8resources/views/admin/goodstype/add.blade.php
-
4resources/views/admin/goodstype/edit.blade.php
-
8resources/views/admin/goodstype/index.blade.php
-
51routes/web.php
@ -0,0 +1,42 @@ |
|||
<?php |
|||
namespace App\Common; |
|||
|
|||
use JPush\Client as JPushMsg; |
|||
use Illuminate\Support\Facades\Log; |
|||
|
|||
//极光推送,"jpush/jpush": "v3.5.*"
|
|||
class JPush |
|||
{ |
|||
const APP_KEY = 'b82cd9fcd0cbb92866d6d726'; |
|||
const APP_SECRET = 'ac92d336f90842051dc12f49'; |
|||
|
|||
//$registration_id = getenv('registration_id');
|
|||
|
|||
public static function send($msg, $param='') |
|||
{ |
|||
$client = new JPushMsg(self::APP_KEY, self::APP_SECRET, null); |
|||
|
|||
$push_payload = $client->push(); |
|||
$push_payload = $push_payload->setPlatform('all'); |
|||
if(isset($param['mobile'])){$push_payload = $push_payload->addAlias(md5($param['mobile']));} |
|||
$push_payload = $push_payload->addAllAudience(); |
|||
$push_payload = $push_payload->setNotificationAlert($msg); |
|||
|
|||
try |
|||
{ |
|||
$push_payload->send(); |
|||
} |
|||
catch (JPushMsg\Exceptions\APIConnectionException $e) |
|||
{ |
|||
Log::info($e); |
|||
return false; |
|||
} |
|||
catch (JPushMsg\Exceptions\APIRequestException $e) |
|||
{ |
|||
Log::info($e); |
|||
return false; |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
} |
@ -0,0 +1,133 @@ |
|||
<?php |
|||
namespace App\Common; |
|||
|
|||
class ReturnData |
|||
{ |
|||
//通用
|
|||
const SUCCESS = 0; //成功
|
|||
const FORBIDDEN = 8001; //权限不足
|
|||
const SYSTEM_FAIL = 8002; //系统错误,如数据写入失败之类的
|
|||
const PARAMS_ERROR = 8003; //参数错误
|
|||
const NOT_FOUND = 8004; //资源未找到
|
|||
const TOKEN_ERROR = 8005; //token错误
|
|||
const SIGN_FAIL = 8006; //签名错误
|
|||
const RECORD_EXIST = 8007; //记录已存在
|
|||
const RECORD_NOT_EXIST = 8008; //记录不存在
|
|||
const NOT_MODIFY = 8009; //没有变动
|
|||
const IMG_TYPE_FALSE = 8010; //图片格式不正确
|
|||
|
|||
//参数相关
|
|||
const EMAIL_EXIST = 8201; //邮箱已存在
|
|||
const EMAIL_FORMAT_FAIL = 8202; //邮箱格式不对正确
|
|||
const MOBILE_NOT_FIND = 8204; //手机号码不存在
|
|||
const MOBILE_HAS_MORE = 8205; //存在多个手机号码
|
|||
const NAME_EXIST = 8206; //名称已被使用
|
|||
const MOBILE_EXIST = 8207; //手机号已存在
|
|||
const NOT_UP_GRADE = 8208; //不符合升级条件
|
|||
const NOT_DOWN_GRADE = 8209; //不符合降级条件
|
|||
|
|||
//登录、账号相关
|
|||
const USERNAME_REQUIRED = 8401; //登录账号为必填
|
|||
const PASSWORD_REQUIRED = 8402; //登录密码为必填
|
|||
const USERNAME_EXIST = 8403; //登录账号已被使用
|
|||
const ADMINNAME_REQUIRED = 8404; //管理员姓名不能为空
|
|||
const PASSWORD_NOT_MATCH = 8405; //密码错误
|
|||
const OLD_PASSWORD_NOT_MATCH = 8406; //旧密码不匹配
|
|||
const PASSWORD_CONFIRM_FAIL = 8407; //两次输入的密码不匹配
|
|||
const PASSWORD_FORMAT_FAIL = 8408; //密码格式不对
|
|||
const APPLY_SIGN_FAIL = 8510; //注册邀请码错误
|
|||
|
|||
//验证码
|
|||
const CODE_NOT_EXIST = 8801; //当前状态不能操作
|
|||
|
|||
//app
|
|||
const AUTH_FAIL = 9001; //鉴权失败
|
|||
const TOKEN_EXP = 9002; //Token失效
|
|||
const MOBILE_FORMAT_FAIL = 9003; //手机格式不正确
|
|||
const VERIFY_TYPE_FAIL = 9004; //验证码业务类型无效
|
|||
const BANK_TYPE_FAIL = 9005; //该银行不支持
|
|||
const INVALID_IDCARD = 9006;//身份证无效
|
|||
const REQUEST_AMOUNT_MIN_LESS = 9007;//小于最小提现金额
|
|||
const SERVICE_AMOUNT_NOT_ENOUGH = 9008;//可提现余额不足
|
|||
|
|||
//中文错误详情
|
|||
public static $codeTexts = array( |
|||
0 => '操作成功', |
|||
8001 => '权限不足', |
|||
8002 => '系统错误,请联系管理员', |
|||
8003 => '参数错误', |
|||
8004 => '资源未找到', |
|||
8005 => 'token错误', |
|||
8006 => '签名错误', |
|||
8007 => '记录已存在', |
|||
8008 => '记录不存在', |
|||
8009 => '没有变动', |
|||
8010 => '图片格式不正确', |
|||
//参数错误
|
|||
8201 => '邮箱已存在', |
|||
8202 => '邮箱格式不对正确', |
|||
8204 => '手机号码不存在', |
|||
8205 => '存在多个手机号码', |
|||
8206 => '名称已被使用', |
|||
8207 => '手机号已存在', |
|||
8208 => '不符合升级条件', |
|||
8209 => '不符合降级条件', |
|||
//登录、账号相关
|
|||
8401 => '登录账号为必填', |
|||
8402 => '登录密码为必填', |
|||
8403 => '登录账号已被使用', |
|||
8404 => '管理员姓名不能为空', |
|||
8405 => '登录失败', |
|||
8406 => '原密码不匹配', |
|||
8407 => '两次输入的密码不匹配', |
|||
8408 => '密码格式错误,请输入%s到%s位字符', |
|||
8510 => '注册邀请码不存在或已被使用', |
|||
//app
|
|||
9001 => '鉴权失败', |
|||
9002 => 'Token失效', |
|||
9003 => '手机格式不正确', |
|||
9004 => '验证码业务类型无效', |
|||
9005 => '该银行不支持', |
|||
9006 => '身份证无效', |
|||
9007 => '小于最小提现金额', |
|||
9008 => '可提现余额不足', |
|||
//验证码
|
|||
8801 =>'验证码无效', |
|||
); |
|||
|
|||
public static function create($code, $msg = '', $data = null) |
|||
{ |
|||
if (empty($msg) && isset(self::$codeTexts[$code])) |
|||
{ |
|||
$msg = self::$codeTexts[$code]; |
|||
} |
|||
|
|||
return array('code' => $code, 'msg' => $msg, 'data' => $data); |
|||
} |
|||
|
|||
public static function success($msg = '', $data = null) |
|||
{ |
|||
if (empty($msg) && isset(self::$codeTexts[self::SUCCESS])) |
|||
{ |
|||
$msg = self::$codeTexts[self::SUCCESS]; |
|||
} |
|||
|
|||
return array('code' => self::SUCCESS, 'msg' => $msg, 'data' => $data); |
|||
} |
|||
|
|||
public static function error($code, $msg = '', $data = null) |
|||
{ |
|||
if (empty($msg) && isset(self::$codeTexts[$code])) |
|||
{ |
|||
$msg = self::$codeTexts[$code]; |
|||
} |
|||
|
|||
if ($code == ReturnCode::SUCCESS) |
|||
{ |
|||
$code = ReturnCode::SYSTEM_FAIL; |
|||
$msg = '系统错误'; |
|||
} |
|||
|
|||
return array('code' => $code, 'msg' => $msg, 'data' => $data); |
|||
} |
|||
} |
@ -0,0 +1,105 @@ |
|||
<?php |
|||
namespace App\Common; |
|||
use DB; |
|||
|
|||
class Token |
|||
{ |
|||
const TYPE_APP = 0; |
|||
const TYPE_ADMIN = 1; |
|||
const TYPE_WEIXIN = 2; |
|||
const TYPE_WAP = 3; |
|||
const TYPE_PC = 4; |
|||
|
|||
// 已验证的type
|
|||
public static $type; |
|||
// 验证为token时的uid
|
|||
public static $uid; |
|||
// 验证为sign时的app.id
|
|||
public static $app; |
|||
// 已验证的data
|
|||
public static $data = []; |
|||
|
|||
/** |
|||
* 验证token |
|||
* |
|||
* @param $token |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public static function checkToken($token) |
|||
{ |
|||
$token = DB::table('token')->where('token', $token)->first(); |
|||
|
|||
if ($token) |
|||
{ |
|||
self::$type = $token->type; |
|||
self::$uid = $token->uid; |
|||
self::$data = $token->data ? json_decode($token->data, true) : []; |
|||
} |
|||
|
|||
return $token ? true : false; |
|||
} |
|||
|
|||
/** |
|||
* 验证sign, |
|||
* sign生成方式:md5(app_key + app_secret + time) |
|||
* 必传参数:app_key, sign, sign_time |
|||
* |
|||
* @param $appKey |
|||
* @param $signTime |
|||
* @param $sign |
|||
* |
|||
* @return bool |
|||
*/ |
|||
public static function checkSign($appKey, $signTime, $sign) |
|||
{ |
|||
if (!$appRes = DB::table('appsign')->where('app_key', $appKey)->first()) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
//验证sign
|
|||
$newSign = md5($appKey . $appRes->app_secret . $signTime); |
|||
if ($sign == $newSign) |
|||
{ |
|||
self::$type = self::TYPE_ADMIN; |
|||
self::$app = $appRes; |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* 生成token |
|||
* |
|||
* @param $type |
|||
* @param $uid |
|||
* @param $data |
|||
* |
|||
* @return string |
|||
*/ |
|||
public static function getToken($type, $uid, $data = []) |
|||
{ |
|||
//支持多账号登录
|
|||
if ($token = DB::table('token')->where(['type' => $type, 'uid' => $uid])->orderBy('id', 'desc')->first()) |
|||
{ |
|||
if($data == $token->data && strtotime($token->expired_at)>time()) |
|||
{ |
|||
return $token->token; |
|||
} |
|||
} |
|||
|
|||
//生成新token
|
|||
$token = md5($type . '-' . $uid . '-' . microtime() . rand(0, 9999)); |
|||
DB::table('token')->insert([ |
|||
'token' => $token, |
|||
'type' => $type, |
|||
'uid' => $uid, |
|||
'data' => $data ? json_encode($data) : '', |
|||
'expired_at' => date('Y-m-d H:i:s') |
|||
]); |
|||
|
|||
return $token; |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
<?php |
|||
namespace App\Http\Middleware; |
|||
|
|||
use Closure; |
|||
use App\Common\ReturnData; |
|||
use App\Common\Token; |
|||
|
|||
class Token |
|||
{ |
|||
/** |
|||
* Token验证 |
|||
* token可以在header里面传递【Token】,也可以在参数里面传【token】,注意区分大小写 |
|||
*/ |
|||
public function handle($request, Closure $next) |
|||
{ |
|||
$token = $request->header('Token') ?: $request->input('token'); |
|||
|
|||
if (!$token) |
|||
{ |
|||
return ReturnData::create(ReturnData::FORBIDDEN); |
|||
} |
|||
|
|||
if (!Token::checkToken($token)) |
|||
{ |
|||
return ReturnData::create(ReturnData::TOKEN_ERROR); |
|||
} |
|||
|
|||
return $next($request); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue