Files
2022-01-28 16:25:41 +08:00

203 lines
5.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
defined('APPPATH') OR exit('No direct script access allowed');
ini_set('display_errors','On');
error_reporting(E_ERROR);
/**
* Created by PhpStorm.
* User: xuxb
* Date: 2020/3/10
* Time: 17:13
*/
abstract class Wxapp extends CI_Controller{
protected $log_file;//日志文件
protected $log_dir;
protected $app_id;//应用idhd_app.id
protected $request;
protected $lc_redis;
protected $env;//网络环境 d开发, t测试, p生产
protected $host;
protected $api_host;
protected $redis_mcode;//手机验证码
protected $white_mobile;//手机验证白名单
protected $white_appid;//appid校验白名单
private $inputs;
function __construct($set_app_id = 0){
parent::__construct();
//加载输入参数
$this->input_param();
//app_id是必需的
$this->app_id = $app_id = $set_app_id ? $set_app_id : $this->input_param('app_id');
$this->data['app_id'] = $app_id;
$this->get_env();
$r = &load_cache('redis');
$this->lc_redis = $r;
//日志文件
$class_name = lcfirst(get_class($this));//调用类的名称,子类或者当前类名称
$this->log_file = "h5_wxapp{$app_id}_{$class_name}.log";
$this->log_dir = "h5_wxapp{$app_id}_{$class_name}";
$this->redis_mcode = "wxapp_{$app_id}_mcode_";
$this->white_mobile = array('18063762579');
$this->load->helper('cookie');
//根据app_id重载model
$this->load->rebuild_model($this->app_id);
$this->load->library('hd_exception');
}
/**
* 所有方法请求入口
* @param $method
* @return mixed
*/
function _remap($method){
try{
if('index' == $method){
$method = $this->request;
} else {
$method = $this->request.'_'. $method;
}
if(!in_array($method, $this->white_appid) && !$this->app_id){
debug_log("[error] ". __FUNCTION__ . "# not app_id", $this->log_file);
throw new Hd_exception('请求超时', 403);
}
$data = $this->$method();
if($data['view']){//返回视图
$this->data = array_merge($this->data, $data);
return $this->show_view($data['view'], $data['alone']);
} else {//返回json数据
return $this->show_json($data);
}
} catch(Hd_exception $e){//处理异常
$code = $e->getCode();
$msg = $e->getMessage();
if($this->input->is_ajax_request()){
return $this->show_json(array(), $code, $msg);
} else {
$this->data = array('heading' => 'Warning', 'message' => $msg);
return $this->show_view('errors/html/error_404');
}
}
}
/**
* 获取参数(只支持application/json格式)
* @param string $key
* @return array|mixed
*/
protected function input_param($key = ''){
if($key){
return $this->inputs[$key];
}
$request = $this->input->method();
switch($request){
case 'post':
case 'put':
case 'delete':
$input = json_decode(file_get_contents('php://input'), true);
break;
default:
$input = $this->input->get();
}
$this->request = $request;
$this->inputs = $input;
return $this->inputs;
}
/**
* @param $view
* @param $alone "独立页面"
*/
protected function show_view($view, $alone = false){
if($alone){
$this->load->view($view, $this->data);
} else{
$this->load->view('h5/header',$this->data);
$this->load->view($view);
$this->load->view('h5/footer');
}
}
/**
* @param $data
* @param int $code
* @param string $msg
* @param string $url
*/
protected 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));
}
/**
* @param $ukey
* @return array|mixed
*/
protected function api_session($ukey){
$redis = &load_cache('redis');
$app_key = "";
switch($this->app_id){
case 1:
$app_key = "sbcard";
break;
case 2:
$app_key= "xcard";
}
if(!$app_key){
return array();
}
$redis_login = "wxapp_{$app_key}_login_";
//data:{"uid":"用户ID", "session_key":"微信session_key"}
$data = $redis->get($redis_login.$ukey);
$session = array();
if($data){
$session = json_decode($data, true);
}
return $session;
}
/**
* 获取环境
*/
private function get_env(){
if (false !== strpos($_SERVER['HTTP_HOST'], 'dev')) { //dev 测试
$this->env = 'd';
$this->host = "https://hd-wxdev.xiaoyu.com";
$this->api_host = "https://hd-api-dev.xiaoyu.com";
} elseif (false !== strpos($_SERVER['HTTP_HOST'], 'test')) {//test 测试
$this->env = 't';
$this->host = "https://www-test.haodian.cn";
$this->api_host = "https://api.test.haodian.cn";
} else { // 正式
$this->env = 'p';
$this->host = "https://www.haodian.cn";
$this->api_host = "https://api.haodian.cn";
}
}
}