recharge.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <template>
  2. <view class="content">
  3. <view class="title">
  4. <text class="iconfont icon-qian"></text>
  5. <text>江西南昌交通学院</text>
  6. </view>
  7. <view class="input_amount">
  8. <view class="amount_tip">充值金额(元)</view>
  9. <view class="amount_inp">
  10. <text></text>
  11. <input type="number" maxlength="4" v-model="amount" @input="onInput" @blur="onBlur"
  12. placeholder="请输入大于10,小于100元" placeholder-class="ph_class" />
  13. </view>
  14. <text>最多可输入金额100元</text>
  15. </view>
  16. <view class="amount_select">
  17. <view class="amount_btn">
  18. <text @tap="sel_amount(10)" :class="{selStyle:amount == 10}">充10元</text>
  19. <text @tap="sel_amount(20)" :class="{selStyle:amount == 20}">充20元</text>
  20. <text @tap="sel_amount(50)" :class="{selStyle:amount == 50}">充50元</text>
  21. <text @tap="sel_amount(100)" :class="{selStyle:amount == 100}">充100元</text>
  22. </view>
  23. <view class="reminder">温馨提示:最少充值金额为10元</view>
  24. <view class="payment">支付金额:<text>{{amount}}</text>元</view>
  25. <button class="btn_submit" type="primary" @tap="chongzhi">确认提交</button>
  26. <button class="btn_tel" type="default" @tap="callPhone">客服热线:{{phone_number}}</button>
  27. <view class="tips">
  28. 尊敬的用户,你好!因项目提现业务配置问题导致您无法提现对此,我们深表歉意!您可致电运营商处理提现问题,运营商电话:13645689854。感谢您对我们工作的支持和理解。
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. amount: 10, // 金额
  38. phone_number: '13645689854', // 客户热线电话
  39. userinfo: '', // 用户信息
  40. order_id: '', // 订单号
  41. pay_info: '', // 支付参数。将此参数透传给支付 SDK 发起支付
  42. access_token: '',
  43. sub_appid: '',
  44. card_number: '',
  45. user_name: '',
  46. test: getApp().globalData.test // 获取全局变量,控制是否是测试环境
  47. };
  48. },
  49. onLoad(options) {
  50. try {
  51. // 获取存储的用户数据
  52. const value = uni.getStorageSync('userinfo_storage_key')
  53. let item = decodeURIComponent(options.item)
  54. // console.log(item)
  55. if (item === '{}' && value == '') { // 如果没有用户信息,返回首页
  56. uni.redirectTo({
  57. url: '../index/index'
  58. });
  59. return;
  60. }
  61. // 先用传过来的用户信息
  62. if (item != '') {
  63. // 处理JSON字符串
  64. // this.userinfo = item.replace(/"/g, "'");
  65. this.userinfo = JSON.parse(item)
  66. // 更新存储的用户信息
  67. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  68. } else if (JSON.stringify(value) !== '{}') {
  69. // 处理JSON字符串
  70. // this.userinfo = value.replace(/"/g, "'");
  71. this.userinfo = value;
  72. }
  73. } catch (e) {
  74. console.log(e)
  75. }
  76. if (this.test) //测试环境
  77. this.amount = 0.01
  78. this.sub_appid = this.userinfo.sub_appid
  79. this.card_number = this.userinfo.card_number
  80. this.user_name = this.userinfo.name
  81. // 获取应用有效凭证
  82. this.get_valid_credentials()
  83. },
  84. methods: {
  85. /**
  86. * 获取应用有效凭证
  87. */
  88. async get_valid_credentials() {
  89. const res = await this.$myRequest({
  90. host: 'wecard',
  91. url: '/cgi-bin/oauth2/token',
  92. method: 'POST',
  93. header: {
  94. 'content-type': 'application/json'
  95. },
  96. data: {
  97. 'app_key': this.userinfo.appkey,
  98. 'app_secret': this.userinfo.app_secret,
  99. 'grant_type': 'client_credentials',
  100. 'scope': 'base',
  101. 'ocode': this.userinfo.ocode
  102. }
  103. });
  104. if (typeof(res.data.access_token) != 'undefined') {
  105. // 获取到应用有效凭证,保存到页面变量中
  106. this.access_token = res.data.access_token
  107. } else {
  108. uni.showToast({
  109. title: '获取凭证失败',
  110. duration: 2000
  111. })
  112. }
  113. },
  114. /**
  115. * 组合地址,发起支付
  116. */
  117. async jsapi() {
  118. if (this.test) { // 测试环境
  119. const res = await this.$myRequest({
  120. host: 'wecard',
  121. url: '/cgi-bin/pay/app/mppay',
  122. method: 'POST',
  123. header: {
  124. 'content-type': 'application/json'
  125. },
  126. data: {
  127. 'access_token': this.access_token,
  128. 'sub_appid': this.sub_appid,
  129. 'user_id': this.card_number,
  130. 'order_id': this.order_id = this.$getOrderId('shuifei'),
  131. 'amount': this.amount * 100,
  132. 'order_type': 1,
  133. 'callback_url': 'http://wvwviw.natappfree.cc/HotWaters/wxpayrecharge.action'
  134. }
  135. })
  136. } else {
  137. const res = await this.$myRequest({
  138. host: 'wecard',
  139. url: '/cgi-bin/pay/app/mppay',
  140. method: 'POST',
  141. header: {
  142. 'content-type': 'application/json'
  143. },
  144. data: {
  145. 'access_token': this.access_token,
  146. 'sub_appid': this.sub_appid,
  147. 'user_id': this.card_number,
  148. 'order_id': this.order_id = this.$getOrderId('shuifei'),
  149. 'amount': this.amount * 100,
  150. 'order_type': 1,
  151. 'callback_url': this.$code_base_url + '/HotWater/wxpayrecharge.action'
  152. }
  153. })
  154. }
  155. this.pay_info = res.data.data.pay_info
  156. // 发起支付
  157. this.pay_amount()
  158. },
  159. // 调起支付
  160. pay_amount() {
  161. var OpenMidas = require("@/static/openMidas.js") // 引入小程序目录下的SDK文件
  162. if (this.test) { // 测试环境
  163. // 设置支付配置
  164. wx['OpenMidasConfig'] = {
  165. apiCommonConf: {
  166. version: "weixiao"
  167. },
  168. cgiDomain: {
  169. test: "midas.weixiao.qq.com/api", // 私有化参数联系微卡客服进行获取
  170. },
  171. webDomain: "https://midas.weixiao.qq.com/h5", // 私有化参数联系微卡客服进行获取
  172. sandboxWebDomain: "https://midas.weixiao.qq.com/h5" // 私有化参数联系微卡客服进行获取
  173. }
  174. var payInfo = this.pay_info; // 请求mppay接口返回的数据
  175. // var appMetaData = "card_number=" + this.card_number + "&user_name=" + this.user_name + "&account=" + this
  176. // .amount; // 自定义回调数据
  177. OpenMidas.init("test");
  178. } else {
  179. // 设置支付配置
  180. wx['OpenMidasConfig'] = {
  181. apiCommonConf: {
  182. version: "weixiao"
  183. },
  184. cgiDomain: {
  185. release: "midas.weixiao.qq.com/api", // 私有化参数联系微卡客服进行获取
  186. },
  187. webDomain: "https://midas.weixiao.qq.com/h5", // 私有化参数联系微卡客服进行获取
  188. sandboxWebDomain: "https://midas.weixiao.qq.com/h5" // 私有化参数联系微卡客服进行获取
  189. }
  190. var payInfo = this.pay_info; // 请求mppay接口返回的数据
  191. // var appMetaData = "card_number=" + this.card_number + "&user_name=" + this.user_name + "&account=" + this
  192. // .amount; // 自定义回调数据
  193. OpenMidas.init("release");
  194. }
  195. OpenMidas.pay(
  196. payInfo,
  197. // function(resultCode, innerCode, resultMsg, appMetaData) {
  198. function(resultCode, innerCode, resultMsg) {
  199. // console.log(resultCode); // 支付响应状态码
  200. // console.log(innerCode); // 支付响应内部错误码
  201. // console.log(resultMsg); // 支付响应说明
  202. // console.log(appMetaData); // 自定义回调数据
  203. this.$store.state.payInfo.from = 'reshui_pay'
  204. this.$store.state.payInfo.resultCode = resultCode
  205. this.$store.state.payInfo.resultMsg = resultMsg
  206. uni.navigateBack({
  207. delta: 1
  208. })
  209. }
  210. // },
  211. // appMetaData
  212. )
  213. },
  214. /**
  215. * 输入充值金额
  216. */
  217. onInput(e) {
  218. if (this.test) { // 测试环境
  219. return
  220. }
  221. const v = e.detail.value
  222. this.amount = 10
  223. const zero = /^(0{1,})|[^0-9]/g
  224. let final = 0
  225. if (!v) {
  226. final = 0
  227. } else {
  228. final = v.toString().replace(zero, (v) => {
  229. return 0
  230. })
  231. if (final.split('')[0] * 1 === 0) {
  232. final = final.slice(1) - 0 || 0
  233. }
  234. if (final > 100) {
  235. final = 100
  236. }
  237. }
  238. this.$nextTick(() => {
  239. this.amount = final.toString() || '0'
  240. })
  241. },
  242. /**
  243. * 输入框,失去焦点时
  244. */
  245. onBlur() {
  246. if (this.test) { // 测试环境
  247. return
  248. }
  249. if (this.amount < 10) {
  250. this.amount = 10
  251. }
  252. },
  253. /**
  254. * 选择充值金额
  255. */
  256. sel_amount(m) {
  257. this.amount = m
  258. },
  259. /**
  260. * 调用接口实现充值功能
  261. */
  262. chongzhi() {
  263. if (this.test) { // 测试环境
  264. uni.showModal({
  265. title: '提示',
  266. content: '您选择了充值:¥' + this.amount + ' 元',
  267. cancelText: '算了',
  268. confirmText: '充值',
  269. success: (res) => {
  270. if (res.confirm) {
  271. // 组合地址,发起支付
  272. this.jsapi()
  273. } else if (res.cancel) {
  274. return
  275. }
  276. }
  277. })
  278. return
  279. }
  280. // 生产环境
  281. if (this.amount < 10 || this.amount > 100) {
  282. uni.showToast({
  283. icon: 'none',
  284. title: '最少充值10元,最多充值100元!',
  285. mask: true,
  286. duration: 3000
  287. })
  288. return
  289. } else {
  290. uni.showModal({
  291. title: '提示',
  292. content: '您选择了充值:¥' + this.amount + ' 元',
  293. cancelText: '算了',
  294. confirmText: '充值',
  295. success: (res) => {
  296. if (res.confirm) {
  297. // 组合地址,发起支付
  298. this.jsapi()
  299. } else if (res.cancel) {
  300. return
  301. }
  302. }
  303. })
  304. }
  305. },
  306. /**
  307. * 拨打电话
  308. */
  309. callPhone() {
  310. uni.makePhoneCall({
  311. phoneNumber: this.phone_number
  312. });
  313. }
  314. }
  315. }
  316. </script>
  317. <style lang="scss" scoped>
  318. .content {
  319. display: flex;
  320. flex-direction: column;
  321. background-color: #F5F5F5;
  322. width: 750rpx;
  323. // height: 100%;
  324. // align-items: stretch;
  325. .title {
  326. display: flex;
  327. align-items: center;
  328. padding: 40rpx;
  329. text:nth-child(1) {
  330. color: $my-color-primary;
  331. font-size: 50rpx;
  332. }
  333. text:nth-child(2) {
  334. font-size: 32rpx;
  335. margin-left: 15rpx;
  336. font-family: Microsoft YaHei-3970(82674968);
  337. color: #333333;
  338. }
  339. }
  340. .input_amount {
  341. background-color: #FFFFFF;
  342. width: 670rpx;
  343. height: 210rpx;
  344. padding: 40rpx 40rpx 0 40rpx;
  345. border-radius: 40rpx 40rpx 0 0;
  346. .amount_tip {
  347. font-size: 32rpx;
  348. font-family: Microsoft YaHei-3970(82674968);
  349. }
  350. .amount_inp {
  351. display: flex;
  352. flex-direction: row;
  353. border-bottom: 1px solid #cccccc;
  354. margin-top: 20rpx;
  355. margin-bottom: 10rpx;
  356. text {
  357. width: 50rpx;
  358. height: 74rpx;
  359. font-size: 50rpx;
  360. font-weight: bold;
  361. padding-top: 6rpx;
  362. font-family: Microsoft YaHei-3970(82674968);
  363. }
  364. text::before {
  365. content: '¥';
  366. }
  367. input {
  368. width: 620rpx;
  369. height: 80rpx;
  370. font-size: 50rpx;
  371. font-weight: bold;
  372. font-family: Microsoft YaHei-3970(82674968);
  373. }
  374. /deep/.ph_class {
  375. font-size: 28rpx;
  376. font-weight: normal;
  377. }
  378. }
  379. text {
  380. font-size: 26rpx;
  381. font-family: Microsoft YaHei-3970(82674968);
  382. font-weight: 500;
  383. }
  384. }
  385. .amount_select {
  386. padding: 40rpx;
  387. .amount_btn {
  388. display: flex;
  389. justify-content: space-between;
  390. margin: 30rpx 0 20rpx;
  391. text {
  392. width: 150rpx;
  393. height: 120rpx;
  394. line-height: 120rpx;
  395. font-size: 32rpx;
  396. font-family: Microsoft YaHei-3970(82674968);
  397. color: #333333;
  398. text-align: center;
  399. background: #FFFFFF;
  400. border: 3rpx solid #AAAAAA;
  401. }
  402. .selStyle {
  403. border: 0rpx;
  404. width: 156rpx;
  405. height: 126rpx;
  406. background: url(../../static/images/jinebeijing2x.png) 0rpx 0rpx no-repeat;
  407. background-size: 100%;
  408. }
  409. }
  410. .reminder {
  411. height: 26rpx;
  412. font-size: 26rpx;
  413. font-family: Microsoft YaHei-3970(82674968);
  414. color: $my-color-primary;
  415. }
  416. .payment {
  417. margin-top: 65rpx;
  418. font-size: 32rpx;
  419. font-family: Microsoft YaHei-3970(82674968);
  420. color: #333333;
  421. text {
  422. color: $my-color-primary;
  423. }
  424. }
  425. button {
  426. margin-top: 38rpx;
  427. }
  428. .btn_submit {
  429. border-radius: 10rpx;
  430. background-color: $my-color-primary;
  431. font-family: Microsoft YaHei-3970(82674968);
  432. color: #FFFFFF;
  433. }
  434. .btn_tel {
  435. border-radius: 10rpx;
  436. background: #CCCCCC;
  437. font-family: Microsoft YaHei-3970(82674968);
  438. color: #FFFFFF;
  439. }
  440. .tips {
  441. margin-top: 20rpx;
  442. font-size: 24rpx;
  443. font-family: Microsoft YaHei-3970(82674968);
  444. color: $my-color-primary;
  445. line-height: 36rpx;
  446. }
  447. }
  448. }
  449. </style>