作者 景龙
1 个管道 的构建 失败 耗费 0 秒

修改积分增增加规则

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\admin\model\User;
use think\Db;
/**
* 系统消息管理
*
* @icon fa fa-circle-o
*/
class Message extends Backend
{
/**
* Message模型对象
* @var \app\admin\model\Message
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->request->filter(['strip_tags']);
$this->model = new \app\admin\model\Message;
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
$user_ids = $this->request->post("user_ids");
if ($params) {
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
//系统推送消息
$id = $this->model->id;
$userModel = new User();
if(!empty($user_ids)){
$users = explode(',',$user_ids);
}else{
$users = $userModel->where(['status'=>'normal'])->column('id');
}
$logList = [];
foreach ($users as $value){
$log['message_id'] = $id;
$log['createtime'] = time();
$log['user_id'] = $value;
array_push($logList,$log);
}
$row = false;
if(!empty($logList)){
$row = Db::name('message_log')->insertAll($logList);
}
if ($result !== false && $row) {
$this->success();
} else {
$this->error($this->model->getError());
}
} catch (\think\exception\PDOException $e) {
$this->error($e->getMessage());
} catch (\think\Exception $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = NULL)
{
$row = $this->model->get($ids);
$userIds=Db::name('message_log')->where(['message_id'=>$ids])->column('user_id');
if (!$row)
$this->error(__('No Results were found'));
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
$user_ids = $this->request->post("user_ids");
if ($params) {
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validate($validate);
}
$userModel = new User();
if(!empty($user_ids)){
$users = explode(',',$user_ids);
}else{
$users = $userModel->where(['status'=>'normal'])->column('id');
}
$result = $row->allowField(true)->save($params);
$sameUsers = array_intersect($userIds,$users);
$needDeleteUsersIds = array_diff($userIds, $sameUsers);
$newUsersIds = array_diff($users, $sameUsers);
$logList = [];
foreach ($newUsersIds as $value){
$log['message_id'] = $ids;
$log['createtime'] = time();
$log['user_id'] = $value;
array_push($logList,$log);
}
//删除旧数据
$row2 = true;
if(!empty($needDeleteUsersIds)){
$row2 = Db::name('message_log')->where(['user_id'=>['in',$needDeleteUsersIds],'message_id'=>$ids])->delete();
}
//新增新数据
$row = true;
if(!empty($logList)){
$row = Db::name('message_log')->insertAll($logList);
}
if ($result !== false && $row && $row2) {
$this->success();
} else {
$this->error($row->getError());
}
} catch (\think\exception\PDOException $e) {
$this->error($e->getMessage());
} catch (\think\Exception $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$this->view->assign("userIds", implode(',',$userIds));
$this->view->assign("row", $row);
return $this->view->fetch();
}
/**
* 删除
*/
public function del($ids = "")
{
if ($ids) {
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$count = $this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
foreach ($list as $k => $v) {
$count += $v->delete();
Db::name('message_log')->where(['message_id'=>$v['id']])->delete();
}
if ($count) {
$this->success();
} else {
$this->error(__('No rows were deleted'));
}
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
}
<?php
return [
'Id' => 'ID',
'Souce' => '消息来源',
'To_uid' => '接收对象',
'Title' => '标题',
'Description' => '内容',
'Createtime' => '创建时间',
];
<?php
namespace app\admin\model;
use think\Model;
class Message extends Model
{
// 表名
protected $name = 'message';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'deletetime_text'
];
public function getDeletetimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['deletetime']) ? $data['deletetime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setDeletetimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}
}
<?php
namespace app\admin\validate;
use think\Validate;
class Message extends Validate
{
/**
* 验证规则
*/
protected $rule = [
];
/**
* 提示消息
*/
protected $message = [
];
/**
* 验证场景
*/
protected $scene = [
'add' => [],
'edit' => [],
];
}
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('To_uid')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-user_ids" data-source="user/user/index" placeholder="为空将发送给全部用户" type="text"
data-field="username" data-multiple="true" class="form-control selectpage" name="user_ids" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Title')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-title" data-rule="required" class="form-control" name="row[title]" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Description')}:</label>
<div class="col-xs-12 col-sm-8">
<textarea id="c-description" data-rule="required" rows="5" class="form-control editor" name="row[description]" cols="50"></textarea>
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('To_uid')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-user_ids" data-source="user/user/index" placeholder="为空将发送给全部用户" type="text"
data-field="username" data-multiple="true" class="form-control selectpage" name="user_ids" value="{$userIds}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Title')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-title" data-rule="required" class="form-control" name="row[title]" type="text" value="{$row.title}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Description')}:</label>
<div class="col-xs-12 col-sm-8">
<textarea id="c-description" data-rule="required" rows="5" class="form-control editor" name="row[description]" cols="50">{$row.description}</textarea>
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
<div class="panel panel-default panel-intro">
{:build_heading()}
<div class="panel-body">
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="one">
<div class="widget-body no-padding">
<div id="toolbar" class="toolbar">
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('message/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('message/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('message/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
<a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('message/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>
<div class="dropdown btn-group {:$auth->check('message/multi')?'':'hide'}">
<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>
<ul class="dropdown-menu text-left" role="menu">
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=normal"><i class="fa fa-eye"></i> {:__('Set to normal')}</a></li>
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
</ul>
</div>
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-edit="{:$auth->check('message/edit')}"
data-operate-del="{:$auth->check('message/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
... ... @@ -4,6 +4,7 @@ namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\Bank;
use app\admin\model\off\Line;
use think\Db;
use think\Validate;
/**
... ... @@ -11,8 +12,8 @@ use think\Validate;
*/
class Person extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $noNeedLogin = ['getScore'];
protected $noNeedRight = ['getScore'];
protected $person = 0;//个人
protected $company = 1;//企业
protected $user_id = '';//token存贮user_id
... ... @@ -240,4 +241,19 @@ class Person extends Api
$this->error('请求方式错误');
}
}
/**
* 根据规则获取积分
* @param $share_uid
* @param $type
*/
public function getScore($share_uid,$type){
$score = config('site.'.$type);
//给分享用户增加积分
$user = new \app\admin\model\User();
$user->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
//成为分享用户的下线
$offlineModel = new Line();
$offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]);
}
}
... ...
... ... @@ -3,8 +3,6 @@
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\off\Line;
use think\Validate;
/**
* 卖废品接口**
... ... @@ -51,11 +49,8 @@ class Sell extends Api
}
//如果携带分享uid,则按照积分增加
if(!empty($sell_data['share_uid'])){
$score = config('site.share_sell');
$user = new User();
$user->where(['id'=>$sell_data['share_uid'],'status'=>'normal'])->setInc('score', $score);
$offlineModel = new Line();
$offlineModel->create(['uid'=>$sell_data['share_uid'],'s_score'=>$score,'off_uid'=>$this->user_id]);
$person = new Person();
$person->getScore($sell_data['share_uid'],'share_sell');
}
$sell = new \app\admin\model\Sell();
$data['uid'] = $this->user_id;
... ...
... ... @@ -10,7 +10,6 @@ use Yansongda\Pay\Pay;
use app\admin\model\Account;
use app\admin\model\User;
use app\admin\model\Message;
use app\admin\model\off\Line;
use think\Log;
use fast\Http;
use think\Validate;
... ... @@ -144,13 +143,8 @@ class Wxpay extends Api
//如果携带分享uid,则按照积分增加
if(!empty($share_uid)){
$score = config('site.share_purchase');
//给分享用户增加积分
$user = new User();
$user->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
//成为分享用户的下线
$offlineModel = new Line();
$offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]);
$person = new Person();
$person->getScore($share_uid,'share_purchase');
}
}
} catch (Exception $e) {
... ...
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'message/index',
add_url: 'message/add',
edit_url: 'message/edit',
del_url: 'message/del',
multi_url: 'message/multi',
table: 'message',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'souce', title: __('Souce'), formatter: Table.api.formatter.normal,searchList: {0: __('System'), 1: __('AdminPush')}},
{field: 'title', title: __('Title')},
// {field: 'description', title: __('Description')},
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
},
add: function () {
Controller.api.bindevent();
},
edit: function () {
Controller.api.bindevent();
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
\ No newline at end of file