CgfController.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app;
  13. /*require "../vendor/cgf/src/Cgf.php";
  14. require "../vendor/cgf/src/SqlToCgfDefinition.php";
  15. require "../vendor/cgf/src/TableInfoInterface.php";
  16. require "../vendor/cgf/src/TableInfo/Mysql.php";
  17. require "../vendor/cgf/src/CommentParser.php";
  18. require "../vendor/cgf/src/Definition.php";
  19. require "../vendor/cgf/src/Form.php";
  20. require "../vendor/cgf/src/Framework/Thinkphp/Bootstrap.php";
  21. require "../vendor/cgf/src/validate.php";
  22. require "../vendor/cgf/src/validate/ThinkphpValidate.php";
  23. require "../vendor/cgf/src/Template.php";
  24. require "../vendor/cgf/src/Framework/Thinkphp/ThinkphpTemplate.php";*/
  25. use app\BaseController;
  26. //use Cgf\Cgf;
  27. use Cgf\Cgf;
  28. use think\App;
  29. use think\facade\View;
  30. use think\facade\Db;
  31. /**
  32. * 控制器基础类
  33. */
  34. class CgfController extends BaseController
  35. {
  36. public $cgf; //静态类会不会更好?
  37. public $m;
  38. public $moduleName;
  39. public $controllerName;
  40. public $actionName;
  41. function __construct(App $app)
  42. {
  43. parent::__construct($app);
  44. $this->moduleName = \think\facade\Request::app();
  45. $this->controllerName = \think\facade\Request::controller();
  46. $this->actionName = \think\facade\Request::action();
  47. //var_dump($this->moduleName,$this->controllerName,$this->actionName);exit;
  48. $tableName = APP::parseName($this->controllerName);
  49. $this->m = Db::name($this->controllerName);
  50. error_reporting(E_ALL ^ E_NOTICE);
  51. $appBasePath = __DIR__;
  52. $dbconfig = include('../config/database.php');
  53. $dbconfig = $dbconfig['connections']['mysql'];
  54. //dump(Cgf::$config);exit;
  55. $dbconfig = Cgf::getDbConfigFromThinkPHP($dbconfig);
  56. $cgfConf = [];
  57. $cgfConf['dbConfig'] = $dbconfig;
  58. $cgfConf['savePath'] = $appBasePath . "/Cgf/definition";//保存cgf生成的定义文件
  59. $cgfConf['framework'] = 'thinkphp';//使用的框架
  60. $cgfConf['validate'] = 'thinkphp';//使用验证库
  61. $cgfConf['form'] = 'bootstrap';//表单使用的框架
  62. $cgfConf['currentName'] = 'common';//当前模块名
  63. $cgfConf['tableName'] = $tableName;//表名
  64. $cgfConf['controllerName'] = $this->controllerName;//控制器名
  65. $cgfConf['appRootPath'] = $appBasePath;//框架应用程序根目录
  66. $cgfConf['parentTemplatePath'] = $appBasePath . '/view/public/';//cgf生成模板使用的父模板,cgf会根据这里的模板来生成应用模板
  67. $cgfConf['templateSavePath'] = $appBasePath . "/view/{$tableName}";//cgf生成的模板保存路径
  68. $cgfConf['availableModule'] = ['common', 'admin'];//可用模块
  69. $cgfConf['autoHiddenPrimaryKey'] = false;//是否将主键表单类型设为hidden
  70. $this->cgf = new Cgf($cgfConf);
  71. }
  72. public function index()
  73. {
  74. //数据
  75. $map = $this->_search();
  76. if (method_exists($this, '_filter')) {
  77. $this->_filter($map);
  78. }
  79. if (!empty ($this->m)) {
  80. $this->_list($this->m, $map);
  81. }
  82. //模板
  83. $r = $this->cgf->generateListsTemplate();
  84. return view('');
  85. }
  86. public function add()
  87. {
  88. $tpl = $this->cgf->generateAddTemplatel();
  89. //配置select选项和选中值
  90. $allColumnOptionsDefinition = $this->cgf->getAllColumnOptions();
  91. foreach ($allColumnOptionsDefinition as $column => $option) {
  92. $this->assign('opt_' . $column, $option);
  93. $this->assign($column . '_selected', input($column,''));
  94. }
  95. /*if (method_exists($this, '_replacePublic')) {
  96. $this->_replacePublic($vo);
  97. }
  98. $this->pageTitle = $this->getControllerTitle(CONTROLLER_NAME) . "添加";
  99. $this->cgf = "cgf";*/
  100. return view();
  101. }
  102. function edit()
  103. {
  104. $this->cgf->generateAddTemplatel();
  105. $pk = "id";
  106. $id = input($pk);
  107. $where = [];
  108. $where[$pk] = $id;
  109. if (MODULE_NAME == 'User') $where['user_id'] = $this->user_id;
  110. $vo = $this->m->where($where)->find();
  111. /* if (method_exists($this, '_replacePublic')) {
  112. $this->_replacePublic($vo);
  113. }*/
  114. //$pageTitle = $this->getControllerTitle(CONTROLLER_NAME) . "编辑";
  115. $pageTitle = '';
  116. $this->cgf = "cgf";
  117. $view = ['vo'=>$vo,'action'=>'edit','cgf' => 'cgf', 'pageTitle' => $pageTitle];
  118. return view('add', $view);
  119. }
  120. //保存添加和编辑
  121. function save()
  122. {
  123. if (haveUploadFile()) {
  124. $uploadInfo = $this->commonUpload();
  125. if (!empty($uploadInfo)) {
  126. foreach ($uploadInfo as $k => $v) {
  127. $_POST[$k] = $v['path'];
  128. }
  129. }
  130. }
  131. //var_dump($this->isAjax());exit;
  132. //$id = I($this->m->getPk ());
  133. $id = I('id');
  134. //$vo = $this->m->getById ( $id );
  135. //自动验证
  136. //$tableInfo = new TableInfo('',$this->dbConnection);
  137. $validate = new ThinkphpValidate();
  138. $this->cgf->setValidate($validate);
  139. $tableName = $this->m->getTableName();
  140. $rules = $this->cgf->generateValidate($tableName);
  141. //$auto = $tableInfo->getAutoComplete($tableName);
  142. $data = $this->m->create();
  143. if (empty($id)) {
  144. $isNew = 1;
  145. unset($_POST['id']);
  146. //$r = $this->m->validate($rules)->create ();
  147. /*var_dump($_GET);
  148. var_dump($_POST);
  149. var_dump($_REQUEST);
  150. var_dump($r);eixt;*/
  151. //$_POST['user_id'] = $this->user_id; //添加时默认加上用户id
  152. if (false === $this->m->validate($rules)->create()) {
  153. //$this->error ( $this->m->getError () );
  154. }
  155. $r = $this->m->add();
  156. $id = $r;
  157. } else {
  158. if (false === $this->m->validate($rules)->create()) {
  159. $this->error($this->m->getError());
  160. }
  161. $r = $this->m->save();
  162. }
  163. //保存当前数据对象
  164. if (method_exists($this, '_after_save') && $id) $this->_after_save($id);
  165. //echo $this->m->getLastSql();exit;
  166. if ($id !== false) { //保存成功
  167. $ret = [];
  168. //$this->assign ( 'jumpUrl', cookie( '_currentUrl_' ) );
  169. if (I('multiple')) { //多图,需要关联图片到对应的主题,
  170. //注意,不支持一个主题里有两个以上字段都是多图上传。此情况需要在图片表里增加标识,属于哪个字段,
  171. //或在主题字段里存储用逗号分隔的图片id,如:1,2,3
  172. //其实不用forech,直接取$uploadInfo[0]即可,反正又不支持多字段多图上传
  173. if (!empty($uploadInfo)) {
  174. foreach ($uploadInfo as $k => $v) {
  175. $ids[$k] = array_column($v, 'id');
  176. M('File')->where(['id' => ['in', $ids[$k]]])->setField('tid', $id);
  177. }
  178. $ret = $uploadInfo;
  179. }
  180. }
  181. if ($this->successRedirectUrl) {
  182. $redirectUrl = $this->successRedirectUrl . $id;
  183. } elseif ($isNew && $this->newInfoNextUrl) {
  184. $redirectUrl = $this->newInfoNextUrl . $id;
  185. } else {
  186. //$redirectUrl = cookie( '_currentUrl_' );
  187. $redirectUrl = "/" . CONTROLLER_NAME;
  188. }
  189. C('info_id', $id);
  190. $ret['id'] = $id;
  191. $this->success($ret, '成功', $redirectUrl);
  192. } else {
  193. //失败提示
  194. $this->error('失败了!');
  195. }
  196. }
  197. function _initialize() {
  198. //由于 Think\controller construct 里先调用了_initialize ,
  199. // 但userBase 验证登录是放在_initialize,导致此类construct 还没执行,就被跳转了
  200. // 跳转代码获取不到ret_format 导出返回信息仍是html 跳转
  201. //所以要此代码从construct 移动到此处
  202. if(IS_AJAX){
  203. C('ret_format','json');
  204. }elseif(!empty(I(C('VAR_JSONP_HANDLER')))){
  205. C('ret_format','jsonp');
  206. }
  207. if($this->enableLog && !IS_CLI) $this->requestLog();
  208. //白名单优先
  209. if(!empty(C('whitelist'))) {
  210. //只允许白名单中的controller
  211. $wl_control = C('whitelist.controller');
  212. $wl_action = C('whitelist.action');
  213. $wl_url = C('whitelist.url');
  214. $wl_url = array_map(
  215. function($value) {
  216. return strtolower($value);
  217. },
  218. $wl_url
  219. );
  220. $current_url = CONTROLLER_NAME . '/' . ACTION_NAME;
  221. //var_dump(lcfirst($current_url));
  222. //var_dump($wl_url);exit;
  223. if (in_array(lcfirst(CONTROLLER_NAME), $wl_control)
  224. || in_array(lcfirst(ACTION_NAME), $wl_action)
  225. || in_array(strtolower($current_url), $wl_url)
  226. ) {
  227. //通过白名单
  228. } else {
  229. $this->error('control 1 非法访问');
  230. }
  231. }
  232. //黑名单
  233. if(!empty(C('blacklist'))){
  234. //禁用访问黑名单中的controller
  235. $bl_control = C('blacklist.controller');
  236. $bl_action = C('blacklist.action');
  237. $bl_url = C('blacklist.url');
  238. $current_url = CONTROLLER_NAME.'/'.ACTION_NAME;
  239. if( in_array(lcfirst(CONTROLLER_NAME),$bl_control)
  240. || in_array(lcfirst(ACTION_NAME),$bl_action)
  241. || in_array(strtolower($current_url),$bl_url)
  242. ){
  243. $this->error('control 2 非法访问');
  244. }
  245. }
  246. $referer = empty($_SERVER['HTTP_REFERER']) ? '/' : $_SERVER['HTTP_REFERER'];
  247. C('referer',$referer);
  248. $this->platform = $this->getPlatform();
  249. //$this->referer = $_SERVER['HTTP_REFERER'];
  250. /*//url带openid 自动写cookie,session等登录标识
  251. $open_id = I('open_id');
  252. if($open_id){
  253. $r = M('User')->where(["open_id" => $open_id,"type" => I('type')])->find();
  254. if(!empty($r)){
  255. $r['user_id'] = $r['id'];
  256. $this->tempStorageOpenidUser = $r;
  257. setUserAuth($r); //登录前在getAuth 里增加个标识,如果get open_id有值,不必取cookie,直接标识为登录
  258. }
  259. }
  260. //exit('x');exit;
  261. //用户信息
  262. if(!empty($this->user_id)){
  263. $m = M('User');
  264. $r = $m->find($this->user_id);
  265. //var_dump($r);
  266. //echo $m->getLastSql();
  267. //exit;
  268. if(empty($r['nickname']) && !empty($r['username'])) $r['nickname'] = $r['username'];
  269. $this->assign ( 'user', $r );
  270. }
  271. if(C('USER_AUTH_ON')){
  272. import ( '@.ORG.Util.RBAC_WEB' );
  273. $app = 'USER';
  274. }else{
  275. import ( 'ORG.Util.RBAC' );
  276. $app = APP_NAME;
  277. }*/
  278. }
  279. function getPlatform(){
  280. $platform = I('server.platform');
  281. if(empty($platform)){
  282. if(IS_MOBILE){
  283. if(is_weixin()){
  284. $platform = 'wx';
  285. }else{
  286. $platform = 'wap';
  287. }
  288. }else{
  289. $platform = 'pc';
  290. }
  291. }
  292. return $platform;
  293. }
  294. /**
  295. * 设置用户id
  296. * @param $user_id
  297. */
  298. protected function setUserId($user_id){
  299. $_REQUEST['user_id'] = $_POST['user_id'] = $_GET['user_id'] = $user_id;//$this->user_id;
  300. }
  301. //有连接表显示列表
  302. public function indexLink($option=array())
  303. {
  304. //列表过滤器,生成查询Map对象
  305. $map = $this->_search ();
  306. if (method_exists ( $this, '_filter' )) {
  307. $this->_filter ( $map );
  308. }
  309. $name=CONTROLLER_NAME;
  310. if (! empty ( $this->m )) {
  311. if ($option['join']) {
  312. $this->_listLink($this->m, $map, $option);
  313. } else {
  314. $this->_list($this->m, $map);
  315. }
  316. }
  317. if(!IS_AJAX){
  318. $tplContent = $this->generateListTpl();
  319. }
  320. $this->pageTitle = $this->getControllerTitle(CONTROLLER_NAME)."列表";
  321. $this->cgf="cgf";
  322. $this->toview();
  323. }
  324. public function msg($result,$text = '',$url=''){
  325. if(false !== $result){
  326. $this->success($text."成功");
  327. }else{
  328. $this->error($text."失败");
  329. }
  330. }
  331. //获取用户登录凭证信息
  332. function getAuth(){
  333. $u = getUserAuth();
  334. if(!ONLINE){
  335. //$u['user_id'] = 4;
  336. }
  337. //===============================================临时代码 所有付费用户绑定完后,就要删除 同时要删除get-weixin-code.html里110行的临时代码 ===============================================
  338. if(is_weixin() && !empty($u)){
  339. $code = I('code');
  340. if(!empty($code)) { //是微信用户,且已经用手机登录过了,而且没有绑定过unionid,且unionid没有被其它用户绑定过,则绑定
  341. $rU = M('User')->find($u['user_id']);
  342. if(empty($rU['wx_union_id']) || $rU['wx_union_id']==null ){
  343. $conf = C('wechat');
  344. $this->auth = new WechatAuth($conf['appid'], $conf['appSecret']);
  345. if (empty($token)) {
  346. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$conf['appid']}&secret={$conf['appSecret']}&code={$code}&grant_type=authorization_code";
  347. $accessToken = file_get_contents($url);
  348. $accessToken = json_decode($accessToken, 1);
  349. //var_dump($accessToken);
  350. $token = $accessToken['access_token'];
  351. $open_id = $accessToken['openid'];
  352. C('openid',$open_id);
  353. $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$token}&openid=$open_id";
  354. //echo $url;
  355. $userinfo = file_get_contents($url);
  356. $wxUserInfo = json_decode($userinfo, true);
  357. if($wxUserInfo['errcode'] == '41001'){
  358. tplog("wx_error:".$userinfo['errmsg'],'ERR', '', LOG_PATH.'/wx.log');
  359. }
  360. if(!empty($wxUserInfo['unionid'])){
  361. $rUnionIdUser = M('User')->where(['wx_union_id'=>$wxUserInfo['unionid']])->find();
  362. if(empty($rUnionIdUser)){
  363. $mUser = D('User');
  364. $userInfo = $mUser->wxUserInfoToUser($wxUserInfo);
  365. $mUser->where(['id' => $rU['id']])->save($userInfo);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. //===============================================临时代码 所有付费用户绑定完后,就要删除 ===============================================
  373. if(empty($u)){
  374. $open_id = I('open_id');
  375. $type = I('type');
  376. $code = I('code');
  377. if(!empty($open_id) && in_array($type,['wx','wb','qq','gzh']) ){ //公众号搜索进入
  378. $field = $type.'_open_id';
  379. $where[$field] = $open_id;
  380. $u['user_id'] = M('User')->where($where)->getField('id');
  381. //微信公众号用户浏览,第一次要设置cookie,相当于登录操作
  382. if($type=='gzh'){
  383. setUserAuth($u);
  384. }
  385. }elseif(!empty($code)){ //公众号链接进入
  386. $conf = C('wechat');
  387. /* $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$conf['appid']}&secret={$conf['appSecret']}&code={$code}&grant_type=authorization_code";
  388. $r = file_get_contents($url);
  389. var_dump($r);//exit;*/
  390. //$this->wechat = new Wechat($conf['token'], $conf['appid'], $conf['crypt']);
  391. $this->auth = new WechatAuth($conf['appid'], $conf['appSecret']);
  392. //$token = S('weixin_token');
  393. if(empty($token)){
  394. //$accessToken = $this->auth->getAccessToken('code',$code);
  395. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$conf['appid']}&secret={$conf['appSecret']}&code={$code}&grant_type=authorization_code";
  396. $accessToken = file_get_contents($url);
  397. $accessToken = json_decode($accessToken,1);
  398. //var_dump($accessToken);
  399. $token =$accessToken['access_token'];
  400. $open_id=$accessToken['openid'];
  401. C('openid',$open_id);
  402. $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$token}&openid=$open_id";
  403. //echo $url;
  404. $userinfo = file_get_contents($url);
  405. $userinfo = json_decode($userinfo,true);
  406. //var_dump($userinfo);//exit('x==========');
  407. if($userinfo['errcode'] == '41001'){
  408. tplog("wx_error:".$userinfo['errmsg'],'ERR', '', LOG_PATH.'/wx.log');
  409. }
  410. if(!empty($userinfo['unionid'])){
  411. $mUser = M('User');
  412. $rUser = $mUser->where(['wx_union_id'=>$userinfo['unionid']])->find();
  413. if(!empty($rUser)){
  414. $u['user_id'] = $rUser['id'];
  415. setUserAuth($u);
  416. }else{
  417. $id = $this->registerLinkGzh($userinfo);
  418. $u['user_id'] = M('User')->where(['id'=>$id])->getField('id');
  419. //微信公众号用户浏览,第一次要设置cookie,相当于登录操作
  420. setUserAuth($u);
  421. // var_dump($u);
  422. // var_dump($userinfo);exit('============');
  423. //var_dump($userinfo);exit('x');
  424. //S('weixin_token',$token,$accessToken['expires_in']-120);
  425. }
  426. }
  427. }
  428. /*$this->auth->setAccessToken($token);
  429. $this->token = $token;
  430. $this->openid = $open_id;
  431. exit('x');
  432. $info = $this->auth->getUserInfo($this->openid);
  433. var_dump($info);exit('x');
  434. $this->registerUser();*/
  435. }
  436. if(empty($u)){
  437. return false;
  438. }
  439. }
  440. //要去数据库验证有没有此id
  441. $tableInfo = new TableInfo('list');
  442. $selectFields = $tableInfo->createListSelectFields('user');
  443. $dbUserInfo = M('User')->field($selectFields)->find($u['user_id']);
  444. if(empty($dbUserInfo)){
  445. debug('没有此用户');
  446. //echo '没有此用户';
  447. return false;
  448. }
  449. $dbUserInfo['nickname'] = !empty($dbUserInfo['nickname']) ? $dbUserInfo['nickname'] : ( !empty($dbUserInfo['username']) ? $dbUserInfo['username'] : '竞拍用户') ;
  450. $dbUserInfo['avatar'] = img($dbUserInfo['avatar'],'user_avatar');
  451. $this->user_id = $dbUserInfo['id'];
  452. $this->user = $dbUserInfo;
  453. $this->assign('user_id',$this->user_id);
  454. $this->assign('user',$this->user);
  455. return $dbUserInfo;
  456. }
  457. /**
  458. * 公众号链接新用户注册,unionid注册
  459. * @param $info
  460. */
  461. function registerLinkGzh($info){
  462. $userInfo = [];
  463. $userInfo['unionid'] = $info['unionid'];
  464. $userInfo['gzh_open_id'] = $info['openid'];
  465. $userInfo['province'] = $info['province'];
  466. $userInfo['city'] = $info['city'];
  467. $userInfo['avatar'] = $info['headimgurl'];
  468. $userInfo['sex'] = $info['sex'];
  469. $userInfo['nickname'] = $info['nickname'];
  470. $sex_map=[0=>2,1=>1,2=>0];
  471. $userInfo['sex'] = $sex_map[$info['sex']];
  472. $extra = [];
  473. $extra['subscribe'] = $info['subscribe'];
  474. $extra['groupid'] = $info['groupid'];
  475. $extra['tagid_list'] = $info['tagid_list'];
  476. $extra['subscribe_time'] = $info['subscribe_time'];
  477. $userInfo['extra'] = json_encode($extra,JSON_UNESCAPED_UNICODE);
  478. //wx_open_id
  479. /** @var \Common\Model\UserModel $mUser */
  480. $mUser = D('User');
  481. $r = $mUser->oauthUnionidAdd('wx',$userInfo);
  482. return $r;
  483. }
  484. /**
  485. * 公众号搜索新用户注册
  486. */
  487. function registerUser(){
  488. /** @var \Common\Model\UserModel $mUser */
  489. $mUser = D('User');
  490. $field = 'wx_open_id';
  491. $r = $mUser->where(['gzh_open_id' => $this->openid ])->find();
  492. if(empty($r)){
  493. $info = $this->auth->getUserInfo($this->openid);
  494. if(empty($info)){
  495. $message = "token:{$this->token}| open_id: {$this->openid}";
  496. tplog($message, 'ERR', '', LOG_PATH.'/wx.log');
  497. }
  498. $userInfo = [];
  499. $userInfo['province'] = $info['province'];
  500. $userInfo['city'] = $info['city'];
  501. $userInfo['avatar'] = $info['headimgurl'];
  502. $userInfo['sex'] = $info['sex'];
  503. $userInfo['nickname'] = $info['nickname'];
  504. $sex_map=[0=>2,1=>1,2=>0];
  505. $userInfo['sex'] = $sex_map[$info['sex']];
  506. $extra = [];
  507. $extra['subscribe'] = $info['subscribe'];
  508. $extra['groupid'] = $info['groupid'];
  509. $extra['tagid_list'] = $info['tagid_list'];
  510. $extra['subscribe_time'] = $info['subscribe_time'];
  511. $userInfo['extra'] = json_encode($extra,JSON_UNESCAPED_UNICODE);
  512. //wx_open_id
  513. $r = $mUser->oauthAdd($this->openid,'gzh',$userInfo);
  514. }
  515. }
  516. /**
  517. * 访问日志,记录用户请求的参数
  518. */
  519. function requestLog(){
  520. $data = array();
  521. $data['url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  522. if(IS_POST){
  523. $params = $_POST;
  524. }elseif(IS_GET){
  525. $params = $_GET;
  526. }
  527. if(empty($params)) $params['input'] = file_get_contents("php://input");
  528. $data['params'] = json_encode($params);
  529. //$data['cookie'] = json_encode($_COOKIE);
  530. //$data['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  531. $data['ip'] = get_client_ip();
  532. $detail = array();
  533. $detail['request'] = $_REQUEST;
  534. $header = [];
  535. $fields = ['HTTP_USER_ID','HTTP_DEVICE_VID','HTTP_DEVICE_ID','HTTP_PLATFORM','HTTP_VERSION']; //'HTTP_USER_AGENT',
  536. foreach ($fields as $k => $v){
  537. if(empty($_SERVER[$v])) continue;
  538. $header[$v] = $_SERVER[$v];
  539. }
  540. /*$this->version = I('server.HTTP_VERSION');
  541. $this->device_id = I('device_id') ?:I('server.HTTP_DEVICE_ID');
  542. $this->platform = I('server.HTTP_PLATFORM');
  543. $user_id = I('user_id') ?: I('server.HTTP_USER_ID');
  544. $detail['server'] = $_SERVER;*/
  545. //$detail['header'] = $header;
  546. //$data['detail'] = json_encode($detail);
  547. $url = $_SERVER['REQUEST_METHOD']." ".$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']." ".$_SERVER['SERVER_PROTOCOL']."\r\n";
  548. $request = $url.getallheaders(true);
  549. $raw_post = '';
  550. if(IS_POST){
  551. $raw_post = http_build_query($_POST);
  552. if(empty($raw_post)){
  553. $raw_post = file_get_contents("php://input");
  554. }
  555. }
  556. $request .= "\r\n".$raw_post;
  557. $data['detail'] = $request;
  558. $data['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  559. $data['platform'] = I('server.HTTP_PLATFORM');
  560. $data['user_id'] = cookie('user_id');//cookie可能取出null,要求字段必须可为null
  561. $data['create_time'] = date("Y-m-d H:i:s");
  562. $data['method'] = $_SERVER['REQUEST_METHOD'];
  563. $data['date_int'] = time();
  564. try{
  565. $m = M('LogRequest','',C('log_db'));
  566. //$m->create($data);
  567. $logId = $m->add($data);
  568. C('logId',$logId);
  569. }catch (\Exception $e){
  570. tplog($e->getMessage());
  571. }
  572. //echo $m->getLastSql();exit;
  573. }
  574. /**
  575. * 记录响应,调用的地方有:\Think\Control->ajaxReturn()
  576. * @param $id
  577. * @param $response
  578. */
  579. function responseLog($id,$response){
  580. /* $data = [];
  581. $data['id'] = $id;
  582. $data['response'] = $response;
  583. $m = M('LogRequest','',C('log_db'));
  584. $m->save($data);*/
  585. }
  586. public function lists() {
  587. //列表过滤器,生成查询Map对象
  588. $map = $this->_search ();
  589. if (method_exists ( $this, '_filter' )) {
  590. $this->_filter ( $map );
  591. }
  592. $name=CONTROLLER_NAME;
  593. //$model = D ($name);
  594. if (! empty ( $this->m )) {
  595. $this->_list ( $this->m, $map );
  596. }
  597. $this->toview ();
  598. return;
  599. //exit('lists erorr');
  600. // $this->display ();
  601. //return;
  602. }
  603. /**
  604. * 根据表单生成查询条件 进行列表过滤
  605. * @param string $name
  606. * @return array
  607. */
  608. protected function _search($name = '') {
  609. $map=[];
  610. $autoIndistinct = true;
  611. $fields = Db::name($this->controllerName)->getFields(); //包含字段类型,注释等
  612. foreach ( $fields as $column => $definition ) {
  613. $inputValue = input($column);
  614. if ( $inputValue !== null && $inputValue != '') {
  615. if($autoIndistinct && $this->columnType($definition['type']) == 'string'){
  616. $map[] = [$column, 'like', '%'.$inputValue.'%'];
  617. }
  618. }
  619. }
  620. //配置select选项和选中值
  621. $options = $this->cgf->getAllColumnOptions();
  622. foreach ($options as $column => $option) {
  623. $this->assign('opt_' . $column, $option);
  624. $this->assign($column . '_selected', input($column,[]));
  625. //var_dump($column . '_selected');
  626. }
  627. /*dump($fields);
  628. $fields = Db::name($this->controllerName)->getTableFields();
  629. dump($fields);exit;
  630. $map = array ();
  631. foreach ( $fields as $key => $val ) {
  632. $inputValue = input($val);
  633. if ( $inputValue !== null && $inputValue != '') {
  634. $map[] = [$val, 'like', '%'.$inputValue.'%'];
  635. }
  636. }*/
  637. return $map;
  638. }
  639. function columnType($type){
  640. if( strpos($type,'char') !== false || strpos($type,'text') !== false){
  641. return 'string';
  642. }elseif (strpos($type,'int') !== false || strpos($type,'decimal') !== false || strpos($type,'numeric') || strpos($type,'double')){
  643. return 'int';
  644. }else{
  645. return '';
  646. }
  647. }
  648. /**
  649. * 根据表单生成查询条件
  650. * 进行列表过滤
  651. * @param $model 模型对象
  652. * @param $map 过滤条件
  653. * @param string $sortBy 排序
  654. * @param bool $asc 是否正序
  655. */
  656. protected function _list($model, $map, $sortBy = '', $asc = false) {
  657. //排序字段 默认为主键名
  658. if (!empty( $_REQUEST ['_order'] )) {
  659. $order = $_REQUEST ['_order'];
  660. } else {
  661. $order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
  662. }
  663. //排序方式默认按照倒序排列
  664. //接受 sost参数 0 表示倒序 非0都 表示正序
  665. //$setOrder = setOrder(array(array('viewCount', 'a.view_count'), 'a.id'), $orderBy, $orderType, 'a');
  666. if (!empty( $_REQUEST ['_sort'] )) {
  667. $sort = $_REQUEST ['_sort'] ;
  668. } else {
  669. $sort = $asc ? 'asc' : 'desc';
  670. }
  671. //取得满足条件的记录数
  672. $pk = $model->getPk();
  673. $count = $model->where ( $map )->count ();
  674. if ($count > 0) {
  675. //import ( "ORG.Util.Page" );
  676. //创建分页对象
  677. if (! empty ( $_REQUEST ['listRows'] )) {
  678. $listRows = $_REQUEST ['listRows'];
  679. } elseif(!empty($this->listRows)){
  680. $listRows = $this->listRows;
  681. } else {
  682. $listRows = '10';
  683. }
  684. if(strtolower(MODULE_NAME) == 'user'){
  685. unset($_GET['user_id']);
  686. }
  687. //========================================== cgf start =========================================
  688. //1.生成查询字段
  689. $selectFields = $this->cgf->generateListSelectColumn();
  690. //var_dump($selectFields);exit;
  691. //if(strpos($order,'`') === false) $order = "`" . $order . "` ";
  692. //$r = $model->select();
  693. //var_dump($r);exit;
  694. $voList = $model->field($selectFields)->where($map)->order($order.' '. $sort)->paginate(5, false, ['query' => []]);
  695. //去掉前台不显示的字段,与上面$selectFields功能重复
  696. /*if(C('ret_format') == 'json' || C('ret_format') == 'jsonp'){
  697. $tableInfo = new TableInfo('list');
  698. $tableName = $this->m->getTableName();
  699. $fields = $tableInfo->generateHomeListFields($tableName);
  700. foreach ($voList as $k => &$v){
  701. foreach ($v as $column => $value){
  702. if(!in_array($column,$fields)) unset($v[$column]);
  703. }
  704. }
  705. }*/
  706. //2.当前表有关联的表字段时,取关联表信息并合并。实现join功能
  707. $this->cgf->mergeRelatedTableData($voList);
  708. //3.调用字段显示处理函数
  709. $this->cgf->executeColumnCallback($voList);
  710. //与后台管理的list里调用相关显示函数有重复,如|optionValue
  711. //if (method_exists ( $this, 'dateToViewModel' )) $this->dateToViewModel ( $voList );
  712. //========================================== cgf end =========================================
  713. if (method_exists ( $this, '_join' )) $this->_join ( $voList );
  714. //将导出功能注入到此处
  715. if(ACTION_NAME == 'exportExcel'){
  716. $this->realExportExcel($voList);
  717. }
  718. //var_dump($voList);exit;
  719. //echo $model->getlastsql();exit('x');
  720. //分页跳转的时候保证查询条件
  721. foreach ( $map as $key => $val ) {
  722. if (! is_array ( $val ) && !in_array($key,['_logic'])) {
  723. //$p->parameter .= "$key=" . urlencode ( $val ) . "&";
  724. if(strtolower(MODULE_NAME) == 'user' && $key =='user_id'){
  725. continue;
  726. }
  727. //$p->parameter[$key] = urlencode ( $val );
  728. }
  729. }
  730. $page = $voList->render();
  731. $sortImg = $sort == 'desc' ? "glyphicon-arrow-down" : "glyphicon-arrow-up"; //排序图标 glyphicon glyphicon-arrow-up
  732. if($sort=='desc'){
  733. $sortImg = 'glyphicon-arrow-down';
  734. }elseif($sort='asc'){
  735. $sortImg = 'glyphicon-arrow-up';
  736. }else{
  737. $sortImg = 'glyphicon-sort';
  738. }
  739. $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
  740. $sort = $sort == 'desc' ? 'asc' : 'desc'; //页面上显示的下一次排序方式
  741. //模板赋值显示
  742. $this->assign ( 'list', $voList );
  743. $this->assign ( 'sort', $sort );
  744. $this->assign ( 'order', $order );
  745. $this->assign ( 'sortImg', $sortImg );
  746. $this->assign ( 'sortType', $sortAlt );
  747. $this->assign ( "page", $page );
  748. $this->assign ( "totalPages", $p->totalPages );
  749. $this->assign ( "nowPage", $p->nowPage );
  750. $this->assign ('totalRows',$count);
  751. //顶部简易分页
  752. if($this->enableLitePage){
  753. $nextIndex = $p->nowPage+1;
  754. if($nextIndex > $p->totalPages) $nextIndex = $p->totalPages;
  755. $nextPageUrl = $p->url($nextIndex);
  756. $prevIndex = $p->nowPage-1;
  757. if($prevIndex < 1) $prevIndex = 1;
  758. $prevPageUrl = $p->url($prevIndex);
  759. $this->assign('nextPageUrl',$nextPageUrl);
  760. $this->assign('prevPageUrl',$prevPageUrl);
  761. }
  762. }else{
  763. $this->assign ( 'list', [] );
  764. }
  765. //cookie( '_currentUrl_', __SELF__ );
  766. return;
  767. }
  768. function assign($key,$value){
  769. View::assign($key,$value);
  770. }
  771. /**
  772. +----------------------------------------------------------
  773. * 根据表单生成查询条件
  774. * 进行列表过滤
  775. * 返回结果,不输出
  776. +----------------------------------------------------------
  777. * @access protected
  778. +----------------------------------------------------------
  779. * @param Model $model 数据对象
  780. * @param HashMap $map 过滤条件
  781. * @param string $sortBy 排序
  782. * @param boolean $asc 是否正序
  783. +----------------------------------------------------------
  784. * @return void
  785. +----------------------------------------------------------
  786. * @throws ThinkExecption
  787. +----------------------------------------------------------
  788. */
  789. protected function _getlist($model, $map, $sortBy = '', $asc = false) {
  790. //排序字段 默认为主键名
  791. if (isset ( $_REQUEST ['_order'] )) {
  792. $order = $_REQUEST ['_order'];
  793. } else {
  794. $order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
  795. }
  796. //排序方式默认按照倒序排列
  797. //接受 sost参数 0 表示倒序 非0都 表示正序
  798. //$setOrder = setOrder(array(array('viewCount', 'a.view_count'), 'a.id'), $orderBy, $orderType, 'a');
  799. if (isset ( $_REQUEST ['_sort'] )) {
  800. $sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
  801. } else {
  802. $sort = $asc ? 'asc' : 'desc';
  803. }
  804. //取得满足条件的记录数
  805. $pk = $model->getPk();
  806. $count = $model->where ( $map )->count ( $pk );
  807. if ($count > 0) {
  808. import ( "ORG.Util.Page" );
  809. //创建分页对象
  810. if (! empty ( $_REQUEST ['listRows'] )) {
  811. $listRows = $_REQUEST ['listRows'];
  812. } else {
  813. $listRows = '';
  814. }
  815. $p = new Page ( $count, $listRows );
  816. //echo C('PAGE_STYLE');exit;
  817. $p->style = C('PAGE_STYLE');//设置风格
  818. //分页查询数据
  819. //var_dump($p->listRows);exit;
  820. $voList = $model->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
  821. //echo $model->getlastsql();
  822. //分页跳转的时候保证查询条件
  823. foreach ( $map as $key => $val ) {
  824. if (! is_array ( $val )) {
  825. $p->parameter .= "$key=" . urlencode ( $val ) . "&";
  826. }
  827. }
  828. //分页显示
  829. $page = $p->show ();
  830. //列表排序显示
  831. $sortImg = $sort; //排序图标
  832. $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
  833. $sort = $sort == 'desc' ? 'asc' : 'desc'; //页面上显示的排序方式
  834. //模板赋值显示
  835. return array('list' => $voList ,
  836. 'sort' => $sort,
  837. 'order' => $order);
  838. //$this->assign ( 'sortImg', $sortImg );
  839. //$this->assign ( 'sortType', $sortAlt );
  840. //$this->assign ( "page", $page );*
  841. /*$this->assign ( 'list', $voList );
  842. $this->assign ( 'sort', $sort );
  843. $this->assign ( 'order', $order );
  844. $this->assign ( 'sortImg', $sortImg );
  845. $this->assign ( 'sortType', $sortAlt );
  846. $this->assign ( "page", $page );*/
  847. }
  848. //cookie( '_currentUrl_', __SELF__ );
  849. return;
  850. }
  851. /**
  852. +----------------------------------------------------------
  853. * 连接查询列表显示
  854. * 进行列表过滤
  855. +----------------------------------------------------------
  856. * @access protected
  857. +----------------------------------------------------------
  858. * @param Model $model 数据对象
  859. * @param HashMap $map 过滤条件
  860. * @param string $sortBy 排序
  861. * @param boolean $asc 是否正序
  862. +----------------------------------------------------------
  863. * @return void
  864. +----------------------------------------------------------
  865. * @throws ThinkExecption
  866. +----------------------------------------------------------
  867. */
  868. protected function _listLink($model,$map,$option=array(), $sortBy = '', $asc = false,$sql='') {
  869. extract($option);
  870. //$option['join'] = $join; //有查询条件,开启连接查询
  871. $field || $field = "*";
  872. $table || $table = $model->getTableName();
  873. //$table = "{$this->trueTableName} j";
  874. //$r = $this->table($table)->field($field)->join($join)->where($map)->count();
  875. //dump($r);
  876. //return $r;
  877. //排序字段 默认为主键名
  878. if (isset ( $_REQUEST ['_order'] )) {
  879. $order = $_REQUEST ['_order'];
  880. } else {
  881. $order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
  882. }
  883. //排序方式默认按照倒序排列
  884. //接受 sost参数 0 表示倒序 非0都 表示正序
  885. //$setOrder = setOrder(array(array('viewCount', 'a.view_count'), 'a.id'), $orderBy, $orderType, 'a');
  886. if (isset ( $_REQUEST ['_sort'] )) {
  887. $sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
  888. } else {
  889. $sort = $asc ? 'asc' : 'desc';
  890. }
  891. //取得满足条件的记录数
  892. $pk = $model->getPk();
  893. if ($option['num'] ){ //限制取几条记录,直接返回指定条记录
  894. if($sql){
  895. $voList = $model->query($sql);
  896. }elseif($option['join']){
  897. if(strpos($order,'`') === false) $order = "`" . $order . "` ";
  898. $voList = $model->table($table)->alias('a')->field($field)->join($option['join'])->where($map)->order( $order .' '. $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
  899. }else{
  900. $voList = $model->where($map)->order( "`" . $order . "` " .' '. $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
  901. }
  902. return $voList;
  903. }else{ //分页
  904. if($sql){
  905. $count = $count = $model->query(getCountSql($sql));
  906. $count = $count[0];
  907. }elseif($option['join']){
  908. //if(strpos($order,'`') === false) $order = "`" . $order . "` ";
  909. $count = $model->table($table)->alias('a')->field($field)->join($join)->where($map)->count();
  910. }else{
  911. $count = $model->where ( $map )->count ( $pk );
  912. }
  913. if($count<0) return;
  914. import ( "ORG.Util.Page" );
  915. //创建分页对象
  916. if (! empty ( $_REQUEST ['listRows'] )) {
  917. $listRows = $_REQUEST ['listRows'];
  918. } else {
  919. $listRows = '';
  920. }
  921. $p = new \Think\Page ( $count, $listRows );
  922. //echo C('PAGE_STYLE');exit;
  923. //$s = rand(1,25);echo $s;
  924. $p->style = C('PAGE_STYLE');//设置风格
  925. //分页查询数据
  926. if($sql){
  927. $voList = $model->query($sql);
  928. }elseif($option['join']){
  929. if(strpos($order,'`') === false) $order = "`" . $order . "` ";
  930. $voList = $model->table($table)->alias('a')->field($field)->join($join)->where($map)->order( $order .' '. $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
  931. }else{
  932. $voList = $model->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
  933. }
  934. //高亮关键字
  935. if(C('highLightKeyword') && $_REQUEST['keyword']){
  936. $keyword = $_REQUEST['keyword'];
  937. foreach($voList as $k => $v){
  938. $voList[$k]['jtitle'] = hightLightKeyword($v['jtitle'],$keyword);
  939. $voList[$k]['request'] = hightLightKeyword($v['request'],$keyword);
  940. $voList[$k]['ctitle'] = hightLightKeyword($v['ctitle'],$keyword);
  941. }
  942. }
  943. //分页跳转的时候保证查询条件
  944. foreach ( $map as $key => $val ) {
  945. if (! is_array ( $val )) {
  946. $p->parameter .= "$key=" . urlencode ( $val ) . "&";
  947. }
  948. }
  949. //分页显示
  950. $page = $p->show ();
  951. //列表排序显示
  952. $sortImg = $sort; //排序图标
  953. $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
  954. $sort = $sort == 'desc' ? 'asc' : 'desc'; //页面上显示的排序方式
  955. //模板赋值显示
  956. $this->assign ( 'list', $voList );
  957. $this->assign ( 'sort', $sort );
  958. $this->assign ( 'order', $order );
  959. $this->assign ( 'sortImg', $sortImg );
  960. $this->assign ( 'sortType', $sortAlt );
  961. $this->assign ( "page", $page );
  962. $this->assign ( "totalPages", $p->totalPages );
  963. $this->assign ( "nowPage", $p->nowPage );
  964. $this->assign ('totalRows',$count);
  965. }
  966. //cookie( '_currentUrl_', __SELF__ );
  967. return;
  968. }
  969. public function delete() {
  970. //删除指定记录
  971. $name=CONTROLLER_NAME;
  972. //$model = M ($name);
  973. if (! empty ( $this->m )) {
  974. $pk = $this->m->getPk ();
  975. $id = I($pk);
  976. if (!empty ( $id )) {
  977. $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
  978. $list=$this->m->where ( $condition )->setField ( 'status', 0 );
  979. if ($list!==false) {
  980. $this->success ('删除成功!',cookie ( '_currentUrl_' ));
  981. } else {
  982. $this->error ('删除失败!');
  983. }
  984. } else {
  985. $this->error ( '非法操作' );
  986. }
  987. }
  988. }
  989. public function foreverdelete() {
  990. //删除指定记录
  991. $name=CONTROLLER_NAME;
  992. //$model = D ($name);
  993. if (! empty ( $this->m )) {
  994. $pk = $this->m->getPk ();
  995. $id = I($pk);
  996. if (isset ( $id )) {
  997. $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
  998. //用户中心增加用户id条件
  999. if(MODULE_NAME == 'User') $condition['user_id'] = $this->user_id;
  1000. $r = $this->m->where($condition)->select();
  1001. if(empty($r)) $this->error ( '非法操作' );
  1002. if (false !== $this->m->where ( $condition )->delete ()) {
  1003. if (!empty(I(C('VAR_JSONP_HANDLER'))) || IS_AJAX){
  1004. $method_name = '_after_' . ACTION_NAME;
  1005. if(method_exists($this,$method_name)){
  1006. //if(is_callable([$this,$method_name])){
  1007. $this->$method_name($r);
  1008. /* if ($after->isPublic()) {
  1009. $after->invoke($module);
  1010. }*/
  1011. }
  1012. }
  1013. $this->assign ( 'jumpUrl', cookie ( '_currentUrl_' ) );
  1014. $this->success ('删除成功!',cookie ( '_currentUrl_' ));
  1015. } else {
  1016. $this->error ('删除失败!');
  1017. }
  1018. } else {
  1019. $this->error ( '非法操作' );
  1020. }
  1021. }
  1022. $this->forward ();
  1023. }
  1024. public function clear() {
  1025. //删除指定记录
  1026. $name=CONTROLLER_NAME;
  1027. //$this->m = D ($name);
  1028. if (! empty ( $this->m )) {
  1029. if (false !== $this->m->where ( 'status=1' )->delete ()) {
  1030. $this->assign ( "jumpUrl", $this->getReturnUrl () );
  1031. $this->success ( L ( '_DELETE_SUCCESS_' ) );
  1032. } else {
  1033. $this->error ( L ( '_DELETE_FAIL_' ) );
  1034. }
  1035. }
  1036. $this->forward ();
  1037. }
  1038. public function forbid() {
  1039. $name=CONTROLLER_NAME;
  1040. //$model = D ($name);
  1041. $pk = $this->m->getPk ();
  1042. $id = $_REQUEST [$pk];
  1043. $condition = array ($pk => array ('in', $id ) );
  1044. $list=$this->m->forbid ( $condition );
  1045. if ($list!==false) {
  1046. $this->assign ( "jumpUrl", $this->getReturnUrl () );
  1047. $this->success ( '状态禁用成功' );
  1048. } else {
  1049. $this->error ( '状态禁用失败!' );
  1050. }
  1051. }
  1052. public function checkPass() {
  1053. $name=CONTROLLER_NAME;
  1054. //$model = D ($name);
  1055. $pk = $this->m->getPk ();
  1056. $id = $_GET [$pk];
  1057. $condition = array ($pk => array ('in', $id ) );
  1058. if (false !== $this->m->checkPass( $condition )) {
  1059. $this->assign ( "jumpUrl", $this->getReturnUrl () );
  1060. $this->success ( '状态批准成功!' );
  1061. } else {
  1062. $this->error ( '状态批准失败!' );
  1063. }
  1064. }
  1065. public function recycle() {
  1066. $name=CONTROLLER_NAME;
  1067. //$model = D ($name);
  1068. $pk = $this->m->getPk ();
  1069. $id = $_GET [$pk];
  1070. $condition = array ($pk => array ('in', $id ) );
  1071. if (false !== $this->m->recycle ( $condition )) {
  1072. $this->assign ( "jumpUrl", $this->getReturnUrl () );
  1073. $this->success ( '状态还原成功!' );
  1074. } else {
  1075. $this->error ( '状态还原失败!' );
  1076. }
  1077. }
  1078. public function recycleBin() {
  1079. $map = $this->_search ();
  1080. $map ['status'] = - 1;
  1081. $name=CONTROLLER_NAME;
  1082. //$model = D ($name);
  1083. if (! empty ( $this->m )) {
  1084. $this->_list ( $this->m, $map );
  1085. }
  1086. $this->display ();
  1087. }
  1088. /**
  1089. +----------------------------------------------------------
  1090. * 默认恢复操作
  1091. *
  1092. +----------------------------------------------------------
  1093. * @access public
  1094. +----------------------------------------------------------
  1095. * @return string
  1096. +----------------------------------------------------------
  1097. * @throws FcsException
  1098. +----------------------------------------------------------
  1099. */
  1100. function resume() {
  1101. //恢复指定记录
  1102. $name=CONTROLLER_NAME;
  1103. //$model = D ($name);
  1104. $pk = $this->m->getPk ();
  1105. $id = $_GET [$pk];
  1106. $condition = array ($pk => array ('in', $id ) );
  1107. if (false !== $this->m->resume ( $condition )) {
  1108. $this->assign ( "jumpUrl", $this->getReturnUrl () );
  1109. $this->success ( '状态恢复成功!' );
  1110. } else {
  1111. $this->error ( '状态恢复失败!' );
  1112. }
  1113. }
  1114. function saveSort() {
  1115. $seqNoList = $_POST ['seqNoList'];
  1116. if (! empty ( $seqNoList )) {
  1117. //更新数据对象
  1118. $name=CONTROLLER_NAME;
  1119. //$model = D ($name);
  1120. $col = explode ( ',', $seqNoList );
  1121. //启动事务
  1122. $this->m->startTrans ();
  1123. foreach ( $col as $val ) {
  1124. $val = explode ( ':', $val );
  1125. $this->m->id = $val [0];
  1126. $this->m->sort = $val [1];
  1127. $result = $this->m->save ();
  1128. if (! $result) {
  1129. break;
  1130. }
  1131. }
  1132. //提交事务
  1133. $this->m->commit ();
  1134. if ($result!==false) {
  1135. //采用普通方式跳转刷新页面
  1136. $this->success ( '更新成功' );
  1137. } else {
  1138. $this->error ( $this->m->getError () );
  1139. }
  1140. }
  1141. }
  1142. protected function msgText($nextModel,$nextModelText,$id){
  1143. $app = __APP__;
  1144. $url = __CONTROLLER__;
  1145. return "发布成功! <a href='$app/$nextModel/add'>发布{$nextModelText}信息</a> <a href='$url/edit/id/$id'>返回修改信息</a> <a href='$url/'>返回列表</a>";
  1146. }
  1147. public function show($content="",$charset='',$contentType='',$prefix=''){
  1148. $id = I('id');
  1149. $vo = $this->m->getById ( $id );
  1150. if (method_exists ( $this, '_show' )) {
  1151. $this->_show ( $vo );
  1152. }
  1153. $this->vo = $vo;
  1154. $this->toview();
  1155. }
  1156. function upload($stype='file'){
  1157. $uploadInfo = $this->commonUpload($stype);
  1158. $this->upload = $uploadInfo;
  1159. $this->toview();
  1160. }
  1161. /**
  1162. * 通用上传,支持多张上传
  1163. * @param string $moduleDir 模块目录名
  1164. * @return array 上传结果,一张图片,一个array,key为上传控件的名字
  1165. */
  1166. protected function commonUpload($stype='file'){
  1167. $type = I('type','file');
  1168. if(C('UPLOAD_STRORAGE') == 'oss'){
  1169. //传到aliyun
  1170. C('UPLOAD_SITEIMG_OSS.savePath',$stype.'/');
  1171. $setting=C('UPLOAD_SITEIMG_OSS');
  1172. $upload = new \Think\Upload($setting);
  1173. $info = $upload->upload();
  1174. }else{
  1175. $config = array(
  1176. 'subName' => array('date','Ymd'),
  1177. );
  1178. //$modelName = "file/";
  1179. $rootPath = C('SAVE_PATH');
  1180. $upload = new \Think\Upload($config);
  1181. $upload->maxSize = 31457280 ;// 设置附件上传大小
  1182. $upload->exts = array('jpg', 'gif', 'png', 'jpeg','doc','docx','rar','zip','tar','tar.gz','tgz','7z','txt','flv','mp4','mov');// 设置附件上传类型
  1183. $upload->rootPath = $rootPath; // 设置附件上传根目录
  1184. $upload->savePath = $stype.'/'; // 设置附件上传(子)目录
  1185. //传到本地
  1186. $info = $upload->upload();
  1187. }
  1188. if(!$info) {
  1189. $this->error($upload->getError());
  1190. }else{
  1191. $ret = [];
  1192. $f = M('image');
  1193. $multiple = I('multiple');
  1194. //从数组中取出重复的元素
  1195. if(!$multiple){
  1196. $file_names = array_column($info,'key');
  1197. $unique_arr = array_unique ( $file_names );
  1198. //有多个文件的file对象的名称
  1199. $muti_file_key = array_diff_assoc ( $file_names, $unique_arr );
  1200. }
  1201. //var_dump($repeat_arr);
  1202. //exit;
  1203. $goods_id = 1;
  1204. foreach ($info as $k => $fileInfo){
  1205. /* $cropInfo = I('avatar_data');
  1206. $cropInfo = json_decode($cropInfo,1);
  1207. foreach ($cropInfo as &$v){
  1208. $v = round($v);
  1209. }*/
  1210. $filePath = $fileInfo['savepath'].$fileInfo['savename'];
  1211. /* $data = [];
  1212. if( $this->crop($fileInfo,$cropInfo['width'],$cropInfo['height'],$cropInfo['x'],$cropInfo['y'],$cropInfo['rotate'])){
  1213. $filePath .= "_{$cropInfo['width']}x{$cropInfo['height']}.{$fileInfo['ext']}";
  1214. }
  1215. $data['url'] = URL_IMG.'/'.$filePath;*/
  1216. //$data = $info['avatar_file'];
  1217. $data = [];
  1218. $data['res_id'] = $goods_id;
  1219. $data['type'] = 20;
  1220. //$data['user_id'] = $this->user_id;
  1221. $data['hd_url'] = $filePath;
  1222. $data['nhd_url'] = $filePath;
  1223. $data['create_t'] = time();
  1224. /*$data['title'] = I('title');
  1225. $data['content'] = I('content');
  1226. $data['type'] = $type;
  1227. $data['input_name'] = trim(trim($fileInfo['key'],"'"),'_');*/
  1228. $id = $f->add($data);
  1229. if( $multiple
  1230. || !empty($muti_file_key) && in_array($data['input_name'],$muti_file_key)){ //多文件上传,返回数组
  1231. $ret[$data['input_name']][] = ["id" => $id,'path' => $filePath,'url' => img($filePath,'big'), ];
  1232. }else{
  1233. $ret[$data['input_name']] = ["id" => $id,'path' => $filePath,'url' => img($filePath,'big'), ];
  1234. }
  1235. }
  1236. return $ret;
  1237. //$this->success();
  1238. //var_dump($data);exit('x');
  1239. //$this->toview($data);
  1240. }
  1241. }
  1242. function responseFormat(){
  1243. $format = "";
  1244. if(IS_AJAX || C('RETRUN_FORMAT') == "android_json" || I('ret_format') == 'json' || $_SERVER['HTTP_ACCEPT'] == 'application/json'){ //json,app: code,msg,data
  1245. return "json";
  1246. }elseif (!empty(I(C('VAR_JSONP_HANDLER')))){ //jsonp
  1247. return "jsonp";
  1248. }elseif(isMobile()){
  1249. return "wap";
  1250. }else{
  1251. return "web";
  1252. }
  1253. }
  1254. /**
  1255. * @name 根据请求方式,显示对应的格式到页面
  1256. * @param 数据 array $data
  1257. * @param 格式类型 int $type
  1258. * @return member
  1259. */
  1260. public function toview($data = "", $tpl=""){
  1261. if(!APP_DEBUG) {
  1262. //action结束,缓存viewData
  1263. \Think\Hook::listen('setViewData', $this); // //tag('setViewData',$this);
  1264. }
  1265. if(empty($data)) $data = $this->get();
  1266. //var_dump($data);//exit;
  1267. //if(!empty($tpl)) $this->display($tpl);
  1268. //var_dump($_SERVER);exit;
  1269. if(IS_AJAX || C('RETRUN_FORMAT') == "android_json" || I('ret_format') == 'json' || $_SERVER['HTTP_ACCEPT'] == 'application/json'){ //json,app: code,msg,data
  1270. if(empty($data)) $data = (object)$data;
  1271. $this->success($data,"",1);
  1272. }elseif (!empty(I(C('VAR_JSONP_HANDLER')))){ //jsonp
  1273. $this->ajaxReturn(array("code" =>1, "msg" => "","data" => $data),'JSONP');
  1274. }elseif(isMobile()){ //wap
  1275. empty($tpl) && $tpl = RAW_ACTION_NAME;
  1276. $wapTpl = "wap_".$tpl;
  1277. $templateFile = $this->view->parseTemplate($wapTpl);
  1278. //var_dump($templateFile);exit;
  1279. //if()
  1280. $www_host = parse_url(URL_WWW)['host'];
  1281. if($_SERVER['HTTP_HOST'] == $www_host){
  1282. redirect(URL_M.__SELF__);
  1283. }
  1284. /*
  1285. elseif ($_SERVER['HTTP_HOST'] == $news_host){
  1286. //新闻则跳转到wap的新闻模块
  1287. redirect(URL_M.'news/'.__SELF__);
  1288. }*/
  1289. //var_dump(SCHEME.$_SERVER['HTTP_HOST'] ,URL_M, SCHEME.$_SERVER['HTTP_HOST'] , URL_USER,URL_M.__SELF__);exit;
  1290. //if(SCHEME.$_SERVER['HTTP_HOST'] !=URL_M && SCHEME.$_SERVER['HTTP_HOST'] != URL_USER) redirect(URL_M.__SELF__);
  1291. if(is_file($templateFile)) $this->display($wapTpl);
  1292. else $this->display($tpl);
  1293. }else{ //web
  1294. $this->display($tpl);
  1295. }
  1296. /* if(!empty(I(C('VAR_JSONP_HANDLER')))){ //ajax返回,默认json格式
  1297. $this->ajaxReturn($data,$type);
  1298. }elseif(IS_AJAX){ //jsonp格式
  1299. //var_dump($data);exit;
  1300. $this->success($data,"成功!",1);
  1301. }elseif(C('RETRUN_FORMAT') == "android_json" || I('ret_format') == 'json'){
  1302. $this->ajaxReturn($data); //android格式数据必须有个[]
  1303. }elseif(isMobile()){
  1304. $this->display("wap".__ACTION__);
  1305. }else{ //html
  1306. $this->display($tpl);
  1307. } */
  1308. }
  1309. /*function success($data,$msg = 'success',$code = 1,$jumpUrl = ''){
  1310. $this->dispatchJump2($data,$msg,$code,$jumpUrl);
  1311. }*/
  1312. /**
  1313. * @param string $firstParam
  1314. * @param string $secondParam
  1315. * @param string $jumpUrl
  1316. * @param bool $ajax
  1317. */
  1318. function success($firstParam='成功',$secondParam='',$jumpUrl='',$ajax=false){
  1319. //第一个参数是数组或对象,说明是app返回,则第二个参数是msg
  1320. if(is_array($firstParam) || is_object($firstParam)){ //api返回
  1321. $data = $firstParam;
  1322. $msg = $secondParam;
  1323. }else{ //pc返回,第一个参数做msg,第二个参数做跳转url
  1324. $data = '';
  1325. $msg = $firstParam;
  1326. $jumpUrl = $secondParam;
  1327. }
  1328. $this->dispatchJump2($data,$msg,1,$jumpUrl,$ajax);
  1329. }
  1330. function error($message='',$jumpUrl='',$ajax=false,$status=0){
  1331. $ret_format = $this->responseFormat();
  1332. if(in_array($ret_format,['json','jsonp']) ){
  1333. $data = [];
  1334. $data['code'] = $status;
  1335. $data['msg'] = $message;
  1336. $data['data'] = (object)array();
  1337. if($jumpUrl) $data['jumpUrl'] = $jumpUrl;
  1338. $this->ajaxReturn($data,$ret_format);
  1339. }
  1340. /*if("json" == $this->responseFormat() || "jsonp" == $this->responseFormat()) $ajax = 1;
  1341. if($ajax || IS_AJAX) {// AJAX提交
  1342. $data = is_array($ajax)?$ajax:array();
  1343. $data['code'] = $status;
  1344. $data['msg'] = $message;
  1345. $data['data'] = (object)array();
  1346. $type = C('ret_format');
  1347. if($type != 'json' && $type != 'jsonp') $type = 'json';
  1348. $this->ajaxReturn($data,$type);
  1349. }*/
  1350. if(is_int($ajax)) $this->assign('waitSecond',$ajax);
  1351. if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
  1352. // 提示标题
  1353. $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  1354. //如果设置了关闭窗口,则提示完毕后自动关闭窗口
  1355. if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
  1356. $this->assign('status',$status); // 状态
  1357. //保证输出不受静态缓存影响
  1358. C('HTML_CACHE_ON',false);
  1359. if($status) { //发送成功信息
  1360. $this->assign('message',$message);// 提示信息
  1361. // 成功操作后默认停留1秒
  1362. if(!isset($this->waitSecond)) $this->assign('waitSecond','1');
  1363. // 默认操作成功自动返回操作前页面
  1364. if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  1365. if(isMobile()){
  1366. $this->display(C('TMPL_ACTION_SUCCESS_WAP'));
  1367. }else{
  1368. $this->display(C('TMPL_ACTION_SUCCESS'));
  1369. }
  1370. }else{
  1371. $this->assign('error',$message);// 提示信息
  1372. //发生错误时候默认停留3秒
  1373. if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
  1374. // 默认发生错误的话自动返回上页
  1375. if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  1376. if(isMobile()){
  1377. $this->display(C('TMPL_ACTION_ERROR_WAP'));
  1378. }else{
  1379. $this->display(C('TMPL_ACTION_ERROR'));
  1380. }
  1381. // 中止执行 避免出错后继续执行
  1382. exit ;
  1383. }
  1384. }
  1385. // $this->dispatchJump2($message,1,$jumpUrl,$ajax);
  1386. function dispatchJump2($data, $msg='', $status = 1, $jumpUrl='', $ajax=false){
  1387. if (!empty(I(C('VAR_JSONP_HANDLER')))){ //jsonp
  1388. if(empty($data)) $data = (object)[];
  1389. $this->ajaxReturn(array("code" =>$status, "msg" => $msg,"data" => $data),'JSONP');
  1390. }
  1391. if(IS_AJAX) {// AJAX和ret_format=json
  1392. if(empty($data)) $data = (object)[];
  1393. $ret = [];
  1394. $ret['code'] = $status;
  1395. $ret['msg'] = $msg;
  1396. $ret['data'] = $data;
  1397. $ret['jumpUrl'] = $jumpUrl;
  1398. $this->ajaxReturn($ret);
  1399. }
  1400. if(is_int($ajax)) $this->assign('waitSecond',$ajax);
  1401. if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
  1402. // 提示标题
  1403. $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  1404. //如果设置了关闭窗口,则提示完毕后自动关闭窗口
  1405. if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
  1406. $this->assign('status',$status); // 状态
  1407. //保证输出不受静态缓存影响
  1408. C('HTML_CACHE_ON',false);
  1409. if($status) { //发送成功信息
  1410. $this->assign('message',$msg);// 提示信息
  1411. if($this->jumpMenu){
  1412. $this->jumpMenu = str_replace('{$info_id}', C('info_id'),$this->jumpMenu);
  1413. $this->assign('jumpMenu',$this->jumpMenu);
  1414. }
  1415. // 成功操作后默认停留1秒
  1416. if(!isset($this->waitSecond)) $this->assign('waitSecond','1');
  1417. // 默认操作成功自动返回操作前页面
  1418. if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  1419. if(isMobile()){
  1420. $this->display(C('TMPL_ACTION_SUCCESS_WAP'));
  1421. }else{
  1422. $this->display(C('TMPL_ACTION_SUCCESS'));
  1423. }
  1424. }else{
  1425. $this->assign('error',$msg);// 提示信息
  1426. //发生错误时候默认停留3秒
  1427. if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
  1428. // 默认发生错误的话自动返回上页
  1429. if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  1430. if(isMobile()){
  1431. $this->display(C('TMPL_ACTION_ERROR_WAP'));
  1432. }else{
  1433. $this->display(C('TMPL_ACTION_ERROR'));
  1434. }
  1435. // 中止执行 避免出错后继续执行
  1436. exit ;
  1437. }
  1438. }
  1439. /**
  1440. * @param string $firstParam data | msg
  1441. * @param string $secondParam msg | jumpUrl
  1442. * @param string $jumpUrl
  1443. * @param bool $ajax
  1444. */
  1445. //function response($firstParam='',$secondParam='',$jumpUrl=''){
  1446. function error_new($msg='',$jumpUrl='',$ajax=false){
  1447. /* //第一个参数是数组或对象,说明是app返回,则第二个参数是msg
  1448. if(is_array($firstParam) || is_object($firstParam)){ //api返回
  1449. $data = $firstParam;
  1450. $msg = $secondParam;
  1451. }else{ //pc返回,第一个参数做msg,第二个参数做跳转url
  1452. $data = '';
  1453. $msg = $firstParam;
  1454. $jumpUrl = $secondParam;
  1455. }*/
  1456. $status = 0;
  1457. $ret_format = $this->responseFormat();
  1458. if(in_array($ret_format,['josn','jsonp']) ){
  1459. $data = [];
  1460. $data['code'] = $status;
  1461. $data['msg'] = $msg;
  1462. $data['data'] = (object)array();
  1463. $this->ajaxReturn($data,$ret_format);
  1464. }
  1465. //保证输出不受静态缓存影响
  1466. C('HTML_CACHE_ON',false);
  1467. $this->assign('error',$msg);// 提示信息
  1468. //发生错误时候默认停留3秒
  1469. if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
  1470. // 默认发生错误的话自动返回上页
  1471. if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  1472. if(isMobile()){
  1473. $this->display(C('TMPL_ACTION_ERROR_WAP'));
  1474. }else{
  1475. $this->display(C('TMPL_ACTION_ERROR'));
  1476. }
  1477. // 中止执行 避免出错后继续执行
  1478. exit ;
  1479. }
  1480. //用户信息
  1481. function userinfo(){
  1482. if(empty($this->user_id)) return;
  1483. $u = M('User');
  1484. $userinfo = $u->find($this->user_id);
  1485. unset($userinfo['id']);
  1486. unset($userinfo['pwd']);
  1487. unset($userinfo['open_id']);
  1488. unset($userinfo['bind']);
  1489. $userinfo = json_encode($userinfo);
  1490. $this->userinfo = $userinfo;
  1491. }
  1492. //设置标题
  1493. function setTitle($title){
  1494. $this->pageTitle = empty($title) ? C('SITE_TITLE') : $title.'_'.C('SITE_TITLE');
  1495. //$title && $title = $title."_";
  1496. //$this->pageTitle = $title.C('SITE_TITLE');
  1497. }
  1498. //验证码
  1499. public function createVerifyCode(){
  1500. $Verify = new \Think\Verify();
  1501. $Verify->entry();
  1502. }
  1503. function setParam($key,$value){
  1504. $_REQUEST[$key] = $_POST[$key] = $_GET[$key] = $value;
  1505. }
  1506. }