user.js 6.1 KB

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