关于php:国内外多语言版盲盒网站开发建设-第二篇

53次阅读

共计 3867 个字符,预计需要花费 10 分钟才能阅读完成。


接着后面的第一篇,往下面写,这个是国内外多语言版盲盒网站零碎开发建设 第二篇,间接进入正题了。

退出登录

public function logout()

{
    // 退出本站
    $this->auth->logout();
    $this->success(__('Logout successful'), url('user/index'));
}

/**
     * 个人信息
     */
    public function profile()
    {
        $this->view->assign(‘title’, __(‘Profile’));
        return $this->view->fetch();
    }

批改明码

public function changepwd()

{if ($this->request->isPost()) {$oldpassword = $this->request->post("oldpassword");
        $newpassword = $this->request->post("newpassword");
        $renewpassword = $this->request->post("renewpassword");
        $token = $this->request->post('__token__');
        $rule = [
            'oldpassword'   => 'require|length:6,30',
            'newpassword'   => 'require|length:6,30',
            'renewpassword' => 'require|length:6,30|confirm:newpassword',
            '__token__'     => 'token',
        ];

        $msg = ['renewpassword.confirm' => __('Password and confirm password don\'t match')
        ];
        $data = [
            'oldpassword'   => $oldpassword,
            'newpassword'   => $newpassword,
            'renewpassword' => $renewpassword,
            '__token__'     => $token,
        ];
        $field = ['oldpassword'   => __('Old password'),
            'newpassword'   => __('New password'),
            'renewpassword' => __('Renew password')
        ];
        $validate = new Validate($rule, $msg, $field);
        $result = $validate->check($data);
        if (!$result) {$this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
            return false;
        }

        $ret = $this->auth->changepwd($newpassword, $oldpassword);
        if ($ret) {$this->success(__('Reset password successful'), url('user/login'));
        } else {$this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
        }
    }
    $this->view->assign('title', __('Change password'));
    return $this->view->fetch();}

设置过滤办法

public function attachment()

{
    // 设置过滤办法
    $this->request->filter(['strip_tags']);
    if ($this->request->isAjax()) {$mimetypeQuery = [];
        $where = [];
        $filter = $this->request->request('filter');
        $filterArr = (array)json_decode($filter, true);
        if (isset($filterArr['mimetype']) && preg_match("/[]\,|\*]/", $filterArr['mimetype'])) {$this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
            $mimetypeQuery = function ($query) use ($filterArr) {$mimetypeArr = explode(',', $filterArr['mimetype']);
                foreach ($mimetypeArr as $index => $item) {if (stripos($item, "/*") !== false) {$query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
                    } else {$query->whereOr('mimetype', 'like', '%' . $item . '%');
                    }
                }
            };
        } elseif (isset($filterArr['mimetype'])) {$where['mimetype'] = ['like', '%' . $filterArr['mimetype'] . '%'];
        }

        if (isset($filterArr['filename'])) {$where['filename'] = ['like', '%' . $filterArr['filename'] . '%'];
        }

        if (isset($filterArr['createtime'])) {$timeArr = explode('-', $filterArr['createtime']);
            $where['createtime'] = ['between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
        }

        $model = new Attachment();
        $offset = $this->request->get("offset", 0);
        $limit = $this->request->get("limit", 0);
        $total = $model
            ->where($where)
            ->where($mimetypeQuery)
            ->where('user_id', $this->auth->id)
            ->order("id", "DESC")
            ->count();

        $list = $model
            ->where($where)
            ->where($mimetypeQuery)
            ->where('user_id', $this->auth->id)
            ->order("id", "DESC")
            ->limit($offset, $limit)
            ->select();
        $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
        foreach ($list as $k => &$v) {$v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
        }
        unset($v);
        $result = array("total" => $total, "rows" => $list);

        return json($result);
    }
    $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
    return $this->view->fetch();}

}

Ajax 异步申请接口

class Ajax extends Frontend
{

protected $noNeedLogin = ['lang', 'upload'];
protected $noNeedRight = ['*'];
protected $layout = '';

加载语言包

public function lang()

{header('Content-Type: application/javascript');
    header("Cache-Control: public");
    header("Pragma: cache");

    $offset = 30 * 60 * 60 * 24; // 缓存一个月
    header("Expires:" . gmdate("D, d M Y H:i:s", time() + $offset) . "GMT");

    $controllername = input("controllername");
    $this->loadlang($controllername);
    // 强制输入 JSON Object
    $result = jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
    return $result;
}

 上传文件
 
   

public function upload()
    {
        return action(‘api/common/upload’);
    }

}

正文完
 0