jiaofei.vue 11 KB

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