recharge.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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" v-if="item.giftAmount">{{item.giftAmount}}</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. submitNum:0,
  75. submitNumCk:1,
  76. showModel:false,
  77. selectItem:{},
  78. userId: '',
  79. detail: {},
  80. moneyActiveClass: 1,
  81. submitForm:{},
  82. qrCodeImg:"",
  83. moneyList: [
  84. ],
  85. list: [{
  86. id:'0',
  87. name: '微信支付',
  88. icon: 'wechat-pay-fill',
  89. color: '#22ac38',
  90. },
  91. {
  92. id:'1',
  93. name: '支付宝支付',
  94. icon: 'alipay-fill',
  95. color: '#1677ff',
  96. },
  97. ],
  98. // u-radio-group的v-model绑定的值如果设置为某个radio的name,就会被默认选中
  99. value2: '0',
  100. otherNum:"",
  101. value1: '',
  102. checked: true,
  103. }
  104. },
  105. onReady() {
  106. if (this.carhelp.getPersonInfo()) {
  107. this.userId = this.carhelp.getPersonInfo().id;
  108. }
  109. this.init();
  110. },
  111. methods: {
  112. confirm(){
  113. },
  114. ckInput(text){
  115. if(text.indexOf('.')>0){
  116. this.$nextTick(()=>{
  117. this.otherNum=text.substring(0,text.indexOf('.'))
  118. uni.showToast({
  119. title:"请输入正整数"
  120. })
  121. })
  122. }
  123. var t =Number(text);
  124. if(t<1){
  125. this.$nextTick(()=>{
  126. this.otherNum='';
  127. })
  128. }
  129. if(t>500){
  130. this.$nextTick(()=>{
  131. this.otherNum=500;
  132. })
  133. }
  134. },
  135. init1() {
  136. uni.showLoading({
  137. title: "加载中",
  138. mask: true,
  139. })
  140. API.personalCenter().then((res) => {
  141. this.detail = res.data
  142. this.isReady = true;
  143. uni.hideLoading()
  144. }).catch(error => {
  145. uni.showToast({
  146. title: error
  147. })
  148. })
  149. },
  150. init() {
  151. uni.showLoading({
  152. title: "加载中",
  153. mask: true,
  154. })
  155. var data = {
  156. type:2 };
  157. API.marketingData(data).then((res) => {
  158. this.moneyList = res.data.chargingMarketingList
  159. if (this.moneyList.length > 0) {
  160. this.selectItem = this.moneyList[0];
  161. this.moneyActiveClass = this.moneyList[0].id
  162. console.log(this.moneyActiveClass)
  163. }
  164. uni.hideLoading()
  165. this.init1()
  166. }).catch(error => {
  167. uni.showToast({
  168. title: error
  169. })
  170. })
  171. },
  172. moneyClick(index,item) {
  173. this.moneyActiveClass = index;
  174. if(item){
  175. this.selectItem=item;
  176. }
  177. },
  178. // 选中某个单选框时,由radio时触发
  179. radioChange(e) {
  180. // console.log(e);
  181. },
  182. // 选中任一radio时,由radio-group触发
  183. radioGroupChange(e) {
  184. // console.log(e);
  185. },
  186. checkboxChange() {
  187. this.checked = !this.checked;
  188. },
  189. alpy(){
  190. uni.showLoading({
  191. title: "加载中",
  192. mask: true,
  193. })
  194. Pay.alpay(this.submitForm).then((response) => {
  195. let qrcode = new QRCode('qrcode', {
  196. width: 200,
  197. height: 200,
  198. text: response.data,
  199. correctLevel: QRCode.CorrectLevel.M,
  200. })
  201. var canvas = document.getElementsByTagName('canvas')[0];
  202. this.qrCodeImg = convertCanvasToImage(canvas);
  203. uni.hideLoading()
  204. this.showModel=true
  205. }).catch(error => {
  206. uni.showToast({
  207. title: error
  208. })
  209. })
  210. },
  211. wxpy(){
  212. Pay.wxpay(this.submitForm).then((response) => {
  213. if(!response.result){
  214. uni.showToast({
  215. title: response.message
  216. })
  217. return
  218. }
  219. var data = response.data
  220. uni.hideLoading()
  221. console.log("Pay+"+new Date().getTime())
  222. wxPayJs(data);
  223. }).catch(error => {
  224. uni.showToast({
  225. title: error
  226. })
  227. })
  228. },
  229. rechargeNow() {
  230. uni.showLoading({
  231. title: "加载中",
  232. mask: true,
  233. })
  234. this.submitForm={};
  235. if(!this.otherNum){
  236. this.submitForm.chargingMarketingId=this.selectItem.id;
  237. }else{
  238. this.submitForm.amount=this.otherNum
  239. }
  240. console.log(this.submitForm)
  241. if(this.value2=="0"){
  242. this.wxpy()
  243. }else{
  244. this.alpy()
  245. }
  246. }
  247. },
  248. onShow() {
  249. if (this.isReady) {
  250. this.init1()
  251. }
  252. }
  253. }
  254. </script>
  255. <style>
  256. page {
  257. background-color: #fff;
  258. }
  259. </style>
  260. <style lang="scss" scoped>
  261. /deep/.u-radio-group {
  262. width: 100%;
  263. }
  264. /deep/.u-radio {
  265. position: relative;
  266. }
  267. /deep/.u-radio__icon-wrap {
  268. position: absolute;
  269. right: 0;
  270. }
  271. .recharge {
  272. padding: 16px;
  273. .title {
  274. font-size: 16px;
  275. }
  276. p {
  277. color: #666;
  278. margin-top: 4px;
  279. }
  280. .self-stop.active{
  281. border: 1px solid;
  282. background-color: #EFFFF7;
  283. border-color: #00B962;
  284. color: #00B962;
  285. }
  286. .rechargeMain {
  287. display: flex;
  288. flex-wrap: wrap;
  289. justify-content: space-between;
  290. margin-top: 12px;
  291. margin-bottom: 20px;
  292. .recharge-item {
  293. width: 31%;
  294. border: 1px solid #e3e3e3;
  295. padding: 15px 0;
  296. border-radius: 4px;
  297. text-align: center;
  298. margin-bottom: 10px;
  299. font-size: 16px;
  300. position: relative;
  301. }
  302. .amount {
  303. // width: 41.9%;
  304. height: 16px;
  305. line-height: 15px;
  306. border-radius: 0px 4px 0px 4px;
  307. background-color: rgba(0, 185, 98, 100);
  308. color: rgba(255, 255, 255, 100);
  309. font-size: 10px;
  310. text-align: center;
  311. position: absolute;
  312. top: 0;
  313. right: 0;
  314. }
  315. .active {
  316. background-color: #EFFFF7;
  317. border-color: #00B962;
  318. color: #00B962;
  319. }
  320. }
  321. }
  322. .recharge-input {
  323. margin-top: 8px;
  324. margin-bottom: 32px;
  325. }
  326. .recharge-radio {
  327. margin-top: 10px;
  328. .recharge-radio-item {
  329. display: flex;
  330. align-items: center;
  331. }
  332. .recharge-radio-name {
  333. margin-left: 8px;
  334. }
  335. }
  336. .recharge-btn {
  337. position: fixed;
  338. left: 16px;
  339. right: 16px;
  340. bottom: 16px;
  341. }
  342. .success-btn {
  343. margin-top: 10px;
  344. background-color: #00B962 !important;
  345. flex: 1;
  346. border-color: #00B962 !important;
  347. color: #fff !important;
  348. }
  349. </style>