Eugrade 批量用户注册功能/全局汉化上线

Posted ·859 Views·4456 Words

背景

咳咳...主要是鸽太久没更新了,一个月之后来水一篇文章吧...

最近 Eugrade 被推广到一个普通初中了,大概有两个班 90 个人左右使用 (主要是看上了 Grades 成绩管理系统)

需要批量注册成员账户并加入班级的需求就出现了,满足他们 🙂

好像一直没放过体验地址:https://www.eugrade.com

 

代码

//提交批量用户注册
        handle_multi_submit() {
            if (!this.multi.data_status) {
                this.multi.names = '';
                this.multi.names_check = true;

                //获取全部姓名
                $('input[name=multi_member]').each(function () {
                    var temp_name = $(this).val();
                    temp_name = temp_name.split("|").join(""); //删除分隔符
                    if (temp_name !== '') { //删除分隔符后仍剩其他字符
                        antd.multi.names = antd.multi.names + '|' + temp_name;
                    } else {
                        antd.multi.names_check = false;
                    }
                });

                if (this.multi.names_check) {

                    //获取全部用户昵称
                    this.multi.names = this.multi.names.substr(1, (this.multi.names).length);

                    var query_string = "pwd=" + this.multi.pwd + "&names=" + this.multi.names + "&class_id=" + this.opened_class_info.id + "&user_id=" + this.user.id;

                    //提交请求
                    axios.post(
                            '../interact/multi_create_member.php',
                            query_string
                        )
                        .then(res => {
                            if (res.data.status) {
                                //获取注册成功的用户名+邮箱与失败的用户名
                                this.multi.return.fails = res.data.info.fails;
                                this.multi.return.successes = res.data.info.successes;
                                this.multi.data_status = true;
                                this.$message.success(res.data.mes);
                                this.open_class_info(this.opened_class_info.index);
                            } else {
                                this.multi.data_status = false;
                                this.multi.confirm_multi_loading = false;
                                this.multi.count = 0;
                                this.multi.names = '';
                                this.multi.names_check = true;
                                this.multi.pwd = '';
                                this.multi.return.fails = [];
                                this.multi.return.successes = [];
                                this.$message.error(res.data.mes);
                            }
                        })


                } else {
                    antd.$message.error('Invaild nickname, please avoid using special characters');
                }
            } else {
                this.handle_multi_cancel();
            }
        },

↑ Vue.js 代码

 

<!-- 批量成员添加 -->
    <a-modal :title="lang.tab[5]" :visible="multi.visible" @ok="handle_multi_submit" :confirm-loading="multi.confirm_multi_loading" @cancel="handle_multi_cancel">
        <template v-if="!multi.data_status">
            <p v-html="lang.tab[6]"></p>
            <a-input placeholder="Password" v-model="multi.pwd">
                <a-icon slot="prefix" type="key" />
            </a-input>
            <br />
            <div style="display:flex;margin: 15px 0;">
                <a-button type="primary" @click="add_multi_member" style="width: 49%;margin-right: 2%;">+ Add</a-button>
                <a-button @click="remove_multi_member" style="width: 49%;">- Remove</a-button>
            </div>
            <div v-for="(count,index) in multi.count" style="margin-bottom:10px">
                <a-input :placeholder="'Nickname ' + (index + 1)" name="multi_member">
                    <a-icon slot="prefix" type="user" />
                </a-input>
            </div>
        </template>
        <template v-else>
            <div v-if="!!multi.return.fails">
                <h2 style="margin-bottom:0px">Fails</h2>
                <p>Please change the following nickname(s):</p>
                <div v-for="f in multi.return.fails" class="multi-list">
                    <p><b>{{ f.name }}</b></p>
                </div>
            </div>
            <div v-if="!!multi.return.successes">
                <h2 style="margin-bottom:0px">Successes</h2>
                <p>Accounts of the following nickname(s) have been generated and added to this class:</p>
                <div v-for="s in multi.return.successes" class="multi-list">
                    <p><b>{{ s.name }}</b> | Email: <b>{{ s.email }}</b> | PWD: <b>{{ multi.pwd }}</b></p>
                </div>
            </div>
        </template>
    </a-modal>
    <!-- 批量成员添加 -->

↑ HTML 代码

 

$name_array = explode('|', $names);
                $status = true;

                //转换昵称为拼音,存入数组
                for ($i = 0; $i < count($name_array); $i++) {
                    if (empty($name_array[$i])) {
                        $status = false;
                    } else {
                        $names_array[] = Pinyin::getPinyin($name_array[$i]);
                    }
                }

                if ($status) {

                    for ($j = 0; $j < count($names_array); $j++) {

                        /* 注册用户 */
                        //用户信息
                        $count_n = 0;
                        $count_e = 0;
                        $name = $name_array[$j];
                        //原始输入用户名
                        $name_origin = $name_array[$j];
                        $email = $names_array[$j] . '@eugrade.com';
                        //判断用户名重复
                        if (!$check->name($name)) {
                            while (!$check->name($name) && $count_n <= 10) {
                                $count_n++;
                                $name = $name_array[$j] . rand(0, 999);
                            }
                        }
                        //判断邮箱重复
                        if (!$check->email($email)) {
                            while (!$check->email($email) && $count_e <= 10) {
                                $count_e++;
                                $email = $names_array[$j] . rand(0, 999) . '@eugrade.com';
                            }
                        }

                        //尝试 10 次添加随机数生成唯一用户名+邮箱失败
                        if ($count_e > 10 || $count_n > 10) {
                            $false[] = $name_origin; //保存注册失败的用户名
                        } else {
                            $pwd = (string) $pwd;
                            $this_id = Lazer::table('users')->lastId() + 1;

                            //存入数据库
                            $row = Lazer::table('users');
                            $row->id = $this_id;
                            $row->name = $name;
                            $row->email = $email;
                            $row->pwd = md5(md5($pwd) . md5($pwd));
                            $row->class = (string) $id;
                            $row->type = 1;
                            $row->avatar = 'https://static.ouorz.com/default_avatar.png';
                            $row->date = time();
                            $row->save();

                            //储存注册数据
                            $return_array[$j]['name'] = $name_origin;
                            $return_array[$j]['email'] = $email;
                            /* 注册用户结束 */

                            /* 加入班级 */
                            $class = Lazer::table('classes')->limit(1)->where('id', '=', (int) $id)->find();
                            $class->set(array(
                                'member' => $class->member . ',' . (string) $this_id
                            ));
                            $class->save();
                            /* 加入班级结束 */
                        }
                    }

                    //存在注册失败的用户
                    if (!!$false) {
                        $status = 1;
                        $code = 126;
                        $info = array(
                            'fails' => $false,
                            'successes' => $return_array
                        );
                        $mes = 'Successfully created some members, but there are still some errors...';
                    } else {
                        //全部注册成功
                        $status = 1;
                        $code = 127;
                        $info = array(
                            'successes' => $return_array
                        );
                        $mes = 'Successfully created the members';
                    }
                } else {
                    $status = 0;
                    $code = 128;
                    $mes = 'System error';
                }

↑ PHP 代码

 

后记

好啦,任务完成 🙂

那我继续回去准备升学季了,再有个一两个月不更新也是有可能的。顺便一提,雅思成绩终于过关了,耶!

Comments

Leave a comment to join the discussion