1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| <script> export default { data() { return { from_currentcy_index: 1, to_currentcy_index: 0, from_amount: 1, to_amount: 0, meta_amount: 0, cny_exchange_list: {}, currentcies: [{ "currency": "CNY", "name": "人民币" }, { "currency": "USD", "name": "美元" }, { "currency": "EUR", "name": "欧元" }, { "currency": "JPY", "name": "日元" }, { "currency": "HKD", "name": "港币" }, { "currency": "KRW", "name": "韩元" }, { "currency": "RUB", "name": "卢布" }, { "currency": "GBP", "name": "英镑" }, { "currency": "SGD", "name": "新加坡元" }, { "currency": "TWD", "name": "新台币" }, { "currency": "CAD", "name": "加拿大元" }, { "currency": "AUD", "name": "澳大利亚元" }, { "currency": "INR", "name": "印度卢比" }, { "currency": "THB", "name": "泰国铢" }, { "currency": "MOP", "name": "澳门元" }, { "currency": "NZD", "name": "新西兰元" }, { "currency": "SEK", "name": "瑞典克朗" }, ], foreign_exchanges:[] } }, onReady() { this.get_cny_exchange() }, computed: { reversed_meta_amount: function() {
return (1 / this.meta_amount).toFixed(4) } }, watch: { from_amount: function(val) { this.calc_exchange(false) } }, methods: { calc_exchange: function(init = true) { let from_currentcy_obj = this.cny_exchange_list[this.currentcies[this.from_currentcy_index].currency] let to_currentcy_obj = this.cny_exchange_list[this.currentcies[this.to_currentcy_index].currency]
this.meta_amount = (1 / from_currentcy_obj.rate * to_currentcy_obj.rate).toFixed(4) if (init) { this.to_amount = this.meta_amount } else { this.to_amount = (this.from_amount * this.meta_amount).toFixed(4) }
}, save_storage:function(){ this.$jsonp('https://api.jisuapi.com/exchange/single?currency=CNY&appkey=你申请的appkey').then((res) => { if (res.status == 0) { this.cny_exchange_list = res.result.list this.cny_exchange_list.CNY = { "name": "人民币", "rate": 1, "updatetime": this.cny_exchange_list.USD.updatetime } uni.setStorageSync('cny_exchange_storage', this.cny_exchange_list); uni.setStorageSync('exchanges_exp', this.get_now_timestamp()+1000*3600*2); this.calc_exchange() this.get_foreign_exchange() } else { uni.showModal({ title: "警告", content: "数据请求出错,请联系管理员", showCancel: false }) } }).catch(err => { console.log('有结单独的果吗?', err) }) }, get_now_timestamp:function(){ let d = new Date() return d.getTime() },
get_cny_exchange: function() { if(uni.getStorageSync('exchanges_exp')>this.get_now_timestamp()){ const cny_exchange_storage = uni.getStorageSync('cny_exchange_storage'); if (cny_exchange_storage) { this.cny_exchange_list = cny_exchange_storage this.calc_exchange() this.get_foreign_exchange() } else { this.save_storage() } }else{ this.save_storage() } }, from_currentcy_change: function(e) { this.from_currentcy_index = e.target.value this.calc_exchange(false) },
to_currentcy_change: function(e) { this.to_currentcy_index = e.target.value this.calc_exchange(false) }, get_foreign_exchange:function(){ this.currentcies.forEach((item, index)=>{ console.log(item, index) if(item.currency != "CNY"){ this.foreign_exchanges.push({ curreny:item.currency, name:item.name,s rate:(1/this.cny_exchange_list[item.currency].rate).toFixed(4), updatetime:this.cny_exchange_list[item.currency].updatetime, }) } }) } } } </script>
|