user.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. bindAccount (type) {
  100. switch (type) {
  101. case 'eos':
  102. this.bindEos('eos')
  103. break
  104. case 'tg':
  105. this.bindTg()
  106. break
  107. case 'eth':
  108. this.bindEth()
  109. break
  110. case 'tron':
  111. this.bindTron()
  112. break
  113. case 'meetone':
  114. this.bindEos('meetone')
  115. break
  116. }
  117. },
  118. // eos/meetone绑定
  119. async bindEos (eosType) {
  120. setUserOpt('eosType', eosType)
  121. this.isLoading = true
  122. if (this.scatter) {
  123. this.doScatterBind().then((res) => {
  124. this.isLoading = false
  125. this.changeUserBinds({
  126. type: eosType,
  127. account: res.account
  128. })
  129. }).catch((e) => {
  130. showError(e)
  131. this.isLoading = false
  132. })
  133. } else {
  134. // 连接scatter
  135. await ScatterJS.scatter.connect('MEE_CHAT').then(async connected => {
  136. if (connected) {
  137. // 设置scatter
  138. this.setScatter(ScatterJS.scatter)
  139. // 清空全局scatter引用
  140. window.ScatterJS = null
  141. this.doScatterBind().then((res) => {
  142. this.isLoading = false
  143. this.changeUserBinds({
  144. type: eosType,
  145. account: res.account
  146. })
  147. }).catch((e) => {
  148. showError(e)
  149. this.isLoading = false
  150. })
  151. }
  152. })
  153. }
  154. },
  155. // telegram绑定
  156. async bindTg () {
  157. this.winHandler = openBlankWindow('')
  158. let { data } = await API.user.tgCSRF({
  159. type: 'bind'
  160. })
  161. this.winHandler.location.href = data.data.url
  162. var loop = setInterval(() => {
  163. if (this.winHandler != null && this.winHandler.closed) {
  164. clearInterval(loop)
  165. this.winHandler = null
  166. }
  167. }, 800)
  168. this.bindCheck(data.data.csrf_token)
  169. },
  170. // eth绑定
  171. async bindEth () {
  172. this.isLoading = true
  173. try {
  174. let { account, sign } = await EthHelper.initEth()
  175. await API.user.ethBind({
  176. account: account,
  177. sign: sign
  178. })
  179. await this.changeUserBinds({
  180. type: 'eth',
  181. account: account
  182. })
  183. } catch (e) {
  184. this.isLoading = false
  185. }
  186. this.isLoading = false
  187. },
  188. // tron绑定
  189. async bindTron () {
  190. this.isLoading = true
  191. try {
  192. let { account, sign } = await TronHelper.initTron()
  193. await API.user.tronBind({
  194. account: account,
  195. sign: sign
  196. })
  197. await this.changeUserBinds({
  198. type: 'tron',
  199. account: account
  200. })
  201. } catch (e) {
  202. }
  203. this.isLoading = false
  204. },
  205. async bindCheck (uuID, times) {
  206. this.isLoading = true
  207. if (times === 0) {
  208. // 第一次调用
  209. clearTimeout(this.timeoutHandler)
  210. } else if (times >= 60) {
  211. return false
  212. }
  213. let res = {}
  214. try {
  215. // 校验telegram
  216. res = await API.user.tgBind2({ csrf_token: uuID })
  217. } catch (ex) {
  218. this.isLoading = false
  219. }
  220. if (res.data && res.data.data && res.data.data.status > 0) {
  221. this.isLoading = false
  222. if (this.winHandler != null) {
  223. this.winHandler.close()
  224. this.winHandler = null
  225. }
  226. this.getSyncInfo && this.getSyncInfo(this.params)// 关联页
  227. this.getUserInfo()
  228. } else if (!res.data) {
  229. if (this.winHandler != null) {
  230. this.winHandler.close()
  231. this.winHandler = null
  232. }
  233. } else if ((this.winHandler != null) || (res.data && res.data.status == 0)) {
  234. // 定时检查是否登录成功
  235. this.timeoutHandler = setTimeout(() => {
  236. this.bindCheck(uuID, ++times)
  237. }, 1000)
  238. }
  239. },
  240. unbindAccount (type) {
  241. confirmPopup(`${this.$t('userinfo.unbindMsg')} ${type.toLocaleUpperCase()} ?`).then(() => {
  242. API.user.unBind({
  243. type
  244. }).then(() => {
  245. if (type == 'eos') this.scatter && this.scatter.logout && this.scatter.logout()
  246. this.$showTips(this.$t('userinfo.unbindSuccess'))
  247. this.changeUserBinds({
  248. type: type,
  249. account: ''
  250. })
  251. })
  252. })
  253. }
  254. }
  255. }