jiaofei.vue 10 KB

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