vue和php-前后台交互

vue和php-前后台交互

前端主要代码:




后台主要代码:

1. 导入两个实体类:

use backend\modules\main\models\MyEmployee;

use backend\modules\main\models\MyRegisterMember;

2. controller 主要代码

class EmployeeController extends BaseController
{
    // 不需要验证的方法
    public $noAuthActions = ['select-employee', 'register-member'];

    //从数据库导出导购方法(多个导购在前台形成列表)
    public function actionSelectEmployee() {
     LogUtil::info('in controller, get guilds-employees', 'main'); 
     $employees = [];
     $employees = MyEmployee::getEmployeeList();
     //LogUtil::info('in controller, finish query', 'main'); 
     //LogUtil::info( $employees, 'main'); 
     return ['items' => $employees]; 
     }

    //注册会员方法
    public function actionRegisterMember() {
    $params = $this->getParams();
    LogUtil::info('in controller, register member', 'main', ['user' => $params]);

    $MyRegisterMember = new MyRegisterMember();
    if (empty($params['userName'])) {
         return $this->generateResult(5001, '请填写姓名');
    }
    if (empty($params['selectedSex'])) {
        return $this->generateResult(5002, '请选择性别');
    }
    if (empty($params['phoneNumber'])) {
        return $this->generateResult(5003, '请填写电话');
    }
    if (empty($params['selectedGuild'])) {
        return $this->generateResult(5004, '请选择导购');
    }
    $MyRegisterMember->name = $params['userName'];
    $MyRegisterMember->gender = $params['selectedSex'];
    $MyRegisterMember->phone = $params['phoneNumber'];
    $MyRegisterMember->guild = $params['selectedGuild'];
    $MyRegisterMember->save();
    if ($MyRegisterMember->errors)
    {
        return self::generateResult(300, '对不起,服务器错误,注册失败');
    } else {
        return self::generateResult(200, '注册成功!');
        }  
    }

    private static function generateResult($errorNumber, $errorMessage)
    {
        LogUtil::info('in controller, register member info', 'main', ['message' => $errorMessage]);
        return ['item' => $errorMessage];
    }
    // public function generateResult($number, $errorMessage){
    //     $errorMessages = [];
    //     array_push($errorMessages, self::getErrorMessages($number, $errorMessage));
    //     return ['item' => $errorMessages];
    // }
}

实体类:MyEmployee.php

all();
        return $employees;
    } 
}

实体类:MyRegisterMember.php

 

转载于:https://www.cnblogs.com/sinceForever/p/7643352.html

你可能感兴趣的:(vue和php-前后台交互)