This commit is contained in:
lcc
2024-06-03 20:40:14 +08:00
parent c98ef3635e
commit a17aa85a98
2 changed files with 105 additions and 88 deletions
+101 -84
View File
@@ -1,11 +1,13 @@
<?php
/**
* Created by PhpStorm.
* User: xuxb
* Date: 2019/12/4
* Time: 19:35
*/
abstract class Wxapp extends HD_Controller{
abstract class Wxapp extends HD_Controller
{
const STATUS_NOR = 1;//状态正常
const STATUS_USED = 1;//被使用
const STATUS_DEL = -1;//删除
@@ -49,7 +51,8 @@ abstract class Wxapp extends HD_Controller{
* @param $inputs (请求参数)
* @param string $app_key (应用key)
*/
function __construct($inputs, $app_key){
function __construct($inputs, $app_key)
{
parent::__construct();
$this->app_key = $app_key;
@@ -92,7 +95,7 @@ abstract class Wxapp extends HD_Controller{
$this->app_redis = &load_cache('redis');
//加载model
if($this->model_app_user){
if ($this->model_app_user) {
$this->load->model($this->model_app_user, 'app_user_model');
}
@@ -105,8 +108,9 @@ abstract class Wxapp extends HD_Controller{
* @param string $key
* @return array|mixed
*/
function input_param($key = ''){
if($key){
function input_param($key = '')
{
if ($key) {
return $this->inputs[$key];
}
@@ -119,64 +123,65 @@ abstract class Wxapp extends HD_Controller{
* @return mixed
* @throws Exception
*/
function __call($name, $arguments){
function __call($name, $arguments)
{
$version = $arguments[0];
$sversion = $arguments[1];
//一些接口的分支需要校验,如biz里的rec_list,是需要登录的
$pre_call = 'call__';
if(0 === strpos($name, $pre_call)){
if (0 === strpos($name, $pre_call)) {
$name = substr($name, strlen($pre_call));
}
$method = $name;
if($version){//版本号的方法存在用版本号的,否则继续用默认的方法
if(method_exists($this, $method . '__' . $version)){
if ($version) {//版本号的方法存在用版本号的,否则继续用默认的方法
if (method_exists($this, $method . '__' . $version)) {
$method .= '__' . $version;
}
}
if($sversion){//小版本,设置某个方法的版本
if ($sversion) {//小版本,设置某个方法的版本
$sversion = str_replace('.', '_', $sversion);
if(method_exists($this, $method . '__' . $sversion)){
if (method_exists($this, $method . '__' . $sversion)) {
$method .= '__' . $sversion;
}
}
if(!method_exists($this, $method)){
debug_log("[fail]". __FUNCTION__ . ": request not allow; method:{$method}", $this->log_file);
if (!method_exists($this, $method)) {
debug_log("[fail]" . __FUNCTION__ . ": request not allow; method:{$method}", $this->log_file);
throw new Exception('非法请求', API_CODE_NONE);
}
//某个方法或者整个ct在白名单里无需校验
$session = $this->session;
if(!in_array($name, $this->login_white) && 'all' != $this->login_white){
if(!$session){
if (!in_array($name, $this->login_white) && 'all' != $this->login_white) {
if (!$session) {
throw new Exception('还未登录', API_CODE_LOGOUT);
}
$user = $this->u_entity->get(array('id' => $session['uid']));
if(!$user || -1 == $user['status']){
if (!$user || -1 == $user['status']) {
$this->logout($this->ukey);
debug_log("[error]# user is delete, sql=". $this->u_entity->last_query(), __FUNCTION__, $this->log_dir);
debug_log("[error]# user is delete, sql=" . $this->u_entity->last_query(), __FUNCTION__, $this->log_dir);
throw new Exception('登录超时', API_CODE_LOGOUT);
}
// 校验用户状态
if(in_array($name, $this->check_status) || 'all' == $this->check_status){
if(self::STATUS_NOR != $session['status']){
if (in_array($name, $this->check_status) || 'all' == $this->check_status) {
if (self::STATUS_NOR != $session['status']) {
throw new Exception('用户被禁用', API_CODE_FORB);
}
}
//是否绑定手机号
if(in_array($name, $this->check_mobile) || 'all' == $this->check_mobile){
if(!$session['mobile'] || !mobile_valid($session['mobile'])){
if (in_array($name, $this->check_mobile) || 'all' == $this->check_mobile) {
if (!$session['mobile'] || !mobile_valid($session['mobile'])) {
throw new Exception('请绑定正确的手机号', API_CODE_FORB);
}
}
// 校验用户头像
if(in_array($name, $this->check_headimg) || 'all' == $this->check_headimg){
if(!$session['headimg']){
if (in_array($name, $this->check_headimg) || 'all' == $this->check_headimg) {
if (!$session['headimg']) {
throw new Exception('获取头像信息失败', API_CODE_FORB);
}
}
@@ -199,12 +204,13 @@ abstract class Wxapp extends HD_Controller{
* @param $data
* @return string
*/
protected function refresh_login($data){
protected function refresh_login($data)
{
$redis = $this->app_redis;
$ukey = md5("{$data['uid']}_{$data['session_key']}");
$redis->save($this->redis_login.$ukey, json_encode($data, JSON_UNESCAPED_UNICODE), 30 * 24 * 3600);
$redis->save($this->redis_login . $ukey, json_encode($data, JSON_UNESCAPED_UNICODE), 30 * 24 * 3600);
$this->ukey = $ukey;
return $ukey;
@@ -214,12 +220,13 @@ abstract class Wxapp extends HD_Controller{
* 删除登录记录
* @param $ukey
*/
protected function logout($ukey = ''){
protected function logout($ukey = '')
{
!$ukey && $ukey = $this->input_param('ukey');
if($ukey){
if ($ukey) {
$redis = $this->app_redis;
$redis->delete($this->redis_login.$ukey);
$redis->delete($this->redis_login . $ukey);
}
}
@@ -227,9 +234,10 @@ abstract class Wxapp extends HD_Controller{
* 校验用户是否黑名单
* @return bool
*/
protected function is_black(){
protected function is_black()
{
$mobile = $this->session['mobile'];
if(!$mobile){
if (!$mobile) {
return false;
}
@@ -240,29 +248,30 @@ abstract class Wxapp extends HD_Controller{
*
* @return array|mixed
*/
private function fetch_session(){
private function fetch_session()
{
$ukey = $this->input_param('ukey');
$this->ukey = $ukey;
if(!$ukey){
if (!$ukey) {
return array();
}
$redis = $this->app_redis;
//data:{"uid":"用户ID", "session_key":"微信session_key"}
$data = json_decode($redis->get($this->redis_login.$ukey), true);
$data = json_decode($redis->get($this->redis_login . $ukey), true);
if($data){
if ($data) {
$session = $data;
$source_uid = $uid = $session['uid'];
$user = $this->u_entity->get(array('id' => $uid));
if($user){
if ($user) {
//判断是否超级管理员批马甲
$json = $user['jsondata'] ? json_decode($user['jsondata'], true) : array();
if($json['majia']){//披上马甲
if ($json['majia']) {//披上马甲
$muid = $json['majia']['uid'];
$row = $this->u_entity->get(array('id' => $muid));
if($row){
if ($row) {
$uid = $muid;
$session['is_majia'] = 1;
$session['source_uid'] = $source_uid;
@@ -271,19 +280,19 @@ abstract class Wxapp extends HD_Controller{
}
}
// 角色切换处理
if ($session['group_id_type']){
if ($session['group_id_type']) {
$user['group_id'] = $user['group_id1'];
$user['biz_id'] = $user['biz_id1'];
$user['city_id'] = $user['city_id1'];
}
//设置默认城市,取biz_id对应城市
if (strlen($user['biz_id']) > strlen(str_replace(',', '', $user['biz_id']))){
if (strlen($user['biz_id']) > strlen(str_replace(',', '', $user['biz_id']))) {
$this->load->model("biz/biz_model");
$biz = $this->biz_model->get(['id' => intval($user['biz_id']), 'status' => 1], 'city_id');
$user['city_id'] = $biz && $biz['city_id'] ? $biz['city_id'] : 0;
}
}
if($user){
if ($user) {
$session = array_merge($session, $user);
$this->session = $session;
//更新登录有效时间
@@ -302,25 +311,26 @@ abstract class Wxapp extends HD_Controller{
* @param int $type "-1删除 0重置 1新增"
* @return mixed
*/
protected function set_session($arr = array(), $type=0){
protected function set_session($arr = array(), $type = 0)
{
$redis = $this->app_redis;
$ukey = $this->ukey;
$data = json_decode($redis->get($this->redis_login.$ukey), true);
if(-1 == $type){//删除
foreach($arr as $k){
$data = json_decode($redis->get($this->redis_login . $ukey), true);
if (-1 == $type) {//删除
foreach ($arr as $k) {
unset($data[$k]);
}
} elseif(0 == $type){//重置
} elseif (0 == $type) {//重置
$data = $arr;
} elseif(1 == $type){//新增
foreach($arr as $k => $v){
} elseif (1 == $type) {//新增
foreach ($arr as $k => $v) {
$data[$k] = $v;
}
}
$redis->save($this->redis_login.$ukey, json_encode($data, JSON_UNESCAPED_UNICODE), 30 * 24 * 3600);
$redis->save($this->redis_login . $ukey, json_encode($data, JSON_UNESCAPED_UNICODE), 30 * 24 * 3600);
$this->ukey = $ukey;
return $ukey;
@@ -331,12 +341,13 @@ abstract class Wxapp extends HD_Controller{
* @param string $key (name, logo, biz_cate)
* @return mixed
*/
protected function app_config($key = ''){
protected function app_config($key = '')
{
$this->load->model('app/app_model');
$this->load->model("app/appusual/app_config_model");
if($this->app){
$app = $this->app;
if ($this->app) {
$app = $this->app;
} else {
$where = array('id' => $this->app_id);
$app = $this->app_model->get($where);
@@ -345,8 +356,8 @@ abstract class Wxapp extends HD_Controller{
$where = array('app_id' => $this->app_id);
$select = "k,v";
$map_config = $this->app_config_model->map('k', 'v', $where, '', 0, 0, $select);
if($map_config){
foreach($map_config as $k => $v){
if ($map_config) {
foreach ($map_config as $k => $v) {
$v && $json[$k] = json_decode($v, true);
}
}
@@ -354,11 +365,11 @@ abstract class Wxapp extends HD_Controller{
$this->app = $app;
}
if(!$key){
if (!$key) {
return $app;
}elseif($app[$key]){
} elseif ($app[$key]) {
return $app[$key];
}else{
} else {
$json = $app['json'];
return $json[$key];
}
@@ -370,14 +381,15 @@ abstract class Wxapp extends HD_Controller{
* @param $app_id
* @return null
*/
protected function get_config($app_key = '', $app_id = ''){
protected function get_config($app_key = '', $app_id = '')
{
$this->config->load('app', true, true);
$configs = $this->config->item('app');
if($app_key){
if ($app_key) {
return $configs[$app_key];
} elseif($app_id) {
foreach($configs as $k => $v){
if($app_id == $v['app_id']){
} elseif ($app_id) {
foreach ($configs as $k => $v) {
if ($app_id == $v['app_id']) {
return $v;
}
}
@@ -389,9 +401,10 @@ abstract class Wxapp extends HD_Controller{
/**
* @return string
*/
protected function cityid(){
protected function cityid()
{
if(!$this->city_id){
if (!$this->city_id) {
$city_id = $this->app_config('city_id');
!$city_id && $city_id = '350200';
$this->city_id = $city_id;
@@ -404,7 +417,8 @@ abstract class Wxapp extends HD_Controller{
* 获取用户当前访问的城市ID
* @return mixed
*/
protected function ucityid(){
protected function ucityid()
{
$json = $this->session['jsondata'];
!is_array($json) && $json = json_decode($json, true);
return $json['city_id'];
@@ -416,17 +430,18 @@ abstract class Wxapp extends HD_Controller{
* @param string $cityid 取默认城市id
* @return array
*/
protected function config_ucity($k = '',$cityid=''){
if($cityid){
protected function config_ucity($k = '', $cityid = '')
{
if ($cityid) {
$city_id = $this->cityid();
}else{
} else {
$city_id = $this->ucityid();
}
$citys = $this->app_config("citys");
$config_city = $citys[$city_id];
1 == $config_city && $config_city = array();
return $k ? $config_city[$k]:$config_city;
return $k ? $config_city[$k] : $config_city;
}
protected function get_pager()
@@ -443,11 +458,12 @@ abstract class Wxapp extends HD_Controller{
* @param $code
* @return mixed|string
*/
protected function wx_session($code){
protected function wx_session($code)
{
$appid = $this->wx_config['appid'];
$secret = $this->wx_config['secret'];
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
debug_log("[info] ". __FUNCTION__ . "微信授权:\n{$url}", $this->log_file);
debug_log("[info] " . __FUNCTION__ . "微信授权:\n{$url}", $this->log_file);
// $ch = curl_init($url);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
// //关闭https验证
@@ -456,7 +472,7 @@ abstract class Wxapp extends HD_Controller{
// $res = curl_exec($ch);
$res = file_get_contents($url);
//存日志
debug_log("[info] ". __FUNCTION__ . "res={$res}", $this->log_file);
debug_log("[info] " . __FUNCTION__ . "res={$res}", $this->log_file);
$ret = json_decode($res, true);
// {
// "session_key": "会话密钥",
@@ -466,8 +482,8 @@ abstract class Wxapp extends HD_Controller{
// "errmsg": "错误信息"
// }
if(!$ret['session_key']){
debug_log("[fail] ". __FUNCTION__ . ": session_key is null", $this->log_file);
if (!$ret['session_key']) {
debug_log("[fail] " . __FUNCTION__ . ": session_key is null", $this->log_file);
}
return $ret;
@@ -479,20 +495,19 @@ abstract class Wxapp extends HD_Controller{
* @param $iv
* @return array|mixed|string
*/
protected function wx_data($encrypted, $iv){
require_once COMMPATH."third_party/WeChat/wxBizDataCrypt.php";
protected function wx_data($encrypted, $iv)
{
require_once COMMPATH . "third_party/WeChat/wxBizDataCrypt.php";
$pc = new WXBizDataCrypt($this->wx_config['appid'], $this->session['session_key']);
$wx_data = '';
$errCode = $pc->decryptData($encrypted, $iv, $wx_data);
debug_log("[info] ". __FUNCTION__ . ":code={$errCode}; wxdata:{$wx_data}", $this->log_file);
debug_log("[info] " . __FUNCTION__ . ":code={$errCode}; wxdata:{$wx_data}", $this->log_file);
if ($errCode == 0) {
$wx_data = json_decode($wx_data, true);
return $wx_data;
}
debug_log("[warning] ". __FUNCTION__ . ":appid=" . $this->wx_config['appid'] . "; session_key=" . $this->session['session_key'] . "; encrypted={$encrypted}; iv={$iv}; wxdata:{$wx_data}", $this->log_file);
debug_log("[warning] " . __FUNCTION__ . ":appid=" . $this->wx_config['appid'] . "; session_key=" . $this->session['session_key'] . "; encrypted={$encrypted}; iv={$iv}; wxdata:{$wx_data}", $this->log_file);
return array();
}
@@ -501,12 +516,13 @@ abstract class Wxapp extends HD_Controller{
* @param $inputs
* @return array
*/
private function set_input($inputs){
if(!$inputs){
private function set_input($inputs)
{
if (!$inputs) {
return array();
}
foreach($inputs as $k => $v){
if('undefined' === $v){//前端空参数过滤
foreach ($inputs as $k => $v) {
if ('undefined' === $v) {//前端空参数过滤
$inputs[$k] = '';
}
}
@@ -517,7 +533,8 @@ abstract class Wxapp extends HD_Controller{
}
//获取当前门店id
protected function get_biz_id(){
protected function get_biz_id()
{
return $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
}
}
+4 -4
View File
@@ -107,7 +107,7 @@ class User extends Wxapp
throw new Exception('登录失败', API_CODE_FAIL);
}
$open_id = $session['openid'];
$user = $this->app_user_model->get(['openid' => $open_id]);
$user = $this->app_user_model->get(['openid' => $open_id, 'group_id' => 0]);
if (!$user) {
$userData = [
'openid' => $open_id,
@@ -328,7 +328,7 @@ class User extends Wxapp
['title' => '战败申请(人)', 'icon' => 'icon-statistics-custom-5', 'total' => $defeat_count, 'page' => '/pages/customer/optDefeat/index'],
];
$where = ['status' => 0,'biz_id' => $biz_id];
$where = ['status' => 0, 'biz_id' => $biz_id];
$group_id == 1 && $where['sale_id'] = $uid;
$fq_total = $this->orders_model->count($where);
$where = [
@@ -600,9 +600,9 @@ class User extends Wxapp
{
$uid = $this->session['uid'];
$user = $this->app_user_model->get(array('id' => $uid));
$user = $this->app_user_model->get(array('id' => $uid, 'group_id' => 0));
$data = [
'mobile' => $user['mobile'],
'mobile' => $user['mobile'] ? $user['mobile'] : '',
];
return $data;
}