jiaofei.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <view class="content">
  3. <view class="select-item" @tap="navigateToSelect">
  4. <view class="picker-item-logol">
  5. <image class="picker-item-logo-left" src="/static/image/room.png"></image>
  6. </view>
  7. <view class="picker-item-label">已选房间</view>
  8. <view class="picker-item-content" :class="{'font-txt':add_class==1}">{{roomSelect}}</view>
  9. <view class="picker-item-logor">
  10. <image class="picker-item-logo-right" src="/static/image/right.png"></image>
  11. </view>
  12. </view>
  13. <view class="show-item">
  14. <view class="picker-item-logol">
  15. <image class="picker-item-logo-left" src="/static/image/elec.png"></image>
  16. </view>
  17. <view class="picker-item-label">剩余电量</view>
  18. <view class="picker-item-content font-txt">{{remainElec}}度</view>
  19. </view>
  20. <view class="show-item add-money">
  21. <view class="add-money-show">
  22. <view class="picker-item-logol money-logo">
  23. <image class="picker-item-logo-left" src="/static/image/money.png"></image>
  24. </view>
  25. <view class="picker-item-label money-show">请选择金额</view>
  26. </view>
  27. <view class="add-money-list">
  28. <view class="add-money-button">
  29. <button class="select-submit" :data-item="10" @tap="add_money"
  30. :class="{'bg-color':add_class1==1}">10元</button>
  31. </view>
  32. <view class="add-money-button">
  33. <button class="select-submit" :data-item="20" @tap="add_money">20元</button>
  34. </view>
  35. <view class="add-money-button">
  36. <button class="select-submit" :data-item="30" @tap="add_money">30元</button>
  37. </view>
  38. <view class="add-money-button">
  39. <button class="select-submit" :data-item="50" @tap="add_money">50元</button>
  40. </view>
  41. <view class="add-money-button">
  42. <button class="select-submit" :data-item="100" @tap="add_money">100元</button>
  43. </view>
  44. <view class="add-money-button">
  45. <button class="select-submit" :data-item="200" @tap="add_money">200元</button>
  46. </view>
  47. </view>
  48. </view>
  49. <view class="show-item">
  50. <input class="input-money" maxlength="4" placeholder="请输入金额(1-200)" v-model:value="inputMoney"
  51. placeholder-class="#B3B3B3" @input="onInput" @blur="onBlur" />
  52. </view>
  53. <view class="submit-item">
  54. <button class="submit" :data-rooms='room' @tap="input_money">充值</button>
  55. </view>
  56. <view class="change-show" @tap="navigateToShow">
  57. 台账管理
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. export default {
  63. data() {
  64. return {
  65. index: 0,
  66. roomSelect: '', //房间号
  67. remainElec: 80.1, //剩余电量
  68. add_class: '', //增加class属性
  69. add_class1: '',
  70. inputMoney: '', //手动输入增加金额
  71. addMoney: 0, //选择充值金额
  72. userinfo: {}, //用户信息
  73. order_id: '', // 订单号
  74. card_number: '', //用户卡号
  75. dom: '', //房间号
  76. campus: '', //校区
  77. access_token: '',
  78. sub_appid: '', //商户号
  79. pay_info: '',
  80. test: getApp().globalData.test
  81. }
  82. },
  83. onLoad: function(options) {
  84. let item_tmp = decodeURIComponent(options.item)
  85. //将字符串解密转换成对象
  86. let item = JSON.parse(item_tmp);
  87. // console.log(options.o, item)
  88. // 如果是选择宿舍号返回
  89. if (options.o == 'select') {
  90. this.roomSelect = item.roomSelect;
  91. this.add_class = 1;
  92. this.dom = item.dom;
  93. }
  94. try {
  95. // 获取存储的用户数据
  96. const value = uni.getStorageSync('userinfo_storage_key')
  97. // console.log(item)
  98. if (item_tmp == '{}') { // 如果没有用户信息,返回首页
  99. uni.redirectTo({
  100. url: '../index/index'
  101. })
  102. return
  103. }
  104. // 如果是选择宿舍号返回
  105. if (options.o == 'index') {
  106. this.userinfo = item;
  107. // 更新存储的用户信息
  108. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  109. this.roomSelect = this.campus + this.dom
  110. } else if (value != '') {
  111. this.userinfo = value;
  112. }
  113. this.campus = this.userinfo.campus
  114. this.dom = this.userinfo.dorm_number
  115. this.sub_appid = this.userinfo.sub_appid
  116. this.add_class = 1
  117. } catch (e) {
  118. console.log(e)
  119. }
  120. //将缓存中的卡号进行获取
  121. this.card_number = this.userinfo.card_number
  122. // 查询用户信息
  123. this.get_valid_credentials()
  124. },
  125. methods: {
  126. /**
  127. * 获取应用有效凭证
  128. */
  129. async get_valid_credentials() {
  130. const value = uni.getStorageSync('userinfo_storage_key')
  131. // console.log(value)
  132. const res = await this.$myRequest({
  133. host: 'wecard',
  134. url: '/cgi-bin/oauth2/token',
  135. method: 'POST',
  136. header: {
  137. 'content-type': 'application/json'
  138. },
  139. data: {
  140. 'app_key': value.appkey,
  141. 'app_secret': value.app_secret,
  142. 'grant_type': 'client_credentials',
  143. 'scope': 'base',
  144. 'ocode': value.ocode
  145. }
  146. });
  147. if (typeof(res.data.access_token) != 'undefined') {
  148. // 获取到应用有效凭证,保存到页面变量中
  149. this.access_token = res.data.access_token
  150. } else {
  151. uni.showToast({
  152. title: '获取凭证失败',
  153. duration: 2000
  154. })
  155. }
  156. },
  157. //跳转到选择页面
  158. navigateToSelect() {
  159. uni.navigateTo({
  160. url: '../select/select?roomSelect=' + this.roomSelect,
  161. });
  162. },
  163. //跳转到台账页面
  164. navigateToShow(e) {
  165. uni.navigateTo({
  166. url: '../show/show?card_number=' + this.card_number,
  167. });
  168. },
  169. //增加固定金额
  170. add_money(e) {
  171. this.add_class1 = 0
  172. this.addMoney = e.currentTarget.dataset.item
  173. this.confirm_room('add_money')
  174. },
  175. confirm_room(op) {
  176. // 检查房间号
  177. if (this.roomSelect == '') {
  178. uni.showToast({
  179. title: '请选择房间号',
  180. icon: 'success'
  181. })
  182. return
  183. }
  184. // 输入金额进行充值
  185. if (op == 'input_money') {
  186. //判断输入是否为空或不是数字
  187. if (this.inputMoney == '' || this.inputMoney == null) {
  188. uni.showToast({
  189. title: '请输入充值金额',
  190. duration: 2000
  191. })
  192. return
  193. }
  194. if (isNaN(this.inputMoney)) {
  195. uni.showToast({
  196. title: '请输入数值',
  197. duration: 2000
  198. })
  199. return
  200. }
  201. if (!this.test) {
  202. if (this.inputMoney > 200 || this.inputMoney < 10) {
  203. uni.showToast({
  204. title: '限额10~200元',
  205. duration: 2000
  206. })
  207. return
  208. }
  209. }
  210. this.addMoney = this.inputMoney
  211. }
  212. // 确认充值对话框
  213. this.confirm_dialog()
  214. },
  215. // 确认充值对话框
  216. confirm_dialog() {
  217. uni.showModal({
  218. // title: '确定充值'+this.inputMoney+'元',
  219. title: '确定充值' + this.addMoney + '元',
  220. success: (res) => {
  221. if (res.confirm) {
  222. this.jsapi()
  223. } else if (res.cancel) {
  224. console.log('用户点击取消');
  225. }
  226. }
  227. })
  228. },
  229. //增加可变金额
  230. input_money(e) {
  231. // console.log(e)
  232. this.confirm_room('input_money')
  233. },
  234. //准备支付
  235. async jsapi() {
  236. const res = await this.$myRequest({
  237. host: 'wecard',
  238. url: "/cgi-bin/pay/app/mppay",
  239. method: 'POST',
  240. header: {
  241. 'content-type': 'application/json'
  242. },
  243. data: {
  244. 'access_token': this.access_token,
  245. 'sub_appid': this.sub_appid,
  246. 'user_id': this.card_number,
  247. "order_id": this.order_id = get_order_id('dianfei'),
  248. "amount": this.addMoney * 100,
  249. 'order_type': 1,
  250. 'callback_url': ''
  251. }
  252. })
  253. if (res.data.code == 0) {
  254. this.pay_info = res.data.data.pay_info
  255. // 发起支付
  256. this.pay_amount()
  257. } else {
  258. uni.showToast({
  259. icon: 'none',
  260. title: res.data.message,
  261. duration: 3000
  262. })
  263. }
  264. },
  265. // 调起支付
  266. pay_amount() {
  267. var OpenMidas = require("@/static/openMidas.js"); // 引入小程序目录下的SDK文件
  268. // 设置支付配置
  269. wx['OpenMidasConfig'] = {
  270. apiCommonConf: {
  271. version: "weixiao"
  272. },
  273. cgiDomain: {
  274. test: "midas.weixiao.qq.com/api", // 私有化参数联系微卡客服进行获取
  275. },
  276. webDomain: "https://midas.weixiao.qq.com/h5", // 私有化参数联系微卡客服进行获取
  277. sandboxWebDomain: "https://midas.weixiao.qq.com/h5" // 私有化参数联系微卡客服进行获取
  278. }
  279. var payInfo = this.pay_info; // 请求mppay接口返回的数据
  280. // console.log(payInfo)
  281. var appMetaData = "app=test&version=1.1"; // 自定义回调数据
  282. OpenMidas.init("test");
  283. OpenMidas.pay(
  284. payInfo,
  285. function(resultCode, innerCode, resultMsg, appMetaData) {
  286. console.log(resultCode); // 支付响应状态码
  287. console.log(innerCode); // 支付响应内部错误码
  288. console.log(resultMsg); // 支付响应说明
  289. console.log(appMetaData); // 自定义回调数据
  290. // todo:处理业务逻辑
  291. },
  292. appMetaData
  293. );
  294. },
  295. /**
  296. * 输入充值金额
  297. */
  298. onInput(e) {
  299. if (this.test) {
  300. return
  301. }
  302. const v = e.detail.value
  303. this.inputMoney = 10
  304. const zero = /^(0{1,})|[^0-9]/g
  305. let final = 0
  306. if (!v) {
  307. final = 0
  308. } else {
  309. final = v.toString().replace(zero, (v) => {
  310. return 0
  311. })
  312. if (final.split('')[0] * 1 === 0) {
  313. final = final.slice(1) - 0 || 0
  314. }
  315. if (final > 200) {
  316. final = 200
  317. }
  318. }
  319. this.$nextTick(() => {
  320. this.inputMoney = final.toString() || '0'
  321. })
  322. },
  323. /**
  324. * 失去焦点时
  325. */
  326. onBlur() {
  327. if (this.test) {
  328. return
  329. }
  330. if (this.inputMoney < 10) {
  331. this.inputMoney = 10
  332. }
  333. }
  334. },
  335. }
  336. function get_order_id(head) {
  337. const date = new Date()
  338. let year = date.getFullYear()
  339. let month = date.getMonth() + 1
  340. let day = date.getDate()
  341. let hour = date.getHours()
  342. let minute = date.getMinutes()
  343. let second = date.getSeconds()
  344. let millisecond = date.getMilliseconds()
  345. let len_mill = millisecond.toString().length
  346. month = month > 9 ? month : '0' + month
  347. day = day > 9 ? day : '0' + day
  348. second = second > 9 ? second : '0' + second
  349. if (len_mill == 1) {
  350. millisecond = (Math.floor(Math.random () * 9000) + 10000).toString() + millisecond
  351. } else if (len_mill == 2) {
  352. millisecond = (Math.floor(Math.random () * 900) + 1000).toString() + millisecond
  353. } else if (len_mill == 3) {
  354. millisecond = (Math.floor(Math.random () * 900) + 100).toString() + millisecond
  355. }
  356. return `${head}${year}${month}${day}${hour}${minute}${second}${millisecond}`
  357. }
  358. </script>
  359. <style>
  360. @import url("jiaofei.css");
  361. </style>