edit.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="uni-container">
  3. <uni-forms ref="form" v-model="formData" :rules="rules" validateTrigger="bind" @submit="submit">
  4. <uni-forms-item name="username" label="用户名" required>
  5. <uni-easyinput v-model="formData.username" :clearable="false" placeholder="请输入用户名" />
  6. </uni-forms-item>
  7. <uni-forms-item name="nickname" label="用户昵称" required>
  8. <uni-easyinput v-model="formData.nickname" :clearable="false" placeholder="请输入用户昵称" />
  9. </uni-forms-item>
  10. <uni-forms-item v-if="showPassword" name="password" label="重置密码" key="password">
  11. <uni-easyinput v-model="formData.password" :clearable="false" placeholder="请输入重置密码">
  12. <view slot="right" class="cancel-reset-password-btn" @click="trigger">取消</view>
  13. </uni-easyinput>
  14. </uni-forms-item>
  15. <uni-forms-item v-else label="重置密码">
  16. <span class="reset-password-btn" @click="trigger">点击重置密码</span>
  17. </uni-forms-item>
  18. <uni-forms-item name="role" label="角色列表" class="flex-center-x">
  19. <uni-data-checkbox multiple :localdata="roles" v-model="formData.role" />
  20. </uni-forms-item>
  21. <uni-forms-item name="tags" label="用户标签" labelWidth="100" class="flex-center-x">
  22. <uni-data-checkbox ref="checkboxTags" :multiple="true" v-model="formData.tags" collection="uni-id-tag"
  23. field="tagid as value, name as text"></uni-data-checkbox>
  24. <span class="link-btn" @click="gotoTagAdd">新增</span>
  25. <span class="link-btn" @click="gotoTagList" style="margin-left: 10px;">管理</span>
  26. </uni-forms-item>
  27. <uni-forms-item name="authorizedApp" label="可登录应用">
  28. <view class="uni-forms-item-flex-center-x">
  29. <uni-data-checkbox :multiple="true" v-model="formData.authorizedApp" :localdata="appList"></uni-data-checkbox>
  30. <span class="link-btn" @click="gotoAppList">管理</span>
  31. </view>
  32. <view v-if="formDataId === userId" class="uni-form-item-tips">当前有未添加的应用{{ unknownAppidsCom }},建议点击右侧管理进行添加</view>
  33. </uni-forms-item>
  34. <uni-forms-item name="mobile" label="手机号">
  35. <uni-easyinput v-model="formData.mobile" :clearable="false" placeholder="请输入手机号" />
  36. </uni-forms-item>
  37. <uni-forms-item name="email" label="邮箱">
  38. <uni-easyinput v-model="formData.email" :clearable="false" placeholder="请输入邮箱" />
  39. </uni-forms-item>
  40. <uni-forms-item name="status" label="用户状态">
  41. <switch v-if="Number(formData.status) < 2" @change="binddata('status', $event.detail.value)" :checked="formData.status" :disabled="formDataId === userId" />
  42. <view v-else class="uni-form-item-empty">{{parseUserStatus(formData.status)}}</view>
  43. <view v-if="formDataId === userId" class="uni-form-item-tips">请勿禁用当前登录的账号</view>
  44. </uni-forms-item>
  45. <view class="uni-button-group">
  46. <button style="width: 100px;" type="primary" class="uni-button" @click="submitForm">{{$t('common.button.submit')}}</button>
  47. <navigator open-type="navigateBack" style="margin-left: 15px;"><button style="width: 100px;" class="uni-button">{{$t('common.button.back')}}</button></navigator>
  48. </view>
  49. </uni-forms>
  50. </view>
  51. </template>
  52. <script>
  53. import { validator } from '@/js_sdk/validator/uni-id-users.js';
  54. const db = uniCloud.database();
  55. const dbCmd = db.command;
  56. const dbCollectionName = 'uni-id-users';
  57. function getValidator(fields) {
  58. let result = {}
  59. for (let key in validator) {
  60. if (fields.includes(key)) {
  61. result[key] = validator[key]
  62. }
  63. }
  64. return result
  65. }
  66. export default {
  67. data() {
  68. return {
  69. showPassword: false,
  70. formData: {
  71. "username": "",
  72. "nickname": "",
  73. "password": undefined,
  74. "role": [],
  75. "tags": [],
  76. "authorizedApp": [],
  77. "mobile": undefined,
  78. "email": undefined,
  79. "status": false //默认禁用
  80. },
  81. rules: {
  82. ...getValidator(["username", "password", "role", "mobile", "email"]),
  83. "status": {
  84. "rules": [{
  85. "format": "bool"
  86. }]
  87. }
  88. },
  89. roles: [],
  90. userId:"",
  91. appList:[],
  92. unknownAppids:[]
  93. }
  94. },
  95. onLoad(e) {
  96. const id = e.id
  97. this.formDataId = id
  98. let userInfo = uni.getStorageSync("uni-id-pages-userInfo") || {};
  99. this.userId = userInfo._id;
  100. this.getDetail(id)
  101. this.loadroles();
  102. },
  103. methods: {
  104. /**
  105. * 跳转应用管理的 list 页
  106. */
  107. gotoAppList() {
  108. uni.navigateTo({
  109. url: '../app/list'
  110. })
  111. },
  112. gotoTagList() {
  113. uni.navigateTo({
  114. url: '../tag/list'
  115. })
  116. },
  117. gotoTagAdd() {
  118. uni.navigateTo({
  119. url: '../tag/add',
  120. events: {
  121. refreshCheckboxData: () => {
  122. this.$refs.checkboxTags.loadData();
  123. }
  124. }
  125. })
  126. },
  127. /**
  128. * 切换重置密码框显示或隐藏
  129. */
  130. trigger() {
  131. this.showPassword = !this.showPassword
  132. },
  133. /**
  134. * 触发表单提交
  135. */
  136. submitForm(form) {
  137. this.$refs.form.submit();
  138. },
  139. /**
  140. * 表单提交
  141. * @param {Object} event 回调参数 Function(callback:{value,errors})
  142. */
  143. submit(event) {
  144. const {
  145. value,
  146. errors
  147. } = event.detail
  148. // 表单校验失败页面会提示报错 ,要停止表单提交逻辑
  149. if (errors) {
  150. return
  151. }
  152. uni.showLoading({
  153. title: '修改中...',
  154. mask: true
  155. })
  156. // 是否启用功能的数据类型转换, 0 正常, 1 禁用
  157. if (typeof value.status === "boolean") {
  158. value.status = Number(!value.status)
  159. }
  160. value.uid = this.formDataId
  161. this.$request('updateUser', value).then(() => {
  162. uni.showToast({
  163. title: '修改成功'
  164. })
  165. const eventChannel = this.getOpenerEventChannel();
  166. if (eventChannel.emit) eventChannel.emit('refreshData');
  167. setTimeout(() => uni.navigateBack(), 500)
  168. }).catch(err => {
  169. uni.showModal({
  170. content: err.message || '请求服务失败',
  171. showCancel: false
  172. })
  173. }).finally(err => {
  174. uni.hideLoading()
  175. })
  176. },
  177. resetPWd(resetData) {
  178. this.$request('system/user/resetPwd', resetData)
  179. .then().catch(err => {
  180. uni.showModal({
  181. content: err.message || '请求服务失败',
  182. showCancel: false
  183. })
  184. }).finally()
  185. },
  186. /**
  187. * 获取表单数据
  188. * @param {Object} id
  189. */
  190. getDetail(id) {
  191. uni.showLoading({
  192. mask: true
  193. })
  194. db.collection(dbCollectionName)
  195. .doc(id)
  196. .field('username,nickname,role,dcloud_appid as authorizedApp,tags,mobile,email,status')
  197. .get()
  198. .then((res) => {
  199. const data = res.result.data[0]
  200. if (data) {
  201. if (data.status === undefined) {
  202. data.status = true
  203. }
  204. if (data.status === 0) {
  205. data.status = true
  206. }
  207. if (data.status === 1) {
  208. data.status = false
  209. }
  210. this.formData = Object.assign(this.formData, data);
  211. this.loadAppList(this.formData.authorizedApp);
  212. }
  213. }).catch((err) => {
  214. uni.showModal({
  215. content: err.message || '请求服务失败',
  216. showCancel: false
  217. })
  218. }).finally(() => {
  219. uni.hideLoading()
  220. })
  221. },
  222. loadroles() {
  223. db.collection('uni-id-roles').limit(500).get().then(res => {
  224. const roleIds = []
  225. this.roles = res.result.data.map(item => {
  226. roleIds.push(item.role_id)
  227. return {
  228. value: item.role_id,
  229. text: item.role_name
  230. }
  231. })
  232. if (roleIds.indexOf('admin') === -1) {
  233. this.roles.unshift({
  234. value: 'admin',
  235. text: '超级管理员'
  236. })
  237. }
  238. }).catch(err => {
  239. uni.showModal({
  240. title: '提示',
  241. content: err.message,
  242. showCancel: false
  243. })
  244. })
  245. },
  246. loadAppList(authorizedApp){
  247. db.collection('opendb-app-list').limit(500).get().then(res => {
  248. let list = res.result.data.map((item, index) => {
  249. return {
  250. value: item.appid,
  251. text: item.name
  252. }
  253. });
  254. if (!list) list = [];
  255. authorizedApp.map((appid) => {
  256. let info = list.find((item) => {
  257. return item.value === appid;
  258. });
  259. if (!info) {
  260. this.unknownAppids.push(appid);
  261. list.push({
  262. value: appid,
  263. text: `未知应用${appid}`
  264. });
  265. }
  266. });
  267. this.appList = list;
  268. }).catch(err => {
  269. uni.showModal({
  270. title: '提示',
  271. content: err.message,
  272. showCancel: false
  273. })
  274. })
  275. },
  276. // status 对应文字显示
  277. parseUserStatus(status) {
  278. if (status === 0) {
  279. return '启用'
  280. } else if (status === 1) {
  281. return '禁用'
  282. } else if (status === 2) {
  283. return '审核中'
  284. } else if (status === 3) {
  285. return '审核拒绝'
  286. } else if (status === 4) {
  287. return '已注销'
  288. } else if (typeof status !== "undefined") {
  289. return '未知'
  290. } else {
  291. return '启用'
  292. }
  293. }
  294. },
  295. computed:{
  296. unknownAppidsCom(){
  297. let str = "";
  298. this.unknownAppids.map((item, index) => {
  299. str += item;
  300. if (index !== this.unknownAppids.length-1){
  301. str += "、";
  302. }
  303. });
  304. return str;
  305. }
  306. }
  307. }
  308. </script>
  309. <style>
  310. .reset-password-btn {
  311. /* height: 100%; */
  312. line-height: 36px;
  313. color: #007AFF;
  314. text-decoration: underline;
  315. cursor: pointer;
  316. }
  317. .cancel-reset-password-btn {
  318. color: #007AFF;
  319. padding-right: 10px;
  320. cursor: pointer;
  321. }
  322. ::v-deep .uni-forms-item__label {
  323. width: 90px !important;
  324. }
  325. .uni-forms-item-flex-center-x {
  326. display: flex;
  327. align-items: center;
  328. flex-wrap: wrap;
  329. }
  330. </style>