166 lines
4.6 KiB
PHP
166 lines
4.6 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: lock
|
|
* Date: 2018/11/4
|
|
* Time: 下午1:39
|
|
* 初始化
|
|
*/
|
|
|
|
namespace app\index\controller;
|
|
|
|
|
|
|
|
use app\common\classLibrary\ClWechat;
|
|
use app\index\validate\customValidate;
|
|
use think\Controller;
|
|
use think\Log;
|
|
use think\Request;
|
|
|
|
class Common extends Controller
|
|
{
|
|
public static $error_code_name = 'error_code';
|
|
|
|
public static $error_message = 'error_msg';
|
|
|
|
public static $result = array('error_code'=>0);
|
|
|
|
public static $data_name = 'data';
|
|
|
|
public static $insert_id = 'insert_id';
|
|
|
|
public static $forbid_return_where_arr = array('page','pageSize');
|
|
|
|
const DEFAULT_KEY = 'access_token_name';
|
|
|
|
const SELF_DEFAULT_KEY = '0e15ad1bdc3fb96a63a2c28c45625e6a';
|
|
|
|
const SELF_SECRET_SECOND = 10;
|
|
|
|
/**
|
|
* check access_token
|
|
*/
|
|
public function _initialize()
|
|
{
|
|
header('Access-Control-Allow-Origin:*');
|
|
header('Access-Control-Allow-Methods:POST');
|
|
header('Access-Control-Allow-Headers:x-requested-with,content-type');
|
|
$access_token = Request::instance()->post('access_token');
|
|
$debug = 0;
|
|
$debug = Request::instance()->post('debug');
|
|
if(!$debug)
|
|
{
|
|
$now_time = time();
|
|
$all_secret_arr = array();
|
|
$tmp_time_arr = array();
|
|
$md5_arr = array();
|
|
for ($i=-2; $i<= self::SELF_SECRET_SECOND;$i++)
|
|
{
|
|
$tmp_time = $now_time + $i;
|
|
$tmp_time_arr[] = $tmp_time;
|
|
$total_str = md5(self::DEFAULT_KEY.self::SELF_DEFAULT_KEY.$tmp_time);
|
|
$md5_arr[] = $total_str;
|
|
$all_secret_arr[] = $total_str;
|
|
}
|
|
if(!in_array($access_token,$all_secret_arr))
|
|
{
|
|
self::$result[self::$error_code_name] = 1003;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $field_arr
|
|
* array(array('field_name'=>'user_name','type' => 'email','require' => true))
|
|
* @return array
|
|
*/
|
|
public function checkHave($field_arr)
|
|
{
|
|
$return_result = array(self::$error_code_name => 0);
|
|
$rule = array();
|
|
$field = array();
|
|
$data = array();
|
|
$rule_data = array();
|
|
$post_value_arr = Request::instance()->post();
|
|
foreach ($field_arr as $field_val)
|
|
{
|
|
$post_value = '';
|
|
if (array_key_exists($field_val['field_name'],$post_value_arr))
|
|
{
|
|
$post_value = $post_value_arr[$field_val['field_name']];
|
|
if(trim($post_value) == "") {
|
|
$data[$field_val['field_name']] = $post_value;
|
|
}else
|
|
{
|
|
$data[$field_val['field_name']] = $post_value;
|
|
}
|
|
}
|
|
if (trim($field_val['rule']) != '') {
|
|
$rule_data[$field_val['field_name']] = $post_value;
|
|
$rule[$field_val['field_name']] = $field_val['rule'];
|
|
$field[$field_val['field_name']] = $field_val['field_name'];
|
|
}
|
|
}
|
|
$validate = new customValidate($rule,[],$field);
|
|
$result = $validate->check($rule_data);
|
|
if(!$result)
|
|
{
|
|
$return_result[self::$error_code_name] = '1';
|
|
$return_result[self::$error_message] = $validate->getError();
|
|
}else{
|
|
$return_result[self::$data_name] = $data;
|
|
}
|
|
return $return_result;
|
|
}
|
|
|
|
/**
|
|
* deleteImage
|
|
* @param $url_path
|
|
* @return bool
|
|
*/
|
|
public static function deleteImage($url_path)
|
|
{
|
|
$root_path = ROOT_PATH . 'public' . DS . 'upload';
|
|
$tmp_path = $root_path.DS.$url_path;
|
|
$result = true;
|
|
if(file_exists($tmp_path))
|
|
{
|
|
if (!unlink($tmp_path))
|
|
{
|
|
$result = false;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @param $field_arr
|
|
* @param $data
|
|
* @param int $type
|
|
* @return array
|
|
*/
|
|
public static function returnWhere($field_arr,$data,$type = 1)
|
|
{
|
|
$where = array();
|
|
$new_field_arr = array();
|
|
if($type == 1)
|
|
{
|
|
$new_field_arr = array();
|
|
foreach ($field_arr as $each_v)
|
|
{
|
|
$new_field_arr[] = $each_v['field_name'];
|
|
}
|
|
}else{
|
|
$new_field_arr = $field_arr;
|
|
}
|
|
foreach ($new_field_arr as $field_each_v)
|
|
{
|
|
if (array_key_exists($field_each_v,$data) && !in_array($field_each_v,self::$forbid_return_where_arr))
|
|
{
|
|
$where[$field_each_v] = $data[$field_each_v];
|
|
}
|
|
}
|
|
return $where;
|
|
}
|
|
|
|
} |