recharge.vue 7.9 KB

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