| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- <?php
- class CommonController extends Controller
- {
- public function actionSendCode()
- {
- $phone = Helper::getPostString('phone', '');
- if (!Helper::isPhone($phone)) {
- Helper::error('手机号码格式错误');
- }
- // 验证码发送限制
- Helper::dealCommonResult(Helper::limitSmsSend(10, $phone, 5), false);
- if (!DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'))) {
- Helper::error('该手机号用户不存在');
- }
- $code = (string)random_int(100000,999999);
- RedisInstance::getInstance()->set('user_code:'.$phone, $code, 600);
- // 发送短信
- Helper::dealCommonResult(SMS::getInstance()->send($phone, '2094847', [$code]));
- }
- public function actionJson()
- {
- $newData = [];
- $data = file_get_contents(PROJECT_PATH . '/runtime/city.json');
- foreach (json_decode($data, true) as $province) {
- $newData[] = ['label' => $province['province'], 'value' => $province['province'], 'children' => array_map(function($city) {
- return ['label' => $city['city'], 'value' => $city['city'], 'children' => array_map(function($area) {
- return ['label' => $area['area'], 'value' => $area['area']];
- }, $city['areas'])];
- }, $province['citys'])];
- }
- echo json_encode($newData, JSON_UNESCAPED_UNICODE);
- }
- public function actionEdit()
- {
- $table = Helper::getGetString('t1');
- if (!$table) {
- Helper::error('参数错误');
- }
- $table = DB::formTableName($table);
- $sql = "show full columns from {$table}";
- $taleInfo = \Yii::app()->db->createCommand($sql)->queryAll();
- if (!$taleInfo) {
- Helper::error('该表不存在');
- }
- $excludeField = ['create_date', 'update_date', 'is_delete', 'id', 'city', 'area', 'province'];
- $this->getEditHtml($taleInfo, $excludeField);
- }
- public function getEditHtml(array $tableFieldArr, array $excludeField = []): void
- {
- $data = [];
- $notNullField = [];
- $allowEmptyField = [];
- echo '$data = [<br/>';
- foreach ($tableFieldArr as $column) {
- if (in_array($column['Field'], $excludeField)) {
- continue;
- }
- $name = $column['Field'];
- $tmpl = "'{$name}' => Helper::{{method}}('{$name}'),<br/>";
- if (Helper::hasAnyString($column['Type'], ['decimal', 'double', 'float'])) {
- $method = 'getPostFloat';
- } elseif (Helper::hasAnyString($column['Type'], ['int'])) {
- $method = 'getPostInt';
- } elseif (Helper::hasAnyString($column['Type'], ['datetime'])) {
- $method = 'getPostDatetime';
- } elseif (Helper::hasAnyString($column['Type'], ['date'])) {
- $method = 'getPostDate';
- } else {
- $method = 'getPostString';
- }
- echo str_replace('{{method}}', $method, $tmpl);
- if ($column['Null'] == 'NO') {
- $notNullField[] = $name;
- }
- if ($column['Default'] != null || $method == 'getPostInt') {
- $allowEmptyField[] = $name;
- }
- }
- echo '];<br/><br/>';
- echo '$notNullField = ' . json_encode($notNullField) . ';<br/>';
- echo '$allowEmptyField = ' . json_encode($allowEmptyField) . ';<br/>';
- }
- public function actionsqlToTs()
- {
- $table = Helper::getGetString('t1');
- if (!$table) {
- Helper::error('参数错误');
- }
- $table = strtolower($table);
- $table1 = DB::formTableName($table);
- $sql = "show full columns from {$table1}";
- $taleInfo = \Yii::app()->db->createCommand($sql)->queryAll();
- if (!$taleInfo) {
- Helper::error('该表不存在');
- }
- $excludeField = ['create_date', 'update_date', 'is_delete'];
- echo $this->getTsInterFace($table, $taleInfo, $excludeField);
- // echo $this->getTableHtml($taleInfo, $excludeField);
- $editTmpl = file_get_contents(PROJECT_PATH . '/protected/runtime/edit.tmpl');
- $fromExcludeField = ['create_date', 'update_date', 'is_delete', 'id', 'city', 'area', 'province'];
- echo $this->getTsInterFace($table, $taleInfo, $fromExcludeField);
- $formDefault = $this->getFormDefault($taleInfo, $fromExcludeField);
- list($template, $rule) = $this->getFormAndRule($table, $taleInfo, $fromExcludeField);
- $editTmpl = str_replace(
- ['{{template}}', '{{table}}', '{{formDefault}}', '{{formRule}}', '{{ucTable}}'],
- [$template, $table, $formDefault, $rule, ucfirst($table)],
- $editTmpl
- );
- $filePath = PROJECT_PATH . "/web/src/views/{$table}/edit.vue";
- file_put_contents($filePath, $editTmpl);
- // 格式化代码
- echo "<h4>命令后执行下面代码</h4>";
- echo "<p style='color: red;'>npx prettier --write $filePath</p>";
- }
- /**
- * 生成ts接口
- * @param string $table
- * @param array $tableFieldArr
- * @param array $excludeField 排除的字段
- * @return string
- */
- private function getTsInterFace(string $table, array $tableFieldArr, array $excludeField = []): string
- {
- $content = '';
- foreach ($tableFieldArr as $column) {
- if (in_array($column['Field'], $excludeField)) {
- continue;
- }
- $type = $this->getTypeByColumn($column);
- $temp = $column['Field'] . ": {$type}";
- $comment = $column['Comment'] ? " // {$column['Comment']}" : '';
- $content.= $temp . " {$comment}, <br/>";
- }
- $template = <<<typescript
- interface {$table} {<br/>
- $content}<br/><br/>
- typescript;
- return $template;
- }
- private function getTypeByColumn(array $column) :string
- {
- $type = 'string';
- if (str_contains($column['Field'], 'ids')) {
- $type = 'number[]';
- } elseif (Helper::hasAnyString($column['Type'], ['int', 'decimal', 'double', 'float'])) {
- if (Helper::hasAnyString($column['Field'], ['is_', 'can_', 'has_', 'status', '_status', 'type'])) {
- $type = '0|1';
- } else {
- $type = 'number';
- }
- }
- return $type;
- }
- /**
- * 生成form默认值
- * @param array $tableFieldArr
- * @param array $excludeField 排除的字段
- * @return string
- */
- private function getFormDefault(array $tableFieldArr, array $excludeField = []): string
- {
- $content = '';
- foreach ($tableFieldArr as $column) {
- if (in_array($column['Field'], $excludeField)) {
- continue;
- }
- $type = $this->getTypeByColumn($column);
- if (in_array($type, ['number', '0|1'])) {
- $default = $column['Default'] ? : 0;
- $content.= $column['Field'] . ": {$default},";
- } else {
- $default = $column['Default'] ? : '';
- $content.= $column['Field'] . ": '{$default}',";
- }
- }
- return $content;
- }
- /**
- * 生成form验证规则 前后端需要保持一致
- * @param string $table
- * @param array $tableFieldArr
- * @param array $excludeField 排除的字段
- * @return string
- */
- private function getFormAndRule(string $table, array $tableFieldArr, array $excludeField = []): array
- {
- $html = '';
- $ruleHtml = '';
- foreach ($tableFieldArr as $column) {
- if (in_array($column['Field'], $excludeField)) {
- continue;
- }
- $comment = explode(' ', $column['Comment'])[0];
- $rule = [];
- if ($column['Null'] == 'NO') {
- $rule[] = "{ required: true, message: '请输入{$comment}', trigger: 'blur' }";
- }
- $type = $this->getTypeByColumn($column);
- if ($type == 'string') {
- // 图片处理
- if (Helper::hasAnyString($column['Field'], ['img', 'image'])) {
- // 单张图片
- } elseif (Helper::hasAnyString($column['Field'], ['images', 'imgs'])) {
- // 多张图片
- } elseif (Helper::hasAnyString($column['Field'], ['_id'])) {
- // select
- } elseif (Helper::hasAnyString($column['Field'], ['ids'])) {
- // 多选框
- } else {
- // input框
- $inputType = 'text';
- $max = $this->getMaxLength($column);
- if ($max) {
- $min = Helper::hasAnyString($column['Field'], ['']) ? 0 : 0;
- if ($min) {
- $rule[] = "{ min: {$min}, max: {$max}, message: '长度在{$min}到{$max}个字符', trigger: 'blur' }";
- } else {
- $rule[] = "{ max: {$max}, message: '长度最多{$max}个字符', trigger: 'blur' }";
- }
- if ($max > 64) {
- $inputType = 'textarea';
- }
- }
- $html.= $this->getInputHtml($comment, $column['Field'], $inputType, $max);
- }
- } elseif ($type == 'number') {
- $html.= $this->getInputHtml($comment, $column['Field'], 'number');
- } elseif ($type == '0|1') {
- $rule = '';
- $html.= $this->getSelectHtml($comment, $column['Field'], $this->getSelectData($column, $type));
- }
- if ($rule) {
- $rule = implode(',', $rule);
- $ruleHtml.= $column['Field'] . ": [{$rule}],";
- }
- }
- $content = <<<typescript
- <template>
- <ElForm ref="formRef" :model="formData" :rules="rules" label-width="auto">
- <el-row :gutter="20">
- {$html}
- <el-col :span="24">
- <ElFormItem label=" " prop="">
- <ElButton type="primary" @click="handleSubmit">提交</ElButton>
- </ElFormItem>
- </el-col>
- </el-row>
- </ElForm>
- </template>
- typescript;
- return [$content, $ruleHtml];
- }
- public function getInputHtml(string $name, string $field, string $type, int $maxLength = 0): string
- {
- $maxLengthHtml = '';
- if ($type != 'number' && $maxLength > 0) {
- $maxLengthHtml = "maxlength='{$maxLength}'";
- }
- $elHtml = $type != 'textarea' ? ':xs="24" :lg="8" :sm="12"' : ':span=24';
- return <<<input
- <el-col {$elHtml}>
- <ElFormItem label="{$name}" prop="{$field}">
- <ElInput v-model="formData.{$field}" $maxLengthHtml type="{$type}"/>
- </ElFormItem>
- </el-col>
- input;
- }
- public function getSelectHtml(string $name, string $field, array $selectData, bool $multi = false): string
- {
- if (!$selectData) {
- return [];
- }
- $data = '';
- foreach ($selectData as $key => $item) {
- if (is_numeric($key)) {
- $data.= "{label: '{$item}', value: {$key}}, ";
- } else {
- $data.= "{label: '{$item}', value: '{$key}'},";
- }
- }
- return <<<select
- <el-col :xs="24" :lg="8" :sm="12">
- <ElFormItem label="{$name}" prop="{$field}">
- <el-select v-model="formData.{$field}" placeholder="请选择">
- <el-option v-for="item in [{$data}]" :key="item.value" :value="item.value" :label="item.label"/>
- </el-select>
- </ElFormItem>
- </el-col>
- select;
- }
- /**
- * 生成table字段配置
- * @param array $tableFieldArr
- * @param array $excludeField 排除的字段
- * @return string
- */
- private function getTableHtml(array $tableFieldArr, $excludeField = []): string
- {
- $tableContent = '';
- foreach ($tableFieldArr as $column) {
- if (in_array($column['Field'], $excludeField)) {
- continue;
- }
- $commentArr = explode(' ', $column['Comment']);
- $JsTableArr = ['prop' => $column['Field'], 'label' => $commentArr[0]];
- // 长字段列表需要添加showOverflowTooltip
- if ($this->getMaxLength($column) || $this->isTextField($column)) {
- $JsTableArr['showOverflowTooltip'] = true;
- }
- if (str_contains($column['Field'], 'ids')) {
- $JsTableArr = []; // TODO:这种一般是tag集合
- } elseif (Helper::hasAnyString($column['Field'], ['is', 'can', 'has', 'status'])) {
- if (count($commentArr) == 3) {
- $arr1 = explode('-', $commentArr[1]);
- $arr2 = explode('-', $commentArr[2]);
- $tempArr[intval($arr1[0])] = $arr1[1];
- $tempArr[intval($arr2[0])] = $arr2[1];
- } else {
- $tempArr = [0 => '否', 1 => '是'];
- }
- $JsTableArr['formatter'] = <<<HTML
- (row) => {<br/>
- return h(ElTag, { type: row.{$column['Field']} ? 'success' : 'danger' }, () => row.{$column['Field']} ? '{$tempArr[1]}' : '{$tempArr[0]}')<br/>
- }
- HTML;
- }
- $tableContent.= $this->getHtmlWithJsTableArr($JsTableArr);
- }
- $template = <<<typescript
- {<br/>
- $tableContent}<br/><br/>
- typescript;
- return $template;
- }
- private function getMaxLength(array $column):int
- {
- preg_match('/(\d+)/', $column['Type'], $match);
- return $match[0] ?? 0;
- }
- private function isNumberFiled(?string $type = null)
- {
- return $type == 'number' || str_contains($type, '|');
- }
- private function isTextField(array $column):int
- {
- return Helper::hasAnyString($column['Type'], ['text']);
- }
- public function getSelectData(array $column, ?string $type = null):array
- {
- if (!$type) {
- $type = $this->getTypeByColumn($column);
- }
- if (!$this->isNumberFiled($type)) {
- return [];
- }
- $arr = [];
- preg_match_all('/(\d+\-\S+)/', $column['Comment'], $match);
- if ($match) {
- foreach ($match[0] as $matchItem) {
- list($key, $value) = explode('-', $matchItem);
- $arr[$key] = $value;
- }
- }
- return $arr;
- }
- private function getHtmlWithJsTableArr(array $arr): string
- {
- if (!$arr) {
- return '';
- }
- $str = '{';
- foreach ($arr as $k => $v) {
- $str.= " {$k}:{$v},";
- }
- return trim($str, ',') . ' },<br/>';
- }
- public function actionSetPassword()
- {
- $phone = Helper::getPostString('phone');
- $code = Helper::getPostString('code');
- $password = Helper::getPostString('password');
- if (!Helper::isPhone($phone)) {
- Helper::error('手机号码格式错误');
- }
- if (!$code || !$password) {
- Helper::error('参数错误');
- }
- if (RedisInstance::getInstance()->get('user_code:'.$phone) != $code) {
- Helper::error('验证码错误');
- }
- $id = DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'));
- if (!$id) {
- Helper::error('该手机号用户不存在');
- }
- DB::updateById('useradmin', ['password' => md5($password)], $id);
- Helper::ok();
- }
- /**
- * ajax提交图片temp临时存放
- */
- public function actionUploadtemp(){
- $type = strtolower($_FILES['surface']['type']);
- $size = $_FILES['surface']['size'];
- $maxsize = isset($_POST['imgmaxsize'])?$_POST['imgmaxsize']:'512000';
- $imagetype = isset($_POST['type'])?$_POST['type']:'all';
- if(stristr($type, 'png') || stristr($type, 'jpeg') || stristr($type, 'jpg')) {
- if($size > $maxsize){
- echo 'size';//图片容量超过200K
- }else{
- if(stristr($type, 'png')) {
- $extention = '.png';
- }else{
- $extention = '.jpg';
- }
- $temp_words = LewaimaiString::create_noncestr(32);//随机图片名
- $temp_url= 'public/temp/images/admin_'.Yii::app()->user->_id . '/';
- if(!is_dir($temp_url)){
- mkdir($temp_url,0755,true);
- }
- $temp_name = $temp_url.$temp_words.$extention;
- $time = LewaimaiString:: GetOutTradeNo();
- if(copy($_FILES['surface']['tmp_name'], $temp_name)){
- chmod($temp_name,0755);
- $cdn_url = getUpyunUploadPath();
- $fileName = $temp_words . $extention;
- $surfaceimg = $cdn_url.$fileName;
- LewaimaiImageUpload('./'.$temp_name,$surfaceimg);
- echo $surfaceimg;
- //清除本地文件
- LewaimaiUtility::clearTempimages();
- }else{
- echo 'error';//出错
- }
- }
- }else{
- echo 'suffix';//格式不对
- }
- exit;
- }
- public function actionCropImg()
- {
- $type = strtolower($_FILES['surface']['type']);
- $size = $_FILES['surface']['size'];
- $maxsize = isset($_POST['imgmaxsize'])?$_POST['imgmaxsize']:'512000';
- $imagetype = isset($_POST['type'])?$_POST['type']:'all';
- if(stristr($type, 'png') || stristr($type, 'jpeg') || stristr($type, 'jpg')) {
- if($size > $maxsize){
- echo 'size';//图片容量超过200K
- }else{
- if(stristr($type, 'png')) {
- $extention = '.png';
- }else{
- $extention = '.jpg';
- }
- $temp_words = LewaimaiString::create_noncestr(32);//随机图片名
- $temp_url= 'public/temp/images/admin_'.Yii::app()->user->_id . '/';
- if(!is_dir($temp_url)){
- mkdir($temp_url,0755,true);
- }
- $temp_name = $temp_url.$temp_words.$extention;
- $time = LewaimaiString:: GetOutTradeNo();
- $file = $_FILES['surface']['tmp_name'];
- if(isset($_POST['avatar_src']) && isset($_POST['avatar_data'])) {
- require_once 'CropImage.php';
- $crop = new CropImage($_POST['avatar_src'], $_POST['avatar_data'], $_FILES['surface']);
- $file = $crop->getResult();
- }
- if(copy($file, $temp_name)){
- chmod($temp_name,0755);
- $cdn_url = getUpyunUploadPath();
- $fileName = $temp_words . $extention;
- $surfaceimg = $cdn_url.$fileName;
- LewaimaiImageUpload('./'.$temp_name,$surfaceimg);
- echo $surfaceimg;
- //清除本地文件
- LewaimaiUtility::clearTempimages();
- }else{
- echo 'error';//出错
- }
- }
- }else{
- echo 'suffix';//格式不对
- }
- exit;
- }
- }
|