运维小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // pages/dailyreport/index.js
  2. const api = require('../../api/index.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. fileList: [], // 用于存储选中的xlsx文件路径
  9. file:[],
  10. powerindex:'',
  11. datestart:'',
  12. powerdata:[],
  13. filedata:[]
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad(options) {
  19. this.getpowerdata()
  20. },
  21. getpowerstationdatalog(){
  22. let data = {
  23. powerstationId:this.data.powerindex==''?'':this.data.powerdata[this.data.powerindex].powerstationId
  24. }
  25. api.request(`/syspowerstation/selectpowerstationdatalog`, 'POST',data)
  26. .then((res) => {
  27. console.log(res);
  28. this.setData({
  29. powerstationdata:res.data
  30. })
  31. })
  32. .catch((err) => {
  33. console.error('请求失败:', err);
  34. // 在这里处理请求失败的情况
  35. });
  36. },
  37. // 上传xlsx文档--点击触发
  38. bindtoXlsx() {
  39. let _this = this
  40. // 先检查隐私授权状态
  41. wx.getPrivacySetting({
  42. success: (res) => {
  43. if (res.needAuthorization) {
  44. // 弹出隐私协议弹窗
  45. wx.requirePrivacyAuthorize({
  46. success: () => {
  47. console.log('用户已同意隐私协议');
  48. _this.chooseXlsxFile(); // 继续执行上传
  49. },
  50. fail: () => {
  51. console.log('用户拒绝了隐私协议');
  52. wx.showToast({
  53. title: '需同意隐私协议才能使用',
  54. icon: 'none'
  55. });
  56. }
  57. });
  58. } else {
  59. _this.chooseXlsxFile(); // 已授权,直接上传
  60. }
  61. }
  62. });
  63. },
  64. // 选择xlsx文件
  65. chooseXlsxFile() {
  66. let _this = this
  67. wx.chooseMessageFile({
  68. count: 1, // 只允许选择1个文件
  69. type: 'file', // 选择文件类型
  70. extension: ['xlsx'], // 限制只选择xlsx格式
  71. success(res) {
  72. console.log('选中的文件:', res);
  73. const tempFiles = res.tempFiles;
  74. // 存储选中的文件路径
  75. _this.setData({
  76. fileList: tempFiles.map(file => file.path),
  77. filedata:tempFiles
  78. });
  79. console.log(_this.data.fileList);
  80. // 上传到服务器
  81. // _this.uploadXlsxFile(tempFiles[0]); // 因为count=1,直接取第一个
  82. _this.setData({
  83. file:tempFiles[0]
  84. })
  85. },
  86. fail(err) {
  87. console.error("文件选择失败:", err);
  88. wx.showToast({
  89. title: '文件选择失败',
  90. icon: 'none'
  91. });
  92. }
  93. })
  94. },
  95. // 上传xlsx到服务器
  96. uploadXlsxFile() {
  97. console.log(api.baseUrl);
  98. let _this = this
  99. if (_this.data.powerindex==''||_this.data.datestart==''||_this.data.file.length<1) {
  100. wx.showToast({
  101. title: '请填写电站与上传月份',
  102. icon: 'none'
  103. });
  104. return
  105. }
  106. const token = wx.getStorageSync('token');
  107. // 显示加载中提示
  108. wx.showLoading({
  109. title: '上传中...',
  110. })
  111. console.log(_this.data.file);
  112. wx.uploadFile({
  113. // url: 'http://192.168.8.102:9999/syspowerstation/insertexcel',
  114. url: `${api.baseUrl}/syspowerstation/insertexcel`,
  115. filePath: _this.data.file.path,
  116. method:'POST',
  117. name: 'fis', // 与后端接收参数名保持一致
  118. formData: {
  119. 'powerstationId':_this.data.powerdata[_this.data.powerindex].powerstationId,
  120. 'currentYearMonth':_this.data.datestart,
  121. 'fisname': _this.data.file.name, // 传递文件名
  122. 'fileType': 'xlsx' // 传递文件类型
  123. },
  124. header: {
  125. 'Authorization': 'Bearer ' + token,
  126. 'Content-Type': 'multipart/form-data'
  127. },
  128. success: (res) => {
  129. wx.hideLoading();
  130. console.log('上传成功响应:', res);
  131. try {
  132. const obj = JSON.parse(res.data);
  133. if (obj.code === 200) { // 假设200为成功状态码
  134. wx.showToast({
  135. title: '上传成功',
  136. icon: 'success'
  137. });
  138. // 根据实际返回数据结构存储文件信息
  139. _this.getpowerstationdatalog()
  140. } else {
  141. wx.showToast({
  142. title: obj.msg || '上传失败',
  143. icon: 'none'
  144. });
  145. }
  146. } catch (e) {
  147. console.error('解析响应失败:', e);
  148. wx.showToast({
  149. title: '上传失败',
  150. icon: 'none'
  151. });
  152. }
  153. },
  154. fail: (err) => {
  155. wx.hideLoading();
  156. console.error('上传失败:', err);
  157. wx.showToast({
  158. title: '网络错误,上传失败',
  159. icon: 'none'
  160. });
  161. }
  162. });
  163. },
  164. bindpowerChange(e){
  165. console.log('picker发送选择改变,携带值为', e.detail.value)
  166. this.setData({
  167. powerindex: e.detail.value
  168. })
  169. this.getpowerstationdatalog()
  170. },
  171. // 电站
  172. getpowerdata(){
  173. api.request(`/syspowerstation/selectpowestation`, 'POST')
  174. .then((res) => {
  175. console.log(res);
  176. this.setData({
  177. powerdata:res.data
  178. })
  179. this.getpowerstationdatalog()
  180. })
  181. .catch((err) => {
  182. console.error('请求失败:', err);
  183. // 在这里处理请求失败的情况
  184. });
  185. },
  186. // 选择时间
  187. bindstartChange: function(e) {
  188. console.log('picker发送选择改变,携带值为', e.detail.value)
  189. this.setData({
  190. datestart: e.detail.value
  191. })
  192. },
  193. /**
  194. * 生命周期函数--监听页面初次渲染完成
  195. */
  196. onReady() {
  197. },
  198. /**
  199. * 生命周期函数--监听页面显示
  200. */
  201. onShow() {
  202. },
  203. /**
  204. * 生命周期函数--监听页面隐藏
  205. */
  206. onHide() {
  207. },
  208. /**
  209. * 生命周期函数--监听页面卸载
  210. */
  211. onUnload() {
  212. },
  213. /**
  214. * 页面相关事件处理函数--监听用户下拉动作
  215. */
  216. onPullDownRefresh() {
  217. },
  218. /**
  219. * 页面上拉触底事件的处理函数
  220. */
  221. onReachBottom() {
  222. },
  223. /**
  224. * 用户点击右上角分享
  225. */
  226. onShareAppMessage() {
  227. }
  228. })