后端

member-add-or-update.vue 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <el-dialog
  3. :title="!dataForm.id ? '新增' : '修改'"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible"
  6. >
  7. <el-form
  8. :model="dataForm"
  9. :rules="dataRule"
  10. ref="dataForm"
  11. @keyup.enter.native="dataFormSubmit()"
  12. label-width="80px"
  13. >
  14. <el-form-item label="姓名" prop="truename">
  15. <el-input v-model="dataForm.truename" placeholder="姓名"></el-input>
  16. </el-form-item>
  17. <el-form-item label="用户名" prop="username">
  18. <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
  19. </el-form-item>
  20. <el-form-item label="密码" prop="password">
  21. <el-input v-model="dataForm.password" placeholder="密码"></el-input>
  22. </el-form-item>
  23. <el-form-item label="头像" prop="titlePic">
  24. <!-- <el-input v-model="dataForm.titlePic" placeholder="头像"></el-input> -->
  25. <el-upload
  26. class="avatar-uploader"
  27. :action="upload_url"
  28. :show-file-list="false"
  29. :on-success="successHandle"
  30. :before-upload="beforeUploadHandle"
  31. ref="upload"
  32. :data="thumb"
  33. name="upload_file"
  34. >
  35. <img v-if="showimg" :src="showimg" class="avatar" />
  36. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  37. </el-upload>
  38. </el-form-item>
  39. <el-form-item label="所属机构" prop="organization">
  40. <el-input v-model="dataForm.organization" placeholder="所属机构"></el-input>
  41. </el-form-item>
  42. <el-form-item label="职位" prop="position">
  43. <el-input v-model="dataForm.position" placeholder="职位"></el-input>
  44. </el-form-item>
  45. <el-form-item label="职称" prop="jobTitle">
  46. <el-input v-model="dataForm.jobTitle" placeholder="职称"></el-input>
  47. </el-form-item>
  48. <el-form-item label="电话" prop="phone">
  49. <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
  50. </el-form-item>
  51. <el-form-item label="邮箱" prop="email">
  52. <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
  53. </el-form-item>
  54. <el-form-item label="审核" prop="isCheck">
  55. <el-radio v-model="dataForm.isCheck" :label="0"></el-radio>
  56. <el-radio v-model="dataForm.isCheck" :label="1"></el-radio>
  57. </el-form-item>
  58. </el-form>
  59. <span slot="footer" class="dialog-footer">
  60. <el-button @click="visible = false">取消</el-button>
  61. <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  62. </span>
  63. </el-dialog>
  64. </template>
  65. <script>
  66. export default {
  67. data() {
  68. var checkPwd = (rule, value, callback) => {
  69. if (!value) {
  70. if (this.dataForm.id === 0) {
  71. return callback(new Error("密码不可为空"));
  72. } else {
  73. return callback();
  74. }
  75. } else {
  76. if (!value.match(/^.*(?=.{6,})/)) {
  77. return callback(new Error("密码需6位或以上"));
  78. }
  79. return callback();
  80. }
  81. };
  82. var checkEmail = (rule, value, callback) => {
  83. if (!value) {
  84. return callback();
  85. } else {
  86. if (
  87. !value.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/)
  88. ) {
  89. return callback(new Error("邮箱格式错误"));
  90. }
  91. return callback();
  92. }
  93. };
  94. var checkPhone = (rule, value, callback) => {
  95. if (!value) {
  96. return callback(new Error("电话不能为空"));
  97. } else {
  98. if (!value.match(/^1(3|4|5|6|7|8)\d{9}$/)) {
  99. return callback(new Error("电话格式错误"));
  100. } else {
  101. return callback();
  102. }
  103. }
  104. };
  105. return {
  106. add: false,
  107. visible: false,
  108. dataForm: {
  109. id: 0,
  110. username: "",
  111. password: "",
  112. truename: "",
  113. titlePic: "",
  114. organization: "",
  115. position: "",
  116. jobTitle: "",
  117. phone: "",
  118. email: "",
  119. createTime: "",
  120. isCheck: 0
  121. },
  122. dataRule: {
  123. username: [
  124. { required: true, message: "用户名不能为空", trigger: "blur" }
  125. ],
  126. truename: [
  127. { required: true, message: "姓名不能为空", trigger: "blur" }
  128. ],
  129. // password: [
  130. // { required: true, message: '密码不能为空', trigger: 'blur' }
  131. // ],
  132. // phone: [
  133. // { required: true, message: '电话不能为空', trigger: 'blur' }
  134. // ]
  135. password: [{ validator: checkPwd, trigger: "blur" }],
  136. phone: [{ validator: checkPhone, trigger: "blur" }],
  137. email: [{ validator: checkEmail, trigger: "blur" }]
  138. },
  139. upload_url: "",
  140. radio: "0",
  141. thumb: { isthumb: 0 },
  142. showimg: ""
  143. };
  144. },
  145. methods: {
  146. init(id) {
  147. this.dataForm.id = id || 0;
  148. if (this.dataForm.id === 0) {
  149. this.add = true;
  150. }
  151. this.visible = true;
  152. this.$nextTick(() => {
  153. this.$refs["dataForm"].resetFields();
  154. if (this.dataForm.id) {
  155. this.$http({
  156. url: this.$http.adornUrl(`/admin/member/info/${this.dataForm.id}`),
  157. method: "get",
  158. params: this.$http.adornParams()
  159. }).then(({ data }) => {
  160. window.console.log(data);
  161. if (data && data.code === 0) {
  162. this.dataForm.username = data.member.username;
  163. this.dataForm.truename = data.member.truename;
  164. this.dataForm.titlePic = data.member.titlePic;
  165. this.dataForm.organization = data.member.organization;
  166. this.dataForm.position = data.member.position;
  167. this.dataForm.jobTitle = data.member.jobTitle;
  168. this.dataForm.phone = data.member.phone;
  169. this.dataForm.email = data.member.email;
  170. this.dataForm.createTime = data.member.createTime;
  171. this.dataForm.modifyTime = data.member.modifyTime;
  172. this.dataForm.isCheck = data.member.isCheck;
  173. this.showimg =
  174. "http://121.42.53.174:9008/static" + data.member.titlePic;
  175. }
  176. });
  177. }
  178. });
  179. // 上传路径
  180. this.upload_url = this.$http.adornUrl(
  181. `/sys/filemanager/uploadimg?token=${this.$cookie.get("token")}`
  182. );
  183. },
  184. // 表单提交
  185. dataFormSubmit() {
  186. this.$refs["dataForm"].validate(valid => {
  187. if (valid) {
  188. this.$http({
  189. url: this.$http.adornUrl(
  190. `/admin/member/${!this.dataForm.id ? "save" : "update"}`
  191. ),
  192. method: "post",
  193. data: this.$http.adornData({
  194. id: this.dataForm.id || undefined,
  195. username: this.dataForm.username,
  196. password: this.dataForm.password,
  197. truename: this.dataForm.truename,
  198. sex: this.dataForm.sex,
  199. titlePic: this.dataForm.titlePic,
  200. organization: this.dataForm.organization,
  201. position: this.dataForm.position,
  202. jobTitle: this.dataForm.jobTitle,
  203. phone: this.dataForm.phone,
  204. email: this.dataForm.email,
  205. createTime: this.dataForm.createTime,
  206. modifyTime: this.dataForm.modifyTime
  207. })
  208. }).then(({ data }) => {
  209. if (data && data.code === 0) {
  210. this.$message({
  211. message: "操作成功",
  212. type: "success",
  213. duration: 1500,
  214. onClose: () => {
  215. this.visible = false;
  216. this.$emit("refreshDataList");
  217. }
  218. });
  219. } else {
  220. this.$message.error(data.msg);
  221. }
  222. });
  223. }
  224. });
  225. },
  226. // 上传图像
  227. submitUpload() {
  228. this.$refs.upload.submit();
  229. },
  230. // 上传之前
  231. beforeUploadHandle(file) {
  232. this.thumb.isthumb = this.radio;
  233. if (
  234. file.type !== "image/jpg" &&
  235. file.type !== "image/jpeg" &&
  236. file.type !== "image/png" &&
  237. file.type !== "image/gif"
  238. ) {
  239. this.$message.error("只支持jpg、png、gif格式的图片!");
  240. return false;
  241. }
  242. },
  243. // 上传成功
  244. successHandle(response, file) {
  245. if (response && response.code === 0) {
  246. if (response.hasOwnProperty("thumb")) {
  247. this.dataForm.titlePic = response.thumb;
  248. this.showimg = "http://121.42.53.174:9008/static" + response.thumb;
  249. } else {
  250. this.showimg = "http://121.42.53.174:9008/static" + response.picname;
  251. this.dataForm.titlePic = response.picname;
  252. this.dialogVisible = true;
  253. }
  254. } else {
  255. this.$message.error(response.msg);
  256. }
  257. }
  258. }
  259. };
  260. </script>
  261. <style>
  262. .avatar-uploader .el-upload {
  263. border: 1px dashed #d9d9d9;
  264. border-radius: 6px;
  265. cursor: pointer;
  266. position: relative;
  267. overflow: hidden;
  268. }
  269. .avatar-uploader .el-upload:hover {
  270. border-color: #409eff;
  271. }
  272. .avatar-uploader-icon {
  273. font-size: 28px;
  274. color: #8c939d;
  275. width: 178px;
  276. height: 178px;
  277. line-height: 178px;
  278. text-align: center;
  279. }
  280. .avatar {
  281. width: 178px;
  282. height: 178px;
  283. display: block;
  284. }
  285. </style>