<?php // +---------------------------------------------------------------------- // | bronet [ 以客户为中心 以奋斗者为本 ] // +---------------------------------------------------------------------- // | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +--------------------------------------------------------------------- // | Author: Tiger // +---------------------------------------------------------------------- namespace cmf\controller; use app\index\model\UserModel; use EasyWeChat\Foundation\Application; use think\Db; use app\admin\model\ThemeModel; use think\View; class WeChatBaseController extends BaseController { public function _initialize() { // 监听home_init hook('home_init'); parent::_initialize(); $siteInfo = cmf_get_site_info(); View::share('site_info', $siteInfo); } public function _initializeView() { $cmfThemePath = config('cmf_theme_path'); $cmfDefaultTheme = cmf_get_current_theme(); $themePath = "{$cmfThemePath}{$cmfDefaultTheme}"; $root = cmf_get_root(); //使cdn设置生效 $cdnSettings = cmf_get_option('cdn_settings'); if (empty($cdnSettings['cdn_static_root'])) { $viewReplaceStr = [ '__ROOT__' => $root, '__TMPL__' => "{$root}/{$themePath}", '__STATIC__' => "{$root}/static", '__WEB_ROOT__' => $root ]; } else { $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/'); $viewReplaceStr = [ '__ROOT__' => $root, '__TMPL__' => "{$cdnStaticRoot}/{$themePath}", '__STATIC__' => "{$cdnStaticRoot}/static", '__WEB_ROOT__' => $cdnStaticRoot ]; } $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr); config('template.view_base', "{$themePath}/"); config('view_replace_str', $viewReplaceStr); $themeErrorTmpl = "{$themePath}/error.html"; if (file_exists_case($themeErrorTmpl)) { config('dispatch_error_tmpl', $themeErrorTmpl); } $themeSuccessTmpl = "{$themePath}/success.html"; if (file_exists_case($themeSuccessTmpl)) { config('dispatch_success_tmpl', $themeSuccessTmpl); } } /** * 加载模板输出 * @access protected * @param string $template 模板文件名 * @param array $vars 模板输出变量 * @param array $replace 模板替换 * @param array $config 模板参数 * @return mixed */ protected function fetch($template = '', $vars = [], $replace = [], $config = []) { $template = $this->parseTemplate($template); $more = $this->getThemeFileMore($template); $this->assign('theme_vars', $more['vars']); $this->assign('theme_widgets', $more['widgets']); return parent::fetch($template, $vars, $replace, $config); } /** * 自动定位模板文件 * @access private * @param string $template 模板文件规则 * @return string */ private function parseTemplate($template) { // 分析模板文件规则 $request = $this->request; // 获取视图根目录 if (strpos($template, '@')) { // 跨模块调用 list($module, $template) = explode('@', $template); } $viewBase = config('template.view_base'); if ($viewBase) { // 基础视图目录 $module = isset($module) ? $module : $request->module(); $path = $viewBase . ($module ? $module . DS : ''); } else { $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : config('template.view_path'); } $depr = config('template.view_depr'); if (0 !== strpos($template, '/')) { $template = str_replace(['/', ':'], $depr, $template); $controller = cmf_parse_name($request->controller()); if ($controller) { if ('' == $template) { // 如果模板文件名为空 按照默认规则定位 $template = str_replace('.', DS, $controller) . $depr . $request->action(); } elseif (false === strpos($template, $depr)) { $template = str_replace('.', DS, $controller) . $depr . $template; } } } else { $template = str_replace(['/', ':'], $depr, substr($template, 1)); } return $path . ltrim($template, '/') . '.' . ltrim(config('template.view_suffix'), '.'); } /** * 获取模板文件变量 * @param string $file * @param string $theme * @return array */ private function getThemeFileMore($file, $theme = "") { //TODO 增加缓存 $theme = empty($theme) ? cmf_get_current_theme() : $theme; // 调试模式下自动更新模板 if (APP_DEBUG) { $themeModel = new ThemeModel(); $themeModel->updateTheme($theme); } $themePath = config('cmf_theme_path'); $file = str_replace('\\', '/', $file); $file = str_replace('//', '/', $file); $file = str_replace(['.html', '.php', $themePath . $theme . "/"], '', $file); $files = Db::name('theme_file')->field('more')->where(['theme' => $theme])->where(function ($query) use ($file) { $query->where(['is_public' => 1])->whereOr(['file' => $file]); })->select(); $vars = []; $widgets = []; foreach ($files as $file) { $oldMore = json_decode($file['more'], true); if (!empty($oldMore['vars'])) { foreach ($oldMore['vars'] as $varName => $var) { $vars[$varName] = $var['value']; } } if (!empty($oldMore['widgets'])) { foreach ($oldMore['widgets'] as $widgetName => $widget) { $widgetVars = []; if (!empty($widget['vars'])) { foreach ($widget['vars'] as $varName => $var) { $widgetVars[$varName] = $var['value']; } } $widget['vars'] = $widgetVars; $widgets[$widgetName] = $widget; } } } return ['vars' => $vars, 'widgets' => $widgets]; } public function checkUserLogin() { $userId = cmf_get_current_user_id(); if (empty($userId)) { $this->error("用户尚未登录", url("user/login/index")); } } /** * 检查微信用户登录 */ public function checkWeChatUserLogin() { // $user=Db::name('user')->where('id',2)->find(); // cmf_update_current_user($user); $userId = cmf_get_current_user_id(); if (empty($userId)) { $config = [ 'app_id' => config('wechat_config.app_id'), 'secret' => config('wechat_config.secret'), 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => url('user/index/callback'), ], ]; $app = new Application($config); $target_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; session('target_url',$target_url); $app->oauth->redirect()->send(); } } /** * 用户是否微信打开 */ public function isWechat(){ $result = cmf_is_wechat(); if(empty($result)){ $this->error('请在微信浏览器打开!',''); } } /** * 用户是否移动端打开 */ public function isMobile(){ $result = cmf_is_Mobile(); if(empty($result)){ $this->error('请在手机端打开!',''); } } /** * 阻止拉黑用户登录 */ public function ban(){ $user_id = cmf_get_current_user_id(); if (empty($user_id)) { $this->error("用户尚未登录", url("index/index/index")); } $user = Db::name('user')->where('id',$user_id)->find(); if($user['user_status'] == 0){ $this->error('兄嘚,你被拉黑啦!',''); } } /** * 判断是否注册手机号 */ public function isRegister(){ $user_id = cmf_get_current_user_id(); if (empty($user_id)) { $this->error("用户尚未登录", url("index/index/index")); } $user = Db::name('user')->where(array('id'=>$user_id))->find(); if(empty($user['mobile'])){ return false; }else{ return true; } } /** * 判断是否填写问卷调查 */ public function isQuestion(){ $user_id = cmf_get_current_user_id(); $question = Db::name('question')->where('user_id',$user_id)->find(); if(empty($question)){ return false; }else{ return true; } } /** * 判断当前用户是否注册代理人身份 */ public function isAgent(){ //判断当前用户是否注册过代理人手机号 $user_id = cmf_get_current_user_id(); $userModel = new UserModel(); $user = $userModel->findUserData(array('id'=>$user_id)); if($user['type'] != 2){ //重定向到注册代理人身份页面 $this->redirect('agent_register_mobile/index'); } return $user; } /** * 模板消息 */ public function template($templateId,$data,$url=null,$openid){ $options=config('wechat_config'); $app = new Application($options); $notice = $app->notice; if(empty($url)){ $notice->uses($templateId)->andData($data)->andReceiver($openid)->send(); }else{ $notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($openid)->send(); } } public function note($content,$mobile){ $data = array( 'content' => $content,//短信内容 'mobile' => $mobile,//手机号码 'productid' => '887361',//产品id 'xh' => ''//小号 ); $result = send_sms($data); if(substr($result,0,strpos($result,',')) == "1"){ return true; }else{ return false; } } /** * 处理数据 */ public function regroup($data, $insurance_type_id = null, $question = null, $user = null){ $arr = []; $result = []; foreach($data as $key => $vo){ $result1 = []; $result2 = []; foreach($vo as $k=>$v){ if($v['form_type'] == 1){ $result1[] = $v['insurance_price']; if($v['order_expire_time'] < time() or $v['status'] == 1 or $v['order_about_time'] > (time()-30*24*60*60)){ $result2[] = $v['insurance_price']; } }else if($v['form_type'] == 2){ $result1[] = $v['subjoin_price']; if($v['order_expire_time'] < time() or $v['status'] == 1 or $v['order_about_time'] > (time()-30*24*60*60)){ $result2[] = $v['subjoin_price']; } } } $arr['name'] = $key; $arr['sum'] = array_sum($result2); $arr['count'] = count($result2); if(!empty($question) && !empty($user)){ if($v['insurer'] == $user['name']){ if ($insurance_type_id == 1) { //重疾 $standard = ($question['personage_year_income'] * 5) + ($question['not_return'] * 0.3); if ($standard <= $arr['sum']) { //绿油油 $arr['main_arrows'] = 1; } else { //红彤彤 $arr['main_arrows'] = 2; } } else if ($insurance_type_id == 2) { //人寿 $standard = $question['personage_year_income'] * 10 + $question['not_return']; if ($standard <= $arr['sum']) { //绿油油 $arr['main_arrows'] = 1; } else { //红彤彤 $arr['main_arrows'] = 2; } } else if ($insurance_type_id == 3) { //出行 $standard = $question['personage_year_income'] * 10 + $question['not_return']; if ($standard <= $arr['sum']) { //绿油油 $arr['main_arrows'] = 1; } else { //红彤彤 $arr['main_arrows'] = 2; } } else if ($insurance_type_id == 4) { //医疗 $standard = 1000000; if ($standard >= $arr['sum']) { //绿油油 $arr['main_arrows'] = 1; } else { //红彤彤 $arr['main_arrows'] = 2; } } } } if(empty($result1)){//未有保单 $arr['type'] = 1; }else if(empty($result2)){//保障缺失 $arr['type'] = 2; }else{ $arr['type'] = 3; } $result[] = $arr; } return $result; } }