account_add_edit.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <div class="container add_edit">
  3. <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
  4. <el-form-item label="账号" prop="username" ref="username">
  5. <el-input v-model="form.username" placeholder="请输入账号" clearable></el-input>
  6. </el-form-item>
  7. <el-form-item label="密码" prop="password" ref="password" v-if="dataId === ''">
  8. <el-input v-model="form.password" show-password placeholder="请输入密码"></el-input>
  9. </el-form-item>
  10. <!-- <el-form-item label="角色" prop="role" ref="role">
  11. <el-select class="sele" multiple v-model="role" @change="roleAdd">
  12. <el-option v-for="(item, idx) in roleOptions" :key="idx" :label="item.role_id"
  13. :value="item.role_id"></el-option>
  14. </el-select>
  15. <div class="remark_txt">用户可拥有多种角色</div>
  16. </el-form-item> -->
  17. <el-form-item label="性别">
  18. <!-- <el-radio v-model="form.gender" label="0">未知</el-radio> -->
  19. <el-radio v-model="form.gender" label="1">男</el-radio>
  20. <el-radio v-model="form.gender" label="2">女</el-radio>
  21. </el-form-item>
  22. <el-form-item label="头像">
  23. <uni-file-picker v-model="imageValue" fileMediatype="image" returnType="object"
  24. :image-styles="imageStyle" @select="imgUpload" @delete="imgDelete" />
  25. </el-form-item>
  26. <el-form-item label="手机号">
  27. <el-input v-model="form.phone" type="number" placeholder="请输入手机号" clearable></el-input>
  28. </el-form-item>
  29. <!-- <el-form-item label="备注">
  30. <el-input v-model="form.comment" placeholder="请输入备注" clearable></el-input>
  31. </el-form-item> -->
  32. <!-- <el-form-item label="状态">
  33. <el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" :active-value="0"
  34. :inactive-value="1"></el-switch>
  35. </el-form-item> -->
  36. <el-form-item>
  37. <el-button round icon="el-icon-check" class="confirm_btn" @click="submitForm('ruleForm')">提交</el-button>
  38. <el-button round icon="el-icon-back" @click="returnPage">返回</el-button>
  39. </el-form-item>
  40. </el-form>
  41. </div>
  42. </template>
  43. <script>
  44. import bus from '@/common/bus';
  45. const __name = 'uni-id-users';
  46. const __role = 'uni-id-roles';
  47. export default {
  48. data() {
  49. // const validateUsername = async (rule, value, callback) => {
  50. // if (!value) {
  51. // return callback(new Error('请输入账号'));
  52. // }
  53. // let res = await this.$db[__name]
  54. // .field('username')
  55. // .where({
  56. // _id: this.$db.$cmd.neq(this.dataId),
  57. // username: value
  58. // })
  59. // .tofirst({
  60. // loadding: false
  61. // });
  62. // if (res.datas) {
  63. // callback(new Error(value + ' 账号已存在'));
  64. // return;
  65. // }
  66. // callback();
  67. // };
  68. return {
  69. dataId: '',
  70. roleOptions: [],
  71. role: [],
  72. form: {
  73. username: '',
  74. password: '',
  75. role: [],
  76. gender: '0',
  77. avatar: '',
  78. phone: '',
  79. comment: '',
  80. state: 0
  81. },
  82. imageValue: null,
  83. imageStyle: {
  84. height: '150px',
  85. width: '150px'
  86. },
  87. rules: {
  88. username: [{
  89. required: true,
  90. // validator: validateUsername,
  91. trigger: 'change'
  92. }],
  93. password: [{
  94. required: true,
  95. message: '请输入密码',
  96. trigger: 'change'
  97. }],
  98. // role: [{
  99. // required: true,
  100. // message: '请选择角色',
  101. // trigger: 'change'
  102. // }]
  103. }
  104. };
  105. },
  106. methods: {
  107. loadData() {
  108. // await this.$db[__role].tolist({
  109. // page: 1,
  110. // rows: 500
  111. // }).then(res => {
  112. // if (res.code == 200) {
  113. // this.roleOptions = res.datas;
  114. // }
  115. // });
  116. if (!this.dataId) {
  117. return;
  118. }
  119. // await this.$db[__name]
  120. // .field('username, role, gender, avatar, mobile, comment, status')
  121. // .tofirst(this.dataId)
  122. // .then(res => {
  123. // if (res.code == 200) {
  124. // for (let item in this.form) {
  125. // this.form[item] = res.datas[item];
  126. // }
  127. // this.roleOptions.forEach(x => {
  128. // if (res.datas.role.find(g => g == x.role_id)) {
  129. // this.role.push(x.role_id);
  130. // }
  131. // });
  132. // if (this.form.avatar) {
  133. // this.imageValue = {};
  134. // this.imageValue.url = this.form.avatar;
  135. // }
  136. // }
  137. // });
  138. },
  139. submitForm(formName) {
  140. // this.$refs[formName].validate((valid, obj) => {
  141. // this.$api.set_unvalidated_form_focus(this, obj);
  142. // if (valid) {
  143. if (!this.dataId) {//新增
  144. this.$axios.post("/admin/save",
  145. {
  146. 'createBy':uni.getStorageSync("nickName"),//创建人
  147. 'createTime':this.now_date,//创建时间
  148. "username": this.form.username,
  149. "phone": this.form.phone,
  150. "avatar": this.form.avatar,
  151. "password": this.form.password,
  152. "state": 1,
  153. "gender": this.form.gender
  154. },
  155. {
  156. headers:{
  157. 'Mall-Token': uni.getStorageSync("token")
  158. }
  159. }).then(response => {
  160. let res = response
  161. console.log(res)
  162. if (res.success) {
  163. this.getOpenerEventChannel().emit('loadData');
  164. this.returnPage();
  165. } else {
  166. }
  167. }).catch(res =>{
  168. });
  169. } else {
  170. // delete this.form.password;
  171. // let userData = uni.getStorageSync('useadmin_user');
  172. // this.form.dataId = this.dataId;
  173. // this.$func.useadmin.call('user/edit', this.form).then(res => {
  174. // if (res.code == 200) {
  175. // if (userData._id == this.dataId) {
  176. // userData.avatar = this.form.avatar;
  177. // uni.setStorageSync('useadmin_user', userData);
  178. // bus.$emit('setUserMsg');
  179. // }
  180. // this.getOpenerEventChannel().emit('loadData');
  181. // this.returnPage();
  182. // }
  183. // });
  184. }
  185. // }
  186. // });
  187. },
  188. // roleAdd() {
  189. // this.form.role = [];
  190. // this.roleOptions.forEach(x => {
  191. // if (this.role.find(g => g == x.role_id)) {
  192. // this.form.role.push(x.role_id);
  193. // }
  194. // });
  195. // },
  196. //获取当前时间
  197. getNowDate() {
  198. var _this = this;
  199. // this.timer = setInterval(function() {
  200. var aData = new Date();
  201. var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
  202. var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
  203. var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
  204. var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
  205. var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
  206. var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
  207. // console.log(aData.getTime())
  208. _this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
  209. // console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
  210. // }, 86400000);
  211. },
  212. imgUpload(e) {
  213. this.form.avatar = e.tempFiles[0].file;
  214. const file = this.form.avatar;
  215. const formData = new FormData();
  216. formData.append('file', file);
  217. // 文件上传
  218. this.$axios.post("/file/open/",formData,
  219. {
  220. headers:{
  221. 'Content-type' : 'multipart/form-data'
  222. }
  223. }).then(response => {
  224. let res = response
  225. if (res.success) {
  226. this.form.avatar =res.data
  227. } else {
  228. uni.showToast({
  229. icon:'error',
  230. title: '请重新上传!'
  231. });
  232. this.form.avatar =''
  233. this.imageValue = null;
  234. }
  235. }).catch(res =>{
  236. uni.showToast({
  237. icon:'error',
  238. title: '请重新上传!'
  239. });
  240. this.form.avatar =''
  241. this.imageValue = null;
  242. });
  243. },
  244. imgDelete() {
  245. this.imageValue = null;
  246. this.form.avatar = '';
  247. },
  248. returnPage() {
  249. bus.$emit('closeTab');
  250. this.$router.push('/pages/system_set/account/account');
  251. }
  252. },
  253. created() {
  254. this.dataId = this.$route.query.id || '';
  255. // this.loadData();
  256. }
  257. };
  258. </script>
  259. <style></style>