|
|
<?php
|
|
|
// +----------------------------------------------------------------------
|
|
|
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
|
|
|
// +----------------------------------------------------------------------
|
|
|
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
|
|
|
// +----------------------------------------------------------------------
|
|
|
// | Author: Dean <zxxjjforever@163.com>
|
|
|
// +----------------------------------------------------------------------
|
|
|
namespace api\admin\controller;
|
|
|
|
|
|
use cmf\controller\RestBaseController;
|
|
|
use think\Db;
|
|
|
use think\Validate;
|
|
|
use app\portal\model\MobileCodeModel;
|
|
|
use app\portal\model\UserModel;
|
|
|
use cmf\lib\Storage;
|
|
|
use cmf\lib\Upload;
|
|
|
/**
|
|
|
* @title 用户中心
|
|
|
*/
|
|
|
class PersonController extends RestBaseController
|
|
|
{
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @title 用户密码登录
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/login
|
|
|
* @method POST
|
|
|
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
|
|
|
* @param name:mobile type:int require:1 default: other: desc:登录手机号
|
|
|
* @param name:password type:string require:1 default: other: desc:登录密码
|
|
|
* @return token:be8a600721c417207b45925cba4075f77a8d789b18637c7b7dfe49c883bc5996
|
|
|
*/
|
|
|
public function login(){
|
|
|
if($this->request->isPost()){
|
|
|
$validate = new Validate([
|
|
|
'mobile' => 'require',
|
|
|
'password' => 'require'
|
|
|
]);
|
|
|
$validate->message([
|
|
|
'mobile.require' => '请输入您的手机号!',
|
|
|
'password.require' => '请输入您的密码!'
|
|
|
]);
|
|
|
|
|
|
$data = $this->request->param();
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
|
|
|
$userQuery = Db::name("user");
|
|
|
$findUser = $userQuery->where(['mobile'=>$data['mobile'],'user_type'=>2])->find();
|
|
|
|
|
|
if (empty($findUser)) {
|
|
|
$this->error("用户不存在!");
|
|
|
} else {
|
|
|
switch ($findUser['user_status']) {
|
|
|
case 0:
|
|
|
$this->error('您已被拉黑!');
|
|
|
case 2:
|
|
|
$this->error('账户还没有验证成功!');
|
|
|
}
|
|
|
if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
|
|
|
$this->error("密码不正确!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//验证设备
|
|
|
// $allowedDeviceTypes = ['mobile', 'android', 'iphone', 'ipad', 'web', 'pc', 'mac'];
|
|
|
// if (empty($data['device_type']) || !in_array($data['device_type'], $allowedDeviceTypes)) {
|
|
|
// $this->error("请求错误,未知设备!");
|
|
|
// }
|
|
|
|
|
|
$userTokenQuery = Db::name("user_token")
|
|
|
->where('user_id', $findUser['id']);
|
|
|
// ->where('device_type', $data['device_type']);
|
|
|
$findUserToken = $userTokenQuery->find();
|
|
|
$currentTime = time();
|
|
|
$expireTime = $currentTime + 24 * 3600 * 180;
|
|
|
$token = md5(uniqid()) . md5(uniqid());
|
|
|
if (empty($findUserToken)) {
|
|
|
$result = $userTokenQuery->insert([
|
|
|
'token' => $token,
|
|
|
'user_id' => $findUser['id'],
|
|
|
'expire_time' => $expireTime,
|
|
|
'create_time' => $currentTime,
|
|
|
// 'device_type' => $data['device_type']
|
|
|
]);
|
|
|
} else {
|
|
|
$result = $userTokenQuery
|
|
|
->where('user_id', $findUser['id'])
|
|
|
// ->where('device_type', $data['device_type'])
|
|
|
->update([
|
|
|
'token' => $token,
|
|
|
'expire_time' => $expireTime,
|
|
|
'create_time' => $currentTime
|
|
|
]);
|
|
|
}
|
|
|
if (empty($result)) {
|
|
|
$this->error("登录失败!");
|
|
|
}
|
|
|
$this->success("登录成功!", ['token' => $token]);
|
|
|
}else{
|
|
|
$this->error("请求方式错误!");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 手机验证码登录
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/verifyLogin
|
|
|
* @method POST
|
|
|
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
|
|
|
* @param name:mobile type:int require:1 default: other: desc:登录手机号
|
|
|
* @param name:mobile_code type:init require:1 default: other: desc:验证码
|
|
|
* @return token:be8a600721c417207b45925cba4075f77a8d789b18637c7b7dfe49c883bc5996
|
|
|
*/
|
|
|
public function verifyLogin(){
|
|
|
if($this->request->isPost()){
|
|
|
$data = $this->request->post();
|
|
|
$rule = config('site.v_code');
|
|
|
$validate = new Validate($rule['rule'],$rule['msg']);
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
//验证验证码是否正确
|
|
|
$this->validateMobileCode($data);
|
|
|
|
|
|
$userQuery = Db::name("user");
|
|
|
$findUser = $userQuery->where(['mobile'=>$data['mobile'],'user_type'=>2])->find();
|
|
|
|
|
|
if (empty($findUser)) {
|
|
|
$this->error("用户不存在!");
|
|
|
} else {
|
|
|
switch ($findUser['user_status']) {
|
|
|
case 0:
|
|
|
$this->error('您已被拉黑!');
|
|
|
case 2:
|
|
|
$this->error('账户还没有验证成功!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$userTokenQuery = Db::name("user_token")
|
|
|
->where('user_id', $findUser['id']);
|
|
|
$findUserToken = $userTokenQuery->find();
|
|
|
$currentTime = time();
|
|
|
$expireTime = $currentTime + 24 * 3600 * 180;
|
|
|
$token = md5(uniqid()) . md5(uniqid());
|
|
|
if (empty($findUserToken)) {
|
|
|
$result = $userTokenQuery->insert([
|
|
|
'token' => $token,
|
|
|
'user_id' => $findUser['id'],
|
|
|
'expire_time' => $expireTime,
|
|
|
'create_time' => $currentTime,
|
|
|
]);
|
|
|
} else {
|
|
|
$result = $userTokenQuery
|
|
|
->where('user_id', $findUser['id'])
|
|
|
->update([
|
|
|
'token' => $token,
|
|
|
'expire_time' => $expireTime,
|
|
|
'create_time' => $currentTime
|
|
|
]);
|
|
|
}
|
|
|
if (empty($result)) {
|
|
|
$this->error("登录失败!");
|
|
|
}
|
|
|
$this->success("登录成功!", ['token' => $token]);
|
|
|
}else{
|
|
|
$this->error("请求方式错误!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 忘记密码
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/forgetPassword
|
|
|
* @method POST
|
|
|
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
|
|
|
* @param name:mobile type:int require:1 default: other: desc:登录手机号
|
|
|
* @param name:mobile_code type:init require:1 default: other: desc:验证码
|
|
|
*/
|
|
|
public function forgetPassword(){
|
|
|
if($this->request->isPost()){
|
|
|
$data = $this->request->post();
|
|
|
$rule = config('site.v_code');
|
|
|
$validate = new Validate($rule['rule'],$rule['msg']);
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
//验证验证码是否正确
|
|
|
$this->validateMobileCode($data);
|
|
|
$this->success('成功');
|
|
|
}else{
|
|
|
$this->error('请求方式错误!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 重置密码
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/resetPassword
|
|
|
* @method POST
|
|
|
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
|
|
|
* @param name:mobile type:int require:1 default: other: desc:登录手机号
|
|
|
* @param name:password type:string require:1 default: other: desc:重置密码
|
|
|
* @param name:password_again type:string require:1 default: other: desc:重置密码(再次输入)
|
|
|
*/
|
|
|
public function resetPassword(){
|
|
|
if($this->request->isPost()){
|
|
|
$data = $this->request->post();
|
|
|
$userModel = new UserModel();
|
|
|
$rule = config('site.f_pass');
|
|
|
$validate = new Validate($rule['rule'],$rule['msg']);
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
if($data['password_again'] != $data['password']){
|
|
|
$this->error('两次输入的密码不一致');
|
|
|
}
|
|
|
$password = cmf_password($data['password_again']);
|
|
|
$res = $userModel
|
|
|
->where('mobile',$data['mobile'])
|
|
|
->update(['user_pass'=>$password]);
|
|
|
if($res){
|
|
|
$this->success('重置密码成功');
|
|
|
}else{
|
|
|
$this->error('重置密码失败');
|
|
|
}
|
|
|
}else{
|
|
|
$this->error('请求方式错误!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 发送手机验证码
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/sendMobileCode
|
|
|
* @method POST
|
|
|
* @param name:is_token type:int require:1 default: other: desc:不用带token标识(传1)
|
|
|
* @param name:mobile type:int require:1 default: other: desc:登录手机号
|
|
|
*/
|
|
|
public function sendMobileCode(){
|
|
|
if($this->request->isPost()){
|
|
|
// Db::startTrans();
|
|
|
$mobile = $this->request->post('mobile','');
|
|
|
$mc = new MobileCodeModel();
|
|
|
$search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
|
|
|
if (!preg_match($search,$mobile)) {
|
|
|
$this->error('手机号格式有误');
|
|
|
}
|
|
|
$mobile_code = rand(100000, 999999);
|
|
|
$content = "【恒瑞消防】您的验证码为".$mobile_code.",如非本人操作请忽略本短信!";
|
|
|
$info = $mc->where([
|
|
|
'mobile' => $mobile,
|
|
|
'create_date' => date('Y-m-d')
|
|
|
])->find();
|
|
|
if($info){
|
|
|
if(time() < $info['create_time']+60 && $info['is_use'] == 0){
|
|
|
$this->error('不能频繁发送验证码');
|
|
|
}
|
|
|
if($info['count'] > 10){
|
|
|
$this->error('今天发送验证码的次数已达到了上限');
|
|
|
}
|
|
|
$res = $mc->where('id',$info['id'])->data([
|
|
|
'mobile' => $mobile,
|
|
|
'mobile_code' => $mobile_code,
|
|
|
'is_use' => 0,
|
|
|
'expire_time' => time()+300,
|
|
|
'count' => $info['count'] +1
|
|
|
])->update();
|
|
|
}else{
|
|
|
$res = $mc->insert([
|
|
|
'mobile' => $mobile,
|
|
|
'mobile_code' => $mobile_code,
|
|
|
'is_use' => 0,
|
|
|
'expire_time' => time()+300,
|
|
|
'count' => 1,
|
|
|
'create_time' => time(),
|
|
|
'create_date' => date('Y-m-d')
|
|
|
]);
|
|
|
}
|
|
|
if($res) {
|
|
|
//发送验证码
|
|
|
$this->sendCode($mobile, $content);
|
|
|
$this->success('验证码发送成功,请注意查收!');
|
|
|
}
|
|
|
}else{
|
|
|
$this->error('请求方式错误!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//发送手机验证码
|
|
|
private function sendCode($mobile,$content){
|
|
|
date_default_timezone_set('PRC');//设置时区
|
|
|
$url = "http://www.ztsms.cn/sendNSms.do";//提交地址
|
|
|
$username = 'zhenweishoujizhan';//用户名
|
|
|
$password = 'Cxz307312';//原密码
|
|
|
$data = array(
|
|
|
'content' => $content,//短信内容
|
|
|
'mobile' => $mobile,//手机号码
|
|
|
'productid' => '676767',//产品id
|
|
|
'xh' => ''//小号
|
|
|
);
|
|
|
$isTranscoding = false;
|
|
|
$data['content'] = $isTranscoding === true ? mb_convert_encoding($data['content'], "UTF-8") : $data['content'];
|
|
|
$data['username']=$username;
|
|
|
$data['tkey'] = date('YmdHis');
|
|
|
$data['password'] = md5(md5($password) . $data['tkey']);
|
|
|
$curl = curl_init();// 启动一个CURL会话
|
|
|
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
|
|
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
|
|
|
$result = curl_exec($curl); // 执行操作
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
//验证验证码是否正确
|
|
|
private function validateMobileCode($post){
|
|
|
$mc = new MobileCodeModel();
|
|
|
$res_find = $mc->where(['mobile' => $post['mobile'], 'mobile_code' => $post['mobile_code'], 'is_use' => 0, 'create_date' => date('Y-m-d'),])
|
|
|
->where('expire_time','gt',time())->find();
|
|
|
if($res_find){
|
|
|
$res_update = $mc->where('id',$res_find['id'])->setField('is_use',1);
|
|
|
if($res_update){
|
|
|
return true;
|
|
|
}
|
|
|
}else{
|
|
|
$this->error('验证未通过');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 退出登录
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/logout
|
|
|
* @method GET
|
|
|
* @header name:token type:string require:1 default: other: desc:header
|
|
|
*/
|
|
|
public function logout(){
|
|
|
if($this->request->isGet()){
|
|
|
$userId = $this->getUserId();
|
|
|
Db::name('user_token')
|
|
|
->where(['token' => $this->token, 'user_id' => $userId])
|
|
|
->update(['token' => '']);
|
|
|
$this->success("退出成功!");
|
|
|
}else{
|
|
|
$this->error('请求方式错误!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 获取个人信息
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/info
|
|
|
* @method GET
|
|
|
* @header name:token type:string require:1 default: other: desc:header
|
|
|
* @return id:用户id
|
|
|
* @return user_login:姓名
|
|
|
* @return identity:身份 1:甲方员工,2:乙方员工 3:甲方领导,4:乙方领导 5:甲方总领导
|
|
|
* @return avatar:图片
|
|
|
* @return is_update:姓名是否可以修改 0:允许,1:禁止
|
|
|
* @return is_work:上班状态 1:上班,2:下班
|
|
|
*/
|
|
|
public function info(){
|
|
|
if($this->request->isGet()){
|
|
|
$info = $this->user;
|
|
|
$info1 = $this->getIndexData();
|
|
|
$info['identity'] = $info1['identity'];
|
|
|
$info['company_name'] = isset($info1['company_name'])&&!empty($info1['company_name'])?$info1['company_name']:'';
|
|
|
$this->success('成功',$info);
|
|
|
}else{
|
|
|
$this->error('请求方式错误!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//获取身份有关信息(identity: :1:甲方员工,2:乙方员工,3:甲方领导,4:乙方领导,5:甲方总领导)
|
|
|
public function getIndexData(){
|
|
|
//查询用户所属公司
|
|
|
$identity = $this->user['identity'];
|
|
|
|
|
|
if($identity == 0){
|
|
|
//员工
|
|
|
//找到公司名称,甲方或者乙方企业
|
|
|
$result = [];
|
|
|
|
|
|
$where = ['u_s_id'=>['like','%,'.$this->userId.',%']];
|
|
|
$field = 'id c_id,company_name,pid';
|
|
|
$res = $this->getCompany($where,$field);
|
|
|
if($res){
|
|
|
$result['company_name'] = $res['company_name'];
|
|
|
//查找企业拥有的服务
|
|
|
if($res['pid'] == 0){
|
|
|
//甲方企业
|
|
|
$result['identity'] = config('site.a_staff');
|
|
|
}else{
|
|
|
//乙方企业
|
|
|
$result['identity'] = config('site.b_staff');
|
|
|
}
|
|
|
}
|
|
|
}else if($identity == 1){
|
|
|
//领导
|
|
|
$where = ['u_l_id' => $this->userId];
|
|
|
$field = 'id c_id,company_name,pid';
|
|
|
$res = $this->getCompany($where,$field);
|
|
|
$result['company_name'] = $res['company_name'];
|
|
|
if($res['pid'] == 0){
|
|
|
//甲方
|
|
|
$result['identity'] = config('site.a_leader');
|
|
|
}else{
|
|
|
//乙方
|
|
|
$result['identity'] = config('site.b_leader');
|
|
|
}
|
|
|
}else{
|
|
|
//总领导
|
|
|
$result['identity'] = config('site.a_leaders');
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
//获取企业相关信息
|
|
|
public function getCompany($where,$field){
|
|
|
$res = Db::name('company')
|
|
|
->where($where)
|
|
|
->field($field)
|
|
|
->order('id desc')
|
|
|
->find();
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 上下班
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/work
|
|
|
* @method GET
|
|
|
* @header name:token type:string require:1 default: other: desc:header
|
|
|
* @param name:is_work type:int require:1 default: other: desc:上下班状态(下班:2,上班:1)
|
|
|
*/
|
|
|
public function work(){
|
|
|
if($this->request->isGet()){
|
|
|
$data = $this->request->get();
|
|
|
$userModel = new UserModel();
|
|
|
$rule = config('site.is_work');
|
|
|
$validate = new Validate($rule['rule'],$rule['msg']);
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
$res = $userModel->where('id',$this->userId)->update($data);
|
|
|
if($res){
|
|
|
$this->success('成功');
|
|
|
}else{
|
|
|
$this->error('失败');
|
|
|
}
|
|
|
}else{
|
|
|
$this->error('请求方式错误!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 修改个人资料
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/updateInfo
|
|
|
* @method POST
|
|
|
* @header name:token type:string require:1 default: other: desc:header
|
|
|
* @param name:user_login type:string require:1 default: other: desc:姓名
|
|
|
* @param name:avatar type:string require:1 default: other: desc:照片
|
|
|
*/
|
|
|
public function updateInfo(){
|
|
|
if($this->request->isPost()){
|
|
|
$data = $this->request->post();
|
|
|
$userModel = new UserModel();
|
|
|
$info = $this->user;
|
|
|
if($info['is_update'] == 0){
|
|
|
$data['is_update'] = 1;
|
|
|
}
|
|
|
$res = $userModel->where('id',$this->userId)->update($data);
|
|
|
if($res){
|
|
|
$this->success('修改成功');
|
|
|
}else{
|
|
|
$this->error('修改失败');
|
|
|
}
|
|
|
}else{
|
|
|
$this->error('请求方式错误!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 上传文件
|
|
|
* @description 接口说明
|
|
|
* @author 开发者
|
|
|
* @url /api/admin/person/uploadFile
|
|
|
* @method POST
|
|
|
* @header name:token type:string require:1 default: other: desc:header
|
|
|
* @param name:source[] type:file require:1 default: other: desc:文件
|
|
|
* @return src:文件全路径 多张返回以逗号隔开
|
|
|
*/
|
|
|
public function uploadFile(){
|
|
|
$files = request()->file('source');
|
|
|
if (empty($files)) {
|
|
|
$this->error('未检出文件上传');
|
|
|
}
|
|
|
$countFile = count($files);
|
|
|
if($countFile > 9){
|
|
|
$this->error('最多上传9张图片');
|
|
|
}
|
|
|
$date = date('Ymd',time());
|
|
|
$host = config('site.host');
|
|
|
$images = '';
|
|
|
foreach($files as $file){
|
|
|
//移动到框架应用根目录/public/uploads/ 目录下
|
|
|
//允许文件大小200k
|
|
|
$info = $file->validate(['size'=>20480000,'ext'=>'jpg,png,gif,mp3,wma,wav,mp4,avi,wmv,rm,rmvb,mkv'])->move(ROOT_PATH . 'public' . DS . 'uploads');
|
|
|
if($info){
|
|
|
$images .= $host.'/uploads/'.$date.'/'.$info->getFilename().',';
|
|
|
}else{
|
|
|
// 上传失败获取错误信息
|
|
|
$this->error($file->getError());
|
|
|
}
|
|
|
}
|
|
|
$this->success('上传成功',['src'=>rtrim($images,',')]);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|