adduser.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <!-- 新增用户 -->
  2. <template>
  3. <view class="adduser">
  4. <view class="warp">
  5. <view class="item">
  6. <view class="title">账号</view>
  7. <input class="msg" type="text" v-model="userInfo.account" placeholder="请输入账号(系统会自动加上前缀)" placeholder-style="color:#e8e8e8" />
  8. </view>
  9. <view class="item">
  10. <view class="title">用户昵称</view>
  11. <input class="msg" type="text" v-model="userInfo.name" placeholder="请输入真实姓名" placeholder-style="color:#e8e8e8"/>
  12. </view>
  13. <view class="item">
  14. <view class="title">角色</view>
  15. <view class="uni-list-cell">
  16. <view class="uni-list-cell-db">
  17. <picker @change="bindPickerChange" :value="userIndex" :range="userArray" range-key="name" class="uni-upd">
  18. <view class="uni-input" :class=" userIndex === 0 ? '' : 'txtcolor'">{{ userArray[userIndex].name }}</view>
  19. </picker>
  20. <view class="icon-warp">
  21. <image
  22. src="../../../static/images/moreicon.png"
  23. class="iconRight"
  24. ></image>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="item">
  30. <view class="title">手机号</view>
  31. <input class="msg" type="text" v-model="userInfo.phone" placeholder="请输入手机号" placeholder-style="color:#e8e8e8"/>
  32. </view>
  33. <view v-if="userdata">
  34. <view class="item">
  35. <view class="title">密码</view>
  36. <input class="msg" id="password" v-model="passwordone" type="password" placeholder="默认不修改密码" placeholder-style="color:#e8e8e8"/>
  37. </view>
  38. <view class="item">
  39. <view class="title">确认密码</view>
  40. <input class="msg" id="password" v-model="passwordtwo" type="password" placeholder="默认不修改密码" placeholder-style="color:#e8e8e8"/>
  41. </view>
  42. </view>
  43. <view v-if="!userdata">
  44. <view class="item">
  45. <view class="title">密码</view>
  46. <input class="msg" id="password" v-model="passwordone" type="password" placeholder="请输入密码" placeholder-style="color:#e8e8e8"/>
  47. </view>
  48. <view class="item">
  49. <view class="title">确认密码</view>
  50. <input class="msg" id="password" v-model="passwordtwo" type="password" placeholder="再次输入密码" placeholder-style="color:#e8e8e8"/>
  51. </view>
  52. </view>
  53. </view>
  54. <view class="btn">
  55. <view class="sure" @tap='submitUserInfo'>确认</view>
  56. </view>
  57. <rf-loading v-if="loading"></rf-loading>
  58. </view>
  59. </template>
  60. <script>
  61. export default {
  62. name: 'adduser',
  63. components: {},
  64. data () {
  65. //这里存放数据
  66. return {
  67. loading: false,
  68. userArray: [{ name: '请选择角色', id: '' }],
  69. userIndex: 0,
  70. roleList:[],
  71. // 用户信息
  72. passwordone: null,
  73. passwordtwo: null,
  74. userInfo: {
  75. account: '', // 账号必传
  76. name: '', //昵称必传
  77. roleId: '', // 角色id必传
  78. phone: '', // 手机号必传
  79. password: '', // 密码必传
  80. status: '', // 状态
  81. id: '',
  82. mid: uni.getStorageSync('scenicMessage').id,
  83. },
  84. namePrefix: uni.getStorageSync('scenicMessage').account + '-',
  85. // 编辑
  86. userdata: null
  87. };
  88. },
  89. onLoad(option) {
  90. // this.userdata = JSON.parse(option.userdata)
  91. if (JSON.parse(option.userdata).id) {
  92. this.userdata = JSON.parse(option.userdata)
  93. this.userdata.account = this.userdata.account.substring(this.namePrefix.length)
  94. this.userdata.password = ''
  95. this.userInfo = this.userdata
  96. console.log(this.userInfo);
  97. }
  98. },
  99. onShow () {
  100. this.getRoleList().then( ()=>{
  101. if (this.userInfo.id) {
  102. this.userArray.forEach((item,index) => {
  103. if (item.name === this.userInfo.roleName) {
  104. this.userIndex = index
  105. }
  106. });
  107. }
  108. })
  109. },
  110. //方法集合
  111. methods: {
  112. // 角色选择
  113. bindPickerChange(e) {
  114. this.userIndex = e.detail.value
  115. this.userInfo.roleId = this.userArray[this.userIndex].id
  116. },
  117. // 角色列表
  118. async getRoleList () {
  119. await this.$http.get('/mrole/getList',{
  120. mid:this.userInfo.mid
  121. }).then( res=>{
  122. if (res.code === 200 && res.msg==='OK') {
  123. res.data.forEach(element => {
  124. if (element.name) {
  125. this.roleList.push({name: element.name,id: element.id})
  126. this.userArray = this.userArray.concat(this.roleList)
  127. }
  128. });
  129. }
  130. })
  131. },
  132. // 提交表单
  133. submitUserInfo () {
  134. this.verify().then( userInfo =>{
  135. this.loading = true
  136. // 账号添加景区前缀
  137. userInfo.account = this.namePrefix +this.userInfo.account
  138. this.$http.post('/user/save',userInfo).then( res=>{
  139. console.log(res);
  140. if (res.code === 200 && res.msg === 'OK') {
  141. if (this.userdata) {
  142. this.$mHelper.toast('修改成功')
  143. } else {
  144. this.$mHelper.toast('添加成功')
  145. }
  146. this.loading = false
  147. setTimeout(() => {
  148. this.$mRouter.back()
  149. }, 500);
  150. }
  151. })
  152. })
  153. },
  154. // 表单验证规则
  155. verify () {
  156. console.log(this.userInfo);
  157. return new Promise((resolve,reject) =>{
  158. // 验证
  159. if ( !this.userInfo.account || !this.userInfo.name || !this.userInfo.roleId || !this.userInfo.phone ) {
  160. let obj = this.userInfo
  161. let arr = []
  162. for (let key in obj) {
  163. if (key === 'account' && !this.userInfo.account) {
  164. this.$mHelper.toast('请输入账号')
  165. return
  166. } else if (key === 'name' && !this.userInfo.name) {
  167. this.$mHelper.toast('请输入昵称')
  168. return
  169. } else if (key === 'phone' && !this.userInfo.phone) {
  170. this.$mHelper.toast('请输入手机号')
  171. return
  172. } else if (key === 'roleId' && !this.userInfo.roleId) {
  173. this.$mHelper.toast('请选择角色')
  174. return
  175. }
  176. }
  177. }
  178. // 账号验证
  179. if (this.userInfo.account) {
  180. let pattern = /^[a-zA-Z][a-zA-Z0-9_]{4,}$/
  181. if ( !pattern.test(this.userInfo.account)) {
  182. this.$mHelper.toast('请输入5位以上,以字母开头,可含数字、字母、下划线的账号!')
  183. return
  184. }
  185. }
  186. // 手机号验证
  187. if (this.userInfo.phone) {
  188. let pattern = /^1[3|4|5|7|8][0-9]\d{8}$/
  189. if (!pattern.test(this.userInfo.phone)) {
  190. this.$mHelper.toast('请输入正确的手机号')
  191. return
  192. }
  193. }
  194. // 密码验证
  195. if ( this.userdata) {
  196. // 编辑
  197. console.log('编辑');
  198. if (this.passwordone === this.passwordtwo) {
  199. this.userInfo.password = this.passwordtwo
  200. if (this.userInfo.password) { // 判断用户是否修改密码
  201. let pattern = /^[a-zA-Z0-9_]{6,}$/
  202. if ( !pattern.test(this.userInfo.password)) {
  203. this.$mHelper.toast('请输入6位以上,可含字母、数字、下划线的密码!')
  204. return
  205. }
  206. }
  207. } else {
  208. this.passwordone = ''
  209. this.passwordtwo = ''
  210. this.$mHelper.toast('两次密码不一致,请重新输入')
  211. return
  212. }
  213. } else {
  214. // 新增
  215. console.log('新增');
  216. if (this.passwordone === this.passwordtwo && this.passwordtwo && this.passwordone ) {
  217. this.userInfo.password = this.passwordtwo
  218. let pattern = /^[a-zA-Z0-9_]{6,}$/
  219. if ( !pattern.test(this.userInfo.password)) {
  220. this.$mHelper.toast('请输入6位以上,可含字母、数字、下划线的密码!')
  221. return
  222. }
  223. } else {
  224. this.passwordone = ''
  225. this.passwordtwo = ''
  226. this.$mHelper.toast('两次密码不一致,请重新输入')
  227. return
  228. }
  229. }
  230. resolve(this.userInfo)
  231. })
  232. }
  233. }
  234. }
  235. </script>
  236. <style lang='scss' scoped>
  237. .adduser {
  238. padding-top: 24upx;
  239. .warp {
  240. background-color: #fff;
  241. padding: 0 31upx;
  242. .item {
  243. display: flex;
  244. padding: 30upx 0;
  245. border-bottom: 1px solid #e1e1e1;
  246. &:last-child {
  247. border-bottom: none;
  248. }
  249. .title {
  250. color: #666666;
  251. width: 170upx;
  252. letter-spacing: 2upx;
  253. }
  254. .msg {
  255. flex: 1;
  256. }
  257. .uni-list-cell {
  258. flex: 1;
  259. }
  260. .uni-list-cell-db {
  261. display: flex;
  262. justify-content: space-between;
  263. }
  264. .uni-upd {
  265. width: 100%;
  266. }
  267. .uni-input {
  268. color: #e8e8e8;
  269. }
  270. .txtcolor {
  271. color: #000;
  272. }
  273. .icon-warp {
  274. width: 19upx;
  275. .iconRight {
  276. width: 19upx;
  277. height: 27upx;
  278. }
  279. }
  280. }
  281. }
  282. .btn {
  283. display: flex;
  284. justify-content: center;
  285. align-items: center;
  286. margin-top: 41upx;
  287. .sure {
  288. width: 600upx;
  289. height: 98upx;
  290. background-color: #8064f7;
  291. text-align: center;
  292. line-height: 98upx;
  293. letter-spacing: 2upx;
  294. border-radius: 6upx;
  295. color: #fff;
  296. font-size: 32upx;
  297. }
  298. }
  299. }
  300. </style>