Skip to content

Libraries for base functions and other specific usages including WebAPI, Database Access, IDCardReader, PDF, QRCode, Barcode, SVG, WinForm UI, Short message, Wechat, IM

sunbirdwyk/wyk-base

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overall

Libraries for base functions and other specific usages. Function including:

  • WebAPI (wyk.api/wyk.api.core/wyk.api.fw)
  • Database Access (wyk.db/wyk.db.fw/wyk.db.tool)
  • IDCardReader (wyk.idcard)
  • PDF (wyk.pdf/wyk.pdf.fw)
  • QRCode (wyk.qrcode)
  • Barcode (wyk.basic/wyk.pdf)
  • SVG (wyk.svg)
  • IM Refered (wyk.im)
  • Office Refered (wyk.office)
  • Short Message Refered (wyk.sms)
  • Wechat Refered (wyk.wx)
  • WinForm UI (wyk.ui.forms)

Notes:

Projects with .core ending is .net core (3.0) projects, extra functions for .net core Projects with .fw ending is .net framework (4.6.1) projects, extra functions for .net framework Other projects are .net standard (2.0) projects


Project description


wyk.api

Models and functions for api call

usage:

  1. set server_url in ApiUtil at the beginning
  2. set token after login and get the return token, you can get token in header on API server side
  3. device and user_agent is optional and if you set it, you can get these content in header on API server side
ApiUtil.server_url = "http://testapi.com/";
ApiUtil.get("UserLogin?user_name=testuser&password=testpass", 
     new ApiSuccess((data)=>{
         //API success code
     }), 
     new ApiFailed(message,code)=>{
         //API failed code
     });

on API Side:

Please return ApiResult for each APIController, the following code block is a login Action for .net Framework WebApi project

/// <summary>
/// 人员登录
/// </summary>
/// <param name="user_name">用户名</param>
/// <param name="password">密码</param>
/// <returns>返回带有token和账户信息的字典</returns>
[HttpGet]
[Route("api/UserLogin")]
public ApiResult Login(string user_name, string password)
{
    if(user_name.isNull())
        return ApiResult.errorParameter("用户名不能为空");
    var account = AccountDAO.get(account_type, user_name);
    if (account == null || account.Password != password)
        return ApiResult.errorCustom("用户不存在或密码输入错误");
    if (!account.is_enabled)
        return ApiResult.errorCustom("用户未激活, 请联系管理员激活后再开始使用");
    var record = new LoginRecord(account);
    var commonInfo = ApiHeaderInfo.load(Request);
    record.user_agent = commonInfo.user_agent;
    string msg = record.insert();
    if (!msg.isNull())
    {
        return ApiResult.errorDataSave(msg);
    }
    var login = LoginRecordDAO.getSession(record, account);
    var data = new Dictionary<string, object>();
    data["session"] = login;
    LoginManager.set(login);
    if (commonInfo.device.StartsWith("PC:"))
    {//PC端, 返回权限设置
        data["privilege"] = login.privilege.privilegeContent();
    }
    return ApiResult.success(data);
}

.net core should convert the ApiResult to ObjectResult, you can set an api filter inherited from ApiFilterBase for global for auto convert


wyk.basic

Base models and functions common used for my developing


wyk.db

Database access library for create, access databases including:

  • Access
  • Sql Server
  • Oracle
  • MySql

How to use:

  1. use wyk.db.tool create database and tables specifications in "表维护" tab

generated table specification:

<?xml version="1.0" encoding="utf-8" ?>
<db_table name="c_company" description="单位信息表">
<db_column  name="id"  type="Integer"  primary="1"  description="单位ID" ></db_column>
<db_column  name="name"  type="Varchar"  allownull="1"  length="50"  description="单位名称" ></db_column>
<db_column  name="shortcut"  type="Varchar"  allownull="1"  length="50"  json_ignore="1"  description="名称拼音简写" ></db_column>
<db_column  name="charger"  type="Varchar"  allownull="1"  length="50"  description="负责人" ></db_column>
<db_column  name="charger_phone"  type="Varchar"  allownull="1"  length="50"  description="负责人联系电话" ></db_column>
<db_column  name="image_url"  type="Text"  allownull="1"  description="介绍图片" ></db_column>
<db_column  name="summary"  type="Text"  allownull="1"  description="简介" ></db_column>
<db_column  name="description"  type="Text"  allownull="1"  description="详细介绍" ></db_column>
<db_column  name="enter_time"  type="DateTime"  allownull="1"  description="录入时间" ></db_column>
<db_column  name="opt"  type="Varchar"  allownull="1"  length="50"  description="操作员" ></db_column>
<db_column  name="opt_id"  type="Integer"  allownull="1"  description="操作员ID" ></db_column>
</db_table>
  1. use "导出表适配类" auto create table adapters

generated TableAdapter DBTA_CCompany

using Newtonsoft.Json;
using System;
using wyk.basic;
using wyk.db;

namespace wyk.hmweb.model
{
    /// <summary>
    /// 表名:DBTA_CCompany
    /// 描述:单位信息表
    /// Auto generated by wyk.db.DBTableManager 
    /// </summary>
    [DBTable("c_company", "单位信息表")]
    public class DBTA_CCompany : DBTableAdapter
    {
        /// <summary>
        /// 单位ID
        /// </summary>
        [DBColumn("id", DBDataType.Integer, 0, 0, "", false, true, false, "单位ID", false)]
        public int id = 0;

        /// <summary>
        /// 单位名称
        /// </summary>
        [DBColumn("name", DBDataType.Varchar, 50, 0, "", true, false, false, "单位名称", false)]
        public string name = "";

        /// <summary>
        /// 名称拼音简写
        /// </summary>
        [DBColumn("shortcut", DBDataType.Varchar, 50, 0, "", true, false, false, "名称拼音简写", true)]
        [JsonIgnore]
        public string shortcut = "";

        /// <summary>
        /// 负责人
        /// </summary>
        [DBColumn("charger", DBDataType.Varchar, 50, 0, "", true, false, false, "负责人", false)]
        public string charger = "";

        /// <summary>
        /// 负责人联系电话
        /// </summary>
        [DBColumn("charger_phone", DBDataType.Varchar, 50, 0, "", true, false, false, "负责人联系电话", false)]
        public string charger_phone = "";

        /// <summary>
        /// 介绍图片
        /// </summary>
        [DBColumn("image_url", DBDataType.Text, 0, 0, "", true, false, false, "介绍图片", false)]
        public string image_url = "";

        /// <summary>
        /// 简介
        /// </summary>
        [DBColumn("summary", DBDataType.Text, 0, 0, "", true, false, false, "简介", false)]
        public string summary = "";

        /// <summary>
        /// 详细介绍
        /// </summary>
        [DBColumn("description", DBDataType.Text, 0, 0, "", true, false, false, "详细介绍", false)]
        public string description = "";

        /// <summary>
        /// 录入时间
        /// </summary>
        [DBColumn("enter_time", DBDataType.DateTime, 0, 0, "", true, false, false, "录入时间", false)]
        [JsonIgnore]
        public DateTime enter_time = DateTimeUtil.defaultTime();
        public string EnterTime
        {
            get
            {
                if (DateTimeUtil.isDefaultTime(enter_time))
                    return "";
                else
                    return enter_time.ToString("yyyy-MM-dd HH:mm:ss");
            }
            set
            {
                enter_time = value.datetime();
            }
        }

        /// <summary>
        /// 操作员
        /// </summary>
        [DBColumn("opt", DBDataType.Varchar, 50, 0, "", true, false, false, "操作员", false)]
        public string opt = "";

        /// <summary>
        /// 操作员ID
        /// </summary>
        [DBColumn("opt_id", DBDataType.Integer, 0, 0, "", true, false, false, "操作员ID", false)]
        public int opt_id = 0;

    }
}
  1. inherit the generated table adapters for the real use class

real use class

using System;
using wyk.basic;

namespace wyk.hmweb.model
{
    public class Company : DBTA_CCompany
    {
        public override void processRefFeilds()
        {
            shortcut = name.pinyinShort();
            if (enter_time.isDefault())
                enter_time = DateTime.Now;
        }

        public bool checkDuplicate()
        {
            if (!checkDuplicate<Company>(columns(new string[] { "name" })))
                return checkDuplicate<Company>(columns(new string[] { "code" }));
            return true;
        }
    }
}
  1. inherit DBBaseDAO<T> for database access

DAO for c_company:

using System;
using System.Data;
using wyk.basic;
using wyk.db;
using wyk.hmweb.model;

namespace wyk.hmweb.dal
{
    public class CompanyDAO : DBBaseDAO<Company>
    {
        public static DataTable getListTable(string filter)
        {
            var query =
                "select id, " +
                "  name, " +
                "  charger, " +
                "  charger_phone, " +
                "  image_url, " +
                "  summary " +
                "from " + TABLE_NAME + " " +
                "where 1=1 ";
            if (!filter.isNull())
                query += " and (name like '%" + filter + "%' or shortcut like '%" + filter + "%') ";
            query += " order by name ";
            return DBQuery.query(query);
        }

        public static Company getByName(string name)
        {
            return get(queryParam("name", name).createList());
        }

        public static List<Company> getList(string charger) {

        }
    }
}
  1. set connection at the beginning of your use(only once);
    //Set by connection string
    DBUtil.connection_string = "server=[SERVER];database=[DB-Name];uid=[USER-NAME];pwd=[PASSWORD];Provider=SQLOLEDB;";

    //Set by DBConnection
    var connection = new DBConnection();
    connection.db_type = DBType.SqlServer;
    connection.server_name = "";
    connection.database = "";
    connection.user_name = "";
    connection.password = "";
    connection.refreshConnectionString();
    DBUtil.connection = connection;
  1. now you can use most of the functions for data access and your custom functions
    var company = CompanyDAO.get(id);
    var data = CompanyDAO.getListTable("test");
    var company = CompanyDAO.getByName("company-name");

    var error_message = company.insert();

    error_message = company.update();

    company = new Company();
    company.id = company.nextId();
    company.insert();

Create/Update database and tables with db spec files

  1. Create database
    string msg = DBInitializer.initialize(DBUtil.connection, [Spec-File-Folder]);
    if (msg == "")
        msg = "数据库创建完成! 当前版本号: " + new DBUpdator().db_version.db_version;
    else
        msg = "数据库创建失败, 错误信息:" + msg.htmlEncode();
  1. Check db update

Use inherited DBUpdater for version control

using wyk.db;

namespace wyk.hmweb.db
{
    [DBVersion(1, 1, 3)]
    public class DBUpdator : DBUpdater
    {
    }
}

Check db version:

    var updator = new DBUpdator();
    var current = updator.getCurrentDBVersion(DBUtil.connection);
    if (current.compare(new_version) < 0)
    {
        //New version found, DB deed update
    }
    else
    {
        //Current DB Version is newest, no need update
    }
  1. Update database
    var updator = new DBUpdator();
    var version = updator.updateDB(DBUtil.connection, "wyk.hmweb.db", ref msg);
    if (msg == "")
        msg = "数据库更新完成! 当前版本号:" + version.db_version;
    else
        msg = "数据库更新失败, 错误信息:" + msg;
  1. If you want update database to a new version:
  • create a class inherited from DBUpdateProfile:
using wyk.db;

namespace wyk.hmweb.db
{
    [DBVersion(1, 0, 1)]
    class DBUpdate_1_0_1 : DBUpdateProfile
    { 
        /*
        /// <summary>
        /// 范例 sql 语句
        /// </summary>
        [DBUpdateItem()]
        public string update_1 = "insert into sys_configuration values ('test1','testtest')";
        /// <summary>
        /// 范例 新增列 Add Column
        /// </summary>
        [DBUpdateItem("sys_configuration", DBDataType.Integer, 0, 0, "", false, false, false, "", false)]
        public string update_2 = "test1";
        /// <summary>
        /// 范例 删除列 Delete Column
        /// </summary>
        [DBUpdateItem("sys_configuration")]
        public string update_3 = "test1";*/
    }
}
  • Change attribute value to the new version in DBUpdator (inherited from DBUpdater)
[DBVersion(1, 0, 1)]
public class DBUpdator : DBUpdater
...
  • Call updator.updateDB to update the current database to new version

wyk.idcard

to be continued...

About

Libraries for base functions and other specific usages including WebAPI, Database Access, IDCardReader, PDF, QRCode, Barcode, SVG, WinForm UI, Short message, Wechat, IM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages