adduser.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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: [{ name: '请选择角色', id: '' }],
  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()
  101. .then( ()=>{
  102. console.log(this.userArray);
  103. if (this.userInfo.id) {
  104. this.userArray.forEach((item,index) => {
  105. if (item.name === this.userInfo.roleName) {
  106. this.userIndex = index
  107. }
  108. });
  109. }
  110. })
  111. },
  112. //方法集合
  113. methods: {
  114. // 角色选择
  115. bindPickerChange(e) {
  116. this.userIndex = e.detail.value
  117. this.userInfo.roleId = this.userArray[this.userIndex].id
  118. },
  119. // 角色列表
  120. async getRoleList () {
  121. this.userArray = [{ name: '请选择角色', id: '' }],
  122. await this.$http.get('/mrole/getList',{
  123. mid:this.userInfo.mid
  124. }).then( res=>{
  125. console.log(res);
  126. if (res.code === 200 && res.msg==='OK') {
  127. res.data.forEach(element => {
  128. if (element.name) {
  129. this.userArray.push({name: element.name,id: element.id})
  130. }
  131. });
  132. }
  133. })
  134. },
  135. // 提交表单
  136. submitUserInfo () {
  137. this.verify().then( userInfo =>{
  138. this.loading = true
  139. // 账号添加景区前缀
  140. userInfo.account = this.namePrefix +this.userInfo.account
  141. this.$http.post('/user/save',userInfo).then( res=>{
  142. console.log(res);
  143. if (res.code === 200 && res.msg === 'OK') {
  144. if (this.userdata) {
  145. this.$mHelper.toast('修改成功')
  146. } else {
  147. this.$mHelper.toast('添加成功')
  148. }
  149. this.loading = false
  150. setTimeout(() => {
  151. this.$mRouter.back()
  152. }, 500);
  153. }
  154. })
  155. })
  156. },
  157. // 表单验证规则
  158. verify () {
  159. console.log(this.userInfo);
  160. return new Promise((resolve,reject) =>{
  161. // 验证
  162. if ( !this.userInfo.account || !this.userInfo.name || !this.userInfo.roleId || !this.userInfo.phone ) {
  163. let obj = this.userInfo
  164. let arr = []
  165. for (let key in obj) {
  166. if (key === 'account' && !this.userInfo.account) {
  167. this.$mHelper.toast('请输入账号')
  168. return
  169. } else if (key === 'name' && !this.userInfo.name) {
  170. this.$mHelper.toast('请输入昵称')
  171. return
  172. } else if (key === 'phone' && !this.userInfo.phone) {
  173. this.$mHelper.toast('请输入手机号')
  174. return
  175. } else if (key === 'roleId' && !this.userInfo.roleId) {
  176. this.$mHelper.toast('请选择角色')
  177. return
  178. }
  179. }
  180. }
  181. // 账号验证
  182. if (this.userInfo.account) {
  183. let pattern = /^[a-zA-Z][a-zA-Z0-9_]{4,}$/
  184. if ( !pattern.test(this.userInfo.account)) {
  185. this.$mHelper.toast('请输入5位以上,以字母开头,可含数字、字母、下划线的账号!')
  186. return
  187. }
  188. }
  189. // 手机号验证
  190. if (this.userInfo.phone) {
  191. let pattern = /^1[3|4|5|7|8][0-9]\d{8}$/
  192. if (!pattern.test(this.userInfo.phone)) {
  193. this.$mHelper.toast('请输入正确的手机号')
  194. return
  195. }
  196. }
  197. // 密码验证
  198. if ( this.userdata) {
  199. // 编辑
  200. console.log('编辑');
  201. if (this.passwordone === this.passwordtwo) {
  202. this.userInfo.password = this.passwordtwo
  203. if (this.userInfo.password) { // 判断用户是否修改密码
  204. let pattern = /^[a-zA-Z0-9_]{6,}$/
  205. if ( !pattern.test(this.userInfo.password)) {
  206. this.$mHelper.toast('请输入6位以上,可含字母、数字、下划线的密码!')
  207. return
  208. }
  209. }
  210. } else {
  211. this.passwordone = ''
  212. this.passwordtwo = ''
  213. this.$mHelper.toast('两次密码不一致,请重新输入')
  214. return
  215. }
  216. } else {
  217. // 新增
  218. console.log('新增');
  219. if (this.passwordone === this.passwordtwo && this.passwordtwo && this.passwordone ) {
  220. this.userInfo.password = this.passwordtwo
  221. let pattern = /^[a-zA-Z0-9_]{6,}$/
  222. if ( !pattern.test(this.userInfo.password)) {
  223. this.$mHelper.toast('请输入6位以上,可含字母、数字、下划线的密码!')
  224. return
  225. }
  226. } else {
  227. this.passwordone = ''
  228. this.passwordtwo = ''
  229. this.$mHelper.toast('两次密码不一致,请重新输入')
  230. return
  231. }
  232. }
  233. resolve(this.userInfo)
  234. })
  235. }
  236. }
  237. }
  238. </script>
  239. <style lang='scss' scoped>
  240. .adduser {
  241. padding-top: 24upx;
  242. .warp {
  243. background-color: #fff;
  244. padding: 0 31upx;
  245. .item {
  246. display: flex;
  247. padding: 30upx 0;
  248. border-bottom: 1px solid #e1e1e1;
  249. &:last-child {
  250. border-bottom: none;
  251. }
  252. .title {
  253. color: #666666;
  254. width: 170upx;
  255. letter-spacing: 2upx;
  256. }
  257. .msg {
  258. flex: 1;
  259. }
  260. .uni-list-cell {
  261. flex: 1;
  262. }
  263. .uni-list-cell-db {
  264. display: flex;
  265. justify-content: space-between;
  266. }
  267. .uni-upd {
  268. width: 100%;
  269. }
  270. .uni-input {
  271. color: #e8e8e8;
  272. }
  273. .txtcolor {
  274. color: #000;
  275. }
  276. .icon-warp {
  277. width: 19upx;
  278. .iconRight {
  279. width: 19upx;
  280. height: 27upx;
  281. }
  282. }
  283. }
  284. }
  285. .btn {
  286. display: flex;
  287. justify-content: center;
  288. align-items: center;
  289. margin-top: 41upx;
  290. .sure {
  291. width: 600upx;
  292. height: 98upx;
  293. background-color: #8064f7;
  294. text-align: center;
  295. line-height: 98upx;
  296. letter-spacing: 2upx;
  297. border-radius: 6upx;
  298. color: #fff;
  299. font-size: 32upx;
  300. }
  301. }
  302. }
  303. </style>