191 lines
5.8 KiB
PHP
Executable File
191 lines
5.8 KiB
PHP
Executable File
<?php
|
||
|
||
//平台用户入口
|
||
|
||
namespace api\controllers\gm\v1;
|
||
|
||
use Yii;
|
||
use yii\web\Controller;
|
||
use common\models\Member as User;
|
||
use addons\models\AcUserExt;
|
||
use addons\models\AcStore;
|
||
use addons\models\AcEmployee;
|
||
use addons\models\AcPlat;
|
||
use common\models\CVcode;
|
||
|
||
class Common extends Controller
|
||
{
|
||
protected int $pid = 0;
|
||
protected int $user_id = 0;
|
||
protected int $site_id = 0;
|
||
protected int $store_id = 0;
|
||
protected int $employee_id = 0;
|
||
protected object $user_info;
|
||
protected array $postdata = []; //重新整理一下客户端传输的数据
|
||
protected bool $imgHttp = TRUE; //图片附件地址生成http(s) //false:不生成http网址; true:仅生成http网址; https:生成https网址
|
||
protected int $page = 1; //当前数据分页
|
||
protected int $pageSize = 10; //默认数据分页大小
|
||
public $enableCsrfValidation = FALSE;
|
||
//替代常规的_construct 析构函数;其他方法调用前执行
|
||
protected int $timestamp = TIMESTAMP;
|
||
|
||
//替代常规的_construct 析构函数;其他方法调用前执行
|
||
|
||
public function init()
|
||
{
|
||
parent::init();
|
||
//判断请求内容类型 content-type,支持 json请求
|
||
$postdata = Yii::$app->request->post();
|
||
$headers = Yii::$app->request->headers;
|
||
$contentType = $headers->get('content-type');
|
||
if ($contentType == "application/json") {
|
||
$postdata = json_decode(file_get_contents('php://input'), TRUE);
|
||
}
|
||
$this->postdata = $postdata;
|
||
|
||
$session = Yii::$app->session;
|
||
if (isset($postdata['pid'])) {
|
||
if ((int)$postdata['pid'] > 0) {
|
||
$this->pid = (int)$postdata['pid'];
|
||
$session->set('pid', $this->pid);
|
||
} else {
|
||
$session->set('pid', 0);
|
||
}
|
||
} else if (isset($_GET['pid'])) {
|
||
if ((int)$_GET['pid'] > 0) {
|
||
$this->pid = $_GET['pid'];
|
||
$session->set('pid', $this->pid);
|
||
} else {
|
||
$session->set('pid', 0);
|
||
}
|
||
} else {
|
||
$session->set('pid', 0);
|
||
}
|
||
|
||
$this->site_id = Yii::$app->params['siteId'];
|
||
|
||
if (isset($postdata['uid']) && (int)$postdata['uid'] > 0) {
|
||
$this->user_id = (int)$postdata['uid'];
|
||
}
|
||
if ($this->user_id) {
|
||
$userModel = new User();
|
||
$res = $userModel->findOne($this->user_id);
|
||
if (!$res) {
|
||
$this->user_id = 0;
|
||
} else {
|
||
$this->user_info = $res;
|
||
}
|
||
//TBD 后期需加上更多登陆验证逻辑
|
||
}
|
||
if (isset($postdata['employee_id']) && (int)$postdata['employee_id'] > 0) {
|
||
$this->employee_id = (int)$postdata['employee_id'];
|
||
}
|
||
|
||
$page = isset($postdata['page']) ? (int)$postdata['page'] : 1;
|
||
$this->page = $page <= 0 ? 1 : $page;
|
||
$pageSize = isset($postdata['psize']) ? (int)$postdata['psize'] : 10;
|
||
$this->pageSize = $pageSize <= 0 ? 10 : $pageSize;
|
||
}
|
||
|
||
public function result($msg, $data = [], $code = 0)
|
||
{
|
||
$return = [];
|
||
$return['code'] = $code;
|
||
$return['msg'] = $msg;
|
||
if ($data) {
|
||
$return['data'] = $data;
|
||
}
|
||
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
||
}
|
||
|
||
public function userToken($user = [], $refresh = FALSE, $expire = NULL)
|
||
{
|
||
//生成token(用户数据,是否强制更新,过期时间 // TBD 具体方法有待优化
|
||
$expire = $expire ? $expire : '+1 day';
|
||
$token = Yii::$app->session->get('memberToken');
|
||
$tokenExpired = Yii::$app->session->get('memberTokenExpired');
|
||
if (!$token || $refresh) {
|
||
$tokenExpired = strtotime($expire) * 1000;
|
||
$token = md5('username=' . $user['username'] . '&mobile=' . $user['mobile'] . '&time=' . $tokenExpired);
|
||
Yii::$app->session->set('memberToken', $token);
|
||
Yii::$app->session->set('memberTokenExpired', $tokenExpired);
|
||
}
|
||
$user['token'] = $token;
|
||
$user['tokenExpired'] = $tokenExpired;
|
||
return $user;
|
||
}
|
||
|
||
/*用户权限获取判断
|
||
@user 用户信息(model原型)
|
||
@auth 要授权的行为动作
|
||
@data 要操作的数据
|
||
*/
|
||
public function userPrivilege($user,$auth,$data)
|
||
{
|
||
//TD:待明确更多用户权限角色的配置、获取
|
||
switch ($auth) {
|
||
case 'feeEdit':
|
||
case 'feeOrderEdit':
|
||
case 'feeDriverEdit':
|
||
//允许本人操作该员工创建或被关联的运单、数据
|
||
if($user->id == $data->create_by) return true;
|
||
//允许超管操作
|
||
if($user->id == 1) return true;
|
||
return false;
|
||
break;
|
||
case 'orderPreEdit':
|
||
case 'orderEdit':
|
||
case 'orderPreDelete':
|
||
default:
|
||
//允许员工本人操作该员工创建或被关联的运单、数据
|
||
if($user->id == $data->from_mid) return true;
|
||
if($data->employee_id == $this->employee_id) return true;
|
||
|
||
//允许超管操作
|
||
if($user->id == 1) return true;
|
||
|
||
return false;
|
||
break;
|
||
}
|
||
}
|
||
|
||
/*
|
||
* SN格式化处理(ID转SN)
|
||
* */
|
||
public function idToSn($id,$type){
|
||
switch ($type) {
|
||
case 'order':
|
||
$pre = 'YD';
|
||
$sn = '00000000000'; //11位
|
||
$sn = substr($sn, 0, 11 - strlen($id));
|
||
$sn .= $id;
|
||
break;
|
||
}
|
||
return $pre.$sn;
|
||
}
|
||
|
||
//校验财务验证码
|
||
public function vcodeCwCheck($vcode){
|
||
$plat = AcPlat::findOne($this->pid);
|
||
$platExt = json_decode($plat->remark, TRUE);
|
||
if(!isset($platExt['cw_phone'])) return ['res'=>false,'err'=>'平台尚未配置财务手机号','code'=>1];
|
||
|
||
$vcodeModel = new CVcode();
|
||
$_time = time();
|
||
//查询上次发送记录
|
||
$where = [];
|
||
$where['mobile'] = $platExt['cw_phone'];
|
||
$where['deleted'] = 0;
|
||
$where['type'] = 'caiwu';
|
||
$res = $vcodeModel->find()->where($where)->orderby('id DESC')->one();
|
||
if(!$res) return ['res'=>false,'err'=>'没有验证码发送记录','code'=>2];
|
||
if ($vcode != $res->code) {
|
||
return ['res'=>false,'err'=>'财务操作验证码校验失败,请检查并重新输入验证码!','code'=>3];
|
||
}
|
||
if (($_time - $res->create_at) > 7200) {
|
||
return ['res'=>false,'err'=>'验证码已超时,请重新获取后再填写提交!','code'=>4];
|
||
}
|
||
return ['res'=>true,'err'=>'','code'=>0];
|
||
}
|
||
}
|