recharge.vue 11 KB

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