• 微信小程序echart图


    一、文件后缀构成

    JSON:

    1.app.json小程序配置

    page——小程序所有的页面路径

    window——小程序所有页面的顶部背景颜色,文字颜色

     1 {
     2   "pages": [
     3    
     4     "pages/index/index",
     5     "pages/logs/logs",
     6     "pages/hvscore/hvscore",
     7     "pages/historyscore/his_score",
     8     "pages/index/page1"
     9   ],
    10   "window": {
    11     "backgroundTextStyle": "light",
    12     "navigationBarBackgroundColor": "#fff",
    13     "navigationBarTitleText": "小河儿航旅分查询",
    14     "navigationBarTextStyle": "black",
    15     "enablePullDownRefresh": "true",
    16     "backgroundColor": "#eeeeee"
    17   },
    18   
    19   "networkTimeout": {
    20     "request": 10000,
    21     "downloadFile": 10000
    22   },
    23   "debug": true
    24 }

    2. project.config.json工具配置

    工具就自动会帮你恢复到当时你开发项目时的个性化配置,其中会包括编辑器的颜色、代码上传时自动压缩等等一系列选项。

    3.page.json页面配置

    用来表示 pages/logs 目录下的 logs.json 这类和小程序页面相关的配置;开发者可以独立定义每个页面的一些属性,例如刚刚说的顶部颜色、是否允许下拉刷新等等。

    首页示例

    前台:

     1 <view class='box3'>
     2 
     3   <view class="container">
     4     <view class="page-body">
     5       <view class="index-hd">
     6         <image class="logo-avatar" src="../images/hanglvlogo.png"></image>
     7       </view>
     8 
     9       <form bindsubmit="formSubmit" bindreset="formReset">
    10         <view class="page-section boder">
    11           <input style='text-align:center;' type="number" value='{{mobile}}' class="weui-input" name="mobile" confirm-type='next' placeholder="输入手机号查询您的航旅分" bindinput ="mobile" />
    12         </view>
    13         <view class='lab1'>您输入的手机号用于确保是您本人在进行查询,航旅分会对您的信息进行保护,绝不会向他人泄漏</view>
    14 
    15         <view class="btn1">
    16           <button type='primary' loading='{{loading}}'  form-type='submit'>下一步</button>
    17         </view>
    18 
    19         <view class='lab1'>航旅分是一个航旅人群的评分系统,通过大数据分析方法,用分值来对乘客在航旅市场的出行行为价值进行度量</view>
    20       </form>
    21 
    22     </view>
    23   </view>
    24 </view>
    View Code

    后台:

      1 import WxValidate from '../../utils/WxValidate.js'
      2 const scoreUrl = require('../../config').scoreUrl
      3 
      4 var Validate = "";
      5 
      6 var app =getApp();
      7 Page({
      8 
      9   /**
     10    * 页面的初始数据
     11    */
     12   data: {
     13     mobile:""
     14   },
     15 
     16   mobile: function (e) {
     17     this.setData({
     18       mobile: e.detail.value
     19     })
     20   },
     21 
     22   /**
     23    * 生命周期函数--监听页面加载
     24    */
     25   onLoad: function (options) {
     26 
     27 
     28 
     29     // 验证字段的规则
     30     const rules = {
     31       mobile: {
     32         required: true,
     33         tel: true,
     34       },
     35     }
     36 
     37     // 验证字段的提示信息,若不传则调用默认的信息
     38     const messages = {
     39       mobile: {
     40         required: '请输入手机号',
     41         tel: '输入正确的号码',
     42       },
     43     }
     44     // 创建实例对象
     45     Validate = new WxValidate(rules, messages)
     46 
     47 
     48 
     49     //是否绑定WXid 
     50 
     51     wx.showToast({
     52             title: '读取账号信息',  //标题  
     53             icon: 'loading',  //图标,支持"success"、"loading"
     54             mask: true,  //是否显示透明蒙层,防止触摸穿透,默认:false  
     55             duration: 3000,
     56             success: function() { }, //接口调用成功的回调函数  
     57      
     58     });
     59 
     60 
     61     //获取用户信息
     62     wx.getUserInfo({
     63       success: function (result) {
     64         var nickName = result.userInfo.nickName;
     65         var openid = getApp().globalData.openid;
     66         if (openid=="")
     67         {
     68           openid = wx.getStorageSync("openid");
     69         }
     70 
     71         if (openid=="")
     72         {
     73             console.warn("openid为空");
     74             return false;
     75         }
     76         wx.request({
     77           url: scoreUrl,//请求地址
     78           data: {//发送给后台的数据
     79             nickName: nickName,
     80             openid: openid,
     81           },
     82           header: {//请求头
     83             "Content-Type": "application/x-www-form-urlencoded"
     84           },
     85           method: "POST",//get为默认方法/POST
     86           success: function (res) {
     87             console.log(res.data);//res.data相当于ajax里面的data,为后台返回的数据
     88             if (res.data.Err_code == "200") {
     89               var auuid = res.data.Err_content.split("/");
     90               wx.redirectTo({
     91                 url: '../hvscore/hvscore?uuid=' + auuid[0] + '&huuid=' + auuid[1] + "&nickName=" + nickName
     92               })
     93             }
     94 
     95           }
     96         })
     97 
     98       }
     99     })
    100   },
    101 
    102   /**
    103    * 生命周期函数--监听页面初次渲染完成
    104    */
    105   onReady: function () {
    106 
    107 
    108 
    109 
    110     
    111 
    112   },
    113 
    114   /**
    115    * 生命周期函数--监听页面显示
    116    */
    117   onShow: function () {
    118     
    119   },
    120 
    121   /**
    122    * 生命周期函数--监听页面隐藏
    123    */
    124   onHide: function () {
    125     
    126   },
    127 
    128   /**
    129    * 生命周期函数--监听页面卸载
    130    */
    131   onUnload: function () {
    132     
    133   },
    134 
    135   /**
    136    * 页面相关事件处理函数--监听用户下拉动作
    137    */
    138   onPullDownRefresh: function () {
    139     
    140   },
    141 
    142   /**
    143    * 页面上拉触底事件的处理函数
    144    */
    145   onReachBottom: function () {
    146     
    147   },
    148 
    149   /**
    150    * 用户点击右上角分享
    151    */
    152   onShareAppMessage: function () {
    153     
    154   },
    155   formSubmit: function (event) {
    156     var that = this;
    157 
    158     if (!Validate.checkForm(event)) {
    159       const error = Validate.errorList[0]
    160       //提示信息
    161       //console.log("我没通过验证:" + error);
    162       wx.showToast({
    163         title: `${error.msg} `,
    164         duration: 2000
    165       })
    166 
    167       return false
    168     }
    169 
    170     wx.navigateTo({
    171       url: 'indexs2?mobile=' + that.data.mobile
    172     })
    173   }
    174 })
    View Code

    chart图示例

    前台:

     1 <!--pages/historyscore/his_score.wxml-->
     2 <view class="container">
     3 <view class="page-body">
     4 
     5 <view class='title_center'>历史分值变化</view>
     6 
     7 <view class='line'>
     8 <canvas canvas-id="lineCanvas" class="canvas"></canvas>
     9 </view>
    10 
    11 </view>
    12 </view>
    View Code

    后台:

      1 // pages/historyscore/his_score.js
      2 
      3 const util = require('../../utils/util.js')
      4 const wxCharts = require('../../utils/wxcharts-min.js')
      5 
      6 let windowWidth = 500;
      7 try {
      8   let res = wx.getSystemInfoSync();
      9   windowWidth = res.windowWidth;
     10 } catch (e) {
     11   console.warn("我出错了")
     12 }
     13 var app = getApp()
     14 
     15 Page({
     16 
     17   /**
     18    * 页面的初始数据
     19    */
     20   data: {
     21     score: [],
     22     months: [],
     23     nowmax: 0,
     24     nowmin: 0
     25   },
     26 
     27   touchHandler: function (e) {
     28     console.log(lineChart.getCurrentDataIndex(e));
     29     lineChart.showToolTip(e, {
     30       format: function (item, category) {
     31         return category + ' ' + item.name + ':' + item.data
     32       }
     33     });
     34   },
     35 
     36   /**
     37    * 生命周期函数--监听页面加载
     38    */
     39   onLoad: function (options) {
     40     var that = this;
     41     var uuid = options.huuid;
     42     //uuid = "9ad923f1d62b49d9bb17ff80b52fae8c";
     43 
     44     //调用接口获取图表数据
     45     wx.request({
     46       url: 'https://www.xiaoheer.com/hanglvscore/getsharescore.ashx',
     47       data: {
     48         uuid: uuid
     49       },
     50       method: "POST",
     51       header: {
     52         "Content-Type": "application/x-www-form-urlencoded"  //post
     53       },
     54       success: function (res) {
     55         if (res.data.ErrorRes.Err_code == 200) {
     56           var myArray = new Array();
     57           //Y轴数据
     58           for (var i = 0; i < res.data.flightscore.length; i++) {
     59             myArray[i] = res.data.flightscore[i].scoreinfo[5].score;
     60           }
     61           var max = myArray[0];
     62           var min = myArray[0];
     63           for (var i = 0; i < myArray.length; i++) {
     64             if (myArray[i] > max) {
     65               max = myArray[i];
     66             };
     67             if (myArray[i] < min) {
     68               min = myArray[i]
     69             };
     70           }
     71           max = parseInt(max) + 1;
     72           min = parseInt(min) - 1;
     73           that.setData({ nowmax: max });
     74           that.setData({ nowmin: min });
     75 
     76           //X轴数据
     77           var armonth = new Array();
     78           for (var i = 0; i < res.data.flightscore.length; i++) {
     79             armonth[i] = res.data.flightscore[i].basicinfo.reportDate;
     80 
     81           }
     82           myArray = myArray.reverse();
     83           armonth = armonth.reverse();
     84 
     85           var windowWidth = 120;
     86           try {
     87             var res = wx.getSystemInfoSync();
     88             windowWidth = res.windowWidth;
     89           } catch (e) {
     90             console.error('getSystemInfoSync failed!');
     91           }
     92           console.log(armonth);
     93           //实例化图表
     94           new wxCharts({
     95             canvasId: 'lineCanvas',
     96             type: 'line',
     97             categories: armonth,
     98             animation: true,
     99             background: 'White',
    100             series: [{
    101               name: '报告日期',
    102               data: myArray,
    103               format: function (val, name) {
    104                 return val.toFixed(1);
    105               }
    106             }
    107             ],
    108             xAxis: {
    109               disableGrid: true
    110             },
    111             yAxis: {
    112               title: '航旅分数',
    113               format: function (val) {
    114                 return val.toFixed(1);
    115               },
    116               min: min,
    117               max: max
    118             },
    119             dataLabel: true,
    120              windowWidth,
    121             height: 500,
    122             dataPointShape: true,
    123             extra: {
    124               lineStyle: 'curve'
    125             }
    126           });
    127         }
    128       }
    129     })
    130 
    131   },
    132 
    133   /**
    134    * 生命周期函数--监听页面初次渲染完成
    135    */
    136   onReady: function () {
    137 
    138   },
    139 
    140   /**
    141    * 生命周期函数--监听页面显示
    142    */
    143   onShow: function () {
    144 
    145   },
    146 
    147   /**
    148    * 生命周期函数--监听页面隐藏
    149    */
    150   onHide: function () {
    151 
    152   },
    153 
    154   /**
    155    * 生命周期函数--监听页面卸载
    156    */
    157   onUnload: function () {
    158 
    159   },
    160 
    161   /**
    162    * 页面相关事件处理函数--监听用户下拉动作
    163    */
    164   onPullDownRefresh: function () {
    165 
    166   },
    167 
    168   /**
    169    * 页面上拉触底事件的处理函数
    170    */
    171   onReachBottom: function () {
    172 
    173   },
    174 
    175   /**
    176    * 用户点击右上角分享
    177    */
    178   onShareAppMessage: function () {
    179 
    180   }
    181 })
    View Code

    WXML:HTML(页面结构)

     1 <view class='box3'>
     2 
     3   <view class="container">
     4     <view class="page-body">
     5 
     6       <view class="index-hd">
     7         <image class="logo-avatar" src="../images/hanglvlogo.png"></image>
     8       </view>
     9 
    10       <form bindsubmit="formSubmit" bindreset="formReset">
    11 
    12         <view class="page-section">
    13           <view class="weui-cells weui-cells_after-title">
    14             <view class="weui-cell weui-cell_input">
    15               <view class="weui-cell__bd" style='border-collapse:separate'>
    16                 <label class='weui-cjr' style='float:left; margin-top:21px;'>乘机人</label>
    17                 <input style='padding-left:30px; padding-top:9px;' type="text" class="weui-input" name="username" confirm-type='next' placeholder="请输入你的姓名" />
    18               </view>
    19             </view>
    20           </view>
    21 
    22           <view class="weui-cells weui-cells_after-title">
    23             <view class="weui-cell weui-cell_input">
    24               <view class="weui-cell__bd">
    25                 <label class='weui-sfz' style='float:left; margin-top:21px;'>身份证</label>
    26                 <input style='padding-left:30px; padding-top:9px;' type="idcard" class="weui-input" name="idcard" confirm-type='next' placeholder="请输入你的身份证" />
    27               </view>
    28             </view>
    29           </view>
    30 
    31           <view class="weui-cells weui-cells_after-title">
    32             <view class="weui-cell weui-cell_input">
    33               <view class="weui-cell__bd">
    34                 <label class='weui-sjh' style='float:left; margin-top:21px;'>手机号</label>
    35                 <input style='padding-left:30px; padding-top:9px;' type="number" class="weui-input" name="mobile" confirm-type='next' placeholder="请输入你的手机号" />
    36               </view>
    37             </view>
    38           </view>
    39 
    40         </view>
    41 
    42         <view class="btn-area">
    43           <button type='primary' loading='{{loading}}' form-type='submit'>查看我的航旅分</button>
    44         </view>
    45 
    46       </form>
    47     </view>
    48   </view>
    49 </view>
    View Code

    WXSS:CSS(页面样式)

     1 @import "../common/lib/weui.wxss";
     2 @import "../common/index.wxss";
     3 
     4 .userinfo-avatar {
     5   width: 178rpx;
     6   height: 178rpx;
     7   margin: 20rpx;
     8   border-radius: 50%;
     9 }
    10 
    11 .logo-avatar {
    12   width: 236rpx;
    13   height: 75rpx;
    14   margin: 20rpx;
    15 }
    16 
    17 .page-section-title {
    18   padding-top: 5px;
    19 }
    20 
    21 .box3 {
    22   background-color: white;
    23   -webkit-box-shadow: 1px 1px 1px #fff;
    24   -moz-box-shadow: 1px 1px 1px #fff;
    25   width: 96%;
    26   height: 70%;
    27   margin-top: 7px;
    28   margin-left: 7px;
    29   border-radius: 10px 10px 10px 10px; /**左上角-右上角-左下角-右下角*/
    30 }
    View Code

    JS:JS(页面逻辑交互)

      1 // pages/test/test1.js
      2 import WxValidate from '../../utils/WxValidate.js'
      3 const scoreUrl = require('../../config').scoreUrl
      4 
      5 var Validate = "";
      6 
      7 Page({
      8   /**
      9    * 页面的初始数据
     10    */
     11   data: {
     12 
     13   },
     14 
     15 
     16   formSubmit: function (e) {
     17     var that = this;
     18     var formData = e.detail.value;
     19     console.log(formData.username);
     20 
     21     if (!Validate.checkForm(e)) {
     22       const error = Validate.errorList[0]
     23       //提示信息
     24       //console.log("我没通过验证:" + error);
     25       wx.showToast({
     26         title: `${error.msg} `,
     27         duration: 2000
     28       })
     29 
     30       return false
     31     }
     32 
     33 
     34     that.setData(
     35       { loading: true }
     36     );
     37 
     38 
     39     wx.request({
     40       url: scoreUrl,
     41       data: {
     42         name: formData.username,
     43         idcard: formData.idcard,
     44         mobile: formData.mobile
     45       },
     46       header: {
     47         'Content-Type': 'application/json'
     48       },
     49       success: function (res) {
     50         //console.log(res.data);
     51 
     52         that.setData(
     53           { loading: false }
     54         );
     55         var uuid = "";
     56         if (res.data.Err_code == "200") {
     57           uuid = res.data.Err_content;
     58           var auuid = uuid.split("/");
     59           wx.navigateTo({
     60             url: '../hvscore/hvscore?uuid=' + auuid[0] + "&huuid=" + auuid[1],
     61           });
     62 
     63         } else {
     64           var msg = res.data.Err_content;
     65           if (res.data.Err_content == "NODATA") {
     66             msg = "未查到航旅分";
     67           }
     68           wx.showToast({
     69             title: msg,
     70             duration: 2000
     71           })
     72 
     73         }
     74         //console.log("uuid=" + uuid);
     75 
     76 
     77       }
     78     });
     79 
     80   },
     81   formReset: function () {
     82     console.log('form发生了reset事件');
     83     this.modalTap2();
     84   },
     85   /**
     86    * 生命周期函数--监听页面加载
     87    */
     88   onLoad: function (options) {
     89 
     90     var app = getApp();
     91     var that = this;
     92     wx.getUserInfo({
     93       success: function (res) {
     94         // success  
     95         switch (res.userInfo.gender) {
     96           case 0:
     97             res.userInfo.sex = "未知"
     98             break;
     99           case 1:
    100             res.userInfo.sex = "男"
    101             break;
    102           case 2:
    103             res.userInfo.sex = "女"
    104             break;
    105         }
    106         app.globalData.userInfo = res.userInfo;
    107       },
    108     })
    109     // 验证字段的规则
    110     const rules = {
    111       username: {
    112         required: true,
    113       },
    114 
    115       idcard: {
    116         required: true,
    117         idcard: true,
    118       },
    119       mobile: {
    120         required: true,
    121         tel: true,
    122       },
    123     }
    124 
    125     // 验证字段的提示信息,若不传则调用默认的信息
    126     const messages = {
    127       username: {
    128         required: '请输入姓名',
    129       },
    130       idcard: {
    131         required: '请输入身份证号码',
    132         idcard: '身份证号码输入有误',
    133       },
    134       mobile: {
    135         required: '请输入手机号',
    136         tel: '手机号输入有误',
    137       },
    138     }
    139     // 创建实例对象
    140     Validate = new WxValidate(rules, messages)
    141 
    142 
    143 
    144   },
    145   /**
    146    * 生命周期函数--监听页面初次渲染完成
    147    */
    148   onReady: function () {
    149 
    150   },
    151 
    152   /**
    153    * 生命周期函数--监听页面显示
    154    */
    155   onShow: function () {
    156 
    157   },
    158 
    159   /**
    160    * 生命周期函数--监听页面隐藏
    161    */
    162   onHide: function () {
    163 
    164   },
    165 
    166   /**
    167    * 生命周期函数--监听页面卸载
    168    */
    169   onUnload: function () {
    170 
    171   },
    172 
    173   /**
    174    * 页面相关事件处理函数--监听用户下拉动作
    175    */
    176   onPullDownRefresh: function () {
    177 
    178   },
    179 
    180   /**
    181    * 页面上拉触底事件的处理函数
    182    */
    183   onReachBottom: function () {
    184 
    185   },
    186 
    187   /**
    188    * 用户点击右上角分享
    189    */
    190   onShareAppMessage: function () {
    191 
    192   }
    193 })
    View Code

     引入微信表单验证以及提交跳转页面

     1 import WxValidate from '../../utils/WxValidate.js'
     2 const scoreUrl = require('../../config').scoreUrl
     3 
     4 var Validate = "";
     5 
     6 // 验证字段的规则
     7     const rules = {
     8       mobile: {
     9         required: true,
    10         tel: true,
    11       },
    12     }
    13 
    14     // 验证字段的提示信息,若不传则调用默认的信息
    15     const messages = {
    16       mobile: {
    17         required: '请输入手机号',
    18         tel: '输入正确的号码',
    19       },
    20     }
    21     // 创建实例对象
    22     Validate = new WxValidate(rules, messages)
    23 
    24   formSubmit: function (event) {
    25     var that = this;
    26 
    27     if (!Validate.checkForm(event)) {
    28       const error = Validate.errorList[0]
    29       //提示信息
    30       //console.log("我没通过验证:" + error);
    31       wx.showToast({
    32         title: `${error.msg} `,
    33         duration: 2000
    34       })
    35 
    36       return false
    37     }
    38 
    39     wx.navigateTo({
    40       url: 'indexs2?mobile=' + that.data.mobile
    41     })
    42   }
    View Code

    调用接口

     1         wx.request({
     2           url: scoreUrl,//请求地址
     3           data: {//发送给后台的数据
     4             nickName: nickName,
     5             openid: openid,
     6           },
     7           header: {//请求头
     8             "Content-Type": "application/x-www-form-urlencoded"
     9           },
    10           method: "POST",//get为默认方法/POST
    11           success: function (res) {
    12             console.log(res.data);//res.data相当于ajax里面的data,为后台返回的数据
    13             if (res.data.Err_code == "200") {
    14               var auuid = res.data.Err_content.split("/");
    15               wx.redirectTo({
    16                 url: '../hvscore/hvscore?uuid=' + auuid[0] + '&huuid=' + auuid[1] + "&nickName=" + nickName
    17               })
    18             }
    19 
    20           }
    21         })
    View Code

    接口

     1 public class getopenid : IHttpHandler
     2 {
     3 
     4     public void ProcessRequest(HttpContext context)
     5     {
     6         context.Response.ContentType = "text/plain";
     7         
     8         string code = context.Request.QueryString["code"];
     9         bllhanglv bll = new bllhanglv();
    10 
    11         string result = bll.getopenid(code);
    12         context.Response.Write(result);
    13     }
    14 
    15     public bool IsReusable
    16     {
    17         get
    18         {
    19             return false;
    20         }
    21     }
    22 
    23 }
    View Code

    app.js配置

     1 //app.js
     2 
     3 
     4 App({
     5   
     6 
     7   onLaunch: function () {
     8     var that = this;
     9     // 展示本地存储能力
    10     var logs = wx.getStorageSync('logs') || []
    11     logs.unshift(Date.now())
    12     wx.setStorageSync('logs', logs);
    13 
    14     // 登录
    15     wx.login({
    16       success: res => {
    17         // 发送 res.code 到后台换取 openId, sessionKey, unionId
    18         //var setURL = "https://api.weixin.qq.com/sns/jscode2session";
    19         var setURL = "https://www.xiaoheer.com/hanglvscore/getopenid.ashx?code=" + res.code;
    20         wx.request({
    21           url: setURL,
    22           method:'GET',
    23           success: function (res) {
    24             wx.setStorageSync("session_key",res.data.session_key);
    25             wx.setStorageSync("openid", res.data.openid);
    26             that.globalData.openid = res.data.openid;
    27 
    28           }
    29 
    30 
    31         })
    32       }
    33     })
    34     // 获取用户信息
    35     wx.getSetting({
    36       success: res => {
    37         if (res.authSetting['scope.userInfo']) {
    38           // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
    39           wx.getUserInfo({
    40             success: res => {
    41               // 可以将 res 发送给后台解码出 unionId
    42               this.globalData.userInfo = res.userInfo;
    43               wx.setStorageSync("encryptedData", res.encryptedData)
    44               wx.setStorageSync("iv", res.iv)
    45               // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
    46               // 所以此处加入 callback 以防止这种情况
    47               if (this.userInfoReadyCallback) {
    48                 this.userInfoReadyCallback(res)
    49               }
    50               console.log(this.globalData.userInfo);
    51             }
    52           })
    53         }
    54       }
    55     })
    56   },
    57   globalData: {
    58     userInfo:null,
    59     encryptedData:"",
    60     iv:"",
    61     session_key:"",
    62     openid:""
    63   }
    64 })
    View Code
    限定目的,能使人生变得简洁。
  • 相关阅读:
    希尔排序例子
    C
    重构最大堆的例子
    基于堆的优先队列和用优先队列排序的例子
    堆排序例子
    分治法示例
    三路划分的快速排序算法
    二叉搜索例子
    标准快速排序
    【转载】JAVA5新特性
  • 原文地址:https://www.cnblogs.com/lx07/p/7999086.html
Copyright © 2020-2023  润新知