| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- // pages/workorder/index.js
- const api = require('../../api/index.js');
-
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- array:['日常','故障','其他'],
- namelist:[],
- typelist:['日常','故障','其他'],
- devicedata:[],
- index:1,
- nameindex:'',
- typeindex:0,
- deviceindex:'',
- date:'',
- partnerAvite:[],
- priority:'1',
- workorderContent:'',
- switchtype:true,
- datestart:'',
- dateend:'',
- powerindex:'',
- powerdata:[],
- workorderName:'',
- workorderAddress:'',
- visible:false,
- checkedItems:[],
- checkedPartnerIds:'',
- equipment:false,
- equipmentItems:[],
- equipmentPartnerIds:''
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- console.log(options.name);
- this.setData({
- index:options.name
- })
- this.getpowerdata()
- },
- onworkorder(){
-
- if (this.data.workorderName==''||this.data.dateend==''||this.data.datestart==''||this.data.checkedPartnerIds==''||this.data.equipmentPartnerIds==''||this.data.powerindex=='') {
- wx.showToast({
- title: '请填写完整工单',
- icon: 'none'
- });
- return
- }
-
- if (!this.data.switchtype) {
- return
- }
- this.setData({
- switchtype:false
- })
- let data = {
- workorderName:this.data.workorderName,
- workorderAddress:this.data.workorderAddress,
- workorderImg:this.data.partnerAvite.length>0?this.data.partnerAvite.toString():'',
- workorderContent:this.data.workorderContent,
- workoderPartner:this.data.checkedPartnerIds,
- workorderEndtime:this.data.dateend, // 开始时间
- workorderStarttime:this.data.datestart, // 结束时间
- workorderGrade:this.data.priority,
- powerstationId:this.data.powerdata[this.data.powerindex].powerstationId,
- type:(this.data.index*1)+1,
- deviceId:this.data.equipmentPartnerIds
- }
- api.request(`/workoder/createworkorder`, 'POST',data)
- .then((res) => {
- console.log(res);
- if (res.code==200) {
- // this.requestSubscribeMessage();
- // this.loadMessages();
- this.setData({
- switchtype:true
- })
- wx.switchTab({
- url: '/pages/index/index',
- })
- }else{
- wx.showToast({
- title: '提交失败',
- icon: 'none'
- });
- }
- })
- .catch((err) => {
- console.error('请求失败:', err);
- // 在这里处理请求失败的情况
- this.setData({
- switchtype:true
- })
- });
- },
- bindpowerChange(e){
- console.log('picker发送选择改变,携带值为', e.detail.value)
- this.setData({
- powerindex: e.detail.value
- })
- this.getselectdevicelist()
- this.getselectinformationall()
-
- },
- // 电站
- getpowerdata(){
-
- api.request(`/syspowerstation/selectpowestation`, 'POST')
- .then((res) => {
- console.log(res);
- this.setData({
- powerdata:res.data
- })
-
- })
- .catch((err) => {
- console.error('请求失败:', err);
- // 在这里处理请求失败的情况
- });
- },
- // 用户信息
- getselectinformationall(){
- this.setData({
- namelist:[]
- })
- let data = {
- powerstationId:this.data.powerdata[this.data.powerindex].powerstationId
- }
- api.request(`/partner/selectinformationall`, 'POST',data)
- .then((res) => {
- console.log(res);
- const newNamelist = res.data.map(item => ({
- ...item, // 保留原有字段(如partnerName)
- isChecked: false // 新增选中状态字段,默认未选中
- }));
- this.setData({
- namelist:newNamelist
- })
- })
- .catch((err) => {
- console.error('请求失败:', err);
- // 在这里处理请求失败的情况
- });
- },
- onVisibleChange(e){
- console.log(e);
- this.setData({
- visible:false,
- equipment:false
-
- })
- },
- // 设备列表
- getselectdevicelist(){
- this.setData({
- devicedata:[]
- })
- let data = {
- powerstationId:this.data.powerdata[this.data.powerindex].powerstationId
- }
- api.request(`/device/selectdevicelist`, 'POST',data)
- .then((res) => {
- console.log(res);
- const newNamelist = res.data.map(item => ({
- ...item, // 保留原有字段(如partnerName)
- isChecked: false // 新增选中状态字段,默认未选中
- }));
- this.setData({
- devicedata:newNamelist
- })
- })
- .catch((err) => {
- console.error('请求失败:', err);
- // 在这里处理请求失败的情况
- });
- },
- onPeakprice(e){
- console.log(e.detail.value);
- this.setData({
- workorderName:e.detail.value
- })
- },
- onaddress(e){
- console.log(e.detail.value);
- this.setData({
- workorderAddress:e.detail.value
- })
- },
- ontextarea(e){
- console.log(e);
- this.setData({
- workorderContent:e.detail.value
- })
- },
- // 当namelist为空时,点击触发提示
- showEmptyTip() {
- wx.showToast({
- title: '请先选择电站',
- icon: 'none', // 不显示图标,只显示文字
- duration: 2000 // 提示2秒
- });
- },
- /**
- * 切换选项的选中状态
- */
- toggleCheck(e) {
- const index = e.currentTarget.dataset.index;
- // 复制原列表并切换当前选项的选中状态
- const newNamelist = [...this.data.namelist];
- newNamelist[index].isChecked = !newNamelist[index].isChecked;
-
- // 1. 筛选所有选中的选项
- const checkedItems = newNamelist.filter(item => item.isChecked);
- // 2. 提取选中项的partnerId,并用逗号拼接(若没有选中项,返回空字符串)
- const checkedPartnerIds = checkedItems.map(item => item.partnerId).join(',');
-
- // 更新data:同时保存列表、选中项、拼接后的ID字符串
- this.setData({
- namelist: newNamelist,
- checkedItems: checkedItems, // 保存完整选中项(可选)
- checkedPartnerIds: checkedPartnerIds // 保存逗号拼接的ID字符串(方便后续使用)
- });
-
- // 打印结果示例:若选中ID为1、3的项,会输出 "当前选中的partnerId:1,3"
- console.log("当前选中的partnerId:", this.data.checkedPartnerIds);
- },
- /**
- * 切换选项的选中状态
- */
- togequipment(e) {
- const index = e.currentTarget.dataset.index;
- // 复制原列表并切换当前选项的选中状态
- const newNamelist = [...this.data.devicedata];
- newNamelist[index].isChecked = !newNamelist[index].isChecked;
-
- // 1. 筛选所有选中的选项
- const checkedItems = newNamelist.filter(item => item.isChecked);
- // 2. 提取选中项的partnerId,并用逗号拼接(若没有选中项,返回空字符串)
- const checkedPartnerIds = checkedItems.map(item => item.deviceId).join(',');
-
- // 更新data:同时保存列表、选中项、拼接后的ID字符串
- this.setData({
- devicedata: newNamelist,
- equipmentItems: checkedItems, // 保存完整选中项(可选)
- equipmentPartnerIds: checkedPartnerIds // 保存逗号拼接的ID字符串(方便后续使用)
- });
- if (this.data.index==1) {
- this.setData({
- workorderAddress:this.data.equipmentItems.map(item => item.devicesName)
-
- })
- }
- // 打印结果示例:若选中ID为1、3的项,会输出 "当前选中的partnerId:1,3"
- console.log("当前选中的partnerId:", this.data.equipmentPartnerIds);
- },
- // 用户弹窗
- bindPickerChange(e){
- console.log('picker发送选择改变,携带值为', e.detail.value)
- this.setData({
- visible:true
- })
- // nameindex: e.detail.value
- },
- Indeedbutton1(){
- this.setData({
- visible:false
- })
- },
- Indeedbutton2(){
- this.setData({
- equipment:false
- })
- },
- binddeviceChange(){
- // console.log('picker发送选择改变,携带值为', e.detail.value)
- this.setData({
- equipment:true
- // deviceindex: e.detail.value
- })
-
- },
- bindeventChange(e){
- this.setData({
- index: e.detail.value
- })
- },
- // 开始时间
- bindstartChange: function(e) {
- console.log('picker发送选择改变,携带值为', e.detail.value)
- this.setData({
- datestart: e.detail.value
- })
- },
- // 结束时间
- bindendChange: function(e) {
- const selectedEnd = e.detail.value;
- console.log('picker发送选择改变,携带值为', e.detail.value)
- const { datestart } = this.data;
-
- // 检查结束时间是否早于开始时间
- if (datestart && selectedEnd < datestart) {
- wx.showToast({
- title: '结束时间不能早于开始时间',
- icon: 'none',
- duration: 2000
- });
- return; // 不更新结束时间
- }
-
- // 验证通过,更新结束时间
- this.setData({
- dateend: selectedEnd
- });
-
- },
- // 工单优先级
- onradio(e){
- console.log(e.detail.value);
- this.setData({
- priority:e.detail.value
- })
- },
- // 预览图片
- previewImage(e) {
- const src = e.currentTarget.dataset.src;
- wx.previewImage({
- current: src, // 当前显示图片的http链接
- urls: [src] // 需要预览的图片http链接列表
- });
- },
- bindCancel(e){
- console.log(e.currentTarget.dataset.index);
- let index = e.currentTarget.dataset.index
- // 使用 filter 方法创建新数组(推荐)
- const newPartnerAvite = this.data.partnerAvite.filter((item, i) => i !== index);
-
- this.setData({
- partnerAvite: newPartnerAvite
- });
- },
- // 上传头像--项目--文件1
- bindtoImage(){
- let _this = this
- // 先检查隐私授权状态
- wx.getPrivacySetting({
- success: (res) => {
- if (res.needAuthorization) {
- // 弹出隐私协议弹窗
- wx.requirePrivacyAuthorize({
- success: () => {
- console.log('用户已同意隐私协议');
- _this.choosetoImage(); // 继续执行上传
- },
- fail: () => {
- console.log('用户拒绝了隐私协议');
- wx.showToast({
- title: '需同意隐私协议才能使用',
- icon: 'none'
- });
- }
- });
- } else {
- _this.choosetoImage(); // 已授权,直接上传
- }
- }
- });
- },
- // 上传头像图片
- choosetoImage() {
- let _this = this
- wx.chooseMedia({
- count: 1,
- mediaType: ['image','video'],
- sourceType: ['album', 'camera'],
- success(res) {
- console.log(res);
- const tempFiles = res.tempFiles;
- _this.setData({
- imageList: tempFiles.map(file => file.tempFilePath),
- });
- _this.uploadtoImages(tempFiles);
- },
- fail(err) {
- console.error("选择失败:", err);
- }
- })
- },
- // 上传到服务器
- uploadtoImages(files) {
- let _this =this
- const token = wx.getStorageSync('token');
- files.forEach((file) => {
- wx.uploadFile({
- url: 'https://esos-iot.com:8443/config/upload/webPost',
- filePath: file.tempFilePath,
- name: 'file',
- formData: {
- file:file.tempFilePath
- },
- header: {
- 'Authorization': 'Bearer ' + token // Also add to header if needed
- },
- success: (res) => {
- console.log(res);
- const obj = JSON.parse(res.data);
- this.data.partnerAvite.push(obj.data.partnerAvite)
- this.setData({
- partnerAvite:this.data.partnerAvite
- })
- },
- fail: (err) => {
- console.error('上传失败', err);
- },
- });
- });
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
-
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
-
- },
-
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
-
- },
-
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
-
- },
-
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
-
- },
-
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
-
- },
-
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
-
- }
- })
|