edit.vue 7.5 KB

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