Frontend to backend ThinkPHP development site (3), backend thinkphp

Source: Internet
Author: User

Frontend to backend ThinkPHP development site (3), backend thinkphp

I continued the development of this project in the third night, with little time. I didn't write much code tonight. I still wrote a public controller and a public JS on the wheel tonight. Go directly to the code!

The following is a public controller that inherits from the backend controller. The constructor verifies whether the current user is logged on and provides quick access to the data of the current user.

<? Phpnamespace Admin \ Controller; use Think \ Controller;/*** background management public Controller */class CommonController extends Controller {public function _ construct () {parent :: __construct (); $ this-> _ init ();}/*** initialize */private function _ init () {// if you have logged on to $ isLogin = $ this-> isLogin (); if (! $ IsLogin) {// jump to the logon page $ this-> redirect ('/admin. php? C = login ') ;}}/*** get current logon user information */public function getLoginUser () {return session ('adminuser ');} /*** determine whether to log on */public function isLogin () {$ user = $ this-> getLoginUser (); return ($ user & is_array ($ user ));} /*** update data status */public function setStatus ($ data, $ models) {try {if ($ _ POST) {$ id = $ data ['id']; $ status = $ data ['status']; if (! $ Id) {return jsonResult (0, 'Id does not exist');} $ ret = D ($ models)-> updateStatusById ($ ID, $ status); if ($ ret) {return jsonResult (1, 'Operation succeeded ');} else {return jsonResult (0, 'Operation failed') ;}} return jsonResult (0, 'content not submitted ');} catch (Exception $ ex) {return jsonResult (0, $ e-> getMessage ());}} /*** data sorting */public function listorder ($ model = '') {$ listorder =$ _ POST ['liststorder']; $ jumpUrl = $ _ SERVER ['HTTP _ referer']; $ errors = array (); $ resultData = array ('Dump _ url' => $ jumpUrl); try {if ($ listorder) {foreach ($ listorder as $ id => $ v) {$ id = D ($ model) -> updateListorderById ($ id, $ v); if ($ id = FALSE) {$ errors [] = $ id ;}} if (array_count_values ($ errors)> 0) {$ group = implode (',', $ errors); return jsonResult (0, "sorting failed-{$ group}", $ data, $ resultData );} return jsonResult (1, 'sorted successfully', $ resultData) ;}} catch (Exception $ ex) {return jsonResult (0, $ ex-> getMessage ());} return jsonResult (0, 'sorting failed ', $ ResultData) ;}}?>

In the following section, JS is mainly used to perform some form operations.

Var common = function (queryDom) {if (! QueryDom) {console. error ('specify the DOM to be operated '); return;} function commonObj () {this. dom = '';} function todelete (url, data) {$. post (url, data, function (s) {if (s. status = 1) {return dialog. success (s. message, ''); // jump to the relevant page} else {return dialog. error (s. message) ;}}, "JSON") ;}/ *** submit form operation */commonObj. prototype. add = function (formDom, func) {$ (this. dom ). click (function () {var data = $ (formDom ). serializeArray (); PostData ={}; $ (data ). each (function (I) {postData [this. name] = this. value;}); console. log (postData); // post the obtained data to the server url = SCOPE. save_url; jump_url = SCOPE. jump_url; $. post (url, postData, function (result) {if (result. status = 1) {// successful if (typeof (func) = 'function') {func ();} else {return dialog. success (result. message, jump_url) ;}} else if (result. status = 0) {// return dialog failed. error (result. message) ;}}, "JSON") ;};}/*** edit module */commonObj. prototype. click = function () {$ (this. dom ). on ('click', function () {var id = $ (this ). attr ('attr-id'); var url = SCOPE. edit_url + '& id =' + id; window. location. href = url;});}/** delete operation */commonObj. prototype. delete = function () {$ (this. dom ). on ('click', function () {var id = $ (this ). attr ('attr-id'); var a = $ (this ). attr ("attr-a"); var message = $ (this ). attr ("attr-mess Age "); var url = SCOPE. set_status_url; data ={}; data ['id'] = id; data ['status'] =-1; layer. open ({type: 0, title: 'submit? ', Btn: ['yes', 'no'], icon: 3, closeBtn: 2, content: "OK" + message, scrollbar: true, yes: function () {// execute the relevant jump todelete (url, data) ;},}) ;}/ *** sort operation */commonObj. prototype. order = function () {$ (this. dom ). click (function () {// get the listorder content var data =$ ("# singcms-listorder "). serializeArray (); postData ={}; $ (data ). each (function (I) {postData [this. name] = this. value;}); console. log (data); var url = SCOPE. l Istorder_url; $. post (url, postData, function (result) {if (result. status = 1) {// return dialog. success (result. message, result ['data'] ['dump _ url']);} else if (result. status = 0) {// return dialog failed. error (result. message, result ['data'] ['dump _ url']) ;}, "JSON") ;}/ *** change status */commonObj. prototype. updateStatus = function () {$ (this. dom ). on ('click', function () {var id = $ (this ). attr ('attr-id'); var stat Us = $ (this ). attr ("attr-status"); var url = SCOPE. set_status_url; data ={}; data ['id'] = id; data ['status'] = status; layer. open ({type: 0, title: 'submit? ', Btn: ['yes', 'no'], icon: 3, closeBtn: 2, content: "Are you sure you want to change the status", scrollbar: true, yes: function () {// execute the relevant jump todelete (url, data) ;},}) ;}) ;}commonobj. prototype. push = function () {$ (this. dom ). click (function () {var id = $ ("# select-push "). val (); if (id = 0) {return dialog. error ("select recommended bit");} push ={}; postData ={}; $ ("input [name = 'pushcheck']: checked "). each (function (I) {push [I] = $ (this ). val () ;}); postData ['push'] = push; postData ['position _ id'] = id; // console. log (postData); return; var url = SCOPE. push_url; $. post (url, postData, function (result) {if (result. status = 1) {// TODOreturn dialog. success (result. message, result ['data'] ['dump _ url']);} if (result. status = 0) {// TODOreturn dialog. error (result. message) ;}}, "json") ;}}return new commonObj ();}

It's just a little bit tonight. It's just a little bit slow, and no one will remind me of the progress of the project. The main reason is that I can finish it myself. The Code was written here, so it was so difficult to run it that it would certainly have some bugs. Let's debug the code after a module has been developed!

Source Code address: https://github.com/YoZiLin/TP-CMS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.