Files
liche/home/controllers/h5/market/sylive/Login.php
T
2022-09-29 14:30:16 +08:00

99 lines
3.8 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller{
const WX_SESSION = "market_wx_info";
protected $secret = "market_sylive_h5";
public function __construct(){
parent::__construct();
session_start();
$this->load->helper('cookie');
$this->load->model('market/market_sylive_user_model', 'user_model');
}
public function index(){
if($this->input->is_ajax_request()){
$redis = &load_cache('redis');
$code = $this->input->post('code');
$mobile = $this->input->post('mobile');
$key = "sylive_login_code_".$mobile;
if(!$code || $code!=$redis->get($key)){
$this->show_json('',400,'请输入正确的验证码');
}
$user=$this->user_model->get(array('mobile' => $mobile,'organizationId>'=>0));
if($user['status']){
$this->show_json('',400,'用户已禁用');
}
$this->bind_openid($user);
$session = ['uid' => $user['userId'],'org_id'=>$user['organizationId']];
$ukey = liche_authcode(json_encode($session, JSON_UNESCAPED_UNICODE), 'ENCODE', $this->secret);
set_cookie("ukey", $ukey, 86400 * 30);
$this->show_json('',200,'登录成功');
}else{
$this->load->view('h5/market/sylive/login');
}
}
//绑定微信openid
private function bind_openid($user){
$wx_info = $_SESSION[self::WX_SESSION];
if(!$wx_info['openid']){
return ['code' => 0,'msg' => '不存在公众号信息,无需绑定'];
}
if($user['openid']){
return ['code' => 0,'msg' => '该用户已绑定公众号'];
}
$is_bind = $this->user_model->count(['openid'=>$wx_info['openid'],'organizationId>'=>0,'status>='=>0]); //是否绑定
if($is_bind){
return ['code' => 0,'msg' => '该公众号已存在绑定账号'];
}
$update = [
'openid' => $wx_info['openid']
];
$wx_info['nickname'] && $update['nickname'] = strval($wx_info['nickname']);
$wx_info['headimgurl'] && $update['headimg'] = strval($wx_info['headimgurl']);
$wx_info['unionid'] && $update['unionid'] = $wx_info['unionid'];
$this->user_model->update($update,['userId'=>$user['userId']]);
//删除普通账户
$this->user_model->update(['status'=>-1],['openid'=>$wx_info['openid'],'organizationId'=>0,'status>='=>0]);
return ['code' => 1,'msg' => '绑定成功'];
}
//获取验证码
public function get_code(){
$mobile = $this->input->post('mobile');
if(!mobile_valid($mobile)){
$this->show_json('',400,'请输入正确的手机号码');
}
$user=$this->user_model->get(array('mobile' => $mobile, 'status' => 0,'organizationId>'=>0));
if(!$user){
$this->show_json('',400,'用户不存在');
}
if($user['status']){
$this->show_json('',400,'用户已禁用');
}
$redis = &load_cache('redis');
$key = "sylive_login_code_".$mobile;
if(!$code = $redis->get($key)){
$this->load->helper('string');
$code = random_string('numeric', 4);
$redis->save($key, $code, 60*5);
}
send_sms($mobile, $code);
$this->show_json('',200, '验证码已发送');
}
private function show_json($data, $code = 200, $msg = 'success', $url = ''){
if(!isset($data['code'])){
$data = array('data' => $data, 'code' => $code, 'msg' => $msg, 'url' => $url);
}
exit(json_encode($data));
}
public function test(){
$this->load->library('market/sylive_entity');
$oid = 1;
$req = $this->sylive_entity->get_level($oid);
print_r($req);
}
}