在Common文件中创建BaseModel
public class BaseModel<T> where T : class, new() { /// <summary> /// 获取上下文 /// </summary> /// <returns>EF上下文</returns> public static DemoContext Create() { DemoContext db = CallContext.GetData("db") as DemoContext; if (db == null) { db = new DemoContext(); CallContext.SetData("db", db); } return db; } /// <summary> /// 上下文 /// </summary> DemoContext db = Create(); /// <summary> /// 查询数据,IQueryable延迟加载集合,调用时才会去加载至内存,List查询后会立即加载至内存中 /// </summary> /// <param name="whereLambda">查询条件</param> /// <returns>数据集合</returns> public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda) { return db.Set<T>().Where(whereLambda); } public int Add(T entity) { db.Set<T>().Add(entity); return db.SaveChanges(); } public int Delete(int id) { //通过ID查询这条实体 T entity = db.Set<T>().Find(id); db.Set<T>().Remove(entity); return db.SaveChanges(); } public int Update(T entity) { //设置当前实体的状态为修改 db.Entry(entity).State = System.Data.Entity.EntityState.Modified; return db.SaveChanges(); } }
在Common中创建HttpRequestHelper
public class HttpRequestHelper { public static string HttpGet(string url) { string result = string.Empty; try { HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url); wbRequest.Method = "GET"; HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse(); using (Stream responseStream = wbResponse.GetResponseStream()) { using (StreamReader sReader = new StreamReader(responseStream)) { result = sReader.ReadToEnd(); } } } catch (Exception ex) { } return result; } public static string HttpPost(string url, string paramData, Dictionary<string, string> headerDic = null) { string result = string.Empty; try { HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url); wbRequest.Method = "POST"; wbRequest.ContentType = "application/x-www-form-urlencoded"; wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData); if (headerDic != null && headerDic.Count > 0) { foreach (var item in headerDic) { wbRequest.Headers.Add(item.Key, item.Value); } } using (Stream requestStream = wbRequest.GetRequestStream()) { using (StreamWriter swrite = new StreamWriter(requestStream)) { swrite.Write(paramData); } } HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse(); using (Stream responseStream = wbResponse.GetResponseStream()) { using (StreamReader sread = new StreamReader(responseStream)) { result = sread.ReadToEnd(); } } } catch (Exception ex) { } return result; } }
全局变量globalData在app.js页面和其他js页面调用与修改问题
全局变量globalData
小程序中js页面声明的变量与函数只能在此页面中使用,并且在不同页面中可以用相同的变量命名。而如果想要某些数据在所有页面中都能使用,那就可以使用全局变量globalData进行数据的存取。globalData在app.js中进行设置。
//app.js文件 App({ //在小程序完成初始化时,触发onLaunch(全局只触发一次) // Do something initial when launch. onLaunch(options) { }, //全局变量 globalData: { Info: 12, newsList: [{}] } })
一、app.js页面中globalData变量的修改
1、在app.js页面中要修改globalData变量的值,如果直接在onLaunch: function () 函数中修改,只需要用this调用globalData进行修改。
this.globalData.Info = 15; console.log(this.globalData.Info) //15
2、若使用了请求wx.request等 则需要使用 var that = this,然后用that调用globalData。
//app.js文件 App({ //在小程序完成初始化时,触发onLaunch(全局只触发一次) // Do something initial when launch. onLaunch(options) { //请求信息 var that = this; wx.request({ url: 'https:', //此处略 data: { x: "", y: "" }, success: function (res) { console.log(res.data); let list = []; for (var i = 0; i < 4; i++) { let obj = {}; obj.id = res.data.result.data[i].uniquekey; obj.title = res.data.result.data[i].title; obj.date = res.data.result.data[i].date; obj.url = res.data.result.data[i].url; obj.poster = res.data.result.data[i].thumbnail_pic_s; list.push(obj); } that.globalData.newsList = list; } }) }, //全局变量 globalData: { Info: 12, newsList: [{}] } })
二、其他js页面中globalData变量的读取和修改
当需要在其他页面读取和修改globalData中的数据时,需要用var app = getApp()进行引用
1、读取
var app = getApp(); //声明app实例 Page({ onl oad: function () { console.log(app.globalData.Info); } })
2、修改数据
var app = getApp(); Page({ onl oad: function () { app.globalData.Info = 22; } })
标签:临时,app,db,globalData,result,wbRequest,data From: https://www.cnblogs.com/shiliumu/p/16817257.html