userModal.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <div class="userModal">
  3. <el-form
  4. :model="form"
  5. :rules="rules"
  6. ref="ruleForm"
  7. :label-width="formLabelWidth"
  8. >
  9. <el-form-item label="用户头像" prop="avatar">
  10. <div style="width: 150px !important">
  11. <el-upload
  12. class="avatar-uploader"
  13. action="/api-web/uploadFile"
  14. accept=".png, .jpg, .jpeg, .gif"
  15. :headers="headers"
  16. :show-file-list="false"
  17. :on-success="handleAvatarSuccess"
  18. :before-upload="beforeAvatarUpload"
  19. >
  20. <img v-if="form.avatar" :src="form.avatar" class="avatar" />
  21. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  22. </el-upload>
  23. </div>
  24. <div
  25. class="tips"
  26. style="line-height: 1;color: red;"
  27. >
  28. 支持的格式:png, jpg, jpeg, gif;
  29. </div>
  30. </el-form-item>
  31. <el-form-item label="姓名" prop="realName">
  32. <el-input
  33. v-model.trim="form.realName"
  34. placeholder="请输入姓名"
  35. autocomplete="off"
  36. ></el-input>
  37. </el-form-item>
  38. <el-form-item label="手机号" prop="phone">
  39. <el-input
  40. v-model.trim.number="form.phone"
  41. disabled
  42. placeholder="请输入手机号"
  43. autocomplete="off"
  44. ></el-input>
  45. </el-form-item>
  46. <el-form-item label="性别" prop="gender">
  47. <el-radio-group v-model="form.gender">
  48. <el-radio :label="1">男</el-radio>
  49. <el-radio :label="0">女</el-radio>
  50. </el-radio-group>
  51. </el-form-item>
  52. <el-form-item label="邮政编码" prop="postalCode">
  53. <el-input
  54. v-model.trim="form.postalCode"
  55. placeholder="请输入邮政编码"
  56. autocomplete="off"
  57. ></el-input>
  58. </el-form-item>
  59. <el-form-item label="通讯地址" prop="contactAddress">
  60. <el-input
  61. v-model.trim="form.contactAddress"
  62. placeholder="请输入通讯地址"
  63. autocomplete="off"
  64. ></el-input>
  65. </el-form-item>
  66. </el-form>
  67. <span slot="footer" class="dialog-footer">
  68. <el-button @click="$listeners.close()">取 消</el-button>
  69. <el-button @click="onRoleSubmit('ruleForm')" type="primary"
  70. >确 定</el-button
  71. >
  72. </span>
  73. </div>
  74. </template>
  75. <script>
  76. import { getInfo } from "@/api/user";
  77. import { simpleUpdate } from "@/api/systemManage";
  78. import { isvalidPhone } from "@/utils/validate";
  79. import { getToken } from "@/utils/auth";
  80. let validPhone = (rule, value, callback) => {
  81. if (!value) {
  82. callback(new Error("请输入电话号码"));
  83. } else if (!isvalidPhone(value)) {
  84. callback(new Error("请输入正确的11位手机号码"));
  85. } else {
  86. callback();
  87. }
  88. };
  89. export default {
  90. name: "userModel",
  91. data() {
  92. return {
  93. headers: {
  94. Authorization: getToken(),
  95. },
  96. formLabelWidth: "100px",
  97. form: {
  98. id: null,
  99. avatar: null,
  100. realName: null,
  101. gender: null,
  102. phone: null,
  103. contactAddress: null,
  104. postalCode: null
  105. },
  106. rules: {
  107. avatar: [
  108. { required: true, message: "请上传用户头像", trigger: "blur, change" }
  109. ],
  110. realName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  111. gender: [{ required: true, message: "请选择性别", trigger: "change" }],
  112. phone: [
  113. {
  114. type: "number",
  115. required: true,
  116. validator: validPhone,
  117. trigger: "blur"
  118. },
  119. {
  120. pattern: /^1[3456789]\d{9}$/,
  121. message: "请输入正确的手机号",
  122. trigger: "blur"
  123. }
  124. ]
  125. }
  126. };
  127. },
  128. mounted() {
  129. this.__init();
  130. },
  131. methods: {
  132. async __init() {
  133. try {
  134. const res = await getInfo()
  135. const { id, avatar,realName,gender,phone,contactAddress,postalCode } = res.data
  136. this.form = {
  137. id,
  138. avatar,
  139. realName,
  140. gender,
  141. phone,
  142. contactAddress,
  143. postalCode
  144. }
  145. } catch(e) {}
  146. },
  147. handleAvatarSuccess(res) {
  148. this.form.avatar = res.data.url;
  149. },
  150. beforeAvatarUpload(file) {
  151. const imageType = {
  152. "image/png": true,
  153. "image/jpg": true,
  154. "image/jpeg": true,
  155. "image/gif": true
  156. };
  157. const isImage = imageType[file.type];
  158. const isLt2M = file.size / 1024 / 1024 < 2;
  159. if (!isImage) {
  160. this.$message.error("只能上传图片格式!");
  161. return false
  162. }
  163. if (!isLt2M) {
  164. this.$message.error("上传头像图片大小不能超过 2MB!");
  165. return false
  166. }
  167. // const imageWidth = this.imageWidthM;
  168. // const imageHeigh = this.imageHeightM;
  169. // const _URL = window.URL || window.webkitURL;
  170. // const isSize = new Promise((resolve, reject) => {
  171. // const img = new Image();
  172. // img.onload = function () {
  173. // if (imageWidth && imageHeigh) {
  174. // this.width === imageWidth && this.height === imageHeigh
  175. // ? resolve()
  176. // : reject(`请上传${imageWidth}x${imageHeigh}尺寸图片`);
  177. // } else if (imageWidth && !imageHeigh) {
  178. // this.width === imageWidth
  179. // ? resolve()
  180. // : reject(`请上传宽为${imageWidth}的图片`);
  181. // } else if (!imageWidth && imageHeigh) {
  182. // this.height === imageHeigh
  183. // ? resolve()
  184. // : reject(`请上传高为${imageHeigh}的图片`);
  185. // } else {
  186. // resolve();
  187. // }
  188. // };
  189. // img.src = _URL.createObjectURL(file);
  190. // }).then(
  191. // () => {
  192. // return file;
  193. // },
  194. // (src) => {
  195. // this.$message.error(src);
  196. // this.uploadImgLoading = false;
  197. // return Promise.reject();
  198. // }
  199. // );
  200. // && isSize
  201. return isImage && isLt2M;
  202. },
  203. onRoleSubmit() {
  204. // employee/simpleUpdate
  205. this.$refs['ruleForm'].validate(async (valid) => {
  206. if (valid) {
  207. const { phone, ...res } = this.form
  208. simpleUpdate({...res}).then((res) => {
  209. this.messageTips("修改", res);
  210. });
  211. } else {
  212. return;
  213. }
  214. });
  215. },
  216. messageTips(title, res) {
  217. if (res.code == 200) {
  218. this.$message.success(title + "成功");
  219. const { realName, avatar } = this.form
  220. this.$store.dispatch('user/setUserName', realName)
  221. this.$store.dispatch('user/setUserAvatar', avatar)
  222. this.$listeners.close();
  223. } else {
  224. this.$message.error(res.msg);
  225. }
  226. },
  227. }
  228. };
  229. </script>
  230. <style lang="less" scoped>
  231. /deep/.avatar-uploader .el-upload {
  232. border: 1px dashed #d9d9d9;
  233. border-radius: 6px;
  234. cursor: pointer;
  235. position: relative;
  236. overflow: hidden;
  237. background: #e7e7e7;
  238. width: 150px;
  239. height: 150px;
  240. }
  241. .avatar {
  242. width: 100%;
  243. height: 100%;
  244. }
  245. .avatar-uploader-icon {
  246. font-size: 28px;
  247. color: #8c939d;
  248. width: 150px;
  249. height: 150px;
  250. line-height: 150px;
  251. text-align: center;
  252. }
  253. .dialog-footer {
  254. width: 100%;
  255. display: block;
  256. text-align: right;
  257. }
  258. /deep/.el-select,
  259. /deep/.el-date-editor.el-input {
  260. width: 100% !important;
  261. }
  262. .setWidth {
  263. display: inline-block;
  264. }
  265. </style>