recharge.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <template>
  2. <view>
  3. <u-navbar title="充值"></u-navbar>
  4. <view class="recharge">
  5. <view class="title">选择充值金额</view>
  6. <p>当前余额{{detail.balance}}</p>
  7. <view class="rechargeMain">
  8. <view class="recharge-item" :class="!otherNum&&moneyActiveClass == item.id ? 'active' : ''"
  9. v-for="(item,index) in moneyList" :key="item.id" @click="moneyClick(item.id,item)">
  10. {{item.rechargeAmount}}
  11. <view class="amount">赠20元</view>
  12. </view>
  13. </view>
  14. <p>其他充值金额</p>
  15. <view
  16. :class="otherNum? 'active' : ''"
  17. class="recharge-input self-stop">
  18. <u-input v-model="otherNum" @input="ckInput" type="text" :border="true" />
  19. </view>
  20. <view class="title">选择支付方式</view>
  21. <view class="recharge-radio">
  22. <u-radio-group v-model="value2" @change="radioGroupChange" :wrap="true" width="100%">
  23. <u-radio active-color="#00B962" @change="radioChange" v-for="(item, index) in list" :key="index"
  24. :name="item.id" :disabled="item.disabled" width="100%">
  25. <view class="recharge-radio-item">
  26. <u-icon :name="item.icon" custom-prefix="custom-icon" :color="item.color" size="48">
  27. </u-icon>
  28. <view class="recharge-radio-name">
  29. {{item.name}}
  30. </view>
  31. </view>
  32. </u-radio>
  33. </u-radio-group>
  34. </view>
  35. <u-modal v-model="showModel"
  36. :show-cancel-button="true"
  37. @confirm="confirm"
  38. confirm-text="支付成功?"
  39. title="扫码支付" >
  40. <view style="
  41. text-align: center;
  42. ">
  43. <img id="qrcode2" :src="qrCodeImg" >
  44. </view>
  45. <view style="
  46. text-align: center;
  47. ">请使用支付宝扫码</view>
  48. </u-modal>
  49. <img id="qrcode" style="display: none;" >
  50. <view class="recharge-btn">
  51. <u-checkbox-group>
  52. <u-checkbox v-model="checked" shape="circle" @change="checkboxChange()">我已阅读并同意《充值协议》</u-checkbox>
  53. </u-checkbox-group>
  54. <u-button class="success-btn" shape="circle" type="success" @click="rechargeNow">
  55. <span>立即充值</span>
  56. </u-button>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import * as Pay from '@/apis/weixin.js'
  63. import QRCode from 'qrcodejs2'
  64. import {
  65. convertCanvasToImage,
  66. } from '@/utils'
  67. import {
  68. wxPayJs
  69. } from '@/utils/wxpay'
  70. import * as API from '@/apis/finance.js'
  71. export default {
  72. data() {
  73. return {
  74. showModel:false,
  75. selectItem:{},
  76. userId: '',
  77. detail: {},
  78. moneyActiveClass: 1,
  79. submitForm:{},
  80. qrCodeImg:"",
  81. moneyList: [
  82. ],
  83. list: [{
  84. id:'0',
  85. name: '微信支付',
  86. icon: 'wechat-pay-fill',
  87. color: '#22ac38',
  88. },
  89. {
  90. id:'1',
  91. name: '支付宝支付',
  92. icon: 'alipay-fill',
  93. color: '#1677ff',
  94. },
  95. ],
  96. // u-radio-group的v-model绑定的值如果设置为某个radio的name,就会被默认选中
  97. value2: '0',
  98. otherNum:"",
  99. value1: '',
  100. checked: true,
  101. }
  102. },
  103. onReady() {
  104. if (this.carhelp.getPersonInfo()) {
  105. this.userId = this.carhelp.getPersonInfo().id;
  106. }
  107. this.init();
  108. },
  109. methods: {
  110. confirm(){
  111. },
  112. ckInput(text){
  113. if(text.indexOf('.')>0){
  114. this.$nextTick(()=>{
  115. this.otherNum=text.substring(0,text.indexOf('.'))
  116. uni.showToast({
  117. title:"请输入正整数"
  118. })
  119. })
  120. }
  121. var t =Number(text);
  122. if(t<1){
  123. this.$nextTick(()=>{
  124. this.otherNum='';
  125. })
  126. }
  127. if(t>500){
  128. this.$nextTick(()=>{
  129. this.otherNum=500;
  130. })
  131. }
  132. },
  133. init1() {
  134. uni.showLoading({
  135. title: "加载中",
  136. mask: true,
  137. })
  138. API.personalCenter().then((res) => {
  139. this.detail = res.data
  140. this.isReady = true;
  141. uni.hideLoading()
  142. }).catch(error => {
  143. uni.showToast({
  144. title: error
  145. })
  146. })
  147. },
  148. init() {
  149. uni.showLoading({
  150. title: "加载中",
  151. mask: true,
  152. })
  153. var data = {
  154. type:2 };
  155. API.marketingData(data).then((res) => {
  156. this.moneyList = res.data.chargingMarketingList
  157. if (this.moneyList.length > 0) {
  158. this.selectItem = this.moneyList[0];
  159. this.moneyActiveClass = this.moneyList[0].id
  160. console.log(this.moneyActiveClass)
  161. }
  162. uni.hideLoading()
  163. this.init1()
  164. }).catch(error => {
  165. uni.showToast({
  166. title: error
  167. })
  168. })
  169. },
  170. moneyClick(index,item) {
  171. this.moneyActiveClass = index;
  172. if(item){
  173. this.selectItem=item;
  174. }
  175. },
  176. // 选中某个单选框时,由radio时触发
  177. radioChange(e) {
  178. // console.log(e);
  179. },
  180. // 选中任一radio时,由radio-group触发
  181. radioGroupChange(e) {
  182. // console.log(e);
  183. },
  184. checkboxChange() {
  185. this.checked = !this.checked;
  186. },
  187. alpy(){
  188. uni.showLoading({
  189. title: "加载中",
  190. mask: true,
  191. })
  192. Pay.alpay(this.submitForm).then((response) => {
  193. let qrcode = new QRCode('qrcode', {
  194. width: 200,
  195. height: 200,
  196. text: response.data,
  197. correctLevel: QRCode.CorrectLevel.M,
  198. })
  199. var canvas = document.getElementsByTagName('canvas')[0];
  200. this.qrCodeImg = convertCanvasToImage(canvas);
  201. uni.hideLoading()
  202. this.showModel=true
  203. }).catch(error => {
  204. uni.showToast({
  205. title: error
  206. })
  207. })
  208. },
  209. wxpy(){
  210. uni.showLoading({
  211. title: "加载中",
  212. mask: true,
  213. })
  214. Pay.wxpay(this.submitForm).then((response) => {
  215. if(!response.result){
  216. uni.showToast({
  217. title: response.message
  218. })
  219. return
  220. }
  221. var data = response.data
  222. uni.hideLoading()
  223. console.log("Pay+"+new Date().getTime())
  224. wxPayJs(data);
  225. }).catch(error => {
  226. uni.showToast({
  227. title: error
  228. })
  229. })
  230. },
  231. rechargeNow() {
  232. this.submitForm={};
  233. if(!this.otherNum){
  234. this.submitForm.chargingMarketingId=this.selectItem.id;
  235. }else{
  236. this.submitForm.amount=this.otherNum
  237. }
  238. console.log(this.submitForm)
  239. if(this.value2=="0"){
  240. this.wxpy()
  241. }else{
  242. this.alpy()
  243. }
  244. }
  245. },
  246. onShow() {
  247. if (this.isReady) {
  248. this.init1()
  249. }
  250. }
  251. }
  252. </script>
  253. <style>
  254. page {
  255. background-color: #fff;
  256. }
  257. </style>
  258. <style lang="scss" scoped>
  259. /deep/.u-radio-group {
  260. width: 100%;
  261. }
  262. /deep/.u-radio {
  263. position: relative;
  264. }
  265. /deep/.u-radio__icon-wrap {
  266. position: absolute;
  267. right: 0;
  268. }
  269. .recharge {
  270. padding: 16px;
  271. .title {
  272. font-size: 16px;
  273. }
  274. p {
  275. color: #666;
  276. margin-top: 4px;
  277. }
  278. .self-stop.active{
  279. border: 1px solid;
  280. background-color: #EFFFF7;
  281. border-color: #00B962;
  282. color: #00B962;
  283. }
  284. .rechargeMain {
  285. display: flex;
  286. flex-wrap: wrap;
  287. justify-content: space-between;
  288. margin-top: 12px;
  289. margin-bottom: 20px;
  290. .recharge-item {
  291. width: 31%;
  292. border: 1px solid #e3e3e3;
  293. padding: 15px 0;
  294. border-radius: 4px;
  295. text-align: center;
  296. margin-bottom: 10px;
  297. font-size: 16px;
  298. position: relative;
  299. }
  300. .amount {
  301. // width: 41.9%;
  302. height: 16px;
  303. line-height: 15px;
  304. border-radius: 0px 4px 0px 4px;
  305. background-color: rgba(0, 185, 98, 100);
  306. color: rgba(255, 255, 255, 100);
  307. font-size: 10px;
  308. text-align: center;
  309. position: absolute;
  310. top: 0;
  311. right: 0;
  312. }
  313. .active {
  314. background-color: #EFFFF7;
  315. border-color: #00B962;
  316. color: #00B962;
  317. }
  318. }
  319. }
  320. .recharge-input {
  321. margin-top: 8px;
  322. margin-bottom: 32px;
  323. }
  324. .recharge-radio {
  325. margin-top: 10px;
  326. .recharge-radio-item {
  327. display: flex;
  328. align-items: center;
  329. }
  330. .recharge-radio-name {
  331. margin-left: 8px;
  332. }
  333. }
  334. .recharge-btn {
  335. position: fixed;
  336. left: 16px;
  337. right: 16px;
  338. bottom: 16px;
  339. }
  340. .success-btn {
  341. margin-top: 10px;
  342. background-color: #00B962 !important;
  343. flex: 1;
  344. border-color: #00B962 !important;
  345. color: #fff !important;
  346. }
  347. </style>