user.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { mapState, mapActions } from 'vuex'
  2. import API from '@/api'
  3. import User from '@/store/db/User.js'
  4. import { confirmPopup, openBlankWindow, getMeechatType } from '@/util/util'
  5. import ScatterJS from 'scatter-js/dist/scatter.esm'
  6. export const otherInfoMixins = {
  7. data () {
  8. return {
  9. accountList: null,
  10. userInfo: null,
  11. meechatType: getMeechatType()// meechat版本
  12. }
  13. },
  14. computed: {
  15. ...mapState({
  16. members: state => state.group.members,
  17. groupId: state => state.group.groupId,
  18. sessionList: state => state.chat.sessionList,
  19. meId: state => state.userId,
  20. meInfo: state => state.userInfo
  21. }),
  22. linkToOther () {
  23. let youId = this.userInfo.user_id
  24. let sessionId = Number(youId) < Number(this.meId) ? `${youId}-${this.meId}` : `${this.meId}-${youId}`
  25. return `${location.origin}/#/pm/${sessionId}`
  26. }
  27. },
  28. methods: {
  29. ...mapActions(['getUserInfo']),
  30. sendMsg () {
  31. this.visible = false
  32. let youId = this.userInfo.user_id
  33. let sessionId = Number(youId) < Number(this.meId)
  34. ? `${youId}-${this.meId}`
  35. : `${this.meId}-${youId}`
  36. let repeatFlag = this.sessionList.some(e => {
  37. return e.session_id == sessionId
  38. })
  39. if (!repeatFlag) {
  40. let obj = {
  41. cover_photo: this.userInfo.cover_photo,
  42. is_group: '0',
  43. name: this.userInfo.nick_name,
  44. // read_hash:null
  45. session_id: sessionId
  46. }
  47. this.$store.commit('addSessionItem', obj)
  48. }
  49. // 创建members,应对还没聊天记录的情况
  50. let myId = this.meInfo.user_id
  51. let members = {}
  52. members[myId] = {
  53. cover_photo: this.meInfo.cover_photo,
  54. nick_name: this.meInfo.nick_name
  55. }
  56. members[this.userInfo.user_id] = {
  57. cover_photo: this.userInfo.cover_photo,
  58. nick_name: this.userInfo.nick_name
  59. }
  60. this.$store.commit('updateGroup', {
  61. key: 'members',
  62. data: members
  63. })
  64. this.$store.commit('updateGroup', {
  65. key: 'privateName',
  66. data: this.userInfo.nick_name
  67. })
  68. this.$router.push({ path: `/pm/${sessionId}` })
  69. }
  70. },
  71. async created () {
  72. if (!this.meInfo) {
  73. await this.getUserInfo()
  74. }
  75. API.user.getOtherInfo({
  76. target_id: this.userId,
  77. group_id: this.groupId || null
  78. }).then(({ data }) => {
  79. this.accountList = data.data.binds
  80. this.userInfo = data.data
  81. this.visible = true
  82. if (data.data.user_id) {
  83. // 更新他人的信息
  84. let objUser = new User()
  85. let newData = {
  86. cover_photo: data.data.cover_photo,
  87. nick_name: data.data.nick_name,
  88. user_name: data.data.user_name
  89. }
  90. objUser.updateObject(newData, { user_id: data.data.user_id })
  91. }
  92. })
  93. }
  94. }
  95. // 绑定相关
  96. export const bindAccountMixins = {
  97. data () {
  98. return {
  99. isLoading: false
  100. }
  101. },
  102. computed: {
  103. ...mapState({
  104. scatter: state => state.scatter
  105. })
  106. },
  107. methods: {
  108. ...mapActions([
  109. 'setScatter',
  110. 'doScatterLogin',
  111. 'doScatterBind',
  112. 'getUserInfo'
  113. ]),
  114. async bindEos () {
  115. this.isLoading = true
  116. if (window.EOS) {
  117. this.doScatterBind().then(() => {
  118. this.isLoading = false
  119. this.getUserInfo()
  120. }).catch((e) => {
  121. console.log(e)
  122. this.isLoading = false
  123. })
  124. } else {
  125. // 连接scatter
  126. await ScatterJS.scatter.connect('MEE_CHAT').then(async connected => {
  127. if (connected) {
  128. // 设置scatter
  129. this.setScatter(ScatterJS.scatter)
  130. // 清空全局scatter引用
  131. window.ScatterJS = null
  132. this.doScatterBind().then(() => {
  133. this.isLoading = false
  134. this.getUserInfo()
  135. }).catch((e) => {
  136. console.log(e)
  137. this.isLoading = false
  138. })
  139. }
  140. })
  141. }
  142. },
  143. async bindTg () {
  144. this.winHandler = openBlankWindow('')
  145. let { data } = await API.user.tgCSRF({
  146. type: 'bind'
  147. })
  148. this.winHandler.location.href = data.data.url
  149. var loop = setInterval(() => {
  150. if (this.winHandler != null && this.winHandler.closed) {
  151. clearInterval(loop)
  152. this.winHandler = null
  153. }
  154. }, 800)
  155. this.bindCheck(data.data.csrf_token)
  156. },
  157. async bindCheck (uuID, times) {
  158. this.isLoading = true
  159. if (times === 0) {
  160. // 第一次调用
  161. clearTimeout(this.timeoutHandler)
  162. } else if (times >= 60) {
  163. return false
  164. }
  165. let res = {}
  166. try {
  167. // 校验telegram
  168. res = await API.user.tgBind2({ csrf_token: uuID })
  169. } catch (ex) {
  170. this.isLoading = false
  171. }
  172. if (res.data && res.data.data && res.data.data.status > 0) {
  173. this.isLoading = false
  174. if (this.winHandler != null) {
  175. this.winHandler.close()
  176. this.winHandler = null
  177. }
  178. this.getSyncInfo && this.getSyncInfo(this.params)// 关联页
  179. this.getUserInfo()
  180. } else if (!res.data) {
  181. if (this.winHandler != null) {
  182. this.winHandler.close()
  183. this.winHandler = null
  184. }
  185. } else if ((this.winHandler != null) || (res.data && res.data.status == 0)) {
  186. // 定时检查是否登录成功
  187. this.timeoutHandler = setTimeout(() => {
  188. this.bindCheck(uuID, ++times)
  189. }, 1000)
  190. }
  191. },
  192. unbindAccount (type) {
  193. confirmPopup(`${this.$t('userinfo.unbindMsg')} ${type.toLocaleUpperCase()} ?`).then(() => {
  194. API.user.unBind({
  195. type
  196. }).then(() => {
  197. if (type == 'eos') this.scatter && this.scatter.forgetIdentity && this.scatter.forgetIdentity()
  198. this.$showTips(this.$t('userinfo.unbindSuccess'))
  199. this.getUserInfo()
  200. })
  201. })
  202. }
  203. }
  204. }