mine.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <view class="home">
  3. <view class="form-area">
  4. <u--form
  5. labelPosition="left"
  6. :model="form"
  7. :rules="rules"
  8. ref="Form1"
  9. :labelWidth="100"
  10. labelAlign="left">
  11. <u-form-item
  12. label="姓名"
  13. prop="name"
  14. borderBottom>
  15. <u--input
  16. v-model="form.name"
  17. >
  18. </u--input>
  19. </u-form-item>
  20. <u-form-item
  21. label="工号"
  22. prop="ID"
  23. borderBottom>
  24. <u--input
  25. v-model="form.ID"
  26. disabled
  27. >
  28. </u--input>
  29. </u-form-item>
  30. <u-form-item
  31. label="手机号"
  32. prop="phone"
  33. borderBottom>
  34. <u--input
  35. v-model="form.phone"
  36. >
  37. </u--input>
  38. </u-form-item>
  39. <u-form-item
  40. label="邮箱"
  41. prop="email"
  42. borderBottom>
  43. <u--input
  44. v-model="form.email"
  45. >
  46. </u--input>
  47. </u-form-item>
  48. <u-form-item
  49. label="密码"
  50. prop="pwd"
  51. borderBottom>
  52. <view class="demo-layout" style="width: 300rpx;">
  53. <u-button type="success" size="small" @click="toPopupShow">修改密码</u-button>
  54. </view>
  55. </u-form-item>
  56. </u--form>
  57. </view>
  58. <u-popup :show="popupShow" @close="popupShow = false" >
  59. <view class="popup-area">
  60. <view class="form-area" style="padding:0 20rpx;">
  61. <u--form
  62. labelPosition="left"
  63. :model="pwdForm"
  64. :rules="pwdRules"
  65. ref="Form2"
  66. :labelWidth="100"
  67. labelAlign="left">
  68. <u-form-item
  69. label="旧密码"
  70. prop="oldpwd"
  71. >
  72. <u--input
  73. v-model="pwdForm.oldpwd"
  74. type="password"
  75. clearable
  76. password>
  77. </u--input>
  78. </u-form-item>
  79. <u-form-item
  80. label="新密码"
  81. prop="newpwd"
  82. >
  83. <u--input
  84. v-model="pwdForm.newpwd"
  85. type="password"
  86. clearable
  87. password>
  88. </u--input>
  89. </u-form-item>
  90. <u-form-item
  91. label="确认密码"
  92. prop="surepwd"
  93. >
  94. <u--input
  95. v-model="pwdForm.surepwd"
  96. type="password"
  97. clearable
  98. password>
  99. </u--input>
  100. </u-form-item>
  101. </u--form>
  102. </view>
  103. <view class="pwd-btn">
  104. <u-button size="small" @click="popupShow = false">取消</u-button>
  105. <u-button type="success" size="small" @click="changePwd">确认</u-button>
  106. </view>
  107. </view>
  108. </u-popup>
  109. <view class="btn-area">
  110. <!-- <u-button type="info" size="small" @click="toLogout">退出登录</u-button> -->
  111. <u-button type="success" size="small" @click="save">保存</u-button>
  112. </view>
  113. </view>
  114. </template>
  115. <script>
  116. import cache from '@/utils/storage.js'
  117. import { getInfo,saveUserInfo,savePassword } from '@/api/user.js'
  118. export default {
  119. data() {
  120. return {
  121. form:{
  122. name:cache.session.getJSON('nickName'),
  123. ID:cache.session.getJSON('userName'),
  124. phone:cache.session.getJSON('phone'),
  125. email:cache.session.getJSON('email'),
  126. pwd:""
  127. },
  128. popupShow:false,
  129. userId:cache.session.getJSON('userId'),
  130. rules:{
  131. name:[
  132. {
  133. required: true,
  134. message: '请输入姓名',
  135. // blur和change事件触发检验
  136. trigger: ['blur']
  137. }
  138. ],
  139. phone:[
  140. {
  141. message: '请输入手机号',
  142. // blur和change事件触发检验
  143. trigger: ['blur']
  144. },
  145. {
  146. // 自定义验证函数,见上说明
  147. validator: (rule, value, callback) => {
  148. // 上面有说,返回true表示校验通过,返回false表示不通过
  149. // uni.$u.test.mobile()就是返回true或者false的
  150. return uni.$u.test.mobile(value);
  151. },
  152. message: '手机号码不正确',
  153. // 触发器可以同时用blur和change
  154. trigger: ['blur'],
  155. }
  156. ],
  157. email:[
  158. {
  159. message: '请输入邮箱',
  160. // blur和change事件触发检验
  161. trigger: ['blur']
  162. },
  163. {
  164. // 自定义验证函数,见上说明
  165. validator: (rule, value, callback) => {
  166. // 上面有说,返回true表示校验通过,返回false表示不通过
  167. // uni.$u.test.mobile()就是返回true或者false的
  168. return uni.$u.test.email(value);
  169. },
  170. message: '邮箱不正确',
  171. // 触发器可以同时用blur和change
  172. trigger: ['blur'],
  173. }
  174. ]
  175. },
  176. pwdForm:{
  177. oldpwd:"",
  178. newpwd:"",
  179. surepwd:""
  180. },
  181. pwdRules:{
  182. oldpwd:[
  183. {
  184. required: true,
  185. message: '请输入旧密码',
  186. // blur和change事件触发检验
  187. trigger: ['blur'],
  188. },
  189. {
  190. pattern: /^[0-9a-zA-Z]*$/g,
  191. // 正则检验前先将值转为字符串
  192. transform(value) {
  193. return String(value);
  194. },
  195. message: '密码只能包含字母或数字'
  196. },
  197. {
  198. min: 6,
  199. max: 16,
  200. message: '长度在6-16个字符之间'
  201. }
  202. ],
  203. newpwd:[
  204. {
  205. required: true,
  206. message: '请输入新密码',
  207. // blur和change事件触发检验
  208. trigger: ['blur'],
  209. },
  210. {
  211. pattern: /^[0-9a-zA-Z]*$/g,
  212. // 正则检验前先将值转为字符串
  213. transform(value) {
  214. return String(value);
  215. },
  216. message: '密码只能包含字母或数字'
  217. },
  218. {
  219. min: 6,
  220. max: 16,
  221. message: '长度在6-16个字符之间'
  222. }
  223. ],
  224. surepwd:[
  225. {
  226. required: true,
  227. message: '请确认密码',
  228. // blur和change事件触发检验
  229. trigger: ['blur'],
  230. },
  231. {
  232. pattern: /^[0-9a-zA-Z]*$/g,
  233. // 正则检验前先将值转为字符串
  234. transform(value) {
  235. return String(value);
  236. },
  237. message: '密码只能包含字母或数字'
  238. },
  239. {
  240. min: 6,
  241. max: 16,
  242. message: '长度在6-16个字符之间'
  243. },
  244. {
  245. validator: (rule, value, callback) => {
  246. if(value != this.pwdForm.newpwd){
  247. return false;
  248. }else{
  249. callback()
  250. }
  251. },
  252. message: '密码与新密码不一致'
  253. }
  254. ]
  255. }
  256. };
  257. },
  258. methods:{
  259. toPopupShow(){
  260. this.popupShow = true
  261. this.pwdForm = {
  262. oldpwd:"",
  263. newpwd:"",
  264. surepwd:""
  265. }
  266. },
  267. save(){
  268. const that = this
  269. uni.showLoading({
  270. title:'保存中',
  271. icon:'none'
  272. })
  273. this.$refs.Form1.validate().then(() => {
  274. let data = {
  275. email:this.form.email,
  276. nickName:this.form.name,
  277. phonenumber:this.form.phone,
  278. userId:this.userId
  279. }
  280. saveUserInfo(data).then(res=>{
  281. uni.hideLoading()
  282. console.log(res)
  283. uni.showToast({
  284. icon:'success',
  285. title:"修改成功!"
  286. })
  287. // uni.$u.toast()
  288. if(res.data.code===200 && res.data.msg === "操作成功"){
  289. that.initInfo()
  290. }
  291. })
  292. }).catch(errors => {
  293. uni.hideLoading()
  294. uni.$u.toast(errors[0].message)
  295. })
  296. },
  297. changePwd(){
  298. uni.showLoading({
  299. title:'修改中',
  300. icon:'none'
  301. })
  302. this.$refs.Form2.validate().then(() => {
  303. savePassword(this.pwdForm.oldpwd,this.pwdForm.newpwd).then(res=>{
  304. // console.log(res)
  305. uni.hideLoading()
  306. uni.$u.toast("修改成功!")
  307. that.pwdForm = {
  308. oldpwd:"",
  309. newpwd:"",
  310. surepwd:""
  311. }
  312. })
  313. }).catch(errors => {
  314. uni.hideLoading()
  315. uni.$u.toast(errors[0].message)
  316. })
  317. },
  318. initInfo(){
  319. const that = this
  320. getInfo().then(res=>{
  321. if(res.data.code===200){
  322. cache.session.setJSON('role',res.data.roles[0])
  323. cache.session.setJSON('phone',res.data.user.phonenumber)
  324. cache.session.setJSON('email',res.data.user.email)
  325. cache.session.setJSON('nickName',res.data.user.nickName)
  326. cache.session.setJSON('userName',res.data.user.userName)
  327. cache.session.setJSON('userId',res.data.user.userId)
  328. that.form = {
  329. name:res.data.user.nickName,
  330. ID:res.data.user.userName,
  331. phone:res.data.user.phonenumber,
  332. email:res.data.user.email,
  333. pwd:""
  334. }
  335. }
  336. })
  337. }
  338. },
  339. onReady() {
  340. this.$refs.Form1.setRules(this.rules)
  341. this.$refs.Form2.setRules(this.pwdRules)
  342. }
  343. }
  344. </script>
  345. <style lang="scss" scoped>
  346. .home{
  347. position: relative;
  348. height: 100vh;
  349. }
  350. .layout-row{
  351. padding:50rpx 30rpx;
  352. border-bottom:1rpx solid #c6c6c6;
  353. font-size: 30rpx;
  354. }
  355. .form-area{
  356. /deep/.u-form-item{
  357. line-height: 300%;
  358. font-size: 30rpx;
  359. }
  360. /deep/ .u-form-item__body{
  361. padding-right: 20rpx;
  362. }
  363. /deep/ .u-form-item__body__left__content__label{
  364. padding-left:40rpx
  365. }
  366. }
  367. .btn-area{
  368. margin-top: 50rpx;
  369. width: 100%;
  370. box-sizing: border-box;
  371. padding: 0px 200rpx;
  372. // right: 50rpx;
  373. // display: flex;
  374. // /deep/.u-button{
  375. // margin: 30rpx;
  376. // }
  377. }
  378. .popup-area{
  379. height: 700rpx;
  380. box-sizing: border-box;
  381. // padding: 0 30rpx;
  382. .pwd-btn{
  383. display: flex;
  384. padding-top: 50rpx;
  385. /deep/.u-button{
  386. margin: 30rpx;
  387. }
  388. }
  389. }
  390. </style>