add.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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="password" label="初始密码" required>
  11. <uni-easyinput v-model="formData.password" :clearable="false" placeholder="请输入初始密码" />
  12. </uni-forms-item>
  13. <uni-forms-item name="role" label="角色列表" class="flex-center-x">
  14. <uni-data-checkbox multiple :localdata="roles" v-model="formData.role" />
  15. </uni-forms-item>
  16. <uni-forms-item name="tags" label="用户标签" labelWidth="100" class="flex-center-x">
  17. <uni-data-checkbox ref="checkbox" :multiple="true" v-model="formData.tags" collection="uni-id-tag"
  18. field="tagid as value, name as text"></uni-data-checkbox>
  19. <span class="link-btn" @click="gotoTagAdd">新增</span>
  20. <span class="link-btn" @click="gotoTagList" style="margin-left: 10px;">管理</span>
  21. </uni-forms-item>
  22. <uni-forms-item name="authorizedApp" label="可登录应用" labelWidth="100" class="flex-center-x">
  23. <uni-data-checkbox :multiple="true" v-model="formData.authorizedApp" collection="opendb-app-list"
  24. field="appid as value, name as text"></uni-data-checkbox>
  25. <span class="link-btn" @click="gotoAppList">管理</span>
  26. </uni-forms-item>
  27. <uni-forms-item name="mobile" label="手机号">
  28. <uni-easyinput v-model="formData.mobile" :clearable="false" placeholder="请输入手机号" />
  29. </uni-forms-item>
  30. <uni-forms-item name="email" label="邮箱">
  31. <uni-easyinput v-model="formData.email" :clearable="false" placeholder="请输入邮箱" />
  32. </uni-forms-item>
  33. <uni-forms-item name="status" label="是否启用">
  34. <switch @change="binddata('status', $event.detail.value)" :checked="formData.status" />
  35. </uni-forms-item>
  36. <view class="uni-button-group">
  37. <button style="width: 100px;" type="primary" class="uni-button"
  38. @click="submitForm">{{$t('common.button.submit')}}</button>
  39. <navigator open-type="navigateBack" style="margin-left: 15px;"><button style="width: 100px;"
  40. class="uni-button">{{$t('common.button.back')}}</button></navigator>
  41. </view>
  42. </uni-forms>
  43. </view>
  44. </template>
  45. <script>
  46. import {
  47. validator
  48. } 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. formData: {
  65. "username": "",
  66. "nickname": "",
  67. "password": "",
  68. "role": [],
  69. "authorizedApp": [],
  70. "tags": [],
  71. "mobile": "",
  72. "email": "",
  73. "status": true //默认启用
  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() {
  87. this.loadroles()
  88. },
  89. methods: {
  90. /**
  91. * 跳转应用管理的 list 页
  92. */
  93. gotoAppList() {
  94. uni.navigateTo({
  95. url: '../app/list'
  96. })
  97. },
  98. gotoTagList() {
  99. uni.navigateTo({
  100. url: '../tag/list'
  101. })
  102. },
  103. gotoTagAdd() {
  104. uni.navigateTo({
  105. url: '../tag/add',
  106. events: {
  107. refreshCheckboxData: () => {
  108. this.$refs.checkbox.loadData()
  109. }
  110. }
  111. })
  112. },
  113. /**
  114. * 触发表单提交
  115. */
  116. submitForm() {
  117. this.$refs.form.submit();
  118. },
  119. /**
  120. * 表单提交
  121. * @param {Object} event 回调参数 Function(callback:{value,errors})
  122. */
  123. submit(event) {
  124. const {
  125. value,
  126. errors
  127. } = event.detail
  128. // 表单校验失败页面会提示报错 ,要停止表单提交逻辑
  129. if (errors) {
  130. return
  131. }
  132. uni.showLoading({
  133. title: '提交中...',
  134. mask: true
  135. })
  136. // 是否启用功能的数据类型转换, 0 正常, 1 禁用
  137. if (typeof value.status === "boolean") {
  138. value.status = Number(!value.status)
  139. }
  140. this.$request('addUser', value).then(() => {
  141. uni.showToast({
  142. title: '新增成功'
  143. })
  144. this.getOpenerEventChannel().emit('refreshData')
  145. setTimeout(() => uni.navigateBack(), 500)
  146. }).catch(err => {
  147. uni.showModal({
  148. content: err.message || '请求服务失败',
  149. showCancel: false
  150. })
  151. }).finally(err => {
  152. uni.hideLoading()
  153. })
  154. },
  155. loadroles() {
  156. db.collection('uni-id-roles').limit(500).get().then(res => {
  157. const roleIds = []
  158. this.roles = res.result.data.map(item => {
  159. roleIds.push(item.role_id)
  160. return {
  161. value: item.role_id,
  162. text: item.role_name
  163. }
  164. })
  165. if (roleIds.indexOf('admin') === -1) {
  166. this.roles.unshift({
  167. value: 'admin',
  168. text: '超级管理员'
  169. })
  170. }
  171. }).catch(err => {
  172. uni.showModal({
  173. title: '提示',
  174. content: err.message,
  175. showCancel: false
  176. })
  177. })
  178. }
  179. }
  180. }
  181. </script>
  182. <style>
  183. ::v-deep .uni-forms-item__label {
  184. width: 90px !important;
  185. }
  186. </style>