# 模板文件

根据框架设计和高可用性要求,我们自研了一套精细化的模板,基于FreeMarker编写,遵循FreeMarker书写规范,如需根据自己需要二次开发的小伙伴可以直接修改,根据层级结构的划分,框架所用到的模板文件包括:控制器模板模型模板服务层模板数据列表页模板编辑表单页模板JS模板,下面逐个完整的诠释每个模板文件的内容:

# 控制器模板


// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\index\controller;


use app\index\service\<?php echo $moduleName?>Service;

/**
 * <?php echo $moduleTitle?>管理-控制器
 * @author <?php echo $author?>

 * @since: <?php echo $since?>

 * Class <?php echo $moduleName?>

 * @package app\admin\controller
 */
class <?php echo $moduleName?> extends Backend
{
    /**
     * 初始化方法
     * @author <?php echo $author?>

     * @since: <?php echo $since?>

     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->service = new <?php echo $moduleName?>Service();
    }
	
<?php if ($columnList) {?>
<?php foreach ($columnList as $val) { ?>
    <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>
    <?php if ($val['columnName'] != "status") {?>

	/**
	 * 设置<?php echo $val['columnComment']?>
	 
	 * @return mixed
	 * @since <?php echo $since?>
	 
	 * @author <?php echo $author?>
	 
	 */
	public function <?php echo $val['columnSwitchName']?>()
	{
		if (IS_POST) {
			$result = $this->service-><?php echo $val['columnSwitchName']?>();
            return $this->jsonReturn($result);
		}
	}
    <?php } ?>
	<?php } ?>
<?php } ?>
<?php } ?>

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

# 模型模板


// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\index\model;


/**
 * <?php echo $moduleTitle?>-模型
 * @author <?php echo $author?>

 * @since: <?php echo $since?>

 * Class <?php echo $moduleName?>

 * @package app\admin\model
 */
class <?php echo $moduleName?> extends BaseModel
{
    // 设置数据表名
    protected $name = "<?php echo $tableName?>";

    /**
     * 获取缓存信息
     * @param int $id 记录ID
     * @return \app\common\model\数据信息|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author <?php echo $author?>

     * @since: <?php echo $since?>

     */
    public function getInfo($id)
    {
        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
        if ($info) {
    <?php if ($columnList) {?>
    <?php foreach ($columnList as $val) { ?>
        <?php if (isset($val['columnImage']) && $val['columnImage']) {?>

            // <?php echo $val['columnComment']?>

            $info['<?php echo $val['columnName']?>'] = get_image_url($info['<?php echo $val['columnName']?>']);
        <?php } ?>
    <?php } ?>
    <?php } ?>

        }
        return $info;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

# 服务层模板


// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\index\service;


use app\index\model\<?php echo $moduleName?>;

/**
 * <?php echo $moduleTitle?>管理-服务类
 * @author <?php echo $author?>

 * @since: <?php echo $since?>

 * Class <?php echo $moduleName?>Service
 * @package app\admin\service
 */
class <?php echo $moduleName?>Service extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new <?php echo $moduleName?>();
    }
	
	/**
     * 获取数据列表
     * @return array
     * @since <?php echo $since?>

     * @author <?php echo $author?>

     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];
		
<?php foreach ($queryList as $val) {?>
	<?php if (isset($val['columnValue'])) {?>

	    // <?php echo $val['columnComment'];?>

        $<?php echo $val['columnName']?> = isset($param['<?php echo $val['columnName']?>']) ? (int)$param['<?php echo $val['columnName']?>'] : 0;
        if ($<?php echo $val['columnName']?>) {
            $map[] = ['<?php echo $val['columnName']?>', '=', $<?php echo $val['columnName']?>];
        }
	<?php } else {?>

	    // <?php echo $val['columnComment'];?>

        $<?php echo $val['columnName']?> = isset($param['<?php echo $val['columnName']?>']) ? trim($param['<?php echo $val['columnName']?>']) : '';
        if ($<?php echo $val['columnName']?>) {
            $map[] = ['<?php echo $val['columnName']?>', 'like', "%{$<?php echo $val['columnName']?>}%"];
        }
	<?php } ?>
<?php } ?>

        return parent::getList($map); // TODO: Change the autogenerated stub
    }

<?php if ($moduleImage) {?>
	/**
     * 添加或编辑
     * @return array
     * @since <?php echo $since?>

     * @author <?php echo $author?>

     */
    public function edit()
    {
        // 参数
        $data = request()->param();
	<?php foreach ($columnList as $val) { ?>
        <?php if (isset($val['columnImage']) && $val['columnImage']) {?>

		// <?php echo $val['columnComment']?>处理
        $<?php echo $val['columnName']?> = trim($data['<?php echo $val['columnName']?>']);
        if (strpos($<?php echo $val['columnName']?>, "temp")) {
            $data['<?php echo $val['columnName']?>'] = save_image($<?php echo $val['columnName']?>, '<?php echo strtolower($moduleName)?>');
        } else {
            $data['<?php echo $val['columnName']?>'] = str_replace(IMG_URL, "", $data['<?php echo $val['columnName']?>']);
        }
        <?php } ?>
    <?php } ?>
        
        return parent::edit($data); // TODO: Change the autogenerated stub
    }
<?php } ?>

<?php if ($columnList) {?>
<?php foreach ($columnList as $val) { ?>
    <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>
    <?php if ($val['columnName'] != "status") {?>

	/**
     * 设置<?php echo $val['columnComment']?>
     
	 * @return array
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     * @since <?php echo $since?>
     
	 * @author <?php echo $author?>
     
	 */
    public function <?php echo $val['columnSwitchName']?>()
    {
		// 参数
        $data = request()->param();
        if (!$data['id']) {
            return message('记录ID不能为空', false);
        }
		if (!$data['<?php echo $val['columnName']?>']) {
            return message('记录<?php echo $val['columnComment']?>不能为空', false);
        }
        $error = '';
        $item = [
            'id' => $data['id'],
            '<?php echo $val['columnName']?>' => $data['<?php echo $val['columnName']?>']
        ];
        $rowId = $this->model->edit($item, $error);
        if (!$rowId) {
            return message($error, false);
        }
        return message();
    }
    <?php } ?>
	<?php } ?>
<?php } ?>
<?php } ?>

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

# Vue文件模板

<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="table.where" label-width="77px" class="ele-form-search"
               @keyup.enter.native="$refs.table.reload()" @submit.native.prevent>
        <el-row :gutter="15">
        <?php foreach ($queryList as $val) {?>
  <el-col :md="6" :sm="12">
            <el-form-item label="<?php echo $val['columnComment']?>:">
              <el-input v-model="table.where.<?php echo $val['columnName']?>" placeholder="请输入<?php echo $val['columnComment']?>" clearable/>
            </el-form-item>
          </el-col>
        <?php } ?>
          <el-col :md="6" :sm="12">
            <div class="ele-form-actions">
              <el-button type="primary" @click="$refs.table.reload()" icon="el-icon-search" class="ele-btn-icon">查询
              </el-button>
              <el-button @click="(table.where={})&&$refs.table.reload()">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 操作按钮 -->
      <div class="ele-table-tool ele-table-tool-default">
        <el-button @click="showEdit=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">添加
        </el-button>
        <el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small">批量删除
        </el-button>
      </div>
      <!-- 数据表格 -->
      <ele-data-table ref="table" :config="table" :choose.sync="choose" height="calc(100vh - 315px)" highlight-current-row>
        <template slot-scope="{index}">
          <el-table-column type="selection" width="45" align="center" fixed="left"/>
          <el-table-column type="index" :index="index" label="编号" width="60" align="center" fixed="left" show-overflow-tooltip/>
    <?php foreach ($columnList as $val) {?>
      <?php if ($val['columnName'] != 'id' && $val['columnName'] != 'create_user' && $val['columnName'] != 'create_time' && $val['columnName'] != 'update_time' && $val['columnName'] != 'mark') {?>
        <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>
          
          <el-table-column prop="<?php echo $val['columnName']?>" label="<?php echo $val['columnComment']?>" sortable="custom" :resizable="false" min-width="120">
              <template slot-scope="{row}">
                  <el-switch v-model="row.<?php echo $val['columnName']?>" @change="<?php echo $val['columnSwitchName']?>(row)" :active-value="1" :inactive-value="2"/>
              </template>
          </el-table-column>
        <?php } elseif (isset($val['columnImage']) && $val['columnImage']) {?>

          <el-table-column label="<?php echo $val['columnComment']?>" min-width="100" align="center">
              <template slot-scope="{row}">
                  <el-avatar shape="square" :size="35" :src="row.<?php echo $val['columnName']?>"/>
              </template>
          </el-table-column>
        <?php } else {?>

          <el-table-column prop="<?php echo $val['columnName']?>" label="<?php echo $val['columnComment']?>" sortable="custom" show-overflow-tooltip min-width="120"/>
        <?php } ?>
      <?php } ?>
    <?php } ?>

          <el-table-column label="创建时间" sortable="custom" show-overflow-tooltip min-width="160">
            <template slot-scope="{row}">{{ row.create_time | toDateString }}</template>
          </el-table-column>
          <el-table-column label="更新时间" sortable="custom" show-overflow-tooltip min-width="160">
            <template slot-scope="{row}">{{ row.update_time | toDateString }}</template>
          </el-table-column>
          <el-table-column label="操作" width="130px" align="center" :resizable="false"  fixed="right">
            <template slot-scope="{row}">
              <el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false">修改</el-link>
              <el-popconfirm title="确定要删除此<?php echo $moduleTitle?>吗?" @confirm="remove(row)" class="ele-action">
                <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <el-dialog :title="editForm.id?'修改<?php echo $moduleTitle?>':'修改<?php echo $moduleTitle?>'" :visible.sync="showEdit" width="450px"
               @closed="editForm={}" :destroy-on-close="true" :lock-scroll="false">
      <el-form :model="editForm" ref="editForm" :rules="editRules" label-width="100px">
<?php if ($columnList) {?>
  <?php foreach ($columnList as $val) { ?>
      <?php if (isset($val['columnUpload'])) {?>

        <el-form-item label="<?php echo $val['columnComment']?>:">
            <uploadImage :limit="1" v-model="editForm.<?php echo $val['columnName']?>"></uploadImage>
        </el-form-item>
      <?php } elseif (isset($val['columnRow'])) { ?>
      <?php } else {?>
      <?php if (isset($val['columnValue'])) {?>
        <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

        <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
          <el-switch
                  v-model="editForm.<?php echo $val['columnName']?>"
                  active-text="是"
                  inactive-text="否">
          </el-switch>
        </el-form-item>
        <?php } elseif ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>

        <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
          <el-select v-model="editForm.<?php echo $val['columnName']?>" placeholder="请选择<?php echo $val['columnComment']?>" class="ele-block" clearable>
            <el-option
                    v-for="item in []"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <?php } elseif ($val['dataType'] == 'date' || $val['dataType'] == 'datetime') {?>

        <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
            <el-date-picker
                    v-model="editForm.<?php echo $val['columnName']?>"
                    type="datetime"
                    placeholder="选择<?php echo $val['columnComment']?>"
                    size="small">
            </el-date-picker>
        </el-form-item>
        <?php } ?>
        <?php } else {?>
            <?php if ($val['dataType'] == 'varchar') {?>

        <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
            <el-input v-model="editForm.<?php echo $val['columnName']?>" placeholder="请输入<?php echo $val['columnComment']?>" clearable/>
        </el-form-item>
        <?php } else {?>

        <el-form-item label="<?php echo $val['columnComment']?>:" prop="<?php echo $val['columnName']?>">
            <el-input-number v-model="editForm.<?php echo $val['columnName']?>" controls-position="right" :min="0"
                              placeholder="请输入<?php echo $val['columnComment']?>" class="ele-fluid ele-text-left"/>
        </el-form-item>
            <?php } ?>
        <?php } ?>
      <?php } ?>
    <?php } ?>
<?php } ?>

      </el-form>
      <div slot="footer">
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Sys<?php echo $moduleName?>",
  data() {
    return {
      table: {url: '/<?php echo $moduleName2?>/index', where: {}},  // 表格配置
      choose: [],  // 表格选中数据
      showEdit: false,  // 是否显示表单弹窗
      editForm: {},  // 表单数据
      editRules: {  // 表单验证规则
<?php if ($columnList) {?>
  <?php foreach ($columnList as $val) { ?>
    <?php if (isset($val['columnValue'])) {?>
        <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

        <?php echo $val['columnName']?>: [
          {required: true, message: '请选择<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
        <?php } elseif ($val['dataType'] == 'bigint' || $val['dataType'] == 'int' || $val['dataType'] == 'smallint' || $val['dataType'] == 'tinyint') {?>

        <?php echo $val['columnName']?>: [
          {required: true, message: '请选择<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
        <?php } elseif ($val['dataType'] == 'date' || $val['dataType'] == 'datetime') {?>

        <?php echo $val['columnName']?>: [
          {required: true, message: '请选择<?php echo $val['columnComment']?>', trigger: 'blur'}
        
        <?php } ?>
    <?php } else {?>
        <?php if ($val['dataType'] == 'varchar') {?>
          
        <?php echo $val['columnName']?>: [
          {required: true, message: '请输入<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
        <?php } else {?>

        <?php echo $val['columnName']?>: [
          {required: true, message: '请输入<?php echo $val['columnComment']?>', trigger: 'blur'}
        ],
        <?php } ?>
    <?php } ?>
  <?php } ?>
<?php } ?>

      },
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 显示编辑
     */
    edit(row) {
      this.$http.get('/<?php echo $moduleName2?>/info?id=' + row.id).then(res => {
        if (res.data.code === 0) {
          this.editForm = res.data.data;
          this.showEdit = true;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 保存编辑
     */
    save() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({lock: true});
          this.$http.post('/<?php echo $moduleName2?>/edit', this.editForm).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.showEdit = false;
              this.$message({type: 'success', message: res.data.msg});
              this.$refs.table.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /**
     * 刪除(批量刪除)
     */
    remove(row) {
      console.log(row)
      if (!row) {  // 批量删除
        if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
        let ids = this.choose.map(d => d.id);
        this.$confirm('确定要删除选中的<?php echo $moduleTitle?>吗?', '提示', {type: 'warning'}).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.post('/<?php echo $moduleName2?>/delete', {id: ids}).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              this.$refs.table.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => 0);
      } else {  // 单个删除
        const loading = this.$loading({lock: true});
        this.$http.post('/<?php echo $moduleName2?>/delete', {id:row.id}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.$refs.table.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }
    },
  <?php foreach ($columnList as $val) {?>
    <?php if (isset($val['columnSwitch']) && $val['columnSwitch']) {?>

    /**
     * 更改<?php echo $val['columnComment']?>

     */
    <?php echo $val['columnSwitchName']?>(row) {
        const loading = this.$loading({lock: true});
        let params = Object.assign({}, row);
        this.$http.post("/<?php echo $moduleName2?>/<?php echo $val['columnSwitchName']?>", params).then(res => {
            loading.close();
            if (res.data.code === 0) {
                this.$message({type: 'success', message: res.data.msg});
            } else {
                row.<?php echo $val['columnName']?> = !row.<?php echo $val['columnName']?> ? 2 : 1;
                this.$message.error(res.data.msg);
            }
        }).catch(e => {
            loading.close();
            this.$message.error(e.message);
        });
    },
  <?php } ?>
<?php } ?>
  }
}
</script>

<style scoped>
.ele-block >>> .el-upload, .ele-block >>> .el-upload-dragger {
  width: 100%;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312