editInfo.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div class="edit-box" v-if="value !== null">
  3. <back-bar :title="`${$t('h5.edit')} ${$t(`h5.${type}`)}`"></back-bar>
  4. <el-input :placeholder="`${$t('h5.input')} ${$t(`h5.${type}`)}`" v-model="value" clearable autofocus></el-input>
  5. <div class="submit-wrap">
  6. <el-button type="primary" size="medium" @click="submit">完成</el-button>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import Vue from 'vue'
  12. import API from '@/api'
  13. import { Input } from 'element-ui'
  14. import backBar from '@/components/backBar'
  15. import { mapState, mapMutations, mapActions } from 'vuex'
  16. Vue.component(Input.name, Input)
  17. export default {
  18. name: 'editNickname',
  19. components: {
  20. backBar
  21. },
  22. computed: {
  23. ...mapState({
  24. group: state => state.group,
  25. userInfo: state => state.userInfo,
  26. groupName: state => state.group.groupName,
  27. groupNotice: state => state.group.groupNotice,
  28. nickname: state => state.userInfo.nick_name,
  29. username: state => state.userInfo.user_name
  30. }),
  31. type () {
  32. return this.$route.params.type
  33. }
  34. },
  35. data () {
  36. return {
  37. value: null
  38. }
  39. },
  40. methods: {
  41. ...mapMutations([
  42. 'updateGroup',
  43. 'setUserNickName',
  44. 'updateMemberInfo',
  45. 'setUserUserName',
  46. 'initGroup',
  47. 'changeSessionId'
  48. ]),
  49. ...mapActions([
  50. 'updateSessionItem',
  51. 'getUserInfo',
  52. 'getGroupInfo'
  53. ]),
  54. submit () {
  55. if (!this.value.length) {
  56. this.$showTips(this.$t('userinfo.inputIsEmpty'))
  57. return
  58. }
  59. let value = this.value
  60. switch (this.type) {
  61. case 'nickname':
  62. if (value.length > 16) {
  63. this.$showTips(this.$t('userinfo.nickTooLong'))
  64. return
  65. }
  66. this.changeNickname(value)
  67. break
  68. case 'username':
  69. if (/^[a-zA-Z_0-9-]{5,20}$/i.test(value)) {
  70. this.changeUsername(value)
  71. } else {
  72. this.$showTips(this.$t('userinfo.wrongPattern'))
  73. }
  74. break
  75. case 'groupName':
  76. if (value.length > 16) {
  77. this.$showTips(this.$t('userinfo.groupNameTooLong'))
  78. return
  79. }
  80. this.changeGroupName(value)
  81. break
  82. case 'groupNotice':
  83. this.changeGroupNotice(value)
  84. break
  85. }
  86. },
  87. changeNickname (value) {
  88. API.user.changeNickName({
  89. nick_name: value
  90. }).then(({ data }) => {
  91. this.setUserNickName(value)
  92. this.updateMemberInfo({
  93. userId: this.$store.state.userId,
  94. nickName: value
  95. })
  96. this.successBack('/editMe')
  97. })
  98. },
  99. changeUsername (value) {
  100. API.user.changeUserName({
  101. user_name: value
  102. }).then(({ data }) => {
  103. this.setUserUserName(value)
  104. this.updateMemberInfo({
  105. userId: this.$store.state.userId,
  106. userName: value
  107. })
  108. this.successBack('/editMe')
  109. })
  110. },
  111. changeGroupName (value) {
  112. let { groupId } = this.group
  113. API.group.changeTitle({
  114. group_id: groupId,
  115. title: value
  116. }).then(({ data }) => {
  117. // 更新侧边栏
  118. this.updateSessionItem({
  119. sessionId: groupId,
  120. data: {
  121. name: value
  122. }
  123. })
  124. // 更新群组数据
  125. this.$store.commit('updateGroup', {
  126. key: 'groupName',
  127. data: value
  128. })
  129. this.successBack(`/groupSet/${groupId}`)
  130. })
  131. },
  132. changeGroupNotice (value) {
  133. let { groupId } = this.group
  134. API.group.changeNotice({
  135. group_id: groupId,
  136. notice: value
  137. }).then(() => {
  138. this.$showTips(this.$t('public.updateSucc'))
  139. this.$store.commit('updateGroup', {
  140. key: 'groupNotice',
  141. data: value
  142. })
  143. this.successBack(`/groupSet/${groupId}`)
  144. })
  145. },
  146. successBack (url) {
  147. this.$showTips(this.$t('public.updateSucc'))
  148. setTimeout(() => {
  149. this.$router.replace(url)
  150. }, 2000)
  151. }
  152. },
  153. async mounted () {
  154. if (this.type === 'nickname' || this.type === 'username') {
  155. !this.userInfo && await this.getUserInfo()
  156. } else {
  157. if (!this.group.groupId) {
  158. let groupId = this.$route.params.id
  159. this.initGroup({
  160. groupId: groupId,
  161. useCache: false
  162. })
  163. this.changeSessionId(groupId)
  164. await this.getGroupInfo()
  165. }
  166. }
  167. this.value = this[this.type]
  168. }
  169. }
  170. </script>
  171. <style lang="scss">
  172. .edit-box{
  173. .el-input__inner{
  174. height: px2rem(106);
  175. line-height: px2rem(106);
  176. border: none;
  177. border-radius: 0;
  178. }
  179. .submit-wrap{
  180. display: flex;
  181. justify-content: flex-end;
  182. margin-top: px2rem(10);
  183. padding: 0 px2rem(10);
  184. }
  185. }
  186. </style>