.NET开源商城CoreShop简记

.NET开源商城CoreShop简记
大概本地运行起来了,官网 https://www.coreshop.cn/
  1. 先附加SQLSERVER库,
  2. 运行redis,
  3. vs打开后项目9.APP下的admin是后台,api是接口,二个项目里的Appsetting.json里的数据库连接字符串和redis的字符串都得改,redis运行时默认密码是空的,然后api的那个跨域也设置为true(取消跨域),运行api项目,再运行admin项目,默认用户名密码都是coreshop,不出意外的话后台应该就能看到了,
  4. 用hbuilder打开项目文件夹uniapp下的coreshop文件夹,改D:\workspace\CoreShop-v1.5.5\CoreCms.Net.Uni-App\CoreShop\common\setting\constVarsHelper.js文件里的baseurl为之前API的地址,如:export const apiBaseUrl = 'http://localhost:2015';,再在浏览器中运行,不出意外的话应该是可以看到了,自己测试过小程序,结果在小程序上运行不起来,不知道怎么回事。。算了。。先不管了。。
  5. 后台用的 layuiadmin ,在D:\workspace\CoreShop-v1.5.5\CoreCms.Net.Web.Admin\wwwroot\lib\layuiAdmin\config.js里看相关的配置的,默认视图是在wwwroot下的views下,在VS中创建文件 D:\workspace\CoreShop-v1.5.5\CoreCms.Net.Web.Admin\wwwroot\views\content\article\niunandemo\index.html,内容:
<h1>牛腩测试一下</h1>
<input id="aaa" class="layui-input" />
<button type="button" class="layui-btn" onclick="aaa()">添加数据测试</button>

<script>
    function aaa() {
        layui.use([ 'laydate'],
            function () {
                var $ = layui.$, laydate = layui.laydate;
                laydate.render({
                    elem: '#aaa'
                });
                var url = layui.setter.apiUrl + "Api/NiunanUserInfo/Test";
                var postdata = { username: "niunan", password: "123456" }
                $.get(url, postdata, function (data) {
                    alert("服务端返回:" + JSON.stringify(data));
                });
            });
    }
</script>

6. 接下来做自己的后端接口,流程:数据库中建表 --》2.Entity项目中建立模型 --》4.Repository里建立仓储接口和仓储实现 --》3.Services中建立服务接口和实现 --》9.App 中的CoreCms.Net.Web.Admin项目中建立控制器来实现接口

create table NiunanUserInfo(
 Id int primary key identity,
 CreateTime datetime not null default getdate(),
 UpdateTime datetime not null default getdate(),
 UserName nvarchar(50) not null default ''
)

 

// CoreCms.Net.Model\Entities\NiunanDemo\NiunanUserInfo.cs
using SqlSugar;
using System.ComponentModel.DataAnnotations;

namespace CoreCms.Net.Model.Entities
{
    /// <summary>
    /// 牛腩自己建的用户表
    /// </summary>
    [SugarTable("NiunanUserInfo", TableDescription = "牛腩用户表")]
    public partial class NiunanUserInfo
    {
        /// <summary>
        /// 用户表
        /// </summary>
        public NiunanUserInfo()
        {
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        [Display(Name = "用户ID")]
        [SugarColumn(ColumnDescription = "用户ID", IsPrimaryKey = true, IsIdentity = true)]
        [Required(ErrorMessage = "请输入{0}")]
        public System.Int32 Id { get; set; }
 
        /// <summary>
        /// UserName
        /// </summary>
        [Display(Name = "UserName")]
        [SugarColumn(ColumnDescription = "UserName", IsNullable = true)]
        [StringLength(50, ErrorMessage = "【{0}】不能超过{1}字符长度")]
        public System.String UserName { get; set; }
       
        /// <summary>
        /// 创建时间
        /// </summary>
        [Display(Name = "创建时间")]
        [SugarColumn(ColumnDescription = "创建时间", IsNullable = true)]
        public System.DateTime CreateTime { get; set; } = System.DateTime.Now;
        /// <summary>
        /// 更新时间
        /// </summary>
        [Display(Name = "更新时间")]
        [SugarColumn(ColumnDescription = "更新时间", IsNullable = true)]
        public System.DateTime UpdateTime { get; set; }  = System.DateTime.Now;
    }
}
// CoreCms.Net.IRepository\NiunanDemo\INiunanUserInfoRepository.cs
using CoreCms.Net.Model.Entities;

namespace CoreCms.Net.IRepository
{
 
    public interface INiunanUserInfoRepository : IBaseRepository<NiunanUserInfo>
    {
    }
}


// CoreCms.Net.Repository\NiunanDemo\NiunanUserInfoRepository.cs
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.Model.Entities;

namespace CoreCms.Net.Repository
{ 
    public class NiunanUserInfoRepository : BaseRepository<NiunanUserInfo>, INiunanUserInfoRepository
    {
        public NiunanUserInfoRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }


    }
}
// CoreCms.Net.IServices\NiunanDemo\INiunanUserInfoServices.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using CoreCms.Net.Model.Entities;

namespace CoreCms.Net.IServices
{ 
    public interface INiunanUserInfoServices  : IBaseServices<NiunanUserInfo>
    {
     
    }
}

// CoreCms.Net.Services\NiunanDemo\NiunanUserInfoServices.cs
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;

namespace CoreCms.Net.Services
{ 
    public class NiunanUserInfoServices : BaseServices<NiunanUserInfo>, INiunanUserInfoServices
    {
        private readonly INiunanUserInfoRepository _dal;
        private readonly IUnitOfWork _unitOfWork;

        public NiunanUserInfoServices(IUnitOfWork unitOfWork, INiunanUserInfoRepository dal)
        {
            _dal = dal;
            BaseDal = dal;
            _unitOfWork = unitOfWork;
        }
    }
}
// CoreCms.Net.Web.Admin\Controllers\NiunanDemo\NiunanUserInfoController.cs
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Web.Admin.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.ComponentModel;

namespace CoreCms.Net.Web.Admin.Controllers
{
    /// <summary>
    ///     牛腩测试
    /// </summary>
    [Description("牛腩测试")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    [RequiredErrorForAdmin]
    [Authorize(Permissions.Name)]
    public class NiunanUserInfoController : ControllerBase
    {
        private readonly INiunanUserInfoServices _services;

        public NiunanUserInfoController(INiunanUserInfoServices _services)
        {
            this._services = _services;
        }
        [AllowAnonymous]
        [HttpGet]
        public string Test(string username, string password)
        {
            try
            {
                int id = _services.Insert(new NiunanUserInfo()
                {
                    UserName = username,
                });
                return "插入成功,返回的ID是:"+id;
            }
            catch (Exception ex)
            {
                return "出错:" + ex.Message;
            }
        }
    }
}

 

posted @ 2024-07-07 10:23  牛腩  阅读(6)  评论(0编辑  收藏  举报