editInfo.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. 'updateMemberNickName',
  45. 'setUserUserName',
  46. 'updateMemberNickName',
  47. 'initGroup',
  48. 'changeSessionId'
  49. ]),
  50. ...mapActions([
  51. 'updateSessionItem',
  52. 'getUserInfo',
  53. 'getGroupInfo'
  54. ]),
  55. submit () {
  56. if (!this.value.length) {
  57. this.$showTips(this.$t('userinfo.inputIsEmpty'))
  58. return
  59. }
  60. let value = this.value
  61. switch (this.type) {
  62. case 'nickname':
  63. if (value.length > 16) {
  64. this.$showTips(this.$t('userinfo.nickTooLong'))
  65. return
  66. }
  67. this.changeNickname(value)
  68. break
  69. case 'username':
  70. if (/^[a-zA-Z_0-9-]{5,20}$/i.test(value)) {
  71. this.changeUsername(value)
  72. } else {
  73. this.$showTips(this.$t('userinfo.wrongPattern'))
  74. }
  75. break
  76. case 'groupName':
  77. if (value.length > 16) {
  78. this.$showTips(this.$t('userinfo.groupNameTooLong'))
  79. return
  80. }
  81. this.changeGroupName(value)
  82. break
  83. case 'groupNotice':
  84. this.changeGroupNotice(value)
  85. break
  86. }
  87. },
  88. changeNickname (value) {
  89. API.user.changeNickName({
  90. nick_name: value
  91. }).then(({ data }) => {
  92. this.setUserNickName(value)
  93. this.updateMemberNickName({
  94. userId: this.$store.state.userId,
  95. nickName: value
  96. })
  97. this.successBack('/editMe')
  98. })
  99. },
  100. changeUsername (value) {
  101. API.user.changeUserName({
  102. user_name: value
  103. }).then(({ data }) => {
  104. this.setUserUserName(value)
  105. this.updateMemberNickName({
  106. userId: this.$store.state.userId,
  107. userName: value
  108. })
  109. this.successBack('/editMe')
  110. })
  111. },
  112. changeGroupName (value) {
  113. let { groupId } = this.group
  114. API.group.changeTitle({
  115. group_id: groupId,
  116. title: value
  117. }).then(({ data }) => {
  118. // 更新侧边栏
  119. this.updateSessionItem({
  120. sessionId: groupId,
  121. data: {
  122. name: value
  123. }
  124. })
  125. // 更新群组数据
  126. this.$store.commit('updateGroup', {
  127. key: 'groupName',
  128. data: value
  129. })
  130. this.successBack(`/groupSet/${groupId}`)
  131. })
  132. },
  133. changeGroupNotice (value) {
  134. let { groupId } = this.group
  135. API.group.changeNotice({
  136. group_id: groupId,
  137. notice: value
  138. }).then(() => {
  139. this.$showTips(this.$t('public.updateSucc'))
  140. this.$store.commit('updateGroup', {
  141. key: 'groupNotice',
  142. data: value
  143. })
  144. this.successBack(`/groupSet/${groupId}`)
  145. })
  146. },
  147. successBack (url) {
  148. this.$showTips(this.$t('public.updateSucc'))
  149. setTimeout(() => {
  150. this.$router.replace(url)
  151. }, 2000)
  152. }
  153. },
  154. async mounted () {
  155. if (this.type === 'nickname' || this.type === 'username') {
  156. !this.userInfo && await this.getUserInfo()
  157. } else {
  158. if (!this.group.groupId) {
  159. let groupId = this.$route.params.id
  160. this.initGroup({
  161. groupId: groupId,
  162. useCache: false
  163. })
  164. this.changeSessionId(groupId)
  165. await this.getGroupInfo()
  166. }
  167. }
  168. this.value = this[this.type]
  169. }
  170. }
  171. </script>
  172. <style lang="scss">
  173. .edit-box{
  174. .el-input__inner{
  175. height: px2rem(106);
  176. line-height: px2rem(106);
  177. border: none;
  178. border-radius: 0;
  179. }
  180. .submit-wrap{
  181. display: flex;
  182. justify-content: flex-end;
  183. margin-top: px2rem(10);
  184. padding: 0 px2rem(10);
  185. }
  186. }
  187. </style>