edit.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!--
  2. 商城「我的」资料编辑:改昵称、看/改生日、退出登录。
  3. 改昵称成功后必须写入 vuex loginInfo,否则返回「我的」仍显示旧名字。
  4. -->
  5. <template>
  6. <div class="app-content">
  7. <form @submit="formSubmit">
  8. <div class="module-com input-line-wrap">
  9. <tui-list-cell class="line-cell" :hover="false">
  10. <div class="tui-title">昵称</div>
  11. <input v-model="form.name" placeholder-class="phcolor" class="tui-input" name="name" placeholder="请填写" type="nickname" />
  12. </tui-list-cell>
  13. <tui-list-cell class="line-cell" :hover="false">
  14. <div class="tui-title">生日</div>
  15. <div class="birthday-display" v-if="Number(userInfo.birthdayMonth)>0">
  16. {{ userInfo.lunar==1?"农历 ":"" }}{{ userInfo.birthdayMonth }}月{{ userInfo.birthdayDate }}日
  17. <text @click="goToBirthday()" style="color:#007aff;margin-left:20upx;">修改</text>
  18. </div>
  19. <div class="birthday-display" v-else>
  20. <text @click="goToBirthday()" style="color:#007aff;">去设置</text>
  21. </div>
  22. </tui-list-cell>
  23. <div class="btn-wrap">
  24. <button class="button-com red big" formType="submit">确认</button>
  25. </div>
  26. <div class="btn-wrap">
  27. <button class="button-com default big" @click="logout">退出登录</button>
  28. </div>
  29. </div>
  30. </form>
  31. </div>
  32. </template>
  33. <script>
  34. import TuiListCell from '@/components/plugin/list-cell'
  35. const form = require('@/utils/formValidation.js')
  36. import { currentInfo,updateInfo,clearLogin } from "@/api/user";
  37. export default {
  38. name: 'edit',
  39. components: {
  40. TuiListCell
  41. },
  42. data() {
  43. return {
  44. form: {
  45. name: ''
  46. },
  47. userInfo:{}
  48. }
  49. },
  50. onLoad() {
  51. //返回上一页时刷新
  52. let pages = getCurrentPages();
  53. let prevPage = pages[ pages.length - 2 ];
  54. prevPage.$vm.pageAction = {refresh:1}
  55. },
  56. onShow(){
  57. this.getLoginInfo();
  58. },
  59. methods: {
  60. logout(){
  61. let that = this
  62. that.$util.confirmModal({content:'确认退出?'},() => {
  63. // #ifdef APP-PLUS
  64. uni.clearStorage()
  65. that.$store.commit("setLoginInfo", {})
  66. plus.runtime.restart()
  67. // #endif
  68. // #ifdef MP-WEIXIN
  69. clearLogin().then(res=>{
  70. if(res.code == 1){
  71. uni.clearStorage()
  72. that.$store.commit("setLoginInfo", {})
  73. uni.reLaunch({url:'/pages/home/recent'})
  74. }
  75. })
  76. // #endif
  77. })
  78. },
  79. init(){
  80. //this.getLoginInfo();
  81. },
  82. /**
  83. * 拉取当前登录用户信息:回填生日展示,并把已有昵称写入 form.name 供输入框渲染
  84. */
  85. getLoginInfo(){
  86. currentInfo().then(res=>{
  87. if(res.code == 1){
  88. const info = res.data && res.data.info ? res.data.info : {}
  89. this.userInfo = info
  90. // 昵称绑定在 form.name,未赋值时输入框一直空白
  91. this.form.name = info.name != null ? String(info.name) : ''
  92. }
  93. })
  94. },
  95. goToBirthday() {
  96. this.pageTo({ url: '/pages/user/birthday'})
  97. },
  98. /**
  99. * 提交昵称:校验后调 /user/update,并把新名字写入 loginInfo(含本地缓存)。
  100. * 「我的」页读的是 loginInfo.name,只改接口不写 vuex 会看起来没更新。
  101. */
  102. formSubmit(e) {
  103. let rules = [
  104. {
  105. name: 'name',
  106. rule: ['required'],
  107. msg: ['请输入昵称']
  108. }
  109. ]
  110. let formData = (e && e.detail && e.detail.value) ? e.detail.value : this.form
  111. let checkRes = form.validation(formData, rules)
  112. if (checkRes) {
  113. this.$msg(checkRes)
  114. return
  115. }
  116. const name = String(formData.name != null ? formData.name : this.form.name).trim()
  117. updateInfo({ name }).then(res => {
  118. if (res.code == 1) {
  119. const loginInfo = Object.assign({}, this.$store.getters.getLoginInfo || {}, { name })
  120. this.$store.commit('setLoginInfo', loginInfo)
  121. this.$msg('已修改')
  122. setTimeout(() => {
  123. uni.navigateBack({})
  124. }, 1000)
  125. }
  126. })
  127. }
  128. }
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. // 公共
  133. .line-cell {
  134. .tui-title {
  135. width: 210upx;
  136. color: $fontColor2;
  137. line-height: 80upx;
  138. }
  139. .tui-input {
  140. width: calc(100% - 210upx);
  141. font-size: 28upx;
  142. min-height: 80upx;
  143. padding: 20upx;
  144. }
  145. .tui-placeholder {
  146. color: #ccc;
  147. }
  148. .phcolor {
  149. color: #ccc;
  150. }
  151. .birthday-display {
  152. min-height: 80upx;
  153. padding: 20upx;
  154. display: flex;
  155. align-items: center;
  156. font-size: 28upx;
  157. }
  158. }
  159. .btn-wrap {
  160. width: 90%;
  161. margin: 60upx auto;
  162. .button-com {
  163. width: 100%;
  164. margin: 0 auto;
  165. min-height: 100upx;
  166. padding: 30upx;
  167. font-size: 32upx;
  168. display: flex;
  169. align-items: center;
  170. justify-content: center;
  171. }
  172. }
  173. .code-wrap {
  174. .tui-input {
  175. width: calc(100% - 410upx);
  176. font-size: 28upx;
  177. }
  178. .tui-code {
  179. width: 200upx;
  180. text-align: center;
  181. border-left: 1upx solid $borderColor;
  182. color: $mainColor;
  183. }
  184. }
  185. </style>