Frontend to backend ThinkPHP development site (2), backend thinkphp
The ThinkPHP version I used this time is 3.2.3, and a pop-up layer plug-in called layer will be used. The official website address is http://layer.layui.com /. If you don't talk nonsense, go to the QR code stage.
1. General Method Writing
This is the backend public method. Now we will write two methods for the time being, and then we will continue to add more public methods if necessary.
<? Php/*** return JSON data */function jsonResult ($ status, $ message, $ data) {$ result = array ('status' => $ status, 'message' => $ message, 'data' => $ data); exit (json_encode ($ result ));} /*** MD5 encryption password */function getMd5Password ($ password) {return md5 ($ password. C ('md5 _ pre') ;}?>
Common pop-up JS method Encapsulation
Var dialog = {/*** error pop-up layer * @ param {String} content */error: function (message) {layer. open ({content: message, icon: 2, title: 'error message '});}, /*** successfully popped up the layer * @ param {String} content * @ param {String} jump address */success: function (message, url) {layer. open ({content: message, icon: 1, yes: function () {location. href = url ;}}) ;},/*** confirm the pop-up layer * @ param {String} content * @ param {String} jump address */confirm: function (message, url) {layer. open ({content: message, icon: 3, btn: ['yes', 'no'], yes: function () {location. href = url ;}}) ;},/*** no need to jump to the confirmation pop-up layer of the specified page * @ param {string} content */toconfirm: function (message) {layer. open ({content: message, icon: 3, btn: ['OK']}) ;},/*** load Layer */load: function () {var index = layer. load (1, {shade: [0.6, '# 000'] // 0.1 transparent white background}); return index ;}}
2. Logon:
The background user operation class is added to the Model layer and is mainly used for some data operations.
<? Phpnamespace Common \ Model; use Think \ Model;/*** background user operation class */class AdminModel extends Model {private $ _ db = null; public function _ construct () {$ this-> _ db = M ('admin ');} /*** obtain user information by user name * $ username string username */public function getAdminByUserName ($ username = '') {$ ret = $ this-> _ db-> where ("user_name = '{$ username}'")-> find (); return $ ret ;} /*** update data based on adminid * $ id int id * $ data object data to be updated */public function updat EByAdminId ($ id, $ data) {if (! $ Id |! Is_numeric ($ id) {throw_exception ("ID is invalid");} if (! $ Data |! Is_array ($ data) {throw_exception ('invalid updated data');} return $ this-> _ db-> where ("admin_id = {$ id }"). save ($ data) ;}}?>
Backend implementation logic of the logon Function
<? Phpnamespace Admin \ Controller; use Think \ Controller; class LoginController extends Controller {public function index () {if (session ('adminuser ')) {$ this-> redirect ('/admin. php? C = Index') ;}$ this-> display ();} public function check () {$ username =$ _ POST ['username']; $ password = $ _ POST ['Password']; if (! Trim ($ username) {return jsonResult (0, 'user name cannot be blank ');} if (! Trim ($ password) {return jsonResult (0, 'password cannot be blank ');} $ ret = D ('admin')-> getAdminByUsername ($ username ); if (! Ret | $ ret ['status']! = 1) {return jsonResult (0, 'this user does not exist');} if ($ ret ['Password']! = GetMd5Password ($ password) {return jsonResult (0, 'user name or password error');} D ("Admin ") -> updateByAdminId ($ ret ['admin _ id'], array ('Last _ login_time '=> time (); session ('adminuser', $ ret ); return jsonResult (1, 'logon successful ') ;}}?>
Frontend JS login logic implementation
Var login = {check: function () {// obtain the username and password var username =$ ('input [name = "username"] ') on the logon page. val (), password = $ ('input [name = "password"] '). val (); if (! Username) {dialog. error ('user name cannot be blank ');} if (! Password) {dialog. error ('password cannot be blank ');} var url = "/index. php? M = admin & c = login & a = check ", data = {" username ": username," password ": password}; var load = dialog. load (); $. post (url, data, function (result) {layer. close (load); if (result. status = 0) {return dialog. error (result. message);} if (result. status = 1) {return dialog. success (result. message, '/admin. php? C = Index') ;}}, 'json ');}}
Today, we have simply done this. At the beginning of the project, it takes a long time to build the wheel. After the wheel is built, the car can be started quickly! (Too many requests) then """
Source Code address: https://github.com/YoZiLin/TP-CMS