作者 何书鹏
1 个管道 的构建 通过 耗费 1 秒

生成战队二维码

... ... @@ -4,6 +4,7 @@ namespace app\admin\controller;
use app\common\controller\Backend;
use app\admin\model\TeamCategory;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Exception;
use think\exception\PDOException;
... ... @@ -84,6 +85,54 @@ class Team extends Backend
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
// 生成二维码
$qrcode = $this->createQrcode($this->model->id);
$this->model->save(['team_qrcode' => '/'.$qrcode]);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 参与赛事
*/
public function match()
... ... @@ -233,4 +282,35 @@ class Team extends Backend
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
/**
* 生成小程序二维码
*/
public function createQrcode($id)
{
// 本地路径
$dir = 'uploads/team';
if (!file_exists($dir)){
mkdir($dir,0777,true);
}
// 用户小程序码
$qrcode = $dir.'/'.$id.'.png';
$qrcode_width = 430;
if(!file_exists($qrcode) || imagesx(imagecreatefromjpeg(ROOT_PATH.'public/'.$qrcode)) != $qrcode_width){
// 获取小程序配置
$miniprogram = [
'mini_program' => \config('miniprogram.basic')
];
$app = new Application($miniprogram);
$miniProgram = $app->mini_program;
$response = $miniProgram->qrcode->appCodeUnlimit($id, 'pages/index/index', $qrcode_width);
$contents = $response->__toString();
if (empty($contents) || '{' === $contents[0]) {
$this->error('Invalid media response content.');
}
file_put_contents($qrcode, $contents);
}
return $qrcode;
}
}
... ...
... ... @@ -30,7 +30,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
{field: 'teamcategory.name', title: __('Teamcategory.name'), searchList: Config.categoryList},
{field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.toggle},
{field: 'is_wxjoin', title: __('Is_wxjoin'), searchList: {"0":__('Is_wxjoin 0'),"1":__('Is_wxjoin 1')}, formatter: Table.api.formatter.toggle},
{field: 'team_qrcode', title: __('Team_qrcode')},
{field: 'team_qrcode', title: __('Team_qrcode'), events: Table.api.events.image, formatter: Table.api.formatter.image},
{field: 'user_num', title: __('User_num')},
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
//操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
... ...