recharge.vue 11 KB

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