• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

vue和php-前后台交互

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

前端主要代码:

<template>
    <div class="main-member-info">
    <form @submit.prevent="submit">
        <input type="text" v-model="userName" placeholder="请输入你的姓名"  class="name-style" maxlength="20"/>
        <div class="sex">
            <select v-model="selectedSex">
                <option>boy</option>
                <option  selected = "selected">girl</option>
            </select>
        </div> 
            <input type="text" v-model="phoneNumber" placeholder="请输入你的手机号"  class="phone-number-style"/>
        <div class="guide-style">
            <select v-model="selectedGuild">
                <option selected="selected" value="">请选择一个导购: </option>
                <option v-for="guild in guilds" value ="{{ guild.name }}">{{ guild.name }}</option>
            </select>
        </div>
        <input type="submit" value ="注册" class="register-style"/>
    </form>       
    </div>        
</template>
<script>
import {setDocumentTitle, showToast, checkTel} from '../../../../../static/h5/utils/interaction'
export default
    {
    data () {
        return {
        userName: '',
        selectedSex: '',
        phoneNumber: '',
        guilds: [],
        selectedGuild: ''
        }
    },
    ready () {
        this.getAllEmployees()
        setDocumentTitle('会员注册')
    },
    methods: {
        getAllEmployees () {
        let _this = this
        let params = {}
        // /omnisocials-backend/src/backend/modules/main/controllers/EmployeeController.php(actionSelectEmployee())
        this.$resource('main/employee/select-employee', params).get().then((resp) => {
            _this.guilds = _this.guilds.concat(resp.data.items)
        }, (resp) => {
            showToast('导购导出失败,请稍后再试')
        })
        },
        submit () {
        if (!this.validation()) {
            return
        }
        let _this = this
        let params = {
            userName: this.userName,
            selectedSex: this.selectedSex,
            phoneNumber: this.phoneNumber,
            selectedGuild: this.selectedGuild
        }
        this.$resource('main/employee/register-member').save(params).then((resp) => {
            _this.errorMessages = resp.data.item
            showToast(_this.errorMessages)
        }, (resp) => {
            showToast('注册失败!')
        })
        },
        validation () {
        let name = ''
        let tel = ''
        if (!this.userName) {
            showToast('请输入姓名!')
            return false
        }

        if (!this.phoneNumber) {
            showToast('请输入手机号码!')
            return false
        }

        if (!checkTel(this.phoneNumber)) {
            showToast('手机号格式不正确')
            return false
        }

        if (!this.selectedGuild) {
            showToast('请选择导购!')
            return false
        }

        return true
        }
    }
    }
</script>
<style lang="less">
@import './registermember.less';
</style>

后台主要代码:

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

<?php
namespace backend\modules\main\models;

use backend\components\BaseModel;
class MyEmployee extends BaseModel
{
    public static function collectionName()
    {
        return 'employee';
    }
    public function attributes()
    {
    return ['name', 'gender', 'phone'];
    }
    public function safeAttributes()
    {
    return ['name', 'gender', 'phone'];
    }
    public function fields()
    {
    return ['name', 'gender', 'phone'];
    }
    public static function getEmployeeList() 
    {
        $employees=self::find()->all();
        return $employees;
    } 
}

实体类:MyRegisterMember.php

<?php
namespace backend\modules\main\models;

use backend\components\BaseModel;
class MyRegisterMember extends BaseModel
{
    public static function collectionName()
    {
        return 'member';
    }
    public function attributes()
    {
        return array_merge(
            parent::attributes(), ['name', 'gender', 'phone', 'guild']);
    }
    public function safeAttributes()
    {
        return array_merge(
            parent::attributes(), ['name', 'gender', 'phone', 'guild']);
    }
    public function fields()
    {
        return array_merge(
            parent::attributes(), ['name', 'gender', 'phone', 'guild']);
    }
}
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php类的自动加载发布时间:2022-07-10
下一篇:
Minor【PHP框架】3.路由、控制器、视图发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap