~~~
<?php
/**
* Created by PhpStorm.
* Power by Mikkle
* QQ:776329498
* Date: 2017/3/22
* Time: 13:25
*/
namespace app\member\controller;
use app\base\model\Member;
use think\Loader;
use think\Cache;
class Login extends Base
{
protected $config = [
'debug' => true,//是否是在开发中调试模式
'debug_app_key' => '************', //融云开发环境下的key 仅供测试使用
'debug_app_secret' => 'D5Y4lpfcl6', //融云开发环境下的SECRET 仅供测试使用
'app_key' => '', //融云生产环境下的key
'app_secret' => '', //融云生产环境下的SECRET
];
protected $app_key;
protected $app_secret;
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
if ($this->config['debug'] == true){
$this->app_key =$this->config['debug_app_key'];
$this->app_secret=$this->config['debug_app_secret'];
}else{
$this->app_key =$this->config['app_key'];
$this->app_secret=$this->config['app_secret'];
}
}
/**
* 通过用户名密码获取用户信息及融云Token
* Power by Mikkle
* QQ:776329498
* @return \think\response\Json
*/
public function getToken(){
// 获取用户id
$user=$this->getUid();
if ($user['code']!=1001){
return json($user);
}
$user = $user['data'];
$user_info = $user->toArray();
// 获取融云token
$token=json_decode($this->getRongCloudToken($user['id'],$user['userid']));
if (isset($token->code)){
if ($token->code == 200){
if ($user_info['r_token'] != $token->token){
$user_info['r_token'] =$token->token;
$user->update(['r_token'=>$token->token],['id'=>$user_info['id']]);
}
}else{
return json(self::showReturnCodeWithOutData(1031,'网络错误'));
}
}
return json(self::showReturnCode(1001,$user_info,'获取成功'));
}
private function getUid(){
//数据库字段 网页字段转换
$param = [
'userid' => 'userid',
'userpwd' => 'userpwd',
'mobile' => 'mobile',
];
$param_data = $this->buildParam($param);
if (empty($param_data['userid'])&&empty($param_data['mobile'])) return self::showReturnCodeWithOutData(1003);
$check_login = $this->doModelAction($param_data, 'base/Member.login', 'base/Member', 'checkLogin');
if (!isset($check_login['code'])) $this->showReturnCodeWithSaveLog(1111);
if ($check_login['code'] == 1001) {
}
return $check_login;
}
/**
* 获取融云Token
* Power by Mikkle
* QQ:776329498
* @param $user_id
* @param $username
* @param string $logo
* @return mixed
*/
private function getRongCloudToken($user_id,$username,$logo=''){
$logo = empty($logo) ? 'http://www.rongcloud.cn/images/logo.png' : $logo;
include_once( EXTEND_PATH.'/rongcloud/RongCloud.php');
$rong_cloud = new \RongCloud($this->app_key,$this->app_secret);
$result = $rong_cloud->user()->getToken($user_id, $username ,$logo);
return $result;
}
/**
* 发送手机短信验证码
* #User: Mikkle
* #Email:776329498@qq.com
* #Date:
* @param bool|false $mobile
* @return 出错返回1003错误码和错误详情|返回错误码
*/
public function sendRegSms($mobile=false){
// if (!$this->request->isAjax() ) return $this->showReturnCodeWithOutData(1002);
if(!$mobile) $mobile=$this->request->param('mobile');
if (empty($mobile)||!is_numeric($mobile)||strlen($mobile)!=11) return $this->showReturnCodeWithOutData(1003);
//验证手机号码是否存在并且是唯一
if ($this->checkMobileExist($mobile)) return $this->showReturnCodeWithOutData(1004,'此手机已存在,请更换号码或联系管理员');
$data_mobile['mobile']=$mobile;
if ( isset($result_mobile['code']) ) return $result_mobile;
if(!$this->checkRegSms($mobile)) return $this->showReturnCodeWithOutData(1003,'请勿一分钟内重复申请验证码');
$req=$this->sendSmsCode($mobile);
if ($req===true){
return $this->showReturnCodeWithOutData(1001);
}else{
return $this->showReturnCodeWithOutData(1003,$req);
}
}
/**
* 检测手机验证码并获取融云Token
* Power by Mikkle
* QQ:776329498
* @param bool|false $mobile
* @param bool|false $code
* @return 出错返回1003错误码和错误详情|array
*/
public function regAction($mobile=false,$code=false,$userpwd = false){
// if (!$this->request->isAjax() ) return $this->showReturnCodeWithOutData(1002);
//验证手机号码是否存在并且是唯一
if (empty($mobile)) $mobile=$this->request->param('mobile');
if (empty($code)) $code=$this->request->param('code');
if (empty($userpwd)) $userpwd=$this->request->param('userpwd');
$data_mobile=['mobile'=>$mobile,'code'=>$code,'userpwd'=>$userpwd];
$result_mobile=$this->validate($data_mobile,'base/Member.reg');
if ( true!==$result_mobile ) return $this->showReturnCodeWithOutData(1003,$result_mobile);
if ($this->checkMobileExist($mobile)) return $this->showReturnCodeWithOutData(1004,'此手机已存在,请更换号码或联系管理员');
if (!$this->checkRegSms($mobile,$code)){
return $this->showReturnCodeWithOutData(1010,'请求的验证码错误!');
}else{
$reg_data = [
'mobile'=>$mobile,
'userpwd'=>$userpwd,
'r_token'=>null,
];
$model_user = new Member();
$new_user = $model_user ->reg($reg_data);
$user_info = $model_user->toArray();
// 获取token
$token=json_decode($this->getRongCloudToken($user_info['id'],$user_info['userid']));
if (isset($token->code)){
if ($token->code == 200){
if ($user_info['r_token'] != $token->token){
$user_info['r_token'] =$token->token;
$new_user->update(['r_token'=>$token->token],['id'=>$user_info['id']]);
}
}else{
return json(self::showReturnCodeWithOutData(1031,'网络错误'));
}
}
return json(self::showReturnCode(1001,$user_info,'注册成功'));
}
}
/**
* 判断手机是否存在
* Power by Mikkle
* QQ:776329498
* @param string $mobile
* @return bool
*/
private function checkMobileExist($mobile=''){
$model = new Member();
$return = $model->checkMobileExist($mobile);
return $return;
}
/**
* Ucpaas短信接口
* Power by Mikkle
* QQ:776329498
* @param bool|false $mobile
* @return bool
*/
protected function sendSmsCode($mobile = false)
{
if (!$mobile) return false;
$code = rand(10000, 99999);
$templateId ='38484';//验证码模版
$req = controller('base/Ucpaas')->sendSms($mobile,$templateId,$code);
if ($req === true) {
$data_cache = [
'mobile' => $mobile,
'code' => (string)$code,
'times' => time() + 60,
];
$this->setRegSmsCache($data_cache);
return true;
} else {
return $req;
}
}
/**
* 检测手机短信验证码
* #User: Mikkle
* #Email:776329498@qq.com
* #Date:
* @param $mobile
* @param bool|false $code
* @return bool
*/
protected function checkRegSms($mobile, $code = false)
{
if (!$mobile) return false;
if ($code === false) { //判断60秒以内是否重复发送
if (!Cache::has('sms_' . $mobile)) return true;
if (Cache::get('sms_' . $mobile)['times'] > time()) {
return false;
} else {
return true;
}
} else { //判断验证码是否输入正确
if (!Cache::has('sms_' . $mobile)) return false;
if (Cache::get('sms_' . $mobile)['code'] == $code) {
return true;
} else {
return false;
}
}
}
/**
* 设置手机短息验证码缓存
* #User: Mikkle
* #Email:776329498@qq.com
* #Date:
* @param $data_cache
*/
protected function setRegSmsCache($data_cache)
{
Cache::set('sms_' . $data_cache['mobile'], $data_cache, 300);
}
}
~~~
- 序言及更新日志
- 前言一 开发PHP必备的环境(你可以不看)
- LinUX系统ThinkPHP5链接MsSQL数据库的pdo_dblib扩展
- centos7.2挂载硬盘攻略
- Centos系统Redis安装及Redis的PHP扩展安装
- Centos系统增加Swap(系统交换区)的方法
- 前言二 开发PHP软件配置和介绍(你依然可以不看)
- 数据库SQL文件
- 本地Git(版本控制)的搭建
- GIT远程仓库的克隆和推送
- Git常用命令
- PHP面向对象思想实战经验领悟
- PHP面向对象实战----命名空间
- PHP面向对象实战----继承
- 基类实战--底层方法封装
- 基类实战--构造函数实战
- 基类实战--析构函数的使用
- TP5实战开发前篇---控制器(controller)
- 控制器中Request类的使用
- 控制器中基类的使用
- TP5实战开发前篇---模型篇(model)
- TP5实战开发前篇---验证器篇(Validate)
- TP5实战课程入门篇---花拳绣腿
- 模块以及类的文件的建立
- Api开发------单条信息显示
- Api开发---单条信息复杂关联显示
- Api开发---查询信息缓存Cache的应用
- TP5实战技巧---开发思路 引路造桥
- TP5实战技巧---整合基类 化繁为简
- TP5实战课程入门篇---数据操作
- Api开发---数据的添加和修改
- API开发---快速开发API通用接口
- TP5专用微信sdk使用教程
- THINKPHP5微信SDK更新记录及升级指导
- TP5专用SDK 微信参数配置方法
- 微信公众号推送接口对接教程
- 微信推送接口对接示例含扫描登录微信端部分
- TP5专用微信支付SDK使用简介
- TP5专用支付宝支付SDK使用说明
- 使用NW将开发的网站打包成桌面应用
- TP5高阶实战课程 进阶篇概述
- 进阶篇一 实战开发之习惯及要求
- 进阶篇二 实战开发之控制器
- 控制器基类之控制器基类使用方法
- 控制器基类之控制器基类常用方法分享
- 控制器基类之构造函数的使用方法
- 进阶篇三 实战开发之权限控制
- TP5实战源码 --- 全局用户信息验证类Auth
- TP5实战源码 --- 微信Auth实战开发源码
- 进阶篇四 实战开发之模型
- 模型基类之模型基类的用途
- 模型基类之常用数据处理方法
- 模型逻辑层之实战代码(含事务)
- 模型实战开发之模型常用方法
- 模型实战源码 --- 乐观锁的应用
- 模型实战技巧---Model事件功能的使用
- 模型事件实战应用---数据库操作日志
- 进阶篇五 实战开发之缓存(Cache)
- TP5实战源码---应用缓存获取城市信息
- TP5实战源码---应用缓存获取分类详情
- 进阶篇六 TP5类库的封装和使用
- DataEdit快捷操作类库
- ShowCode快捷使用类库
- 阿里大于 短信API接口 TP5专用类库
- DatabaseUpgrade数据库对比及更新类库
- AuthWeb权限类使用说明
- 进阶篇七 服务层的应用
- 服务层源码示例
- 服务层基类源码
- 进阶篇八 应用层Redis数据处理基类
- Redis服务层基类源码
- 进阶篇九 使用Redis类库处理一般的抢购(秒杀)活动示例
- 进阶篇十 某大型项目应用本Redis类源码示例(含事务 乐观锁)
- 进阶篇十一 逻辑层的应用
- 逻辑层基类源码
- 进阶篇 服务层代码示例
- 高阶实战课程 进阶篇持续新增中
- 高阶篇一 TP5命令行之守护任务源码
- TP5实战源码 --- 命令行
- TP5实战源码 --- 通过shell建立PHP守护程序
- 高阶篇二 使用Redis队列发送微信模版消息
- 高阶篇二 之 Worker队列基类源码
- 高阶篇三 TP5实战之Redis缓存应用
- Redis实战源码之Hash专用类库源码
- Redis实战源码之Model类结合
- Redis实战源码之模型Hash基类源码
- Redis实战源码之Hash查询使用技巧
- Redis实战源码之 shell脚本中redis赋值和取值
- 高阶篇四 Swoole的实战应用
- swoole基类代码
- Swoole扩展WebsocketServer专用类
- 基于Swoole的多Room聊天室的程序
- Swoole守护服务shell源码
- 高阶篇五 命令行异步多进程队列类的应用
- tp_worker类源码
- WorkerBase
- WorkerCommand
- WorkerRedis
- Redis类
- CycleWorkBase
- WorkerHookBase异步钩子
- 队列日志SQL
- 高阶篇六 定时执行队列类库以及使用方法
- 定时队列类库源码
- 高阶篇七 异步执行循环队列类库以及使用教程
- CycleWorkBase源码
- 高阶实战课程 进阶篇持续新增中
- Extend便捷类库源码库
- 阿里相关类库
- SendSms--验证码API接口文件
- 权限相关类库目录
- AuthWeb 权限验证类库
- Redis便捷操作类库(20171224更新)
- Redis
- Tools工具类库集
- Curl类库
- DataEdit
- Rand类库
- ShowCode类库
- Upload类库
- 附件集合
- 附件一:微信支付 实战开发源码
- 微信支付类库源代码
- Common_util_pub.php
- DownloadBill_pub.php
- JsApi_pub.php
- NativeCall_pub.php
- NativeLink_pub.php
- OrderQuery_pub.php
- Refund_pub.php
- RefundQuery_pub.php
- SDKRuntimeException.php
- ShortUrl_pub.php
- UnifiedOrder_pub.php
- Wxpay_client_pub.php
- Wxpay_server_pub.php
- WxPayConf_pub.php
- 微信支付回调页面源码
- 附件二 顺丰快递BSP接口实战开发源码
- 顺丰快递BSP接口实战开发源码
- 顺丰BSP基类
- 顺丰BSP基础代码
- 顺丰BSP下单接口
- 顺丰BSP查单接口
- 顺丰BSP确认/取消接口
- 附件三 APP注册登陆接口源码(含融云平台接口)
- 附件四 TP5订单Model(含事务 获取器 修改器等方法)
- 附录五 RSA加密解密
- Rsa文件源码
- 附件六 阿里大于短信接口
- 附件七 AES加解密类
- AES加解密类源码
- 附件八 TP5路由设置源码
- 附件九 TP5 Excel导入导出下载便捷类库
- Excel类库TP5源码
- 附件十 TP5便捷操作Redis类库源码
- TP5源码 Redis操作便捷类库
- 附件十一 TP5源码 上传文件入库类源码
- 上传类Upload源码
- Upload类上传配置文件
- 存储图像文件的数据库SQL文件
- 存储文件的数据库SQL文件
- 附件十二 TP5 图片处理增强类 支持缩略图在线显示
- 附件十三 微信推送消息接口类库源码
- 附件十三 微信推送消息接口类库源码 之 基类
- 附件十四 存储微信昵称的处理方法