mine.vue 8.6 KB

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