contract.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <v-page>
  3. <view style="height: var(--status-bar-height)"></view>
  4. <!-- <view class="d-flex justify-center p-t-md">
  5. <view class="d-flex bg-form-panel-4 rounded-lg overflow-scroll">
  6. <view :class="{ 'bg-gradient-blue color-plain rounded-lg': tab1 == 0 }" @click="changeTab1(0)"
  7. class="p-y-xs p-x-lg min-w-60 fn-center">{{ $t('contract.title') }}</view>
  8. <view :class="{ 'bg-gradient-blue color-plain rounded-lg': tab1 == 1 }" @click="changeTab1(1)"
  9. class="p-y-xs p-x-lg min-w-60 fn-center">{{$t('miao.a1')}}</view>
  10. </view>
  11. </view> -->
  12. <!-- <v-header :left-arrow="false" :title="$t('contract.title')"></v-header> -->
  13. <template v-if="tab1 == 0">
  14. <van-tabs :ellipsis="false" :border="false" class="border-b" :active="tab"
  15. @change="tab = $event.detail.name">
  16. <van-tab :title="`${$t('contract.a0')}`" :name="0"></van-tab>
  17. <van-tab :title="`${$t('contract.a1')}(${symbolLeft})`" :name="1"></van-tab>
  18. <van-tab :title="$t('contract.a2')" :name="2"></van-tab>
  19. <van-tab :title="$t('contract.a3')" :name="3"></van-tab>
  20. <van-tab :title="`${$t('contract.a')}(${symbolLeft})`" :name="4"></van-tab>
  21. </van-tabs>
  22. <!-- 开仓 -->
  23. <view class="layout-main" v-if="tab == 0">
  24. <open-position :query="query" :collect="collect" @option="option" :isShow="isShow" ref="openposition"
  25. @symbol="symbolListShow = !symbolListShow" @getSymbolDetail="symbolDetail = $event" />
  26. </view>
  27. <!-- 持仓 -->
  28. <main class="layout-main" v-if="tab == 1">
  29. <position :isShow="isShow" :symbolDetail="symbolDetail" :symbolLeft="symbolLeft" />
  30. </main>
  31. <!-- 委托 -->
  32. <main class="layout-main" v-if="tab == 2">
  33. <contract-entrustment style="min-height: 100%" />
  34. </main>
  35. <!-- 历史 -->
  36. <main class="layout-main" v-if="tab == 3">
  37. <contract-history v-if="tab == 3" />
  38. </main>
  39. <!-- 流水 -->
  40. <main class="layout-main" v-if="tab == 4">
  41. <contract-bill :symbolLeft="symbolLeft" />
  42. </main>
  43. </template>
  44. <!-- 秒合约 -->
  45. <template v-if="tab1 == 1">
  46. <contract-miao ref="miao"/>
  47. </template>
  48. <van-popup :show="symbolListShow" @close="symbolListShow = false" closeable close-on-popstate position="left"
  49. custom-style="height:100%;width:70%">
  50. <symbol-list :collect="collect" :title="$t('contract.a4')" :marketList="marketList"
  51. @check-symbol="checkSymbol" />
  52. </van-popup>
  53. <van-dialog use-slot :show="contractStatusShow" :title="contractAgreement.title" @confirm="openContract"
  54. show-cancel-button @cancel="contractStatusShow = false">
  55. <view class="p-md overflow-scroll">
  56. <rich-text class="p-md" :nodes="contractAgreement.body"></rich-text>
  57. </view>
  58. </van-dialog>
  59. <ComFooter tab="contract"></ComFooter>
  60. </v-page>
  61. </template>
  62. <script>
  63. import openPosition from "@/pages/exchange/open-position";
  64. import position from "@/pages/exchange/position";
  65. import contractEntrustment from "@/pages/exchange/contract-entrustment";
  66. import contractHistory from "@/pages/exchange/contract-history";
  67. import symbolList from "@/pages/exchange/symbol-list";
  68. import contractBill from "@/pages/exchange/contract-bill";
  69. import ComFooter from "./components/footer.vue";
  70. import Home from "@/api/home";
  71. import Contract from "@/api/contract";
  72. import contractMiao from "@/pages/exchange/contract-miao"
  73. import {
  74. mapState
  75. } from "vuex";
  76. export default {
  77. name: "contract",
  78. props: {
  79. // isShow: {
  80. // default: false,
  81. // type: Boolean,
  82. // required: false,
  83. // },
  84. },
  85. components: {
  86. openPosition,
  87. position,
  88. contractEntrustment,
  89. contractHistory,
  90. symbolList,
  91. contractBill,
  92. ComFooter,
  93. contractMiao
  94. },
  95. data() {
  96. return {
  97. tab: 0,
  98. tab1: 0,
  99. collect: [],
  100. marketList: [],
  101. symbolListShow: false,
  102. contractStatusShow: false,
  103. contractAgreement: {},
  104. symbolDetail: {},
  105. query: {},
  106. isShow: false,
  107. };
  108. },
  109. onShow() {
  110. if(this.$refs.miao){
  111. if(this.tab1==0){
  112. this.linkSocket();
  113. }else{
  114. this.$refs.miao.linkSocket()
  115. }
  116. }else{
  117. this.$nextTick(()=>{
  118. if(this.tab1==0){
  119. this.linkSocket();
  120. }else{
  121. this.$refs.miao.linkSocket()
  122. }
  123. })
  124. }
  125. this.isShow = true;
  126. },
  127. onHide() {
  128. if(this.tab1==0){
  129. this.destroyed()
  130. }else{
  131. this.$refs.miao.destroyed()
  132. }
  133. this.isShow = false;
  134. },
  135. computed: {
  136. symbolLeft() {
  137. if (!this.query.symbol) return "";
  138. return this.query.symbol.split("/")[0];
  139. },
  140. isLogin() {
  141. return Boolean(uni.getStorageSync("token"));
  142. },
  143. ...mapState({
  144. ws: "ws1",
  145. }),
  146. },
  147. methods: {
  148. changeTab1(cur){
  149. this.tab1 = cur
  150. if(cur==0){
  151. this.getMarketList();
  152. this.getCollect();
  153. this.linkSocket()
  154. this.$refs.miao.destroyed()
  155. }else{
  156. this.$nextTick(()=>{
  157. this.$refs.miao.linkSocket()
  158. this.destroyed()
  159. })
  160. }
  161. },
  162. // 获取市场行情
  163. getMarketList() {
  164. Contract.getMarketList().then((res) => {
  165. // 整理数据格式
  166. this.marketList = res.data.map((item) => {
  167. item.marketInfoList.forEach((el) => {
  168. el.coin_name = el.symbol;
  169. });
  170. return item;
  171. });
  172. if (!this.query.symbol) {
  173. let parentItem = this.marketList[0].marketInfoList[0];
  174. this.checkSymbol(parentItem);
  175. }
  176. });
  177. },
  178. //
  179. checkSymbol(obj) {
  180. this.symbolListShow = false;
  181. if (obj.pair_name != this.query.symbol)
  182. this.query = {
  183. symbol: obj.pair_name
  184. };
  185. },
  186. // 获取自选列表
  187. getCollect() {
  188. if (!this.isLogin) return;
  189. Home.getCollect()
  190. .then((res) => {
  191. this.collect = res.data || [];
  192. })
  193. .catch(() => {});
  194. },
  195. // 链接socket
  196. linkSocket() {
  197. let msg = "swapMarketList";
  198. this.ws.send({
  199. cmd: "sub",
  200. msg: msg,
  201. });
  202. this.ws.on("message", (res) => {
  203. if (!this.isShow) return;
  204. let {
  205. data,
  206. sub
  207. } = res;
  208. if (sub == msg) {
  209. this.marketList = data.map((item) => {
  210. item.marketInfoList.forEach((el) => {
  211. el.coin_name = el.symbol;
  212. });
  213. return item;
  214. });
  215. }
  216. });
  217. },
  218. // 添加自选
  219. option() {
  220. let data = {
  221. pair_name: this.query.symbol,
  222. };
  223. Home.option(data)
  224. .then((res) => {
  225. this.getCollect();
  226. if (res.data) {
  227. this.$toast(this.$t("exchange.a6"));
  228. } else {
  229. this.$toast(this.$t("exchange.a7"));
  230. }
  231. })
  232. .catch(() => {});
  233. },
  234. // 获取开通永续合约
  235. openStatus() {
  236. if (!this.isLogin) return;
  237. Contract.openStatus({}).then((res) => {
  238. if (!res.data.open) {
  239. this.contractAgreement = res.data.contractAgreement;
  240. this.contractStatusShow = true;
  241. this.$refs.openposition.getSymbolDetail();
  242. }
  243. });
  244. },
  245. // 开通永续合约
  246. openContract() {
  247. Contract.opening().then(() => {
  248. this.$toast(this.$t("contract.a5"));
  249. this.contractStatusShow = false;
  250. });
  251. },
  252. destroyed() {
  253. this.ws.send({
  254. cmd: "unsub",
  255. msg: "swapMarketList",
  256. });
  257. },
  258. },
  259. created() {
  260. if(this.tab1==0){
  261. this.getMarketList();
  262. this.getCollect();
  263. }
  264. // this.openStatus();
  265. },
  266. };
  267. </script>