微信小程序 简单通讯

//index.js
//获取应用实例
var app = getApp()
Page({
data: {
textdata:  "测试 wx.request",
motto: 'Hello World',
userInfo: {}
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
console.log('onLoad')
var that = this
//调用应用实例的方法获取全局数据
app.getUserInfo(function(userInfo){
//更新数据
that.setData({
userInfo:userInfo
})})//method中设置你想调用的方法名
//var result = post('http://localhost:8000/MyService/GetMaintCode', { encode: 123 });


 var  that  =  this;
    wx.request({
url:  'http://localhost:8000/MyService/MyService/Task/GetMaintCode',
      data:  {},
      method:  'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
      // header: {}, // 设置请求的 header 默认是application/json  
      success:  function  (res)  {
        // 操作json数据  
        var  text  = "";
        for (var  i  in  res.data)  {
          text  +=  i  +  "."  +  res.data[i].title  +  "\r\n";
        }
that.setData({ textdata: res.data });
console.log( res.data);
      },
      fail:  function  ()  {
        // fail  
      },
      complete:  function  ()  {
        // complete  
      }
    })  


 
},

 
})
function post(URL, PARAMS) {
var temp = document.createElement("form");
temp.action = URL;
temp.method = "post";
temp.style.display = "none";
for (var x in PARAMS) {
var opt = document.createElement("textarea");
opt.name = x;
opt.value = PARAMS[x];
// alert(opt.name)
temp.appendChild(opt);
}
document.body.appendChild(temp);
temp.submit();
return temp;
}

  

<!--index.wxml-->
<view class="container">
  <view  bindtap="bindViewTap" class="userinfo">
    <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    <text class="userinfo-nickname">{{textdata}}</text>
  </view>
  <view class="usermotto">
    <text class="user-motto">{{motto}}</text>
  </view>
</view>

  

/**index.wxss**/
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.usermotto {
  margin-top: 200px;
}

  

//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
    logs: []
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(function (log) {
        return util.formatTime(new Date(log))
      })
    })
  },

  formHandle: function (e) {
    //method中设置你想调用的方法名
    var method = 'getMaintCode';
    //wsdlurl中设置需要访问的webservice的url地址
    var wsdlurl = 'http://127.0.0.1:9999/MaintService/metadata?wsdl=wsdl0';
    var targetNamespace = 'http://127.0.0.1/';
    //datacopy中拼字符串,即http传输中的soap信息
    var datacopy = '<?xml version="1.0" encoding="utf-8"?>';
    datacopy += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.ws.*.com/">';
    datacopy += '<soapenv:Header/>';get
    datacopy += '<soapenv:Body>';
    //接着拼你要访问的方法名、参数名和你传递的实参值,比如我要访问的方法是getReader(String arg0,int arg1)
    //而我的实际调用是getReader('libsys',2),所以拼字符串如下
    datacopy += '<ser:GetMaintCode>';
    datacopy += '<arg0>123</arg0>';
    //datacopy += '<arg1>2</arg1>';
    datacopy += '</ser:GetMaintCode>';
    datacopy += '</soapenv:Body>';
    datacopy += '</soapenv:Envelope>';
    wx.request({
      url: wsdlurl,
      data: datacopy,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
      header: {
        'content-type': 'text/xml; charset=utf-8',
        'SOAPAction': targetNamespace + method,
      },
      // 设置请求的 header
      success: function (res) {
        // success
        var resData = res.data;
        console.log(resData);
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete 
      }
    })
  },
})

  

{
    "navigationBarTitleText": "查看启动日志"
}

  

<!--logs.wxml-->
<view class="container log-list">
  <block wx:for="{{logs}}" wx:for-item="log" wx:key="*this">
    <text class="log-item">{{index + 1}}. {{log}}</text>
  </block>
</view>

  

.log-list {  display: flex;  flex-direction: column;  padding: 40rpx;}.log-item {  margin: 10rpx;  background: lightpink} 

.log-list {
  display: flex;
  flex-direction: column;
  padding: 40rpx;
}
.log-item {
  margin: 10rpx;
  background: lightpink
}


查找
无结果

/pages/logs/logs.wxss 

  

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime
}

  

//app.js
App({
  onLaunch: function() {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },

  getUserInfo: function(cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.getUserInfo({
        withCredentials: false,
        success: function(res) {
          that.globalData.userInfo = res.userInfo
          typeof cb == "function" && cb(that.globalData.userInfo)
        }
      })
    }
  },

  globalData: {
    userInfo: null
  }
})

  

{
  "pages":[
    "pages/index/index",
    "pages/logs/logs"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle":"black"
  }
}

  

/**app.wxss**/
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
} 

  

posted @ 2017-09-06 12:02  sunlyk  阅读(210)  评论(0编辑  收藏  举报