recharge.vue 11 KB

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