jiaofei.vue 11 KB

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