From 94b5647b6b3b0083a46e6c47d419762a97d8f41d Mon Sep 17 00:00:00 2001
From: lccsw <805383944@qq.com>
Date: Wed, 20 Jul 2022 10:46:53 +0800
Subject: [PATCH] add-liche-credit
---
admin/controllers/app/liche/Member.php | 123 +++++++++++++++++
admin/views/app/liche/member/add_credit.php | 39 ++++++
admin/views/app/liche/member/add_items.php | 41 ++++++
.../app/liche/member/credit_log_lists.php | 79 +++++++++++
admin/views/app/liche/member/get_freeze.php | 22 +++
admin/views/app/liche/member/lists.php | 33 +++--
api/controllers/Welcome.php | 4 +
api/controllers/wxapp/liche/Credit.php | 69 ++++++++++
api/controllers/wxapp/liche/User.php | 3 +
common/libraries/entity/Credits_entity.php | 125 ++++++++++++++++++
.../app/liche/App_liche_credits_log_model.php | 33 +++++
sql/app/liche.sql | 23 ++++
12 files changed, 583 insertions(+), 11 deletions(-)
create mode 100644 admin/views/app/liche/member/add_credit.php
create mode 100644 admin/views/app/liche/member/add_items.php
create mode 100755 admin/views/app/liche/member/credit_log_lists.php
create mode 100644 admin/views/app/liche/member/get_freeze.php
create mode 100644 api/controllers/wxapp/liche/Credit.php
create mode 100644 common/libraries/entity/Credits_entity.php
create mode 100755 common/models/app/liche/App_liche_credits_log_model.php
diff --git a/admin/controllers/app/liche/Member.php b/admin/controllers/app/liche/Member.php
index b7913b99..6a217028 100755
--- a/admin/controllers/app/liche/Member.php
+++ b/admin/controllers/app/liche/Member.php
@@ -7,6 +7,8 @@ class Member extends HD_Controller{
{
parent::__construct();
$this->load->model('app/liche/App_liche_users_model', 'userM');
+ $this->load->model('app/liche/app_liche_credits_log_model');
+ $this->load->library('entity/credits_entity');
}
//首页信息
@@ -149,4 +151,125 @@ class Member extends HD_Controller{
{
}
+
+ //冻结资金
+ public function get_freeze(){
+ $uid = $this->input->get_post('uid');
+ $user = $this->userM->get(['id'=>$uid]);
+ if(!$user){
+ return $this->show_json(SYS_CODE_FAIL, '参数错误');
+ }
+ if($this->input->method() == 'post'){
+ $credit = abs(intval($this->input->post('credits')));
+ $result = $this->credits_entity->freeze($uid,$credit,1);
+ if($result['code']){
+ return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
+ }else{
+ return $this->show_json(SYS_CODE_FAIL, $result['msg']);
+ }
+ }
+ $this->data['uid'] = $uid;
+ $this->data['user'] = $user;
+ return $this->show_view('app/liche/member/get_freeze');
+ }
+
+ //增加积分
+ public function add_credit(){
+ $uid = $this->input->get_post('uid');
+ $user = $this->userM->get(['id'=>$uid]);
+ if(!$user){
+ return $this->show_json(SYS_CODE_FAIL, '参数错误');
+ }
+ if($this->input->method() == 'post'){
+ $credits = abs(intval($this->input->post('credits')));
+ $remark = $this->input->post('remark');
+ $type = $this->input->post('type');
+ $jsondata['admin_id'] = $this->uid;
+ $result = $this->credits_entity->change($uid,$credits,4,0,$remark,$jsondata);
+ if($result['code']){
+ if($type==2){
+ $this->credits_entity->freeze($uid,$credits);
+ }
+ return $this->show_json(SYS_CODE_SUCCESS, '添加成功');
+ }else{
+ return $this->show_json(SYS_CODE_FAIL, $result['msg']);
+ }
+ }
+ $this->data['uid'] = $uid;
+ $this->data['user'] = $user;
+ return $this->show_view('app/liche/member/add_credit');
+ }
+
+ public function get_credit_log(){
+ $params = $this->input->get();
+ $page = $params['page'];
+ $size = $params['size'];
+ !$page && $page = 1;
+ !$size && $size = 40;
+
+ $where = [];
+
+ if(strlen($params['type'])){
+ $where['type'] = $params['type'];
+ }else{
+ $params['type'] = '';
+ }
+
+ $params['uid'] && $where['uid'] = $params['uid'];
+
+ $type_arr = $this->app_liche_credits_log_model->get_type();
+ $count = $this->app_liche_credits_log_model->count($where);
+ $lists = [];
+ if($count){
+ $rows = $this->app_liche_credits_log_model->select($where,'id desc',$page,$size);
+ $uids_arr = array_unique(array_column($rows,'uid'));
+ $users = [];
+ if($uids_arr){
+ $uids_str = implode(',',$uids_arr);
+ $u_where = [
+ "id in ({$uids_str})" => null
+ ];
+ $users = $this->userM->map('id','nickname',$u_where,'id desc',0,0,'id,nickname');
+ }
+ foreach($rows as $item){
+ $item['type_cn'] = $type_arr[$item['type']];
+ $item['nickname'] = $users[$item['uid']];
+ $lists[] = $item;
+ }
+ }
+ $this->data['params'] = $params;
+ $this->data['type_arr'] = $type_arr;
+ $this->data['lists'] = $lists;
+ $this->data['_title'] = '积分日志';
+ $this->data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
+
+ return $this->show_view('app/liche/member/credit_log_lists',true);
+ }
+
+ //兑换积分
+ public function add_items(){
+ $uid = $this->input->get_post('uid');
+ $user = $this->userM->get(['id'=>$uid]);
+ if(!$user){
+ return $this->show_json(SYS_CODE_FAIL, '参数错误');
+ }
+ if($this->input->method() == 'post'){
+ $credits = -1 * abs(intval($this->input->post('credits')));
+ $remark = $this->input->post('remark');
+ $item_title = $this->input->post('item_title');
+ $item_code = $this->input->post('item_code');
+ $jsondata['admin_id'] = $this->uid;
+ $jsondata['item_title'] = $item_title;
+ $jsondata['item_code'] = $item_code;
+ $result = $this->credits_entity->change($uid,$credits,5,0,$remark,$jsondata);
+ if($result['code']){
+ return $this->show_json(SYS_CODE_SUCCESS, '兑换成功');
+ }else{
+ return $this->show_json(SYS_CODE_FAIL, $result['msg']);
+ }
+ }
+ $this->data['uid'] = $uid;
+ $this->data['user'] = $user;
+ return $this->show_view('app/liche/member/add_items');
+ }
}
diff --git a/admin/views/app/liche/member/add_credit.php b/admin/views/app/liche/member/add_credit.php
new file mode 100644
index 00000000..e030de33
--- /dev/null
+++ b/admin/views/app/liche/member/add_credit.php
@@ -0,0 +1,39 @@
+
\ No newline at end of file
diff --git a/admin/views/app/liche/member/add_items.php b/admin/views/app/liche/member/add_items.php
new file mode 100644
index 00000000..83899173
--- /dev/null
+++ b/admin/views/app/liche/member/add_items.php
@@ -0,0 +1,41 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/admin/views/app/liche/member/credit_log_lists.php b/admin/views/app/liche/member/credit_log_lists.php
new file mode 100755
index 00000000..87fc607f
--- /dev/null
+++ b/admin/views/app/liche/member/credit_log_lists.php
@@ -0,0 +1,79 @@
+
+
+
+
+
+
共有= $pager['totle'] ?>条数据
+
+
+
+ ID
+ 用户
+ 收入积分
+ 支出积分
+ 剩余积分
+ 类型
+ 备注
+ 创建时间
+
+
+
+
+
+ = $v['id'] ?>
+ = $v['nickname'] ?>
+ = $v['credit_in'] ?>
+ = $v['credit_out'] ?>
+ = $v['credit_left'] ?>
+ = $v['type_cn'] ?>
+ = $v['remark'] ?>
+ = date('Y-m-d H:i',$v['c_time']) ?>
+
+
+
+
+
+
+
+
+
+
+
diff --git a/admin/views/app/liche/member/get_freeze.php b/admin/views/app/liche/member/get_freeze.php
new file mode 100644
index 00000000..6d27aec4
--- /dev/null
+++ b/admin/views/app/liche/member/get_freeze.php
@@ -0,0 +1,22 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/admin/views/app/liche/member/lists.php b/admin/views/app/liche/member/lists.php
index 7e8597f8..1762a75e 100755
--- a/admin/views/app/liche/member/lists.php
+++ b/admin/views/app/liche/member/lists.php
@@ -27,10 +27,11 @@
ID
用户名称
手机号码
- 是否分销
- 分销管理
- 创建时间
- 操作
+ 剩余积分
+ 冻结积分
+ 是否分销
+ 分销管理
+ 创建时间
@@ -39,6 +40,8 @@
= $v['id'] ?>
= $v['nickname'] ?>
= $v['mobile'] ?>
+ = $v['credits'] ?>
+ = $v['freeze_credits'] ?>
onchange="set_publish(this,=$v['id']?>)"/>
@@ -46,7 +49,21 @@
onchange="set_publish(this,=$v['id']?>,'manage')"/>
= date('Y-m-d H:i:s',$v['c_time']) ?>
-
+
+
+
+
+ 兑换商品
+
+
+ 增加积分
+
+
+ 解冻积分
+
+
+ 积分日志
+
编辑马甲
-
diff --git a/api/controllers/Welcome.php b/api/controllers/Welcome.php
index b80b45fe..90ba84e1 100755
--- a/api/controllers/Welcome.php
+++ b/api/controllers/Welcome.php
@@ -11,5 +11,9 @@ class Welcome extends CI_Controller {
}
public function test(){
+ $this->load->library('entity/credits_entity');
+// $res = $this->credits_entity->change(4,10);
+ $res = $this->credits_entity->freeze(4,10,1);
+ print_r($res);
}
}
diff --git a/api/controllers/wxapp/liche/Credit.php b/api/controllers/wxapp/liche/Credit.php
new file mode 100644
index 00000000..850bac19
--- /dev/null
+++ b/api/controllers/wxapp/liche/Credit.php
@@ -0,0 +1,69 @@
+login_white = array();//登录白名单
+ $this->check_status = array();//用户状态校验
+ $this->check_mobile = array();//需要手机号
+ $this->check_headimg = array();//授权微信信息
+ $this->load->model('app/liche/app_liche_credits_log_model');
+ }
+
+ protected function get(){
+ $page = $this->input_param('page');
+ $size = $this->input_param('size');
+ !$page && $page = 1;
+ !$size && $size = 20;
+
+ $where = [
+ 'uid' => $this->session['uid']
+ ];
+ $count = $this->app_liche_credits_log_model->count($where);
+ $list = [];
+ if($count){
+ $fileds = 'id,credit_in,credit_out,remark,type,c_time';
+ $rows = $this->app_liche_credits_log_model->select($where,'id desc',$page,$size,$fileds);
+ $type_arr = $this->app_liche_credits_log_model->get_type();
+ foreach ($rows as $item) {
+ $title = $type_arr[$item['type']];
+ $money = $item['credit_in'] ? "+{$item['credit_in']}" : "-{$item['credit_out']}";
+ $temp = [
+ 'id' => $item['id'],
+ 'title' => $title,
+ 'desc' => $item['remark'] ? $item['remark'] : '',
+ 'money' => $money
+ ];
+ $list[] = $temp;
+ }
+ }
+ $data = [
+ 'list' => $list,
+ 'total' => $count,
+ 'bg_list' => [
+ 'https://qs.haodian.cn/wechat_app/liche/topicActivity/lantushijia/ex-1.jpg'
+ ]
+ ];
+ return $data;
+ }
+
+ protected function get_rule(){
+ $data = [
+ 'title' => '规则标题',
+ 'content' => '活动规则',
+ ];
+ return $data;
+ }
+}
\ No newline at end of file
diff --git a/api/controllers/wxapp/liche/User.php b/api/controllers/wxapp/liche/User.php
index 5abb737a..1e42c0c8 100644
--- a/api/controllers/wxapp/liche/User.php
+++ b/api/controllers/wxapp/liche/User.php
@@ -100,6 +100,8 @@ class User extends Wxapp{
],
'isdealer' => $this->session['dealer'],
'istop' => $istop,
+ 'credits' => $user['credits'],
+ 'freeze_credits' => $user['freeze_credits']
);
return $data;
@@ -237,6 +239,7 @@ class User extends Wxapp{
'have_new' => $un_pay ? true : false
],
['title'=>'我的爱车','icon'=>'https://qs.haodian.cn/wechat_app/liche/mine/list-icon-1.png','url'=>'/pages/mine/myCar/index'],
+ ['title'=>'我的积分','icon'=>'https://qs.haodian.cn/wechat_app/liche/mine/list-icon-6.png','url'=>'/pages/topicActivity/integral/index'],
['title'=>'联系小狸','icon'=>'https://qs.haodian.cn/wechat_app/liche/mine/list-icon-3.png'],
];
if($this->session['dealer'] || $this->session['manage']){
diff --git a/common/libraries/entity/Credits_entity.php b/common/libraries/entity/Credits_entity.php
new file mode 100644
index 00000000..068928b8
--- /dev/null
+++ b/common/libraries/entity/Credits_entity.php
@@ -0,0 +1,125 @@
+ci = get_instance();
+ $this->ci->load->model('app/liche/app_liche_users_model');
+ $this->ci->load->model('app/liche/app_liche_credits_log_model');
+ }
+
+ /**
+ * 积分变动
+ * @param $uid 用户id
+ * @param $credit 积分
+ * @param $type 类型 0活动 1派单成功 2完成试驾 3开票 4后台录入 5兑换商品
+ * @param $target_id 来源id
+ * @param $remark 备注
+ * @param $jsondata 其它json数据
+ * @return array
+ */
+ public function change($uid,$credit,$type=0,$target_id=0,$remark='',$jsondata=[]){
+ if(!$uid || !$credit){
+ return ['code'=>0 ,'msg'=>'参数错误'];
+ }
+ $row = $this->ci->app_liche_users_model->get(['id'=>$uid]);
+ if(!$row){
+ return ['code'=>0 ,'msg'=>'用户不存在'];
+ }
+ try {
+ $this->ci->db->trans_begin();
+ $up_where = [
+ 'id' => $uid
+ ];
+ $credit_left = $row['credits']+$row['freeze_credits'];
+ if($credit>0){
+ $up_credit = [
+ "credits = credits+{$credit}" => null
+ ];
+ $credit_left += $credit;
+ }else{
+ $credit_out = abs($credit);
+ $up_credit = [
+ "credits = credits-{$credit_out}" => null
+ ];
+ $up_where["credits >="] = abs($credit_out);
+ $credit_left -= $credit_out;
+ }
+ $this->ci->app_liche_users_model->update($up_credit,$up_where);
+ $user_aff_rows = $this->ci->app_liche_users_model->db->affected_rows();
+ if(!$user_aff_rows){
+ throw new Exception('积分不足');
+ }
+ $add_log = [
+ 'uid' => $uid,
+ 'type' => $type,
+ 'credit_left' => $credit_left,
+ 'c_time' => time()
+ ];
+ if($credit>0){
+ $add_log['credit_in'] = $credit;
+ }else{
+ $add_log['credit_out'] = abs($credit);
+ }
+ $target_id && $add_log['target_id'] = $target_id;
+ $remark && $add_log['remark'] = $remark;
+ $jsondata && $add_log['jsondata'] = json_encode($jsondata,JSON_UNESCAPED_UNICODE);
+ $this->ci->app_liche_credits_log_model->add($add_log);
+ $log_aff_rows = $this->ci->app_liche_users_model->db->affected_rows();
+ if(!$log_aff_rows){
+ throw new Exception('积分不足');
+ }
+ $this->ci->db->trans_commit();
+ return ['code'=>1,'msg'=>'操作成功'];
+ }catch (Exception $e){
+ $this->ci->db->trans_rollback();
+ return ['code'=>0,'msg'=>$e->getMessage()];
+ }
+ }
+
+ /**
+ * 冻结积分
+ * @param $uid
+ * @param $credit
+ * @param $type (0冻结资金 1解冻资金)
+ * @return array
+ */
+ public function freeze($uid,$credit,$type=0){
+ $row = $this->ci->app_liche_users_model->get(['id'=>$uid]);
+ $where = [
+ 'id' => $uid
+ ];
+ if($type){
+ if($row['freeze_credits']<$credit){
+ return ['code'=>0,'msg'=>'积分不足'];
+ }
+ $update = [
+ "credits = credits+{$credit}" => null,
+ "freeze_credits = freeze_credits-{$credit}" => null
+ ];
+ $where['freeze_credits>='] = $credit;
+ }else{
+ if($row['credits']<$credit){
+ return ['code'=>0,'msg'=>'冻结积分不足'];
+ }
+ $update = [
+ "credits = credits-{$credit}" => null,
+ "freeze_credits = freeze_credits+{$credit}" => null
+ ];
+ $where['credits>='] = $credit;
+ }
+ $this->ci->app_liche_users_model->update($update,$where);
+ $aff_rows = $this->ci->app_liche_users_model->db->affected_rows();
+ if($aff_rows){
+ return ['code'=>1,'msg'=>'操作成功'];
+ }else{
+ return ['code'=>0,'msg'=>'操作失败'];
+ }
+ }
+}
diff --git a/common/models/app/liche/App_liche_credits_log_model.php b/common/models/app/liche/App_liche_credits_log_model.php
new file mode 100755
index 00000000..024d64fd
--- /dev/null
+++ b/common/models/app/liche/App_liche_credits_log_model.php
@@ -0,0 +1,33 @@
+'活动积分',1=>'派单成功',2=>'完成试驾',
+ 3=>'开票',4=>'后台录入',5=>'兑换商品'
+ ];
+
+ public function __construct()
+ {
+ parent::__construct($this->table_name, 'default');
+ }
+
+ /**
+ * @param $type
+ * @return string|string[]
+ */
+ public function get_type($type){
+ if($this->type_arr[$type]){
+ $result = $this->type_arr[$type];
+ }else{
+ $result = $this->type_arr;
+ }
+ return $result;
+ }
+}
diff --git a/sql/app/liche.sql b/sql/app/liche.sql
index f98499b5..c10624d3 100644
--- a/sql/app/liche.sql
+++ b/sql/app/liche.sql
@@ -22,6 +22,8 @@ alter table lc_app_liche_users add dealer tinyint(1) not null default '0'
alter table lc_app_liche_users add up_uid int(10) not null default '0' comment '上级uid' after dealer;
alter table lc_app_liche_users add manage tinyint(1) not null default 0 comment '是否分销管理' after dealer;
alter table lc_app_liche_users add deal_uid int(10) not null default 0 comment '审核分销uid' after dealer;
+alter table lc_app_liche_users add credits int(10) unsigned not null default '0' COMMENT '当前可用积分' after nickname;
+alter table lc_app_liche_users add freeze_credits int(10) unsigned not null default '0' COMMENT '冻结积分' after credits;
-- ----------------------------
-- Title:狸车小程序cms
@@ -101,3 +103,24 @@ create table lc_app_liche_orders (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户购车订单'
alter table lc_app_liche_orders add img varchar(100) not null default '' comment '支付截图' after descrip;
alter table lc_app_liche_orders add admin_id int(10) not null default 0 comment '管理员id' after uid;
+
+-- ----------------------------
+-- Title:狸车小程序用户积分日志表
+-- Author:lcc
+-- Table:lc_app_liche_credits_log
+-- ---------------------------
+drop table if exists lc_app_liche_credits_log;
+create table lc_app_liche_credits_log (
+ id int(10) unsigned not null auto_increment comment '自增id',
+ uid int(10) unsigned not null comment '用户id',
+ credit_in int(11) not null comment '积分入账',
+ credit_out int(11) not null comment '积分出账',
+ credit_left int(11) not null comment '剩余积分',
+ type tinyint(1) not null default '1' comment '类型 0活动 1派单成功 2完成试驾 3开票 4后台录入 5兑换商品',
+ target_id int(10) not null default '0' comment '来源id',
+ remark text comment '备注更多信息',
+ jsondata json default null comment '其他信息',
+ c_time int(10) unsigned not null default '0' comment '创建时间',
+ u_time timestamp not null default current_timestamp on update current_timestamp,
+primary key (id)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='狸车小程序用户积分日志表'