index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <template>
  2. <view>
  3. <!--#ifdef APP-PLUS-->
  4. <view class="lz-status_bar">
  5. <view class="lz-top_view"></view>
  6. </view>
  7. <!--#endif-->
  8. <!-- #ifndef MP-WEIXIN -->
  9. <view class="kaoshi-head">
  10. <view class="kaoshi-head-top">
  11. <view class="kaoshi-head-left" @tap="$navigateBack">
  12. <view class="iconfont icon-zuojiantou"></view>
  13. </view>
  14. <view class="kaoshi-head-m">个人设置</view>
  15. </view>
  16. </view>
  17. <!--#endif-->
  18. <!-- #ifndef MP-WEIXIN -->
  19. <uni-list>
  20. <view class="setting-tx">
  21. <uni-list-item title="头像" :showArrow="false"></uni-list-item>
  22. <view><img :src="userinfo.user_id ? user_info.avatar : static_media.img01" @tap="getFaceimg" /></view>
  23. </view>
  24. <view class="setting-nc">
  25. <uni-list-item title="昵称" show-badge="true" :badge-text="user_info.nickname" @tap="show_modal = true">
  26. </uni-list-item>
  27. </view>
  28. <view @tap="show_changepwd = true" hover-class="none">
  29. <uni-list-item title="修改密码"></uni-list-item>
  30. </view>
  31. </uni-list>
  32. <view class="tuichu" @tap="logout">退出登录</view>
  33. <!-- #endif -->
  34. <!-- #ifdef MP-WEIXIN -->
  35. <view class="seting_touxiang">
  36. <view class="t01">设置头像</view>
  37. <view class="t02">
  38. <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  39. <image class="avatar" :src="avatar? avatar:'../../../static/img/txk.png'"></image>
  40. </button>
  41. </view>
  42. </view>
  43. <view class="seting_nc">
  44. <view class="n01">设置昵称</view>
  45. <view class="n02"><input type="nickname" v-model="nickname" @blur="changeName" placeholder="暂未设置"></view>
  46. </view>
  47. <view class="seting_button">
  48. <button :loading="isSubmit" type="primary" class="primary" form-type="submit" @tap="submit">提交修改</button>
  49. </view>
  50. <!-- #endif -->
  51. <view class="lz-modal" :class="{ 'lz-modal-show': show_modal }">
  52. <view class="lz-modal-dialog">
  53. <view class="lz-list">
  54. <view class="lz-list-header">
  55. <view>修改昵称</view>
  56. <view class="iconfont icon-cuo1" @tap="show_modal = false"></view>
  57. </view>
  58. <view class="lz-list-body">
  59. <view class="lz-list-item lz-list-item-middle">
  60. <view class="lz-list-line">
  61. <view class="lz-list-content lz-text__align--l">
  62. <input type="text" v-model="nickname" placeholder="请输入您的昵称" />
  63. </view>
  64. </view>
  65. </view>
  66. <button type="primary" class="primary" @tap="editUserInfo('nickname')">提交</button>
  67. </view>
  68. </view>
  69. <view class="lz-margin--b-md"></view>
  70. </view>
  71. </view>
  72. <view class="lz-modal" :class="{ 'lz-modal-show': show_changepwd }">
  73. <view class="lz-modal-dialog">
  74. <view class="lz-list">
  75. <view class="lz-list-header">
  76. <view>修改密码</view>
  77. <view class="iconfont icon-cuo1" @tap="show_changepwd = false"></view>
  78. </view>
  79. <view class="lz-list-body">
  80. <view class="lz-list-item lz-list-item-middle">
  81. <view class="lz-list-line">
  82. <view class="lz-list-content lz-text__align--l">
  83. <input type="text" password="true" v-model="password" placeholder="请输入您的密码,至少六位" />
  84. </view>
  85. </view>
  86. </view>
  87. <button type="primary" class="primary" @tap="changePwd('nickname')">提交</button>
  88. </view>
  89. </view>
  90. <view class="lz-margin--b-md"></view>
  91. </view>
  92. </view>
  93. </view>
  94. </template>
  95. <script>
  96. import {
  97. mapState
  98. } from 'vuex'
  99. import uniList from '@/components/uni-list/uni-list.vue';
  100. import uniListItem from '@/components/uni-list-item/uni-list-item.vue';
  101. export default {
  102. data() {
  103. return {
  104. user_info: [],
  105. show_modal: false,
  106. show_changepwd: false,
  107. nickname: '',
  108. avatar: '',
  109. password: '',
  110. };
  111. },
  112. onLoad() {
  113. this.get_user_info();
  114. },
  115. onShow() {},
  116. computed: {
  117. ...mapState(['subject', 'userinfo']),
  118. static_media() {
  119. return {
  120. img01: this.$myConfig.localMedia + '/static/img/logo.png',
  121. }
  122. }
  123. },
  124. components: {
  125. uniList,
  126. uniListItem
  127. },
  128. methods: {
  129. async submit() {
  130. let str = this.nickname.trim();
  131. if (str.length == 0) {
  132. uni.showToast({
  133. title: '请输入合法的昵称',
  134. icon: 'none'
  135. })
  136. return
  137. }
  138. // if ((/[^/a-zA-Z0-9\u4E00-\u9FA5]/g).test(str)) {
  139. // uni.showToast({
  140. // title: '请输入中英文和数字',
  141. // icon: 'none'
  142. // })
  143. // return
  144. // }
  145. if (this.isSubmit) return
  146. this.isSubmit = true
  147. let res = await this.$myHttp.post({
  148. url: this.$myHttp.urlMap.userProfile,
  149. data: {
  150. nickname: this.nickname,
  151. avatar: this.avatar
  152. },
  153. needLogin: true
  154. })
  155. if (res.code == 1) {
  156. this.isSubmit = false
  157. uni.navigateBack()
  158. } else {
  159. this.isSubmit = false
  160. uni.showToast({
  161. title: res.msg,
  162. icon: 'none'
  163. })
  164. }
  165. },
  166. changeName(e) {
  167. this.nickname = e.detail.value;
  168. // console.log('this.nick_name.length',this.nickname.length);
  169. },
  170. onChooseAvatar(e) {
  171. let that = this
  172. this.avatar = e.detail.avatarUrl
  173. uni.uploadFile({
  174. url: that.$myConfig.api + 'api/common/upload?token=' + that.userinfo.token, //仅为示例,非真实的接口地址
  175. filePath: that.avatar,
  176. header: {
  177. token: that.userinfo.token
  178. },
  179. name: 'file',
  180. formData: {
  181. 'user': 'test'
  182. },
  183. success: (uploadFileRes) => {
  184. if (uploadFileRes) {
  185. let res = JSON.parse(uploadFileRes.data);
  186. that.avatar = res.data.fullurl
  187. // that.editUserInfo('avatar');
  188. }
  189. }
  190. });
  191. },
  192. async get_user_info() {
  193. let res = await this.$myHttp.post({
  194. url: this.$myHttp.urlMap.userInfo,
  195. needLogin: true
  196. })
  197. if (res.code === 1) {
  198. this.user_info = res.data
  199. this.nickname = res.data.nickname
  200. this.avatar = res.data.avatar
  201. }
  202. //
  203. },
  204. async logout() {
  205. const valid = await this.$myUserLogin.logout();
  206. if (valid) {
  207. uni.navigateTo({
  208. url: '/pages/auth/login/index'
  209. });
  210. } else {
  211. uni.showToast({
  212. icon: 'none',
  213. title: '退出登录失败'
  214. });
  215. }
  216. },
  217. getFaceimg() {
  218. let that = this;
  219. uni.chooseImage({
  220. count: 1, //默认9
  221. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  222. sourceType: ['album'], //从相册选择
  223. success: function(chooseImageRes) {
  224. const tempFilePaths = chooseImageRes.tempFilePaths;
  225. uni.uploadFile({
  226. url: that.$myConfig.api + 'api/common/upload?token=' + that.userinfo
  227. .token, //仅为示例,非真实的接口地址
  228. filePath: tempFilePaths[0],
  229. header: {
  230. token: that.userinfo.token
  231. },
  232. name: 'file',
  233. formData: {
  234. 'user': 'test'
  235. },
  236. success: (uploadFileRes) => {
  237. if (uploadFileRes) {
  238. let res = JSON.parse(uploadFileRes.data);
  239. that.avatar = res.data.url
  240. that.editUserInfo('avatar');
  241. }
  242. }
  243. });
  244. }
  245. });
  246. },
  247. //
  248. async changePwd() {
  249. if (this.password != '' && this.password != undefined && this.password.length >= 6) {
  250. } else {
  251. this.$myUtils.$prompt.showToast({
  252. icon: 'none',
  253. title: '密码不符合要求'
  254. });
  255. return
  256. }
  257. let res = await this.$myHttp.post({
  258. url: this.$myHttp.urlMap.resetpwd,
  259. data: {
  260. newpassword: this.password
  261. },
  262. needLogin: true
  263. });
  264. if (res.code == 1) {
  265. uni.showToast({
  266. icon: 'success',
  267. title: '密码修改成功'
  268. })
  269. setTimeout(function() {
  270. uni.reLaunch({
  271. url: '/pages/auth/login/index?form_type=0'
  272. });
  273. }, 1000)
  274. }
  275. },
  276. async bindLogin() {
  277. /**
  278. * 下面简单模拟下服务端的处理
  279. * 检测用户账号密码是否在已注册的用户列表中
  280. * 实际开发中,使用 uni.request 将账号信息发送至服务端,客户端在回调函数中获取结果信息。
  281. */
  282. const data = {
  283. account: this.nickname,
  284. password: this.password
  285. };
  286. const valid = await this.$myUserLogin.login(data);
  287. },
  288. async editUserInfo(type) {
  289. let data = {}
  290. if (type == 'nickname') {
  291. if (this.nickname != '' && this.nickname != undefined) {
  292. data['nickname'] = this.nickname
  293. } else {
  294. this.$myUtils.$prompt.showToast({
  295. icon: 'none',
  296. title: '昵称不能为空'
  297. });
  298. return
  299. }
  300. } else if (type == 'avatar') {
  301. data['avatar'] = this.avatar
  302. }
  303. let res = await this.$myHttp.post({
  304. url: this.$myHttp.urlMap.editUserInfo,
  305. data: data,
  306. needLogin: true
  307. });
  308. if (res.code == 1) {
  309. if (type == 'nickname') {
  310. this.user_info.nickname = this.nickname
  311. this.$store.commit('setUserInfo', {
  312. userinfo: {
  313. ...this.userinfo,
  314. nickname: this.nickname
  315. }
  316. });
  317. uni.showToast({
  318. icon: 'none',
  319. title: '修改成功'
  320. });
  321. } else if (type == 'avatar') {
  322. this.get_user_info();
  323. this.$store.commit('setUserInfo', {
  324. userinfo: {
  325. ...this.userinfo,
  326. avatar: this.user_info.avatar
  327. }
  328. });
  329. uni.showToast({
  330. icon: 'success',
  331. title: '头像上传成功'
  332. })
  333. }
  334. }
  335. this.show_modal = false
  336. }
  337. }
  338. };
  339. </script>
  340. <style>
  341. page {
  342. background: #f6f7f9;
  343. }
  344. .kaoshi-head {
  345. border-bottom: none
  346. }
  347. .nc {
  348. position: relative;
  349. margin-right: 36px;
  350. margin-top: 12px;
  351. }
  352. .setting-tx {
  353. position: relative;
  354. }
  355. .setting-tx img {
  356. position: absolute;
  357. width: 32px;
  358. height: 32px;
  359. right: 21px;
  360. top: 7px;
  361. border-radius: 50%;
  362. }
  363. .lz-list-header {
  364. justify-content: space-between;
  365. }
  366. .tuichu {
  367. text-align: center;
  368. font-size: 16px;
  369. padding: 14px 0;
  370. color: #000;
  371. background: #fff;
  372. border-top: 12px solid #f6f7f9;
  373. }
  374. .lz-list-body {
  375. padding-bottom: 12px;
  376. }
  377. .lz-list-item .lz-list-line .lz-list-content {
  378. flex: 1;
  379. color: #000;
  380. font-size: 17px;
  381. line-height: 1.5;
  382. text-align: left;
  383. width: auto;
  384. overflow: hidden;
  385. text-overflow: ellipsis;
  386. white-space: nowrap;
  387. padding-top: 7px;
  388. padding-bottom: 7px;
  389. margin: 0 18px 12px;
  390. }
  391. .lz-list-body::before {
  392. border-bottom: none;
  393. }
  394. .lz-list-body::before {
  395. border-top: none;
  396. }
  397. .lz-modal {
  398. position: fixed;
  399. top: 0;
  400. right: 0;
  401. bottom: 0;
  402. left: 0;
  403. z-index: 999;
  404. opacity: 0;
  405. outline: 0;
  406. text-align: center;
  407. transform: scale(1.185);
  408. backface-visibility: hidden;
  409. perspective: 1000px;
  410. background: rgba(0, 0, 0, .4);
  411. transition: all .3s ease-in-out 0s;
  412. pointer-events: none;
  413. }
  414. .lz-modal .lz-modal-dialog {
  415. position: relative;
  416. display: inline-block;
  417. vertical-align: middle;
  418. margin-left: auto;
  419. margin-right: auto;
  420. width: 340px;
  421. max-width: 100%;
  422. background-color: #fff;
  423. border-radius: 10px;
  424. overflow: hidden;
  425. }
  426. .lz-modal::before {
  427. content: "\200B";
  428. display: inline-block;
  429. height: 100%;
  430. vertical-align: middle;
  431. }
  432. .lz-list-header {
  433. display: flex;
  434. flex-direction: row;
  435. width: 90%;
  436. margin: 0 auto;
  437. padding: 17px;
  438. align-items: center;
  439. }
  440. .lz-list-header .icon-cuo1 {
  441. font-size: 17px;
  442. color: #9e9e9e;
  443. }
  444. .lz-modal.lz-modal-show {
  445. opacity: 1;
  446. transition-duration: .3s;
  447. transform: scale(1);
  448. overflow-x: hidden;
  449. overflow-y: auto;
  450. pointer-events: auto;
  451. }
  452. /*个人设置样式 小程序2.27.1新增*/
  453. .seting_touxiang {
  454. width: 90%;
  455. padding: 0 5%;
  456. background: #fff;
  457. display: flex;
  458. align-items: center;
  459. border-bottom: #f2f2f2 solid 1px;
  460. height: 60px;
  461. }
  462. .seting_touxiang .t01 {
  463. flex: 1;
  464. font-size: 14px;
  465. }
  466. .seting_touxiang .t02 button::after {
  467. border: none;
  468. }
  469. .seting_touxiang .t02 button {
  470. padding-left: 0;
  471. padding-right: 0;
  472. line-height: 0;
  473. border-radius: 100px;
  474. overflow: hidden;
  475. }
  476. .seting_touxiang .t02 button image {
  477. width: 40px;
  478. height: 40px;
  479. display: block;
  480. }
  481. .seting_nc {
  482. width: 90%;
  483. padding: 0 5%;
  484. background: #fff;
  485. display: flex;
  486. align-items: center;
  487. height: 60px;
  488. }
  489. .seting_nc .n01 {
  490. flex: 1;
  491. font-size: 14px;
  492. }
  493. .seting_nc .n02 {
  494. flex: 1;
  495. }
  496. .seting_nc .n02 input {
  497. text-align: right;
  498. font-size: 14px;
  499. }
  500. .seting_button {
  501. width: 100%;
  502. margin-top: 18px;
  503. color: #fff;
  504. }
  505. .seting_button button::after {
  506. border: none;
  507. color: #fff;
  508. }
  509. .seting_button button {
  510. background: #3c7bfc !important;
  511. font-size: 16px;
  512. }
  513. </style>