recharge.vue 8.5 KB

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