金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
13 KiB

  1. <template>
  2. <view id="recharge-index">
  3. <view class="contents">
  4. <!-- 这里不知道为什么title的样式没有用我先用行内样式解决 -->
  5. <view class="mx-1px-bottom title" style="font-size: 13px;background: #FFFFFF;font-weight: 600;">
  6. <!-- <i class="iconfont icon-recharge"></i> -->
  7. <span>
  8. <block v-if="initInfo && initInfo.vip_plan_status">
  9. 会员账号
  10. </block>
  11. <block v-else>
  12. 会员余额充值
  13. </block>
  14. </span>
  15. </view>
  16. <view class="item mx-1px-bottom">
  17. <view class="left">
  18. 会员名
  19. </view>
  20. <view class="right">
  21. {{user.nick_name}}
  22. </view>
  23. </view>
  24. <view class="item mx-1px-bottom">
  25. <view class="left">
  26. 手机号
  27. </view>
  28. <view class="right">
  29. {{user.mobile}}
  30. </view>
  31. </view>
  32. <view class="item mx-1px-bottom">
  33. <view class="left">
  34. 当前余额
  35. </view>
  36. <view class="right">
  37. {{num/100}}
  38. </view>
  39. </view>
  40. <view class="tips">
  41. <block v-if="initInfo && !initInfo.vip_plan_status">
  42. * 会员充值概不退款
  43. </block>
  44. </view>
  45. <view class="money-box">
  46. <view class="money-item" v-for="(item, index) in schemes" :key="index" :data-amount="item.amount" :data-id="item.id" :data-payment="item.payment_amount" @tap="submitPay">
  47. <view class="order">
  48. <view class="old-Price" v-if="initInfo && initInfo.vip_plan_status">
  49. {{item.title}}
  50. </view>
  51. <view class="old-Price" v-else>
  52. {{item.amount/100}}
  53. </view>
  54. <view class="price">
  55. 售价{{item.payment_amount/100}}
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <view class="tips" v-if="initInfo && initInfo.vip_plan_status">
  61. <view>
  62. * VIP享全场购物95折299元享全场购物88折
  63. </view>
  64. <view>
  65. * 多次购买会员余额累积到账VIP等级以最高等级权益为准
  66. </view>
  67. <view>
  68. * 会员账户余额可在平台购物时无限制使用
  69. </view>
  70. <view>
  71. * 会员账户余额不提现不退款
  72. </view>
  73. </view>
  74. </view>
  75. </view>
  76. </template>
  77. <script>
  78. const app = getApp();
  79. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  80. export default {
  81. data() {
  82. return {
  83. num: '',
  84. schemes: [],
  85. user: "",
  86. token: "",
  87. channel: 'wx_lite',
  88. initInfo: '',
  89. order_no:''
  90. };
  91. },
  92. onLoad(e) {
  93. pageLogin(getUrl(), token => {
  94. this.token = token;
  95. this.querySchemes();
  96. this.queryUserInfo(token);
  97. this.queryUserSum(token);
  98. });
  99. },
  100. methods: {
  101. queryUserSum(token) {
  102. // 获取用户余额
  103. this.$http.get({
  104. api: 'api/users/balance/sum',
  105. header: {
  106. Authorization: token
  107. }
  108. }).then(res => {
  109. if (res.statusCode == 200) {
  110. this.setData({
  111. num: res.data.data.sum
  112. });
  113. } else {
  114. wx.showModal({
  115. title: '提示',
  116. content: '数据请求失败',
  117. success: res => {}
  118. });
  119. }
  120. });
  121. },
  122. getOpenid() {
  123. return new Promise((resolve, reject) => {
  124. wx.login({
  125. success: res => {
  126. this.$http.get({
  127. api: `api/oauth/miniprogram/openid`,
  128. data: {
  129. code: res.code
  130. }
  131. }).then(res => {
  132. res = res.data;
  133. resolve(res.data.openid);
  134. }).catch(() => {
  135. reject('获取openid失败');
  136. });
  137. },
  138. fail: () => {
  139. wx.showModal({
  140. content: "接口请求失败"
  141. });
  142. }
  143. });
  144. });
  145. },
  146. querySchemes() {
  147. this.$http.get({
  148. api: 'api/users/balance/schemes'
  149. }).then(res => {
  150. if (res.statusCode == 200) {
  151. this.setData({
  152. schemes: res.data.data
  153. }); // this.schemes=res.data.data;
  154. } else {
  155. wx.showModal({
  156. title: '提示',
  157. content: '数据请求失败',
  158. success: res => {}
  159. });
  160. }
  161. });
  162. },
  163. queryUserInfo(token) {
  164. this.$http.get({
  165. api: 'api/me',
  166. header: {
  167. Authorization: token
  168. }
  169. }).then(res => {
  170. if (res.statusCode == 200) {
  171. this.setData({
  172. user: res.data.data
  173. });
  174. } else {
  175. wx.showModal({
  176. title: '提示',
  177. content: '数据请求失败',
  178. success: res => {}
  179. });
  180. }
  181. });
  182. },
  183. urlPath(url) {
  184. // console.log()
  185. let data = {};
  186. let query = url.slice(url.indexOf("?") + 1);
  187. query = query.split("&");
  188. query.forEach(val => {
  189. data[val.slice(0, val.indexOf("="))] = val.slice(val.indexOf("=") + 1);
  190. }); // console.log(query);
  191. return data;
  192. },
  193. // #ifdef MP-WEIXIN
  194. submitPay(e) {
  195. wx.showLoading();
  196. this.getOpenid().then(res => {
  197. var oauth = this.$cookieStorage.get('user_token');
  198. var data = {
  199. amount: e.currentTarget.dataset.amount,
  200. pay_amount: e.currentTarget.dataset.payment,
  201. channel: this.channel,
  202. recharge_rule_id: e.currentTarget.dataset.id,
  203. extra:{
  204. openid: res,
  205. }
  206. };
  207. this.$http.post({
  208. api: `api/users/balance/charge`,
  209. data: data,
  210. header: {
  211. Authorization: oauth
  212. }
  213. }).then(res => {
  214. res = res.data;
  215. if (res.status) {
  216. if (res.data.name == 'pingxx') {
  217. wx.showModal({
  218. content: '不支持pxx支付',
  219. showCancel: false
  220. });
  221. } else {
  222. this.newcharge(true, res.data.charge);
  223. }
  224. } else {
  225. this.newcharge(false, res.message);
  226. }
  227. }).catch(rej => {
  228. this.newcharge(false);
  229. });
  230. wx.hideLoading();
  231. }).catch(() => {
  232. wx.hideLoading();
  233. wx.showModal({
  234. content: '支付失败',
  235. showCancel: false
  236. });
  237. });
  238. },
  239. // #endif
  240. // #ifdef H5
  241. submitPay(e){
  242. var oauth = this.$cookieStorage.get('user_token');
  243. var openid = this.$cookieStorage.get('openid');
  244. var origin = window.location.origin;
  245. var url = origin+'/pages/recharge/success/success?order_no='+this.order_no;
  246. var fail_url = origin+'/pages/user/personal/personal';
  247. var data = {
  248. amount: e.currentTarget.dataset.amount,
  249. pay_amount: e.currentTarget.dataset.payment,
  250. channel: 'wx_pub',
  251. recharge_rule_id: e.currentTarget.dataset.id,
  252. extra:{
  253. openid:openid,
  254. successUrl:url,
  255. failUrl:fail_url
  256. }
  257. };
  258. this.$http.post({
  259. api: `api/users/balance/charge`,
  260. data: data,
  261. header: {
  262. Authorization: oauth
  263. }
  264. }).then(res => {
  265. res = res.data;
  266. if (res.status) {
  267. if(res.data.redirect_url){
  268. window.location.href = res.data.redirect_url;
  269. }
  270. } else {
  271. wx.showModal({
  272. content: res.message || '支付失败',
  273. showCancel: false
  274. });
  275. }
  276. }).catch(() => {
  277. this.loading = false;
  278. wx.showModal({
  279. content: '支付失败',
  280. showCancel: false
  281. });
  282. });
  283. },
  284. // #endif
  285. // #ifdef APP-PLUS
  286. submitPay(e) {
  287. wx.showLoading();
  288. var oauth = this.$cookieStorage.get('user_token');
  289. var data = {
  290. amount: e.currentTarget.dataset.amount,
  291. pay_amount: e.currentTarget.dataset.payment,
  292. channel:'wx_app',
  293. recharge_rule_id: e.currentTarget.dataset.id,
  294. };
  295. this.$http.post({
  296. api: `api/users/balance/charge`,
  297. data: data,
  298. header: {
  299. Authorization: oauth
  300. }
  301. }).then(res => {
  302. res = res.data;
  303. if (res.status) {
  304. this.order_no = res.data.order_no;
  305. this.appPayment(true,JSON.stringify(res.data.credential));
  306. } else {
  307. this.appPayment(false, res.message);
  308. }
  309. }).catch(rej => {
  310. this.appPayment(false);
  311. });
  312. wx.hideLoading();
  313. },
  314. // #endif
  315. // app支付
  316. appPayment(success,orderInfo) {
  317. var that = this;
  318. if (success) {
  319. // 获取服务供应商
  320. uni.getProvider({
  321. service:'payment',
  322. success:ret=>{
  323. uni.requestPayment({
  324. provider:'wxpay',
  325. orderInfo:orderInfo,
  326. success: res => {
  327. if (res.errMsg == 'requestPayment:ok') {
  328. this.loading = false;
  329. wx.redirectTo({
  330. url: `/pages/recharge/success/success?order_no=${this.order_no}`
  331. });
  332. } else {
  333. wx.showModal({
  334. content: '调用支付失败!',
  335. showCancel: false
  336. });
  337. }
  338. },
  339. fail: err => {
  340. this.setData({
  341. loading: false
  342. });
  343. console.log('fail:' + JSON.stringify(err));
  344. if (err.errMsg == 'requestPayment:fail:[payment微信:-2]User canceled') {
  345. wx.switchTab({
  346. url: `/pages/user/personal/personal`
  347. });
  348. } else {
  349. wx.showModal({
  350. content: '调用支付失败!',
  351. showCancel: false
  352. });
  353. }
  354. }
  355. });
  356. }
  357. })
  358. } else {
  359. this.setData({
  360. loading: false
  361. });
  362. wx.showModal({
  363. content: charge || '请求支付失败,请重试!',
  364. showCancel: false
  365. });
  366. }
  367. },
  368. // 新版支付
  369. newcharge(success, charge) {
  370. if (success) {
  371. var that = this;
  372. if (charge.pay_scene == 'test') {
  373. wx.showModal({
  374. content: '不支持模拟支付',
  375. showCancel: false
  376. });
  377. this.setData({
  378. loading: false
  379. });
  380. } else {
  381. wx.requestPayment({
  382. "timeStamp": charge.credential.wechat.timeStamp,
  383. "nonceStr": charge.credential.wechat.nonceStr,
  384. "package": charge.credential.wechat.package,
  385. "signType": charge.credential.wechat.signType,
  386. "paySign": charge.credential.wechat.paySign,
  387. success: res => {
  388. if (res.errMsg == 'requestPayment:ok') {
  389. this.loading = false
  390. wx.redirectTo({
  391. url: `/pages/recharge/success/success?order_no=${charge.order_no}`
  392. });
  393. } else {
  394. wx.showModal({
  395. content: '调用支付失败!',
  396. showCancel: false
  397. });
  398. }
  399. },
  400. fail: err => {
  401. wx.hideLoading();
  402. if (err.errMsg == 'requestPayment:fail cancel') {
  403. wx.switchTab({
  404. url: `/pages/user/personal/personal`
  405. });
  406. } else {
  407. wx.showModal({
  408. content: '调用支付失败!',
  409. showCancel: false
  410. });
  411. }
  412. }
  413. });
  414. }
  415. } else {
  416. wx.hideLoading();
  417. wx.showModal({
  418. content: charge || '请求支付失败,请重试!',
  419. showCancel: false
  420. });
  421. }
  422. },
  423. setData: function (obj) {
  424. let that = this;
  425. let keys = [];
  426. let val, data;
  427. Object.keys(obj).forEach(function (key) {
  428. keys = key.split('.');
  429. val = obj[key];
  430. data = that.$data;
  431. keys.forEach(function (key2, index) {
  432. if (index + 1 == keys.length) {
  433. that.$set(data, key2, val);
  434. } else {
  435. if (!data[key2]) {
  436. that.$set(data, key2, {});
  437. }
  438. }
  439. data = data[key2];
  440. });
  441. });
  442. }
  443. },
  444. computed: {},
  445. watch: {}
  446. };
  447. </script>
  448. <style>
  449. #recharge-index .contents{height:100%;overflow:auto;box-sizing:border-box}#recharge-index .contents .title{display:flex;align-items:center;font-size:14px;padding:10px 15px}#recharge-index .contents .title i{font-size:20px;color:#000;margin-right:10px}#recharge-index .contents .item{color:#666;background:#FFF;padding:10px 15px;display:flex;align-items:center;font-size:12px}#recharge-index .contents .item .left{flex:1;font-size:14px}#recharge-index .contents .tips{padding:15px 10px;font-size:14px;color:#b2b2b2}#recharge-index .contents .money-box{font-size:0;padding:0 7px}#recharge-index .contents .money-box .money-item{display:inline-block;text-align:center;font-size:14px;width:33%;box-sizing:border-box;margin-bottom:15px;padding:0 7px}#recharge-index .contents .money-box .money-item .order{border:1px solid #1AAD19;border-radius:6px;padding:5px 6px;color:#1AAD19}#recharge-index .contents .money-box .money-item .order .price{min-height:20px;font-size:10px;display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:1;overflow:hidden;text-overflow:ellipsis}
  450. </style>