初始上传
This commit is contained in:
102
app/model/BaseModel.php
Executable file
102
app/model/BaseModel.php
Executable file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use \think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 基础model
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class BaseModel
|
||||
{
|
||||
public $lang;
|
||||
|
||||
/**
|
||||
* 操作成功返回值函数
|
||||
* @param string $data
|
||||
* @param string $code_var
|
||||
* @return array
|
||||
*/
|
||||
public function success($data = '', $code_var = 'SUCCESS')
|
||||
{
|
||||
$lang_array = $this->getLang();
|
||||
$lang_var = $lang_array[$code_var] ?? $code_var;
|
||||
|
||||
if ($code_var == 'SUCCESS') {
|
||||
$code_var = 0;
|
||||
} else {
|
||||
$code_array = array_keys($lang_array);
|
||||
$code_index = array_search($code_var, $code_array);
|
||||
if ($code_index != false) {
|
||||
$code_var = 10000 + $code_index;
|
||||
}
|
||||
}
|
||||
|
||||
return success($code_var, $lang_var, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作失败返回值函数
|
||||
* @param string $data
|
||||
* @param string $code_var
|
||||
* @return array
|
||||
*/
|
||||
public function error($data = '', $code_var = 'FAIL')
|
||||
{
|
||||
|
||||
$lang_array = $this->getLang();
|
||||
|
||||
if (isset($lang_array[$code_var])) {
|
||||
$lang_var = $lang_array[$code_var];
|
||||
} else {
|
||||
$lang_var = $code_var;
|
||||
$code_var = 'FAIL';
|
||||
}
|
||||
$code_array = array_keys($lang_array);
|
||||
$code_index = array_search($code_var, $code_array);
|
||||
if ($code_index != false) {
|
||||
$code_var = -10000 - $code_index;
|
||||
}
|
||||
return error($code_var, $lang_var, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取语言包数组
|
||||
* @return Ambigous <multitype:, unknown>
|
||||
*/
|
||||
public function getLang()
|
||||
{
|
||||
$default_lang = config("lang.default_lang");
|
||||
$cache_common = Cache::get("lang_app/lang/" . $default_lang . '/model.php');
|
||||
if (empty($cache_common)) {
|
||||
$cache_common = include 'app/lang/' . $default_lang . '/model.php';
|
||||
Cache::tag("lang")->set("lang_app/lang/" . $default_lang, $cache_common);
|
||||
}
|
||||
$lang_path = $this->lang ?? '';
|
||||
if (!empty($lang_path)) {
|
||||
$cache_path = Cache::get("lang_" . $lang_path . "/" . $default_lang . '/model.php');
|
||||
if (empty($cache_path)) {
|
||||
$cache_path = include $lang_path . "/" . $default_lang . '/model.php';
|
||||
Cache::tag("lang")->set("lang_" . $lang_path . "/" . $default_lang, $cache_path);
|
||||
}
|
||||
$lang = array_merge($cache_common, $cache_path);
|
||||
} else {
|
||||
$lang = $cache_common;
|
||||
}
|
||||
return $lang;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
859
app/model/Model.php
Executable file
859
app/model/Model.php
Executable file
@@ -0,0 +1,859 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\facade\Db;
|
||||
use think\Validate;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 模型基类
|
||||
*/
|
||||
class Model
|
||||
{
|
||||
|
||||
// 查询对象
|
||||
private static $query_obj = null;
|
||||
|
||||
protected $table = '';
|
||||
protected $connect = '';
|
||||
//验证规则
|
||||
protected $rule = [];
|
||||
//验证信息
|
||||
protected $message = [];
|
||||
//验证场景
|
||||
protected $scene = [];
|
||||
//错误信息
|
||||
protected $error;
|
||||
|
||||
protected $is_cache = 1;
|
||||
|
||||
protected $cache_prefix = 'table_cache_';
|
||||
|
||||
public function __construct($table = '', $option = [])
|
||||
{
|
||||
if ($table) {
|
||||
$this->table = $table;
|
||||
}
|
||||
$this->connect = $option['connect'] ?? 'mysql';
|
||||
$this->is_cache = $this->isCache();
|
||||
}
|
||||
|
||||
public function isCache()
|
||||
{
|
||||
|
||||
$cache_table = config("cache_table");
|
||||
if(!empty($this->table) && in_array($this->table, $cache_table) && !env('APP_DEBUG', 0))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表数据
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param number $page
|
||||
* @param array $join
|
||||
* @param string $group
|
||||
* @param string $limit
|
||||
* @param string $data
|
||||
* @return mixed
|
||||
*/
|
||||
final public function getList($condition = [], $field = true, $order = '', $alias = 'a', $join = [], $group = '', $limit = null)
|
||||
{
|
||||
if ($this->is_cache && empty($join)) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
self::$query_obj = Db::connect($this->connect)->name($this->table)->where($condition)->order($order);
|
||||
|
||||
if (!empty($join)) {
|
||||
self::$query_obj->alias($alias);
|
||||
self::$query_obj = $this->parseJoin(self::$query_obj, $join);
|
||||
}
|
||||
|
||||
if (!empty($group)) {
|
||||
self::$query_obj = self::$query_obj->group($group);
|
||||
}
|
||||
|
||||
if (!empty($limit)) {
|
||||
self::$query_obj = self::$query_obj->limit($limit);
|
||||
}
|
||||
|
||||
$result = self::$query_obj->field($field)->select()->toArray();
|
||||
|
||||
self::$query_obj->removeOption();
|
||||
if ($this->is_cache && empty($join)) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
final public function all()
|
||||
{
|
||||
if ($this->is_cache) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_';
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
$result = Db::connect($this->connect)->name($this->table)->select()->toArray();
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分页列表数据
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @param null $group
|
||||
* @param null $limit
|
||||
* @return mixed
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
final public function pageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null)
|
||||
{
|
||||
//关联查询多表无法控制不缓存,单独业务处理
|
||||
if ($this->is_cache && empty($join)) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
self::$query_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition)->order($order);
|
||||
$count_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition)->order($order);
|
||||
if (!empty($join)) {
|
||||
|
||||
$db_obj = self::$query_obj;
|
||||
self::$query_obj = $this->parseJoin($db_obj, $join);
|
||||
$count_obj = $this->parseJoin($count_obj, $join);
|
||||
}
|
||||
|
||||
if (!empty($group)) {
|
||||
self::$query_obj = self::$query_obj->group($group);
|
||||
$count_obj = $count_obj->group($group);
|
||||
}
|
||||
|
||||
if (!empty($limit)) {
|
||||
self::$query_obj = self::$query_obj->limit($limit);
|
||||
}
|
||||
|
||||
$count = $count_obj->field($field)->count();
|
||||
if($count > 0)
|
||||
{
|
||||
if ($list_rows == 0) {
|
||||
//查询全部
|
||||
$result_data = self::$query_obj->field($field)->limit($count)->page($page)->select()->toArray();
|
||||
$result[ 'page_count' ] = 1;
|
||||
} else {
|
||||
$result_data = self::$query_obj->field($field)->limit($list_rows)->page($page)->select()->toArray();
|
||||
$result[ 'page_count' ] = ceil($count / $list_rows);
|
||||
}
|
||||
}else{
|
||||
$result[ 'page_count' ] = 0;
|
||||
$result_data = [];
|
||||
}
|
||||
|
||||
$result[ 'count' ] = $count;
|
||||
$result[ 'list' ] = $result_data;
|
||||
|
||||
|
||||
self::$query_obj->removeOption();
|
||||
if ($this->is_cache && empty($join)) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分页列表数据
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @param null $group
|
||||
* @param null $limit
|
||||
* @return mixed
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
final public function rawPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null)
|
||||
{
|
||||
//关联查询多表无法控制不缓存,单独业务处理
|
||||
if ($this->is_cache && empty($join)) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($order)) {
|
||||
self::$query_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition)->order($order);
|
||||
$count_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition)->order($order);
|
||||
} else {
|
||||
self::$query_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition)->orderRaw($order);
|
||||
$count_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition)->orderRaw($order);
|
||||
}
|
||||
|
||||
if (!empty($join)) {
|
||||
$db_obj = self::$query_obj;
|
||||
self::$query_obj = $this->parseJoin($db_obj, $join);
|
||||
$count_obj = $this->parseJoin($count_obj, $join);
|
||||
}
|
||||
|
||||
if (!empty($group)) {
|
||||
self::$query_obj = self::$query_obj->group($group);
|
||||
$count_obj = $count_obj->group($group);
|
||||
}
|
||||
|
||||
if (!empty($limit)) {
|
||||
self::$query_obj = self::$query_obj->limit($limit);
|
||||
}
|
||||
|
||||
$count = $count_obj->field($field)->count();
|
||||
if($count > 0)
|
||||
{
|
||||
if ($list_rows == 0) {
|
||||
//查询全部
|
||||
$result_data = self::$query_obj->field($field)->limit($count)->page($page)->select()->toArray();
|
||||
$result[ 'page_count' ] = 1;
|
||||
} else {
|
||||
$result_data = self::$query_obj->field($field)->limit($list_rows)->page($page)->select()->toArray();
|
||||
$result[ 'page_count' ] = ceil($count / $list_rows);
|
||||
}
|
||||
}else{
|
||||
$result_data = [];
|
||||
$result[ 'page_count' ] = 0;
|
||||
}
|
||||
|
||||
$result[ 'count' ] = $count;
|
||||
$result[ 'list' ] = $result_data;
|
||||
|
||||
|
||||
self::$query_obj->removeOption();
|
||||
if ($this->is_cache && empty($join)) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单条数据
|
||||
* @param array $where
|
||||
* @param string $field
|
||||
* @param string $join
|
||||
* @param string $data
|
||||
* @return mixed
|
||||
*/
|
||||
final public function getInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
|
||||
{
|
||||
//关联查询多表无法控制不缓存,单独业务处理
|
||||
if ($this->is_cache && empty($join)) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($join)) {
|
||||
$result = Db::connect($this->connect)->name($this->table)->where($where)->field($field)->find($data);
|
||||
} else {
|
||||
$db_obj = Db::connect($this->connect)->name($this->table)->alias($alias);
|
||||
$db_obj = $this->parseJoin($db_obj, $join);
|
||||
$result = $db_obj->where($where)->field($field)->find($data);
|
||||
}
|
||||
if ($this->is_cache && empty($join)) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* join分析
|
||||
* @access protected
|
||||
* @param array $join
|
||||
* @param array $options 查询条件
|
||||
* @return string
|
||||
*/
|
||||
protected function parseJoin($db_obj, $join)
|
||||
{
|
||||
foreach ($join as $item) {
|
||||
list($table, $on, $type) = $item;
|
||||
$type = strtolower($type);
|
||||
switch ( $type ) {
|
||||
case "left":
|
||||
$db_obj = $db_obj->leftJoin($table, $on);
|
||||
break;
|
||||
case "inner":
|
||||
$db_obj = $db_obj->join($table, $on);
|
||||
break;
|
||||
case "right":
|
||||
$db_obj = $db_obj->rightjoin($table, $on);
|
||||
break;
|
||||
case "full":
|
||||
$db_obj = $db_obj->fulljoin($table, $on);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $db_obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* /**
|
||||
* 获取某个列的数组
|
||||
* @param array $where 条件
|
||||
* @param string $field 字段名 多个字段用逗号分隔
|
||||
* @param string $key 索引
|
||||
* @return array
|
||||
*/
|
||||
final public function getColumn($where = [], $field = '', $key = '')
|
||||
{
|
||||
if ($this->is_cache) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
$result = Db::connect($this->connect)->name($this->table)->where($where)->column($field, $key);
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到某个字段的值
|
||||
* @access public
|
||||
* @param array $where 条件
|
||||
* @param string $field 字段名
|
||||
* @param mixed $default 默认值
|
||||
* @param bool $force 强制转为数字类型
|
||||
* @return mixed
|
||||
*/
|
||||
final public function getValue($where = [], $field = '', $default = null, $force = false)
|
||||
{
|
||||
if ($this->is_cache) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
$result = Db::connect($this->connect)->name($this->table)->where($where)->value($field, $default, $force);
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
* @param array $data 数据
|
||||
* @param boolean $is_return_pk 返回自增主键
|
||||
*/
|
||||
final public function add($data = [], $is_return_pk = true)
|
||||
{
|
||||
|
||||
|
||||
$res = Db::connect($this->connect)->name($this->table)->insert($data, true, $is_return_pk);
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增多条数据
|
||||
* @param array $data 数据
|
||||
* @param int $limit 限制插入行数
|
||||
*/
|
||||
final public function addList($data = [], $limit = null)
|
||||
{
|
||||
|
||||
|
||||
$res = Db::connect($this->connect)->name($this->table)->insertAll($data, false, $limit);
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
* @param array $where 条件
|
||||
* @param array $data 数据
|
||||
*/
|
||||
final public function update($data = [], $where = [])
|
||||
{
|
||||
|
||||
$res = Db::connect($this->connect)->name($this->table)->where($where)->update($data);
|
||||
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
return $res;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置某个字段值
|
||||
* @param array $where 条件
|
||||
* @param string $field 字段
|
||||
* @param string $value 值
|
||||
*/
|
||||
final public function setFieldValue($where = [], $field = '', $value = '')
|
||||
{
|
||||
|
||||
$res = $this->update([ $field => $value ], $where);
|
||||
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
return $res;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据列表
|
||||
* @param array $data_list 数据
|
||||
* @param boolean $replace 是否自动识别更新和写入
|
||||
*/
|
||||
final public function setList($data_list = [], $replace = false)
|
||||
{
|
||||
|
||||
|
||||
$res = Db::connect($this->connect)->name($this->table)->saveAll($data_list, $replace);
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param array $where 条件
|
||||
*/
|
||||
final public function delete($where = [])
|
||||
{
|
||||
|
||||
$res = Db::connect($this->connect)->name($this->table)->where($where)->delete();
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
return $res;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计数据
|
||||
* @param array $where 条件
|
||||
* @param string $type 查询类型 count:统计数量|max:获取最大值|min:获取最小值|avg:获取平均值|sum:获取总和
|
||||
*/
|
||||
final public function stat($where = [], $type = 'count', $field = 'id')
|
||||
{
|
||||
if ($this->is_cache) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
$result = Db::connect($this->connect)->name($this->table)->where($where)->$type($field);
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL查询
|
||||
* @param string $sql
|
||||
* @return mixed
|
||||
*/
|
||||
final public function query($sql = '')
|
||||
{
|
||||
return Db::query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回总数
|
||||
* @param unknown $where
|
||||
*/
|
||||
final public function getCount($where = [], $field = '*', $alias = 'a', $join = null, $group = null)
|
||||
{
|
||||
if ($this->is_cache && empty($join)) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
if (empty($join)) {
|
||||
if (empty($group)) {
|
||||
$result = Db::connect($this->connect)->name($this->table)->where($where)->count($field);
|
||||
} else {
|
||||
$result = Db::connect($this->connect)->name($this->table)->group($group)->where($where)->count($field);
|
||||
}
|
||||
} else {
|
||||
$db_obj = Db::connect($this->connect)->name($this->table)->alias($alias);
|
||||
$db_obj = $this->parseJoin($db_obj, $join);
|
||||
if (empty($group)) {
|
||||
$result = $db_obj->where($where)->count($field);
|
||||
} else {
|
||||
$result = $db_obj->group($group)->where($where)->count($field);
|
||||
}
|
||||
}
|
||||
if ($this->is_cache && empty($join)) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回总数
|
||||
* @param unknown $where
|
||||
*/
|
||||
final public function getSum($where = [], $field = '', $alias = 'a', $join = null)
|
||||
{
|
||||
if ($this->is_cache && empty($join)) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($join)) {
|
||||
$result = Db::connect($this->connect)->name($this->table)->where($where)->sum($field);
|
||||
} else {
|
||||
$db_obj = Db::connect($this->connect)->name($this->table)->alias($alias);
|
||||
$db_obj = $this->parseJoin($db_obj, $join);
|
||||
$result = $db_obj->where($where)->sum($field);
|
||||
}
|
||||
if ($this->is_cache && empty($join)) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL执行,注意只能处理查询问题,如果执行修改需要手动删除缓存
|
||||
*/
|
||||
final public function execute($sql = '')
|
||||
{
|
||||
return Db::execute($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询第一条数据
|
||||
* @param array $condition
|
||||
*/
|
||||
final function getFirstData($condition, $field = '*', $order = "")
|
||||
{
|
||||
if ($this->is_cache) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
$data = Db::connect($this->connect)->name($this->table)->where($condition)->order($order)->field($field)->find();
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询第一条数据
|
||||
* @param array $condition
|
||||
*/
|
||||
final function getFirstDataView($condition, $field = '*', $order = "", $alias = 'a', $join = [], $group = null)
|
||||
{
|
||||
if ($this->is_cache && empty($join)) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
self::$query_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition)->order($order)->field($field);
|
||||
if (!empty($join)) {
|
||||
$db_obj = self::$query_obj;
|
||||
self::$query_obj = $this->parseJoin($db_obj, $join);
|
||||
}
|
||||
|
||||
if (!empty($group)) {
|
||||
self::$query_obj = self::$query_obj->group($group);
|
||||
}
|
||||
$data = self::$query_obj->find();
|
||||
if ($this->is_cache && empty($join)) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证
|
||||
* @param array $data
|
||||
* @param string $scene_name
|
||||
* @return array[$code, $error]
|
||||
*/
|
||||
public function fieldValidate($data, $scene_name = '')
|
||||
{
|
||||
$validate = new Validate($this->rule, $this->message);
|
||||
|
||||
if (empty($scene_name)) {
|
||||
$validate_result = $validate->batch(false)->check($data);
|
||||
} else {
|
||||
$validate->scene($this->scene);
|
||||
$validate_result = $validate->scene($scene_name)->batch(false)->check($data);
|
||||
}
|
||||
|
||||
return $validate_result ? [ true, '' ] : [ false, $validate->getError() ];
|
||||
}
|
||||
|
||||
/**
|
||||
* 事物开启
|
||||
*/
|
||||
final public function startTrans()
|
||||
{
|
||||
|
||||
return Db::startTrans();
|
||||
}
|
||||
|
||||
/**
|
||||
* 事物提交
|
||||
*/
|
||||
final public function commit()
|
||||
{
|
||||
|
||||
return Db::commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 事物回滚
|
||||
*/
|
||||
final public function rollback()
|
||||
{
|
||||
// Cache::clear();
|
||||
return Db::rollback();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
*/
|
||||
final public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增数据
|
||||
* @param array $where
|
||||
* @param $field
|
||||
* @param int $num
|
||||
* @return int
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
final public function setInc($where, $field, $num = 1)
|
||||
{
|
||||
|
||||
$res = Db::connect($this->connect)->name($this->table)->where($where)->inc($field, $num)->update();
|
||||
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减数据
|
||||
* @param $where
|
||||
* @param $field
|
||||
* @param int $num
|
||||
* @return int
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
final public function setDec($where, $field, $num = 1)
|
||||
{
|
||||
|
||||
$res = Db::connect($this->connect)->name($this->table)->where($where)->dec($field, $num)->update();
|
||||
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->clear();
|
||||
}
|
||||
|
||||
return $res;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最大值
|
||||
* @param array $where
|
||||
* @param $field
|
||||
* @return mixed
|
||||
*/
|
||||
final public function getMax($where, $field)
|
||||
{
|
||||
if ($this->is_cache) {
|
||||
$cache_name = $this->cache_prefix. $this->connect . $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name);
|
||||
if (!empty($cache)) {
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$data = Db::connect($this->connect)->name($this->table)->where($where)->max($field);
|
||||
if ($this->is_cache) {
|
||||
Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分页列表数据 只是单纯的实现部分功能 其他使用还是用pageList吧
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @param null $group
|
||||
* @param null $limit
|
||||
* @return array
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
final public function Lists($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null)
|
||||
{
|
||||
self::$query_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition);
|
||||
$count_obj = Db::connect($this->connect)->name($this->table)->alias($alias)->where($condition);
|
||||
if (!empty($join)) {
|
||||
$db_obj = self::$query_obj;
|
||||
self::$query_obj = $this->parseJoin($db_obj, $join);
|
||||
$count_obj = $this->parseJoin($count_obj, $join);
|
||||
}
|
||||
|
||||
if (!empty($group)) {
|
||||
self::$query_obj = self::$query_obj->group($group);
|
||||
$count_obj = $count_obj->group($group);
|
||||
}
|
||||
|
||||
if (!empty($limit)) {
|
||||
self::$query_obj = self::$query_obj->limit($limit);
|
||||
}
|
||||
|
||||
$count = $count_obj->count();
|
||||
if($count > 0)
|
||||
{
|
||||
if ($list_rows == 0) {
|
||||
//查询全部
|
||||
$result_data = self::$query_obj->field($field)->order($order)->limit($count)->page($page)->select()->toArray();
|
||||
$result[ 'page_count' ] = 1;
|
||||
} else {
|
||||
$result_data = self::$query_obj->field($field)->order($order)->limit($list_rows)->page($page)->select()->toArray();
|
||||
$result[ 'page_count' ] = ceil($count / $list_rows);
|
||||
}
|
||||
}else{
|
||||
$result[ 'page_count' ] = 0;
|
||||
$result_data = [];
|
||||
}
|
||||
|
||||
$result[ 'count' ] = $count;
|
||||
$result[ 'list' ] = $result_data;
|
||||
|
||||
self::$query_obj->removeOption();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不读取缓存--获取单条数据
|
||||
* @param array $where
|
||||
* @param string $field
|
||||
* @param string $join
|
||||
* @param string $data
|
||||
* @return mixed
|
||||
*/
|
||||
final public function getInfoTo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
|
||||
{
|
||||
if (empty($join)) {
|
||||
$result = Db::connect($this->connect)->name($this->table)->where($where)->field($field)->find($data);
|
||||
} else {
|
||||
$db_obj = Db::connect($this->connect)->name($this->table)->alias($alias);
|
||||
$db_obj = $this->parseJoin($db_obj, $join);
|
||||
$result = $db_obj->where($where)->field($field)->find($data);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setIsCache($is_cache = 1){
|
||||
$this->is_cache = $is_cache;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
213
app/model/account/Point.php
Executable file
213
app/model/account/Point.php
Executable file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\account;
|
||||
|
||||
use app\model\account\Point as PointModel;
|
||||
use app\model\BaseModel;
|
||||
use app\model\member\MemberAccount;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\system\Cron;
|
||||
|
||||
/**
|
||||
* 积分管理
|
||||
*/
|
||||
class Point extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 积分清零
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function pointClear($params)
|
||||
{
|
||||
$site_id = $params[ 'site_id' ] ?? 0;
|
||||
try {
|
||||
set_time_limit(0);
|
||||
$condition = array (
|
||||
[ 'point', '>', 0 ]
|
||||
);
|
||||
if ($site_id > 0) {
|
||||
$condition[] = [ 'site_id', '=', $site_id ];
|
||||
}
|
||||
$list = model('member')->getList($condition, 'member_id,site_id, point');
|
||||
if (empty($list)) {
|
||||
return $this->success();
|
||||
}
|
||||
$member_account_model = new MemberAccount();
|
||||
$remark = empty($params[ 'remark' ]) ? '积分清零' : $params[ 'remark' ];
|
||||
foreach ($list as $k => $val) {
|
||||
$member_account_model->addMemberAccount($val[ 'site_id' ], $val[ 'member_id' ], "point", -$val[ 'point' ], 'point_set_zero', 0, $remark);
|
||||
}
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 积分重置
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function pointReset($params)
|
||||
{
|
||||
$site_id = $params[ 'site_id' ];
|
||||
//会员积分清零
|
||||
$condition = array (
|
||||
[ 'point', '<>', 0 ]
|
||||
);
|
||||
$common_condition = [];
|
||||
if ($site_id > 0) {
|
||||
$common_condition[] = [ 'site_id', '=', $site_id ];
|
||||
}
|
||||
$member_data = array (
|
||||
'point' => 0
|
||||
);
|
||||
model('member')->update($member_data, array_merge($condition, $common_condition));
|
||||
//会员积分记录清空删除
|
||||
$member_account_condition = array (
|
||||
[ 'account_type', '=', 'point' ]
|
||||
);
|
||||
model('member_account')->delete(array_merge($member_account_condition, $common_condition));
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 积分任务
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getPointTaskConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'POINT_TASK_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'status' => 0,//1开启 0关闭
|
||||
'type' => 'clear',//任务类型 clear 清零 reset 重置
|
||||
'time' => '1/1',//1每年1月1日
|
||||
'time_type' => 1,//时间类型 1每年
|
||||
];
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员配置
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setPointTaskConfig($data, $site_id)
|
||||
{
|
||||
//处理积分任务时间
|
||||
$res = $this->dealWithPointTaskCronTime($data);
|
||||
if($res['code'] < 0) return $res;
|
||||
$data = $res['data'];
|
||||
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '积分配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'POINT_TASK_CONFIG' ] ]);
|
||||
|
||||
//更新定时任务
|
||||
$this->updatePointTaskCron($site_id);
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function dealWithPointTaskCronTime($value)
|
||||
{
|
||||
if($value['status'] == 1){
|
||||
switch($value['time_type']){
|
||||
case 1:
|
||||
$year = date('Y');
|
||||
list($month,$day) = explode('/', $value['time']);
|
||||
$this_year_time = strtotime("{$year}-{$month}-{$day}");
|
||||
if($this_year_time > time()){
|
||||
$cron_time = $this_year_time;
|
||||
}else{
|
||||
$cron_time = strtotime("+1 year", $this_year_time);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return $this->error(null, '时间类型有误');
|
||||
}
|
||||
$value['cron_time'] = $cron_time;
|
||||
}
|
||||
return $this->success($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新积分定时任务
|
||||
* @param $site_id
|
||||
* @param $app_module
|
||||
* @return void
|
||||
*/
|
||||
public function updatePointTaskCron($site_id)
|
||||
{
|
||||
$value = $this->getPointTaskConfig($site_id)['data']['value'];
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([['event', '=', 'CronPointTask'], ['relate_id', '=', $site_id]]);
|
||||
if($value['status'] == 1){
|
||||
switch($value['time_type']){
|
||||
case 1:
|
||||
$year = date('Y');
|
||||
list($month,$day) = explode('/', $value['time']);
|
||||
$this_year_time = strtotime("{$year}-{$month}-{$day}");
|
||||
if($this_year_time > time()){
|
||||
$cron_time = $this_year_time;
|
||||
}else{
|
||||
$cron_time = strtotime("+1 year", $this_year_time);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(!empty($cron_time)){
|
||||
$cron->addCron(1, 0, "定时积分任务", "CronPointTask", $cron_time, $site_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行积分定时任务
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function execPointTaskCron($site_id)
|
||||
{
|
||||
$point_model = new PointModel();
|
||||
$value = $this->getPointTaskConfig($site_id)['data']['value'];
|
||||
$res = $this->success();
|
||||
if($value['status'] == 1){
|
||||
switch($value['type']){
|
||||
case 'clear':
|
||||
$res = $point_model->pointClear([
|
||||
'site_id' => $site_id,
|
||||
'remark' => '积分定时清零',
|
||||
]);
|
||||
break;
|
||||
case 'reset':
|
||||
$res = $point_model->pointReset([
|
||||
'site_id' => $site_id,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($res['code'] < 0) return $res;
|
||||
|
||||
//重设配置
|
||||
$res = $this->setPointTaskConfig($value, $site_id);
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
221
app/model/article/Article.php
Executable file
221
app/model/article/Article.php
Executable file
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\article;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 文章
|
||||
*/
|
||||
class Article extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加文章
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addArticle($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
|
||||
model('article')->startTrans();
|
||||
try {
|
||||
//添加文章
|
||||
model('article')->add($data);
|
||||
//更新分组文章数等信息
|
||||
if ($data[ 'status' ] == 1) {
|
||||
model('article_category')->setInc([ [ 'category_id', '=', $data[ 'category_id' ] ] ], 'article_num');
|
||||
} else {
|
||||
model('article_category')->setInc([ [ 'category_id', '=', $data[ 'category_id' ] ] ], 'article_num');
|
||||
}
|
||||
model('article')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('article')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑文章
|
||||
* @param $condition
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editArticle($data)
|
||||
{
|
||||
$data[ 'update_time' ] = time();
|
||||
|
||||
$info = model('article')->getInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'article_id', '=', $data[ 'article_id' ] ] ], 'category_id');
|
||||
$category_id = $info[ 'category_id' ] ?? 0;
|
||||
model('article')->startTrans();
|
||||
try {
|
||||
//添加文章
|
||||
model('article')->update($data, [ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'article_id', '=', $data[ 'article_id' ] ] ]);
|
||||
|
||||
//减掉原文章类的文章数量
|
||||
if ($category_id != $data[ 'category_id' ]) {
|
||||
//新分类增加
|
||||
model('article_category')->setInc([ [ 'category_id', '=', $data[ 'category_id' ] ] ], 'article_num');
|
||||
//旧分类减少
|
||||
model('article_category')->setDec([ [ 'category_id', '=', $category_id ] ], 'article_num');
|
||||
}
|
||||
|
||||
model('article')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('article')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文章
|
||||
* @param $article_id
|
||||
* @param $site_id
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function deleteArticle($condition)
|
||||
{
|
||||
//文章数
|
||||
$article_info = model('article')->getInfo($condition, 'category_id,status');
|
||||
if (empty($article_info)) {
|
||||
return $this->success('', '数据不合法');
|
||||
} else {
|
||||
|
||||
model('article')->startTrans();
|
||||
try {
|
||||
//删除文章
|
||||
model('article')->delete($condition);
|
||||
|
||||
//更新分组文章数等信息
|
||||
if ($article_info[ 'status' ] == 1) {
|
||||
model('article_category')->setDec([ [ 'category_id', '=', $article_info[ 'category_id' ] ] ], 'article_num');
|
||||
} else {
|
||||
model('article_category')->setDec([ [ 'category_id', '=', $article_info[ 'category_id' ] ] ], 'article_num');
|
||||
}
|
||||
model('article')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('article')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改排序
|
||||
* @param $sort
|
||||
* @param $article_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function modifyArticleSort($sort, $article_id, $site_id)
|
||||
{
|
||||
$res = model('article')->update([ 'sort' => $sort ], [ [ 'article_id', '=', $article_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getArticleInfo($condition = [], $field = '*')
|
||||
{
|
||||
$info = model("article")->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param int $type
|
||||
* @return array
|
||||
*/
|
||||
public function getArticleDetailInfo($condition = [], $field = '*', $type = 1)
|
||||
{
|
||||
$info = model('article')->getInfo($condition, $field);
|
||||
//添加浏览记录
|
||||
if ($type == 2) {
|
||||
model('article')->setInc($condition, 'read_num', 1);
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getArticleList($condition = [], $field = '*', $order = '', $limit = null, $alias = '', $join = [])
|
||||
{
|
||||
$list = model('article')->getList($condition, $field, $order, $alias, $join, '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getArticlePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'pn.sort asc', $field = 'pn.*,png.category_name')
|
||||
{
|
||||
$alias = 'pn';
|
||||
$join = [
|
||||
[
|
||||
'article_category png',
|
||||
'png.category_id = pn.category_id',
|
||||
'left'
|
||||
]
|
||||
];
|
||||
$list = model('article')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成推广二维码链接
|
||||
* @param $qrcode_param
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function urlQrcode($qrcode_param, $app_type, $site_id)
|
||||
{
|
||||
$h5_page = '/pages_tool/article/detail';
|
||||
$pc_page = '/cms/article/detail';
|
||||
$params = [
|
||||
'site_id' => $site_id,
|
||||
'data' => $qrcode_param,
|
||||
'pc_data' => [ 'id' => $qrcode_param[ 'article_id' ] ],
|
||||
'page' => $h5_page,
|
||||
'h5_path' => $h5_page . '?article_id=' . $qrcode_param[ 'article_id' ],
|
||||
'pc_page' => $pc_page,
|
||||
'pc_path' => $pc_page . '?id=' . $qrcode_param[ 'article_id' ],
|
||||
'qrcode_path' => 'upload/qrcode/article',
|
||||
'qrcode_name' => 'article_qrcode' . $qrcode_param[ 'article_id' ] . '_' . $site_id,
|
||||
'app_type' => $app_type,
|
||||
];
|
||||
|
||||
$solitaire = event('PromotionQrcode', $params);
|
||||
return $this->success($solitaire[ 0 ]);
|
||||
}
|
||||
|
||||
}
|
||||
104
app/model/article/ArticleCategory.php
Executable file
104
app/model/article/ArticleCategory.php
Executable file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\article;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 文章分类
|
||||
*/
|
||||
class ArticleCategory extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加文章分类
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addArticleCategory($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
$res = model('article_category')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑文章分类
|
||||
* @param $condition
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editArticleCategory($condition, $data)
|
||||
{
|
||||
$data[ 'update_time' ] = time();
|
||||
|
||||
$res = model('article_category')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文章分类
|
||||
* @param $article_id
|
||||
* @param $site_id
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function deleteArticleCategory($category_id, $site_id)
|
||||
{
|
||||
//文章数
|
||||
$article_count = model('article')->getCount([ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if ($article_count > 0) {
|
||||
return $this->error('', '该分类下存在文章,暂不能删除');
|
||||
} else {
|
||||
$res = model('article_category')->delete([ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章分类信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getArticleCategoryInfo($condition = [], $field = '*')
|
||||
{
|
||||
$info = model('article_category')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章分类列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getArticleCategoryList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('article_category')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getArticleCategoryPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('article_category')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
436
app/model/cart/CartCalculate.php
Executable file
436
app/model/cart/CartCalculate.php
Executable file
@@ -0,0 +1,436 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\cart;
|
||||
|
||||
use addon\coupon\model\Coupon;
|
||||
use addon\manjian\model\Manjian;
|
||||
use app\model\goods\Goods;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 购物车计算
|
||||
* Class CartCalculate
|
||||
* @package app\model\cart
|
||||
*/
|
||||
class CartCalculate extends BaseModel
|
||||
{
|
||||
|
||||
private $error = 0; //是否有错误
|
||||
private $error_msg = ''; //错误描述
|
||||
|
||||
|
||||
/**
|
||||
* 订单计算
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function calculate($data)
|
||||
{
|
||||
$data = $this->getGoodsList($data);
|
||||
$data = $this->manjianPromotion($data);
|
||||
$promotion_money = $data[ 'promotion_money' ] ?? 0;
|
||||
$order_money = $data[ 'goods_money' ] - $promotion_money;
|
||||
$data[ 'order_money' ] = $order_money;
|
||||
|
||||
$data = $this->couponCalculate($data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车商品列表信息
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGoodsList($data)
|
||||
{
|
||||
$site_id = $data[ 'site_id' ];
|
||||
$sku_ids = $data[ 'sku_ids' ] ?? [];
|
||||
$sku_id_list = array_column($sku_ids, 'sku_id');
|
||||
$member_id = $data[ 'member_id' ];
|
||||
$sku_num_list = array_column($sku_ids, 'num', 'sku_id');
|
||||
$goods_model = new Goods();
|
||||
//组装商品列表
|
||||
$field = 'ngs.sku_id, ngs.sku_name, ngs.sku_no, ngs.price, ngs.discount_price, ngs.discount_config, ngs.cost_price, ngs.stock, ngs.sku_image,ngs.goods_id,ngs.goods_name,
|
||||
ngs.site_id, ns.site_name,ngc.cart_id,ngc.num,ngs.is_consume_discount, ngs.member_price, ngs.discount_method';
|
||||
|
||||
$alias = 'ngs';
|
||||
$join = [
|
||||
[
|
||||
'site ns',
|
||||
'ngs.site_id = ns.site_id',
|
||||
'inner'
|
||||
],
|
||||
[ 'goods_cart ngc', 'ngc.sku_id = ngs.sku_id and ngc.member_id = ' . $member_id, 'inner' ]
|
||||
];
|
||||
$condition = [
|
||||
[ 'ngs.sku_id', 'in', $sku_id_list ],
|
||||
[ 'ngs.site_id', '=', $site_id ]
|
||||
];
|
||||
$store_data = $data[ 'store_data' ] ?? [];
|
||||
$store_id = $data[ 'store_id' ] ?? 0;
|
||||
// 如果是连锁运营模式
|
||||
|
||||
if (!empty($store_data) && $store_data[ 'config' ][ 'store_business' ] == 'store') {
|
||||
$join[] = [ 'store_goods_sku sgs', 'ngs.sku_id = sgs.sku_id and sgs.store_id=' . $store_id, 'left' ];
|
||||
$field .= ',IFNULL(sgs.status, 0) as store_goods_status';
|
||||
$field = str_replace('ngs.price', 'IFNULL(IF(ngs.is_unify_price = 1,ngs.price,sgs.price), ngs.price) as price', $field);
|
||||
$field = str_replace('ngs.discount_price', 'IFNULL(IF(ngs.is_unify_price = 1,ngs.discount_price,sgs.price), ngs.discount_price) as discount_price', $field);
|
||||
if ($store_data[ 'store_info' ][ 'stock_type' ] == 'store') {
|
||||
$field = str_replace('ngs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
|
||||
}
|
||||
}
|
||||
$goods_list = model('goods_sku')->getList($condition, $field, '', $alias, $join);
|
||||
if (!empty($goods_list)) {
|
||||
$goods_list = $goods_model->getGoodsListMemberPrice($goods_list, $member_id) ?? [];
|
||||
|
||||
foreach ($goods_list as $k => $v) {
|
||||
$item_num = $sku_num_list[ $v[ 'sku_id' ] ];
|
||||
$price = $v[ 'price' ];
|
||||
if (isset($v[ 'member_price' ]) && !empty($v[ 'member_price' ]) && $v[ 'member_price' ] < $v[ 'price' ]) {
|
||||
$price = $v[ 'member_price' ];
|
||||
}
|
||||
$v[ 'price' ] = $price;
|
||||
$v[ 'goods_money' ] = $price * $item_num;
|
||||
$v[ 'real_goods_money' ] = $v[ 'goods_money' ];
|
||||
$v[ 'coupon_money' ] = 0; //优惠券金额
|
||||
$v[ 'promotion_money' ] = 0; //优惠金额
|
||||
$v[ 'stock' ] = numberFormat($v[ 'stock' ]);
|
||||
if (!empty($data[ 'goods_list' ])) {
|
||||
$data[ 'goods_list' ][] = $v;
|
||||
$data[ 'goods_num' ] += $item_num;
|
||||
$data[ 'goods_money' ] += $v[ 'goods_money' ];
|
||||
} else {
|
||||
$data[ 'site_id' ] = $site_id;
|
||||
$data[ 'site_name' ] = $v[ 'site_name' ];
|
||||
$data[ 'goods_money' ] = $v[ 'goods_money' ];
|
||||
$data[ 'goods_num' ] = $item_num;
|
||||
$data[ 'goods_list' ][] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/****************************************************************************** 满减 start *****************************************************************************/
|
||||
/**
|
||||
* 满减优惠
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function manjianPromotion($data)
|
||||
{
|
||||
$site_id = $data[ 'site_id' ];
|
||||
$promotion_money = $data[ 'promotion_money' ] ?? 0;
|
||||
//先查询全部商品的满减套餐 进行中
|
||||
$manjian_model = new Manjian();
|
||||
$all_info = $manjian_model->getManjianInfo([ [ 'manjian_type', '=', 1 ], [ 'site_id', '=', $site_id ], [ 'status', '=', 1 ] ], 'manjian_name,type,goods_ids,rule_json,manjian_id')[ 'data' ] ?? [];
|
||||
$goods_list = $data[ 'goods_list' ];
|
||||
//存在全场满减(不考虑部分满减情况)
|
||||
if (!empty($all_info)) {
|
||||
$discount_array = $this->getManjianDiscountMoney($all_info, $data);
|
||||
$all_info[ 'discount_array' ] = $discount_array;
|
||||
|
||||
$discount_money = $discount_array[ 'real_discount_money' ];
|
||||
$promotion_money += $discount_money;
|
||||
if (!empty($discount_array[ 'rule' ])) {
|
||||
$goods_list = array_map(function($item) use ($all_info) {
|
||||
$item[ 'promotion' ][ 'manjian' ] = $all_info;
|
||||
return $item;
|
||||
}, $goods_list);
|
||||
}
|
||||
|
||||
} else {
|
||||
$goods_ids = array_unique(array_column($data[ 'goods_list' ], 'goods_id'));
|
||||
|
||||
$manjian_condition = array (
|
||||
[ 'goods_id', 'in', $goods_ids ],
|
||||
[ 'status', '=', 1 ]
|
||||
);
|
||||
$manjian_goods_list = $manjian_model->getManjianGoodsList($manjian_condition, 'manjian_id')[ 'data' ];
|
||||
if (!empty($manjian_goods_list)) {
|
||||
$discount_money = 0;
|
||||
$manjian_goods_list = array_column($manjian_goods_list, 'manjian_id');
|
||||
$manjian_goods_list = array_unique($manjian_goods_list); //去重
|
||||
sort($manjian_goods_list);
|
||||
$manjian_list_result = $manjian_model->getManjianList([ [ 'manjian_id', 'in', $manjian_goods_list ], [ 'status', '=', 1 ] ]);
|
||||
$manjian_list = $manjian_list_result[ 'data' ];
|
||||
foreach ($manjian_list as $k => $v) {
|
||||
$manjian_goods_ids = explode(',', $v[ 'goods_ids' ]);
|
||||
$item_goods_data = [
|
||||
'goods_money' => 0,
|
||||
'goods_num' => 0
|
||||
];
|
||||
$item_goods_list = [];
|
||||
$sku_ids = [];
|
||||
foreach ($goods_list as $goods_k => $goods_item) {
|
||||
if (in_array($goods_item[ 'goods_id' ], $manjian_goods_ids)) {
|
||||
$item_goods_data[ 'goods_money' ] += $goods_item[ 'goods_money' ];
|
||||
$item_goods_data[ 'goods_num' ] += $goods_item[ 'num' ];
|
||||
$item_goods_list[] = $goods_item;
|
||||
array_push($sku_ids, $goods_item[ 'sku_id' ]);
|
||||
$goods_list[ $goods_k ] = $goods_item;
|
||||
}
|
||||
}
|
||||
$discount_array = $this->getManjianDiscountMoney($v, $item_goods_data);
|
||||
|
||||
$manjian_list[ $k ][ 'discount_array' ] = $discount_array;
|
||||
$discount_money += $discount_array[ 'real_discount_money' ];
|
||||
|
||||
if (!empty($discount_array[ 'rule' ])) {
|
||||
$goods_list = array_map(function($item) use ($sku_ids, $v) {
|
||||
if (in_array($item[ 'sku_id' ], $sku_ids)) {
|
||||
$item[ 'promotion' ][ 'manjian' ] = $v;
|
||||
}
|
||||
return $item;
|
||||
}, $goods_list);
|
||||
}
|
||||
}
|
||||
$promotion_money += $discount_money;
|
||||
}
|
||||
}
|
||||
$data[ 'goods_list' ] = $goods_list;
|
||||
$data[ 'promotion_money' ] = $promotion_money;
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 满减优惠金额
|
||||
* @param $manjian_info
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function getManjianDiscountMoney($manjian_info, $data)
|
||||
{
|
||||
$goods_money = $data[ 'goods_money' ];
|
||||
$value = $manjian_info[ 'type' ] == 0 ? $data[ 'goods_money' ] : $data[ 'goods_num' ];
|
||||
|
||||
//阶梯计算优惠
|
||||
$rule_item = json_decode($manjian_info[ 'rule_json' ], true);
|
||||
$discount_money = 0;
|
||||
$money = 0;
|
||||
$rule = []; // 符合条件的优惠规则
|
||||
array_multisort(array_column($rule_item, 'limit'), SORT_ASC, $rule_item); //排序,根据num 排序
|
||||
foreach ($rule_item as $k => $v) {
|
||||
if ($value >= $v[ 'limit' ]) {
|
||||
$rule = $v;
|
||||
if (isset($v[ 'discount_money' ])) {
|
||||
$discount_money = $v[ 'discount_money' ];
|
||||
$money = $v[ 'limit' ];
|
||||
}
|
||||
}
|
||||
}
|
||||
$real_discount_money = min($discount_money, $goods_money);
|
||||
return [ 'discount_money' => $discount_money, 'money' => $money, 'real_discount_money' => $real_discount_money, 'rule' => $rule ];
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理商品满减
|
||||
* @param $goods_list
|
||||
* @param $goods_money
|
||||
* @param $discount_money
|
||||
* @param bool $is_free_shipping
|
||||
* @param array $sku_ids
|
||||
* @return mixed
|
||||
*/
|
||||
public function distributionGoodsDemiscount($goods_list, $goods_money, $discount_money, $is_free_shipping = false, $sku_ids = [])
|
||||
{
|
||||
$temp_discount_money = $discount_money;
|
||||
$last_key = count($goods_list) - 1;
|
||||
foreach ($goods_list as $k => $v) {
|
||||
if ($last_key != $k) {
|
||||
$item_discount_money = round(floor($v[ 'goods_money' ] / $goods_money * $discount_money * 100) / 100, 2);
|
||||
} else {
|
||||
$item_discount_money = $temp_discount_money;
|
||||
}
|
||||
$temp_discount_money -= $item_discount_money;
|
||||
$goods_list[ $k ][ 'promotion_money' ] = $item_discount_money;
|
||||
$real_goods_money = $v[ 'real_goods_money' ] - $item_discount_money;
|
||||
$real_goods_money = max($real_goods_money, 0);
|
||||
$goods_list[ $k ][ 'real_goods_money' ] = $real_goods_money; //真实订单项金额
|
||||
// 满减送包邮
|
||||
if ($is_free_shipping) {
|
||||
if (empty($sku_ids) || in_array($v[ 'sku_id' ], $sku_ids)) {
|
||||
$goods_list[ $k ][ 'is_free_shipping' ] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $goods_list;
|
||||
}
|
||||
|
||||
/****************************************************************************** 满减 end *****************************************************************************/
|
||||
/****************************************************************************** 订单优惠券 start *****************************************************************************/
|
||||
|
||||
/**
|
||||
* 查询可用优惠券
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function couponCalculate($data)
|
||||
{
|
||||
$site_id = $data[ 'site_id' ];
|
||||
$member_id = $data[ 'member_id' ];
|
||||
$goods_money = $data[ 'goods_money' ];//商品总额
|
||||
$coupon_list = [];
|
||||
$goods_list = $data[ 'goods_list' ];
|
||||
|
||||
// 先查询全场通用的优惠券
|
||||
$member_coupon_model = new Coupon();
|
||||
$condition = array (
|
||||
[ 'member_id', '=', $member_id ],
|
||||
[ 'state', '=', 1 ],
|
||||
[ 'site_id', '=', $site_id ]
|
||||
);
|
||||
$member_coupon_model = new Coupon();
|
||||
$member_coupon_list = $member_coupon_model->getCouponList($condition)[ 'data' ];
|
||||
$coupon_array = [];
|
||||
foreach ($member_coupon_list as $k => $v) {
|
||||
//全场优惠券
|
||||
if ($v[ 'goods_type' ] == 1) {
|
||||
//检测门槛
|
||||
if ($v[ 'at_least' ] <= $goods_money) {
|
||||
$coupon_array[] = $v;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
//指定商品可用优惠券
|
||||
if ($v[ 'goods_type' ] == 2) {
|
||||
$coupon_goods_array = explode(",", $v[ 'goods_ids' ]);
|
||||
$least_money = 0;
|
||||
foreach ($goods_list as $k_goods => $v_goods) {
|
||||
if (in_array($v_goods[ 'goods_id' ], $coupon_goods_array)) {
|
||||
$least_money += $v_goods[ 'goods_money' ];
|
||||
}
|
||||
}
|
||||
if ($v[ 'at_least' ] <= $least_money) {
|
||||
$coupon_array[] = $v;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ($v[ 'goods_type' ] == 3) {
|
||||
$coupon_goods_array = explode(",", $v[ 'goods_ids' ]);
|
||||
$least_money = 0;
|
||||
foreach ($goods_list as $k_goods => $v_goods) {
|
||||
if (!in_array($v_goods[ 'goods_id' ], $coupon_goods_array)) {
|
||||
$least_money += $v_goods[ 'goods_money' ];
|
||||
}
|
||||
}
|
||||
if ($v[ 'at_least' ] <= $least_money) {
|
||||
$coupon_array[] = $v;
|
||||
}
|
||||
// continue;
|
||||
}
|
||||
|
||||
}
|
||||
//已领取的优惠券id
|
||||
$ed_coupon_ids = array_column($coupon_array, 'coupon_type_id');
|
||||
|
||||
$ing_coupon_condition = array (
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ]
|
||||
);
|
||||
if (!empty($ed_coupon_ids)) {
|
||||
$ing_coupon_condition[] = [ 'coupon_type_id', 'not in', $ed_coupon_ids ];
|
||||
}
|
||||
|
||||
$ing_coupon_list = $member_coupon_model->getCouponTypeList($ing_coupon_condition)[ 'data' ] ?? [];
|
||||
if (!empty($ing_coupon_list)) {
|
||||
foreach ($ing_coupon_list as $item_k => $item_v) {
|
||||
$goods_type = $item_v[ 'goods_type' ];
|
||||
$item_goods_money = 0;
|
||||
if ($goods_type == 1) {
|
||||
//全局支持优惠券
|
||||
$item_goods_money = $goods_money;
|
||||
} elseif ($goods_type == 2) {
|
||||
// 指定参与商品
|
||||
$item_goods_ids = explode(',', $item_v[ 'goods_ids' ]);
|
||||
$item_goods_money = 0;
|
||||
foreach ($goods_list as $goods_k => $goods_v) {
|
||||
if (in_array($goods_v[ 'goods_id' ], $item_goods_ids)) {
|
||||
$item_goods_money += $goods_v[ 'goods_money' ];
|
||||
}
|
||||
}
|
||||
|
||||
} elseif ($goods_type == 3) {
|
||||
// continue;
|
||||
// 指定不参与商品的优惠券
|
||||
$item_goods_ids = explode(',', $item_v[ 'goods_ids' ]);
|
||||
// $item_goods_ids = array_diff($goods_ids, $item_goods_ids);
|
||||
$item_goods_money = 0;
|
||||
foreach ($goods_list as $goods_k => $goods_v) {
|
||||
if (!in_array($goods_v[ 'goods_id' ], $item_goods_ids)) {
|
||||
$item_goods_money += $goods_v[ 'goods_money' ];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if ($item_goods_money >= $item_v[ 'at_least' ]) {
|
||||
$check_result = $member_coupon_model->checkMemberReceiveCoupon([ 'site_id' => $site_id, 'member_id' => $member_id, 'coupon_type_info' => $item_v ]);
|
||||
if ($check_result[ 'code' ] >= 0) {//只有还可领取的优惠券才可以
|
||||
//核验会员是否还可以领取某张优惠券
|
||||
$item_v[ 'receive_type' ] = 'wait';
|
||||
$item_v[ 'coupon_goods_money' ] = $item_goods_money;
|
||||
$coupon_list[] = $this->getCouponPromotionMoney($item_v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//增加查询可以领取的优惠券
|
||||
$max_coupon_money = 0;
|
||||
|
||||
foreach ($coupon_list as $k => $v) {
|
||||
$item_coupon_money = $v[ 'coupon_money' ] ?? 0;//需修改
|
||||
if ($item_coupon_money > $max_coupon_money) {
|
||||
$max_coupon_money = $v[ 'coupon_money' ];
|
||||
$coupon_info = $v;
|
||||
}
|
||||
}
|
||||
$coupon_money = $coupon_info[ 'coupon_money' ] ?? 0;
|
||||
if ($coupon_money > 0) {
|
||||
$data[ 'coupon_info' ] = $coupon_info;
|
||||
if ($coupon_money > $data[ 'order_money' ]) {
|
||||
$coupon_money = $data[ 'order_money' ];
|
||||
}
|
||||
$data[ 'order_money' ] -= $coupon_money;
|
||||
$data[ 'coupon_money' ] = $coupon_money;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券优惠金额
|
||||
* @param $coupon_info
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCouponPromotionMoney($coupon_info)
|
||||
{
|
||||
$coupon_goods_money = $coupon_info[ 'coupon_goods_money' ];//优惠券支持当前商品的总金额
|
||||
$coupon_money = 0;
|
||||
if ($coupon_info[ 'type' ] == 'reward') { //满减优惠券
|
||||
$coupon_money = min($coupon_info['money'], $coupon_goods_money);
|
||||
} else if ($coupon_info[ 'type' ] == 'divideticket') { //瓜分优惠券
|
||||
$coupon_money = min($coupon_info['money'], $coupon_goods_money);
|
||||
} else if ($coupon_info[ 'type' ] == 'discount') { //折扣优惠券
|
||||
//计算折扣优惠金额
|
||||
$coupon_money = $coupon_goods_money * ( 10 - $coupon_info[ 'discount' ] ) / 10;
|
||||
$coupon_money = $coupon_money > $coupon_info[ 'discount_limit' ] && $coupon_info[ 'discount_limit' ] != 0 ? $coupon_info[ 'discount_limit' ] : $coupon_money;
|
||||
$coupon_money = min($coupon_money, $coupon_goods_money);
|
||||
$coupon_money = round(floor($coupon_money * 100) / 100, 2);
|
||||
}
|
||||
$coupon_info[ 'coupon_money' ] = $coupon_money;
|
||||
return $coupon_info;
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************** 订单优惠券 end *****************************************************************************/
|
||||
|
||||
}
|
||||
153
app/model/diy/Category.php
Executable file
153
app/model/diy/Category.php
Executable file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\diy;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 自定义模板分类
|
||||
*/
|
||||
class Category extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加自定义模板分类
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addCategory($data)
|
||||
{
|
||||
$count = model("diy_template_category")->getCount([ [ 'name', '=', $data[ 'name' ] ], [ 'pid', '=', $data[ 'pid' ] ] ]);
|
||||
if ($count > 0) {
|
||||
return $this->error('', '模板分类名称已存在');
|
||||
}
|
||||
$data[ 'create_time' ] = time();
|
||||
$data[ 'level' ] = $data[ 'pid' ] ? 2 : 1;
|
||||
$res = model("diy_template_category")->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑自定义模板分类
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editCategory($data, $condition)
|
||||
{
|
||||
$data[ 'modify_time' ] = time();
|
||||
$res = model("diy_template_category")->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除自定义模板分类
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteCategory($condition)
|
||||
{
|
||||
$id = model('diy_template_category')->getValue($condition, 'category_id');
|
||||
|
||||
$child_id = model('diy_template_category')->getValue([ [ 'pid', '=', $id ] ], 'category_id');
|
||||
|
||||
if (!empty($child_id)) return $this->error('', '当前模板分类存在子模板分类,不可删除');
|
||||
|
||||
$info = model('diy_template_goods')->getInfo([ [ 'category_id', '=', $id ] ], 'category_id');
|
||||
|
||||
if (!empty($info)) return $this->error('', '当前模板分类正在使用,不可删除');
|
||||
|
||||
$res = model("diy_template_category")->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自定义模板分类信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryInfo($condition, $field = '*')
|
||||
{
|
||||
$info = model("diy_template_category")->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义模板分类列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryTree($condition = [], $order = 'create_time desc', $field = 'category_id, name, pid, level, state')
|
||||
{
|
||||
$list = model("diy_template_category")->getList($condition, $field, $order);
|
||||
$category_list = [];
|
||||
|
||||
//遍历一级商品分类
|
||||
foreach ($list as $k => $v) {
|
||||
if ($v[ 'level' ] == 1) {
|
||||
$category_list[] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
|
||||
$list = array_values($list);
|
||||
|
||||
//遍历二级商品分类
|
||||
foreach ($list as $k => $v) {
|
||||
foreach ($category_list as $ck => $cv) {
|
||||
if ($v[ 'level' ] == 2 && $cv[ 'category_id' ] == $v[ 'pid' ]) {
|
||||
$category_list[ $ck ][ 'child_list' ][] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success($category_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义模板分类列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryList($condition = [], $field = '*', $order = '')
|
||||
{
|
||||
$list = model("diy_template_category")->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义模板分类列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = '', $alias = 'a', $join = [])
|
||||
{
|
||||
$list = model("diy_template_category")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
528
app/model/diy/Template.php
Executable file
528
app/model/diy/Template.php
Executable file
@@ -0,0 +1,528 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\diy;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\web\DiyView;
|
||||
|
||||
/**
|
||||
* 模板页面类型、模板组
|
||||
*/
|
||||
class Template extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加自定义模板类型
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addTemplate($data)
|
||||
{
|
||||
$res = model('diy_template')->add($data);
|
||||
if ($res) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多个自定义模板类型
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addTemplateList($data)
|
||||
{
|
||||
$res = model('diy_template')->addList($data);
|
||||
if ($res) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改自定义模板类型
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editTemplate($data, $condition)
|
||||
{
|
||||
$res = model('diy_template')->update($data, $condition);
|
||||
if ($res) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除自定义模板类型
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteTemplate($condition)
|
||||
{
|
||||
$res = model('diy_template')->delete($condition);
|
||||
if ($res) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板页面类型数量
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateCount($condition)
|
||||
{
|
||||
$res = model('diy_template')->getCount($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板类型信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateInfo($condition = [], $field = '*')
|
||||
{
|
||||
$info = model('diy_template')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板类型列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateList($condition = [], $field = '*', $order = 'sort asc')
|
||||
{
|
||||
$list = model('diy_template')->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板类型分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplatePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('diy_template')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加模板组
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addTemplateGoods($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
$res = model('diy_template_goods')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑模板组
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editTemplateGoods($data, $condition)
|
||||
{
|
||||
$data[ 'modify_time' ] = time();
|
||||
$res = model('diy_template_goods')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除模板组
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteTemplateGoods($condition)
|
||||
{
|
||||
$goods_ids = model('diy_template_goods')->getColumn($condition, 'goods_id');
|
||||
$res = model('diy_template_goods')->delete($condition);
|
||||
if (!empty($goods_ids)) {
|
||||
$count = model('site_diy_template')->getCount([ [ 'template_goods_id', 'in', $goods_ids ] ]);
|
||||
if ($count) return $this->error('', '模板正在使用中不可删除');
|
||||
|
||||
model('diy_template_goods_item')->delete([ [ 'goods_id', 'in', $goods_ids ] ]);
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板组数量
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateGoodsCount($condition)
|
||||
{
|
||||
$res = model('diy_template_goods')->getCount($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板组
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateGoodsInfo($condition, $field = '*')
|
||||
{
|
||||
$info = model('diy_template_goods')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一条模板组
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getFirstTemplateGoods($condition, $field = '*', $order = '')
|
||||
{
|
||||
$info = model('diy_template_goods')->getFirstData($condition, $field, $order);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板组列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateGoodsList($condition = [], $field = '*', $order = '')
|
||||
{
|
||||
$res = model('diy_template_goods')->getList($condition, $field, $order);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板组列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = 'create_time desc', $alias = '', $join = [])
|
||||
{
|
||||
|
||||
$res = model('diy_template_goods')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加模板页面
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addTemplateGoodsItem($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
$res = model('diy_template_goods_item')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多个模板页面
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addTemplateGoodsItemList($data)
|
||||
{
|
||||
$res = model('diy_template_goods_item')->addList($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑模板页面
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editTemplateGoodsItem($data, $condition)
|
||||
{
|
||||
$data[ 'modify_time' ] = time();
|
||||
$res = model('diy_template_goods_item')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除模板页面
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteTemplateGoodsItem($condition)
|
||||
{
|
||||
$res = model('diy_template_goods_item')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板页面
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateGoodsItemInfo($condition, $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$info = model('diy_template_goods_item')->getInfo($condition, $field, $alias, $join);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板页面列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateGoodsItemList($condition = [], $field = '*', $order = 'create_time desc')
|
||||
{
|
||||
$list = model('diy_template_goods_item')->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板页面分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateGoodsItemPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = 'create_time desc')
|
||||
{
|
||||
$list = model('diy_template_goods_item')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用模板
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function useTemplate($params)
|
||||
{
|
||||
try {
|
||||
|
||||
model('diy_template_goods')->startTrans();
|
||||
|
||||
$template_goods_info = model('diy_template_goods')->getInfo([ [ 'goods_id', '=', $params[ 'goods_id' ] ] ], 'goods_id,name,addon_name');
|
||||
if (empty($template_goods_info)) {
|
||||
return $this->error('', '模板组不存在');
|
||||
}
|
||||
|
||||
// 添加店铺模板组关联关系
|
||||
$count = $this->getSiteDiyTemplateCount([ [ 'site_id', '=', $params[ 'site_id' ] ], [ 'template_goods_id', '=', $params[ 'goods_id' ] ] ])[ 'data' ];
|
||||
if ($count == 0) {
|
||||
$this->addSiteDiyTemplate([
|
||||
'name' => $template_goods_info[ 'name' ],
|
||||
'site_id' => $params[ 'site_id' ],
|
||||
'template_goods_id' => $template_goods_info[ 'goods_id' ],
|
||||
'addon_name' => $template_goods_info[ 'addon_name' ]
|
||||
]);
|
||||
} else {
|
||||
$this->modifySiteDiyTemplateIsDefault([
|
||||
'site_id' => $params[ 'site_id' ],
|
||||
'template_goods_id' => $template_goods_info[ 'goods_id' ]
|
||||
]);
|
||||
}
|
||||
|
||||
$diy_view_model = new DiyView();
|
||||
|
||||
// 查询模板页面列表,遍历添加到站点页面中
|
||||
$item_list = $this->getTemplateGoodsItemList([
|
||||
[ 'goods_id', '=', $params[ 'goods_id' ] ]
|
||||
], 'goods_item_id, goods_id, title, name, value, addon_name')[ 'data' ];
|
||||
if (!empty($item_list)) {
|
||||
foreach ($item_list as $k => $v) {
|
||||
|
||||
// 查询页面类型
|
||||
$template_info = $this->getTemplateInfo([
|
||||
[ 'name', '=', $v[ 'name' ] ]
|
||||
], 'name,title')[ 'data' ];
|
||||
|
||||
$type = 'DIY_PAGE';
|
||||
$type_name = '自定义页面';
|
||||
if (!empty($template_info)) {
|
||||
$type = $template_info[ 'name' ];
|
||||
$type_name = $template_info[ 'title' ];
|
||||
}
|
||||
|
||||
$site_diy_view_info = $diy_view_model->getSiteDiyViewInfo([
|
||||
[ 'site_id', '=', $params[ 'site_id' ] ],
|
||||
[ 'name', '=', $v[ 'name' ] ],
|
||||
[ 'template_id', '=', $v[ 'goods_id' ] ],
|
||||
[ 'template_item_id', '=', $v[ 'goods_item_id' ] ],
|
||||
[ 'addon_name', '=', $v[ 'addon_name' ] ],
|
||||
[ 'type', '=', $type ],
|
||||
], 'id')[ 'data' ];
|
||||
|
||||
$site_diy_data = [
|
||||
'site_id' => $params[ 'site_id' ],
|
||||
'name' => $v[ 'name' ], // 模板标识
|
||||
'title' => $v[ 'title' ], // 模板名称
|
||||
'template_id' => $v[ 'goods_id' ], // 所属模板id
|
||||
'template_item_id' => $v[ 'goods_item_id' ], // 模板页面id
|
||||
'value' => $v[ 'value' ], // 模板数据,json格式
|
||||
'addon_name' => $v[ 'addon_name' ],
|
||||
'type' => $type,
|
||||
'type_name' => $type_name,
|
||||
'is_default' => 1
|
||||
];
|
||||
|
||||
// 清除默认页面
|
||||
$diy_view_model->editSiteDiyView([ 'is_default' => 0 ], [
|
||||
[ 'site_id', '=', $params[ 'site_id' ] ],
|
||||
[ 'name', '=', $v[ 'name' ] ]
|
||||
]);
|
||||
|
||||
// 检测模板页面是否存在,有则改,无则加
|
||||
if (!empty($site_diy_view_info)) {
|
||||
// 修改相同页面的默认标识
|
||||
$site_diy_data[ 'modify_time' ] = time();
|
||||
$diy_view_model->editSiteDiyView($site_diy_data, [ [ 'id', '=', $site_diy_view_info[ 'id' ] ] ]);
|
||||
} else {
|
||||
$site_diy_data[ 'create_time' ] = time();
|
||||
$diy_view_model->addSiteDiyView($site_diy_data);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 累加模板组使用次数
|
||||
model('diy_template_goods')->setInc([ [ 'goods_id', '=', $params[ 'goods_id' ] ] ], 'use_num');
|
||||
model('diy_template_goods')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('diy_template_goods')->rollback();
|
||||
return $this->error('', 'File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage() . ',Code:' . $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加店铺关联模板组
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addSiteDiyTemplate($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
$data[ 'is_default' ] = 1; // 设置店铺默认模板
|
||||
model('site_diy_template')->update([ 'is_default' => 0 ], [ [ 'site_id', '=', $data[ 'site_id' ] ] ]);
|
||||
$res = model('site_diy_template')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改店铺关联模板组
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editSiteDiyTemplate($data, $condition)
|
||||
{
|
||||
$res = model('site_diy_template')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加店铺关联模板组
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function modifySiteDiyTemplateIsDefault($data)
|
||||
{
|
||||
model('site_diy_template')->update([ 'is_default' => 0 ], [ [ 'site_id', '=', $data[ 'site_id' ] ] ]);
|
||||
$res = model('site_diy_template')->update([ 'is_default' => 1 ], [ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'template_goods_id', '=', $data[ 'template_goods_id' ] ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除店铺关联模板组
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteSiteDiyTemplate($condition)
|
||||
{
|
||||
$res = model('site_diy_template')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询店铺关联模板组数量
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getSiteDiyTemplateCount($condition)
|
||||
{
|
||||
$res = model('site_diy_template')->getCount($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询店铺关联模板组信息
|
||||
* @param $condition
|
||||
* @param $field
|
||||
* @return array
|
||||
*/
|
||||
public function getSiteDiyTemplateInfo($condition, $field = 'id,name,template_goods_id,is_default')
|
||||
{
|
||||
$res = model('site_diy_template')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 店铺关联模板组列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getSiteDiyTemplateList($condition = [], $field = '*', $order = 'create_time desc')
|
||||
{
|
||||
$list = model('site_diy_template')->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
145
app/model/diy/Theme.php
Executable file
145
app/model/diy/Theme.php
Executable file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\diy;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 自定义模板主题风格配色
|
||||
*/
|
||||
class Theme extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加自定义模板主题风格配色
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addTheme($data)
|
||||
{
|
||||
$count = model("diy_theme")->getCount([ [ 'name', '=', $data[ 'name' ] ] ]);
|
||||
if ($count > 0) {
|
||||
return $this->error('', '主题风格已存在');
|
||||
}
|
||||
$res = model("diy_theme")->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除自定义模板主题风格配色
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteTheme($condition)
|
||||
{
|
||||
$res = model("diy_theme")->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加自定义模板主题风格配色
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addThemeList($data)
|
||||
{
|
||||
foreach ($data as $k => $v) {
|
||||
$count = model("diy_theme")->getCount([ [ 'name', '=', $v[ 'name' ] ] ]);
|
||||
if ($count > 0) {
|
||||
return $this->error('', '主题风格已存在');
|
||||
}
|
||||
}
|
||||
$res = model("diy_theme")->addList($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑自定义模板主题风格配色
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editTheme($data, $condition)
|
||||
{
|
||||
$res = model("diy_theme")->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主题风格数量
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getThemeCount($condition)
|
||||
{
|
||||
$res = model('diy_theme')->getCount($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自定义模板主题风格配色信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getThemeInfo($condition, $field = '*')
|
||||
{
|
||||
$info = model("diy_theme")->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一条主题风格
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getFirstTheme($condition, $field = '*', $order = '')
|
||||
{
|
||||
$info = model('diy_theme')->getFirstData($condition, $field, $order);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义模板主题风格配色列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getThemeList($condition = [], $field = '*', $order = '')
|
||||
{
|
||||
$list = model("diy_theme")->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义模板主题风格配色列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getThemePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = '', $alias = 'a', $join = [])
|
||||
{
|
||||
$list = model("diy_theme")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
246
app/model/express/Config.php
Executable file
246
app/model/express/Config.php
Executable file
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\store\Store;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 配送配置
|
||||
*/
|
||||
class Config extends BaseModel
|
||||
{
|
||||
|
||||
/*********************************************************************** 物流配送 start ***********************************************************************/
|
||||
/**
|
||||
* 物流配送配置
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getExpressConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'EXPRESS_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'express_name' => '快递发货'
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置物流配送配置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function setExpressConfig($data, $is_use, $site_id)
|
||||
{
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '物流配送设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'EXPRESS_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
/*********************************************************************** 物流配送 end ***********************************************************************/
|
||||
/*********************************************************************** 门店自提 start ***********************************************************************/
|
||||
/**
|
||||
* 门店自提配置
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getStoreConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'EXPRESS_STORE_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'store_name' => '门店自提'
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置门店自提配置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function setStoreConfig($data, $is_use, $site_id)
|
||||
{
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
if ($is_use == 1) {
|
||||
$store_model = new Store();
|
||||
$check_result = $store_model->checkIscanStoreTrade($site_id);
|
||||
if ($check_result[ 'code' ] < 0)
|
||||
return $check_result;
|
||||
}
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '门店自提配置设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'EXPRESS_STORE_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置门店自提开关
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setStoreIsuse($is_use, $site_id, $app_module = 'shop')
|
||||
{
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
if ($is_use == 1) {
|
||||
$store_model = new Store();
|
||||
$check_result = $store_model->checkIscanStoreTrade($site_id);
|
||||
if ($check_result[ 'code' ] < 0)
|
||||
return $check_result;
|
||||
}
|
||||
|
||||
$config = new ConfigModel();
|
||||
$res = $config->modifyConfigIsUse($is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'EXPRESS_STORE_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/*********************************************************************** 门店自提 end ***********************************************************************/
|
||||
/*********************************************************************** 外卖配送 start ***********************************************************************/
|
||||
|
||||
/**
|
||||
* 外卖配送配置
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getLocalDeliveryConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'EXPRESS_LOCAL_DELIVERY_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'local_name' => '同城配送'
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置外卖配送配置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function setLocalDeliveryConfig($data, $is_use, $site_id)
|
||||
{
|
||||
if ($site_id === '') {
|
||||
return $this->error('', '缺少必须参数站点id');
|
||||
}
|
||||
if ($is_use == 1) {
|
||||
$local_model = new Local();
|
||||
$check_result = $local_model->checkIsCanTradeLocal($site_id);
|
||||
if ($check_result[ 'code' ])
|
||||
return $check_result;
|
||||
}
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '同城配送配置设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'EXPRESS_LOCAL_DELIVERY_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
/*********************************************************************** 外卖配送 end ***********************************************************************/
|
||||
|
||||
/**
|
||||
* 外卖配送配置
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getDeliverTypeSort($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'DELIVERY_SORT_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'deliver_type' => 'local,express,store'
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function setDeliverTypeSort($data, $site_id)
|
||||
{
|
||||
if ($site_id === '') {
|
||||
return $this->error('', '缺少必须参数站点id');
|
||||
}
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '配置方式排序设置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'DELIVERY_SORT_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配送方式列表
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressTypeList($site_id)
|
||||
{
|
||||
//外卖配送
|
||||
$local = $this->getLocalDeliveryConfig($site_id)[ 'data' ][ 'value' ] ?? [];
|
||||
$store = $this->getStoreConfig($site_id)[ 'data' ][ 'value' ] ?? [];
|
||||
$express = $this->getExpressConfig($site_id)[ 'data' ][ 'value' ] ?? [];
|
||||
|
||||
return [ 'express' => $express[ 'express_name' ], 'store' => $store[ 'store_name' ], 'local' => $local[ 'local_name' ] ];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已启用的配送方式
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getEnabledExpressType($site_id)
|
||||
{
|
||||
$local = $this->getLocalDeliveryConfig($site_id)[ 'data' ];
|
||||
$store = $this->getStoreConfig($site_id)[ 'data' ];
|
||||
$express = $this->getExpressConfig($site_id)[ 'data' ];
|
||||
|
||||
$express_type = [];
|
||||
if ($express[ 'is_use' ]) {
|
||||
$express_type[ 'express' ] = [
|
||||
'name' => $express[ 'value' ][ 'express_name' ],
|
||||
'icon' => 'iconwuliu',
|
||||
'desc' => '支持' . $express[ 'value' ][ 'express_name' ] . '的商品在购买后将会通过快递的方式进行配送,可在订单中查看物流信息'
|
||||
];
|
||||
}
|
||||
if ($store[ 'is_use' ]) {
|
||||
$express_type[ 'store' ] = [
|
||||
'name' => $store[ 'value' ][ 'store_name' ],
|
||||
'icon' => 'icondianpu',
|
||||
'desc' => '支持' . $store[ 'value' ][ 'store_name' ] . '的商品在购买后用户可自行到下单时所选择的自提点进行提货'
|
||||
];
|
||||
}
|
||||
if ($local[ 'is_use' ]) {
|
||||
$express_type[ 'local' ] = [
|
||||
'name' => $local[ 'value' ][ 'local_name' ],
|
||||
'icon' => 'iconwaimaifuwu',
|
||||
'desc' => '支持' . $local[ 'value' ][ 'local_name' ] . '的商品在购买后平台将安排配送人员配送到用户指定的收货地点'
|
||||
];
|
||||
}
|
||||
return $express_type;
|
||||
}
|
||||
}
|
||||
268
app/model/express/Express.php
Executable file
268
app/model/express/Express.php
Executable file
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 物流配送
|
||||
*/
|
||||
class Express extends BaseModel
|
||||
{
|
||||
const express_type = [
|
||||
'express' => [ 'name' => 'express', 'title' => '物流配送' ],
|
||||
'store' => [ 'name' => 'store', 'title' => '门店自提' ],
|
||||
'local' => [ 'name' => 'local', 'title' => '外卖配送' ],
|
||||
];
|
||||
|
||||
/**
|
||||
* 计算费用
|
||||
* @param $shop_goods
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function calculate($param)
|
||||
{
|
||||
/** @var \app\model\order\OrderCreate $order_object */
|
||||
$order_object = $param['order_object'];
|
||||
if (empty($order_object->delivery[ 'member_address' ])) {
|
||||
return $this->error([], '请选择物流配送地址');
|
||||
}
|
||||
$site_id = $order_object->site_id;
|
||||
//模板分组
|
||||
$template_array = [];
|
||||
|
||||
foreach ($order_object->goods_list as $k => $v) {
|
||||
if ($v[ 'is_free_shipping' ] == 1) {
|
||||
continue;
|
||||
}
|
||||
if (isset($template_array[ $v[ 'shipping_template' ] ])) {
|
||||
$template_array[ $v[ 'shipping_template' ] ] = [
|
||||
'num' => $template_array[ $v[ 'shipping_template' ] ][ 'num' ] + $v[ 'num' ],
|
||||
'weight' => $template_array[ $v[ 'shipping_template' ] ][ 'weight' ] + $v[ 'weight' ] * $v[ 'num' ],
|
||||
'volume' => $template_array[ $v[ 'shipping_template' ] ][ 'volume' ] + $v[ 'volume' ] * $v[ 'num' ],
|
||||
'goods_money' => $template_array[ $v[ 'shipping_template' ] ][ 'goods_money' ] + $v[ 'goods_money' ],
|
||||
];
|
||||
} else {
|
||||
$template_array[ $v[ 'shipping_template' ] ] = [
|
||||
'num' => $v[ 'num' ],
|
||||
'weight' => $v[ 'weight' ] * $v[ 'num' ],
|
||||
'volume' => $v[ 'volume' ] * $v[ 'num' ],
|
||||
'goods_money' => $v[ 'goods_money' ]
|
||||
];
|
||||
}
|
||||
}
|
||||
$express_template = new ExpressTemplate();
|
||||
$price = 0;
|
||||
foreach ($template_array as $k_template => $v_template) {
|
||||
if ($k_template == 0) {
|
||||
//默认模板
|
||||
$template_info = $express_template->getDefaultTemplate($site_id);
|
||||
|
||||
} else {
|
||||
//如果选择的模板已经不存在(可能不存在),
|
||||
//默认模板
|
||||
$template_info = $express_template->getExpressTemplateInfo($k_template, $site_id);
|
||||
}
|
||||
|
||||
//判断模板是否配置完善
|
||||
if (empty($template_info[ 'data' ])) {
|
||||
// continue;
|
||||
return $this->error([], 'TEMPLATE_EMPTY');
|
||||
}
|
||||
|
||||
$template_info = $template_info[ 'data' ];
|
||||
$appoint_free_shipping = $template_info[ 'appoint_free_shipping' ] ?? 0;
|
||||
$is_exist_free = false;
|
||||
if ($appoint_free_shipping == 1) {
|
||||
$item_num = $v_template[ 'num' ];
|
||||
$item_goods_money = $v_template[ 'goods_money' ];
|
||||
//免邮区域模板
|
||||
$free_template_list = $template_info[ 'shipping_template_item' ];
|
||||
foreach ($free_template_list as $free_k => $free_v) {
|
||||
//判断是否有适配的区域模板
|
||||
if (strpos($free_v[ 'area_ids' ], '"' . $order_object->delivery[ 'member_address' ][ 'district_id' ] . '"') !== false) {
|
||||
$item_snum = $free_v[ 'snum' ];//条件(件数)
|
||||
$item_sprice = $free_v[ 'sprice' ];//条件 商品总额
|
||||
|
||||
if ($item_sprice <= $item_goods_money && $item_snum <= $item_num) {//满足包邮条件,免邮
|
||||
$is_exist_free = true;
|
||||
// continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$is_exist_free) {
|
||||
//开始计算
|
||||
$is_exist_template = false;
|
||||
foreach ($template_info[ 'template_item' ] as $k_item => $v_item) {
|
||||
if (strpos($v_item[ 'area_ids' ], '"' . $order_object->delivery[ 'member_address' ][ 'district_id' ] . '"') !== false) {
|
||||
$is_exist_template = true;
|
||||
//运算方式
|
||||
switch ( $template_info[ 'fee_type' ] ) {
|
||||
case 1:
|
||||
$tag = $v_template[ 'weight' ];
|
||||
break;
|
||||
case 2:
|
||||
$tag = $v_template[ 'volume' ];
|
||||
break;
|
||||
case 3:
|
||||
$tag = $v_template[ 'num' ];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
//开始计算
|
||||
if ($template_info[ 'fee_type' ] == 1 && $tag == 0) {
|
||||
$price += 0.0;
|
||||
} else {
|
||||
if ($tag <= $v_item[ 'snum' ]) {
|
||||
$price += $v_item[ 'sprice' ];
|
||||
} else {
|
||||
$ext_tag = $tag - $v_item[ 'snum' ];
|
||||
if ($v_item[ 'xnum' ] == 0) {
|
||||
$v_item[ 'xnum' ] = 1;
|
||||
}
|
||||
if (( $ext_tag * 100 ) % ( $v_item[ 'xnum' ] * 100 ) == 0) {
|
||||
$ext_data = $ext_tag / $v_item[ 'xnum' ];
|
||||
} else {
|
||||
$ext_data = floor($ext_tag / $v_item[ 'xnum' ]) + 1;
|
||||
}
|
||||
$price += $v_item[ 'sprice' ] + $ext_data * $v_item[ 'xprice' ];
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if ($is_exist_template == false) {
|
||||
return $this->error('', 'TEMPLATE_AREA_EXIST');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return $this->success([ 'delivery_fee' => $price ]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 区域是否支持配送
|
||||
* @param $area_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function isSupportDelivery($area_id, $site_id)
|
||||
{
|
||||
|
||||
$condition = array (
|
||||
[ 'ati.area_ids', 'like', '"' . $area_id . '"' ],
|
||||
[ 'et.site_id', '=', $site_id ]
|
||||
);
|
||||
|
||||
$alias = 'ati';
|
||||
$join = [
|
||||
[
|
||||
'express_template et',
|
||||
'et.template_id = ati.template_id',
|
||||
'left'
|
||||
]
|
||||
];
|
||||
$field = 'ati.template_id';
|
||||
|
||||
$list = model('express_template_item')->getList($condition, $field, '', $alias, $join);
|
||||
if (empty($list)) {
|
||||
return $this->error('', 'TEMPLATE_AREA_EXIST');
|
||||
} else {
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 积分兑换计算费用
|
||||
* @param $goods_info
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function pointExchangeCalculate($goods_info, $data)
|
||||
{
|
||||
$num = $data[ 'num' ];
|
||||
|
||||
if ($goods_info[ 'is_free_shipping' ] == 1) {
|
||||
return $this->success([ 'delivery_fee' => 0 ]);
|
||||
}
|
||||
|
||||
$template_data = [
|
||||
'num' => $num,
|
||||
'weight' => $goods_info[ 'weight' ] * $num,
|
||||
'volume' => $goods_info[ 'volume' ] * $num
|
||||
];
|
||||
|
||||
$express_template = new ExpressTemplate();
|
||||
$price = 0;
|
||||
|
||||
if ($goods_info[ 'shipping_template' ] == 0) {
|
||||
//默认模板
|
||||
$template_info = $express_template->getDefaultTemplate($data[ 'site_id' ]);
|
||||
} else {
|
||||
//默认模板
|
||||
$template_info = $express_template->getExpressTemplateInfo($goods_info[ 'shipping_template' ], $data[ 'site_id' ]);
|
||||
}
|
||||
|
||||
//判断模板是否配置完善
|
||||
if (empty($template_info[ 'data' ])) {
|
||||
return $this->error([], 'TEMPLATE_EMPTY');
|
||||
}
|
||||
|
||||
$template_info = $template_info[ 'data' ];
|
||||
//开始计算
|
||||
$is_exist_template = false;
|
||||
foreach ($template_info[ 'template_item' ] as $k_item => $v_item) {
|
||||
if (strpos($v_item[ 'area_ids' ], '"' . $data[ 'member_address' ][ 'district_id' ] . '"') !== false) {
|
||||
$is_exist_template = true;
|
||||
//运算方式
|
||||
switch ( $template_info[ 'fee_type' ] ) {
|
||||
case 1:
|
||||
$tag = $template_data[ 'weight' ];
|
||||
break;
|
||||
case 2:
|
||||
$tag = $template_data[ 'volume' ];
|
||||
break;
|
||||
case 3:
|
||||
$tag = $template_data[ 'num' ];
|
||||
break;
|
||||
}
|
||||
//开始计算
|
||||
if ($tag <= $v_item[ 'snum' ]) {
|
||||
$price += $v_item[ 'sprice' ];
|
||||
} else {
|
||||
$ext_tag = $tag - $v_item[ 'snum' ];
|
||||
if ($v_item[ 'xnum' ] == 0) {
|
||||
$v_item[ 'xnum' ] = 1;
|
||||
}
|
||||
if (( $ext_tag * 100 ) % ( $v_item[ 'xnum' ] * 100 ) == 0) {
|
||||
$ext_data = $ext_tag / $v_item[ 'xnum' ];
|
||||
} else {
|
||||
$ext_data = floor($ext_tag / $v_item[ 'xnum' ]) + 1;
|
||||
}
|
||||
$price += $v_item[ 'sprice' ] + $ext_data * $v_item[ 'xprice' ];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($is_exist_template == false) {
|
||||
return $this->error('', 'TEMPLATE_AREA_EXIST');
|
||||
}
|
||||
|
||||
return $this->success([ 'delivery_fee' => $price ]);
|
||||
}
|
||||
}
|
||||
175
app/model/express/ExpressCompany.php
Executable file
175
app/model/express/ExpressCompany.php
Executable file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 物流公司
|
||||
*/
|
||||
class ExpressCompany extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加店铺物流公司
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addExpressCompany($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
$data[ 'modify_time' ] = time();
|
||||
|
||||
$company_template = new ExpressCompanyTemplate();
|
||||
$company_info = $company_template->getExpressCompanyTemplateInfo([ [ 'company_id', '=', $data[ 'company_id' ] ] ]);
|
||||
$data[ 'company_name' ] = $company_info[ 'data' ][ 'company_name' ];
|
||||
$data[ 'logo' ] = $company_info[ 'data' ][ 'logo' ];
|
||||
$data[ 'express_no' ] = $company_info[ 'data' ][ 'express_no' ];
|
||||
$brand_id = model('express_company')->add($data);
|
||||
return $this->success($brand_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改店铺物流公司
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editExpressCompany($data, $condition)
|
||||
{
|
||||
$data[ 'modify_time' ] = time();
|
||||
$res = model('express_company')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除店铺物流公司
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteExpressCompany($condition)
|
||||
{
|
||||
$res = model('express_company')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺物流公司信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressCompanyInfo($condition, $field = 'id, site_id, company_id, express_no, content_json, background_image, font_size, width, height, create_time, modify_time, scale')
|
||||
{
|
||||
$res = model('express_company')->getInfo($condition, $field);
|
||||
if (!empty($res)) {
|
||||
if (empty($res[ 'content_json' ])) {
|
||||
$res[ 'content_json' ] = json_encode($this->getPrintItemList());
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺物流公司列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @param string $group
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressCompanyList($condition = [], $field = 'id, site_id, company_id, express_no, content_json, background_image, font_size, width, height, create_time, modify_time, scale, company_name', $order = '', $alias = '', $join = [], $group = '', $limit = null)
|
||||
{
|
||||
$list = model('express_company')->getList($condition, $field, $order, $alias, $join, $group, $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺物流公司分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressCompanyPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'id, site_id, company_id,express_no, content_json, background_image, font_size, width, height, create_time, modify_time, scale')
|
||||
{
|
||||
$list = model('express_company')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取打印项
|
||||
* @return array
|
||||
*/
|
||||
public function getPrintItemList()
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'item_name' => 'order_no',
|
||||
'item_title' => '订单编号',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_company',
|
||||
'item_title' => '发件人公司',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_name',
|
||||
'item_title' => '发件人姓名',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_address',
|
||||
'item_title' => '发件人地址',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_phone',
|
||||
'item_title' => '发件人电话',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_post_code',
|
||||
'item_title' => '发件人邮编',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_name',
|
||||
'item_title' => '收件人姓名',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_address',
|
||||
'item_title' => '收件人地址',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_phone',
|
||||
'item_title' => '收件人电话',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_post_code',
|
||||
'item_title' => '收件人邮编',
|
||||
],
|
||||
[
|
||||
'item_name' => 'logistics_number',
|
||||
'item_title' => '货到付款物流编号',
|
||||
],
|
||||
[
|
||||
'item_name' => 'collection_payment',
|
||||
'item_title' => '代收金额',
|
||||
],
|
||||
[
|
||||
'item_name' => 'remark',
|
||||
'item_title' => '备注',
|
||||
],
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
264
app/model/express/ExpressCompanyTemplate.php
Executable file
264
app/model/express/ExpressCompanyTemplate.php
Executable file
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 系统物流公司
|
||||
*/
|
||||
class ExpressCompanyTemplate extends BaseModel
|
||||
{
|
||||
|
||||
/***************************************************************** 系统物流公司start **********************************************************************/
|
||||
/**
|
||||
* 添加系统物流公司
|
||||
* @param unknown $data
|
||||
*/
|
||||
public function addExpressCompanyTemplate($data)
|
||||
{
|
||||
$id = model('express_company_template')->add($data);
|
||||
return $this->success($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多个系统物流公司
|
||||
* @param unknown $data
|
||||
*/
|
||||
public function addExpressCompanyTemplateList($data)
|
||||
{
|
||||
$id = model('express_company_template')->addList($data);
|
||||
return $this->success($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改系统物流公司
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editExpressCompanyTemplate($data)
|
||||
{
|
||||
$res = model('express_company_template')->update($data, [ [ 'company_id', '=', $data[ 'company_id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除系统物流公司
|
||||
* @param array $condition
|
||||
*/
|
||||
public function deleteExpressCompanyTemplate($condition)
|
||||
{
|
||||
$res = model('express_company_template')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取物流公司信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getExpressCompanyTemplateInfo($condition, $field = '*')
|
||||
{
|
||||
$res = model('express_company_template')->getInfo($condition, $field);
|
||||
if (!empty($res)) {
|
||||
if (empty($res[ 'content_json' ])) {
|
||||
$res[ 'content_json' ] = json_encode($this->getPrintItemList());
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取物流公司列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getExpressCompanyTemplateList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('express_company_template')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取物流公司分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getExpressCompanyTemplatePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('express_company_template')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改物流公司排序
|
||||
* @param $sort
|
||||
* @param $company_id
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function modifyExpressCompanyTemplateSort($sort, $company_id)
|
||||
{
|
||||
$res = model('express_company_template')->update([ 'sort' => $sort ], [ [ 'company_id', '=', $company_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/***************************************************************** 系统物流公司end **********************************************************************/
|
||||
|
||||
/***************************************************************** 店铺物流公司start **********************************************************************/
|
||||
|
||||
/**
|
||||
* 添加店铺物流公司
|
||||
* @param unknown $data
|
||||
*/
|
||||
public function addExpressCompanyTemplateShop($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
$data[ 'modify_time' ] = time();
|
||||
$company_info = $this->getExpressCompanyTemplateInfo([ [ 'company_id', '=', $data[ 'company_id' ] ] ]);
|
||||
$data[ 'company_name' ] = $company_info[ 'data' ][ 'company_name' ];
|
||||
$brand_id = model('express_company_template_shop')->add($data);
|
||||
return $this->success($brand_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改店铺物流公司
|
||||
* @param unknown $data
|
||||
* @return multitype:string
|
||||
*/
|
||||
public function editExpressCompanyTemplateShop($data, $condition)
|
||||
{
|
||||
$data[ 'modify_time' ] = time();
|
||||
$res = model('express_company_template_shop')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除店铺物流公司
|
||||
* @param unknown $condition
|
||||
*/
|
||||
public function deleteExpressCompanyTemplateShop($condition)
|
||||
{
|
||||
$res = model('express_company_template_shop')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺物流公司信息
|
||||
* @param unknown $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getExpressCompanyTemplateShopInfo($condition, $field = 'id, site_id, company_id, content_json, background_image, font_size, width, height, create_time, modify_time, scale')
|
||||
{
|
||||
$res = model('express_company_template_shop')->getInfo($condition, $field);
|
||||
if (!empty($res)) {
|
||||
if (empty($res[ 'content_json' ])) {
|
||||
$res[ 'content_json' ] = json_encode($this->getPrintItemList());
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺物流公司列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getExpressCompanyTemplateShopList($condition = [], $field = 'company_id as id, site_id, company_id, content_json, background_image, font_size, width, height, create_time, modify_time, scale, company_name', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('express_company_template')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺物流公司分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getExpressCompanyTemplateShopPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'id, site_id, company_id, content_json, background_image, font_size, width, height, create_time, modify_time, scale')
|
||||
{
|
||||
$list = model('express_company_template_shop')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/***************************************************************** 店铺物流公司end **********************************************************************/
|
||||
/**
|
||||
* 获取打印项
|
||||
* @return array
|
||||
*/
|
||||
public function getPrintItemList()
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'item_name' => 'order_no',
|
||||
'item_title' => '订单编号',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_company',
|
||||
'item_title' => '发件人公司',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_name',
|
||||
'item_title' => '发件人姓名',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_address',
|
||||
'item_title' => '发件人地址',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_phone',
|
||||
'item_title' => '发件人电话',
|
||||
],
|
||||
[
|
||||
'item_name' => 'sender_post_code',
|
||||
'item_title' => '发件人邮编',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_name',
|
||||
'item_title' => '收件人姓名',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_address',
|
||||
'item_title' => '收件人地址',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_phone',
|
||||
'item_title' => '收件人电话',
|
||||
],
|
||||
[
|
||||
'item_name' => 'receiver_post_code',
|
||||
'item_title' => '收件人邮编',
|
||||
],
|
||||
[
|
||||
'item_name' => 'logistics_number',
|
||||
'item_title' => '货到付款物流编号',
|
||||
],
|
||||
[
|
||||
'item_name' => 'collection_payment',
|
||||
'item_title' => '代收金额',
|
||||
],
|
||||
[
|
||||
'item_name' => 'remark',
|
||||
'item_title' => '备注',
|
||||
],
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
135
app/model/express/ExpressDeliver.php
Executable file
135
app/model/express/ExpressDeliver.php
Executable file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
|
||||
/**
|
||||
* 配送员信息
|
||||
*/
|
||||
class ExpressDeliver extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取配送员分页列表
|
||||
* @param $condition
|
||||
* @param $field
|
||||
* @param $order
|
||||
* @param $page
|
||||
* @param $page_size
|
||||
* @return array
|
||||
*/
|
||||
public function getDeliverPageLists($condition, $field, $order, $page, $page_size)
|
||||
{
|
||||
$list = model('express_deliver')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配送员列表
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getDeliverLists($condition, $field = '*')
|
||||
{
|
||||
$list = model('express_deliver')->getList($condition, $field);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加配送员
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addDeliver($data)
|
||||
{
|
||||
if (empty($data[ 'deliver_name' ])) {
|
||||
return $this->error('', '配送员姓名不能为空!');
|
||||
}
|
||||
|
||||
if (empty($data[ 'deliver_mobile' ])) {
|
||||
return $this->error('', '配送员手机号不能为空!');
|
||||
}
|
||||
$data[ 'create_time' ] = time();
|
||||
$result = model('express_deliver')->add($data);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑配送员
|
||||
* @param $data
|
||||
* @param $deliver_id
|
||||
* @return array
|
||||
*/
|
||||
public function editDeliver($data, $deliver_id)
|
||||
{
|
||||
if (empty($data[ 'deliver_name' ])) {
|
||||
return $this->error('', '配送员姓名不能为空!');
|
||||
}
|
||||
|
||||
if (empty($data[ 'deliver_mobile' ])) {
|
||||
return $this->error('', '配送员手机号不能为空!');
|
||||
}
|
||||
$data[ 'modify_time' ] = time();
|
||||
$condition = [
|
||||
[ 'deliver_id', '=', $deliver_id ],
|
||||
[ 'site_id', '=', $data[ 'site_id' ] ]
|
||||
];
|
||||
if (isset($data[ 'store_id' ])) {
|
||||
$condition[] = [ 'store_id', '=', $data[ 'store_id' ] ];
|
||||
}
|
||||
$result = model('express_deliver')->update($data, $condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除配送员
|
||||
* @param $deliver_id
|
||||
* @param $site_id
|
||||
* @param int $store_id
|
||||
* @return array
|
||||
*/
|
||||
public function deleteDeliver($deliver_id, $site_id, $store_id = 0)
|
||||
{
|
||||
$condition = [
|
||||
[ 'deliver_id', 'in', $deliver_id ],
|
||||
[ 'site_id', '=', $site_id ]
|
||||
];
|
||||
if ($store_id > 0) {
|
||||
$condition[] = [ 'store_id', '=', $store_id ];
|
||||
}
|
||||
$result = model('express_deliver')->delete($condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配送员信息
|
||||
* @param $deliver_id
|
||||
* @param $site_id
|
||||
* @param int $store_id
|
||||
* @return array
|
||||
*/
|
||||
public function getDeliverInfo($deliver_id, $site_id, $store_id = 0)
|
||||
{
|
||||
$condition = [
|
||||
[ 'deliver_id', '=', $deliver_id ],
|
||||
[ 'site_id', '=', $site_id ]
|
||||
];
|
||||
if ($store_id > 0) {
|
||||
$condition[] = [ 'store_id', '=', $store_id ];
|
||||
}
|
||||
$info = model('express_deliver')->getInfo($condition, 'deliver_name,deliver_mobile,create_time,modify_time,deliver_id');
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
}
|
||||
127
app/model/express/ExpressDelivery.php
Executable file
127
app/model/express/ExpressDelivery.php
Executable file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use addon\electronicsheet\model\ExpressElectronicsheet;
|
||||
use app\model\member\Member;
|
||||
use app\model\BaseModel;
|
||||
|
||||
|
||||
/**
|
||||
* 物流配送
|
||||
*/
|
||||
class ExpressDelivery extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 物流配送
|
||||
* @param $param
|
||||
* @return array|int|string
|
||||
*/
|
||||
public function delivery($param)
|
||||
{
|
||||
$order_id = $param[ 'order_id' ] ?? 0; // 订单id
|
||||
$order_goods_id_array = $param[ 'order_goods_id_array' ];
|
||||
$goods_id_array = $param[ 'goods_id_array' ];
|
||||
$delivery_type = $param[ 'delivery_type' ]; // 物流方式 1 物流配送 0 无需物流
|
||||
$delivery_no = $param[ 'delivery_no' ] ?? ''; // 物流单号
|
||||
$member_id = $param[ 'member_id' ];
|
||||
$site_id = $param[ 'site_id' ];
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $member_id ] ], 'nickname');
|
||||
$member_info = $member_info_result[ 'data' ];
|
||||
|
||||
if ($param[ 'type' ] == 'manual') {
|
||||
$express_company_id = $param[ 'express_company_id' ] ?? 0;
|
||||
$express_company_name = '';
|
||||
|
||||
if ($express_company_id > 0) {
|
||||
$express_company_info = model('express_company')->getInfo([ [ 'company_id', '=', $express_company_id ] ], 'company_name');
|
||||
$express_company_name = $express_company_info[ 'company_name' ];
|
||||
}
|
||||
$template_id = 0;
|
||||
$template_name = '';
|
||||
|
||||
//查询物流单号是否已存在,如果存在就合并入已存在的数据(手动发货才会合并)
|
||||
$condition = array (
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'delivery_no', '=', $delivery_no ],
|
||||
[ 'order_id', '=', $order_id ],
|
||||
[ 'delivery_type', '=', $delivery_type ],
|
||||
[ 'express_company_id', '=', $express_company_id ],
|
||||
[ 'member_id', '=', $member_id ]
|
||||
);
|
||||
$info = model('express_delivery_package')->getInfo($condition, '*');
|
||||
|
||||
if (!empty($info)) {
|
||||
$temp_order_goods_id_arr = explode(',', $info[ 'order_goods_id_array' ]);
|
||||
$temp_goods_id_arr = explode(',', $info[ 'goods_id_array' ]);
|
||||
|
||||
$order_goods_id_array = implode(',', array_unique(array_merge($temp_order_goods_id_arr, $order_goods_id_array)));
|
||||
$goods_id_array = implode(',', array_merge($temp_goods_id_arr, $goods_id_array));
|
||||
$data = array (
|
||||
'order_goods_id_array' => $order_goods_id_array,
|
||||
'goods_id_array' => $goods_id_array,
|
||||
);
|
||||
$result = model('express_delivery_package')->update($data, $condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
} else {
|
||||
$delivery_type = 1;
|
||||
//获取模板信息
|
||||
$template_model = new ExpressElectronicsheet();
|
||||
$template_info = $template_model->getExpressElectronicsheetInfo(
|
||||
[
|
||||
[ 'id', '=', $param[ 'template_id' ] ],
|
||||
[ 'site_id', '=', $site_id ]
|
||||
],
|
||||
'template_name,company_id,company_name'
|
||||
);
|
||||
$template_id = $param[ 'template_id' ];
|
||||
$template_name = $template_info[ 'data' ][ 'template_name' ];
|
||||
$express_company_id = $template_info[ 'data' ][ 'company_id' ];
|
||||
$express_company_name = $template_info[ 'data' ][ 'company_name' ];
|
||||
}
|
||||
|
||||
if ($delivery_type > 0) {
|
||||
$count = model('express_delivery_package')->getCount([ [ 'site_id', '=', $site_id ], [ 'order_id', '=', $order_id ], [ 'delivery_type', '=', $delivery_type ] ]);
|
||||
$num = $count + 1;
|
||||
$package_name = '包裹' . $num;
|
||||
} else {
|
||||
$package_name = '无需物流';
|
||||
}
|
||||
$express_company_info = model('express_company_template')->getInfo([ [ 'company_id', '=', $express_company_id ] ], 'logo');
|
||||
$express_company_image = empty($express_company_info) ? '' : $express_company_info[ 'logo' ];
|
||||
|
||||
$data = array (
|
||||
'order_id' => $order_id,
|
||||
'order_goods_id_array' => implode(',', $order_goods_id_array),
|
||||
'goods_id_array' => implode(',', $goods_id_array),
|
||||
'delivery_no' => $delivery_no,
|
||||
'site_id' => $site_id,
|
||||
'member_id' => $member_id,
|
||||
'member_name' => $member_info[ 'nickname' ] ?? '',
|
||||
'delivery_type' => $delivery_type,
|
||||
'express_company_id' => $express_company_id,
|
||||
'express_company_name' => $express_company_name,
|
||||
'package_name' => $package_name,
|
||||
'delivery_time' => time(),
|
||||
'express_company_image' => $express_company_image,
|
||||
'type' => $param[ 'type' ],
|
||||
'template_id' => $template_id,
|
||||
'template_name' => $template_name
|
||||
);
|
||||
$result = model('express_delivery_package')->add($data);
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
177
app/model/express/ExpressPackage.php
Executable file
177
app/model/express/ExpressPackage.php
Executable file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\OrderCommon;
|
||||
|
||||
/**
|
||||
* 物流配送
|
||||
*/
|
||||
class ExpressPackage extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 修改物流单号和物流公司
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editOrderExpressDeliveryPackage($data)
|
||||
{
|
||||
$order_common_model = new OrderCommon();
|
||||
//订单状态
|
||||
$order_status = model('order')->getValue([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'order_id', '=', $data[ 'order_id' ] ] ], 'order_status');
|
||||
if (empty($order_status)) {
|
||||
return $this->error('', '订单不存在');
|
||||
}
|
||||
if (!in_array($order_status, [ $order_common_model::ORDER_PAY, $order_common_model::ORDER_DELIVERY ])) {
|
||||
return $this->error('', '订单已收货或已完成');
|
||||
}
|
||||
//包裹信息
|
||||
$package_count = model('express_delivery_package')->getCount(
|
||||
[
|
||||
[ 'site_id', '=', $data[ 'site_id' ] ], [ 'order_id', '=', $data[ 'order_id' ] ], [ 'id', '=', $data[ 'package_id' ] ]
|
||||
]
|
||||
);
|
||||
if ($package_count == 0) {
|
||||
return $this->error('', '包裹信息不存在');
|
||||
}
|
||||
model('express_delivery_package')->startTrans();
|
||||
try {
|
||||
|
||||
if ($data[ 'delivery_type' ] == 0) {
|
||||
$data[ 'express_company_id' ] = 0;
|
||||
$data[ 'delivery_no' ] = '';
|
||||
$express_company_name = '';
|
||||
$express_company_image = '';
|
||||
} else {
|
||||
if ($data[ 'express_company_id' ] == '') {
|
||||
return $this->error('', '物流公司不能为空');
|
||||
}
|
||||
if ($data[ 'delivery_no' ] == '') {
|
||||
return $this->error('', '物流单号不能为空');
|
||||
}
|
||||
//获取物流公司名称
|
||||
$express_company_info = model('express_company_template')->getInfo([ [ 'company_id', '=', $data[ 'express_company_id' ] ] ], 'company_name,logo');
|
||||
$express_company_name = $express_company_info[ 'company_name' ];
|
||||
$express_company_image = $express_company_info[ 'logo' ];
|
||||
}
|
||||
|
||||
$condition = [
|
||||
[ 'site_id', '=', $data[ 'site_id' ] ], [ 'order_id', '=', $data[ 'order_id' ] ], [ 'id', '=', $data[ 'package_id' ] ]
|
||||
];
|
||||
model('express_delivery_package')->update(
|
||||
[
|
||||
'delivery_type' => $data[ 'delivery_type' ],
|
||||
'express_company_id' => $data[ 'express_company_id' ],
|
||||
'express_company_name' => $express_company_name,
|
||||
'delivery_no' => $data[ 'delivery_no' ],
|
||||
'express_company_image' => $express_company_image
|
||||
], $condition
|
||||
);
|
||||
|
||||
model('express_delivery_package')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('express_delivery_package')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取物流包裹列表
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressDeliveryPackageList($condition, $field = '*')
|
||||
{
|
||||
$list = model('express_delivery_package')->getList($condition, $field);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取包裹信息
|
||||
* @param $condition
|
||||
* @param string $mobile
|
||||
* @return mixed
|
||||
*/
|
||||
public function package($condition, $mobile = '', $save_trace = false)
|
||||
{
|
||||
$list_result = $this->getExpressDeliveryPackageList($condition);
|
||||
$list = $list_result[ 'data' ];
|
||||
$trace_model = new Trace();
|
||||
foreach ($list as $k => $v) {
|
||||
$temp_array = explode(',', $v[ 'goods_id_array' ]);
|
||||
if (!empty($temp_array)) {
|
||||
//商品名称或者图片中有逗号特殊处理
|
||||
$normal_key = 0;
|
||||
foreach($temp_array as $key=>$val){
|
||||
if(preg_match("/^\d+:\d+:/", $val)){
|
||||
$normal_key = $key;
|
||||
}else{
|
||||
$temp_array[$normal_key] .=','.$val;
|
||||
unset($temp_array[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($temp_array as $temp_k => $temp_v) {
|
||||
$temp_str = str_replace('http://', 'http//', $temp_v);
|
||||
$temp_str = str_replace('https://', 'https//', $temp_str);
|
||||
$temp_item = explode(':', $temp_str);
|
||||
$sku_image = str_replace('https//', 'https://', $temp_item[ '3' ]);
|
||||
$sku_image = str_replace('http//', 'http://', $sku_image);
|
||||
$list[ $k ][ 'goods_list' ][] = [ 'sku_name' => $temp_item[ '2' ], 'num' => $temp_item[ '1' ], 'sku_image' => $sku_image, 'sku_id' => $temp_item[ '0' ] ];
|
||||
}
|
||||
}
|
||||
if(!empty($v['trace'])){
|
||||
$list[ $k ][ 'trace' ] = json_decode($v['trace'], true);
|
||||
}else{
|
||||
$trace_list = $trace_model->trace($v[ 'delivery_no' ], $v[ 'express_company_id' ], $v[ 'site_id' ], $mobile);
|
||||
$list[ $k ][ 'trace' ] = $trace_list[ 'data' ];
|
||||
if($save_trace){
|
||||
model('express_delivery_package')->update(['trace' => json_encode($trace_list['data'], JSON_UNESCAPED_UNICODE)], [['id', '=', $v['id']]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressDeliveryPackagePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$order_list = model('order')->pageList($condition, $field, $order, $page, $page_size);
|
||||
if (!empty($order_list[ 'list' ])) {
|
||||
foreach ($order_list[ 'list' ] as $k => $v) {
|
||||
$order_goods_list = model('order_goods')->getList([
|
||||
'order_id' => $v[ 'order_id' ]
|
||||
]);
|
||||
foreach ($order_goods_list as $ck => $cv) {
|
||||
$order_goods_list[ $ck ][ 'num' ] = numberFormat($order_goods_list[ $ck ][ 'num' ]);
|
||||
}
|
||||
$order_list[ 'list' ][ $k ][ 'order_goods' ] = $order_goods_list;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success($order_list);
|
||||
}
|
||||
|
||||
}
|
||||
215
app/model/express/ExpressTemplate.php
Executable file
215
app/model/express/ExpressTemplate.php
Executable file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 运费模板
|
||||
*/
|
||||
class ExpressTemplate extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加运费模板(控制每个站点最多10条)
|
||||
* @param $data
|
||||
* @param $items
|
||||
* @param $shipping_items
|
||||
* @return array
|
||||
*/
|
||||
public function addExpressTemplate($data, $items, $shipping_items)
|
||||
{
|
||||
$count = model('express_template')->getCount([ 'site_id' => $data[ 'site_id' ] ]);
|
||||
if ($count >= 10) {
|
||||
return $this->error('', 'TEMPLATE_TO_LONG');
|
||||
}
|
||||
|
||||
if ($data[ 'is_default' ] == 1) {
|
||||
model('express_template')->update([ 'is_default' => 0 ], [ 'site_id' => $data[ 'site_id' ] ]);
|
||||
}
|
||||
if (empty($data[ 'appoint_free_shipping' ])) {
|
||||
$data[ 'shipping_surplus_area_ids' ] = '';
|
||||
$shipping_items = [];
|
||||
}
|
||||
|
||||
//模板基础信息
|
||||
$data[ 'create_time' ] = time();
|
||||
$template_id = model('express_template')->add($data);
|
||||
|
||||
//具体模板信息
|
||||
foreach ($items as $k => $v) {
|
||||
$data_item = $v;
|
||||
$data_item[ 'template_id' ] = $template_id;
|
||||
$data_item[ 'fee_type' ] = $data[ 'fee_type' ];
|
||||
if ($data_item[ 'area_ids' ] && $data_item[ 'area_names' ]) {
|
||||
model('express_template_item')->add($data_item);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($shipping_items as $k => $v) {
|
||||
$data_item = $v;
|
||||
$data_item[ 'template_id' ] = $template_id;
|
||||
if ($data_item[ 'area_ids' ] && $data_item[ 'area_names' ]) {
|
||||
model('express_template_free_shipping')->add($data_item);
|
||||
}
|
||||
}
|
||||
return $this->success($template_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改系统运费模板
|
||||
* @param $data
|
||||
* @param $items
|
||||
* @param $shipping_items
|
||||
* @return array
|
||||
*/
|
||||
public function editExpressTemplate($data, $items, $shipping_items)
|
||||
{
|
||||
//设置默认
|
||||
if ($data[ 'is_default' ] == 1) {
|
||||
model('express_template')->update([ 'is_default' => 0 ], [ 'site_id' => $data[ 'site_id' ] ]);
|
||||
}
|
||||
|
||||
$data[ 'modify_time' ] = time();
|
||||
$res = model('express_template')->update($data, [ [ 'template_id', '=', $data[ 'template_id' ] ] ]);
|
||||
if (empty($data[ 'appoint_free_shipping' ])) {
|
||||
$data[ 'shipping_surplus_area_ids' ] = '';
|
||||
$shipping_items = [];
|
||||
}
|
||||
|
||||
//具体模板信息
|
||||
model('express_template_item')->delete([ [ 'template_id', '=', $data[ 'template_id' ] ] ]);
|
||||
foreach ($items as $k => $v) {
|
||||
$data_item = $v;
|
||||
$data_item[ 'template_id' ] = $data[ 'template_id' ];
|
||||
$data_item[ 'fee_type' ] = $data[ 'fee_type' ];
|
||||
if ($data_item[ 'area_ids' ] && $data_item[ 'area_names' ]) {
|
||||
model('express_template_item')->add($data_item);
|
||||
}
|
||||
}
|
||||
|
||||
//具体模板信息
|
||||
model('express_template_free_shipping')->delete([ [ 'template_id', '=', $data[ 'template_id' ] ] ]);
|
||||
foreach ($shipping_items as $k => $v) {
|
||||
$data_item = $v;
|
||||
$data_item[ 'template_id' ] = $data[ 'template_id' ];
|
||||
if ($data_item[ 'area_ids' ] && $data_item[ 'area_names' ]) {
|
||||
model('express_template_free_shipping')->add($data_item);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除系统运费模板
|
||||
* @param int $template_id
|
||||
*/
|
||||
public function deleteExpressTemplate($template_id, $site_id)
|
||||
{
|
||||
$res = model('express_template')->delete([ [ 'template_id', 'in', $template_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if ($res) {
|
||||
model('express_template_item')->delete([ [ 'template_id', 'in', $template_id ] ]);
|
||||
model('express_template_free_shipping')->delete([ [ 'template_id', 'in', $template_id ] ]);
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置默认运费模板
|
||||
* @param int $template_id
|
||||
*/
|
||||
public function updateDefaultExpressTemplate($template_id, $is_default, $site_id)
|
||||
{
|
||||
if ($is_default == 1) {
|
||||
model('express_template')->update([ 'is_default' => 0 ], [ 'site_id' => $site_id ]);
|
||||
}
|
||||
$res = model('express_template')->update([ 'is_default' => 1 ], [ 'template_id' => $template_id ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取运费模板信息
|
||||
* @param $template_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressTemplateInfo($template_id, $site_id)
|
||||
{
|
||||
|
||||
$res = model('express_template')->getInfo([ [ 'template_id', '=', $template_id ], [ 'site_id', '=', $site_id ] ], 'template_id, site_id, template_name, fee_type, create_time, modify_time, is_default, surplus_area_ids, appoint_free_shipping, shipping_surplus_area_ids');
|
||||
if ($res) {
|
||||
$res[ 'template_item' ] = model('express_template_item')->getList([ [ 'template_id', '=', $template_id ] ], '*');
|
||||
$res[ 'shipping_template_item' ] = model('express_template_free_shipping')->getList([ [ 'template_id', '=', $template_id ] ], '*');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认运费模板
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultTemplate($site_id)
|
||||
{
|
||||
|
||||
$res = model('express_template')->getInfo([ [ 'is_default', '=', 1 ], [ 'site_id', '=', $site_id ] ], 'template_id, site_id, template_name, fee_type, create_time, modify_time, is_default');
|
||||
if ($res) {
|
||||
$res[ 'template_item' ] = model('express_template_item')->getList([ [ 'template_id', '=', $res[ 'template_id' ] ] ], '*');
|
||||
$res[ 'shipping_template_item' ] = model('express_template_free_shipping')->getList([ [ 'template_id', '=', $res[ 'template_id' ] ] ], '*');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取运费模板列表(主表查询)
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getExpressTemplateList($condition = [], $field = 'template_id, site_id, template_name, fee_type, create_time, modify_time, is_default', $order = '', $limit = null)
|
||||
{
|
||||
|
||||
$list = model('express_template')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取运费模板列表(主表查询)
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getExpressTemplatePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'template_id, site_id, template_name, fee_type, create_time, modify_time, is_default')
|
||||
{
|
||||
$list = model('express_template')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取运费模板地域运费列表(主表查询)
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getExpressTemplateItemList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('express_template_item')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
86
app/model/express/Kd100.php
Executable file
86
app/model/express/Kd100.php
Executable file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\BaseModel;
|
||||
use extend\Kd100 as Kd100Extend;
|
||||
|
||||
/**
|
||||
* 快递100
|
||||
*/
|
||||
class Kd100 extends BaseModel
|
||||
{
|
||||
|
||||
/*********************************************************************** 快递100 start ***********************************************************************/
|
||||
/**
|
||||
* 快递100配置
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getKd100Config($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'app_module', '=', 'shop' ], [ 'site_id', '=', $site_id ], [ 'config_key', '=', 'EXPRESS_KD100_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置物流配送配置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function setKd100Config($data, $is_use, $site_id)
|
||||
{
|
||||
if ($is_use > 0) {
|
||||
$this->modifyStatus(0, $site_id);
|
||||
}
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '快递100设置', $is_use, [ [ 'app_module', '=', 'shop' ], [ 'site_id', '=', $site_id ], [ 'config_key', '=', 'EXPRESS_KD100_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开关状态
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function modifyStatus($is_use, $site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->modifyConfigIsUse($is_use, [ [ 'app_module', '=', 'shop' ], [ 'site_id', '=', $site_id ], [ 'config_key', '=', 'EXPRESS_KD100_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
/*********************************************************************** 快递100 end ***********************************************************************/
|
||||
|
||||
/**
|
||||
* 查询物流轨迹 并且转化为兼容数据结构
|
||||
* @param $code
|
||||
* @param $express_no
|
||||
* @param $phone
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function trace($code, $express_no, $phone, $site_id)
|
||||
{
|
||||
$config_result = $this->getKd100Config($site_id);
|
||||
$config = $config_result[ 'data' ];
|
||||
if ($config[ 'is_use' ] == 0)
|
||||
return $this->error();
|
||||
|
||||
$kd100_extend = new Kd100Extend($config[ 'value' ]);
|
||||
$result = $kd100_extend->getExpressTracesEnterpriseEdition($express_no, $code, $phone);
|
||||
return $this->success($result);
|
||||
}
|
||||
}
|
||||
95
app/model/express/Kdbird.php
Executable file
95
app/model/express/Kdbird.php
Executable file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\BaseModel;
|
||||
use extend\Kdbird as KdbirdExtend;
|
||||
|
||||
/**
|
||||
* 快递鸟
|
||||
*/
|
||||
class Kdbird extends BaseModel
|
||||
{
|
||||
|
||||
/*********************************************************************** 快递100 start ***********************************************************************/
|
||||
/**
|
||||
* 快递鸟配置
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getKdbirdConfig($site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'app_module', '=', 'shop' ], [ 'site_id', '=', $site_id ], [ 'config_key', '=', 'EXPRESS_KDBIRD_CONFIG' ] ]);
|
||||
$res['data']['value'] = assignData($res['data']['value'], [
|
||||
'EBusinessID' => '',
|
||||
'AppKey' => '',
|
||||
'RequestType' => '1002',//1002 免费版 8001 按次付费 8002 按单付费
|
||||
]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置物流配送配置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function setKdbirdConfig($data, $is_use, $site_id)
|
||||
{
|
||||
if ($is_use > 0) {
|
||||
$this->modifyStatus(0, $site_id);
|
||||
}
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '快递鸟设置', $is_use, [ [ 'app_module', '=', 'shop' ], [ 'site_id', '=', $site_id ], [ 'config_key', '=', 'EXPRESS_KDBIRD_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开关状态
|
||||
* @param $is_use
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function modifyStatus($is_use, $site_id)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->modifyConfigIsUse($is_use, [ [ 'app_module', '=', 'shop' ], [ 'site_id', '=', $site_id ], [ 'config_key', '=', 'EXPRESS_KDBIRD_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
/*********************************************************************** 快递100 end ***********************************************************************/
|
||||
|
||||
/**
|
||||
* 查询物流轨迹 并且转化为兼容数据结构
|
||||
* @param $code
|
||||
* @param $express_no
|
||||
* @param $site_id
|
||||
* @param $mobile
|
||||
* @return array
|
||||
*/
|
||||
public function trace($code, $express_no, $site_id, $mobile)
|
||||
{
|
||||
$config_result = $this->getKdbirdConfig($site_id);
|
||||
$config = $config_result[ 'data' ];
|
||||
|
||||
if ($config[ 'is_use' ] == 0) return $this->error();
|
||||
|
||||
$kd100_extend = new KdbirdExtend($config[ 'value' ]);
|
||||
$result = $kd100_extend->orderTracesSubByJson($express_no, $code, $mobile);
|
||||
if (isset($result[ 'success' ]) && $result[ 'success' ]) {
|
||||
return $this->success($result);
|
||||
} else {
|
||||
return $this->error($result, $result[ 'reason' ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
719
app/model/express/Local.php
Executable file
719
app/model/express/Local.php
Executable file
@@ -0,0 +1,719 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\shop\Shop;
|
||||
use app\model\store\Store;
|
||||
|
||||
/**
|
||||
* 外卖配送
|
||||
*/
|
||||
class Local extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加站点外卖配送配置
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addLocal($data)
|
||||
{
|
||||
$id = model('local')->add($data);
|
||||
return $this->success($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改站点外卖配送配置
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editLocal($data, $condition)
|
||||
{
|
||||
$res = model('local')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除站点外卖配送 (通常删除站点会用到)
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteLocal($condition)
|
||||
{
|
||||
$res = model('local')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取站点外卖配送信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getLocalInfo($condition, $field = '*')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
$store_id = $check_condition['store_id'] ?? 0;
|
||||
|
||||
$info = model('local')->getInfo($condition, $field);
|
||||
if (empty($info)) {
|
||||
$local_data = array(
|
||||
'site_id' => $site_id,
|
||||
'store_id' => $store_id,
|
||||
'update_time' => time()
|
||||
);
|
||||
$this->addLocal($local_data);
|
||||
$info = model('local')->getInfo($condition, $field);
|
||||
}
|
||||
if (!empty($info)) {
|
||||
$local_area_array = [];
|
||||
if (!empty($info['local_area_json'])) {
|
||||
$local_area_array = json_decode($info['local_area_json'], true);
|
||||
}
|
||||
$info['local_area_array'] = $local_area_array;
|
||||
$time_week = [];
|
||||
if (!empty($info['time_week'])) {
|
||||
$time_week = explode(',', $info['time_week']);
|
||||
}
|
||||
$info['time_week'] = $time_week;
|
||||
$area_array = [];
|
||||
if (!empty($info['area_array'])) {
|
||||
$area_array = explode(',', $info['area_array']);
|
||||
}
|
||||
$info['area_array'] = $area_array;
|
||||
if (empty($info['delivery_time'])) {
|
||||
$info['delivery_time'] = [
|
||||
['start_time' => $info['start_time'], 'end_time' => $info['end_time']]
|
||||
];
|
||||
} else {
|
||||
$info['delivery_time'] = json_decode($info['delivery_time'], true);
|
||||
}
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取站点外卖配送列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getLocalList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('local')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取站点外卖配送分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getLocalPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('local')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算费用
|
||||
* @param $shop_goods
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function calculate($params)
|
||||
{
|
||||
/** @var \app\model\order\OrderCreate $order_object */
|
||||
$order_object = $params['order_object'];
|
||||
$is_limit_start_money = $order_object->is_limit_start_money ?? true;//是否限制起送价
|
||||
|
||||
$site_id = $order_object->site_id;
|
||||
$local_condition = array(
|
||||
['site_id', '=', $site_id]
|
||||
);
|
||||
$store_id = $order_object->store_id ?? 0;
|
||||
//todo 应该判断一下是否有门店运营插件
|
||||
if (!addon_is_exit('store')) {
|
||||
$store_id = 0;
|
||||
}
|
||||
if ($store_id > 0) $local_condition[] = ['store_id', '=', $store_id];
|
||||
$local_info = $this->getLocalInfo($local_condition)['data'];
|
||||
if (empty($local_info)) return $this->error('', '没有可以配送的门店');
|
||||
$start_price_error = 0;//起送价错误
|
||||
$distance_error = 0;//配送距离错误
|
||||
$time_error = 0;
|
||||
$error_code = 12;
|
||||
if (!addon_is_exit('store')) {
|
||||
$condition_store_id = $local_info['store_id'];
|
||||
} else {
|
||||
$condition_store_id = $store_id;
|
||||
}
|
||||
$delivery_restrictions_res = (new \app\model\storegoods\StoreGoods)->getStoreGoodsSkuInfo([
|
||||
['sku_id', 'in', array_column($order_object->goods_list, "sku_id")],
|
||||
['is_delivery_restrictions', '=', 0],
|
||||
['store_id', '=', $condition_store_id],
|
||||
]);
|
||||
if (!empty($delivery_restrictions_res['data'])) {
|
||||
$is_limit_start_money = false; // 存在不限制起送的商品直接去除限制
|
||||
}
|
||||
|
||||
|
||||
if (empty($order_object->delivery['member_address'])) {
|
||||
return $this->error(['code' => $error_code], '请选择同城配送地址');
|
||||
}
|
||||
|
||||
$delivery_time_data = $order_object->delivery['buyer_ask_delivery_time'] ?? [];
|
||||
$delivery_start_time = $delivery_time_data['start_time'] ?? '';//配送时间左限
|
||||
$delivery_end_time = $delivery_time_data['end_time'] ?? '';//配送时间右限
|
||||
$error = '所选地址无法配送';
|
||||
//判断时间 是否在时间段内
|
||||
if ($local_info['time_is_open'] == 1 && $order_object->is_check_buyer_ask_delivery_time) {
|
||||
if ($delivery_start_time && $delivery_end_time) {
|
||||
if ($local_info['time_type'] == 1) {//自定义
|
||||
$start_week = date('w', $delivery_start_time);
|
||||
$end_week = date('w', $delivery_end_time);
|
||||
if (!(in_array($start_week, $local_info['time_week']) && in_array($end_week, $local_info['time_week']))) {//判断是否是营业日
|
||||
$time_error++;
|
||||
$error = '配送时间不在营业时间内';
|
||||
}
|
||||
}
|
||||
|
||||
//判断运营时段
|
||||
$start_daytime = strtotime(date('Y-m-d H:i:s', $delivery_start_time)) - strtotime(date('Y-m-d 00:00:00', $delivery_start_time));
|
||||
$end_daytime = strtotime(date('Y-m-d H:i:s', $delivery_end_time)) - strtotime(date('Y-m-d 00:00:00', $delivery_end_time));
|
||||
$delivery_time_range = $local_info['delivery_time'] ?? [];
|
||||
if ($delivery_time_range) {
|
||||
foreach ($delivery_time_range as $v) {
|
||||
$temp_start_time = ($v['start_time']);
|
||||
$temp_end_time = ($v['end_time']);
|
||||
if (!($start_daytime >= $temp_start_time && $end_daytime >= $temp_start_time && $start_daytime <= $temp_end_time && $end_daytime <= $temp_end_time)) {
|
||||
$time_error++;
|
||||
$error = '配送时间不在营业时间段内';
|
||||
} else {
|
||||
$time_error = 0;
|
||||
$error = '';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
$time_error++;
|
||||
$error = '请选择配送时间';
|
||||
}
|
||||
|
||||
} else {
|
||||
$error = '门店未开启配送时间';
|
||||
}
|
||||
|
||||
if ($store_id == 0) {
|
||||
$shop_model = new Shop();
|
||||
$shop_info = $shop_model->getShopInfo([['site_id', '=', $site_id]])['data'] ?? [];
|
||||
} else {
|
||||
$store_model = new Store();
|
||||
$shop_info = $store_model->getStoreInfo([['site_id', '=', $site_id], ['store_id', '=', $store_id]])['data'] ?? [];
|
||||
}
|
||||
if (empty($shop_info['longitude']) || empty($shop_info['latitude'])) {
|
||||
return $this->error(['code' => $error_code], '商家未设置配送地址');
|
||||
}
|
||||
$is_delivery = false;
|
||||
$start_money_array = [];
|
||||
if ($local_info['area_type'] == 1 || $local_info['area_type'] == 2) {
|
||||
if ($order_object->delivery['member_address']['longitude'] == 0 && $order_object->delivery['member_address']['latitude'] == 0) {
|
||||
return $this->error(['code' => $error_code], '下单地址缺少定位信息');
|
||||
}
|
||||
|
||||
$shop_longitude = $shop_info['longitude'];
|
||||
$shop_latitude = $shop_info['latitude'];
|
||||
$longitude = $order_object->delivery['member_address']['longitude'];
|
||||
$latitude = $order_object->delivery['member_address']['latitude'];
|
||||
if ($local_info['area_type'] == 1) {
|
||||
$distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
|
||||
if ($distance <= $local_info['start_distance']) {//是否在起送距离以内
|
||||
$delivery_money = $local_info['start_delivery_money'];
|
||||
} else {
|
||||
$diff_distance = $distance - $local_info['start_distance'];//减去起送距离 求得差
|
||||
if ($local_info['continued_distance'] == 0) return $this->error(['code' => $error_code], '当前配送地址不支持配送');
|
||||
$delivery_money = $local_info['start_delivery_money'] + ceil($diff_distance / $local_info['continued_distance']) * $local_info['continued_delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$delivery_money_array = [];
|
||||
}
|
||||
$local_area_array = $local_info['local_area_array'];
|
||||
|
||||
if (!empty($local_area_array)) {
|
||||
foreach ($local_area_array as $k => $v) {
|
||||
//起送价是否满足
|
||||
if (!$is_limit_start_money || $order_object->goods_money >= $v['start_price']) {
|
||||
$path = $v['path'];
|
||||
if ($v['rule_type'] == 'circle') {//半径
|
||||
|
||||
$item_longitude = $path['center']['longitude'];
|
||||
$item_latitude = $path['center']['latitude'];
|
||||
$radius = $path['radius'];
|
||||
$item_distance = $this->getDistance($latitude, $longitude, $item_latitude, $item_longitude);
|
||||
//判断有无超出范围
|
||||
if ($item_distance <= $radius / 1000) {
|
||||
$is_delivery = true;
|
||||
if ($local_info['area_type'] == 2) {
|
||||
//非半径 配送费是取设置的
|
||||
$delivery_money_array[] = $v['delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$distance_error++;
|
||||
}
|
||||
} else if ($v['rule_type'] == 'polygon') {//多边形
|
||||
|
||||
$point = array(
|
||||
'latitude' => $latitude,
|
||||
'longitude' => $longitude,
|
||||
);
|
||||
//判断坐标是否在多边形内
|
||||
if (is_point_in_polygon($point, $path)) {
|
||||
$is_delivery = true;
|
||||
if ($local_info['area_type'] == 2) {
|
||||
//非半径 配送费是取设置的
|
||||
$delivery_money_array[] = $v['delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$distance_error++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$start_money_array[] = $v['start_price'];
|
||||
$start_price_error++;
|
||||
}
|
||||
}
|
||||
|
||||
//区域配送 存在相交的区域,以配送费低的区域价格来计算
|
||||
if ($local_info['area_type'] == 2) {
|
||||
if (!empty($delivery_money_array)) {
|
||||
$delivery_money = min($delivery_money_array);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//行政区域配送
|
||||
|
||||
if (!$is_limit_start_money || $order_object->goods_money >= $local_info['start_money']) {
|
||||
|
||||
$district_id = $order_object->delivery['member_address']['district_id'];//区县地域id
|
||||
$area_array = $local_info['area_array'];
|
||||
if (in_array($district_id, $area_array)) {
|
||||
//启用阶梯价 的话 也是必须要具体坐标的
|
||||
if ($local_info['is_open_step'] == 1) {
|
||||
|
||||
if ($order_object->delivery['member_address']['longitude'] == 0 && $order_object->delivery['member_address']['latitude'] == 0) {
|
||||
return $this->error(['code' => $error_code], '当前配送地址没有配置定位坐标');
|
||||
}
|
||||
|
||||
$is_delivery = true;
|
||||
$shop_longitude = $shop_info['longitude'];
|
||||
$shop_latitude = $shop_info['latitude'];
|
||||
$longitude = $order_object->delivery['member_address']['longitude'];
|
||||
$latitude = $order_object->delivery['member_address']['latitude'];
|
||||
|
||||
$distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
|
||||
if ($distance <= $local_info['start_distance']) {//是否在起送距离以内
|
||||
$delivery_money = $local_info['start_delivery_money'];
|
||||
} else {
|
||||
$diff_distance = $distance - $local_info['start_distance'];//减去起送距离 求得差
|
||||
$delivery_money = $local_info['start_delivery_money'] + ceil($diff_distance / $local_info['continued_distance']) * $local_info['continued_delivery_money'];
|
||||
}
|
||||
|
||||
} else {
|
||||
$is_delivery = true;
|
||||
$delivery_money = $local_info['delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$distance_error++;//配送距离错误
|
||||
}
|
||||
|
||||
} else {
|
||||
$start_price_error++;//起送价错误
|
||||
$start_money_array[] = $local_info['start_money'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($is_delivery) {
|
||||
if ($delivery_money > 0) {
|
||||
$man_type = $local_info['man_type'];
|
||||
$man_money = $local_info['man_money'];
|
||||
switch ($man_type) {
|
||||
case 'free':
|
||||
if ($order_object->goods_money >= $man_money) {
|
||||
$delivery_money = 0;
|
||||
}
|
||||
break;
|
||||
case 'discount':
|
||||
if ($order_object->goods_money >= $man_money) {
|
||||
$man_discount = $local_info['man_discount'];
|
||||
$delivery_money -= $man_discount;
|
||||
$delivery_money = max($delivery_money, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
$return_result = array(
|
||||
'delivery_money' => $delivery_money,
|
||||
);
|
||||
if ($time_error > 0) {
|
||||
$return_result['code'] = 1;
|
||||
$return_result['error'] = $error;
|
||||
}
|
||||
return $this->success($return_result);
|
||||
} else {
|
||||
if ($distance_error > 0) {
|
||||
$error = '当前地址不在该门店配送区域,请重新选择可配送该区域的门店';
|
||||
$error_code = 10;
|
||||
} else if ($start_price_error > 0 && !isset($order_object->delivery['unlimited_start_money'])) {//todo 这儿要定义不限制起送价
|
||||
$error = '当前商品金额尚不满足商家配送的最低起送价格1';
|
||||
$error_code = 11;
|
||||
}
|
||||
return $this->error(['code' => $error_code, 'start_money_array' => $start_money_array], $error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 区域是否支持配送
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function isSupportDelivery($data)
|
||||
{
|
||||
$local_condition = array(
|
||||
['site_id', '=', $data['site_id']],
|
||||
);
|
||||
if (!empty($data['store_id'])) $local_condition[] = ['store_id', '=', $data['store_id']];
|
||||
$local_info = $this->getLocalInfo($local_condition)['data'];
|
||||
|
||||
$distance_error = 0;//配送距离错误
|
||||
$error_code = 12;
|
||||
|
||||
$is_delivery = false;
|
||||
|
||||
if ($local_info['area_type'] == 1 || $local_info['area_type'] == 2) {
|
||||
if ($data['longitude'] == 0 && $data['latitude'] == 0) {
|
||||
return $this->error(['code' => $error_code], '超出配送范围');
|
||||
}
|
||||
$longitude = $data['longitude'];
|
||||
$latitude = $data['latitude'];
|
||||
$local_area_array = $local_info['local_area_array'];
|
||||
if (!empty($local_area_array)) {
|
||||
foreach ($local_area_array as $k => $v) {
|
||||
$path = $v['path'];
|
||||
if ($v['rule_type'] == 'circle') {//半径
|
||||
|
||||
$item_longitude = $path['center']['longitude'];
|
||||
$item_latitude = $path['center']['latitude'];
|
||||
$radius = $path['radius'];
|
||||
$item_distance = $this->getDistance($latitude, $longitude, $item_latitude, $item_longitude);
|
||||
//判断有无超出范围
|
||||
if ($item_distance <= $radius / 1000) {
|
||||
$is_delivery = true;
|
||||
} else {
|
||||
$distance_error++;
|
||||
}
|
||||
} else if ($v['rule_type'] == 'polygon') {//多边形
|
||||
|
||||
$point = array(
|
||||
'latitude' => $latitude,
|
||||
'longitude' => $longitude,
|
||||
);
|
||||
//判断坐标是否在多边形内
|
||||
if (is_point_in_polygon($point, $path)) {
|
||||
$is_delivery = true;
|
||||
} else {
|
||||
$distance_error++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//行政区域配送
|
||||
$district_id = $data['district_id'];//区县地域id
|
||||
$area_array = $local_info['area_array'];
|
||||
if (in_array($district_id, $area_array)) {
|
||||
//启用阶梯价 的话 也是必须要具体坐标的
|
||||
if ($local_info['is_open_step'] == 1) {
|
||||
if ($data['longitude'] == 0 && $data['latitude'] == 0) {
|
||||
return $this->error(['code' => $error_code], '超出配送范围');
|
||||
}
|
||||
$is_delivery = true;
|
||||
} else {
|
||||
$is_delivery = true;
|
||||
}
|
||||
} else {
|
||||
$distance_error++;//配送距离错误
|
||||
}
|
||||
}
|
||||
if (!$is_delivery && $distance_error) {
|
||||
$error = '超出配送范围';
|
||||
return $this->error(['code' => $error_code], $error);
|
||||
}
|
||||
return $this->success(1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断可用的区域
|
||||
* @param $type
|
||||
* @param $latlng
|
||||
* @param $range
|
||||
*/
|
||||
public function getWithAreaList($type, $latlng, $range)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 求两个已知经纬度之间的距离,单位为km
|
||||
* @param lng1,lng2 经度
|
||||
* @param lat1,lat2 纬度
|
||||
* @return float 距离,单位为km
|
||||
**/
|
||||
public function getDistance($lat1, $lng1, $lat2, $lng2)
|
||||
{
|
||||
//将角度转为狐度
|
||||
$radLat1 = deg2rad($lat1);//deg2rad()函数将角度转换为弧度
|
||||
$radLat2 = deg2rad($lat2);
|
||||
$radLng1 = deg2rad($lng1);
|
||||
$radLng2 = deg2rad($lng2);
|
||||
$a = $radLat1 - $radLat2;
|
||||
$b = $radLng1 - $radLng2;
|
||||
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6371;
|
||||
return round($s, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 积分兑换计算费用
|
||||
* @param $goods_info
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function pointExchangeCalculate($goods_info, $data)
|
||||
{
|
||||
$local_condition = array(
|
||||
['site_id', '=', $data['site_id']]
|
||||
);
|
||||
if (isset($data['delivery']['store_id'])) $local_condition[] = ['store_id', '=', $data['delivery']['store_id']];
|
||||
$local_info = $this->getLocalInfo($local_condition)['data'];
|
||||
if (empty($local_info)) return $this->error('', '没有可以配送的门店');
|
||||
|
||||
$start_price_error = 0;//起送价错误
|
||||
$distance_error = 0;//配送距离错误
|
||||
$time_error = 0;
|
||||
$error_code = 12;
|
||||
$error = '';
|
||||
//判断时间 是否在时间段内
|
||||
if ($local_info['time_is_open'] == 1) {
|
||||
$week = date('w');
|
||||
if (in_array($week, $local_info['time_week'])) {
|
||||
$time = $goods_info['buyer_ask_delivery_time'];
|
||||
if ($time == 0) {
|
||||
$time_error++;
|
||||
$error = '请选择配送时间';
|
||||
}
|
||||
} else {
|
||||
$time_error++;
|
||||
$error = '配送时间不在营业时间内';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$shop_model = new Shop();
|
||||
$shop_info = $shop_model->getShopInfo([['site_id', '=', $data['site_id']]])['data'];
|
||||
$is_delivery = false;
|
||||
|
||||
$start_money_array = [];
|
||||
if ($local_info['area_type'] == 1 || $local_info['area_type'] == 2) {
|
||||
if (empty($data['member_address'])) {
|
||||
return $this->error(['code' => $error_code], '请设置配送地址');
|
||||
}
|
||||
if ($data['member_address']['longitude'] == 0 && $data['member_address']['latitude'] == 0) {
|
||||
return $this->error(['code' => $error_code], '当前配送地址没有配置定位坐标');
|
||||
}
|
||||
|
||||
$shop_longitude = $shop_info['longitude'];
|
||||
$shop_latitude = $shop_info['latitude'];
|
||||
$longitude = $data['member_address']['longitude'];
|
||||
$latitude = $data['member_address']['latitude'];
|
||||
if ($local_info['area_type'] == 1) {
|
||||
$distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
|
||||
if ($distance <= $local_info['start_distance']) {//是否在起送距离以内
|
||||
$delivery_money = $local_info['start_delivery_money'];
|
||||
} else {
|
||||
$diff_distance = $distance - $local_info['start_distance'];//减去起送距离 求得差
|
||||
$delivery_money = $local_info['start_delivery_money'] + ceil($diff_distance / $local_info['continued_distance']) * $local_info['continued_delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$delivery_money_array = [];
|
||||
}
|
||||
$local_area_array = $local_info['local_area_array'];
|
||||
|
||||
if (!empty($local_area_array)) {
|
||||
foreach ($local_area_array as $k => $v) {
|
||||
//起送价是否满足
|
||||
if ($data['exchange_info']['price'] >= $v['start_price']) {
|
||||
$path = $v['path'];
|
||||
if ($v['rule_type'] == 'circle') {//半径
|
||||
|
||||
$item_longitude = $path['center']['longitude'];
|
||||
$item_latitude = $path['center']['latitude'];
|
||||
$radius = $path['radius'];
|
||||
$item_distance = $this->getDistance($latitude, $longitude, $item_latitude, $item_longitude);
|
||||
//判断有无超出范围
|
||||
if ($item_distance <= $radius / 1000) {
|
||||
$is_delivery = true;
|
||||
if ($local_info['area_type'] == 2) {
|
||||
// //非半径 配送费是取设置的
|
||||
$delivery_money_array[] = $v['delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$distance_error++;
|
||||
}
|
||||
} else if ($v['rule_type'] == 'polygon') {//多边形
|
||||
|
||||
$point = array(
|
||||
'latitude' => $latitude,
|
||||
'longitude' => $longitude,
|
||||
);
|
||||
//判断坐标是否在多边形内
|
||||
if (is_point_in_polygon($point, $path)) {
|
||||
$is_delivery = true;
|
||||
if ($local_info['area_type'] == 2) {
|
||||
//非半径 配送费是取设置的
|
||||
$delivery_money_array[] = $v['delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$distance_error++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$start_money_array[] = $v['start_price'];
|
||||
$start_price_error++;
|
||||
}
|
||||
}
|
||||
|
||||
//区域配送 存在相交的区域,以配送费低的区域价格来计算
|
||||
if ($local_info['area_type'] == 2) {
|
||||
if (!empty($delivery_money_array)) {
|
||||
$delivery_money = min($delivery_money_array);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//行政区域配送
|
||||
|
||||
if ($data['exchange_info']['price'] >= $local_info['start_money']) {
|
||||
|
||||
$district_id = $data['member_address']['district_id'];//区县地域id
|
||||
$area_array = $local_info['area_array'];
|
||||
if (in_array($district_id, $area_array)) {
|
||||
//启用阶梯价 的话 也是必须要具体坐标的
|
||||
if ($local_info['is_open_step'] == 1) {
|
||||
|
||||
if ($data['member_address']['longitude'] == 0 && $data['member_address']['latitude'] == 0) {
|
||||
return $this->error(['code' => $error_code], '当前配送地址没有配置定位坐标');
|
||||
}
|
||||
|
||||
$is_delivery = true;
|
||||
$shop_longitude = $shop_info['longitude'];
|
||||
$shop_latitude = $shop_info['latitude'];
|
||||
$longitude = $data['member_address']['longitude'];
|
||||
$latitude = $data['member_address']['latitude'];
|
||||
|
||||
$distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
|
||||
if ($distance <= $local_info['start_distance']) {//是否在起送距离以内
|
||||
$delivery_money = $local_info['start_delivery_money'];
|
||||
} else {
|
||||
$diff_distance = $distance - $local_info['start_distance'];//减去起送距离 求得差
|
||||
$delivery_money = $local_info['start_delivery_money'] + ceil($diff_distance / $local_info['continued_distance']) * $local_info['continued_delivery_money'];
|
||||
}
|
||||
|
||||
} else {
|
||||
$is_delivery = true;
|
||||
$delivery_money = $local_info['delivery_money'];
|
||||
}
|
||||
} else {
|
||||
$distance_error++;//配送距离错误
|
||||
}
|
||||
|
||||
} else {
|
||||
$start_price_error++;//起送价错误
|
||||
$start_money_array[] = $local_info['start_money'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($is_delivery) {
|
||||
$return_result = array(
|
||||
'delivery_money' => $delivery_money,
|
||||
'start_money_array' => $start_money_array,
|
||||
);
|
||||
if ($time_error > 0) {
|
||||
$return_result['code'] = 1;
|
||||
$return_result['error'] = $error;
|
||||
}
|
||||
return $this->success($return_result);
|
||||
} else {
|
||||
if ($distance_error > 0) {
|
||||
$error = '当前地址不在该门店配送区域,请重新选择可配送该区域的门店';
|
||||
$error_code = 10;
|
||||
} else if ($start_price_error > 0) {
|
||||
$error = '当前商品金额尚不满足商家配送的最低起送价格';
|
||||
$error_code = 11;
|
||||
}
|
||||
return $this->error(['code' => $error_code], $error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 核验是否可以开启本地配送
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function checkIsCanTradeLocal($site_id)
|
||||
{
|
||||
$store = new Store();
|
||||
$default_store = $store->getStoreInfo([['site_id', '=', $site_id], ['is_default', '=', 1]], 'store_id')['data'] ?? [];
|
||||
$store_id = $default_store['store_id'] ?? 0;
|
||||
|
||||
$local_info = $this->getLocalInfo([['site_id', '=', $site_id], ['store_id', '=', $store_id]])['data'] ?? [];
|
||||
if (empty($local_info))
|
||||
return $this->error([], '您未完成起送金额、配送费 、配送区域等同城送配置项设置,需设置并提交保存后,才能开启同城配送开关。');
|
||||
|
||||
//判断配置有没有配置完善
|
||||
$local_area_array = $local_info['local_area_array'];
|
||||
$area_array = $local_info['area_array'];
|
||||
if (empty($local_area_array) && empty($area_array))
|
||||
return $this->error([], '您未完成起送金额、配送费 、配送区域等同城送配置项设置,需设置并提交保存后,才能开启同城配送开关。');
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
public function getInfo($condition, $field = '*'){
|
||||
$info = model('local')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
}
|
||||
108
app/model/express/LocalPackage.php
Executable file
108
app/model/express/LocalPackage.php
Executable file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\member\Member;
|
||||
|
||||
/**
|
||||
* 外卖配送
|
||||
*/
|
||||
class LocalPackage extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取外卖配送包裹列表
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getLocalDeliveryPackageList($condition, $field = '*')
|
||||
{
|
||||
$list = model('local_delivery_package')->getList($condition, $field);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取包裹信息
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function package($condition)
|
||||
{
|
||||
$info = model('local_delivery_package')->getInfo($condition, '*');
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 外卖配送
|
||||
*/
|
||||
public function delivery($param)
|
||||
{
|
||||
$order_id = $param[ 'order_id' ] ?? 0;//订单id
|
||||
$order_goods_id_array = $param[ 'order_goods_id_array' ];
|
||||
$goods_id_array = $param[ 'goods_id_array' ];
|
||||
$delivery_type = $param[ 'delivery_type' ];//物流方式 1 物流配送 0 无需物流
|
||||
$delivery_no = $param[ 'delivery_no' ] ?? '';//物流单号
|
||||
$member_id = $param[ 'member_id' ];
|
||||
$site_id = $param[ 'site_id' ];
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $member_id ] ], 'nickname');
|
||||
$member_info = $member_info_result[ 'data' ] ?? [];
|
||||
//查询物流单号是否已存在,如果存在就合并入已存在的数据
|
||||
$condition = array (
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'delivery_no', '=', $delivery_no ],
|
||||
[ 'order_id', '=', $order_id ],
|
||||
[ 'delivery_type', '=', $delivery_type ],
|
||||
[ 'member_id', '=', $member_id ]
|
||||
);
|
||||
$info = model('local_delivery_package')->getInfo($condition, '*');
|
||||
if (empty($info)) {
|
||||
if ($delivery_type > 0) {
|
||||
$count = model('local_delivery_package')->getCount([ [ 'site_id', '=', $site_id ], [ 'order_id', '=', $order_id ], [ 'delivery_type', '=', $delivery_type ] ]);
|
||||
$num = $count + 1;
|
||||
$package_name = '包裹' . $num;
|
||||
} else {
|
||||
$package_name = '商家自配送';
|
||||
}
|
||||
$data = array (
|
||||
'order_id' => $order_id,
|
||||
'order_goods_id_array' => implode(',', $order_goods_id_array),
|
||||
'goods_id_array' => implode(',', $goods_id_array),
|
||||
'delivery_no' => $delivery_no,
|
||||
'site_id' => $site_id,
|
||||
'member_id' => $member_id,
|
||||
'member_name' => $member_info[ 'nickname' ] ?? '',
|
||||
'delivery_type' => $delivery_type,
|
||||
'package_name' => $package_name,
|
||||
'delivery_time' => time(),
|
||||
'deliverer' => $param[ 'deliverer' ] ?? '',
|
||||
'deliverer_mobile' => $param[ 'deliverer_mobile' ] ?? '',
|
||||
);
|
||||
$result = model('local_delivery_package')->add($data);
|
||||
} else {
|
||||
$temp_order_goods_id_arr = explode(',', $info[ 'order_goods_id_array' ]);
|
||||
$temp_goods_id_arr = explode(',', $info[ 'goods_id_array' ]);
|
||||
|
||||
$order_goods_id_array = implode(',', array_unique(array_merge($temp_order_goods_id_arr, $order_goods_id_array)));
|
||||
$goods_id_array = implode(',', array_merge($temp_goods_id_arr, $goods_id_array));
|
||||
$data = array (
|
||||
'order_goods_id_array' => $order_goods_id_array,
|
||||
'goods_id_array' => $goods_id_array,
|
||||
);
|
||||
$result = model('local_delivery_package')->update($data, $condition);
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
}
|
||||
86
app/model/express/Trace.php
Executable file
86
app/model/express/Trace.php
Executable file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\express;
|
||||
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 物流配送
|
||||
*/
|
||||
class Trace extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 物流跟踪信息
|
||||
* @param $code
|
||||
* @param $company_id
|
||||
* @param $site_id
|
||||
* @param $mobile
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function trace($code, $company_id, $site_id, $mobile)
|
||||
{
|
||||
// $result = array(
|
||||
// 'success' => true,//成功与否
|
||||
// 'reason' => '',//错误原因
|
||||
// 'status' => '1',//物流状态:0-无轨迹,1-已揽收, 2-在途中 201-到达派件城市,3-签收,4-问题件
|
||||
// 'status_name' => '已揽收',//物流状态名称:0-无轨迹,1-已揽收, 2-在途中 201-到达派件城市,3-签收,4-问题件,
|
||||
// 'shipper_code' => 'SH',//快递公司编码
|
||||
// 'logistic_code' => $code,//物流运单号
|
||||
// 'list' => array(
|
||||
// [
|
||||
// 'datetime' => '2015-03-08 01:15:00',
|
||||
// 'remark' => '离开广州市 发往北京市(经转)',
|
||||
// ]
|
||||
// )
|
||||
// );
|
||||
$express_company_model = new ExpressCompanyTemplate();
|
||||
$company_info_result = $express_company_model->getExpressCompanyTemplateInfo([ [ 'company_id', '=', $company_id ] ]);
|
||||
if (empty($company_info_result[ 'data' ]))
|
||||
return $this->success([ 'success' => false, 'reason' => '物流公司信息不完整!' ]);
|
||||
|
||||
$company_info_result[ 'data' ][ 'site_id' ] = $site_id;
|
||||
$result = $this->getTrace([ 'code' => $code, 'express_no_data' => $company_info_result[ 'data' ], 'mobile' => $mobile ]);
|
||||
|
||||
if (empty($result)) {
|
||||
$data = [ 'success' => false, 'reason' => '抱歉,没有启用的物流方式' ];
|
||||
return $this->success($data);
|
||||
}
|
||||
if ($result[ 'code' ] < 0 || empty($result[ 'data' ])) {
|
||||
$data = [ 'success' => false, 'reason' => $result['message'] ?? '抱歉,暂无物流记录' ];
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getTrace($data)
|
||||
{
|
||||
$express_no_data = $data[ "express_no_data" ];
|
||||
|
||||
$kd100_model = new Kd100();
|
||||
$kd100_config = $kd100_model->getKd100Config($express_no_data[ "site_id" ]);
|
||||
|
||||
if ($kd100_config[ "data" ][ "is_use" ]) {
|
||||
$express_no = $express_no_data[ "express_no_kd100" ];
|
||||
return $kd100_model->trace($data[ "code" ], $express_no, $data[ 'mobile' ], $express_no_data[ "site_id" ]);
|
||||
}
|
||||
|
||||
$kdbird_model = new Kdbird();
|
||||
$kdbird_config = $kdbird_model->getKdbirdConfig($express_no_data[ "site_id" ]);
|
||||
|
||||
if ($kdbird_config[ "data" ][ "is_use" ]) {
|
||||
$express_no = $express_no_data[ "express_no" ];
|
||||
return $kdbird_model->trace($data[ "code" ], $express_no, $express_no_data[ "site_id" ], $data[ 'mobile' ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
784
app/model/games/Games.php
Executable file
784
app/model/games/Games.php
Executable file
@@ -0,0 +1,784 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\games;
|
||||
|
||||
|
||||
use addon\coupon\model\Coupon;
|
||||
use app\model\BaseModel;
|
||||
use app\model\member\MemberAccount;
|
||||
use app\model\system\Cron;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\upload\Upload;
|
||||
|
||||
/**
|
||||
* 游戏
|
||||
*/
|
||||
class Games extends BaseModel
|
||||
{
|
||||
|
||||
public $status = [
|
||||
0 => '未开始',
|
||||
1 => '进行中',
|
||||
2 => '已结束',
|
||||
3 => '已关闭',
|
||||
];
|
||||
|
||||
/**
|
||||
* 添加游戏活动
|
||||
* @param $game_data
|
||||
* @param $award_json
|
||||
* @return array
|
||||
*/
|
||||
public function addGames($game_data, $award_json)
|
||||
{
|
||||
$time = time();
|
||||
$game_data[ 'create_time' ] = $time;
|
||||
|
||||
if ($time > $game_data[ 'start_time' ] && $time < $game_data[ 'end_time' ]) {
|
||||
$game_data[ 'status' ] = 1;
|
||||
} else {
|
||||
$game_data[ 'status' ] = 0;
|
||||
}
|
||||
model('promotion_games')->startTrans();
|
||||
try {
|
||||
|
||||
$game_id = model('promotion_games')->add($game_data);
|
||||
|
||||
//添加奖品
|
||||
$award_json_data = json_decode($award_json, true);
|
||||
$award_data = [];
|
||||
foreach ($award_json_data as $k => $v) {
|
||||
$award_img = '';
|
||||
if ($v[ 'award_type' ] == 3) {
|
||||
$award_img = $v['award_img'] ?? img("/public/static/img/game_coupon.png");
|
||||
} elseif ($v[ 'award_type' ] == 2) {
|
||||
$award_img = $v['award_img'] ?? img("/public/static/img/game_hongbao.png");
|
||||
} elseif ($v[ 'award_type' ] == 1) {
|
||||
$award_img = $v['award_img'] ?? img("/public/static/img/game_jifen.png");
|
||||
}
|
||||
$item = [
|
||||
'site_id' => $game_data[ 'site_id' ],
|
||||
'game_id' => $game_id,
|
||||
'award_name' => $v[ 'award_name' ],
|
||||
'award_img' => $award_img,
|
||||
'award_type' => $v[ 'award_type' ],
|
||||
'relate_id' => $v[ 'relate_id' ],
|
||||
'relate_name' => $v['relate_name'] ?? '',
|
||||
'point' => $v[ 'point' ],
|
||||
'balance' => $v[ 'balance' ],
|
||||
'award_num' => $v[ 'award_num' ],
|
||||
'award_winning_rate' => $v[ 'award_winning_rate' ],
|
||||
'remaining_num' => $v[ 'award_num' ],
|
||||
'no_winning_img' => $v['no_winning_img'] ?? '',
|
||||
];
|
||||
$award_data[ $k ] = $item;
|
||||
}
|
||||
|
||||
model('promotion_games_award')->addList($award_data);
|
||||
|
||||
$cron = new Cron();
|
||||
if ($game_data[ 'status' ] == 1) {//进行中
|
||||
|
||||
$cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
|
||||
} else {//未进行
|
||||
$cron->addCron(1, 0, "游戏活动开启", "OpenGame", $game_data[ 'start_time' ], $game_id);
|
||||
$cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
|
||||
}
|
||||
|
||||
model('promotion_games')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('promotion_games')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑游戏活动
|
||||
* @param $condition
|
||||
* @param $game_data
|
||||
* @param $award_json
|
||||
* @param $delete_award_ids
|
||||
* @return array
|
||||
*/
|
||||
public function editGames($condition, $game_data, $award_json, $delete_award_ids)
|
||||
{
|
||||
$game_info = model('promotion_games')->getInfo($condition, 'game_id,status');
|
||||
if (in_array($game_info[ 'status' ], [ 2, 3 ])) {
|
||||
return $this->error('', '已关闭或已结束的活动不能编辑');
|
||||
}
|
||||
|
||||
$game_id = $game_info[ 'game_id' ];
|
||||
|
||||
$time = time();
|
||||
$game_data[ 'update_time' ] = $time;
|
||||
|
||||
if ($time > $game_data[ 'start_time' ] && $time < $game_data[ 'end_time' ]) {
|
||||
$game_data[ 'status' ] = 1;
|
||||
} else {
|
||||
$game_data[ 'status' ] = 0;
|
||||
}
|
||||
model('promotion_games')->startTrans();
|
||||
try {
|
||||
$game_info = model('promotion_games')->getInfo($condition);
|
||||
if ($game_info[ 'no_winning_img' ] && $game_data[ 'no_winning_img' ] && $game_info[ 'no_winning_img' ] != $game_data[ 'no_winning_img' ]) {
|
||||
$upload_model = new Upload();
|
||||
$upload_model->deletePic($game_info[ 'no_winning_img' ], $game_data[ 'site_id' ]);
|
||||
}
|
||||
|
||||
model('promotion_games')->update($game_data, $condition);
|
||||
|
||||
if (!empty($delete_award_ids)) {
|
||||
model('promotion_games_award')->delete([ [ 'award_id', 'in', $delete_award_ids ] ]);
|
||||
}
|
||||
|
||||
//添加奖品
|
||||
$award_json_data = json_decode($award_json, true);
|
||||
foreach ($award_json_data as $k => $v) {
|
||||
$item = [
|
||||
'site_id' => $game_data[ 'site_id' ],
|
||||
'game_id' => $game_id,
|
||||
'award_name' => $v[ 'award_name' ],
|
||||
'award_img' => $v['award_img'] ?? '',
|
||||
'award_type' => $v[ 'award_type' ],
|
||||
'relate_id' => $v[ 'relate_id' ],
|
||||
'relate_name' => $v['relate_name'] ?? '',
|
||||
'point' => $v[ 'point' ],
|
||||
'balance' => $v[ 'balance' ],
|
||||
'award_num' => $v[ 'award_num' ],
|
||||
'award_winning_rate' => $v[ 'award_winning_rate' ],
|
||||
'remaining_num' => $v[ 'award_num' ],
|
||||
'no_winning_img' => $v['no_winning_img'] ?? '',
|
||||
];
|
||||
|
||||
if (isset($v[ 'award_id' ]) && $v[ 'award_id' ] > 0) {
|
||||
model('promotion_games_award')->update($item, [ [ 'award_id', '=', $v[ 'award_id' ] ] ]);
|
||||
} else {
|
||||
model('promotion_games_award')->add($item);
|
||||
}
|
||||
}
|
||||
|
||||
$cron = new Cron();
|
||||
|
||||
$cron->deleteCron([ [ 'event', '=', 'OpenGame' ], [ 'relate_id', '=', $game_id ] ]);
|
||||
$cron->deleteCron([ [ 'event', '=', 'CloseGame' ], [ 'relate_id', '=', $game_id ] ]);
|
||||
|
||||
if ($game_data[ 'status' ] == 1) {//进行中
|
||||
|
||||
$cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
|
||||
} else {//未进行
|
||||
$cron->addCron(1, 0, "游戏活动开启", "OpenGame", $game_data[ 'start_time' ], $game_id);
|
||||
$cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
|
||||
}
|
||||
|
||||
model('promotion_games')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('promotion_games')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取游戏奖项
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getGameAward($condition = [], $field = '*')
|
||||
{
|
||||
$data = model('promotion_games_award')->getList($condition, $field);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取游戏信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getGamesInfo($condition, $field = '*')
|
||||
{
|
||||
$res = model('promotion_games')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取游戏详情
|
||||
* @param int $site_id
|
||||
*/
|
||||
public function getGamesDetail($site_id, $game_id)
|
||||
{
|
||||
$res = model('promotion_games')->getInfo([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ], '*');
|
||||
|
||||
if (!empty($res)) {
|
||||
$game_award = model('promotion_games_award')->getList([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ], '*');
|
||||
$res[ 'game_award' ] = $game_award;
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除游戏
|
||||
* @param $site_id
|
||||
* @param $game_id
|
||||
* @return array
|
||||
*/
|
||||
public function deleteGames($site_id, $game_id)
|
||||
{
|
||||
model('promotion_games')->startTrans();
|
||||
try {
|
||||
|
||||
model('promotion_games')->delete([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ]);
|
||||
model('promotion_games_award')->delete([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ]);
|
||||
model('promotion_games')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('promotion_games')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取游戏列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getGamesList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
|
||||
$list = model('promotion_games')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取游戏分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getGamesPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('promotion_games')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭游戏
|
||||
* @param $site_id
|
||||
* @param $game_id
|
||||
* @return array
|
||||
*/
|
||||
public function finishGames($site_id, $game_id)
|
||||
{
|
||||
//团购信息
|
||||
$game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ] ], 'status');
|
||||
if (!empty($game_info)) {
|
||||
|
||||
if ($game_info[ 'status' ] != 3) {
|
||||
$res = model('promotion_games')->update([ 'status' => 3 ], [ [ 'game_id', '=', $game_id ] ]);
|
||||
// if ($res) {
|
||||
// $cron = new Cron();
|
||||
// $cron->deleteCron([ [ 'event', '=', 'OpenGame' ], [ 'relate_id', '=', $game_id ] ]);
|
||||
// $cron->deleteCron([ [ 'event', '=', 'CloseGame' ], [ 'relate_id', '=', $game_id ] ]);
|
||||
// }
|
||||
return $this->success($res);
|
||||
} else {
|
||||
$this->error('', '该游戏已关闭');
|
||||
}
|
||||
|
||||
} else {
|
||||
$this->error('', '该游戏不存在');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 开启游戏
|
||||
* @param $game_id
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function cronOpenGames($game_id)
|
||||
{
|
||||
$game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ] ], 'start_time,status');
|
||||
if (!empty($game_info)) {
|
||||
|
||||
if ($game_info[ 'start_time' ] <= time() && $game_info[ 'status' ] == 0) {
|
||||
$res = model('promotion_games')->update([ 'status' => 1 ], [ [ 'game_id', '=', $game_id ] ]);
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error("", "游戏已开启或者关闭");
|
||||
}
|
||||
|
||||
} else {
|
||||
return $this->error("", "游戏不存在");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启游戏
|
||||
* @param $game_id
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function startGames($game_id)
|
||||
{
|
||||
$game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ] ], 'end_time,status');
|
||||
if (!empty($game_info)) {
|
||||
|
||||
if ($game_info[ 'end_time' ] >= time()) {
|
||||
$res = model('promotion_games')->update([ 'status' => 1 ], [ [ 'game_id', '=', $game_id ] ]);
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error("", "游戏已结束");
|
||||
}
|
||||
|
||||
} else {
|
||||
return $this->error("", "游戏不存在");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭游戏
|
||||
* @param $game_id
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function cronCloseGames($game_id)
|
||||
{
|
||||
$game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ] ], 'start_time,status,end_time');
|
||||
if (!empty($game_info)) {
|
||||
if ($game_info[ 'status' ] != 2 && $game_info['end_time'] <= time()) {
|
||||
$res = model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $game_id ] ]);
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error("", "该游戏已结束");
|
||||
}
|
||||
} else {
|
||||
return $this->error("", "游戏不存在");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽奖
|
||||
* @param $game_id
|
||||
* @param $member_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function lottery($game_id, $member_id, $site_id)
|
||||
{
|
||||
$game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if (empty($game_info)) return $this->error("", "未获取到游戏信息");
|
||||
|
||||
if ($game_info[ 'status' ] == 0 && $game_info[ 'game_type' ] != 'scenefestival') return $this->error("", "游戏尚未开始");
|
||||
if ($game_info[ 'status' ] == 2 || $game_info[ 'status' ] == 3) return $this->error("", "游戏已经结束");
|
||||
|
||||
$member_info = model('member')->getInfo([ [ 'member_id', '=', $member_id ], [ 'site_id', '=', $site_id ], [ 'status', '=', 1 ] ], 'nickname,member_level,point');
|
||||
if (empty($member_info)) return $this->error("", "未获取到会员信息");
|
||||
|
||||
if (!empty($game_info[ 'level_id' ])) {
|
||||
$level = explode(',', $game_info[ 'level_id' ]);
|
||||
if (!in_array($member_info[ 'member_level' ], $level)) {
|
||||
return $this->error("", "只有{$game_info['level_name']}等级的会员可参与该活动");
|
||||
}
|
||||
}
|
||||
|
||||
if ($game_info[ 'join_type' ]) {
|
||||
// 每天
|
||||
$tody_start = strtotime(date("Y-m-d"), time());
|
||||
$luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ], [ 'create_time', 'between', [ $tody_start, time() ] ] ]);
|
||||
if ($luck_draw_num > $game_info[ 'join_frequency' ]) return $this->error("", "您今日的抽奖次数已用完");
|
||||
} else {
|
||||
// 全程
|
||||
$luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ] ]);
|
||||
if ($luck_draw_num > $game_info[ 'join_frequency' ]) return $this->error("", "您的抽奖次数已用完");
|
||||
}
|
||||
|
||||
if ($game_info[ 'points' ] > 0 && $member_info[ 'point' ] < $game_info[ 'points' ]) return $this->error("", "积分不足");
|
||||
|
||||
$lottery_result = $this->lotteryCalculate($game_info);
|
||||
|
||||
model('promotion_games')->startTrans();
|
||||
try {
|
||||
$meber_account = new MemberAccount();
|
||||
if ($game_info[ 'points' ] > 0) {
|
||||
// 扣除积分
|
||||
$meber_account->addMemberAccount($site_id, $member_id, 'point', -$game_info[ 'points' ], $game_info[ 'game_type' ], $game_id, "参与活动扣除");
|
||||
}
|
||||
model('promotion_games')->setInc([ [ 'game_id', '=', $game_id ] ], 'join_num'); // 抽奖人数
|
||||
$record = [
|
||||
'site_id' => $site_id,
|
||||
'game_id' => $game_id,
|
||||
'game_type' => $game_info[ 'game_type' ],
|
||||
'member_id' => $member_id,
|
||||
'member_nick_name' => $member_info[ 'nickname' ],
|
||||
'points' => $game_info[ 'points' ],
|
||||
'create_time' => time()
|
||||
];
|
||||
|
||||
if ($lottery_result[ 'is_winning' ]) {
|
||||
switch ( $lottery_result[ 'award_type' ] ) {
|
||||
case 1:
|
||||
// 积分
|
||||
$meber_account->addMemberAccount($site_id, $member_id, 'point', $lottery_result[ 'point' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
|
||||
break;
|
||||
case 2:
|
||||
// 余额
|
||||
$meber_account->addMemberAccount($site_id, $member_id, 'balance', $lottery_result[ 'balance' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
|
||||
break;
|
||||
case 3:
|
||||
// 优惠券
|
||||
$coupon = new Coupon();
|
||||
$receive_res = $coupon->giveCoupon([ [ 'coupon_type_id' => $lottery_result[ 'relate_id' ], 'num' => 1 ] ], $site_id, $member_id, Coupon::GET_TYPE_ACTIVITY_GIVE);
|
||||
// 如果优惠券发放失败则本次抽奖为未中奖
|
||||
if ($receive_res[ 'code' ] < 0) {
|
||||
$lottery_result = [ 'is_winning' => 0 ];
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
// 赠品
|
||||
break;
|
||||
}
|
||||
|
||||
if ($lottery_result[ 'is_winning' ]) {
|
||||
$record[ 'is_winning' ] = 1;
|
||||
$record[ 'award_id' ] = $lottery_result[ 'award_id' ];
|
||||
$record[ 'award_name' ] = $lottery_result[ 'award_name' ];
|
||||
$record[ 'award_type' ] = $lottery_result[ 'award_type' ];
|
||||
$record[ 'relate_id' ] = $lottery_result[ 'relate_id' ];
|
||||
$record[ 'relate_name' ] = $lottery_result[ 'relate_name' ];
|
||||
$record[ 'point' ] = $lottery_result[ 'point' ];
|
||||
$record[ 'balance' ] = $lottery_result[ 'balance' ];
|
||||
|
||||
model('promotion_games')->setInc([ [ 'game_id', '=', $game_id ] ], 'winning_num'); // 中奖人数
|
||||
|
||||
model('promotion_games_award')->setDec([ [ 'award_id', '=', $lottery_result[ 'award_id' ] ] ], 'remaining_num'); // 剩余数量
|
||||
model('promotion_games_award')->setInc([ [ 'award_id', '=', $lottery_result[ 'award_id' ] ] ], 'receive_num'); // 已领取数量
|
||||
}
|
||||
}
|
||||
model('promotion_games_draw_record')->add($record);
|
||||
model('promotion_games')->commit();
|
||||
return $this->success($lottery_result);
|
||||
} catch (\Exception $e) {
|
||||
model('promotion_games')->rollback();
|
||||
return $this->error("", $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 节日有礼领取
|
||||
* @param $game_id
|
||||
* @param $member_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function receive($game_id, $member_id, $site_id)
|
||||
{
|
||||
$game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if (empty($game_info)) return $this->error("", "未获取到游戏信息");
|
||||
|
||||
if ($game_info[ 'status' ] == 2 || $game_info[ 'status' ] == 3) return $this->error("", "游戏已经结束");
|
||||
|
||||
$member_info = model('member')->getInfo([ [ 'member_id', '=', $member_id ], [ 'site_id', '=', $site_id ], [ 'status', '=', 1 ] ], 'nickname,member_level,point');
|
||||
if (empty($member_info)) return $this->error("", "未获取到会员信息");
|
||||
|
||||
if (!empty($game_info[ 'level_id' ])) {
|
||||
$level = explode(',', $game_info[ 'level_id' ]);
|
||||
if (!in_array($member_info[ 'member_level' ], $level)) {
|
||||
return $this->error("", "只有{$game_info['level_name']}等级的会员可参与该活动");
|
||||
}
|
||||
}
|
||||
|
||||
model('promotion_games')->startTrans();
|
||||
try {
|
||||
$meber_account = new MemberAccount();
|
||||
$award_list = model('promotion_games_award')->getList([ [ 'game_id', '=', $game_id ], [ 'remaining_num', '>', 0 ] ], 'award_id,award_name,award_type,relate_id,relate_name,point,balance');
|
||||
$record = [
|
||||
'site_id' => $site_id,
|
||||
'game_id' => $game_id,
|
||||
'game_type' => $game_info[ 'game_type' ],
|
||||
'member_id' => $member_id,
|
||||
'member_nick_name' => $member_info[ 'nickname' ],
|
||||
'points' => $game_info[ 'points' ],
|
||||
'create_time' => time()
|
||||
];
|
||||
|
||||
if (!empty($award_list)) {
|
||||
foreach ($award_list as $v) {
|
||||
switch ( $v[ 'award_type' ] ) {
|
||||
case 1:
|
||||
// 积分
|
||||
$meber_account->addMemberAccount($site_id, $member_id, 'point', $v[ 'point' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
|
||||
break;
|
||||
case 2:
|
||||
// 余额
|
||||
$meber_account->addMemberAccount($site_id, $member_id, 'balance', $v[ 'balance' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
|
||||
break;
|
||||
case 3:
|
||||
// 优惠券
|
||||
$coupon = new Coupon();
|
||||
$coupon->giveCoupon([['coupon_type_id' => $v[ 'relate_id' ], 'num' => 1]], $site_id, $member_id, Coupon::GET_TYPE_ACTIVITY_GIVE);
|
||||
break;
|
||||
case 4:
|
||||
// 赠品
|
||||
break;
|
||||
}
|
||||
$record[ 'is_winning' ] = 1;
|
||||
$record[ 'award_id' ] = $v[ 'award_id' ];
|
||||
$record[ 'award_name' ] = $v[ 'award_name' ];
|
||||
$record[ 'award_type' ] = $v[ 'award_type' ];
|
||||
$record[ 'relate_id' ] = $v[ 'relate_id' ];
|
||||
$record[ 'relate_name' ] = $v[ 'relate_name' ];
|
||||
$record[ 'point' ] = $v[ 'point' ];
|
||||
$record[ 'balance' ] = $v[ 'balance' ];
|
||||
|
||||
model('promotion_games_award')->setDec([ [ 'award_id', '=', $v[ 'award_id' ] ] ], 'remaining_num'); // 剩余数量
|
||||
model('promotion_games_award')->setInc([ [ 'award_id', '=', $v[ 'award_id' ] ] ], 'receive_num'); // 已领取数量
|
||||
model('promotion_games_draw_record')->add($record);
|
||||
}
|
||||
} else {
|
||||
model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $game_id ] ]);
|
||||
}
|
||||
|
||||
model('promotion_games')->commit();
|
||||
return $this->success([ 'status' => 1 ]);
|
||||
} catch (\Exception $e) {
|
||||
model('promotion_games')->rollback();
|
||||
return $this->error("", $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 中奖计算
|
||||
*/
|
||||
private function lotteryCalculate($data)
|
||||
{
|
||||
$rand = mt_rand(1, 10000);
|
||||
$result = [ 'is_winning' => 0 ];
|
||||
if ($data[ 'winning_rate' ] > 0 && $rand <= ( $data[ 'winning_rate' ] * 100 )) {
|
||||
$award_list = model('promotion_games_award')->getList([ [ 'game_id', '=', $data[ 'game_id' ] ], [ 'remaining_num', '>', 0 ] ], 'award_id,award_name,award_type,relate_id,relate_name,point,balance,award_winning_rate', 'award_winning_rate asc');
|
||||
if (!empty($award_list)) {
|
||||
$rate_arr = [];
|
||||
foreach ($award_list as $item) {
|
||||
$rate_arr[] = $item[ 'award_winning_rate' ];
|
||||
}
|
||||
$key = $this->get_rand($rate_arr);
|
||||
if (isset($award_list[ $key ])) {
|
||||
$result = $award_list[ $key ];
|
||||
$result[ 'is_winning' ] = 1;
|
||||
unset($result[ 'award_winning_rate' ]);
|
||||
}
|
||||
} else {
|
||||
model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $data[ 'game_id' ] ] ]);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 奖品计算
|
||||
*/
|
||||
private function awardCalculate($data)
|
||||
{
|
||||
$award_list = model('promotion_games_award')->getList([ [ 'game_id', '=', $data[ 'game_id' ] ], [ 'remaining_num', '>', 0 ] ], 'award_id,award_name,award_type,relate_id,relate_name,point,balance,award_winning_rate', 'award_winning_rate asc');
|
||||
if (!empty($award_list)) {
|
||||
$rate_arr = [];
|
||||
foreach ($award_list as $item) {
|
||||
$rate_arr[] = $item[ 'award_winning_rate' ];
|
||||
}
|
||||
$key = $this->get_rand($rate_arr);
|
||||
if (isset($award_list[ $key ])) {
|
||||
$result = $award_list[ $key ];
|
||||
$result[ 'is_winning' ] = 1;
|
||||
unset($result[ 'award_winning_rate' ]);
|
||||
}
|
||||
} else {
|
||||
model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $data[ 'game_id' ] ] ]);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机奖励
|
||||
* @param $proArr
|
||||
* @return int|string
|
||||
*/
|
||||
private function get_rand($proArr)
|
||||
{
|
||||
$result = '';
|
||||
//总权重
|
||||
$proSum = array_sum($proArr);
|
||||
//概率数组循环
|
||||
foreach ($proArr as $key => $proCur) {
|
||||
//如果权重为0 不参与计算
|
||||
if($proCur == 0) continue;
|
||||
$randNum = mt_rand(1, $proSum);
|
||||
if ($randNum <= $proCur) {
|
||||
$result = $key;
|
||||
break;
|
||||
} else {
|
||||
$proSum -= $proCur;
|
||||
}
|
||||
}
|
||||
unset ($proArr);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员剩余可抽奖次数
|
||||
* @param $game_id
|
||||
* @param $member_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberSurplusNum($game_id, $member_id, $site_id)
|
||||
{
|
||||
$num = 0;
|
||||
$game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ], [ 'status', '=', 1 ] ], 'join_type,join_frequency');
|
||||
if (!empty($game_info)) {
|
||||
if ($game_info[ 'join_type' ] == 1) {
|
||||
// 每天
|
||||
$today_start = strtotime(date("Y-m-d"), time());
|
||||
$luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ], [ 'create_time', 'between', [ $today_start, time() ] ] ]);
|
||||
if ($luck_draw_num < $game_info[ 'join_frequency' ]) {
|
||||
$num = $game_info[ 'join_frequency' ] - $luck_draw_num;
|
||||
}
|
||||
} else {
|
||||
// 全程
|
||||
$luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ] ]);
|
||||
if ($luck_draw_num < $game_info[ 'join_frequency' ]) {
|
||||
$num = $game_info[ 'join_frequency' ] - $luck_draw_num;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success($num);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 游戏推广二维码
|
||||
* @param $game_id
|
||||
* @param $game_name
|
||||
* @param $url
|
||||
* @param $site_id
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public function qrcode($game_id, $game_name, $url, $site_id, $type = "create")
|
||||
{
|
||||
$data = [
|
||||
'site_id' => $site_id,
|
||||
'app_type' => "all", // all为全部
|
||||
'type' => $type, // 类型 create创建 get获取
|
||||
'data' => [
|
||||
"id" => $game_id
|
||||
],
|
||||
'page' => $url,
|
||||
'qrcode_path' => 'upload/qrcode/games',
|
||||
'qrcode_name' => "games_qrcode_" . $game_id
|
||||
];
|
||||
|
||||
event('Qrcode', $data, true);
|
||||
$app_type_list = config('app_type');
|
||||
$path = [];
|
||||
foreach ($app_type_list as $k => $v) {
|
||||
switch ( $k ) {
|
||||
case 'h5':
|
||||
$wap_domain = getH5Domain();
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?id=' . $game_id;
|
||||
$path[ $k ][ 'img' ] = "upload/qrcode/games/games_qrcode_" . $game_id . "_" . $k . ".png";
|
||||
break;
|
||||
case 'weapp' :
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
|
||||
if (!empty($res[ 'data' ])) {
|
||||
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信小程序';
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
|
||||
}
|
||||
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信小程序';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'wechat' :
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
|
||||
if (!empty($res[ 'data' ])) {
|
||||
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信公众号';
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
|
||||
}
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信公众号';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$return = [
|
||||
'path' => $path,
|
||||
'game_name' => $game_name,
|
||||
];
|
||||
|
||||
return $this->success($return);
|
||||
}
|
||||
|
||||
public function gameUrlQrcode($page, $qrcode_param, $promotion_type, $app_type, $site_id)
|
||||
{
|
||||
$params = [
|
||||
'site_id' => $site_id,
|
||||
'data' => $qrcode_param,
|
||||
'page' => $page,
|
||||
'promotion_type' => $promotion_type,
|
||||
'app_type' => $app_type,
|
||||
'h5_path' => $page . '?id=' . $qrcode_param[ 'id' ],
|
||||
'qrcode_path' => 'upload/qrcode/games',
|
||||
'qrcode_name' => 'games_qrcode_' . $promotion_type . '_' . $qrcode_param[ 'id' ] . '_' . $site_id,
|
||||
];
|
||||
$solitaire = event('PromotionQrcode', $params, true);
|
||||
return $this->success($solitaire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取首条数据
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getFirstInfo($condition, $field = '*', $order = "")
|
||||
{
|
||||
$data = model('promotion_games')->getFirstData($condition, $field, $order);
|
||||
return $this->success($data);
|
||||
}
|
||||
}
|
||||
51
app/model/games/Record.php
Executable file
51
app/model/games/Record.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\games;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 抽奖记录
|
||||
*/
|
||||
class Record extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取抽奖记录列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getGamesDrawRecordList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('promotion_games_draw_record')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取抽奖记录分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getGamesDrawRecordPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('promotion_games_draw_record')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
118
app/model/goods/Batch.php
Executable file
118
app/model/goods/Batch.php
Executable file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\storegoods\StoreGoods;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 商品批量设置
|
||||
*/
|
||||
class Batch extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 批量设置
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function setPrice($params)
|
||||
{
|
||||
$site_id = $params[ 'site_id' ];
|
||||
$type = $params[ 'type' ] ?? '';
|
||||
$price = $params[ 'price' ] ?? 0;
|
||||
$price_type = $params[ 'price_type' ] ?? '';
|
||||
$sign = $params[ 'sign' ] ?? '';
|
||||
$goods_ids = $params[ 'goods_ids' ] ?? '';
|
||||
$precise = $params[ 'precise' ] ?? '';
|
||||
|
||||
$calculate_price_type = $params[ 'calculate_price_type' ] ?? '';
|
||||
$condition = array (
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'goods_id', 'in', $goods_ids ]
|
||||
);
|
||||
|
||||
$price_field_data = array (
|
||||
'sale' => 'price',
|
||||
'cost' => 'cost_price',
|
||||
'market' => 'market_price'
|
||||
);
|
||||
$price_field = $price_field_data[ $price_type ] ?? '';
|
||||
|
||||
$todo_price = 0;
|
||||
switch ( $type ) {
|
||||
case 'money'://固定金额
|
||||
//批量给商品设置价格
|
||||
$todo_price = $price;
|
||||
$update_data = array (
|
||||
$price_field => $price
|
||||
);
|
||||
break;
|
||||
case 'calculate'://公式算法
|
||||
$calculate_price_field = $price_field_data[ $calculate_price_type ] ?? '';
|
||||
switch ( $sign ) {
|
||||
case 'add'://加法
|
||||
$sign_str = '+';
|
||||
break;
|
||||
case 'subtract'://减法
|
||||
$sign_str = '-';
|
||||
break;
|
||||
case 'multiply'://乘法
|
||||
$sign_str = '*';
|
||||
break;
|
||||
case 'division'://除法
|
||||
$sign_str = '/';
|
||||
break;
|
||||
}
|
||||
$calculate_str = $calculate_price_field . $sign_str . $price;
|
||||
switch ( $precise ) {
|
||||
case 1://全部保留
|
||||
$calculate_str = 'FLOOR((' . $calculate_str . ')*100)/100';
|
||||
break;
|
||||
case 2://抹分
|
||||
$calculate_str = 'FLOOR((' . $calculate_str . ')*10)/10';
|
||||
break;
|
||||
case 3://抹角
|
||||
$calculate_str = 'FLOOR(' . $calculate_str . ')';
|
||||
break;
|
||||
case 4://四舍五入到分
|
||||
$calculate_str = 'ROUND(' . $calculate_str . ', 2)';
|
||||
break;
|
||||
case 5://四舍五入到角
|
||||
$calculate_str = 'ROUND(' . $calculate_str . ', 1)';
|
||||
break;
|
||||
case 6://四舍五入到元
|
||||
$calculate_str = 'ROUND(' . $calculate_str . ')';
|
||||
break;
|
||||
}
|
||||
//todo mysql的 round函数和floor函数
|
||||
$update_data[ $price_field ] = Db::raw($calculate_str);
|
||||
$todo_price = $calculate_str;
|
||||
break;
|
||||
}
|
||||
|
||||
model('goods')->update($update_data, $condition);
|
||||
//只有销售价被改动如果存在就忽略设置discount_price才会收到影响
|
||||
if ($price_type == 'sale') {
|
||||
//鉴于限时折扣活动的复杂性(商品关联限时折扣活动表,如果存在就忽略设置discount_price)
|
||||
$sku_update_data[ 'discount_price' ] = Db::raw('if(discount_price = price,' . $todo_price . ',discount_price)');
|
||||
$sku_update_data[ $price_field ] = $update_data[ $price_field ];
|
||||
model('goods_sku')->update($sku_update_data, $condition);
|
||||
}
|
||||
//如果销售价被改动,默认门店的价格也都会被改动
|
||||
if (in_array($price_type, [ 'sale', 'cost' ])) {
|
||||
$store_goods_model = new StoreGoods();
|
||||
$store_goods_model->syncGoodsData([ 'update_data' => $update_data, 'condition' => $condition, 'site_id' => $site_id ]);
|
||||
}
|
||||
return $this->success($update_data);
|
||||
}
|
||||
}
|
||||
207
app/model/goods/Cart.php
Executable file
207
app/model/goods/Cart.php
Executable file
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Stat;
|
||||
|
||||
/**
|
||||
* 购物车
|
||||
*/
|
||||
class Cart extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addCart($data)
|
||||
{
|
||||
$cart_info = model("goods_cart")->getInfo([ [ 'sku_id', '=', $data[ 'sku_id' ] ], [ 'member_id', '=', $data[ 'member_id' ] ] ], 'cart_id, num');
|
||||
if (!empty($cart_info)) {
|
||||
$update = [
|
||||
'num' => $cart_info[ 'num' ] + $data[ 'num' ]
|
||||
];
|
||||
|
||||
$goods_stock = model("goods_sku")->getValue([['sku_id','=',$data['sku_id']]],'stock');
|
||||
if($update['num'] > $goods_stock){
|
||||
$update['num'] = $goods_stock; //购物车数量不能大于库存总数
|
||||
}
|
||||
|
||||
if (isset($data[ 'form_data' ]) && !empty($data[ 'form_data' ])) $update[ 'form_data' ] = $data[ 'form_data' ];
|
||||
|
||||
$res = model("goods_cart")->update($update, [ [ 'cart_id', '=', $cart_info[ 'cart_id' ] ] ]);
|
||||
|
||||
$stat_model = new Stat();
|
||||
$stat_model->switchStat([ 'type' => 'goods_cart', 'data' => [
|
||||
'goods_cart_count' => $data[ 'num' ],
|
||||
'site_id' => $data[ 'site_id' ]
|
||||
] ]);
|
||||
} else {
|
||||
$res = model("goods_cart")->add($data);
|
||||
$stat_model = new Stat();
|
||||
$stat_model->switchStat([ 'type' => 'goods_cart', 'data' => [
|
||||
'goods_cart_count' => $data[ 'num' ],
|
||||
'site_id' => $data[ 'site_id' ]
|
||||
] ]);
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新购物车商品数量
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editCart($data)
|
||||
{
|
||||
$update = [
|
||||
'num' => $data[ 'num' ]
|
||||
];
|
||||
if (isset($data[ 'form_data' ]) && !empty($data[ 'form_data' ])) $update[ 'form_data' ] = $data[ 'form_data' ];
|
||||
$condition = [ [ 'cart_id', '=', $data[ 'cart_id' ] ], [ 'member_id', '=', $data[ 'member_id' ] ] ];
|
||||
$info = model("goods_cart")->getInfo($condition, 'site_id,num');
|
||||
if (empty($info))
|
||||
return $this->error();
|
||||
|
||||
$res = model("goods_cart")->update($update, $condition);
|
||||
$stat_model = new Stat();
|
||||
$stat_model->switchStat([ 'type' => 'goods_cart', 'data' => [
|
||||
'goods_cart_count' => $data[ 'num' ] - $info[ 'num' ],
|
||||
'site_id' => $info[ 'site_id' ]
|
||||
] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除购物车商品项(可以多项)
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function deleteCart($data)
|
||||
{
|
||||
$res = model("goods_cart")->delete([ [ 'cart_id', 'in', explode(',', $data[ 'cart_id' ]) ], [ 'member_id', '=', $data[ 'member_id' ] ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function clearCart($data)
|
||||
{
|
||||
$res = model("goods_cart")->delete([ [ 'member_id', '=', $data[ 'member_id' ] ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员购物车
|
||||
* @param $member_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getCart($member_id, $site_id)
|
||||
{
|
||||
$field = 'ngc.cart_id, ngc.site_id, ngc.member_id, ngc.sku_id, ngc.num, ngs.sku_name,ngs.goods_id,
|
||||
ngs.sku_no, ngs.sku_spec_format,ngs.price,ngs.market_price, ngs.goods_spec_format,
|
||||
ngs.discount_price, ngs.promotion_type, ngs.start_time, ngs.end_time, ngs.stock,
|
||||
ngs.sku_image, ngs.sku_images, ngs.goods_state, ngs.goods_stock_alarm, ngs.is_virtual, ngs.goods_name,
|
||||
ngs.virtual_indate, ngs.is_free_shipping, ngs.shipping_template, ngs.unit, ngs.introduction,ngs.sku_spec_format, ngs.keywords, ngs.max_buy, ngs.min_buy, ns.site_name, ngs.is_limit, ngs.limit_type';
|
||||
$alias = 'ngc';
|
||||
$join = [
|
||||
[
|
||||
'goods_sku ngs',
|
||||
'ngc.sku_id = ngs.sku_id',
|
||||
'inner'
|
||||
],
|
||||
[
|
||||
'site ns',
|
||||
'ngc.site_id = ns.site_id',
|
||||
'inner'
|
||||
],
|
||||
];
|
||||
$list = model("goods_cart")->getList([ [ 'ngc.member_id', '=', $member_id ], [ 'ngc.site_id', '=', $site_id ], [ 'ngs.is_delete', '=', 0 ] ], $field, 'ngc.cart_id desc', $alias, $join);
|
||||
foreach ($list as $k => $v) {
|
||||
$list[ $k ][ 'stock' ] = numberFormat($list[ $k ][ 'stock' ]);
|
||||
}
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车数量
|
||||
* @param $member_id
|
||||
* @return array
|
||||
*/
|
||||
public function getCartCount($member_id)
|
||||
{
|
||||
$list = model("goods_cart")->getCount([ [ 'member_id', '=', $member_id ] ]);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车数量
|
||||
* @param $condition
|
||||
* @param $field
|
||||
* @param string $alias
|
||||
* @param null $join
|
||||
* @return array
|
||||
*/
|
||||
public function getCartSum($condition, $field, $alias = 'a', $join = null)
|
||||
{
|
||||
$sum = model("goods_cart")->getSum($condition, $field, $alias, $join);
|
||||
return $this->success($sum);
|
||||
}
|
||||
|
||||
public function getCartList($condition = [], $field = 'cart_id,site_id,member_id,sku_id,num', $order = 'cart_id desc')
|
||||
{
|
||||
$alias = 'gc';
|
||||
$join = [
|
||||
[
|
||||
'goods_sku gs',
|
||||
'gc.sku_id = gs.sku_id',
|
||||
'inner'
|
||||
]
|
||||
];
|
||||
|
||||
$list = model("goods_cart")->getList($condition, $field, $order, $alias, $join);
|
||||
foreach ($list as $k => $v) {
|
||||
if (isset($v[ 'stock' ])) {
|
||||
$list[ $k ][ 'stock' ] = numberFormat($list[ $k ][ 'stock' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改购物车sku
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editCartSku($data)
|
||||
{
|
||||
$info = model("goods_cart")->getInfo([ [ 'cart_id', '=', $data[ 'cart_id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ], [ 'member_id', '=', $data[ 'member_id' ] ] ], 'sku_id');
|
||||
if (empty($info)) return $this->error();
|
||||
|
||||
if ($info[ 'sku_id' ] == $data[ 'sku_id' ]) {
|
||||
model("goods_cart")->update([ 'num' => $data[ 'num' ] ], [ [ 'cart_id', '=', $data[ 'cart_id' ] ] ]);
|
||||
} else {
|
||||
$cart_info = model("goods_cart")->getInfo([ [ 'sku_id', '=', $data[ 'sku_id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ], [ 'member_id', '=', $data[ 'member_id' ] ] ], 'cart_id');
|
||||
if (!empty($cart_info)) {
|
||||
model("goods_cart")->delete([ [ 'cart_id', '=', $cart_info[ 'cart_id' ] ] ]);
|
||||
}
|
||||
model("goods_cart")->update([ 'num' => $data[ 'num' ], 'sku_id' => $data[ 'sku_id' ] ], [ [ 'cart_id', '=', $data[ 'cart_id' ] ] ]);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
51
app/model/goods/Config.php
Executable file
51
app/model/goods/Config.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Document as DocumentModel;
|
||||
use app\model\system\Config as SystemConfig;
|
||||
|
||||
/**
|
||||
* 商品设置
|
||||
*/
|
||||
class Config extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取售后保障设置
|
||||
*/
|
||||
public function getAfterSaleConfig($site_id)
|
||||
{
|
||||
$document = new DocumentModel();
|
||||
$info = $document->getDocument([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'document_key', '=', 'GOODS_AFTER_SALE'] ]);
|
||||
$config = ( new SystemConfig() )->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'GOODS_AFTER_SALE' ] ]);
|
||||
$info[ 'data' ][ 'is_display' ] = empty($config[ 'data' ][ 'value' ]) ? 0 : $config[ 'data' ][ 'value' ][ 'is_display' ];
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置售后保障
|
||||
* @param $title
|
||||
* @param $content
|
||||
* @param $site_id
|
||||
* @param int $is_display
|
||||
* @return array
|
||||
*/
|
||||
public function setAfterSaleConfig($title, $content, $site_id, $is_display = 0)
|
||||
{
|
||||
$document = new DocumentModel();
|
||||
$res = $document->setDocument($title, $content, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'document_key', '=', 'GOODS_AFTER_SALE'] ]);
|
||||
( new SystemConfig() )->setConfig([ 'is_display' => $is_display ], '售后保障是否显示', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'GOODS_AFTER_SALE' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
}
|
||||
2501
app/model/goods/Goods.php
Executable file
2501
app/model/goods/Goods.php
Executable file
File diff suppressed because it is too large
Load Diff
587
app/model/goods/GoodsApi.php
Executable file
587
app/model/goods/GoodsApi.php
Executable file
@@ -0,0 +1,587 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use addon\bundling\model\Bundling;
|
||||
use addon\coupon\model\Coupon;
|
||||
use addon\fenxiao\model\FenxiaoGoods;
|
||||
use addon\manjian\model\Manjian as ManjianModel;
|
||||
use addon\supermember\model\MemberCard as MemberCardModel;
|
||||
use app\dict\goods\GoodsDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\express\Config as ExpressConfig;
|
||||
use app\model\goods\GoodsCollect as GoodsCollectModel;
|
||||
use app\model\goods\GoodsEvaluate as GoodsEvaluateModel;
|
||||
use app\model\web\Config as ConfigModel;
|
||||
|
||||
/**
|
||||
* 商品
|
||||
*/
|
||||
class GoodsApi extends BaseModel
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* api请求获取商品详情
|
||||
* @param $site_id
|
||||
* @param $sku_id
|
||||
* @param $goods_id
|
||||
* @param $member_id
|
||||
* @param $store_id
|
||||
* @param $store_data
|
||||
* @return array
|
||||
*/
|
||||
public function getGoodsSkuDetail($site_id, $sku_id, $goods_id, $member_id, $store_id, $store_data, $sale_channel = 'all')
|
||||
{
|
||||
$goods_model = new Goods();
|
||||
if (empty($sku_id) && !empty($goods_id)) {
|
||||
$goods_sku_id = model("goods")->getInfo([['goods_id', '=', $goods_id]], 'sku_id');
|
||||
if (empty($goods_sku_id)) {
|
||||
return $this->error("商品不存在");
|
||||
}
|
||||
$sku_id = $goods_sku_id['sku_id'];
|
||||
}
|
||||
|
||||
$condition = [
|
||||
['gs.sku_id', '=', $sku_id],
|
||||
['gs.is_delete', '=', 0],
|
||||
['g.sale_channel', 'in', $sale_channel],
|
||||
];
|
||||
|
||||
$field = 'gs.goods_id,gs.sku_id,gs.qr_id,gs.goods_name,gs.sku_name,gs.is_consume_discount,gs.discount_config,gs.discount_method,gs.sku_spec_format,gs.price,gs.market_price,gs.member_price,gs.discount_price,gs.promotion_type,gs.start_time
|
||||
,gs.end_time,gs.stock,gs.click_num,(g.sale_num + g.virtual_sale) as sale_num,gs.collect_num,gs.sku_image,gs.sku_images
|
||||
,gs.goods_content,gs.goods_state,gs.is_free_shipping,gs.goods_spec_format,gs.goods_attr_format,gs.introduction,gs.unit,gs.video_url
|
||||
,gs.is_virtual,gs.goods_service_ids,gs.max_buy,gs.min_buy,gs.is_limit,gs.limit_type,gs.support_trade_type,g.goods_image,g.keywords,g.stock_show,g.sale_show,g.market_price_show,g.promotion_addon,g.barrage_show,g.evaluate,g.goods_class,g.sale_store,g.sale_channel,g.label_name,g.category_id,g.is_fenxiao,g.fenxiao_type,gs.fenxiao_price';
|
||||
$join = [
|
||||
['goods g', 'g.goods_id = gs.goods_id', 'inner']
|
||||
];
|
||||
// 如果是连锁运营模式
|
||||
if ($store_data['config']['store_business'] == 'store') {
|
||||
$join[] = ['store_goods_sku sgs', 'gs.sku_id = sgs.sku_id and sgs.store_id=' . $store_id, 'left'];
|
||||
|
||||
$field .= ',IFNULL(sgs.status, 0) as store_goods_status';
|
||||
|
||||
$field = str_replace('gs.price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as price', $field);
|
||||
$field = str_replace('gs.discount_price', 'IFNULL(IF(g.is_unify_price = 1,gs.discount_price,sgs.price), gs.discount_price) as discount_price', $field);
|
||||
if ($store_data['store_info']['stock_type'] == 'store') {
|
||||
$field = str_replace('gs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
|
||||
}
|
||||
}
|
||||
$goods_sku_detail = $goods_model->getGoodsSkuInfo($condition, $field, 'gs', $join)['data'];
|
||||
if (empty($goods_sku_detail)) {
|
||||
return $this->success(['goods_sku_detail' => null]);
|
||||
}
|
||||
|
||||
// 处理商品支持的配送方式
|
||||
if ($store_data['config']['store_business'] == 'store') {
|
||||
$support_trade_type = explode(',', $goods_sku_detail['support_trade_type']);
|
||||
$support_trade_type = array_map(function ($item) use ($store_data) {
|
||||
if ($item == 'express' && $store_data['store_info']['is_express']) return $item;
|
||||
elseif ($item == 'local' && $store_data['store_info']['is_o2o']) return $item;
|
||||
elseif ($item == 'store' && $store_data['store_info']['is_pickup']) return $item;
|
||||
}, $support_trade_type);
|
||||
$goods_sku_detail['support_trade_type'] = implode(',', array_filter($support_trade_type));
|
||||
|
||||
// 销售渠道设置为线上销售时门店商品状态为1
|
||||
if ($goods_sku_detail['sale_channel'] == 'online') {
|
||||
$goods_sku_detail['store_goods_status'] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
//库存转换处理
|
||||
$goods_model = new Goods();
|
||||
$goods_sku_detail = $goods_model->goodsStockTransform([$goods_sku_detail], $store_id, $store_data['config']['store_business'])[0];
|
||||
|
||||
$goods_sku_detail['purchased_num'] = 0; // 该商品已购数量
|
||||
$res['goods_sku_detail'] = $goods_sku_detail;
|
||||
|
||||
//判断预售(单独查询详情跳转)
|
||||
$is_join_presale = event('IsJoinPresale', ['sku_id' => $goods_sku_detail['sku_id']], true);
|
||||
if (!empty($is_join_presale) && $is_join_presale['code'] == 0) {
|
||||
$res['goods_sku_detail'] = array_merge($res['goods_sku_detail'], $is_join_presale['data']);
|
||||
$this->handleGoodsDetailData($res['goods_sku_detail'], $member_id, $site_id);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
//用户已登录
|
||||
if ($member_id > 0) {
|
||||
$member_info = model("member")->getInfo([['member_id', '=', $member_id]], 'member_id,is_fenxiao,fenxiao_id,member_level,member_level_type');
|
||||
if (!empty($member_info)) {
|
||||
$res['member_info'] = $member_info;
|
||||
|
||||
//解析会员价
|
||||
$res['goods_sku_detail']['member_price_config'] = json_decode($res['goods_sku_detail']['member_price'], true);
|
||||
$res = $this->getGoodsPrice($res);
|
||||
//商品限购查询购买数量
|
||||
if ($goods_sku_detail['is_limit'] && $goods_sku_detail['limit_type'] == 2 && $goods_sku_detail['max_buy'] > 0) {
|
||||
$res['goods_sku_detail']['purchased_num'] = $goods_model->getGoodsPurchasedNum($goods_sku_detail['goods_id'], $member_id);
|
||||
}
|
||||
//推荐会员卡
|
||||
if (addon_is_exit('supermember')) {
|
||||
if ($member_info['member_level_type'] == 0) {
|
||||
$card_model = new MemberCardModel();
|
||||
$card_recommend = $card_model->getRecommendMemberCard($site_id);
|
||||
if (!empty($card_recommend['data'])) {
|
||||
$res['goods_sku_detail']['membercard'] = $card_recommend['data'];
|
||||
$res = $this->getMemberCardGoodsPrice($res);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 分销佣金详情
|
||||
if (addon_is_exit('fenxiao')) {
|
||||
$fenxiao_goods_model = new FenxiaoGoods();
|
||||
$res = $fenxiao_goods_model->getGoodsFenxiaoDetailInApi($res, $member_id, $site_id);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} else {
|
||||
$res['goods_sku_detail']['member_price'] = '';
|
||||
}
|
||||
// 查询当前商品参与的营销活动信息
|
||||
$goods_promotion = event('GoodsPromotion', ['goods_id' => $goods_sku_detail['goods_id'], 'sku_id' => $goods_sku_detail['sku_id'], 'goods_sku_detail' => $res['goods_sku_detail']]);
|
||||
$res['goods_sku_detail']['goods_promotion'] = $goods_promotion;
|
||||
//查询卡项商品
|
||||
if (addon_is_exit('cardservice') && $goods_sku_detail['goods_class'] == GoodsDict::card) {
|
||||
$res = $this->getGoodsCardDetail($res);
|
||||
}
|
||||
//查询领用优惠券
|
||||
if (addon_is_exit('coupon')) {
|
||||
$coupon = new Coupon();
|
||||
$store_id = $store_data['config']['store_business'] == 'store' ? $store_data['store_info']['store_id'] : 0;
|
||||
$res = $coupon->getGoodsCanReceiveCouponInApi($res, $member_id, $site_id, $store_id);
|
||||
}
|
||||
|
||||
// 组合套餐,连锁门店没有营销活动
|
||||
if (addon_is_exit('bundling') && $store_data['config']['store_business'] == 'shop') {
|
||||
$bundling = new Bundling();
|
||||
$res = $bundling->getBundlingGoodsInApi($res);
|
||||
}
|
||||
if (addon_is_exit('manjian')) {
|
||||
$manjian_model = new ManjianModel();
|
||||
$res['goods_sku_detail']['manjian'] = $manjian_model->getGoodsManjianInfo($goods_sku_detail['goods_id'], $site_id)['data'];
|
||||
}
|
||||
|
||||
$this->handleGoodsDetailData($res['goods_sku_detail'], $member_id, $site_id);
|
||||
|
||||
return $this->success($res);
|
||||
|
||||
}
|
||||
|
||||
public function getGoodsSkuList($site_id, $goods_id, $member_id, $store_id, $store_data)
|
||||
{
|
||||
$goods = new Goods();
|
||||
$condition = [
|
||||
['gs.goods_id', '=', $goods_id],
|
||||
['gs.site_id', '=', $site_id]
|
||||
];
|
||||
$field = 'gs.sku_id,gs.goods_id,g.goods_image,gs.is_consume_discount,gs.discount_config,gs.discount_method,gs.sku_name,gs.sku_spec_format,gs.price,gs.discount_price,gs.promotion_type,gs.end_time,gs.stock,gs.sku_image,gs.sku_images,gs.goods_spec_format,gs.max_buy,gs.min_buy,gs.is_limit,gs.limit_type,gs.support_trade_type,gs.market_price,g.goods_state,gs.member_price,gs.max_buy,g.is_fenxiao,g.fenxiao_type,gs.fenxiao_price,g.promotion_addon';
|
||||
$join = [
|
||||
['goods g', 'g.goods_id = gs.goods_id', 'inner']
|
||||
];
|
||||
|
||||
// 如果是连锁运营模式
|
||||
if ($store_data['config']['store_business'] == 'store') {
|
||||
$join[] = ['store_goods_sku sgs', 'gs.sku_id = sgs.sku_id and sgs.store_id=' . $store_id, 'left'];
|
||||
$field = str_replace('gs.price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as price', $field);
|
||||
$field = str_replace('gs.discount_price', 'IFNULL(IF(g.is_unify_price = 1,gs.discount_price,sgs.price), gs.discount_price) as discount_price', $field);
|
||||
if ($store_data['store_info']['stock_type'] == 'store') {
|
||||
$field = str_replace('gs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
|
||||
}
|
||||
}
|
||||
$list = $goods->getGoodsSkuList($condition, $field, 'gs.sku_id asc', null, 'gs', $join);
|
||||
$goods_sku_list = $list['data'];
|
||||
|
||||
//库存转换处理
|
||||
$goods_model = new Goods();
|
||||
$goods_sku_list = $goods_model->goodsStockTransform($goods_sku_list, $store_id, $store_data['config']['store_business']);
|
||||
|
||||
if ($member_id > 0) {
|
||||
//会员信息
|
||||
$member_info = model("member")->getInfo([['member_id', '=', $member_id]], 'member_id,is_fenxiao,fenxiao_id,member_level,member_level_type');
|
||||
$addon_suppermember_is_exist = addon_is_exit('supermember');
|
||||
if ($addon_suppermember_is_exist) {
|
||||
if ($member_info['member_level_type'] == 0) {
|
||||
$card_model = new MemberCardModel();
|
||||
$card_recommend = $card_model->getRecommendMemberCard($site_id);
|
||||
}
|
||||
}
|
||||
//会员已购买商品数量
|
||||
$buy_num = $goods->getGoodsPurchasedNum($goods_id, $member_id);
|
||||
|
||||
//商品营销活动
|
||||
//组装数据
|
||||
foreach ($goods_sku_list as $k => $v) {
|
||||
$goods_sku_list[$k]['member_info'] = $member_info;
|
||||
$goods_sku_list[$k]['member_price_config'] = json_decode($v['member_price'], true);
|
||||
$goods_sku_list[$k]['member_price'] = '';
|
||||
//商品限购
|
||||
if ($v['is_limit'] && $v['limit_type'] == 2 && $v['max_buy'] > 0) {
|
||||
$goods_sku_list[$k]['purchased_num'] = $buy_num;
|
||||
}
|
||||
//超级会员卡
|
||||
if ($addon_suppermember_is_exist) {
|
||||
if ($member_info['member_level_type'] == 0) {
|
||||
$goods_sku_list[$k]['membercard'] = $card_recommend['data'];
|
||||
}
|
||||
}
|
||||
}
|
||||
//处理商品会员价
|
||||
$goods_sku_list = $this->getGoodsSkuListPrice($goods_sku_list);
|
||||
//处理商品推荐会员卡价格
|
||||
$goods_sku_list = $this->getMemberCardGoodsSkuListPrice($goods_sku_list);
|
||||
|
||||
// 分销佣金详情
|
||||
if (addon_is_exit('fenxiao')) {
|
||||
$fenxiao_goods_model = new FenxiaoGoods();
|
||||
$goods_sku_list = $fenxiao_goods_model->getGoodsSkuListFenxiaoInApi($goods_sku_list, $member_id, $site_id);
|
||||
}
|
||||
|
||||
}
|
||||
//营销活动
|
||||
$goods_promotion = event('GoodsPromotion', ['goods_id' => $goods_id, 'sku_id' => $goods_sku_list[0]['sku_id'], 'goods_sku_detail' => $goods_sku_list[0]]);
|
||||
|
||||
if (addon_is_exit('presale')) {
|
||||
//预售
|
||||
$presale_list = event('IsJoinPresale', ['goods_id' => $goods_id], true)['data'];
|
||||
if (!empty($presale_list)) {
|
||||
$presale_list = array_column($presale_list, null, 'sku_id');
|
||||
}
|
||||
}
|
||||
|
||||
//常规情况组装列表数据
|
||||
foreach ($goods_sku_list as $k => $v) {
|
||||
if (!empty($goods_promotion)) {
|
||||
$goods_sku_list[$k]['goods_promotion'] = $goods_promotion;
|
||||
}
|
||||
if (addon_is_exit('presale')) {
|
||||
$presale_sku = $presale_list[$v['sku_id']] ?? [];
|
||||
if (!empty($presale_sku)) {
|
||||
$goods_sku_list[$k]['promotion_type'] = 'presale';
|
||||
$goods_sku_list[$k]['presale_id'] = $presale_sku['presale_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success($goods_sku_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品sku列表实际价格
|
||||
* @param $goods_sku_list
|
||||
* @return mixed
|
||||
*/
|
||||
private function getGoodsSkuListPrice($goods_sku_list)
|
||||
{
|
||||
if (!addon_is_exit("memberprice")) return $goods_sku_list;
|
||||
$goods_sku_info = $goods_sku_list[0];
|
||||
//商品不参与会员消费折扣
|
||||
if (!$goods_sku_info['is_consume_discount']) return $goods_sku_list;
|
||||
$member_info = $goods_sku_list[0]['member_info'];
|
||||
//非会员
|
||||
if (empty($member_info['member_level'])) return $goods_sku_list;
|
||||
$member_level = model("member_level")->getInfo([['level_id', '=', $member_info['member_level']]], 'level_id,consume_discount,level_type,is_recommend');
|
||||
$member_info['consume_discount'] = $member_level['consume_discount'];
|
||||
$member_info['member_level_info'] = $member_level;
|
||||
foreach ($goods_sku_list as $k => $sku_info) {
|
||||
$member_price = 0;
|
||||
if ($goods_sku_info['discount_config'] == 1) {
|
||||
// 自定义优惠
|
||||
$value = isset($sku_info['member_price_config'][$sku_info['discount_method']][$member_info['member_level']]) ? $sku_info['member_price_config'][$goods_sku_info['discount_method']][$member_info['member_level']] : 0;
|
||||
switch ($sku_info['discount_method']) {
|
||||
case "discount":
|
||||
// 打折
|
||||
if ($value == 0) {
|
||||
$member_price = $sku_info['price'];
|
||||
} else {
|
||||
$member_price = number_format($sku_info['price'] * $value / 10, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
|
||||
case "manjian":
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_sku_info['price'];
|
||||
} else {
|
||||
$member_price = number_format($sku_info['price'] - $value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case "fixed_price":
|
||||
if ($value == 0) {
|
||||
$member_price = $sku_info['price'];
|
||||
} else {
|
||||
// 指定价格
|
||||
$member_price = number_format($value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// 默认按会员享受折扣计算
|
||||
$member_price = number_format($sku_info['price'] * $member_info['consume_discount'] / 100, 2, '.', '');
|
||||
}
|
||||
if ($member_price < $sku_info['price']) {
|
||||
$sku_info['price'] = $member_price;
|
||||
}
|
||||
$sku_info['member_price'] = $member_price;
|
||||
$sku_info['member_info'] = $member_info;
|
||||
$goods_sku_list[$k] = $sku_info;
|
||||
}
|
||||
return $goods_sku_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推荐会员卡对应商品价格
|
||||
* @param $goods_sku_list
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCardGoodsSkuListPrice($goods_sku_list)
|
||||
{
|
||||
$goods_sku_info = $goods_sku_list[0];
|
||||
//商品不参与会员消费折扣
|
||||
if (!$goods_sku_info['is_consume_discount']) return $goods_sku_list;
|
||||
$member_card = $goods_sku_list[0]['membercard'] ?? [];
|
||||
if (!$member_card) return $goods_sku_list;
|
||||
foreach ($goods_sku_list as $k => $sku_info) {
|
||||
$member_price = 0;
|
||||
if ($goods_sku_info['discount_config'] == 1) {
|
||||
// 自定义优惠
|
||||
$value = $sku_info['member_price_config'][$sku_info['discount_method']][$member_card['level_id']] ?? 0;
|
||||
switch ($sku_info['discount_method']) {
|
||||
case "discount":
|
||||
// 打折
|
||||
if ($value == 0) {
|
||||
$member_price = $sku_info['price'];
|
||||
} else {
|
||||
$member_price = number_format($sku_info['price'] * $value / 10, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case "manjian":
|
||||
if ($value == 0) {
|
||||
$member_price = $sku_info['price'];
|
||||
} else {
|
||||
// 满减
|
||||
$member_price = number_format($sku_info['price'] - $value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case "fixed_price":
|
||||
if ($value == 0) {
|
||||
$member_price = $sku_info['price'];
|
||||
} else {
|
||||
// 指定价格
|
||||
$member_price = number_format($value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// 默认按会员享受折扣计算
|
||||
$member_price = number_format($sku_info['price'] * $member_card['consume_discount'] / 100, 2, '.', '');
|
||||
}
|
||||
$goods_sku_list[$k]['membercard']['member_price'] = $member_price;
|
||||
}
|
||||
return $goods_sku_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品信息
|
||||
* @param $goods_sku_detail_array
|
||||
* @return array
|
||||
*/
|
||||
public function getGoodsCardDetail($goods_sku_detail_array)
|
||||
{
|
||||
$condition = [
|
||||
['goods_id', '=', $goods_sku_detail_array['goods_sku_detail']['goods_id']]
|
||||
];
|
||||
$field = 'card_type,renew_price,recharge_money,common_num,discount_goods_type,discount,validity_type,validity_day,validity_time';
|
||||
|
||||
$goods_sku_detail_array['goods_sku_detail']['card_info'] = model('goods_card')->getInfo($condition, $field);
|
||||
$sku_condition = [
|
||||
['gci.card_goods_id', '=', $goods_sku_detail_array['goods_sku_detail']['goods_id']],
|
||||
];
|
||||
$sku_field = 'gci.goods_id,gci.sku_id,gci.num,gci.discount,gs.sku_name,gs.price,gs.sku_image,gs.goods_class_name';
|
||||
$sku_join = [['goods_sku gs', 'gs.sku_id = gci.sku_id', 'inner']];
|
||||
|
||||
$goods_sku_detail_array['goods_sku_detail']['card_info']['relation_goods'] = model('goods_card_item')->getList($sku_condition, $sku_field, 'gci.id asc', 'gci', $sku_join);
|
||||
return $goods_sku_detail_array;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理商品详情公共数据
|
||||
* @param $data
|
||||
* @param $member_id
|
||||
* @param $site_id
|
||||
*/
|
||||
public function handleGoodsDetailData(&$data, $member_id, $site_id)
|
||||
{
|
||||
$goods = new Goods();
|
||||
|
||||
if (!empty($data['sku_images'])) $data['sku_images_list'] = $goods->getGoodsImage($data['sku_images'], $site_id)['data'] ?? [];
|
||||
if (!empty($data['sku_image'])) $data['sku_image_list'] = $goods->getGoodsImage($data['sku_image'], $site_id)['data'] ?? [];
|
||||
if (!empty($data['goods_image'])) $data['goods_image_list'] = $goods->getGoodsImage($data['goods_image'], $site_id)['data'] ?? [];
|
||||
|
||||
// 商品服务
|
||||
if (!empty($data['goods_service_ids'])) {
|
||||
$goods_service = new GoodsService();
|
||||
$data['goods_service'] = $goods_service->getServiceList([['site_id', '=', $site_id], ['id', 'in', $data['goods_service_ids']]], 'service_name,desc,icon')['data'];
|
||||
} else
|
||||
$data['goods_service'] = [];
|
||||
|
||||
// 商品详情配置
|
||||
$config_model = new ConfigModel();
|
||||
$data['config'] = $config_model->getGoodsDetailConfig($site_id)['data']['value'];
|
||||
|
||||
if ($data['is_virtual'] == 0) {
|
||||
$data['express_type'] = (new ExpressConfig())->getEnabledExpressType($site_id);
|
||||
}
|
||||
|
||||
// 获取用户是否关注
|
||||
if ($member_id != 0) {
|
||||
$goods_collect_model = new GoodsCollectModel();
|
||||
$data['is_collect'] = $goods_collect_model->getIsCollect($data['goods_id'], $member_id)['data'];
|
||||
} else {
|
||||
$data['is_collect'] = 0;
|
||||
}
|
||||
|
||||
// 评价查询
|
||||
$goods_evaluate_model = new GoodsEvaluateModel();
|
||||
$condition = [
|
||||
['is_show', '=', 1],
|
||||
['is_audit', '=', 1],
|
||||
['goods_id', '=', $data['goods_id']]
|
||||
];
|
||||
$field = 'evaluate_id,content,images,explain_first,member_name,member_headimg,is_anonymous,again_content,again_images,again_explain,create_time,again_time,scores';
|
||||
$order = "create_time desc";
|
||||
$data['evaluate_list'] = $goods_evaluate_model->getSecondEvaluateInfo($condition, $field, $order)['data'];
|
||||
$data['evaluate_count'] = $goods_evaluate_model->getEvaluateCount($condition)['data'];
|
||||
$config_model = new \app\model\order\Config();
|
||||
$data['evaluate_config'] = $config_model->getOrderEvaluateConfig($site_id)['data']['value'];
|
||||
}
|
||||
|
||||
/**
|
||||
* api请求获取商品价格(计算折扣价与会员价)
|
||||
* @param $goods_sku_detail_array
|
||||
* @return mixed
|
||||
*/
|
||||
private function getGoodsPrice($goods_sku_detail_array)
|
||||
{
|
||||
|
||||
if (!addon_is_exit("memberprice")) return $goods_sku_detail_array;
|
||||
$goods_sku_info = $goods_sku_detail_array['goods_sku_detail'];
|
||||
$member_info = $goods_sku_detail_array['member_info'];
|
||||
$member_price = 0;
|
||||
|
||||
if ($goods_sku_info['is_consume_discount']) {
|
||||
|
||||
if (!empty($member_info['member_level'])) {
|
||||
$member_level = model("member_level")->getInfo([['level_id', '=', $member_info['member_level']]], 'level_id,consume_discount,level_type,is_recommend');
|
||||
$member_info['consume_discount'] = $member_level['consume_discount'];
|
||||
$member_info['member_level_info'] = $member_level;
|
||||
}
|
||||
|
||||
if (!empty($member_info['member_level'])) {
|
||||
if ($goods_sku_info['discount_config'] == 1) {
|
||||
// 自定义优惠
|
||||
$value = $goods_sku_info['member_price_config'][$goods_sku_info['discount_method']][$member_info['member_level']] ?? 0;
|
||||
switch ($goods_sku_info['discount_method']) {
|
||||
case "discount":
|
||||
// 打折
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_sku_info['price'];
|
||||
} else {
|
||||
$member_price = number_format($goods_sku_info['price'] * $value / 10, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case "manjian":
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_sku_info['price'];
|
||||
} else {
|
||||
$member_price = number_format($goods_sku_info['price'] - $value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case "fixed_price":
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_sku_info['price'];
|
||||
} else
|
||||
// 指定价格
|
||||
$member_price = number_format($value, 2, '.', '');
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// 默认按会员享受折扣计算
|
||||
$member_price = number_format($goods_sku_info['price'] * $member_info['consume_discount'] / 100, 2, '.', '');
|
||||
}
|
||||
if ($member_price < $goods_sku_info['price']) {
|
||||
$goods_sku_info['price'] = $member_price;
|
||||
}
|
||||
}
|
||||
}
|
||||
$goods_sku_info['member_price'] = $member_price;
|
||||
$goods_sku_detail_array['goods_sku_detail'] = $goods_sku_info;
|
||||
$goods_sku_detail_array['member_info'] = $member_info;
|
||||
return $goods_sku_detail_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推荐会员卡对应商品价格
|
||||
* @param $goods_sku_detail_array
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCardGoodsPrice($goods_sku_detail_array)
|
||||
{
|
||||
if (!addon_is_exit("memberprice")) return $goods_sku_detail_array;
|
||||
|
||||
$goods_sku_info = $goods_sku_detail_array['goods_sku_detail'];
|
||||
$member_card = $goods_sku_detail_array['goods_sku_detail']['membercard'];
|
||||
|
||||
$member_price = 0;
|
||||
$price = $goods_sku_info['discount_price'];
|
||||
|
||||
if ($goods_sku_info['is_consume_discount']) {
|
||||
if ($goods_sku_info['discount_config'] == 1) {
|
||||
// 自定义优惠
|
||||
$value = $goods_sku_info['member_price_config'][$goods_sku_info['discount_method']][$member_card['level_id']] ?? 0;
|
||||
switch ($goods_sku_info['discount_method']) {
|
||||
case "discount":
|
||||
// 打折
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_sku_info['discount_price'];
|
||||
} else {
|
||||
$member_price = number_format($goods_sku_info['discount_price'] * $value / 10, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case "manjian":
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_sku_info['discount_price'];
|
||||
} else {
|
||||
// 满减
|
||||
$member_price = number_format($goods_sku_info['discount_price'] - $value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case "fixed_price":
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_sku_info['discount_price'];
|
||||
} else {
|
||||
// 指定价格
|
||||
$member_price = number_format($value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// 默认按会员享受折扣计算
|
||||
$member_price = number_format($goods_sku_info['discount_price'] * $member_card['consume_discount'] / 100, 2, '.', '');
|
||||
}
|
||||
if ($member_price < $price) {
|
||||
$price = $member_price;
|
||||
}
|
||||
}
|
||||
$member_card['member_price'] = $price;
|
||||
$goods_sku_detail_array['goods_sku_detail']['membercard'] = $member_card;
|
||||
return $goods_sku_detail_array;
|
||||
}
|
||||
}
|
||||
411
app/model/goods/GoodsAttribute.php
Executable file
411
app/model/goods/GoodsAttribute.php
Executable file
@@ -0,0 +1,411 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 商品类型、属性
|
||||
*/
|
||||
class GoodsAttribute extends BaseModel
|
||||
{
|
||||
|
||||
/************************************************************商品类型*********************************************/
|
||||
|
||||
/**
|
||||
* 添加商品类型
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function addAttrClass($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$class_id = model("goods_attr_class")->add($data);
|
||||
Cache::tag("goods_attr_class_" . $site_id)->clear();
|
||||
return $this->success($class_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑商品类型
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function editAttrClass($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model("goods_attr_class")->update($data, [ [ 'class_id', '=', $data[ 'class_id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
|
||||
if (!empty($data[ 'class_name' ])) {
|
||||
//修改属性表
|
||||
model("goods_attribute")->update([ 'attr_class_name' => $data[ 'class_name' ] ], [ [ 'attr_class_id', '=', $data[ 'class_id' ] ] ]);
|
||||
}
|
||||
//预留修改商品
|
||||
Cache::tag("goods_attr_class_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品类型
|
||||
* @param $class_id
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function deleteAttrClass($class_id, $site_id)
|
||||
{
|
||||
$site_id = $site_id ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model('goods_attr_class')->delete([ [ 'class_id', '=', $class_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if ($res) {
|
||||
|
||||
// 删除商品参数
|
||||
model('goods_attribute')->delete([ [ 'attr_class_id', '=', $class_id ] ]);
|
||||
Cache::tag("goods_attribute_" . $site_id . "_" . $class_id)->clear();
|
||||
|
||||
}
|
||||
Cache::tag("goods_attr_class_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改排序
|
||||
* @param int $sort
|
||||
* @param int $class_id
|
||||
*/
|
||||
public function modifyAttrClassSort($sort, $class_id, $site_id)
|
||||
{
|
||||
$site_id = $site_id ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model('goods_attr_class')->update([ 'sort' => $sort ], [ [ 'class_id', '=', $class_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag("goods_attr_class_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品类型信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getAttrClassInfo($condition, $field = '*')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
$res = model('goods_attr_class')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品类型列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getAttrClassList($condition = [], $field = 'class_id,class_name,sort', $order = 'class_id desc', $limit = null)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
$list = model('goods_attr_class')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品类型分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getAttrClassPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'sort asc', $field = 'class_id,class_name,sort')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
$list = model('goods_attr_class')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************商品参数*********************************************/
|
||||
|
||||
/**
|
||||
* 添加商品参数
|
||||
* @param $attr_class_id
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function addAttribute($attr_class_id, $data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$attr_id = model("goods_attribute")->add($data);
|
||||
Cache::tag("goods_attribute_" . $site_id . "_" . $attr_class_id)->clear();
|
||||
return $this->success($attr_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑商品参数
|
||||
* @param $attr_class_id
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function editAttribute($attr_class_id, $data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model("goods_attribute")->update($data, [ [ 'attr_id', '=', $data[ 'attr_id' ] ], [ 'attr_class_id', '=', $attr_class_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag("goods_attribute_" . $site_id . "_" . $attr_class_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除属性
|
||||
* @param $attr_class_id
|
||||
* @param $attr_id
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function deleteAttribute($attr_class_id, $attr_id, $site_id)
|
||||
{
|
||||
$site_id = $site_id ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model('goods_attribute')->delete([ [ 'attr_id', '=', $attr_id ], [ 'attr_class_id', '=', $attr_class_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
Cache::tag("goods_attribute_" . $site_id . "_" . $attr_class_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改排序
|
||||
* @param $sort
|
||||
* @param $attr_class_id
|
||||
* @param $attr_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function modifyAttributeSort($sort, $attr_class_id, $attr_id, $site_id)
|
||||
{
|
||||
$res = model('goods_attribute')->update([ 'sort' => $sort ], [ [ 'attr_id', '=', $attr_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag("goods_attribute_" . $site_id . "_" . $attr_class_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取属性信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getAttributeInfo($condition, $field = '*')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$attr_class_id = $check_condition['attr_class_id'] ?? '';
|
||||
if ($attr_class_id === '') {
|
||||
return $this->error('', 'REQUEST_GOODS_ATTRIBUTE_ID');
|
||||
}
|
||||
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model('goods_attribute')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品参数列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getAttributeList($condition = [], $field = '*', $order = 'sort asc', $limit = null)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$attr_class_id = $check_condition['attr_class_id'] ?? '';
|
||||
if ($attr_class_id === '') {
|
||||
return $this->error('', 'REQUEST_GOODS_ATTRIBUTE_ID');
|
||||
}
|
||||
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$list = model('goods_attribute')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品规格列表,暂时不加缓存
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getSpecList($condition = [], $field = 'attr_id,attr_name,attr_class_id,sort,is_query,is_spec,attr_value_list,attr_value_list,attr_type,site_id', $order = 'sort asc', $limit = null)
|
||||
{
|
||||
$list = model('goods_attribute')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/************************************************************商品参数值*********************************************/
|
||||
|
||||
/**
|
||||
* 添加属性值
|
||||
* @param $attr_class_id
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function addAttributeValue($attr_class_id, $data)
|
||||
{
|
||||
$attr_value_id = model("goods_attribute_value")->addList($data);
|
||||
if ($attr_value_id) {
|
||||
// 刷新属性值JSON格式
|
||||
$this->refreshAttrValueFormat($attr_class_id, $data[ 0 ][ 'attr_id' ]);
|
||||
Cache::tag("goods_attribute_value_" . $attr_class_id)->clear();
|
||||
return $this->success($attr_value_id);
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑商品参数值
|
||||
* @param $attr_class_id
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function editAttributeValue($attr_class_id, $data)
|
||||
{
|
||||
$res = model("goods_attribute_value")->update($data, [ [ 'attr_value_id', '=', $data[ 'attr_value_id' ] ] ]);
|
||||
if ($res) {
|
||||
// 刷新属性值JSON格式
|
||||
$this->refreshAttrValueFormat($attr_class_id, $data[ 'attr_id' ]);
|
||||
Cache::tag("goods_attribute_value_" . $attr_class_id)->clear();
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新属性值JSON格式
|
||||
* @param $attr_class_id
|
||||
* @param $attr_id
|
||||
* @return array|void
|
||||
*/
|
||||
private function refreshAttrValueFormat($attr_class_id, $attr_id)
|
||||
{
|
||||
$list = model('goods_attribute_value')->getList([ [ 'attr_id', '=', $attr_id ] ], 'attr_value_id,attr_value_name');
|
||||
if (!empty($list)) {
|
||||
$attr_value_format = [];
|
||||
foreach ($list as $k => $v) {
|
||||
$item = [
|
||||
'attr_value_id' => $v[ 'attr_value_id' ],
|
||||
'attr_value_name' => $v[ 'attr_value_name' ]
|
||||
];
|
||||
$attr_value_format[] = $item;
|
||||
}
|
||||
$res = model("goods_attribute")->update([ 'attr_value_format' => json_encode($attr_value_format) ], [ [ 'attr_id', '=', $attr_id ], [ 'attr_class_id', '=', $attr_class_id ] ]);
|
||||
Cache::tag("goods_attribute_" . $attr_class_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除属性值
|
||||
* @param $attr_class_id
|
||||
* @param $attr_id
|
||||
* @param $condition
|
||||
* @return \multitype
|
||||
*/
|
||||
public function deleteAttributeValue($attr_class_id, $attr_id, $condition)
|
||||
{
|
||||
$res = model('goods_attribute_value')->delete($condition);
|
||||
$this->refreshAttrValueFormat($attr_class_id, $attr_id);
|
||||
Cache::tag("goods_attribute_value_" . $attr_class_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品参数值列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getAttributeValueList($condition = [], $field = 'attr_value_id,attr_value_name,attr_id,attr_class_id,sort', $order = '', $limit = null)
|
||||
{
|
||||
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$attr_class_id = $check_condition['attr_class_id'] ?? '';
|
||||
if ($attr_class_id === '') {
|
||||
return $this->error('', 'REQUEST_GOODS_ATTRIBUTE_ID');
|
||||
}
|
||||
|
||||
$list = model('goods_attribute_value')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品规格值列表,暂时不加缓存
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getSpecValueList($condition = [], $field = 'attr_value_id,attr_value_name,attr_id,attr_class_id,sort', $order = 'attr_value_id desc', $limit = null)
|
||||
{
|
||||
$list = model('goods_attribute_value')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
109
app/model/goods/GoodsBrand.php
Executable file
109
app/model/goods/GoodsBrand.php
Executable file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 商品品牌
|
||||
*/
|
||||
class GoodsBrand extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加品牌
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addBrand($data)
|
||||
{
|
||||
$data[ 'create_time' ] = time();
|
||||
$brand_id = model('goods_brand')->add($data);
|
||||
return $this->success($brand_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改品牌
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editBrand($data, $condition)
|
||||
{
|
||||
$res = model('goods_brand')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除品牌
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteBrand($condition)
|
||||
{
|
||||
$res = model('goods_brand')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改排序
|
||||
* @param int $sort
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyBrandSort($sort, $condition)
|
||||
{
|
||||
$res = model('goods_brand')->update([ 'sort' => $sort ], $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取品牌信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getBrandInfo($condition, $field = 'brand_id, brand_name, brand_initial, image_url, banner, brand_desc, sort, create_time, is_recommend')
|
||||
{
|
||||
$res = model('goods_brand')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取品牌列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getBrandList($condition = [], $field = 'brand_id, brand_name, brand_initial, image_url, banner, brand_desc, sort, create_time', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('goods_brand')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取品牌分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getBrandPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = 'brand_id,brand_name,brand_initial,image_url, banner, brand_desc,sort,create_time,is_recommend')
|
||||
{
|
||||
$list = model('goods_brand')->pageList($condition, $field, $order, $page, $page_size, '', '');
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
93
app/model/goods/GoodsBrowse.php
Executable file
93
app/model/goods/GoodsBrowse.php
Executable file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\member\Member;
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Stat;
|
||||
|
||||
/**
|
||||
* 商品浏览历史
|
||||
*/
|
||||
class GoodsBrowse extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加商品浏览记录
|
||||
* @param array $data
|
||||
*/
|
||||
public function addBrowse($data)
|
||||
{
|
||||
$res = model('goods_browse')->getInfo([ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'goods_id', '=', $data[ 'goods_id' ] ] ], 'site_id,id');
|
||||
$data[ 'browse_time' ] = time();
|
||||
$stat = new Stat();
|
||||
$stat->switchStat([ 'type' => 'goods_visit', 'data' => [
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'goods_id' => $data[ 'goods_id' ],
|
||||
'member_id' => $data[ 'member_id' ]
|
||||
] ]);
|
||||
|
||||
if (!empty($res)) {
|
||||
$collect_id = model('goods_browse')->update($data, [ [ 'id', '=', $res[ 'id' ] ] ]);
|
||||
} else {
|
||||
$collect_id = model('goods_browse')->add($data);
|
||||
}
|
||||
Member::modifyLastVisitTime($data[ 'member_id' ]);
|
||||
//添加浏览统计
|
||||
|
||||
$stat->switchStat([ 'type' => 'visit', 'data' => [
|
||||
'visit_count' => 1,
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'app_module' => $data[ 'app_module' ] ?? '',
|
||||
'goods_id' => $data[ 'goods_id' ],
|
||||
] ]);
|
||||
return $this->success($collect_id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除浏览记录
|
||||
* @param int $id
|
||||
* @param int $member_id
|
||||
*/
|
||||
public function deleteBrowse($id, $member_id)
|
||||
{
|
||||
$res = model('goods_browse')->delete([ [ 'member_id', '=', $member_id ], [ 'id', 'in', $id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取浏览记录分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getBrowsePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'browse_time desc', $field = 'id,member_id,browse_time,goods_id,sku_id', $alias = 'a', $join = [])
|
||||
{
|
||||
$list = model('goods_browse')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取浏览商品列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getBrowseList($condition = [], $field = '*', $order = 'id asc', $limit = null)
|
||||
{
|
||||
$list = model('goods_browse')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
}
|
||||
476
app/model/goods/GoodsCategory.php
Executable file
476
app/model/goods/GoodsCategory.php
Executable file
@@ -0,0 +1,476 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 商品分类
|
||||
*/
|
||||
class GoodsCategory extends BaseModel
|
||||
{
|
||||
|
||||
public $cache_model = 'cache_model_goods_category';
|
||||
/**
|
||||
* 添加商品分类
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function addCategory($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
$category_id = model('goods_category')->add($data);
|
||||
$common_data = array (
|
||||
'category_id_1' => 0,
|
||||
'category_id_2' => 0,
|
||||
'category_id_3' => 0,
|
||||
);
|
||||
switch ( $data[ 'level' ] ) {
|
||||
case 1:
|
||||
$common_data[ 'category_id_1' ] = $category_id;
|
||||
break;
|
||||
case 2:
|
||||
$common_data[ 'category_id_1' ] = $data[ 'pid' ];//这让我并没有验证合法性,业务发生变动之后要注意(author:周)
|
||||
$common_data[ 'category_id_2' ] = $category_id;
|
||||
break;
|
||||
case 3:
|
||||
//错误数据纠正
|
||||
$parent_category_info = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'pid' ] ], [ 'site_id', '=', $site_id ] ], 'pid');//这让我并没有验证合法性,业务发生变动之后要注意(author:周)
|
||||
$common_data[ 'category_id_1' ] = $parent_category_info[ 'pid' ];//这让我并没有验证合法性,业务发生变动之后要注意(周)
|
||||
$common_data[ 'category_id_2' ] = $data[ 'pid' ];
|
||||
$common_data[ 'category_id_3' ] = $category_id;
|
||||
|
||||
break;
|
||||
}
|
||||
$data = array_merge($data, $common_data);
|
||||
|
||||
model('goods_category')->update([ 'category_id_' . $data[ 'level' ] => $category_id ], [ [ 'category_id', '=', $category_id ] ]);
|
||||
Cache::tag($this->cache_model)->clear();
|
||||
return $this->success($category_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证分类是否可以修改
|
||||
*/
|
||||
public function checkEditCategory($data)
|
||||
{
|
||||
$category_id = $data[ 'category_id' ];
|
||||
$pid = $data[ 'pid' ] ?? 0;
|
||||
$parent_category_info = model('goods_category')->getInfo([ [ 'category_id', '=', $pid ], [ 'site_id', '=', $data[ 'site_id' ] ] ]) ?? [];
|
||||
if (empty($parent_category_info)) return $this->success();
|
||||
$category_info = model('goods_category')->getInfo([ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $data[ 'site_id' ] ] ]) ?? [];
|
||||
if ($parent_category_info[ 'category_id' ] == $category_info[ 'category_id' ]) {
|
||||
return $this->error('', '不能修改上级为自己');
|
||||
}
|
||||
if ($category_info[ 'level' ] < 3 && $parent_category_info[ 'level' ] == 2) {
|
||||
$child_list = model('goods_category')->getCount([ [ 'pid', '=', $category_info[ 'category_id' ] ] ], 'category_id');
|
||||
if ($child_list > 0) return $this->error('', '当前等级存在下级,不可修改为该上级');
|
||||
}
|
||||
if ($category_info[ 'level' ] == 1 && $parent_category_info[ 'level' ] == 1) {
|
||||
$child_list = model('goods_category')->getColumn([ [ 'pid', '=', $category_info[ 'category_id' ] ] ], 'category_id');
|
||||
|
||||
if ($child_list) {
|
||||
$child_child_list = model('goods_category')->getCount([ [ 'pid', 'in', $child_list ] ], 'category_id');
|
||||
if ($child_child_list > 0) return $this->error('', '当前等级存在下下级,不可修改为该上级');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改商品分类
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function editCategory($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
$check_res = $this->checkEditCategory($data);
|
||||
if ($check_res[ 'code' ] < 0) return $check_res;
|
||||
model('goods_category')->startTrans();
|
||||
try {
|
||||
//仅限当分类不支持跨级别修改
|
||||
$pid = $data[ 'pid' ];
|
||||
$level = 1;
|
||||
$parent_category_info = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'pid' ] ], [ 'site_id', '=', $site_id ] ]);
|
||||
if ($parent_category_info) $level = (int) $parent_category_info[ 'level' ] + 1;
|
||||
$data[ 'level' ] = $level;
|
||||
|
||||
//获取该分类信息
|
||||
$info = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id' ] ], [ 'site_id', '=', $site_id ] ]);
|
||||
$common_data = array (
|
||||
'category_id_1' => 0,
|
||||
'category_id_2' => 0,
|
||||
'category_id_3' => 0,
|
||||
);
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
$common_data[ 'category_id_1' ] = $info[ 'category_id' ];
|
||||
break;
|
||||
case 2:
|
||||
$common_data[ 'category_id_1' ] = $data[ 'pid' ];
|
||||
$common_data[ 'category_id_2' ] = $info[ 'category_id' ];
|
||||
//二三级要同步改动
|
||||
model('goods_category')->update($common_data, [ [ 'pid', '=', $info[ 'category_id' ] ] ]);
|
||||
|
||||
model('goods_category')->update([ 'category_id_3' => Db::raw('category_id') ], [ [ 'pid', '=', $info[ 'category_id' ] ], [ 'level', '=', '3' ] ]);
|
||||
|
||||
break;
|
||||
case 3:
|
||||
//错误数据纠正
|
||||
$parent_category_info = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'pid' ] ], [ 'site_id', '=', $site_id ] ], 'pid');//这让我并没有验证合法性,业务发生变动之后要注意(author:周)
|
||||
$common_data[ 'category_id_1' ] = $parent_category_info[ 'pid' ];//这让我并没有验证合法性,业务发生变动之后要注意(周)
|
||||
$common_data[ 'category_id_2' ] = $data[ 'pid' ];
|
||||
$common_data[ 'category_id_3' ] = $info[ 'category_id' ];
|
||||
|
||||
break;
|
||||
}
|
||||
$info = array_merge($info, $common_data);
|
||||
$data = array_merge($data, $common_data);
|
||||
|
||||
if ($data[ 'is_show' ] == -1) {
|
||||
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
model('goods_category')->update([ 'is_show' => -1 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
$data[ 'category_full_name' ] = $data[ 'category_name' ];
|
||||
break;
|
||||
|
||||
case 2:
|
||||
model('goods_category')->update([ 'is_show' => -1 ], [ [ 'category_id_2', '=', $info[ 'category_id_2' ] ] ]);
|
||||
|
||||
$info_1 = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
case 3:
|
||||
// model('goods_category')->update(['is_show' => -1], [['category_id', 'in', [$info['category_id_1'], $info['category_id_2']]]]);
|
||||
|
||||
$info_1 = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$info_2 = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_2' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $info_2[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
model('goods_category')->update([ 'is_show' => 0 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
|
||||
$data[ 'category_full_name' ] = $data[ 'category_name' ];
|
||||
break;
|
||||
case 2:
|
||||
model('goods_category')->update([ 'is_show' => 0 ], [ [ 'category_id', '=', $info[ 'category_id_1' ] ] ]);
|
||||
|
||||
$info_1 = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
case 3:
|
||||
// model('goods_category')->update(['is_show' => 0], [['category_id', 'in', [$info['category_id_1'], $info['category_id_2']]]]);
|
||||
|
||||
$info_1 = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$info_2 = model('goods_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_2' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $info_2[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($info[ 'pid' ] != $data[ 'pid' ]) {
|
||||
if ($level == 2) {
|
||||
model('goods')->update([
|
||||
'category_json' => Db::raw("REPLACE(category_json, '[\"{$info['pid']},{$data[ 'category_id' ]},', '[\"{$data['pid']},{$data[ 'category_id' ]},')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
model('goods')->update([
|
||||
'category_json' => Db::raw("REPLACE(category_json, '[\"{$info['pid']},{$data[ 'category_id' ]}\"]', '[\"{$data['pid']},{$data[ 'category_id' ]}\"]')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
model('goods')->update([
|
||||
'category_id' => Db::raw("REPLACE(category_id, ',{$info['pid']},{$data[ 'category_id' ]},', ',{$data['pid']},{$data[ 'category_id' ]},')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
} else {
|
||||
model('goods')->update([
|
||||
'category_json' => Db::raw("REPLACE(category_json, '[\"{$info['category_id_1']},{$info['category_id_2']},{$info['category_id_3']}\"]', '[\"{$data['category_id_1']},{$data['category_id_2']},{$data['category_id_3']}\"]')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
model('goods')->update([
|
||||
'category_id' => Db::raw("REPLACE(category_id, ',{$info['category_id_1']},{$info[ 'category_id_2' ]},{$info[ 'category_id_3' ]},', ',{$data['category_id_1']},{$data[ 'category_id_2' ]},{$data[ 'category_id_3' ]},')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$res = model('goods_category')->update($data, [ [ 'category_id', '=', $data[ 'category_id' ] ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
//变更下级等级层级
|
||||
$child_list = model('goods_category')->getColumn([ [ 'site_id', '=', $site_id ], [ 'pid', '=', $data[ 'category_id' ] ] ], 'category_id');
|
||||
if ($child_list) {
|
||||
model('goods_category')->update([ 'level' => (int) $data[ 'level' ] + 1 ], [ [ 'category_id', 'in', $child_list ] ]);
|
||||
$child_child_list = model('goods_category')->getColumn([ [ 'site_id', '=', $site_id ], [ 'pid', 'in', $child_list ] ], 'category_id');
|
||||
model('goods_category')->update([ 'level' => (int) $data[ 'level' ] + 2 ], [ [ 'category_id', 'in', $child_child_list ] ]);
|
||||
}
|
||||
Cache::tag($this->cache_model)->clear();
|
||||
model('goods_category')->commit();
|
||||
return $this->success($res);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Cache::tag($this->cache_model)->clear();
|
||||
model('goods_category')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除分类
|
||||
* @param $category_id
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function deleteCategory($category_id, $site_id)
|
||||
{
|
||||
$site_id = $site_id ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$goods_category_info = $this->getCategoryInfo([
|
||||
[ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ]
|
||||
], "level")[ 'data' ];
|
||||
$field = "category_id_" . $goods_category_info[ 'level' ];
|
||||
|
||||
$res = model('goods_category')->delete([ [ $field, '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
model('goods_category')->delete([ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag($this->cache_model)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分类信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getCategoryInfo($condition, $field = 'category_id,category_name,short_name,pid,level,is_recommend,is_show,sort,image,keywords,description,attr_class_id,attr_class_name,category_id_1,category_id_2,category_id_3,category_full_name,commission_rate,image_adv,link_url,icon')
|
||||
{
|
||||
$res = model('goods_category')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分类列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryList($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_recommend,is_show,sort,image,attr_class_id,attr_class_name,category_id_1,category_id_2,category_id_3,commission_rate,image_adv,icon', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('goods_category')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分类树结构
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryTree($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_recommend,is_show,sort,image,attr_class_name,category_id_1,category_id_2,category_id_3,commission_rate,icon', $order = 'sort asc,category_id desc', $limit = null)
|
||||
{
|
||||
$cache_name = $this->cache_model . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name, "");
|
||||
if (!empty($cache)) {
|
||||
return $this->success($cache);
|
||||
}
|
||||
$list = model('goods_category')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
$goods_category_list = [];
|
||||
//遍历一级商品分类
|
||||
foreach ($list as $k => $v) {
|
||||
if ($v[ 'level' ] == 1) {
|
||||
$goods_category_list[] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
|
||||
$list = array_values($list);
|
||||
|
||||
//遍历二级商品分类
|
||||
foreach ($list as $k => $v) {
|
||||
foreach ($goods_category_list as $ck => $cv) {
|
||||
if ($v[ 'level' ] == 2 && $cv[ 'category_id' ] == $v[ 'pid' ]) {
|
||||
$goods_category_list[ $ck ][ 'child_list' ][] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$list = array_values($list);
|
||||
|
||||
//遍历三级商品分类
|
||||
foreach ($list as $k => $v) {
|
||||
foreach ($goods_category_list as $ck => $cv) {
|
||||
|
||||
if (!empty($cv[ 'child_list' ])) {
|
||||
foreach ($cv[ 'child_list' ] as $third_k => $third_v) {
|
||||
|
||||
if ($v[ 'level' ] == 3 && $third_v[ 'category_id' ] == $v[ 'pid' ]) {
|
||||
$goods_category_list[ $ck ][ 'child_list' ][ $third_k ][ 'child_list' ][] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Cache::tag($this->cache_model)->set($cache_name, $goods_category_list);
|
||||
|
||||
return $this->success($goods_category_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分类分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'category_id,category_name,short_name,pid,level,is_recommend,is_show,sort,image,category_id_1,category_id_2,category_id_3,category_full_name,commission_rate,icon')
|
||||
{
|
||||
$list = model('goods_category')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分类列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryByParent($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_recommend,is_show,sort,image,attr_class_id,attr_class_name,category_id_1,category_id_2,category_id_3,commission_rate,icon', $order = 'sort asc,category_id desc', $limit = null)
|
||||
{
|
||||
$cache_name = $this->cache_model . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
|
||||
$cache = Cache::get($cache_name, "");
|
||||
if (!empty($cache)) {
|
||||
return $this->success($cache);
|
||||
}
|
||||
$list = model('goods_category')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
foreach ($list as $k => $v) {
|
||||
$child_count = model('goods_category')->getCount([ 'pid' => $v[ 'category_id' ] ]);
|
||||
$list[ $k ][ 'child_count' ] = $child_count;
|
||||
}
|
||||
Cache::tag($this->cache_model)->set($cache_name, $list);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改排序
|
||||
* @param int $sort
|
||||
* @param int $category_id
|
||||
*/
|
||||
public function modifyGoodsCategorySort($sort, $category_id, $site_id)
|
||||
{
|
||||
$res = model('goods_category')->update([ 'sort' => $sort ], [ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag($this->cache_model)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分类末端叶子id
|
||||
* @param $category_ids
|
||||
* @return array
|
||||
*/
|
||||
public function getGoodsCategoryLeafIds($category_ids)
|
||||
{
|
||||
$category_list = $this->getCategoryList([['category_id', 'in', $category_ids]], 'category_id, pid')['data'];
|
||||
$tree = list_to_tree($category_list, 'category_id', 'pid', 'child', 0);
|
||||
$category_ids = getTreeLeaf($tree, 'category_id', 'child');
|
||||
return $this->success($category_ids);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改分类显示状态
|
||||
* @param $category_id
|
||||
* @param $is_show
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function modifyGoodsCategoryShow($category_id,$is_show){
|
||||
$res = model('goods_category')->getInfo([['category_id','=',$category_id]], '*');
|
||||
if(empty($res)){
|
||||
return $this->error([],'数据为空');
|
||||
}
|
||||
if($is_show == 0){
|
||||
return $this->modifyGoodsCategoryShowOn($res);
|
||||
}else{
|
||||
return $this->modifyGoodsCategoryShowOff($res);
|
||||
}
|
||||
}
|
||||
|
||||
public function modifyGoodsCategoryShowOff($info){
|
||||
$level = $info['level'];
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
$result = model('goods_category')->update([ 'is_show' => -1 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
break;
|
||||
case 2:
|
||||
$result = model('goods_category')->update([ 'is_show' => -1 ], [ [ 'category_id_2', '=', $info[ 'category_id_2' ] ] ]);
|
||||
break;
|
||||
case 3:
|
||||
default:
|
||||
$result = false;
|
||||
}
|
||||
$result = model('goods_category')->update([ 'is_show' => -1 ], [ [ 'category_id', '=', $info[ 'category_id' ] ] ]);
|
||||
Cache::tag($this->cache_model)->clear();
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
|
||||
public function modifyGoodsCategoryShowOn($info){
|
||||
|
||||
$level = $info['level'];
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
$result = model('goods_category')->update([ 'is_show' => 0 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
break;
|
||||
case 2:
|
||||
$result = model('goods_category')->update([ 'is_show' => 0 ], [ [ 'category_id', '=', $info[ 'category_id_1' ] ] ]);
|
||||
break;
|
||||
case 3:
|
||||
default:
|
||||
$result = false;
|
||||
}
|
||||
$result = model('goods_category')->update([ 'is_show' => 0 ], [ [ 'category_id', '=', $info[ 'category_id' ] ] ]);
|
||||
Cache::tag($this->cache_model)->clear();
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
128
app/model/goods/GoodsCollect.php
Executable file
128
app/model/goods/GoodsCollect.php
Executable file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Stat;
|
||||
|
||||
/**
|
||||
* 商品收藏
|
||||
*/
|
||||
class GoodsCollect extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加收藏
|
||||
* @param array $data
|
||||
*/
|
||||
public function addCollect($data)
|
||||
{
|
||||
$res = model('goods_collect')->getInfo([ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'goods_id', '=', $data[ 'goods_id' ] ] ], 'site_id, collect_id');
|
||||
if (empty($res)) {
|
||||
$data[ 'create_time' ] = time();
|
||||
$collect_id = model('goods_collect')->add($data);
|
||||
if ($collect_id) {
|
||||
model('goods_sku')->setInc([ [ 'goods_id', '=', $data[ 'goods_id' ] ] ], 'collect_num', 1);
|
||||
}
|
||||
//添加统计
|
||||
$stat = new Stat();
|
||||
// $stat->addShopStat(['collect_goods' => 1, 'site_id' => $data['site_id']]);
|
||||
$stat->switchStat([ 'type' => 'collect_goods', 'data' => [ 'collect_goods' => 1, 'site_id' => $data[ 'site_id' ] ] ]);
|
||||
|
||||
return $this->success($collect_id);
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消收藏
|
||||
* @param int $member_id
|
||||
* @param int $goods_id
|
||||
*/
|
||||
public function deleteCollect($member_id, $goods_id)
|
||||
{
|
||||
$res = model('goods_collect')->delete([ [ 'member_id', '=', $member_id ], [ 'goods_id', '=', $goods_id ] ]);
|
||||
if ($res) {
|
||||
model('goods_sku')->setDec([ [ 'goods_id', '=', $goods_id ] ], 'collect_num', 1);
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测商品是否收藏
|
||||
* @param $goods_id
|
||||
* @param unknown $member_id
|
||||
* @return array
|
||||
*/
|
||||
public function getIsCollect($goods_id, $member_id)
|
||||
{
|
||||
$res = model('goods_collect')->getInfo([ [ 'member_id', '=', $member_id ], [ 'goods_id', '=', $goods_id ] ], 'collect_id');
|
||||
if (!empty($res)) {
|
||||
return $this->success(1);
|
||||
} else {
|
||||
return $this->success(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取收藏列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getCollectList($condition = [], $field = 'collect_id, member_id, goods_id, sku_id, category_id, sku_name, sku_price, sku_image, create_time', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('goods_collect')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取收藏分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getCollectPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'gc.create_time desc', $field = '')
|
||||
{
|
||||
|
||||
if (empty($field)) {
|
||||
$field = 'gc.collect_id, gc.member_id, gc.goods_id, gc.sku_id,sku.sku_name, gc.sku_price, gc.sku_image,g.goods_name,g.is_free_shipping,sku.promotion_type,sku.member_price,sku.discount_price,g.sale_num,g.price,g.market_price,g.is_virtual, g.goods_image';
|
||||
}
|
||||
$join = [
|
||||
[ 'goods g', 'gc.goods_id = g.goods_id', 'inner' ],
|
||||
[ 'goods_sku sku', 'g.sku_id = sku.sku_id', 'inner' ]
|
||||
];
|
||||
|
||||
$alias = 'gc';
|
||||
|
||||
$res = model('goods_collect')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
foreach ($res[ 'list' ] as $k => $v) {
|
||||
if (isset($v[ 'sale_num' ])) {
|
||||
$res[ 'list' ][ $k ][ 'sale_num' ] = numberFormat($res[ 'list' ][ $k ][ 'sale_num' ]);
|
||||
}
|
||||
if (isset($v[ 'goods_stock' ])) {
|
||||
$res[ 'list' ][ $k ][ 'goods_stock' ] = numberFormat($res[ 'list' ][ $k ][ 'goods_stock' ]);
|
||||
}
|
||||
if (isset($v[ 'virtual_sale' ])) {
|
||||
$res[ 'list' ][ $k ][ 'virtual_sale' ] = numberFormat($res[ 'list' ][ $k ][ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($v[ 'stock' ])) {
|
||||
$res[ 'list' ][ $k ][ 'stock' ] = numberFormat($res[ 'list' ][ $k ][ 'stock' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
}
|
||||
365
app/model/goods/GoodsCommon.php
Executable file
365
app/model/goods/GoodsCommon.php
Executable file
@@ -0,0 +1,365 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Db;
|
||||
|
||||
class GoodsCommon extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 刷新SKU商品规格项/规格值JSON字符串
|
||||
* @param int $goods_id 商品id
|
||||
* @param string $goods_spec_format 商品完整规格项/规格值json
|
||||
*/
|
||||
public function dealGoodsSkuSpecFormat($goods_id, $goods_spec_format)
|
||||
{
|
||||
if (empty($goods_spec_format)) return;
|
||||
|
||||
$goods_spec_format = json_decode($goods_spec_format, true);
|
||||
|
||||
//根据goods_id查询sku商品列表,查询:sku_id、sku_spec_format 列
|
||||
$sku_list = model('goods_sku')->getList([ [ 'goods_id', '=', $goods_id ], [ 'sku_spec_format', '<>', '' ] ], 'sku_id,sku_spec_format', 'sku_id asc');
|
||||
if (!empty($sku_list)) {
|
||||
|
||||
// $temp = 0;//测试性能,勿删
|
||||
|
||||
//循环SKU商品列表
|
||||
foreach ($sku_list as $k => $v) {
|
||||
// $temp++;
|
||||
|
||||
$sku_format = $goods_spec_format;//最终要存储的值
|
||||
$current_format = json_decode($v[ 'sku_spec_format' ], true);//当前SKU商品规格值json
|
||||
|
||||
$selected_data = [];//已选规格/规格值json
|
||||
|
||||
//1、找出已选规格/规格值json
|
||||
|
||||
//循环完整商品规格json
|
||||
foreach ($sku_format as $sku_k => $sku_v) {
|
||||
// $temp++;
|
||||
|
||||
//循环当前SKU商品规格json
|
||||
foreach ($current_format as $current_k => $current_v) {
|
||||
// $temp++;
|
||||
|
||||
//匹配规格项
|
||||
if ($current_v[ 'spec_id' ] == $sku_v[ 'spec_id' ]) {
|
||||
|
||||
//循环规格值
|
||||
foreach ($sku_v[ 'value' ] as $sku_value_k => $sku_value_v) {
|
||||
// $temp++;
|
||||
|
||||
//匹配规格值id
|
||||
if ($current_v[ 'spec_value_id' ] == $sku_value_v[ 'spec_value_id' ]) {
|
||||
$sku_format[ $sku_k ][ 'value' ][ $sku_value_k ][ 'selected' ] = true;
|
||||
$sku_format[ $sku_k ][ 'value' ][ $sku_value_k ][ 'sku_id' ] = $v[ 'sku_id' ];
|
||||
$selected_data[] = $sku_format[ $sku_k ][ 'value' ][ $sku_value_k ];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//2、找出未选中的规格/规格值json
|
||||
foreach ($sku_format as $sku_k => $sku_v) {
|
||||
// $temp++;
|
||||
|
||||
foreach ($sku_v[ 'value' ] as $sku_value_k => $sku_value_v) {
|
||||
// $temp++;
|
||||
|
||||
if (!isset($sku_value_v[ 'selected' ])) {
|
||||
|
||||
$refer_data = [];//参考已选中的规格/规格值json
|
||||
$refer_data[] = $sku_value_v;
|
||||
|
||||
// 根据已选中的规格值进行参考
|
||||
foreach ($selected_data as $selected_k => $selected_v) {
|
||||
// $temp++;
|
||||
// 排除自身,然后进行参考
|
||||
if ($selected_v[ 'spec_id' ] != $sku_value_v[ 'spec_id' ]) {
|
||||
$refer_data[] = $selected_v;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($sku_list as $again_k => $again_v) {
|
||||
// $temp++;
|
||||
|
||||
//排除当前SKU商品
|
||||
if ($again_v[ 'sku_id' ] != $v[ 'sku_id' ]) {
|
||||
|
||||
$current_format_again = json_decode($again_v[ 'sku_spec_format' ], true);
|
||||
$count = count($current_format_again);//规格总数量
|
||||
$curr_count = 0;//当前匹配规格数量
|
||||
|
||||
//循环当前SKU商品规格json
|
||||
foreach ($current_format_again as $current_again_k => $current_again_v) {
|
||||
// $temp++;
|
||||
|
||||
foreach ($refer_data as $fan_k => $fan_v) {
|
||||
// $temp++;
|
||||
|
||||
if ($current_again_v[ 'spec_value_id' ] == $fan_v[ 'spec_value_id' ]) {
|
||||
$curr_count++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 匹配数量跟规格总数一致表示匹配成功
|
||||
if ($curr_count == $count) {
|
||||
$sku_format[ $sku_k ][ 'value' ][ $sku_value_k ][ 'selected' ] = false;
|
||||
$sku_format[ $sku_k ][ 'value' ][ $sku_value_k ][ 'sku_id' ] = $again_v[ 'sku_id' ];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//没有匹配到规格值,则禁用
|
||||
if (!isset($sku_format[ $sku_k ][ 'value' ][ $sku_value_k ][ 'selected' ])) {
|
||||
$sku_format[ $sku_k ][ 'value' ][ $sku_value_k ][ 'disabled' ] = false;
|
||||
// var_dump(json_encode($sku_format));
|
||||
// var_dump('==========');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// var_dump($sku_format);
|
||||
// var_dump("=========");
|
||||
//修改ns_goods_sku表表中的goods_spec_format字段,将$sku_format值传入
|
||||
model('goods_sku')->update([ 'goods_spec_format' => json_encode($sku_format) ], [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ]);
|
||||
|
||||
}
|
||||
|
||||
// var_dump("性能:" . $temp);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测商品编码重复
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function checkSkuNoRepeat($params)
|
||||
{
|
||||
/*$params = [
|
||||
'sku_list' => [],
|
||||
'site_id' => 1,
|
||||
'goods_id' => 0,
|
||||
];*/
|
||||
|
||||
//查看配置是否开启
|
||||
$config_model = new \app\model\web\Config();
|
||||
$info = $config_model->getGoodsNo($params['site_id'])['data']['value'];
|
||||
if ($info['uniqueness_switch'] == 0) {
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
//商品本身编码重复检测
|
||||
$all_sku_no_arr = [];
|
||||
foreach($params['sku_list'] as $sku_info){
|
||||
if(!empty($sku_info['sku_no'])){
|
||||
$sku_arr = explode(',', $sku_info['sku_no']);
|
||||
foreach($sku_arr as $sku_no){
|
||||
if(in_array($sku_no, $all_sku_no_arr)){
|
||||
return $this->error(null, "编码[{$sku_no}]不可重复录入");
|
||||
}
|
||||
$all_sku_no_arr[] = $sku_no;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if(empty($all_sku_no_arr)) return $this->success();
|
||||
|
||||
//与其他商品是否重复检测
|
||||
$sql_arr = [];
|
||||
foreach($all_sku_no_arr as $sku_no){
|
||||
$sql_arr[] = "FIND_IN_SET('{$sku_no}', sku_no)";
|
||||
}
|
||||
$condition = [
|
||||
['site_id', '=', $params['site_id']],
|
||||
['', 'exp', Db::raw(join(' or ', $sql_arr))],
|
||||
['is_delete', '=', 0]
|
||||
];
|
||||
if (!empty($params['goods_id'])) {
|
||||
$condition[] = ['goods_id', '<>', $params['goods_id']];
|
||||
}
|
||||
$info = model('goods_sku')->getInfo($condition, 'sku_no');
|
||||
if (!empty($info)) {
|
||||
$exist_sku_no_arr = array_intersect($all_sku_no_arr, explode(',', $info['sku_no']));
|
||||
$exist_sku_no_arr = array_values($exist_sku_no_arr);
|
||||
return $this->error(null, "条码[{$exist_sku_no_arr[0]}]已存在");
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品规格检测
|
||||
* @param $sku_ids
|
||||
* @return array
|
||||
*/
|
||||
public function deleteGoodsSkuCheck($sku_ids)
|
||||
{
|
||||
$check_res = event('DeleteGoodsCheck', ['ids' => $sku_ids, 'field' => 'sku_id']);
|
||||
foreach ($check_res as $val){
|
||||
if(!empty($val['cannot_delete_ids'])){
|
||||
$sku_list = model('goods_sku')->getList([['sku_id', 'in', $val['cannot_delete_ids']]], 'sku_name,spec_name');
|
||||
$spec_name = $sku_list[0]['spec_name'];
|
||||
if(empty($spec_name)) $spec_name = '默认规格';
|
||||
return $this->error(null, "规格[".$spec_name."]".$val['reason'].",不可删除");
|
||||
}
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 矫正商品属性数据
|
||||
* @param $goods_info
|
||||
*/
|
||||
public function correctGoodsSpecFormat($goods_info)
|
||||
{
|
||||
if(!empty($goods_info['goods_spec_format'])){
|
||||
$goods_spec_format = json_decode($goods_info['goods_spec_format'], true);
|
||||
$spec_value_ids = [];
|
||||
$repeat_data = [];
|
||||
$min_spec_value_id = 0;
|
||||
foreach($goods_spec_format as $spec_index => $spec){
|
||||
foreach($spec['value'] as $spec_value_index => $spec_value){
|
||||
$spec_value_id = $spec_value['spec_value_id'];
|
||||
if(!in_array($spec_value_id, $spec_value_ids)){
|
||||
$spec_value_ids[] = $spec_value_id;
|
||||
}else{
|
||||
$repeat_data[] = [
|
||||
'spec_index' => $spec_index,
|
||||
'spec_value_index' => $spec_value_index,
|
||||
];
|
||||
}
|
||||
if($spec_value_id < $min_spec_value_id){
|
||||
$min_spec_value_id = $spec_value_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!empty($repeat_data)){
|
||||
foreach($repeat_data as $val){
|
||||
$min_spec_value_id -= 1;
|
||||
$goods_spec_format[$val['spec_index']]['value'][$val['spec_value_index']]['spec_value_id'] = $min_spec_value_id;
|
||||
}
|
||||
}
|
||||
$goods_info['goods_spec_format'] = json_encode($goods_spec_format);
|
||||
|
||||
$goods_spec_format = array_column($goods_spec_format, null, 'spec_name');
|
||||
foreach($goods_spec_format as &$spec){
|
||||
$spec['value'] = array_column($spec['value'], null, 'spec_value_name');
|
||||
}
|
||||
|
||||
foreach($goods_info['sku_list'] as &$sku_info){
|
||||
$sku_spec_format = json_decode($sku_info['sku_spec_format'], true);
|
||||
foreach($sku_spec_format as &$spec_value){
|
||||
$spec_value_id = $goods_spec_format[$spec_value['spec_name']]['value'][$spec_value['spec_value_name']]['spec_value_id'] ?? null;
|
||||
if(!is_null($spec_value_id) && $spec_value['spec_value_id'] != $spec_value_id){
|
||||
$spec_value['spec_value_id'] = $spec_value_id;
|
||||
}
|
||||
}
|
||||
$sku_info['sku_spec_format'] = json_encode($sku_spec_format);
|
||||
}
|
||||
}
|
||||
return $goods_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @param $item_list
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCategoryNames($item_list)
|
||||
{
|
||||
//分类id数据 服务商品goods_class=4,分类字段是service_category
|
||||
$category_ids_arr = [];
|
||||
$service_category_ids_arr = [];
|
||||
foreach($item_list as $item){
|
||||
if($item['goods_class'] != 4){
|
||||
$category_ids = trim($item['category_id'], ',');
|
||||
if($category_ids) $category_ids_arr[] = $category_ids;
|
||||
}else{
|
||||
$service_category_ids = trim($item['service_category'], ',');
|
||||
if($service_category_ids) $service_category_ids_arr[] = $service_category_ids;
|
||||
}
|
||||
}
|
||||
//所有分类数据
|
||||
$category_ids = array_unique(explode(',', join(',', $category_ids_arr)));
|
||||
$category_list = model('goods_category')->getList([['category_id', 'in', $category_ids]], 'category_id,category_name');
|
||||
$category_list = array_column($category_list, null, 'category_id');
|
||||
//所有服务分类数据
|
||||
$service_category_ids = array_unique(explode(',', join(',', $service_category_ids_arr)));
|
||||
$service_category_list = model('service_category')->getList([['category_id', 'in', $service_category_ids]], 'category_id,category_name');
|
||||
$service_category_list = array_column($service_category_list, null, 'category_id');
|
||||
|
||||
foreach ($item_list as &$item_v) {
|
||||
//分类数据
|
||||
$category_names_arr = [];
|
||||
if($item_v['goods_class'] != 4){
|
||||
if(!empty($item_v['category_json'])){
|
||||
$category_ids_arr = json_decode($item_v['category_json'], true);
|
||||
foreach($category_ids_arr as $category_ids){
|
||||
$category_ids = explode(',', $category_ids);
|
||||
$category_names = [];
|
||||
foreach($category_ids as $category_id){
|
||||
$category_name = $category_list[$category_id]['category_name'] ?? '';
|
||||
if($category_name) $category_names[] = $category_name;
|
||||
}
|
||||
$category_names = join('/',$category_names);
|
||||
$category_names_arr[] = $category_names;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$category_names = [];
|
||||
$category_ids = explode(',', trim($item_v['service_category']));
|
||||
foreach($category_ids as $category_id){
|
||||
$category_name = $service_category_list[$category_id]['category_name'] ?? '';
|
||||
if($category_name) $category_names[] = $category_name;
|
||||
}
|
||||
$category_names = join('/',$category_names);
|
||||
$category_names_arr[] = $category_names;
|
||||
}
|
||||
$item_v['category_names'] = join(';', $category_names_arr);
|
||||
}
|
||||
return $item_list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测商品核销日期
|
||||
*/
|
||||
|
||||
public function checkVirtualDate($verify_validity_type,$virtual_indate=''){
|
||||
if ($verify_validity_type == 1) {
|
||||
if (empty($virtual_indate)) {
|
||||
return $this->error('', '有效期不能为空');
|
||||
}
|
||||
} else if ($verify_validity_type == 2) {
|
||||
if (empty($virtual_indate)) {
|
||||
return $this->error('', '有效期不能为空');
|
||||
}
|
||||
if($virtual_indate < time()){
|
||||
return $this->error('', '核销有效期不能小于当前日期');
|
||||
}
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
443
app/model/goods/GoodsEvaluate.php
Executable file
443
app/model/goods/GoodsEvaluate.php
Executable file
@@ -0,0 +1,443 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\dict\order\OrderDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\Config as ConfigModel;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 商品评价
|
||||
*/
|
||||
class GoodsEvaluate extends BaseModel
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 添加评价
|
||||
* @param array $data
|
||||
*/
|
||||
public function addEvaluate($data, $site_id)
|
||||
{
|
||||
model('goods')->startTrans();
|
||||
try {
|
||||
$res = model('goods_evaluate')->getInfo([ [ 'order_id', '=', $data[ 'order_id' ] ] ], 'evaluate_id');
|
||||
if (empty($res)) {
|
||||
|
||||
$config_model = new ConfigModel();
|
||||
//订单评价设置
|
||||
$order_evaluate_config = $config_model->getOrderEvaluateConfig($site_id)[ 'data' ][ 'value' ];
|
||||
|
||||
$data_arr = [];
|
||||
foreach ($data[ 'goods_evaluate' ] as $k => $v) {
|
||||
if (empty($v[ 'content' ])) {
|
||||
model('goods')->rollback();
|
||||
return $this->error('', '商品的评价不能为空!');
|
||||
}
|
||||
$item = [
|
||||
'order_id' => $data[ 'order_id' ],
|
||||
'order_no' => $data[ 'order_no' ],
|
||||
'member_id' => $data[ 'member_id' ],
|
||||
'member_name' => $data[ 'member_name' ],
|
||||
'member_headimg' => $data[ 'member_headimg' ],
|
||||
'is_anonymous' => $data[ 'is_anonymous' ],
|
||||
'order_goods_id' => $v[ 'order_goods_id' ],
|
||||
'goods_id' => $v[ 'goods_id' ],
|
||||
'sku_id' => $v[ 'sku_id' ],
|
||||
'site_id' => $site_id,
|
||||
'sku_name' => $v[ 'sku_name' ],
|
||||
'sku_price' => $v[ 'sku_price' ],
|
||||
'sku_image' => $v[ 'sku_image' ],
|
||||
'content' => !empty($v[ 'content' ]) ? $v[ 'content' ] : '此用户没有填写评价。',
|
||||
'images' => $v[ 'images' ],
|
||||
'scores' => $v[ 'scores' ],
|
||||
'explain_type' => $v[ 'explain_type' ],
|
||||
'is_audit' => ( $order_evaluate_config[ 'evaluate_audit' ] == 1 ? 0 : 1 ),
|
||||
'create_time' => time()
|
||||
];
|
||||
$data_arr[] = $item;
|
||||
|
||||
$evaluate = 0; //评价
|
||||
$evaluate_shaitu = 0; //晒图
|
||||
$evaluate_shipin = 0; //视频
|
||||
$evaluate_haoping = 0; //好评
|
||||
$evaluate_zhongping = 0; //中评
|
||||
$evaluate_chaping = 0; //差评
|
||||
if ($v[ 'explain_type' ] == 1) {
|
||||
//好评
|
||||
$evaluate = 1; //评价
|
||||
$evaluate_haoping = 1; //好评
|
||||
|
||||
} elseif ($v[ 'explain_type' ] == 2) {
|
||||
//中评
|
||||
$evaluate = 1; //评价
|
||||
$evaluate_zhongping = 1; //中评
|
||||
|
||||
} elseif ($v[ 'explain_type' ] == 3) {
|
||||
//差评
|
||||
$evaluate = 1; //评价
|
||||
$evaluate_chaping = 1; //差评
|
||||
}
|
||||
if (!empty($v[ 'images' ])) {
|
||||
$evaluate_shaitu = 1; //晒图
|
||||
}
|
||||
|
||||
if ($order_evaluate_config[ 'evaluate_audit' ] == 0) {
|
||||
Db::name('goods')->where([ [ 'goods_id', '=', $v[ 'goods_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'evaluate' => Db::raw('evaluate+' . $evaluate),
|
||||
'evaluate_shaitu' => Db::raw('evaluate_shaitu+' . $evaluate_shaitu),
|
||||
'evaluate_haoping' => Db::raw('evaluate_haoping+' . $evaluate_haoping),
|
||||
'evaluate_zhongping' => Db::raw('evaluate_zhongping+' . $evaluate_zhongping),
|
||||
'evaluate_chaping' => Db::raw('evaluate_chaping+' . $evaluate_chaping),
|
||||
]);
|
||||
Db::name('goods_sku')->where([ [ 'sku_id', '=', $v[ 'sku_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'evaluate' => Db::raw('evaluate+' . $evaluate),
|
||||
'evaluate_shaitu' => Db::raw('evaluate_shaitu+' . $evaluate_shaitu),
|
||||
'evaluate_haoping' => Db::raw('evaluate_haoping+' . $evaluate_haoping),
|
||||
'evaluate_zhongping' => Db::raw('evaluate_zhongping+' . $evaluate_zhongping),
|
||||
'evaluate_chaping' => Db::raw('evaluate_chaping+' . $evaluate_chaping),
|
||||
]);
|
||||
}
|
||||
}
|
||||
//记录订单日志 start
|
||||
$order_common_model = new OrderCommon();
|
||||
$member_info = model('member')->getInfo([ 'member_id' => $data[ 'member_id' ] ], 'nickname');
|
||||
$order_info = model('order')->getInfo([ 'order_id' => $data[ 'order_id' ] ], 'order_status,order_status_name');
|
||||
$log_data = [
|
||||
'order_id' => $data[ 'order_id' ],
|
||||
'action' => '买家评价了订单',
|
||||
'uid' => $data[ 'member_id' ],
|
||||
'nick_name' => $member_info[ 'nickname' ],
|
||||
'action_way' => 1,
|
||||
'order_status' => $order_info[ 'order_status' ],
|
||||
'order_status_name' => $order_info[ 'order_status_name' ]
|
||||
];
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
|
||||
//记录订单日志 end
|
||||
|
||||
// 修改订单表中的评价标识
|
||||
model('order')->update([ 'is_evaluate' => 1, 'evaluate_status' => OrderDict::evaluated, 'evaluate_status_name' => OrderDict::getEvaluateStatus(OrderDict::evaluated) ], [ [ 'order_id', '=', $data[ 'order_id' ] ] ]);
|
||||
$evaluate_id = model('goods_evaluate')->addList($data_arr);
|
||||
model('goods')->commit();
|
||||
Cache::tag('goods_evaluate')->clear();
|
||||
return $this->success($evaluate_id);
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 评价回复
|
||||
* @param unknown $data
|
||||
*/
|
||||
public function evaluateApply($data)
|
||||
{
|
||||
$res = model('goods_evaluate')->update($data, [ [ 'evaluate_id', '=', $data[ 'evaluate_id' ] ] ]);
|
||||
Cache::tag('goods_evaluate')->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改评价
|
||||
* @param $data
|
||||
* @param array $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editEvaluate($data, $condition = [])
|
||||
{
|
||||
$res = model('goods_evaluate')->update($data, $condition);
|
||||
Cache::tag('goods_evaluate')->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
//操作评价状态
|
||||
public function modifyAuditEvaluate($data, $condition = [])
|
||||
{
|
||||
$list = model('goods_evaluate')->getList($condition, 'goods_id,sku_id,is_audit,explain_type,images');
|
||||
if (!empty($list)) {
|
||||
|
||||
$goods_evaluate = 0; //评价
|
||||
$goods_evaluate_haoping = 0; //好评
|
||||
$goods_evaluate_zhongping = 0; //中评
|
||||
$goods_evaluate_chaping = 0; //差评
|
||||
$goods_evaluate_shaitu = 0; //晒图
|
||||
|
||||
$sku_evaluate = 0; //评价
|
||||
$sku_evaluate_haoping = 0; //好评
|
||||
$sku_evaluate_zhongping = 0; //中评
|
||||
$sku_evaluate_chaping = 0; //差评
|
||||
$sku_evaluate_shaitu = 0; //晒图
|
||||
|
||||
foreach ($list as $k => $v) {
|
||||
|
||||
if ($data[ 'is_audit' ] == 1) {
|
||||
$symbol = '+';
|
||||
if ($v[ 'explain_type' ] == 1) {
|
||||
//好评
|
||||
$goods_evaluate = 1; //评价
|
||||
$sku_evaluate = 1;
|
||||
$goods_evaluate_haoping = 1; //好评
|
||||
$sku_evaluate_haoping = 1;
|
||||
} elseif ($v[ 'explain_type' ] == 2) {
|
||||
//中评
|
||||
$goods_evaluate = 1; //评价
|
||||
$sku_evaluate = 1;
|
||||
$goods_evaluate_zhongping = 1; //中评
|
||||
$sku_evaluate_zhongping = 1;
|
||||
} elseif ($v[ 'explain_type' ] == 3) {
|
||||
//差评
|
||||
$goods_evaluate = 1; //评价
|
||||
$sku_evaluate = 1;
|
||||
$goods_evaluate_chaping = 1; //差评
|
||||
$sku_evaluate_chaping = 1;
|
||||
}
|
||||
|
||||
if (!empty($v[ 'images' ])) {
|
||||
$goods_evaluate_shaitu = 1; //晒图
|
||||
$sku_evaluate_shaitu = 1;
|
||||
}
|
||||
|
||||
Db::name('goods')->where([ [ 'goods_id', '=', $v[ 'goods_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'evaluate' => Db::raw('evaluate' . $symbol . $goods_evaluate),
|
||||
'evaluate_shaitu' => Db::raw('evaluate_shaitu' . $symbol . $goods_evaluate_shaitu),
|
||||
'evaluate_haoping' => Db::raw('evaluate_haoping' . $symbol . $goods_evaluate_haoping),
|
||||
'evaluate_zhongping' => Db::raw('evaluate_zhongping' . $symbol . $goods_evaluate_zhongping),
|
||||
'evaluate_chaping' => Db::raw('evaluate_chaping' . $symbol . $goods_evaluate_chaping),
|
||||
'wait_evaluate_num' => Db::raw('wait_evaluate_num-' . 1),
|
||||
'success_evaluate_num' => Db::raw('success_evaluate_num+' . 1),
|
||||
]);
|
||||
Db::name('goods_sku')->where([ [ 'sku_id', '=', $v[ 'sku_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'evaluate' => Db::raw('evaluate' . $symbol . $sku_evaluate),
|
||||
'evaluate_shaitu' => Db::raw('evaluate_shaitu' . $symbol . $sku_evaluate_shaitu),
|
||||
'evaluate_haoping' => Db::raw('evaluate_haoping' . $symbol . $sku_evaluate_haoping),
|
||||
'evaluate_zhongping' => Db::raw('evaluate_zhongping' . $symbol . $sku_evaluate_zhongping),
|
||||
'evaluate_chaping' => Db::raw('evaluate_chaping' . $symbol . $sku_evaluate_chaping),
|
||||
'wait_evaluate_num' => Db::raw('wait_evaluate_num-' . 1),
|
||||
'success_evaluate_num' => Db::raw('success_evaluate_num+' . 1),
|
||||
]);
|
||||
} else {
|
||||
Db::name('goods')->where([ [ 'goods_id', '=', $v[ 'goods_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'wait_evaluate_num' => Db::raw('wait_evaluate_num-' . 1),
|
||||
'fail_evaluate_num' => Db::raw('fail_evaluate_num+' . 1),
|
||||
]);
|
||||
Db::name('goods_sku')->where([ [ 'sku_id', '=', $v[ 'sku_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'wait_evaluate_num' => Db::raw('wait_evaluate_num-' . 1),
|
||||
'fail_evaluate_num' => Db::raw('fail_evaluate_num+' . 1),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
$res = model('goods_evaluate')->update($data, $condition);
|
||||
Cache::tag('goods_evaluate')->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
//操作追评状态
|
||||
public function modifyAgainAuditEvaluate($data, $condition = [])
|
||||
{
|
||||
$list = model('goods_evaluate')->getList($condition, 'goods_id,sku_id,is_audit,explain_type,images');
|
||||
$res = model('goods_evaluate')->update($data, $condition);
|
||||
|
||||
foreach ($list as $k => $v) {
|
||||
if ($res) {
|
||||
model('goods')->setInc([ [ 'goods_id', '=', $v[ 'goods_id' ] ] ], 'evaluate_zhuiping', 1);
|
||||
model('goods_sku')->setInc([ [ 'sku_id', '=', $v[ 'sku_id' ] ] ], 'evaluate_zhuiping', 1);
|
||||
}
|
||||
}
|
||||
|
||||
Cache::tag('goods_evaluate')->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改商品评价数量
|
||||
* @param $evaluate_ids
|
||||
* @return array
|
||||
*/
|
||||
public function modifyGoodsEvaluateCount($evaluate_ids)
|
||||
{
|
||||
$list = model('goods_evaluate')->getList([ [ 'evaluate_id', 'in', $evaluate_ids ], [ 'is_audit', '<>', 0 ] ], 'goods_id,sku_id,is_audit');
|
||||
if (!empty($list)) {
|
||||
$evaluate = 1; //评价
|
||||
$evaluate_shaitu = 1; //晒图
|
||||
$evaluate_haoping = 1; //好评
|
||||
$evaluate_zhongping = 1; //中评
|
||||
$evaluate_chaping = 1; //差评
|
||||
foreach ($list as $k => $v) {
|
||||
|
||||
if ($v[ 'is_audit' ] == 1) {
|
||||
// 审核拒绝
|
||||
$symbol = '+';
|
||||
|
||||
Db::name('goods')->where([ [ 'goods_id', '=', $v[ 'goods_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'evaluate' => Db::raw('evaluate' . $symbol . $evaluate),
|
||||
'evaluate_shaitu' => Db::raw('evaluate_shaitu' . $symbol . $evaluate_shaitu),
|
||||
'evaluate_haoping' => Db::raw('evaluate_haoping' . $symbol . $evaluate_haoping),
|
||||
'evaluate_zhongping' => Db::raw('evaluate_zhongping' . $symbol . $evaluate_zhongping),
|
||||
'evaluate_chaping' => Db::raw('evaluate_chaping' . $symbol . $evaluate_chaping),
|
||||
]);
|
||||
Db::name('goods_sku')->where([ [ 'sku_id', '=', $v[ 'sku_id' ] ] ])
|
||||
->update(
|
||||
[
|
||||
'evaluate' => Db::raw('evaluate' . $symbol . $evaluate),
|
||||
'evaluate_shaitu' => Db::raw('evaluate_shaitu' . $symbol . $evaluate_shaitu),
|
||||
'evaluate_haoping' => Db::raw('evaluate_haoping' . $symbol . $evaluate_haoping),
|
||||
'evaluate_zhongping' => Db::raw('evaluate_zhongping' . $symbol . $evaluate_zhongping),
|
||||
'evaluate_chaping' => Db::raw('evaluate_chaping' . $symbol . $evaluate_chaping),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 追评
|
||||
* @param array $data
|
||||
* @return multitype:string
|
||||
*/
|
||||
public function evaluateAgain($data, $site_id)
|
||||
{
|
||||
$config_model = new ConfigModel();
|
||||
//订单评价设置
|
||||
$order_evaluate_config = $config_model->getOrderEvaluateConfig($site_id)[ 'data' ][ 'value' ];
|
||||
|
||||
foreach ($data[ 'goods_evaluate' ] as $k => $v) {
|
||||
$item = [
|
||||
'order_id' => $data[ 'order_id' ],
|
||||
'order_goods_id' => $v[ 'order_goods_id' ],
|
||||
'goods_id' => $v[ 'goods_id' ],
|
||||
'sku_id' => $v[ 'sku_id' ],
|
||||
'again_content' => $v[ 'again_content' ],
|
||||
'again_images' => $v[ 'again_images' ],
|
||||
'again_time' => time(),
|
||||
'again_is_audit' => ( $order_evaluate_config[ 'evaluate_audit' ] == 1 ? 0 : 1 ),
|
||||
];
|
||||
$res = model('goods_evaluate')->update($item, [ [ 'order_goods_id', '=', $v[ 'order_goods_id' ] ] ]);
|
||||
|
||||
if ($order_evaluate_config[ 'evaluate_audit' ] == 0) {
|
||||
if ($res) {
|
||||
model('goods')->setInc([ [ 'goods_id', '=', $v[ 'goods_id' ] ] ], 'evaluate_zhuiping', 1);
|
||||
model('goods_sku')->setInc([ [ 'sku_id', '=', $v[ 'sku_id' ] ] ], 'evaluate_zhuiping', 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
model('order')->update([ 'is_evaluate' => 0, 'evaluate_status' => OrderDict::evaluate_again, 'evaluate_status_name' => OrderDict::getEvaluateStatus(OrderDict::evaluate_again) ], [ [ 'order_id', '=', $data[ 'order_id' ] ] ]);
|
||||
Cache::tag('goods_evaluate')->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评价
|
||||
* @param $evaluate_ids
|
||||
* @return array
|
||||
*/
|
||||
public function deleteEvaluate($evaluate_ids)
|
||||
{
|
||||
$res = model('goods_evaluate')->delete([ [ 'evaluate_id', 'in', $evaluate_ids ] ]);
|
||||
Cache::tag('goods_evaluate')->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取评价信息
|
||||
* @param $condition
|
||||
* @param $field
|
||||
* @param $order
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getFirstEvaluateInfo($condition, $field = 'evaluate_id,order_goods_id,goods_id,sku_id,sku_name,sku_price,content,images,explain_first,member_name,member_headimg,member_id,is_anonymous,again_content,again_images,again_explain,create_time,again_time', $order = 'create_time desc')
|
||||
{
|
||||
$info = model('goods_evaluate')->getFirstData($condition, $field, $order);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取评价信息
|
||||
* @param $condition
|
||||
* @param $field
|
||||
* @param $order
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getSecondEvaluateInfo($condition, $field = 'evaluate_id,order_goods_id,goods_id,sku_id,sku_name,sku_price,content,images,explain_first,member_name,member_headimg,member_id,is_anonymous,again_content,again_images,again_explain,create_time,again_time,scores', $order = 'create_time desc')
|
||||
{
|
||||
$info = model('goods_evaluate')->getList($condition, $field, $order, '', '', '', 2);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取评价列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getEvaluateList($condition = [], $field = 'evaluate_id, order_id, order_no, order_goods_id, goods_id, sku_id, sku_name, sku_price, sku_image, content, images, explain_first, member_name, member_id, is_anonymous, scores, again_content, again_images, again_explain, explain_type, is_show, create_time, again_time,shop_desccredit,shop_servicecredit,shop_deliverycredit', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('goods_evaluate')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取评价分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getEvaluatePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = 'evaluate_id, order_id, order_no, order_goods_id, goods_id, sku_id, sku_name, sku_price, sku_image, content, images, explain_first, member_name,member_headimg, member_id, is_anonymous, scores, again_content, again_images, again_explain, explain_type, is_show, create_time, again_time,shop_desccredit,shop_servicecredit,shop_deliverycredit,is_audit,again_is_audit')
|
||||
{
|
||||
$list = model('goods_evaluate')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询评论数量
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getEvaluateCount($condition)
|
||||
{
|
||||
$count = model('goods_evaluate')->getCount($condition);
|
||||
return $this->success($count);
|
||||
}
|
||||
|
||||
}
|
||||
237
app/model/goods/GoodsExport.php
Executable file
237
app/model/goods/GoodsExport.php
Executable file
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use addon\supply\model\Supplier as SupplierModel;
|
||||
use app\model\BaseModel;
|
||||
use app\model\goods\Goods as GoodsModel;
|
||||
use app\model\goods\GoodsBrand as GoodsBrandModel;
|
||||
use app\model\goods\GoodsCategory as GoodsCategoryModel;
|
||||
use app\model\goods\GoodsLabel as GoodsLabelModel;
|
||||
use app\model\system\Export as ExportModel;
|
||||
|
||||
/**
|
||||
* 商品导出
|
||||
* @author Administrator
|
||||
*/
|
||||
class GoodsExport extends BaseModel
|
||||
{
|
||||
public function export($input, $param)
|
||||
{
|
||||
$site_id = $param['site_id'];
|
||||
|
||||
$search_text = $input['search_text'] ?? '';
|
||||
$goods_state = $input['goods_state'] ?? '';
|
||||
$start_sale = $input['start_sale'] ?? 0;
|
||||
$end_sale = $input['end_sale'] ?? 0;
|
||||
$start_price = $input['start_price'] ?? 0;
|
||||
$end_price = $input['end_price'] ?? 0;
|
||||
$goods_class = $input['goods_class'] ?? '';
|
||||
$label_id = $input['label_id'] ?? '';
|
||||
$brand_id = $input['brand_id'] ?? '';
|
||||
$sku_no = $input['sku_no'] ?? '';
|
||||
$promotion_type = $input['promotion_type'] ?? '';
|
||||
$category_id = $input['category_id'] ?? '';
|
||||
$stockalarm = $input['stockalarm'] ?? 0;
|
||||
$goods_ids = $input['goods_ids'] ?? '';
|
||||
$supplier_id = $input['supplier_id'] ?? 0;
|
||||
|
||||
$goods_model = new GoodsModel();
|
||||
$is_install_supply = addon_is_exit('supply');
|
||||
|
||||
$condition_desc = [];
|
||||
$condition = [
|
||||
['g.is_delete', '=', 0],
|
||||
['g.site_id', '=', $site_id],
|
||||
];
|
||||
if (!empty($goods_ids)) {
|
||||
$condition[] = ['g.goods_id', 'in', explode(',', $goods_ids)];
|
||||
$condition_desc[] = ['name' => 'goods_id', 'value' => $goods_ids];
|
||||
}
|
||||
if (!empty($search_text)) {
|
||||
$condition[] = ['g.goods_name', 'like', '%' . $search_text . '%'];
|
||||
$condition_desc[] = ['name' => '商品名称', 'value' => $search_text];
|
||||
}
|
||||
if (!empty($sku_no)) {
|
||||
$condition[] = ['gs.sku_no', 'like', '%' . $sku_no . '%'];
|
||||
$condition_desc[] = ['name' => '商品编码', 'value' => $sku_no];
|
||||
}
|
||||
if (!empty($category_id)) {
|
||||
$condition[] = ['g.category_id', 'like', '%,' . $category_id . ',%'];
|
||||
$goods_category_model = new GoodsCategoryModel();
|
||||
$category_info = $goods_category_model->getCategoryInfo([
|
||||
['category_id', '=', $category_id],
|
||||
['site_id', '=', $site_id]
|
||||
], 'category_full_name')[ 'data' ];
|
||||
if (!empty($category_info)) {
|
||||
$condition_desc[] = ['name' => '商品分类', 'value' => $category_info[ 'category_full_name' ]];
|
||||
}
|
||||
}
|
||||
if (!empty($brand_id)) {
|
||||
$condition[] = ['g.brand_id', '=', $brand_id];
|
||||
$goods_brand_model = new GoodsBrandModel();
|
||||
$goods_brand_info = $goods_brand_model->getBrandInfo([
|
||||
['site_id', '=', $site_id],
|
||||
], 'brand_name')[ 'data' ];
|
||||
if (!empty($goods_brand_info)) {
|
||||
$brand_name = $goods_brand_info[ 'brand_name' ];
|
||||
$condition_desc[] = ['name' => '商品品牌', 'value' => $brand_name];
|
||||
}
|
||||
}
|
||||
if ($goods_class !== '') {
|
||||
$condition[] = ['g.goods_class', '=', $goods_class];
|
||||
$goods_class_info = array_column(event('GoodsClass'), null, 'goods_class');
|
||||
$goods_class_name = $goods_class_info[ $goods_class ][ 'goods_class_name' ];
|
||||
$condition_desc[] = ['name' => '商品类型', 'value' => $goods_class_name];
|
||||
}
|
||||
if (!empty($label_id)) {
|
||||
$condition[] = ['g.label_id', '=', $label_id];
|
||||
$goods_label_model = new GoodsLabelModel();
|
||||
$label_info = $goods_label_model->getLabelInfo([
|
||||
['site_id', '=', $site_id],
|
||||
['id', '=', $label_id]
|
||||
], 'label_name')[ 'data' ];
|
||||
if (!empty($label_info)) {
|
||||
$label_name = $label_info[ 'label_name' ];
|
||||
$condition_desc[] = ['name' => '商品标签', 'value' => $label_name];
|
||||
}
|
||||
}
|
||||
if (!empty($promotion_type)) {
|
||||
$condition[] = ['g.promotion_addon', 'like', "%{$promotion_type}%"];
|
||||
$goods_promotion_type = event('GoodsPromotionType');
|
||||
$goods_promotion_type = array_column($goods_promotion_type, null, 'type');
|
||||
$condition_desc[] = ['name' => '营销活动', 'value' => $goods_promotion_type[$promotion_type]['name'] ?? ''];
|
||||
}
|
||||
if ($goods_state !== '') {
|
||||
$condition[] = ['g.goods_state', '=', $goods_state];
|
||||
$condition_desc[] = ['name' => '商品状态', 'value' => $goods_state == 1 ? '销售中' : '仓库中'];
|
||||
}
|
||||
if ($is_install_supply) {
|
||||
$supplier_model = new SupplierModel();
|
||||
$supplier_info = $supplier_model->getSupplierInfo([['supplier_id', '=', $supplier_id]], 'title')['data'];
|
||||
if(!empty($supplier_info)){
|
||||
$condition[] = ['g.supplier_id', '=', $supplier_id];
|
||||
$condition_desc[] = ['name'=> '供应商', 'value' => $supplier_info['title']];
|
||||
}
|
||||
}
|
||||
if (!empty($start_sale)) $condition[] = ['g.sale_num', '>=', $start_sale];
|
||||
if (!empty($end_sale)) $condition[] = ['g.sale_num', '<=', $end_sale];
|
||||
$sale_name = '';
|
||||
if (!empty($start_sale) && empty($end_sale)) {
|
||||
$sale_name = '大于等于'.$start_sale;
|
||||
} elseif (empty($start_sale) && !empty($end_sale)) {
|
||||
$sale_name = '小于等于'.$end_sale;
|
||||
} elseif (!empty($start_sale) && !empty($end_sale)) {
|
||||
$sale_name = $start_sale . '到' . $end_sale;
|
||||
}
|
||||
if($sale_name) $condition_desc[] = [ 'name' => '商品销量', 'value' => $sale_name ];
|
||||
|
||||
if (!empty($start_price)) $condition[] = ['gs.price', '>=', $start_price];
|
||||
if (!empty($end_price)) $condition[] = ['gs.price', '<=', $end_price];
|
||||
$price_name = '';
|
||||
if (!empty($start_price) && empty($end_price)) {
|
||||
$price_name = '大于等于'.$start_price;
|
||||
} elseif (empty($start_price) && !empty($end_price)) {
|
||||
$price_name = '小于等于'.$end_price;
|
||||
} elseif (!empty($start_price) && !empty($end_price)) {
|
||||
$price_name = $start_price . '到' . $end_price;
|
||||
}
|
||||
if($price_name) $condition_desc[] = [ 'name' => '商品价格', 'value' => $price_name ];
|
||||
|
||||
// 查询库存预警的商品
|
||||
if ($stockalarm) {
|
||||
$stock_alarm = $goods_model->getGoodsStockAlarm($site_id);
|
||||
if(empty($stock_alarm[ 'data' ])) $stock_alarm[ 'data' ] = '-1';
|
||||
$condition[] = ['g.goods_id', 'in', $stock_alarm[ 'data' ]];
|
||||
}
|
||||
|
||||
$join = [];
|
||||
$join[] = ['goods g', "g.goods_id = gs.goods_id", 'left'];
|
||||
if($is_install_supply){
|
||||
$join[] = ['supplier s', 'g.supplier_id = s.supplier_id', 'left'];
|
||||
}
|
||||
|
||||
$table_field = 'g.goods_class,g.category_id,g.category_json,g.service_category,gs.sku_id, gs.sku_no, gs.sku_name, gs.goods_state, gs.goods_class_name, gs.price, gs.market_price, gs.cost_price, gs.stock, gs.stock_alarm, gs.real_stock, gs.weight, gs.volume, gs.unit, gs.sale_num, gs.click_num, gs.collect_num, gs.keywords, gs.introduction, gs.brand_name';
|
||||
if($is_install_supply){
|
||||
$table_field .= ',IF(s.title is null, "", s.title) as supplier_name';
|
||||
}
|
||||
|
||||
$export_field = [
|
||||
['field' => 'sku_no', 'name' => '商品编码'],
|
||||
['field' => 'sku_name', 'name' => '商品名称'],
|
||||
['field' => 'goods_class_name', 'name' => '商品类型'],
|
||||
['field' => 'category_names', 'name' => '商品分类'],
|
||||
['field' => 'goods_state', 'name' => '商品状态'],
|
||||
['field' => 'price', 'name' => '销售价'],
|
||||
['field' => 'market_price', 'name' => '划线价'],
|
||||
['field' => 'cost_price', 'name' => '成本价'],
|
||||
['field' => 'stock', 'name' => '库存'],
|
||||
['field' => 'stock_alarm', 'name' => '库存预警'],
|
||||
['field' => 'real_stock', 'name' => '实物库存'],
|
||||
['field' => 'weight', 'name' => '重量(单位g)'],
|
||||
['field' => 'volume', 'name' => '体积(单位立方米)'],
|
||||
['field' => 'unit', 'name' => '单位'],
|
||||
['field' => 'sale_num', 'name' => '销量'],
|
||||
['field' => 'click_num', 'name' => '点击量'],
|
||||
['field' => 'collect_num', 'name' => '收藏量'],
|
||||
['field' => 'keywords', 'name' => '关键词'],
|
||||
['field' => 'introduction', 'name' => '促销语'],
|
||||
['field' => 'brand_name', 'name' => '品牌名称'],
|
||||
];
|
||||
if($is_install_supply) $export_field[] = ['field' => 'supplier_name', 'name' => '供应商'];
|
||||
|
||||
$param = [
|
||||
'site_id' => $site_id,
|
||||
'from_type' => 'goods',
|
||||
'from_type_name' => '商品',
|
||||
'condition_desc' => $condition_desc,
|
||||
'query' => [
|
||||
'table' => 'goods_sku',
|
||||
'alias' => 'gs',
|
||||
'join' => $join,
|
||||
'condition' => $condition,
|
||||
'field' => $table_field,
|
||||
'chunk_field' => 'gs.sku_id',
|
||||
'chunk_order' => 'asc',
|
||||
],
|
||||
'export_field' => $export_field,
|
||||
'handle' => function($item_list){
|
||||
return $this->handle($item_list);
|
||||
},
|
||||
];
|
||||
$export_model = new ExportModel();
|
||||
return $export_model->export($param);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @param $item_list
|
||||
* @return mixed
|
||||
*/
|
||||
protected function handle($item_list)
|
||||
{
|
||||
//获取分类数据
|
||||
$goods_model = new Goods();
|
||||
$item_list = $goods_model->getCategoryNames($item_list);
|
||||
|
||||
//处理其他字段
|
||||
$format_field = ['goods_num','num','stock','stock_alarm','sale_num','virtual_sale','real_stock'];
|
||||
foreach ($item_list as &$item_v) {
|
||||
foreach($format_field as $field_v){
|
||||
if (isset($item_v[$field_v])) {
|
||||
$item_v[$field_v] = numberFormat($item_v[$field_v]);
|
||||
}
|
||||
}
|
||||
$item_v[ 'goods_state' ] = $item_v[ 'goods_state' ] == 1 ? '销售中' : '仓库中';
|
||||
}
|
||||
return $item_list;
|
||||
}
|
||||
}
|
||||
264
app/model/goods/GoodsImport.php
Executable file
264
app/model/goods/GoodsImport.php
Executable file
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
|
||||
use app\model\BaseModel;
|
||||
use addon\virtualcard\model\VirtualGoods as VirtualCardGoods;
|
||||
use PhpOffice\PhpSpreadsheet\Exception;
|
||||
|
||||
/**
|
||||
* 商品导入
|
||||
*/
|
||||
class GoodsImport extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 读取excel中的数据
|
||||
* @param $excel_path
|
||||
* @return array
|
||||
* @throws \PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function readGoodsExcel($excel_path)
|
||||
{
|
||||
if (!file_exists($excel_path)) return $this->error('', '商品导入Excel文件路径错误');
|
||||
|
||||
$excel_reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||
|
||||
try {
|
||||
$excel_reader->setReadDataOnly(true); // 只读取数据,会忽略所有空白行
|
||||
$php_excel = $excel_reader->load($excel_path);
|
||||
$goods_data = $this->getSheetData($php_excel, 0);
|
||||
$sku_data = $this->getSheetData($php_excel, 1);
|
||||
|
||||
$data = [
|
||||
'original_goods_data' => $goods_data,
|
||||
'original_sku_data' => $sku_data
|
||||
];
|
||||
|
||||
$goods_field = $goods_data[ 1 ];
|
||||
$sku_field = $sku_data[ 1 ];
|
||||
|
||||
unset($goods_data[ 1 ], $goods_data[ 2 ], $sku_data[ 1 ], $sku_data[ 2 ]); // 移除表头
|
||||
|
||||
$data[ 'list' ] = $goods_data;
|
||||
|
||||
$sku_temp = [];
|
||||
foreach ($sku_data as $k => $sku_item) {
|
||||
$sku_item = array_combine($sku_field, $sku_item);
|
||||
$sku_data[ $k ] = $sku_item;
|
||||
if (isset($sku_temp[ 'goods_' . $sku_item[ 'goods_number' ] ])) {
|
||||
array_push($sku_temp[ 'goods_' . $sku_item[ 'goods_number' ] ], $sku_item);
|
||||
} else {
|
||||
$sku_temp[ 'goods_' . $sku_item[ 'goods_number' ] ] = [ $sku_item ];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data[ 'list' ] as $k => $item) {
|
||||
$item = array_combine($goods_field, $item);
|
||||
$data[ 'list' ][ $k ] = $item;
|
||||
if (isset($item[ 'is_many_sku' ]) && $item[ 'is_many_sku' ] && isset($sku_temp[ 'goods_' . $item[ 'goods_number' ] ])) {
|
||||
$data[ 'list' ][ $k ][ 'sku' ] = $sku_temp[ 'goods_' . $item[ 'goods_number' ] ];
|
||||
}
|
||||
}
|
||||
return $this->success($data);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图表中的数据读取出来
|
||||
* @param $php_excel
|
||||
* @param int $sheet
|
||||
* @return array
|
||||
*/
|
||||
public function getSheetData($php_excel, $sheet = 0)
|
||||
{
|
||||
$all_column = $php_excel->getSheet($sheet)->getHighestColumn(); //取得最大的列号
|
||||
$all_row = $php_excel->getSheet($sheet)->getHighestRow(); //取得一共有多少行
|
||||
|
||||
$data = [];
|
||||
for ($row = 1; $row <= $all_row; $row++) {
|
||||
//从A列读取数据
|
||||
for ($col = 'A'; $col <= $all_column; $col++) {
|
||||
// 读取单元格
|
||||
$data[ $row ][] = trim((string) $php_excel->getSheet($sheet)->getCell("$col$row")->getValue());
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入商品
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param $goods_class
|
||||
* @return array
|
||||
*/
|
||||
public function importGoods($data, $site_id, $goods_class)
|
||||
{
|
||||
switch ( $goods_class ) {
|
||||
case 1:
|
||||
$name = '实物商品导入';
|
||||
$goods_class = new Goods();
|
||||
break;
|
||||
case 2:
|
||||
$name = '虚拟商品导入';
|
||||
$goods_class = new VirtualGoods();
|
||||
break;
|
||||
case 3:
|
||||
$name = '卡密商品导入';
|
||||
$goods_class = new VirtualCardGoods();
|
||||
break;
|
||||
}
|
||||
|
||||
$import_data = [
|
||||
'site_id' => $site_id,
|
||||
'import_time' => time(),
|
||||
'success_num' => 0,
|
||||
'record_name' => $name,
|
||||
'fail_num' => 0,
|
||||
'fail_data' => [],
|
||||
];
|
||||
// 执行导入
|
||||
foreach ($data[ 'list' ] as $key => $item) {
|
||||
$res = $goods_class->importGoods($item, $site_id);
|
||||
if ($res[ 'code' ] == 0) {
|
||||
$import_data[ 'success_num' ] += 1;
|
||||
} else {
|
||||
array_push($import_data[ 'fail_data' ], [
|
||||
'index' => $key,
|
||||
'reason' => $res[ 'message' ]
|
||||
]);
|
||||
$import_data[ 'fail_num' ] += 1;
|
||||
}
|
||||
}
|
||||
unset($data[ 'list' ]);
|
||||
|
||||
// 添加导入记录
|
||||
$import_data[ 'data' ] = json_encode($data);
|
||||
$import_data[ 'fail_data' ] = empty($import_data[ 'fail_data' ]) ? '' : json_encode($import_data[ 'fail_data' ], JSON_UNESCAPED_UNICODE);
|
||||
model('goods_import_record')->add($import_data);
|
||||
|
||||
return $this->success([ 'error_count' => $import_data[ 'fail_num' ], 'success_count' => $import_data[ 'success_num' ] ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导入记录
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getImportPageList($condition = [], $page = 1, $list_rows = PAGE_LIST_ROWS, $field = true, $order = 'import_time desc')
|
||||
{
|
||||
$data = model('goods_import_record')->pageList($condition, $field, $order, $page, $list_rows);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载失败数据
|
||||
* @param $id
|
||||
* @param $site_id
|
||||
* @return array|void
|
||||
* @throws Exception
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
||||
*/
|
||||
public function downloadFailData($id, $site_id)
|
||||
{
|
||||
$info = model('goods_import_record')->getInfo([ [ 'id', '=', $id ], [ 'site_id', '=', $site_id ], [ 'fail_num', '>', 0 ] ], 'data,fail_data');
|
||||
if (empty($info)) return $this->error('', '未获取到导出记录');
|
||||
|
||||
$original_data = json_decode($info[ 'data' ], true);
|
||||
$fail_data = json_decode($info[ 'fail_data' ], true);
|
||||
|
||||
$field = $original_data[ 'original_goods_data' ][ 1 ];
|
||||
$header = $original_data[ 'original_goods_data' ][ 2 ];
|
||||
array_push($field, 'reason');
|
||||
array_push($header, '失败原因(再次上传前请先删除该列)');
|
||||
|
||||
// 需要导出的商品数据
|
||||
$export_goods = [
|
||||
$field,
|
||||
$header
|
||||
];
|
||||
// 需要导出的规格数据
|
||||
$export_sku = [
|
||||
$original_data[ 'original_sku_data' ][ 1 ],
|
||||
$original_data[ 'original_sku_data' ][ 2 ]
|
||||
];
|
||||
|
||||
// 处理sku数据
|
||||
unset($original_data[ 'original_sku_data' ][ 1 ], $original_data[ 'original_sku_data' ][ 2 ]);
|
||||
$sku_data = $original_data[ 'original_sku_data' ];
|
||||
$sku_temp = [];
|
||||
foreach ($sku_data as $k => $sku_item) {
|
||||
$sku = array_combine($export_sku[ 0 ], $sku_item);
|
||||
if (isset($sku_temp[ 'goods_' . $sku[ 'goods_number' ] ])) {
|
||||
array_push($sku_temp[ 'goods_' . $sku[ 'goods_number' ] ], $sku_item);
|
||||
} else {
|
||||
$sku_temp[ 'goods_' . $sku[ 'goods_number' ] ] = [ $sku_item ];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($fail_data as $item) {
|
||||
$goods = $original_data[ 'original_goods_data' ][ $item[ 'index' ] ];
|
||||
array_push($goods, $item[ 'reason' ]);
|
||||
array_push($export_goods, $goods);
|
||||
|
||||
$goods = array_combine($export_goods[ 0 ], $goods);
|
||||
if ($goods[ 'is_many_sku' ] && isset($sku_temp[ 'goods_' . $goods[ 'goods_number' ] ])) {
|
||||
$export_sku = array_merge($export_sku, $sku_temp[ 'goods_' . $goods[ 'goods_number' ] ]);
|
||||
}
|
||||
}
|
||||
|
||||
$letter = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
|
||||
|
||||
$file_name = '商品导入失败数据-' . date('YmdHis');
|
||||
$php_excel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||||
$php_excel->getProperties()->setTitle($file_name); //设置标题
|
||||
// 设置当前sheet
|
||||
$php_excel->setActiveSheetIndex(0);
|
||||
// 设置当前sheet的名称
|
||||
$php_excel->getActiveSheet()->setTitle('商品');
|
||||
|
||||
for ($i = 0; $i < count($export_goods); $i++) {
|
||||
$item = $export_goods[ $i ];
|
||||
for ($j = 0; $j < count($item); $j++) {
|
||||
$php_excel->getActiveSheet()->setCellValue($letter[ $j ] . ( $i + 1 ), $item[ $j ]);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置当前sheet
|
||||
$php_excel->createSheet(1);
|
||||
$php_excel->setActiveSheetIndex(1);
|
||||
// 设置当前sheet的名称
|
||||
$php_excel->getActiveSheet()->setTitle('规格');
|
||||
for ($i = 0; $i < count($export_sku); $i++) {
|
||||
$item = $export_sku[ $i ];
|
||||
for ($j = 0; $j < count($item); $j++) {
|
||||
$php_excel->getActiveSheet()->setCellValue($letter[ $j ] . ( $i + 1 ), $item[ $j ]);
|
||||
}
|
||||
}
|
||||
|
||||
ob_clean();
|
||||
header('Content-Type: application/vnd.ms-excel');
|
||||
header('Content-Disposition: attachment;filename="' . $file_name . '.xlsx');
|
||||
header('Cache-Control: max-age=1');
|
||||
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($php_excel, 'Xlsx');
|
||||
$objWriter->save('php://output');
|
||||
}
|
||||
}
|
||||
165
app/model/goods/GoodsLabel.php
Executable file
165
app/model/goods/GoodsLabel.php
Executable file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 商品分组
|
||||
*/
|
||||
class GoodsLabel extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加商品分组
|
||||
* @param array $data
|
||||
*/
|
||||
public function addLabel($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$data[ 'create_time' ] = time();
|
||||
$label_id = model('goods_label')->add($data);
|
||||
Cache::tag("goods_label_" . $site_id)->clear();
|
||||
return $this->success($label_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改商品分组
|
||||
* @param array $data
|
||||
*/
|
||||
public function editLabel($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$label_info = model('goods_label')->getInfo([ [ 'id', '=', $data[ 'id' ] ] ], 'label_name');
|
||||
if (empty($label_info)) {
|
||||
return $this->error(null, '编辑条件有误');
|
||||
}
|
||||
//分组名称修改同步修改商品表字段
|
||||
if ($label_info[ 'label_name' ] != $data[ 'label_name' ]) {
|
||||
model('goods')->update([ 'label_name' => $data[ 'label_name' ] ], [ [ 'label_id', '=', $data[ 'id' ] ] ]);
|
||||
}
|
||||
|
||||
$data[ 'update_time' ] = time();
|
||||
$res = model('goods_label')->update($data, [ [ 'id', '=', $data[ 'id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
|
||||
Cache::tag("goods_label_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品分组
|
||||
* @param array $condition
|
||||
*/
|
||||
public function deleteLabel($condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
//清空商品与标签的关联
|
||||
$list = model('goods_label')->getList($condition);
|
||||
if(!empty($list)){
|
||||
$label_ids = array_column($list, 'id');
|
||||
model('goods')->update([ 'label_name' => '', 'label_id' => 0 ], [ [ 'label_id', 'in', $label_ids ] ]);
|
||||
}
|
||||
|
||||
$res = model('goods_label')->delete($condition);
|
||||
Cache::tag("goods_label_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改排序
|
||||
* @param $sort
|
||||
* @param $id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function modifySort($sort, $id, $site_id)
|
||||
{
|
||||
$site_id = $site_id ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model('goods_label')->update([ 'sort' => $sort ], [ [ 'id', '=', $id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag("goods_label_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分组信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getLabelInfo($condition = [], $field = '*')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$list = model('goods_label')->getInfo($condition, $field);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分组列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getLabelList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$list = model('goods_label')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分组分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getLabelPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'sort asc', $field = '*')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$list = model('goods_label')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
68
app/model/goods/GoodsLocalRestrictions.php
Executable file
68
app/model/goods/GoodsLocalRestrictions.php
Executable file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\stock\StockData;
|
||||
use app\model\store\Store;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 商品同城起送配置 门店业务
|
||||
*/
|
||||
class GoodsLocalRestrictions extends BaseModel
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 设置不同门店 sku 同城配送模式 非起送 商品业务
|
||||
* @param $goods_sku_array
|
||||
* @param $site_id
|
||||
* @param $store_id restrictions
|
||||
* @param $uid
|
||||
* @return array
|
||||
*/
|
||||
public function setRestrictions($goods_sku_array, $site_id, $store_id)
|
||||
{
|
||||
$store_model = new Store();
|
||||
$store_info = $store_model->getStoreInfo([['store_id', '=', $store_id]])['data'] ?? [];
|
||||
if (empty($store_info)) {
|
||||
$default_store_info = $store_model->getDefaultStore($site_id)['data'] ?? [];
|
||||
$store_id = $default_store_info['store_id'];
|
||||
} else {
|
||||
$store_id = $store_id;
|
||||
}
|
||||
model('goods')->startTrans();
|
||||
try {
|
||||
//校验门店商品,不存在就创建
|
||||
$result = (new StockData())->getStoreSkuAndCreateIfNotExists(['goods_sku_list' => $goods_sku_array, 'store_id' => $store_id]);
|
||||
foreach ($goods_sku_array as $k => $v) {
|
||||
$item_sku_id = $v['sku_id'];
|
||||
//是否限制送货 1是限制 0 不限制
|
||||
if (isset($v['is_delivery_restrictions'])) {
|
||||
$save_data['is_delivery_restrictions'] = $v['is_delivery_restrictions'] ?? 1;
|
||||
}
|
||||
if (!empty($save_data)) {
|
||||
model('store_goods_sku')->update($save_data, [['sku_id', '=', $item_sku_id], ['store_id', '=', $store_id]]);
|
||||
}
|
||||
}
|
||||
model('goods')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('goods')->rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
87
app/model/goods/GoodsPoster.php
Executable file
87
app/model/goods/GoodsPoster.php
Executable file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 商品海报
|
||||
*/
|
||||
class GoodsPoster extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加商品海报
|
||||
* @param array $data
|
||||
*/
|
||||
public function addPoster($data)
|
||||
{
|
||||
if (!empty($data[ 'poster_id' ])) {
|
||||
$poster_id = model('goods_poster')->update($data, [ [ 'poster_id', '=', $data[ 'poster_id' ], [ 'site_id', '=', $data[ 'site_id' ] ] ] ]);
|
||||
} else {
|
||||
$poster_id = model('goods_poster')->add($data);
|
||||
}
|
||||
return $this->success($poster_id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除海报
|
||||
* @param $poster_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function deletePoster($poster_id, $site_id)
|
||||
{
|
||||
$res = model('goods_poster')->delete([ [ 'poster_id', '=', $poster_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取海报信息
|
||||
* @param $poster_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getPosterInfo($poster_id, $site_id)
|
||||
{
|
||||
$res = model('goods_poster')->getInfo([ [ 'poster_id', '=', $poster_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取海报分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getPosterPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'poster_id asc', $field = '*', $alias = 'a', $join = [])
|
||||
{
|
||||
$list = model('goods_poster')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取社群二维码列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getPosterList($condition = [], $field = '*', $order = 'poster_id asc', $limit = null)
|
||||
{
|
||||
$list = model('goods_poster')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
}
|
||||
112
app/model/goods/GoodsService.php
Executable file
112
app/model/goods/GoodsService.php
Executable file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 商品服务
|
||||
*/
|
||||
class GoodsService extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加商品服务
|
||||
* @param array $data
|
||||
*/
|
||||
public function addService($data)
|
||||
{
|
||||
|
||||
$data['create_time'] = time();
|
||||
$service_id = model('goods_service')->add($data);
|
||||
return $this->success($service_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多个商品服务
|
||||
* @param array $data
|
||||
*/
|
||||
public function addServiceList($data)
|
||||
{
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
$data[$k]['create_time'] = time();
|
||||
}
|
||||
$service_id = model('goods_service')->addList($data);
|
||||
return $this->success($service_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改商品服务
|
||||
* @param array $data
|
||||
* @return multitype:string
|
||||
*/
|
||||
public function editService($data, $condition)
|
||||
{
|
||||
$res = model('goods_service')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品服务
|
||||
* @param array $condition
|
||||
*/
|
||||
public function deleteService($condition)
|
||||
{
|
||||
|
||||
$res = model('goods_service')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品服务
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getServiceInfo($condition, $field = '*')
|
||||
{
|
||||
$info = model('goods_service')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品服务列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getServiceList($condition = [], $field = 'id,site_id,service_name,desc,create_time,icon', $order = 'create_time desc', $limit = null)
|
||||
{
|
||||
|
||||
$list = model('goods_service')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品服务分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getServicePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = 'id,site_id,service_name,desc,create_time,icon')
|
||||
{
|
||||
|
||||
$list = model('goods_service')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
88
app/model/goods/GoodsStock.php
Executable file
88
app/model/goods/GoodsStock.php
Executable file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 商品库存
|
||||
*/
|
||||
class GoodsStock extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 增加库存
|
||||
* @param $param
|
||||
* @return array
|
||||
* @throws DbException
|
||||
*/
|
||||
public function incStock($param)
|
||||
{
|
||||
$condition = array (
|
||||
[ "sku_id", "=", $param[ "sku_id" ] ]
|
||||
);
|
||||
$num = $param[ "num" ];
|
||||
$sku_info = model('goods_sku')->getInfo($condition, "goods_id,stock");
|
||||
if (empty($sku_info))
|
||||
return $this->error(-1, "");
|
||||
|
||||
//编辑sku库存
|
||||
$result = model('goods_sku')->setInc($condition, "stock", $num);
|
||||
|
||||
//编辑商品总库存(暂不考虑查询判断)
|
||||
$goods_condition = array (
|
||||
[ "goods_id", "=", $sku_info[ "goods_id" ] ]
|
||||
);
|
||||
$res = model('goods')->setInc($goods_condition, "goods_stock", $num);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少库存
|
||||
* @param $param
|
||||
* @return array
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function decStock($param)
|
||||
{
|
||||
|
||||
$num = $param[ "num" ];
|
||||
$condition = array (
|
||||
[ "sku_id", "=", $param[ "sku_id" ] ]
|
||||
);
|
||||
$sku_info = Db::name("goods_sku")->where($condition)->field("goods_id,stock,sku_name")->lock(true)->find();
|
||||
if (empty($sku_info)) {
|
||||
return $this->error();
|
||||
}
|
||||
if ($sku_info[ "stock" ] < $num) {
|
||||
return $this->error('', $sku_info[ "sku_name" ] . "库存不足!");
|
||||
}
|
||||
//编辑sku库存
|
||||
$result = model('goods_sku')->setDec($condition, "stock", $num);
|
||||
if ($result === false) {
|
||||
return $this->error();
|
||||
}
|
||||
|
||||
//编辑商品总库存(暂不考虑查询判断)
|
||||
$goods_condition = array (
|
||||
[ "goods_id", "=", $sku_info[ "goods_id" ] ]
|
||||
);
|
||||
$res = model('goods')->setDec($goods_condition, "goods_stock", $num);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
}
|
||||
38
app/model/goods/GoodsVerify.php
Executable file
38
app/model/goods/GoodsVerify.php
Executable file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 商品核销
|
||||
*/
|
||||
class GoodsVerify extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 添加待核销商品
|
||||
* @param unknown $data
|
||||
*/
|
||||
public function addGoodsVerify($data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 核销
|
||||
* @param unknown $code
|
||||
* @param unknown $verifier_id
|
||||
*/
|
||||
public function verify($code, $verifier_id)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
492
app/model/goods/ServiceCategory.php
Executable file
492
app/model/goods/ServiceCategory.php
Executable file
@@ -0,0 +1,492 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 项目分类
|
||||
*/
|
||||
class ServiceCategory extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加项目分类
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function addCategory($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
$category_id = model('service_category')->add($data);
|
||||
$info = model('service_category')->getInfo([ [ 'category_id', '=', $category_id ] ]);
|
||||
$common_data = array (
|
||||
'category_id_1' => 0,
|
||||
'category_id_2' => 0,
|
||||
'category_id_3' => 0,
|
||||
);
|
||||
switch ( $data[ 'level' ] ) {
|
||||
case 1:
|
||||
$common_data[ 'category_id_1' ] = $category_id;
|
||||
break;
|
||||
case 2:
|
||||
$common_data[ 'category_id_1' ] = $data[ 'pid' ];//这让我并没有验证合法性,业务发生变动之后要注意(author:周)
|
||||
$common_data[ 'category_id_2' ] = $category_id;
|
||||
break;
|
||||
case 3:
|
||||
//错误数据纠正
|
||||
$parent_category_info = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'pid' ] ], [ 'site_id', '=', $site_id ] ], 'pid');//这让我并没有验证合法性,业务发生变动之后要注意(author:周)
|
||||
$common_data[ 'category_id_1' ] = $parent_category_info[ 'pid' ];//这让我并没有验证合法性,业务发生变动之后要注意(周)
|
||||
$common_data[ 'category_id_2' ] = $data[ 'pid' ];
|
||||
$common_data[ 'category_id_3' ] = $category_id;
|
||||
break;
|
||||
}
|
||||
$data = array_merge($data, $common_data);
|
||||
|
||||
model('service_category')->update([ 'category_id_' . $data[ 'level' ] => $category_id ], [ [ 'category_id', '=', $category_id ] ]);
|
||||
Cache::tag("service_category_" . $site_id)->clear();
|
||||
return $this->success($category_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证分类是否可以修改
|
||||
*/
|
||||
public function checkEditCategory($data)
|
||||
{
|
||||
$category_id = $data[ 'category_id' ];
|
||||
$pid = $data[ 'pid' ] ?? 0;
|
||||
$parent_category_info = model('service_category')->getInfo([ [ 'category_id', '=', $pid ], [ 'site_id', '=', $data[ 'site_id' ] ] ]) ?? [];
|
||||
if (empty($parent_category_info)) return $this->success();
|
||||
$category_info = model('service_category')->getInfo([ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $data[ 'site_id' ] ] ]) ?? [];
|
||||
if ($parent_category_info[ 'category_id' ] == $category_info[ 'category_id' ]) {
|
||||
return $this->error('', '不能修改上级为自己');
|
||||
}
|
||||
if ($category_info[ 'level' ] < 3 && $parent_category_info[ 'level' ] == 2) {
|
||||
$child_list = model('service_category')->getCount([ [ 'pid', '=', $category_info[ 'category_id' ] ] ], 'category_id');
|
||||
if ($child_list > 0) return $this->error('', '当前等级存在下级,不可修改为该上级');
|
||||
}
|
||||
if ($category_info[ 'level' ] == 1 && $parent_category_info[ 'level' ] == 1) {
|
||||
$child_list = model('service_category')->getColumn([ [ 'pid', '=', $category_info[ 'category_id' ] ] ], 'category_id');
|
||||
|
||||
if ($child_list) {
|
||||
$child_child_list = model('service_category')->getCount([ [ 'pid', 'in', $child_list ] ], 'category_id');
|
||||
if ($child_child_list > 0) return $this->error('', '当前等级存在下下级,不可修改为该上级');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改项目分类
|
||||
* @param $data
|
||||
* @return \multitype
|
||||
*/
|
||||
public function editCategory($data)
|
||||
{
|
||||
$site_id = $data['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
$check_res = $this->checkEditCategory($data);
|
||||
if ($check_res[ 'code' ] < 0) return $check_res;
|
||||
model('service_category')->startTrans();
|
||||
try {
|
||||
//仅限当分类不支持跨级别修改
|
||||
$pid = $data[ 'pid' ];
|
||||
$level = 1;
|
||||
$parent_category_info = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'pid' ] ], [ 'site_id', '=', $site_id ] ]);
|
||||
if ($parent_category_info) $level = (int) $parent_category_info[ 'level' ] + 1;
|
||||
$data[ 'level' ] = $level;
|
||||
|
||||
//获取该分类信息
|
||||
$info = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id' ] ], [ 'site_id', '=', $site_id ] ]);
|
||||
$common_data = array (
|
||||
'category_id_1' => 0,
|
||||
'category_id_2' => 0,
|
||||
'category_id_3' => 0,
|
||||
);
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
$common_data[ 'category_id_1' ] = $info[ 'category_id' ];
|
||||
break;
|
||||
case 2:
|
||||
$common_data[ 'category_id_1' ] = $data[ 'pid' ];
|
||||
$common_data[ 'category_id_2' ] = $info[ 'category_id' ];
|
||||
//二三级要同步改动
|
||||
model('service_category')->update($common_data, [ [ 'pid', '=', $info[ 'category_id' ] ] ]);
|
||||
|
||||
model('service_category')->update([ 'category_id_3' => Db::raw('category_id') ], [ [ 'pid', '=', $info[ 'category_id' ] ], [ 'level', '=', '3' ] ]);
|
||||
break;
|
||||
case 3:
|
||||
//错误数据纠正
|
||||
$parent_category_info = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'pid' ] ], [ 'site_id', '=', $site_id ] ], 'pid');//这让我并没有验证合法性,业务发生变动之后要注意(author:周)
|
||||
$common_data[ 'category_id_1' ] = $parent_category_info[ 'pid' ];//这让我并没有验证合法性,业务发生变动之后要注意(周)
|
||||
$common_data[ 'category_id_2' ] = $data[ 'pid' ];
|
||||
$common_data[ 'category_id_3' ] = $info[ 'category_id' ];
|
||||
break;
|
||||
}
|
||||
$info = array_merge($info, $common_data);
|
||||
$data = array_merge($data, $common_data);
|
||||
|
||||
if ($data[ 'is_show' ] == -1) {
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
model('service_category')->update([ 'is_show' => -1 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
$data[ 'category_full_name' ] = $data[ 'category_name' ];
|
||||
break;
|
||||
case 2:
|
||||
model('service_category')->update([ 'is_show' => -1 ], [ [ 'category_id_2', '=', $info[ 'category_id_2' ] ] ]);
|
||||
|
||||
$info_1 = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
case 3:
|
||||
model('service_category')->update([ 'is_show' => -1 ], [ [ 'category_id', 'in', [ $info[ 'category_id_1' ], $info[ 'category_id_2' ] ] ] ]);
|
||||
|
||||
$info_1 = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$info_2 = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_2' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $info_2[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
model('service_category')->update([ 'is_show' => 0 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
|
||||
$data[ 'category_full_name' ] = $data[ 'category_name' ];
|
||||
break;
|
||||
case 2:
|
||||
model('service_category')->update([ 'is_show' => 0 ], [ [ 'category_id', '=', $info[ 'category_id_1' ] ] ]);
|
||||
|
||||
$info_1 = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
case 3:
|
||||
model('service_category')->update([ 'is_show' => 0 ], [ [ 'category_id', 'in', [ $info[ 'category_id_1' ], $info[ 'category_id_2' ] ] ] ]);
|
||||
|
||||
$info_1 = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_1' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$info_2 = model('service_category')->getInfo([ [ 'category_id', '=', $data[ 'category_id_2' ] ], [ 'site_id', '=', $site_id ] ], 'category_id_1,category_id_2,category_id_3,level,category_name');
|
||||
$category_full_name = $info_1[ 'category_name' ] . '/' . $info_2[ 'category_name' ] . '/' . $data[ 'category_name' ];
|
||||
$data[ 'category_full_name' ] = $category_full_name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($info[ 'pid' ] != $data[ 'pid' ]) {
|
||||
if ($level == 2) {
|
||||
model('goods')->update([
|
||||
'category_json' => Db::raw("REPLACE(category_json, '[\"{$info['pid']},{$data[ 'category_id' ]},', '[\"{$data['pid']},{$data[ 'category_id' ]},')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
model('goods')->update([
|
||||
'category_json' => Db::raw("REPLACE(category_json, '[\"{$info['pid']},{$data[ 'category_id' ]}\"]', '[\"{$data['pid']},{$data[ 'category_id' ]}\"]')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
model('goods')->update([
|
||||
'category_id' => Db::raw("REPLACE(category_id, ',{$info['pid']},{$data[ 'category_id' ]},', ',{$data['pid']},{$data[ 'category_id' ]},')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
} else {
|
||||
model('goods')->update([
|
||||
'category_json' => Db::raw("REPLACE(category_json, '[\"{$info['category_id_1']},{$info['category_id_2']},{$info['category_id_3']}\"]', '[\"{$data['category_id_1']},{$data['category_id_2']},{$data['category_id_3']}\"]')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
model('goods')->update([
|
||||
'category_id' => Db::raw("REPLACE(category_id, ',{$info['category_id_1']},{$info[ 'category_id_2' ]},{$info[ 'category_id_3' ]},', ',{$data['category_id_1']},{$data[ 'category_id_2' ]},{$data[ 'category_id_3' ]},')")
|
||||
], [ [ 'category_id', 'like', "%,{$info['pid']},{$data[ 'category_id' ]},%" ], [ 'site_id', '=', $site_id ] ]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$res = model('service_category')->update($data, [ [ 'category_id', '=', $data[ 'category_id' ] ], [ 'site_id', '=', $site_id ] ]);
|
||||
|
||||
//变更下级等级层级
|
||||
$child_list = model('service_category')->getColumn([ [ 'site_id', '=', $site_id ], [ 'pid', '=', $data[ 'category_id' ] ] ], 'category_id');
|
||||
if ($child_list) {
|
||||
model('service_category')->update([ 'level' => (int) $data[ 'level' ] + 1 ], [ [ 'category_id', 'in', $child_list ] ]);
|
||||
$child_child_list = model('service_category')->getColumn([ [ 'site_id', '=', $site_id ], [ 'pid', 'in', $child_list ] ], 'category_id');
|
||||
model('service_category')->update([ 'level' => (int) $data[ 'level' ] + 2 ], [ [ 'category_id', 'in', $child_child_list ] ]);
|
||||
}
|
||||
|
||||
Cache::tag("service_category_" . $site_id)->clear();
|
||||
|
||||
model('service_category')->commit();
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
model('service_category')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除分类
|
||||
* @param $category_id
|
||||
* @param $site_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function deleteCategory($category_id, $site_id)
|
||||
{
|
||||
$site_id = $site_id ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$service_category_info = $this->getCategoryInfo([
|
||||
[ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ]
|
||||
], "level")[ 'data' ];
|
||||
$field = "category_id_" . $service_category_info[ 'level' ];
|
||||
|
||||
$res = model('service_category')->delete([ [ $field, '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
model('service_category')->delete([ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag("service_category_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目分类信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getCategoryInfo($condition, $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2,category_id_3,category_full_name,image_adv,link_url')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model('service_category')->getInfo($check_condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目分类列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryList($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2,category_id_3,image_adv', $order = 'sort asc', $limit = null)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$list = model('service_category')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目分类树结构
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryTree($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2,category_id_3', $order = 'sort asc,category_id desc', $limit = null)
|
||||
{
|
||||
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$data = json_encode([ $condition, $field, $order, $limit ]);
|
||||
$cache = Cache::get("service_category_getCategoryTree_" . $site_id . "_" . $data);
|
||||
if (!empty($cache)) {
|
||||
return $this->success($cache);
|
||||
}
|
||||
$list = model('service_category')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
|
||||
$service_category_list = [];
|
||||
|
||||
//遍历一级项目分类
|
||||
foreach ($list as $k => $v) {
|
||||
if ($v[ 'level' ] == 1) {
|
||||
$service_category_list[] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
|
||||
$list = array_values($list);
|
||||
|
||||
//遍历二级项目分类
|
||||
foreach ($list as $k => $v) {
|
||||
foreach ($service_category_list as $ck => $cv) {
|
||||
if ($v[ 'level' ] == 2 && $cv[ 'category_id' ] == $v[ 'pid' ]) {
|
||||
$service_category_list[ $ck ][ 'child_list' ][] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$list = array_values($list);
|
||||
|
||||
//遍历三级项目分类
|
||||
foreach ($list as $k => $v) {
|
||||
foreach ($service_category_list as $ck => $cv) {
|
||||
|
||||
if (!empty($cv[ 'child_list' ])) {
|
||||
foreach ($cv[ 'child_list' ] as $third_k => $third_v) {
|
||||
|
||||
if ($v[ 'level' ] == 3 && $third_v[ 'category_id' ] == $v[ 'pid' ]) {
|
||||
$service_category_list[ $ck ][ 'child_list' ][ $third_k ][ 'child_list' ][] = $v;
|
||||
unset($list[ $k ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Cache::tag("service_category_" . $site_id)->set("service_category_getCategoryTree_" . $site_id . "_" . $data, $service_category_list);
|
||||
|
||||
return $this->success($service_category_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目分类分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2,category_id_3,category_full_name,commission_rate')
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$list = model('service_category')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目分类列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getCategoryByParent($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2,category_id_3', $order = 'sort asc,category_id desc', $limit = null)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition['site_id'] ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$list = model('service_category')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
foreach ($list as $k => $v) {
|
||||
$child_count = model('service_category')->getCount([ 'pid' => $v[ 'category_id' ] ]);
|
||||
$list[ $k ][ 'child_count' ] = $child_count;
|
||||
}
|
||||
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改排序
|
||||
* @param int $sort
|
||||
* @param int $category_id
|
||||
*/
|
||||
public function modifyGoodsCategorySort($sort, $category_id, $site_id)
|
||||
{
|
||||
$site_id = $site_id ?? '';
|
||||
if ($site_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$res = model('service_category')->update([ 'sort' => $sort ], [ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
Cache::tag("service_category_" . $site_id)->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改显示状态
|
||||
* @param $category_id
|
||||
* @param $is_show
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function modifyCategoryShow($category_id,$is_show){
|
||||
$res = model('service_category')->getInfo([['category_id','=',$category_id]], '*');
|
||||
if(empty($res)){
|
||||
return $this->error([],'数据为空');
|
||||
}
|
||||
|
||||
Cache::tag("service_category_" . $res['site_id'])->clear();
|
||||
if($is_show == 0){
|
||||
return $this->modifyCategoryShowOn($res);
|
||||
}else{
|
||||
return $this->modifyCategoryShowOff($res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function modifyCategoryShowOff($info){
|
||||
$level = $info['level'];
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
$result = model('service_category')->update([ 'is_show' => -1 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
break;
|
||||
case 2:
|
||||
$result = model('service_category')->update([ 'is_show' => -1 ], [ [ 'category_id_2', '=', $info[ 'category_id_2' ] ] ]);
|
||||
break;
|
||||
case 3:
|
||||
default:
|
||||
$result = false;
|
||||
}
|
||||
$result = model('service_category')->update([ 'is_show' => -1 ], [ [ 'category_id', '=', $info[ 'category_id' ] ] ]);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
|
||||
public function modifyCategoryShowOn($info){
|
||||
|
||||
$level = $info['level'];
|
||||
switch ( $level ) {
|
||||
case 1:
|
||||
$result = model('service_category')->update([ 'is_show' => 0 ], [ [ 'category_id_1', '=', $info[ 'category_id_1' ] ] ]);
|
||||
break;
|
||||
case 2:
|
||||
$result = model('service_category')->update([ 'is_show' => 0 ], [ [ 'category_id', '=', $info[ 'category_id_1' ] ] ]);
|
||||
break;
|
||||
case 3:
|
||||
default:
|
||||
$result = false;
|
||||
}
|
||||
$result = model('service_category')->update([ 'is_show' => 0 ], [ [ 'category_id', '=', $info[ 'category_id' ] ] ]);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
}
|
||||
926
app/model/goods/VirtualGoods.php
Executable file
926
app/model/goods/VirtualGoods.php
Executable file
@@ -0,0 +1,926 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\goods;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\storegoods\StoreGoods;
|
||||
use app\model\system\Cron;
|
||||
use app\model\system\Stat;
|
||||
use addon\discount\model\Discount;
|
||||
use app\model\message\Message;
|
||||
|
||||
/**
|
||||
* 虚拟商品
|
||||
*/
|
||||
class VirtualGoods extends GoodsCommon
|
||||
{
|
||||
private $goods_class = array ( 'id' => 2, 'name' => '虚拟商品' );
|
||||
|
||||
private $goods_state = array (
|
||||
1 => '销售中',
|
||||
0 => '仓库中'
|
||||
);
|
||||
|
||||
public function getGoodsState()
|
||||
{
|
||||
return $this->goods_state;
|
||||
}
|
||||
|
||||
public function getGoodsClass()
|
||||
{
|
||||
return $this->goods_class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品添加
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addGoods($data)
|
||||
{
|
||||
model('goods')->startTrans();
|
||||
|
||||
try {
|
||||
|
||||
if (!empty($data[ 'goods_attr_format' ])) {
|
||||
|
||||
$goods_attr_format = json_decode($data[ 'goods_attr_format' ], true);
|
||||
$keys = array_column($goods_attr_format, 'sort');
|
||||
if (!empty($keys)) {
|
||||
array_multisort($keys, SORT_ASC, SORT_NUMERIC, $goods_attr_format);
|
||||
$data[ 'goods_attr_format' ] = json_encode($goods_attr_format);
|
||||
}
|
||||
}
|
||||
|
||||
$goods_image = $data[ 'goods_image' ];
|
||||
$first_image = explode(",", $goods_image)[ 0 ];
|
||||
|
||||
//SKU商品数据
|
||||
if (!empty($data[ 'goods_sku_data' ])) {
|
||||
$data[ 'goods_sku_data' ] = json_decode($data[ 'goods_sku_data' ], true);
|
||||
// if (empty($goods_image)) {
|
||||
// $goods_image = $data[ 'goods_sku_data' ][ 0 ][ 'sku_image' ];
|
||||
// }
|
||||
}
|
||||
|
||||
//商品编码检测
|
||||
$sku_no_check = $this->checkSkuNoRepeat(['sku_list' => $data['goods_sku_data'], 'site_id' => $data['site_id'], 'goods_id' => $data['goods_id'] ?? 0]);
|
||||
if($sku_no_check['code'] < 0){
|
||||
model('goods')->rollback();
|
||||
return $sku_no_check;
|
||||
}
|
||||
|
||||
if ($data[ 'verify_validity_type' ] == 1) {
|
||||
if (empty($data[ 'virtual_indate' ])) {
|
||||
return $this->error('', '有效期不能为空');
|
||||
}
|
||||
} else if ($data[ 'verify_validity_type' ] == 2) {
|
||||
if (empty($data[ 'virtual_indate' ])) {
|
||||
return $this->error('', '有效期不能为空');
|
||||
}
|
||||
if($data[ 'virtual_indate' ] < time()){
|
||||
return $this->error('', '核销有效期不能小于当前日期');
|
||||
}
|
||||
}
|
||||
|
||||
//获取标签名称
|
||||
$label_name = '';
|
||||
if ($data[ 'label_id' ]) {
|
||||
$label_info = model('goods_label')->getInfo([ [ 'id', '=', $data[ 'label_id' ] ] ], 'label_name');
|
||||
$label_name = $label_info[ 'label_name' ] ?? '';
|
||||
}
|
||||
$brand_name = '';
|
||||
if ($data[ 'brand_id' ]) {
|
||||
$brand_info = model('goods_brand')->getInfo([ [ 'brand_id', '=', $data[ 'brand_id' ] ] ], 'brand_name');
|
||||
$brand_name = $brand_info[ 'brand_name' ] ?? '';
|
||||
}
|
||||
|
||||
$goods_data = array (
|
||||
'goods_image' => $goods_image,
|
||||
// 'goods_stock' => $data[ 'goods_stock' ],
|
||||
'price' => !empty($data[ 'goods_sku_data' ][ 0 ][ 'price' ]) ? $data[ 'goods_sku_data' ][ 0 ][ 'price' ] : '',
|
||||
'market_price' => !empty($data[ 'goods_sku_data' ][ 0 ][ 'market_price' ]) ? $data[ 'goods_sku_data' ][ 0 ][ 'market_price' ] : '',
|
||||
'cost_price' => !empty($data[ 'goods_sku_data' ][ 0 ][ 'cost_price' ]) ? $data[ 'goods_sku_data' ][ 0 ][ 'cost_price' ] : '',
|
||||
'goods_spec_format' => $data[ 'goods_spec_format' ],
|
||||
'category_id' => $data[ 'category_id' ],
|
||||
'category_json' => $data[ 'category_json' ],
|
||||
'label_id' => $data[ 'label_id' ],
|
||||
'label_name' => $label_name,
|
||||
'timer_on' => $data[ 'timer_on' ],
|
||||
'timer_off' => $data[ 'timer_off' ],
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ],
|
||||
'sale_show' => $data[ 'sale_show' ] ?? 1,
|
||||
'stock_show' => $data[ 'stock_show' ] ?? 1,
|
||||
'market_price_show' => $data[ 'market_price_show' ] ?? 1,
|
||||
'barrage_show' => $data[ 'barrage_show' ] ?? 1,
|
||||
'virtual_deliver_type' => $data[ 'virtual_deliver_type' ] ?? '',
|
||||
'virtual_receive_type' => $data[ 'virtual_receive_type' ] ?? '',
|
||||
);
|
||||
|
||||
$common_data = array (
|
||||
'goods_name' => $data[ 'goods_name' ],
|
||||
'goods_class' => $this->goods_class[ 'id' ],
|
||||
'goods_class_name' => $this->goods_class[ 'name' ],
|
||||
'goods_attr_class' => $data[ 'goods_attr_class' ],
|
||||
'goods_attr_name' => $data[ 'goods_attr_name' ],
|
||||
'is_limit' => $data[ 'is_limit' ] ?? 0,
|
||||
'limit_type' => $data[ 'limit_type' ] ?? 1,
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'goods_content' => $data[ 'goods_content' ],
|
||||
'goods_state' => $data[ 'goods_state' ],
|
||||
'goods_stock_alarm' => $data[ 'goods_stock_alarm' ],
|
||||
'is_virtual' => 1,
|
||||
'verify_validity_type' => $data[ 'verify_validity_type' ] ?? 0,
|
||||
'is_need_verify' => $data[ 'is_need_verify' ],
|
||||
'virtual_indate' => $data[ 'virtual_indate' ],
|
||||
'goods_attr_format' => $data[ 'goods_attr_format' ],
|
||||
'introduction' => $data[ 'introduction' ],
|
||||
'keywords' => $data[ 'keywords' ],
|
||||
'unit' => $data[ 'unit' ],
|
||||
'brand_id' => $data[ 'brand_id' ],//品牌id
|
||||
'brand_name' => $brand_name,//品牌名称
|
||||
'video_url' => $data[ 'video_url' ],
|
||||
'sort' => $data[ 'sort' ],
|
||||
'goods_service_ids' => $data[ 'goods_service_ids' ],
|
||||
'create_time' => time(),
|
||||
'virtual_sale' => $data[ 'virtual_sale' ],
|
||||
'max_buy' => $data[ 'max_buy' ],
|
||||
'min_buy' => $data[ 'min_buy' ],
|
||||
'recommend_way' => $data[ 'recommend_way' ],
|
||||
'qr_id' => $data[ 'qr_id' ] ?? 0,
|
||||
'template_id' => $data[ 'template_id' ] ?? 0,
|
||||
'form_id' => $data[ 'form_id' ] ?? 0,
|
||||
'sale_channel' => $data[ 'sale_channel' ] ?? 'all',
|
||||
'sale_store' => $data[ 'sale_store' ] ?? 'all',
|
||||
'supplier_id' => $data[ 'supplier_id' ] ?? 0
|
||||
);
|
||||
$goods_id = model('goods')->add(array_merge($goods_data, $common_data));
|
||||
|
||||
$goods_stock = 0;
|
||||
|
||||
$sku_arr = array ();
|
||||
$sku_stock_list = [];
|
||||
//添加sku商品
|
||||
foreach ($data[ 'goods_sku_data' ] as $item) {
|
||||
|
||||
$goods_stock += $item[ 'stock' ];
|
||||
|
||||
$sku_data = array (
|
||||
'sku_name' => $data[ 'goods_name' ] . ' ' . $item[ 'spec_name' ],
|
||||
'spec_name' => $item[ 'spec_name' ],
|
||||
'sku_no' => $item[ 'sku_no' ],
|
||||
'sku_spec_format' => !empty($item[ 'sku_spec_format' ]) ? json_encode($item[ 'sku_spec_format' ]) : "",
|
||||
'price' => $item[ 'price' ],
|
||||
'market_price' => $item[ 'market_price' ],
|
||||
'cost_price' => $item[ 'cost_price' ],
|
||||
'discount_price' => $item[ 'price' ],//sku折扣价(默认等于单价)
|
||||
// 'stock' => $item[ 'stock' ],
|
||||
'stock_alarm' => $item[ 'stock_alarm' ],
|
||||
'sku_image' => !empty($item[ 'sku_image' ]) ? $item[ 'sku_image' ] : $first_image,
|
||||
'sku_images' => $item[ 'sku_images' ],
|
||||
'goods_id' => $goods_id,
|
||||
'is_default' => $item[ 'is_default' ] ?? 0,
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ],
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'verify_num' => $item[ 'verify_num' ] ?? 1
|
||||
);
|
||||
$sku_stock_list[] = [ 'stock' => $item[ 'stock' ], 'site_id' => $data[ 'site_id' ], 'goods_class' => $common_data[ 'goods_class' ] ];
|
||||
$sku_arr[] = array_merge($sku_data, $common_data);
|
||||
}
|
||||
|
||||
model('goods_sku')->addList($sku_arr);
|
||||
|
||||
// 赋值第一个商品sku_id
|
||||
$first_info = model('goods_sku')->getFirstData([ 'goods_id' => $goods_id ], 'sku_id', 'is_default desc,sku_id asc');
|
||||
model('goods')->update([ 'sku_id' => $first_info[ 'sku_id' ] ], [ [ 'goods_id', '=', $goods_id ] ]);
|
||||
|
||||
if (!empty($data[ 'goods_spec_format' ])) {
|
||||
// 刷新SKU商品规格项 / 规格值JSON字符串
|
||||
$this->dealGoodsSkuSpecFormat($goods_id, $data[ 'goods_spec_format' ]);
|
||||
}
|
||||
|
||||
$cron = new Cron();
|
||||
//定时上下架
|
||||
if ($goods_data[ 'timer_on' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时上架", "CronGoodsTimerOn", $goods_data[ 'timer_on' ], $goods_id);
|
||||
}
|
||||
if ($goods_data[ 'timer_off' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时下架", "CronGoodsTimerOff", $goods_data[ 'timer_off' ], $goods_id);
|
||||
}
|
||||
|
||||
//虚拟商品指定审核有效期后自动下架
|
||||
if ($data[ 'verify_validity_type' ] == 2 && $data['goods_state'] == 1) {
|
||||
$cron->addCron(1, 0, "虚拟商品定时下架", "CronVirtualGoodsVerifyOff", $data[ 'virtual_indate' ], $goods_id);
|
||||
}else{
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronVirtualGoodsVerifyOff' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
}
|
||||
//添加店铺添加统计
|
||||
$stat = new Stat();
|
||||
$stat->switchStat([ 'type' => 'add_goods', 'data' => [ 'add_goods_count' => 1, 'site_id' => $data[ 'site_id' ] ] ]);
|
||||
$stat->switchStat([ 'type' => 'goods_on', 'data' => [ 'site_id' => $data[ 'site_id' ] ] ]);
|
||||
|
||||
// 商品设置库存
|
||||
$goods_stock_model = new \app\model\stock\GoodsStock();
|
||||
$sku_list = model('goods_sku')->getList([ 'goods_id' => $goods_id ], 'sku_id');
|
||||
foreach ($sku_stock_list as $k => $v) {
|
||||
$v[ 'sku_id' ] = $sku_list[ $k ][ 'sku_id' ];
|
||||
$goods_stock_model->changeGoodsStock($v);
|
||||
}
|
||||
|
||||
model('goods')->commit();
|
||||
return $this->success($goods_id);
|
||||
} catch (\Exception $e) {
|
||||
model('goods')->rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品编辑
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editGoods($data)
|
||||
{
|
||||
|
||||
model('goods')->startTrans();
|
||||
|
||||
try {
|
||||
|
||||
if (!empty($data[ 'goods_attr_format' ])) {
|
||||
|
||||
$goods_attr_format = json_decode($data[ 'goods_attr_format' ], true);
|
||||
$keys = array_column($goods_attr_format, 'sort');
|
||||
if (!empty($keys)) {
|
||||
array_multisort($keys, SORT_ASC, SORT_NUMERIC, $goods_attr_format);
|
||||
$data[ 'goods_attr_format' ] = json_encode($goods_attr_format);
|
||||
}
|
||||
}
|
||||
|
||||
$goods_id = $data[ 'goods_id' ];
|
||||
$goods_image = $data[ 'goods_image' ];
|
||||
$first_image = explode(",", $goods_image)[ 0 ];
|
||||
|
||||
//SKU商品数据
|
||||
if (!empty($data[ 'goods_sku_data' ])) {
|
||||
$data[ 'goods_sku_data' ] = json_decode($data[ 'goods_sku_data' ], true);
|
||||
// if (empty($goods_image)) {
|
||||
// $goods_image = $data[ 'goods_sku_data' ][ 0 ][ 'sku_image' ];
|
||||
// }
|
||||
}
|
||||
|
||||
//商品编码检测
|
||||
$sku_no_check = $this->checkSkuNoRepeat(['sku_list' => $data['goods_sku_data'], 'site_id' => $data['site_id'], 'goods_id' => $data['goods_id'] ?? 0]);
|
||||
if($sku_no_check['code'] < 0){
|
||||
model('goods')->rollback();
|
||||
return $sku_no_check;
|
||||
}
|
||||
|
||||
if ($data[ 'verify_validity_type' ] == 1) {
|
||||
if (empty($data[ 'virtual_indate' ])) {
|
||||
return $this->error('', '有效期不能为空');
|
||||
}
|
||||
} else if ($data[ 'verify_validity_type' ] == 2) {
|
||||
if (empty($data[ 'virtual_indate' ])) {
|
||||
return $this->error('', '有效期不能为空');
|
||||
}
|
||||
if($data[ 'virtual_indate' ] < time()){
|
||||
return $this->error('', '核销有效期不能小于当前日期');
|
||||
}
|
||||
}
|
||||
|
||||
//获取标签名称
|
||||
$label_name = '';
|
||||
if ($data[ 'label_id' ]) {
|
||||
$label_info = model('goods_label')->getInfo([ [ 'id', '=', $data[ 'label_id' ] ] ], 'label_name');
|
||||
$label_name = $label_info[ 'label_name' ] ?? '';
|
||||
}
|
||||
$brand_name = '';
|
||||
if ($data[ 'brand_id' ]) {
|
||||
$brand_info = model('goods_brand')->getInfo([ [ 'brand_id', '=', $data[ 'brand_id' ] ] ], 'brand_name');
|
||||
$brand_name = $brand_info[ 'brand_name' ] ?? '';
|
||||
}
|
||||
|
||||
$goods_data = array (
|
||||
'goods_image' => $goods_image,
|
||||
// 'goods_stock' => $data[ 'goods_stock' ],
|
||||
'price' => $data[ 'goods_sku_data' ][ 0 ][ 'price' ],
|
||||
'market_price' => $data[ 'goods_sku_data' ][ 0 ][ 'market_price' ],
|
||||
'cost_price' => $data[ 'goods_sku_data' ][ 0 ][ 'cost_price' ],
|
||||
'goods_spec_format' => $data[ 'goods_spec_format' ],
|
||||
'category_id' => $data[ 'category_id' ],
|
||||
'category_json' => $data[ 'category_json' ],
|
||||
'label_id' => $data[ 'label_id' ],
|
||||
'label_name' => $label_name,
|
||||
'timer_on' => $data[ 'timer_on' ],
|
||||
'timer_off' => $data[ 'timer_off' ],
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ],
|
||||
'verify_validity_type' => $data[ 'verify_validity_type' ],
|
||||
'is_need_verify' => $data[ 'is_need_verify' ],
|
||||
'sale_show' => $data[ 'sale_show' ],
|
||||
'stock_show' => $data[ 'stock_show' ],
|
||||
'market_price_show' => $data[ 'market_price_show' ],
|
||||
'barrage_show' => $data[ 'barrage_show' ],
|
||||
|
||||
'virtual_deliver_type' => $data[ 'virtual_deliver_type' ],
|
||||
'virtual_receive_type' => $data[ 'virtual_receive_type' ],
|
||||
);
|
||||
|
||||
$common_data = array (
|
||||
'goods_name' => $data[ 'goods_name' ],
|
||||
'goods_class' => $this->goods_class[ 'id' ],
|
||||
'goods_class_name' => $this->goods_class[ 'name' ],
|
||||
'goods_attr_class' => $data[ 'goods_attr_class' ],
|
||||
'goods_attr_name' => $data[ 'goods_attr_name' ],
|
||||
'is_limit' => $data[ 'is_limit' ] ?? 0,
|
||||
'limit_type' => $data[ 'limit_type' ] ?? 1,
|
||||
'site_id' => $data[ 'site_id' ],
|
||||
'goods_content' => $data[ 'goods_content' ],
|
||||
'goods_state' => $data[ 'goods_state' ],
|
||||
'goods_stock_alarm' => $data[ 'goods_stock_alarm' ],
|
||||
'is_virtual' => 1,
|
||||
'virtual_indate' => $data[ 'virtual_indate' ],
|
||||
'goods_attr_format' => $data[ 'goods_attr_format' ],
|
||||
'introduction' => $data[ 'introduction' ],
|
||||
'keywords' => $data[ 'keywords' ],
|
||||
'unit' => $data[ 'unit' ],
|
||||
'video_url' => $data[ 'video_url' ],
|
||||
'brand_id' => $data[ 'brand_id' ],//品牌id
|
||||
'brand_name' => $brand_name,//品牌名称
|
||||
'sort' => $data[ 'sort' ],
|
||||
'goods_service_ids' => $data[ 'goods_service_ids' ],
|
||||
'modify_time' => time(),
|
||||
'virtual_sale' => $data[ 'virtual_sale' ],
|
||||
'max_buy' => $data[ 'max_buy' ],
|
||||
'min_buy' => $data[ 'min_buy' ],
|
||||
'recommend_way' => $data[ 'recommend_way' ],
|
||||
'qr_id' => $data[ 'qr_id' ] ?? 0,
|
||||
'template_id' => $data[ 'template_id' ] ?? 0,
|
||||
'form_id' => $data[ 'form_id' ] ?? 0,
|
||||
'sale_channel' => $data[ 'sale_channel' ] ?? 'all',
|
||||
'sale_store' => $data[ 'sale_store' ] ?? 'all',
|
||||
'supplier_id' => $data[ 'supplier_id' ] ?? 0
|
||||
);
|
||||
|
||||
model('goods')->update(array_merge($goods_data, $common_data), [ [ 'goods_id', '=', $goods_id ], [ 'goods_class', '=', $this->goods_class[ 'id' ] ] ]);
|
||||
|
||||
$goods_stock = 0;
|
||||
$goods_stock_model = new \app\model\stock\GoodsStock();
|
||||
$sku_stock_list = [];
|
||||
$is_off_store_goods = 0; // 是否下架门店商品
|
||||
|
||||
$discount_model = new Discount();
|
||||
$sku_id_arr = [];
|
||||
foreach ($data[ 'goods_sku_data' ] as $item) {
|
||||
$discount_info = [];
|
||||
if (!empty($item[ 'sku_id' ])) {
|
||||
$discount_info_result = $discount_model->getDiscountGoodsInfo([ [ 'pdg.sku_id', '=', $item[ 'sku_id' ] ], [ 'pd.status', '=', 1 ] ], 'id');
|
||||
$discount_info = $discount_info_result[ 'data' ];
|
||||
}
|
||||
$goods_stock += $item[ 'stock' ];
|
||||
$sku_data = array (
|
||||
'sku_name' => $data[ 'goods_name' ] . ' ' . $item[ 'spec_name' ],
|
||||
'spec_name' => $item[ 'spec_name' ],
|
||||
'sku_no' => $item[ 'sku_no' ],
|
||||
'sku_spec_format' => !empty($item[ 'sku_spec_format' ]) ? json_encode($item[ 'sku_spec_format' ]) : "",
|
||||
'goods_spec_format' => '',
|
||||
'price' => $item[ 'price' ],
|
||||
'market_price' => $item[ 'market_price' ],
|
||||
'cost_price' => $item[ 'cost_price' ],
|
||||
// 'stock' => $item[ 'stock' ],
|
||||
'stock_alarm' => $item[ 'stock_alarm' ],
|
||||
'sku_image' => !empty($item[ 'sku_image' ]) ? $item[ 'sku_image' ] : $first_image,
|
||||
'sku_images' => $item[ 'sku_images' ],
|
||||
'goods_id' => $goods_id,
|
||||
'is_default' => $item[ 'is_default' ] ?? 0,
|
||||
'verify_num' => $item[ 'verify_num' ],
|
||||
'is_consume_discount' => $data[ 'is_consume_discount' ]
|
||||
);
|
||||
if (empty($discount_info)) {
|
||||
$sku_data[ 'discount_price' ] = $item[ 'price' ];
|
||||
}
|
||||
if (!empty($item[ 'sku_id' ])) {
|
||||
$sku_id_arr[] = $item[ 'sku_id' ];
|
||||
model('goods_sku')->update(array_merge($sku_data, $common_data), [ [ 'sku_id', '=', $item[ 'sku_id' ] ], [ 'goods_class', '=', $this->goods_class[ 'id' ] ] ]);
|
||||
} else {
|
||||
$sku_id = model('goods_sku')->add(array_merge($sku_data, $common_data));
|
||||
$item[ 'sku_id' ] = $sku_id;
|
||||
$sku_id_arr[] = $sku_id;
|
||||
}
|
||||
$sku_stock_list[] = [ 'stock' => $item[ 'stock' ], 'sku_id' => $item[ 'sku_id' ], 'site_id' => $data[ 'site_id' ], 'goods_class' => $common_data[ 'goods_class' ] ];
|
||||
}
|
||||
|
||||
// 移除不存在的商品SKU
|
||||
$sku_id_list = model('goods_sku')->getList([ [ 'goods_id', '=', $goods_id ] ], 'sku_id');
|
||||
$sku_id_list = array_column($sku_id_list, 'sku_id');
|
||||
foreach ($sku_id_list as $k => $v) {
|
||||
foreach ($sku_id_arr as $ck => $cv) {
|
||||
if ($v == $cv) {
|
||||
unset($sku_id_list[ $k ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sku_id_list = array_values($sku_id_list);
|
||||
if (!empty($sku_id_list)) {
|
||||
$check = $this->deleteGoodsSkuCheck($sku_id_list);
|
||||
if ($check[ 'code' ] < 0) {
|
||||
model('goods')->rollback();
|
||||
return $check;
|
||||
}
|
||||
$is_off_store_goods = 1;
|
||||
model('goods_sku')->delete([ [ 'sku_id', 'in', implode(",", $sku_id_list) ] ]);
|
||||
}
|
||||
|
||||
// 赋值第一个商品sku_id
|
||||
$first_info = model('goods_sku')->getFirstData([ 'goods_id' => $goods_id ], 'sku_id', 'is_default desc,sku_id asc');
|
||||
model('goods')->update([ 'sku_id' => $first_info[ 'sku_id' ] ], [ [ 'goods_id', '=', $goods_id ] ]);
|
||||
|
||||
if (!empty($data[ 'goods_spec_format' ])) {
|
||||
// 刷新SKU商品规格项 / 规格值JSON字符串
|
||||
$this->dealGoodsSkuSpecFormat($goods_id, $data[ 'goods_spec_format' ]);
|
||||
}
|
||||
|
||||
if($is_off_store_goods == 1) {
|
||||
(new StoreGoods())->modifyStoreGoodsState($goods_id, 0);
|
||||
}
|
||||
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronGoodsTimerOn' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronGoodsTimerOff' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
//定时上下架
|
||||
if ($goods_data[ 'timer_on' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时上架", "CronGoodsTimerOn", $goods_data[ 'timer_on' ], $goods_id);
|
||||
}
|
||||
if ($goods_data[ 'timer_off' ] > 0) {
|
||||
$cron->addCron(1, 0, "商品定时下架", "CronGoodsTimerOff", $goods_data[ 'timer_off' ], $goods_id);
|
||||
}
|
||||
// 商品设置库存
|
||||
//核验和校准改变的sku
|
||||
$goods_stock_model->checkExistGoodsSku([ 'goods_id' => $goods_id ]);
|
||||
foreach ($sku_stock_list as $k => $v) {
|
||||
$goods_stock_model->changeGoodsStock($v);
|
||||
}
|
||||
|
||||
|
||||
//虚拟商品指定审核有效期后自动下架
|
||||
if ($data[ 'verify_validity_type' ] == 2 && $data['goods_state'] == 1) {
|
||||
$cron->addCron(1, 0, "虚拟商品定时下架", "CronVirtualGoodsVerifyOff", $data[ 'virtual_indate' ], $goods_id);
|
||||
}else{
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronVirtualGoodsVerifyOff' ], [ 'relate_id', '=', $goods_id ] ]);
|
||||
}
|
||||
//添加店铺添加统计
|
||||
$stat = new Stat();
|
||||
$stat->switchStat([ 'type' => 'goods_on', 'data' => [ 'site_id' => $data[ 'site_id' ] ] ]);
|
||||
model('goods')->commit();
|
||||
return $this->success($goods_id);
|
||||
} catch (\Exception $e) {
|
||||
model('goods')->rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getGoodsInfo($condition, $field = 'goods_id,goods_name,goods_class,goods_class_name,goods_attr_class,goods_attr_name,,goods_image,goods_content,goods_state,price,market_price,cost_price,goods_stock,goods_stock_alarm,goods_spec_format,goods_attr_format,introduction,keywords,unit,sort,video_url,evaluate,virtual_indate')
|
||||
{
|
||||
$info = model('goods')->getInfo($condition, $field);
|
||||
if (!empty($info)) {
|
||||
if (isset($info[ 'goods_stock' ])) {
|
||||
$info[ 'goods_stock' ] = numberFormat($info[ 'goods_stock' ]);
|
||||
}
|
||||
if (isset($info[ 'sale_num' ])) {
|
||||
$info[ 'sale_num' ] = numberFormat($info[ 'sale_num' ]);
|
||||
}
|
||||
if (isset($info[ 'virtual_sale' ])) {
|
||||
$info[ 'virtual_sale' ] = numberFormat($info[ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($info[ 'real_stock' ])) {
|
||||
$info[ 'real_stock' ] = numberFormat($info[ 'real_stock' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getGoodsList($condition = [], $field = 'goods_id,goods_class,goods_class_name,goods_attr_name,goods_name,site_id,,sort,goods_image,goods_content,goods_state,price,market_price,cost_price,goods_stock,goods_stock_alarm,is_virtual,goods_spec_format,goods_attr_format,create_time', $order = 'create_time desc', $limit = null)
|
||||
{
|
||||
$list = model('goods')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $k => $v) {
|
||||
if (isset($v[ 'goods_stock' ])) {
|
||||
$list[ $k ][ 'goods_stock' ] = numberFormat($list[ $k ][ 'goods_stock' ]);
|
||||
}
|
||||
if (isset($v[ 'sale_num' ])) {
|
||||
$list[ $k ][ 'sale_num' ] = numberFormat($list[ $k ][ 'sale_num' ]);
|
||||
}
|
||||
if (isset($v[ 'virtual_sale' ])) {
|
||||
$list[ $k ][ 'virtual_sale' ] = numberFormat($list[ $k ][ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($v[ 'real_stock' ])) {
|
||||
$list[ $k ][ 'real_stock' ] = numberFormat($list[ $k ][ 'real_stock' ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = 'goods_id,goods_name,site_id,goods_image,goods_state,price,goods_stock,create_time,sale_num')
|
||||
{
|
||||
$res = model('goods')->pageList($condition, $field, $order, $page, $page_size);
|
||||
foreach ($res[ 'list' ] as $k => $v) {
|
||||
if (isset($v[ 'goods_stock' ])) {
|
||||
$res[ 'list' ][ $k ][ 'goods_stock' ] = numberFormat($res[ 'list' ][ $k ][ 'goods_stock' ]);
|
||||
}
|
||||
if (isset($v[ 'sale_num' ])) {
|
||||
$res[ 'list' ][ $k ][ 'sale_num' ] = numberFormat($res[ 'list' ][ $k ][ 'sale_num' ]);
|
||||
}
|
||||
if (isset($v[ 'virtual_sale' ])) {
|
||||
$res[ 'list' ][ $k ][ 'virtual_sale' ] = numberFormat($res[ 'list' ][ $k ][ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($v[ 'real_stock' ])) {
|
||||
$res[ 'list' ][ $k ][ 'real_stock' ] = numberFormat($res[ 'list' ][ $k ][ 'real_stock' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品sku列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getGoodsSkuList($condition = [], $field = 'sku_id,sku_name,price,stock,sale_num,sku_image,goods_id,goods_name,site_id,spec_name', $order = 'create_time desc', $limit = null)
|
||||
{
|
||||
$list = model('goods_sku')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
foreach ($list as $k => $v) {
|
||||
if (isset($v[ 'stock' ])) {
|
||||
$list[ $k ][ 'stock' ] = numberFormat($list[ $k ][ 'stock' ]);
|
||||
}
|
||||
if (isset($v[ 'sale_num' ])) {
|
||||
$list[ $k ][ 'sale_num' ] = numberFormat($list[ $k ][ 'sale_num' ]);
|
||||
}
|
||||
if (isset($v[ 'virtual_sale' ])) {
|
||||
$list[ $k ][ 'virtual_sale' ] = numberFormat($list[ $k ][ 'virtual_sale' ]);
|
||||
}
|
||||
if (isset($v[ 'real_stock' ])) {
|
||||
$list[ $k ][ 'real_stock' ] = numberFormat($list[ $k ][ 'real_stock' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/************************************************************************* 购买的虚拟产品 start *******************************************************************/
|
||||
|
||||
/**
|
||||
* 生成购买的虚拟产品
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function addGoodsVirtual($params)
|
||||
{
|
||||
$data = array (
|
||||
'site_id' => $params[ 'site_id' ],
|
||||
'order_id' => $params[ 'order_id' ],
|
||||
'order_no' => $params[ 'order_no' ],
|
||||
'sku_id' => $params[ 'sku_id' ],
|
||||
'sku_name' => $params[ 'sku_name' ],
|
||||
'code' => $params[ 'code' ] ?? '',
|
||||
'member_id' => $params[ 'member_id' ],
|
||||
'sku_image' => $params[ 'sku_image' ],
|
||||
'expire_time' => $params[ 'expire_time' ] ?? '',
|
||||
'card_info' => $params[ 'card_info' ] ?? '',
|
||||
'sold_time' => $params[ 'sold_time' ] ?? time(),
|
||||
'goods_id' => $params[ 'goods_id' ],
|
||||
'verify_total_count' => $params[ 'verify_total_count' ] ?? 0
|
||||
);
|
||||
$res = model("goods_virtual")->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteGoodsVirtual($condition)
|
||||
{
|
||||
$res = model("goods_virtual")->delete($condition);
|
||||
if ($res === false)
|
||||
return $this->error();
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 核销虚拟商品
|
||||
* @param $code
|
||||
* @return array|mixed|string|null
|
||||
*/
|
||||
public function verify($code)
|
||||
{
|
||||
$goods_virtual_info = model("goods_virtual")->getInfo([ [ "code", '=', $code ], [ "is_veirfy", "=", 0 ] ]);
|
||||
if (empty($goods_virtual_info))
|
||||
return $this->error([], '虚拟商品不存在或已失效');//虚拟商品不存在或已核销
|
||||
|
||||
$order_common_model = new OrderCommon();
|
||||
$local_result = $order_common_model->verifyOrderLock($goods_virtual_info[ 'order_id' ]);
|
||||
if ($local_result[ "code" ] < 0)
|
||||
return $local_result;
|
||||
//核销发送通知
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage([ 'keywords' => "VERIFY", 'order_id' => $goods_virtual_info[ 'order_id' ], 'site_id' => $goods_virtual_info[ 'site_id' ] ]);
|
||||
model('goods_virtual')->startTrans();
|
||||
try {
|
||||
$verify_total_count = $goods_virtual_info[ 'verify_total_count' ];//总核销次数
|
||||
$verify_use_num = $goods_virtual_info[ 'verify_use_num' ];//已核销次数
|
||||
$now_verify_use_num = $verify_use_num + 1;
|
||||
$data = array (
|
||||
"verify_time" => time(),
|
||||
'verify_use_num' => $now_verify_use_num
|
||||
);
|
||||
$is_veirfy = 0;
|
||||
if ($now_verify_use_num >= $verify_total_count) {
|
||||
$data[ 'is_veirfy' ] = 1;
|
||||
$is_veirfy = 1;
|
||||
}
|
||||
|
||||
$res = model("goods_virtual")->update($data, [ [ "code", '=', $code ], [ "is_veirfy", "=", 0 ] ]);
|
||||
|
||||
//判断是否全部核销完毕
|
||||
// $count = model('goods_virtual')->getCount([['order_id','=',$goods_virtual_info[ 'order_id' ]],['is_veirfy','=',0]]);
|
||||
// if($count == 0){//全部核销完毕
|
||||
|
||||
if ($is_veirfy == 1) {//全部核销完毕
|
||||
//订单执行自动完成
|
||||
$result = event('CronOrderTakeDelivery', [ 'relate_id' => $goods_virtual_info[ 'order_id' ] ], true);
|
||||
if ($result[ 'code' ] < 0) {
|
||||
model('goods_virtual')->rollback();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
model('goods_virtual')->commit();
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('goods_virtual')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 虚拟商品详情
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getVirtualGoodsInfo($condition, $field = "*")
|
||||
{
|
||||
$info = model('goods_virtual')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取虚拟商品分页列表
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getVirtualGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$list = model('goods_virtual')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/************************************************************************* 购买的虚拟产品 end *******************************************************************/
|
||||
|
||||
/**
|
||||
* 商品导入
|
||||
* @param $goods_data
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function importGoods($goods_data, $site_id)
|
||||
{
|
||||
try {
|
||||
if (empty($goods_data[ 'goods_name' ])) return $this->error('', '商品名称不能为空');
|
||||
if (empty($goods_data[ 'goods_image' ])) return $this->error('', '商品主图不能为空');
|
||||
if (empty($goods_data[ 'category_1' ]) && empty($goods_data[ 'category_2' ]) && empty($goods_data[ 'category_3' ])) return $this->error('', '商品分类不能为空');
|
||||
|
||||
// 处理商品分类
|
||||
$category_id = '';
|
||||
$category_json = [];
|
||||
if (!empty($goods_data[ 'category_3' ])) {
|
||||
$category_info = model('goods_category')->getInfo([ [ 'level', '=', 3 ], [ 'site_id', '=', $site_id ], [ 'category_full_name', '=', "{$goods_data['category_1']}/{$goods_data['category_2']}/{$goods_data['category_3']}" ] ], 'category_id_1,category_id_2,category_id_3');
|
||||
if (!empty($category_info)) {
|
||||
$category_id = "{$category_info['category_id_1']},{$category_info['category_id_2']},{$category_info['category_id_3']}";
|
||||
}
|
||||
}
|
||||
if (!empty($goods_data[ 'category_2' ]) && empty($category_id)) {
|
||||
$category_info = model('goods_category')->getInfo([ [ 'level', '=', 2 ], [ 'site_id', '=', $site_id ], [ 'category_full_name', '=', "{$goods_data['category_1']}/{$goods_data['category_2']}" ] ], 'category_id_1,category_id_2');
|
||||
if (!empty($category_info)) {
|
||||
$category_id = "{$category_info['category_id_1']},{$category_info['category_id_2']}";
|
||||
}
|
||||
}
|
||||
if (!empty($goods_data[ 'category_1' ]) && empty($category_id)) {
|
||||
$category_info = model('goods_category')->getInfo([ [ 'level', '=', 1 ], [ 'site_id', '=', $site_id ], [ 'category_name', '=', "{$goods_data['category_1']}" ] ], 'category_id_1');
|
||||
if (!empty($category_info)) {
|
||||
$category_id = "{$category_info['category_id_1']}";
|
||||
}
|
||||
}
|
||||
if (empty($category_id)) return $this->error('', '未找到所填商品分类');
|
||||
$category_json = [ $category_id ];
|
||||
|
||||
$sku_data = [];
|
||||
$goods_spec_format = [];
|
||||
$tag = 0;
|
||||
// 处理sku数据
|
||||
if (isset($goods_data[ 'sku' ])) {
|
||||
foreach ($goods_data[ 'sku' ] as $sku_item) {
|
||||
if (empty($sku_item[ 'sku_data' ])) return $this->error('', '规格数据不能为空');
|
||||
|
||||
$spec_name = '';
|
||||
$spec_data = explode(';', $sku_item[ 'sku_data' ]);
|
||||
|
||||
$sku_spec_format = [];
|
||||
foreach ($spec_data as $item) {
|
||||
$spec_item = explode(':', $item);
|
||||
$spec_name .= ' ' . $spec_item[ 1 ];
|
||||
|
||||
// 规格项
|
||||
$spec_index = array_search($spec_item[ 0 ], array_column($goods_spec_format, 'spec_name'));
|
||||
if (empty($goods_spec_format) || $spec_index === false) {
|
||||
$spec = [
|
||||
'spec_id' => -( $tag + getMillisecond() ),
|
||||
'spec_name' => $spec_item[ 0 ],
|
||||
'value' => []
|
||||
];
|
||||
array_push($goods_spec_format, $spec);
|
||||
$tag++;
|
||||
} else {
|
||||
$spec = $goods_spec_format[ $spec_index ];
|
||||
}
|
||||
// 规格值
|
||||
$spec_index = array_search($spec_item[ 0 ], array_column($goods_spec_format, 'spec_name'));
|
||||
$spec_value_index = array_search($spec_item[ 1 ], array_column($spec[ 'value' ], 'spec_value_name'));
|
||||
if (empty($spec[ 'value' ]) || $spec_value_index === false) {
|
||||
$spec_value = [
|
||||
'spec_id' => $spec[ 'spec_id' ],
|
||||
'spec_name' => $spec[ 'spec_name' ],
|
||||
'spec_value_id' => -( $tag + getMillisecond() ),
|
||||
'spec_value_name' => $spec_item[ 1 ],
|
||||
'image' => '',
|
||||
];
|
||||
array_push($goods_spec_format[ $spec_index ][ 'value' ], $spec_value);
|
||||
$tag++;
|
||||
} else {
|
||||
$spec_value = $spec[ 'value' ][ $spec_value_index ];
|
||||
}
|
||||
|
||||
array_push($sku_spec_format, [
|
||||
'spec_id' => $spec[ 'spec_id' ],
|
||||
'spec_name' => $spec[ 'spec_name' ],
|
||||
'spec_value_id' => $spec_value[ 'spec_value_id' ],
|
||||
'spec_value_name' => $spec_value[ 'spec_value_name' ],
|
||||
'image' => '',
|
||||
]);
|
||||
}
|
||||
|
||||
$sku_images_arr = explode(',', $sku_item[ 'sku_image' ]);
|
||||
|
||||
$sku_temp = [
|
||||
'spec_name' => trim($spec_name),
|
||||
'sku_no' => $sku_item[ 'sku_code' ],
|
||||
'sku_spec_format' => $sku_spec_format,
|
||||
'price' => $sku_item[ 'price' ],
|
||||
'market_price' => $sku_item[ 'market_price' ],
|
||||
'cost_price' => $sku_item[ 'cost_price' ],
|
||||
'stock' => $sku_item[ 'stock' ],
|
||||
'stock_alarm' => $sku_item[ 'stock_alarm' ],
|
||||
'verify_num' => empty($sku_item[ 'verify_num' ]) ? 1 : $sku_item[ 'verify_num' ],
|
||||
'sku_image' => empty($sku_item[ 'sku_image' ]) ? '' : $sku_images_arr[ 0 ],
|
||||
'sku_images' => empty($sku_item[ 'sku_image' ]) ? '' : $sku_item[ 'sku_image' ],
|
||||
'sku_images_arr' => empty($sku_item[ 'sku_image' ]) ? [] : $sku_images_arr,
|
||||
'is_default' => 0
|
||||
];
|
||||
|
||||
array_push($sku_data, $sku_temp);
|
||||
}
|
||||
} else {
|
||||
$goods_img = explode(',', $goods_data[ 'goods_image' ]);
|
||||
$sku_data = [
|
||||
[
|
||||
'sku_id' => 0,
|
||||
'sku_name' => $goods_data[ 'goods_name' ],
|
||||
'spec_name' => '',
|
||||
'sku_spec_format' => '',
|
||||
'price' => empty($goods_data[ 'price' ]) ? 0 : $goods_data[ 'price' ],
|
||||
'market_price' => empty($goods_data[ 'market_price' ]) ? 0 : $goods_data[ 'market_price' ],
|
||||
'cost_price' => empty($goods_data[ 'cost_price' ]) ? 0 : $goods_data[ 'cost_price' ],
|
||||
'sku_no' => $goods_data[ 'goods_code' ],
|
||||
'verify_num' => empty($goods_data[ 'verify_num' ]) ? 1 : $goods_data[ 'verify_num' ],
|
||||
'stock' => empty($goods_data[ 'stock' ]) ? 0 : $goods_data[ 'stock' ],
|
||||
'stock_alarm' => empty($goods_data[ 'stock_alarm' ]) ? 0 : $goods_data[ 'stock_alarm' ],
|
||||
'sku_image' => $goods_img[ 0 ],
|
||||
'sku_images' => $goods_data[ 'goods_image' ]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
if (count($goods_spec_format) > 4) return $this->error('', '最多支持四种规格项');
|
||||
|
||||
$data = [
|
||||
'goods_name' => $goods_data[ 'goods_name' ],// 商品名称,
|
||||
'goods_attr_class' => '',// 商品类型id,
|
||||
'goods_attr_name' => '',// 商品类型名称,
|
||||
'site_id' => $site_id,
|
||||
'category_id' => ',' . $category_id . ',',
|
||||
'category_json' => json_encode($category_json),
|
||||
'goods_image' => $goods_data[ 'goods_image' ],// 商品主图路径
|
||||
'goods_content' => '',// 商品详情
|
||||
'goods_state' => 0, //$goods_data['goods_state'] == 1 || $goods_data['goods_state'] == '是' ? 1 : 0,// 商品状态(1.正常0下架)
|
||||
'price' => empty($goods_data[ 'price' ]) ? 0 : $goods_data[ 'price' ],// 商品价格(取第一个sku)
|
||||
'market_price' => empty($goods_data[ 'market_price' ]) ? 0 : $goods_data[ 'market_price' ],// 市场价格(取第一个sku)
|
||||
'cost_price' => empty($goods_data[ 'cost_price' ]) ? 0 : $goods_data[ 'cost_price' ],// 成本价(取第一个sku)
|
||||
'sku_no' => $goods_data[ 'goods_code' ],// 商品sku编码
|
||||
'goods_stock' => empty($goods_data[ 'goods_stock' ]) ? 0 : $goods_data[ 'goods_stock' ],// 商品库存(总和)
|
||||
'goods_stock_alarm' => empty($goods_data[ 'goods_stock_alarm' ]) ? 0 : $goods_data[ 'goods_stock_alarm' ],// 库存预警
|
||||
'goods_spec_format' => empty($goods_spec_format) ? '' : json_encode($goods_spec_format, JSON_UNESCAPED_UNICODE),// 商品规格格式
|
||||
'goods_attr_format' => '',// 商品参数格式
|
||||
'introduction' => $goods_data[ 'introduction' ],// 促销语
|
||||
'keywords' => $goods_data[ 'keywords' ],// 关键词
|
||||
'unit' => $goods_data[ 'unit' ],// 单位
|
||||
'sort' => '',// 排序,
|
||||
'qr_id' => empty($goods_data[ 'qr_id' ]) ? 0 : $goods_data[ 'qr_id' ],// 社群二维码id
|
||||
'template_id' => empty($goods_data[ 'template_id' ]) ? 0 : $goods_data[ 'template_id' ],// 海报id
|
||||
'is_limit' => empty($goods_data[ 'is_limit' ]) ? 0 : $goods_data[ 'is_limit' ],// 是否限购
|
||||
'limit_type' => empty($goods_data[ 'limit_type' ]) ? 0 : $goods_data[ 'limit_type' ],// 限购类型
|
||||
'video_url' => '',// 视频
|
||||
'goods_sku_data' => json_encode($sku_data, JSON_UNESCAPED_UNICODE),// SKU商品数据
|
||||
'goods_service_ids' => '',// 商品服务id集合
|
||||
'label_id' => '',// 商品分组id
|
||||
'virtual_sale' => 0,// 虚拟销量
|
||||
'max_buy' => 0,// 限购
|
||||
'min_buy' => 0,// 起售
|
||||
'recommend_way' => 0, // 推荐方式,1:新品,2:精品,3;推荐
|
||||
'timer_on' => 0,//定时上架
|
||||
'timer_off' => 0,//定时下架
|
||||
'is_consume_discount' => $goods_data[ 'is_consume_discount' ] == 1 || $goods_data[ 'is_consume_discount' ] == '是' ? 1 : 0, //是否参与会员折扣
|
||||
'is_need_verify' => $goods_data[ 'is_need_verify' ] == 1 || $goods_data[ 'is_need_verify' ] == '是' ? 1 : 0,
|
||||
'verify_validity_type' => empty($goods_data[ 'verify_validity_type' ]) ? 0 : $goods_data[ 'verify_validity_type' ], // 核销有效期类型
|
||||
'virtual_indate' => 0,// 虚拟商品有效期
|
||||
'brand_id' => 0,
|
||||
'virtual_deliver_type' => 'auto_deliver',
|
||||
'virtual_receive_type' => 'auto_receive'
|
||||
];
|
||||
if ($goods_data[ 'verify_validity_type' ] == 1) {
|
||||
$data[ 'virtual_date' ] = $goods_data[ 'virtual_indate' ];
|
||||
} elseif ($goods_data[ 'verify_validity_type' ] == 2) {
|
||||
$t1 = intval(( $goods_data[ 'virtual_indate' ] - 25569 ) * 3600 * 24); //转换成1970年以来的秒数
|
||||
$createtime = gmdate('Y-m-d H:i:s', $t1);//格式化时间
|
||||
$data[ 'virtual_time' ] = $createtime;
|
||||
}
|
||||
$res = $this->addGoods($data);
|
||||
return $res;
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
150
app/model/image/GdClass.php
Executable file
150
app/model/image/GdClass.php
Executable file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2022-06-24
|
||||
* Time: 12:23
|
||||
*/
|
||||
|
||||
namespace app\model\image;
|
||||
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
|
||||
/**
|
||||
* Gd 图片策略类(也就是默认)
|
||||
* Class ImagickClass
|
||||
* @package app\model\upload
|
||||
*/
|
||||
class GdClass
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 获取图片实例
|
||||
* @param $path
|
||||
* @return mixed
|
||||
*/
|
||||
public function open($path)
|
||||
{
|
||||
|
||||
$image = Image::make($path);
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片保存
|
||||
* @param $image
|
||||
* @param $new_file
|
||||
* @param $compress
|
||||
* @return mixed
|
||||
*/
|
||||
public function save($image, $new_file, $compress)
|
||||
{
|
||||
return $image->save($new_file, $compress);
|
||||
}
|
||||
|
||||
public function getImageParam($image)
|
||||
{
|
||||
$width = $image->width();//图片宽
|
||||
$height = $image->height();//图片高
|
||||
return [
|
||||
'width' => $width,
|
||||
'height' => $height
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 文字水印
|
||||
* @param $image
|
||||
* @param $text
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @param $size
|
||||
* @param $color
|
||||
* @param $align
|
||||
* @param $valign
|
||||
* @param $angle
|
||||
* @return mixed
|
||||
*/
|
||||
public function textWater($image, $text, $x, $y, $size, $color, $align, $valign, $angle)
|
||||
{
|
||||
// $x = $image->width()/2;
|
||||
// $y = $image->height()/2;
|
||||
$image->text($text, $x, $y, function($font) use ($size, $color, $align, $valign, $angle) {
|
||||
// $font->file($this->config["water"]["watermark_text_file"]);//设置字体文件位置
|
||||
$font->file(PUBLIC_PATH . 'static/font/Microsoft.ttf');
|
||||
$font->size($size);//设置字号大小
|
||||
$font->color($color);//设置字号颜色
|
||||
$font->align($align);//设置字号水平位置
|
||||
$font->valign($valign);//设置字号 垂直位置
|
||||
$font->angle($angle);//设置字号倾斜角度
|
||||
});
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片水印
|
||||
* @param $image
|
||||
* @param $water_path
|
||||
* @param $watermark_opacity
|
||||
* @param $water_rotate
|
||||
* @param $water_position
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @param $p "缩放比例 0-100"
|
||||
* @return mixed
|
||||
*/
|
||||
public function imageWater($image, $water_path, $watermark_opacity, $water_rotate, $water_position, $x, $y, $p = null)
|
||||
{
|
||||
$watermark = Image::make($water_path);
|
||||
|
||||
//根据比率 计算缩放后的水印图大小
|
||||
if(!is_null($p)){
|
||||
$water_real_width = $watermark->width();
|
||||
$water_real_height = $watermark->height();
|
||||
$water_max_width = $image->width() * $p / 100;
|
||||
$water_max_height = $image->height() * $p / 100;
|
||||
if($water_max_height / $water_max_width < $water_real_height / $water_real_width){
|
||||
$water_height = $water_max_height;
|
||||
$water_width = $water_max_height / ($water_real_height / $water_real_width);
|
||||
}else{
|
||||
$water_width = $water_max_width;
|
||||
$water_height = $water_max_width * ($water_real_height / $water_real_width);
|
||||
}
|
||||
$watermark = $watermark->resize($water_width, $water_height);
|
||||
}
|
||||
|
||||
//dd($y, $p, [$water_real_width,$water_real_height], [$water_max_width,$water_max_height], [$water_width,$water_height]);
|
||||
|
||||
$watermark = $watermark->opacity($watermark_opacity)->rotate($water_rotate);
|
||||
$image->insert($watermark, $water_position, $x, $y);
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缩略图
|
||||
* @param $image
|
||||
* @param $width
|
||||
* @param $height
|
||||
* @param string $fit
|
||||
* @param string $fill_color
|
||||
* @return mixed
|
||||
*/
|
||||
public function thumb($image, $width, $height, $fit = 'center', $fill_color = 'ffffff')
|
||||
{
|
||||
|
||||
|
||||
if (!empty($fit)) {
|
||||
$image = $image->fit($width, $height, function($constraint) {
|
||||
// $constraint->aspectRatio();
|
||||
// $constraint->upsize();
|
||||
});
|
||||
} else {
|
||||
$image = $image->resize($width, $height, function($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
$constraint->upsize();
|
||||
});
|
||||
}
|
||||
return $image;
|
||||
}
|
||||
}
|
||||
114
app/model/image/ImageService.php
Executable file
114
app/model/image/ImageService.php
Executable file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2022-06-24
|
||||
* Time: 12:23
|
||||
*/
|
||||
|
||||
namespace app\model\image;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 图片处理服务类
|
||||
*/
|
||||
class ImageService
|
||||
{
|
||||
|
||||
public $imageClass;
|
||||
|
||||
public $image;
|
||||
|
||||
public $width;
|
||||
public $height;
|
||||
|
||||
public function __construct($driver = 'gd')
|
||||
{
|
||||
//可以考虑设计成策略(author 周)
|
||||
switch($driver){
|
||||
case 'gd':
|
||||
$this->imageClass = new GdClass();
|
||||
break;
|
||||
case 'imagick':
|
||||
$this->imageClass = new ImagickClass();
|
||||
break;
|
||||
}
|
||||
// return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片实例
|
||||
* @param $path
|
||||
* @return mixed
|
||||
*/
|
||||
public function open($path){
|
||||
|
||||
$this->image = $this->imageClass->open($path);
|
||||
$this->getImageParam();
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function getImageParam(){
|
||||
$param = $this->imageClass->getImageParam($this->image);
|
||||
$this->width = $param['width'];
|
||||
$this->height = $param['height'];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片保存
|
||||
* @param $new_file
|
||||
* @param int $compress
|
||||
* @return mixed
|
||||
*/
|
||||
public function save($new_file, $compress = 90){
|
||||
return $this->imageClass->save($this->image, $new_file, $compress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文字水印
|
||||
* @param $text
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @param $size
|
||||
* @param $color
|
||||
* @param $align
|
||||
* @param $valign
|
||||
* @param $angle
|
||||
* @return mixed
|
||||
*/
|
||||
public function textWater($text, $x, $y, $size, $color, $align, $valign, $angle){
|
||||
$this->image = $this->imageClass->textWater($this->image, $text, $x, $y, $size, $color, $align, $valign, $angle);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片水印
|
||||
* @param $water_path
|
||||
* @param $water_opacity
|
||||
* @param $water_rotate
|
||||
* @param $water_position
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @return mixed
|
||||
*/
|
||||
public function imageWater($water_path, $water_opacity, $water_rotate, $water_position, $x, $y, $p = null){
|
||||
$this->image = $this->imageClass->imageWater($this->image, $water_path, $water_opacity, $water_rotate, $water_position, $x, $y, $p);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缩略图
|
||||
* @param $width
|
||||
* @param $height
|
||||
* @param string $fit
|
||||
* @param string $fill_color
|
||||
* @return ImageService
|
||||
*/
|
||||
public function thumb($width, $height, $fit = 'center', $fill_color = 'ffffff'){
|
||||
$this->image = $this->imageClass->thumb($this->image, $width, $height, $fit, $fill_color);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
111
app/model/image/ImagickClass.php
Executable file
111
app/model/image/ImagickClass.php
Executable file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2022-06-24
|
||||
* Time: 12:23
|
||||
*/
|
||||
|
||||
namespace app\model\image;
|
||||
|
||||
/**
|
||||
* Imagick 图片策略类
|
||||
* Class ImagickClass
|
||||
* @package app\model\upload
|
||||
*/
|
||||
class ImagickClass
|
||||
{
|
||||
/**
|
||||
* 获取图片实例
|
||||
* @param $path
|
||||
* @return mixed
|
||||
*/
|
||||
public function open($path){
|
||||
|
||||
$image = (new ImagickService())->open($path);
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片保存
|
||||
* @param $image
|
||||
* @param $new_file
|
||||
* @param string $compress
|
||||
* @return mixed
|
||||
*/
|
||||
public function save($image, $new_file, $compress = ''){
|
||||
return $image->save_to($new_file, $compress);
|
||||
}
|
||||
|
||||
public function getImageParam($image){
|
||||
|
||||
$size = $image->getImageParam();
|
||||
return [
|
||||
'width' => $size['width'],
|
||||
'height' => $size['height']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 文字水印
|
||||
* @param $image
|
||||
* @param $text
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @param $size
|
||||
* @param $color
|
||||
* @param $align
|
||||
* @param $valign
|
||||
* @param $angle
|
||||
* @return mixed
|
||||
*/
|
||||
public function textWater($image, $text, $x, $y, $size, $color, $align, $valign, $angle){
|
||||
// $spec = $this->getImageParam($image);
|
||||
// $x = $spec['width']/2;
|
||||
// $y = $spec['height']/2;
|
||||
$style = array(
|
||||
'font' => PUBLIC_PATH.'static/font/Microsoft.ttf',
|
||||
'font_size' => $size,
|
||||
// 'fill_color' => $size,
|
||||
// 'under_color' => $size,
|
||||
);
|
||||
$image->add_text($text, $x, $y, $angle = 0, $style);
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片水印
|
||||
* @param $image
|
||||
* @param $water_path
|
||||
* @param $watermark_opacity
|
||||
* @param $water_rotate
|
||||
* @param $water_position
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @return mixed
|
||||
*/
|
||||
public function imageWater($image, $water_path, $watermark_opacity, $water_rotate, $water_position, $x, $y){
|
||||
$image->add_watermark($water_path, $x, $y);
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缩略图
|
||||
* @param $image
|
||||
* @param $width
|
||||
* @param $height
|
||||
* @param string $fit
|
||||
* @param string $fill_color
|
||||
* @return mixed
|
||||
*/
|
||||
public function thumb($image, $width, $height, $fit = 'center', $fill_color = 'ffffff'){
|
||||
if(!empty($fit)){
|
||||
$fit = 'force';
|
||||
}else{
|
||||
$fit = 'scale';
|
||||
}
|
||||
$image->resize_to($width, $height, $fit);
|
||||
return $image;
|
||||
}
|
||||
}
|
||||
821
app/model/image/ImagickService.php
Executable file
821
app/model/image/ImagickService.php
Executable file
@@ -0,0 +1,821 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2022-06-24
|
||||
* Time: 12:23
|
||||
*/
|
||||
|
||||
namespace app\model\image;
|
||||
|
||||
|
||||
/**
|
||||
* 图片处理服务类
|
||||
* 使用php扩展服务Imagick实现
|
||||
* ImageMagick 官网地址 [url]http:www.imagemagick.org/script/index.php[/url]
|
||||
*
|
||||
* @author weiguang3
|
||||
* @since 20140403
|
||||
*/
|
||||
class ImagickService
|
||||
{
|
||||
private $image = null;
|
||||
|
||||
private $type = null;
|
||||
|
||||
// 构造函数
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
// 析构函数
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->image !== null)
|
||||
$this->image->destroy();
|
||||
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function isBinary($data)
|
||||
{
|
||||
if (is_string($data)) {
|
||||
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
|
||||
return (substr($mime, 0, 4) != 'text' && $mime != 'application/x-empty');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 载入图像
|
||||
public function open($path)
|
||||
{
|
||||
if (filter_var($path, FILTER_VALIDATE_URL)) {
|
||||
$tempFile = tempnam(sys_get_temp_dir(), 'remote_img');
|
||||
file_put_contents($tempFile, file_get_contents($path));
|
||||
$path = $tempFile;
|
||||
}
|
||||
|
||||
if($this->isBinary($path)){
|
||||
$this->image = new \Imagick();
|
||||
$this->image->readImageBlob($path);
|
||||
}else{
|
||||
$this->image = new \Imagick($path);
|
||||
}
|
||||
|
||||
if ($this->image) {
|
||||
$this->type = strtolower($this->image->getImageFormat());
|
||||
|
||||
}
|
||||
$size = $this->image->getImagePage();
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
public function getImageParam(){
|
||||
// $size = $this->image->getImagePage();
|
||||
$size = array(
|
||||
'width' => $this->image->getImageWidth(),
|
||||
'height' => $this->image->getImageHeight(),
|
||||
);
|
||||
return $size;
|
||||
}
|
||||
/**
|
||||
* 图片裁剪
|
||||
* 裁剪规则:
|
||||
* 1. 高度为空或为零 按宽度缩放 高度自适应
|
||||
* 2. 宽度为空或为零 按高度缩放 宽度自适应
|
||||
* 3. 宽度,高度到不为空或为零 按宽高比例等比例缩放裁剪 默认从头部居中裁剪
|
||||
* @param number $width
|
||||
* @param number $height
|
||||
*/
|
||||
|
||||
public function resize($width = 0, $height = 0)
|
||||
{
|
||||
if ($width == 0 && $height == 0) {
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
$color = '';// 'rgba(255,255,255,1)';
|
||||
|
||||
$size = $this->image->getImagePage();
|
||||
|
||||
//原始宽高
|
||||
|
||||
$src_width = $size ['width'];
|
||||
|
||||
$src_height = $size ['height'];
|
||||
|
||||
//按宽度缩放 高度自适应
|
||||
|
||||
if ($width != 0 && $height == 0) {
|
||||
if ($src_width > $width) {
|
||||
$height = intval($width * $src_height / $src_width);
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$this->_resizeGif($width, $height);
|
||||
|
||||
} else {
|
||||
$this->image->thumbnailImage($width, $height, true);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
//按高度缩放 宽度自适应
|
||||
|
||||
if ($width == 0 && $height != 0) {
|
||||
if ($src_height > $height) {
|
||||
$width = intval($src_width * $height / $src_height);
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$this->_resizeGif($width, $height);
|
||||
|
||||
} else {
|
||||
$this->image->thumbnailImage($width, $height, true);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
//缩放的后的尺寸
|
||||
$crop_w = $width;
|
||||
$crop_h = $height;
|
||||
//缩放后裁剪的位置
|
||||
$crop_x = 0;
|
||||
$crop_y = 0;
|
||||
|
||||
if (($src_width / $src_height) < ($width / $height)) {
|
||||
//宽高比例小于目标宽高比例 宽度等比例放大 按目标高度从头部截取
|
||||
|
||||
$crop_h = intval($src_height * $width / $src_width);
|
||||
|
||||
//从顶部裁剪 不用计算 $crop_y
|
||||
|
||||
} else {
|
||||
//宽高比例大于目标宽高比例 高度等比例放大 按目标宽度居中裁剪
|
||||
|
||||
$crop_w = intval($src_width * $height / $src_height);
|
||||
|
||||
$crop_x = intval(($crop_w - $width) / 2);
|
||||
|
||||
}
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$this->_resizeGif($crop_w, $crop_h, true, $width, $height, $crop_x, $crop_y);
|
||||
|
||||
} else {
|
||||
$this->image->thumbnailImage($crop_w, $crop_h, true);
|
||||
|
||||
$this->image->cropImage($width, $height, $crop_x, $crop_y);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理gif图片 需要对每一帧图片处理
|
||||
* @param unknown $t_w 缩放宽
|
||||
* @param unknown $t_h 缩放高
|
||||
* @param string $isCrop 是否裁剪
|
||||
* @param number $c_w 裁剪宽
|
||||
* @param number $c_h 裁剪高
|
||||
* @param number $c_x 裁剪坐标 x
|
||||
* @param number $c_y 裁剪坐标 y
|
||||
*/
|
||||
|
||||
private function _resizeGif($t_w, $t_h, $isCrop = false, $c_w = 0, $c_h = 0, $c_x = 0, $c_y = 0)
|
||||
{
|
||||
$dest = new \Imagick();
|
||||
|
||||
$color_transparent = new \ImagickPixel("transparent"); //透明色
|
||||
|
||||
foreach ($this->image as $img) {
|
||||
$page = $img->getImagePage();
|
||||
|
||||
$tmp = new Imagick();
|
||||
|
||||
$tmp->newImage($page['width'], $page['height'], $color_transparent, 'gif');
|
||||
|
||||
$tmp->compositeImage($img, \Imagick::COMPOSITE_OVER, $page['x'], $page['y']);
|
||||
|
||||
$tmp->thumbnailImage($t_w, $t_h, true);
|
||||
|
||||
if ($isCrop) {
|
||||
$tmp->cropImage($c_w, $c_h, $c_x, $c_y);
|
||||
|
||||
}
|
||||
|
||||
$dest->addImage($tmp);
|
||||
|
||||
$dest->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
|
||||
|
||||
$dest->setImageDelay($img->getImageDelay());
|
||||
|
||||
$dest->setImageDispose($img->getImageDispose());
|
||||
|
||||
}
|
||||
|
||||
$this->image->destroy();
|
||||
|
||||
$this->image = $dest;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 更改图像大小
|
||||
* $fit: 适应大小方式
|
||||
* 'force': 把图片强制变形成 $width X $height 大小
|
||||
* 'scale': 按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小 不完全等于 $width X $height
|
||||
* 'scale_fill': 按比例在安全框 $width X $height 内缩放图片,安全框内没有像素的地方填充色,
|
||||
* 使用此参数时可设置背景填充色 $bg_color = array(255,255,255)(红,绿,蓝, 透明度)
|
||||
* 透明度(0不透明-127完全透明)) 其它: 智能模能 缩放图像并载取图像的中间部分 $width X $height 像素大小
|
||||
* $fit = 'force','scale','scale_fill' 时: 输出完整图像
|
||||
* $fit = 图像方位值 时, 输出指定位置部分图像 字母与图像的对应关系如下:
|
||||
* north_west north north_east
|
||||
* west center east
|
||||
* south_west south south_east
|
||||
*/
|
||||
|
||||
public function resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255, 255, 255, 0))
|
||||
{
|
||||
switch ($fit) {
|
||||
case 'force' :
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$image = $this->image;
|
||||
|
||||
$canvas = new \Imagick();
|
||||
|
||||
$images = $image->coalesceImages();
|
||||
|
||||
foreach ($images as $frame) {
|
||||
$img = new \Imagick();
|
||||
|
||||
$img->readImageBlob($frame);
|
||||
|
||||
$img->thumbnailImage($width, $height, false);
|
||||
|
||||
$canvas->addImage($img);
|
||||
|
||||
$canvas->setImageDelay($img->getImageDelay());
|
||||
|
||||
}
|
||||
|
||||
$image->destroy();
|
||||
|
||||
$this->image = $canvas;
|
||||
|
||||
} else {
|
||||
$this->image->thumbnailImage($width, $height, false);
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'scale' :
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$image = $this->image;
|
||||
|
||||
$images = $image->coalesceImages();
|
||||
|
||||
$canvas = new \Imagick();
|
||||
|
||||
foreach ($images as $frame) {
|
||||
$img = new \Imagick();
|
||||
|
||||
$img->readImageBlob($frame);
|
||||
|
||||
$img->thumbnailImage($width, $height, true);
|
||||
|
||||
$canvas->addImage($img);
|
||||
|
||||
$canvas->setImageDelay($img->getImageDelay());
|
||||
|
||||
}
|
||||
|
||||
$image->destroy();
|
||||
|
||||
$this->image = $canvas;
|
||||
|
||||
} else {
|
||||
$this->image->thumbnailImage($width, $height, true);
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'scale_fill' :
|
||||
|
||||
$size = $this->image->getImagePage();
|
||||
|
||||
$src_width = $size ['width'];
|
||||
|
||||
$src_height = $size ['height'];
|
||||
|
||||
$x = 0;
|
||||
|
||||
$y = 0;
|
||||
|
||||
$dst_width = $width;
|
||||
|
||||
$dst_height = $height;
|
||||
|
||||
if ($src_width * $height > $src_height * $width) {
|
||||
$dst_height = intval($width * $src_height / $src_width);
|
||||
|
||||
$y = intval(($height - $dst_height) / 2);
|
||||
|
||||
} else {
|
||||
$dst_width = intval($height * $src_width / $src_height);
|
||||
|
||||
$x = intval(($width - $dst_width) / 2);
|
||||
|
||||
}
|
||||
|
||||
$image = $this->image;
|
||||
|
||||
$canvas = new \Imagick();
|
||||
|
||||
$color = 'rgba(' . $fill_color [0] . ',' . $fill_color [1] . ',' . $fill_color [2] . ',' . $fill_color [3] . ')';
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$images = $image->coalesceImages();
|
||||
|
||||
foreach ($images as $frame) {
|
||||
$frame->thumbnailImage($width, $height, true);
|
||||
|
||||
$draw = new \ImagickDraw();
|
||||
|
||||
$draw->composite($frame->getImageCompose(), $x, $y, $dst_width, $dst_height, $frame);
|
||||
|
||||
$img = new \Imagick();
|
||||
|
||||
$img->newImage($width, $height, $color, 'gif');
|
||||
|
||||
$img->drawImage($draw);
|
||||
|
||||
$canvas->addImage($img);
|
||||
|
||||
$canvas->setImageDelay($img->getImageDelay());
|
||||
|
||||
$canvas->setImagePage($width, $height, 0, 0);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
$image->thumbnailImage($width, $height, true);
|
||||
|
||||
$draw = new \ImagickDraw();
|
||||
|
||||
$draw->composite($image->getImageCompose(), $x, $y, $dst_width, $dst_height, $image);
|
||||
|
||||
$canvas->newImage($width, $height, $color, $this->get_type());
|
||||
|
||||
$canvas->drawImage($draw);
|
||||
|
||||
$canvas->setImagePage($width, $height, 0, 0);
|
||||
|
||||
}
|
||||
|
||||
$image->destroy();
|
||||
|
||||
$this->image = $canvas;
|
||||
|
||||
break;
|
||||
|
||||
default :
|
||||
|
||||
$size = $this->image->getImagePage();
|
||||
|
||||
$src_width = $size ['width'];
|
||||
|
||||
$src_height = $size ['height'];
|
||||
|
||||
$crop_x = 0;
|
||||
|
||||
$crop_y = 0;
|
||||
|
||||
$crop_w = $src_width;
|
||||
|
||||
$crop_h = $src_height;
|
||||
|
||||
if ($src_width * $height > $src_height * $width) {
|
||||
$crop_w = intval($src_height * $width / $height);
|
||||
|
||||
} else {
|
||||
$crop_h = intval($src_width * $height / $width);
|
||||
|
||||
}
|
||||
|
||||
switch ($fit) {
|
||||
case 'north_west' :
|
||||
|
||||
$crop_x = 0;
|
||||
|
||||
$crop_y = 0;
|
||||
|
||||
break;
|
||||
|
||||
case 'north' :
|
||||
|
||||
$crop_x = intval(($src_width - $crop_w) / 2);
|
||||
|
||||
$crop_y = 0;
|
||||
|
||||
break;
|
||||
|
||||
case 'north_east' :
|
||||
|
||||
$crop_x = $src_width - $crop_w;
|
||||
|
||||
$crop_y = 0;
|
||||
|
||||
break;
|
||||
|
||||
case 'west' :
|
||||
|
||||
$crop_x = 0;
|
||||
|
||||
$crop_y = intval(($src_height - $crop_h) / 2);
|
||||
|
||||
break;
|
||||
|
||||
case 'center' :
|
||||
|
||||
$crop_x = intval(($src_width - $crop_w) / 2);
|
||||
|
||||
$crop_y = intval(($src_height - $crop_h) / 2);
|
||||
|
||||
break;
|
||||
|
||||
case 'east' :
|
||||
|
||||
$crop_x = $src_width - $crop_w;
|
||||
|
||||
$crop_y = intval(($src_height - $crop_h) / 2);
|
||||
|
||||
break;
|
||||
|
||||
case 'south_west' :
|
||||
|
||||
$crop_x = 0;
|
||||
|
||||
$crop_y = $src_height - $crop_h;
|
||||
|
||||
break;
|
||||
|
||||
case 'south' :
|
||||
|
||||
$crop_x = intval(($src_width - $crop_w) / 2);
|
||||
|
||||
$crop_y = $src_height - $crop_h;
|
||||
|
||||
break;
|
||||
|
||||
case 'south_east' :
|
||||
|
||||
$crop_x = $src_width - $crop_w;
|
||||
|
||||
$crop_y = $src_height - $crop_h;
|
||||
|
||||
break;
|
||||
|
||||
default :
|
||||
|
||||
$crop_x = intval(($src_width - $crop_w) / 2);
|
||||
|
||||
$crop_y = intval(($src_height - $crop_h) / 2);
|
||||
|
||||
}
|
||||
|
||||
$image = $this->image;
|
||||
|
||||
$canvas = new \Imagick();
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$images = $image->coalesceImages();
|
||||
|
||||
foreach ($images as $frame) {
|
||||
$img = new \Imagick();
|
||||
|
||||
$img->readImageBlob($frame);
|
||||
|
||||
$img->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
|
||||
|
||||
$img->thumbnailImage($width, $height, true);
|
||||
|
||||
$canvas->addImage($img);
|
||||
|
||||
$canvas->setImageDelay($img->getImageDelay());
|
||||
|
||||
$canvas->setImagePage($width, $height, 0, 0);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
$image->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
|
||||
|
||||
$image->thumbnailImage($width, $height, true);
|
||||
|
||||
$canvas->addImage($image);
|
||||
|
||||
$canvas->setImagePage($width, $height, 0, 0);
|
||||
|
||||
}
|
||||
|
||||
$image->destroy();
|
||||
|
||||
$this->image = $canvas;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 添加水印图片
|
||||
public function add_watermark($path, $x = 0, $y = 0)
|
||||
{
|
||||
$watermark = new \Imagick($path);
|
||||
|
||||
$draw = new \ImagickDraw();
|
||||
|
||||
$draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getImageHeight(), $watermark);
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
$image = $this->image;
|
||||
|
||||
$canvas = new \Imagick();
|
||||
|
||||
$images = $image->coalesceImages();
|
||||
|
||||
foreach ($image as $frame) {
|
||||
$img = new \Imagick();
|
||||
|
||||
$img->readImageBlob($frame);
|
||||
|
||||
$img->drawImage($draw);
|
||||
|
||||
$canvas->addImage($img);
|
||||
|
||||
$canvas->setImageDelay($img->getImageDelay());
|
||||
|
||||
}
|
||||
|
||||
$image->destroy();
|
||||
|
||||
$this->image = $canvas;
|
||||
|
||||
} else {
|
||||
$this->image->drawImage($draw);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 添加水印文字
|
||||
public function add_text($text, $x = 0, $y = 0, $angle = 0, $style = array())
|
||||
{
|
||||
$draw = new \ImagickDraw();
|
||||
|
||||
if (isset ($style ['font']))
|
||||
|
||||
$draw->setFont($style ['font']);
|
||||
|
||||
if (isset ($style ['font_size']))
|
||||
|
||||
$draw->setFontSize($style ['font_size']);
|
||||
|
||||
if (isset ($style ['fill_color']))
|
||||
|
||||
$draw->setFillColor($style ['fill_color']);
|
||||
|
||||
$draw->setTextAlignment( 2 );
|
||||
if (isset ($style ['under_color']))
|
||||
|
||||
$draw->setTextUnderColor($style ['under_color']);
|
||||
|
||||
if ($this->type == 'gif') {
|
||||
foreach ($this->image as $frame) {
|
||||
$frame->annotateImage($draw, $x, $y, $angle, $text);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
$this->image->annotateImage($draw, $x, $y, $angle, $text);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 保存到指定路径
|
||||
public function save_to($path, $compress = 60)
|
||||
{
|
||||
//压缩图片质量
|
||||
// $this->image->setImageFormat('JPEG');
|
||||
|
||||
$this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
|
||||
|
||||
// $compress = $this->image->getImageCompressionQuality() * 0.60;
|
||||
//
|
||||
// if ($compress == 0) {
|
||||
// $compress = 60;
|
||||
// }
|
||||
$this->image->setImageCompressionQuality($compress);
|
||||
$this->image->stripImage();
|
||||
if ($this->type == 'gif') {
|
||||
$this->image->writeImages($path, true);
|
||||
} else {
|
||||
$this->image->writeImage($path);
|
||||
}
|
||||
}
|
||||
|
||||
// 输出图像
|
||||
public function output($header = true)
|
||||
{
|
||||
if ($header)
|
||||
|
||||
header('Content-type: ' . $this->type);
|
||||
|
||||
echo $this->image->getImagesBlob();
|
||||
|
||||
}
|
||||
|
||||
public function get_width()
|
||||
{
|
||||
$size = $this->image->getImagePage();
|
||||
|
||||
return $size ['width'];
|
||||
|
||||
}
|
||||
|
||||
public function get_height()
|
||||
{
|
||||
$size = $this->image->getImagePage();
|
||||
|
||||
return $size ['height'];
|
||||
|
||||
}
|
||||
|
||||
// 设置图像类型, 默认与源类型一致
|
||||
|
||||
public function set_type($type = 'png')
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
$this->image->setImageFormat($type);
|
||||
|
||||
}
|
||||
|
||||
// 获取源图像类型
|
||||
|
||||
public function get_type()
|
||||
{
|
||||
return $this->type;
|
||||
|
||||
}
|
||||
|
||||
public function get_file_size()
|
||||
{
|
||||
if ($this->image) {
|
||||
return 0;//$this->image->getImageLength(); getImageLength not find
|
||||
|
||||
} else {
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function get_file_type()
|
||||
{
|
||||
if ($this->image) {
|
||||
return $this->image->getimagemimetype();
|
||||
|
||||
} else {
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function get_sha1()
|
||||
{
|
||||
if ($this->image) {
|
||||
return sha1($this->image->__tostring());
|
||||
|
||||
} else {
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 当前对象是否为图片
|
||||
|
||||
public function is_image()
|
||||
{
|
||||
if ($this->image)
|
||||
|
||||
return true;
|
||||
|
||||
else
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
* 添加一个边框 $width: 左右边框宽度 $height: 上下边框宽度 $color: 颜色: RGB 颜色 'rgb(255,0,0)' 或 16进制颜色 '#FF0000' 或颜色单词 'white'/'red'...
|
||||
|
||||
*/
|
||||
|
||||
public function border($width, $height, $color = 'rgb(220, 220, 220)')
|
||||
{
|
||||
$color = new \ImagickPixel();
|
||||
|
||||
$color->setColor($color);
|
||||
|
||||
$this->image->borderImage($color, $width, $height);
|
||||
|
||||
}
|
||||
|
||||
public function blur($radius, $sigma)
|
||||
{
|
||||
$this->image->blurImage($radius, $sigma);
|
||||
|
||||
} // 模糊
|
||||
|
||||
public function gaussian_blur($radius, $sigma)
|
||||
{
|
||||
$this->image->gaussianBlurImage($radius, $sigma);
|
||||
|
||||
} // 高斯模糊
|
||||
|
||||
public function motion_blur($radius, $sigma, $angle)
|
||||
{
|
||||
$this->image->motionBlurImage($radius, $sigma, $angle);
|
||||
|
||||
} // 运动模糊
|
||||
|
||||
public function radial_blur($radius)
|
||||
{
|
||||
$this->image->radialBlurImage($radius);
|
||||
|
||||
} // 径向模糊
|
||||
|
||||
public function add_noise($type = null)
|
||||
{
|
||||
$this->image->addNoiseImage($type == null ? \Imagick::NOISE_IMPULSE : $type);
|
||||
|
||||
} // 添加噪点
|
||||
|
||||
public function level($black_point, $gamma, $white_point)
|
||||
{
|
||||
$this->image->levelImage($black_point, $gamma, $white_point);
|
||||
|
||||
} // 调整色阶
|
||||
|
||||
public function modulate($brightness, $saturation, $hue)
|
||||
{
|
||||
$this->image->modulateImage($brightness, $saturation, $hue);
|
||||
|
||||
} // 调整亮度、饱和度、色调
|
||||
|
||||
public function charcoal($radius, $sigma)
|
||||
{
|
||||
$this->image->charcoalImage($radius, $sigma);
|
||||
|
||||
} // 素描
|
||||
|
||||
public function oil_paint($radius)
|
||||
{
|
||||
$this->image->oilPaintImage($radius);
|
||||
|
||||
} // 油画效果
|
||||
|
||||
public function flop()
|
||||
{
|
||||
$this->image->flopImage();
|
||||
|
||||
} // 水平翻转
|
||||
|
||||
public function flip()
|
||||
{
|
||||
$this->image->flipImage();
|
||||
|
||||
} // 垂直翻转
|
||||
|
||||
|
||||
}
|
||||
88
app/model/map/QqMap.php
Executable file
88
app/model/map/QqMap.php
Executable file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace app\model\map;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\web\Config as ConfigModel;
|
||||
use extend\api\HttpClient;
|
||||
|
||||
class QqMap extends BaseModel
|
||||
{
|
||||
protected $key = '';
|
||||
public function __construct($param = [])
|
||||
{
|
||||
if(!empty($param['key'])){
|
||||
$this->key = $param['key'];
|
||||
}else{
|
||||
$config_model = new ConfigModel();
|
||||
$mp_config = $config_model->getMapConfig();
|
||||
$this->key = $mp_config['data']['value']['tencent_map_key'];
|
||||
}
|
||||
}
|
||||
|
||||
protected function httpGet($url, $query_data)
|
||||
{
|
||||
$httpClient = new HttpClient();
|
||||
$res_json = $httpClient->get($url, $query_data);
|
||||
return json_decode($res_json, true);
|
||||
}
|
||||
|
||||
protected function httpPost($url, $post_data)
|
||||
{
|
||||
$httpClient = new HttpClient();
|
||||
$res_json = $httpClient->post($url, $post_data);
|
||||
return json_decode($res_json, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过地址字符串获取详情
|
||||
* @param $address
|
||||
* @return bool|string
|
||||
*/
|
||||
public function addressToDetail($param)
|
||||
{
|
||||
$url = 'https://apis.map.qq.com/ws/geocoder/v1/';
|
||||
$query_data = [
|
||||
'key' => $this->key,
|
||||
'address' => $param['address'],
|
||||
];
|
||||
return $this->httpGet($url, $query_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过ip获取详情
|
||||
* @param $ip
|
||||
* @return bool|string
|
||||
*/
|
||||
public function ipToDetail($param)
|
||||
{
|
||||
$url = 'https://apis.map.qq.com/ws/location/v1/ip';
|
||||
$query_data = [
|
||||
'key' => $this->key,
|
||||
];
|
||||
if($param['ip']){
|
||||
$query_data['ip'] = $param['ip'];
|
||||
}
|
||||
return $this->httpGet($url, $query_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过经纬度获取详情
|
||||
* @param $param
|
||||
* @return bool|string
|
||||
*/
|
||||
public function locationToDetail($param)
|
||||
{
|
||||
$url = 'https://apis.map.qq.com/ws/geocoder/v1/';
|
||||
$query_data = [
|
||||
'key' => $this->key,
|
||||
'location' => $param['location'],//$latitude.','.$longitude
|
||||
'get_poi' => 0,//是否返回周边POI列表:1.返回;0不返回(默认)
|
||||
];
|
||||
return $this->httpGet($url, $query_data);
|
||||
}
|
||||
|
||||
public function getKey(){
|
||||
return $this->key;
|
||||
}
|
||||
}
|
||||
205
app/model/member/Config.php
Executable file
205
app/model/member/Config.php
Executable file
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use app\model\system\Document;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 会员设置
|
||||
*/
|
||||
class Config extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 注册协议
|
||||
* @param $title
|
||||
* @param $content
|
||||
* @param unknown $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setRegisterDocument($title, $content, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
$res = $document->setDocument($title, $content, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'document_key', '=', 'REGISTER_AGREEMENT' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询注册协议
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getRegisterDocument($site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
$info = $document->getDocument([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'document_key', '=', 'REGISTER_AGREEMENT' ] ]);
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 隐私协议
|
||||
* @param $title
|
||||
* @param $content
|
||||
* @param unknown $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setPrivacyConfig($title, $content, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
$res = $document->setDocument($title, $content, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'document_key', '=', 'PRIVACY_AGREEMENT' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询隐私协议
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getPrivacyDocument($site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
$info = $document->getDocument([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'document_key', '=', 'PRIVACY_AGREEMENT' ] ]);
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册规则
|
||||
* array $data
|
||||
*/
|
||||
public function setRegisterConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '注册规则', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'REGISTER_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询注册规则
|
||||
*/
|
||||
public function getRegisterConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'REGISTER_CONFIG' ] ]);
|
||||
$res['data']['value'] = array_merge([
|
||||
'login' => 'username,mobile',//登录类型 账号 手机号
|
||||
'register' => 'username,mobile',//注册类型 账号 手机号
|
||||
'third_party' => 1,//允许三方平台自动注册
|
||||
'bind_mobile' => 0,//是否强制绑定手机
|
||||
'pwd_len' => 6,//密码最小长度
|
||||
'pwd_complexity' => '',//密码复杂程度
|
||||
'agreement_show' => 1,//是否显示政策协议
|
||||
'wap_bg' => '',//手机端背景图
|
||||
'wap_desc' => '',//描述
|
||||
], $res['data']['value']);
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注销协议
|
||||
* @param $title
|
||||
* @param $content
|
||||
* @param unknown $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setCancelDocument($title, $content, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
$res = $document->setDocument($title, $content, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'document_key', '=', 'CANCEL_AGREEMENT' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询注销协议
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getCancelDocument($site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
$info = $document->getDocument([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'document_key', '=', 'CANCEL_AGREEMENT' ] ]);
|
||||
return $info;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注销规则
|
||||
* array $data
|
||||
*/
|
||||
public function setCancelConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '注销规则', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'CANCEL_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询注销规则
|
||||
*/
|
||||
public function getCancelConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'CANCEL_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
//默认值设置
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'is_enable' => 0, //注销开关
|
||||
'is_audit' => 1, //审核开关
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询会员配置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'MEMBER_LEVEL_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
//默认值设置
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'is_update' => 1,
|
||||
];
|
||||
} else {
|
||||
$value = $res[ 'data' ][ 'value' ];
|
||||
$value[ 'is_update' ] = $value[ 'is_update' ] ?? 1;
|
||||
$res[ 'data' ][ 'value' ] = $value;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 积分任务
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setMemberConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '会员等级更新配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'MEMBER_LEVEL_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
377
app/model/member/Login.php
Executable file
377
app/model/member/Login.php
Executable file
@@ -0,0 +1,377 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use addon\wechat\model\Message as WechatMessage;
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Sms;
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class Login extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function login($data)
|
||||
{
|
||||
|
||||
$info = model("member")->getInfo([
|
||||
['username|mobile|email', '=', $data['username']],
|
||||
['password', '=', data_md5($data['password'])],
|
||||
['site_id', '=', $data['site_id']],
|
||||
['is_delete', '=', 0]
|
||||
], 'member_id,username, nickname, mobile, email, status,last_login_time,can_receive_registergift');
|
||||
if (empty($info)) {
|
||||
return $this->error('', 'USERNAME_OR_PASSWORD_ERROR');
|
||||
} elseif ($info['status'] == 0) {
|
||||
return $this->error('', 'MEMBER_IS_LOCKED');
|
||||
} else {
|
||||
if ($info['can_receive_registergift'] == 1) {
|
||||
event("MemberReceiveRegisterGift", ['member_id' => $info['member_id'], 'site_id' => $data['site_id']]);
|
||||
}
|
||||
//更新登录时间
|
||||
model("member")->update([
|
||||
'login_time' => time(),
|
||||
'last_login_time' => time(),
|
||||
'can_receive_registergift' => 0,
|
||||
'login_ip' => request()->ip(),
|
||||
'login_type' => $data['app_type'] ?? '',
|
||||
'login_type_name' => $data['app_type_name'] ?? '',
|
||||
], [['member_id', '=', $info['member_id']]]);
|
||||
|
||||
//执行登录奖励
|
||||
event("MemberLogin", ['member_id' => $info['member_id'], 'site_id' => $data['site_id']], true);
|
||||
|
||||
//用户第三方信息刷新
|
||||
$this->refreshAuth($info['member_id'], $data);
|
||||
return $this->success($info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 第三方登录
|
||||
* @param array $data 必然传输auth_tag, auth_openid
|
||||
*/
|
||||
public function authLogin($data)
|
||||
{
|
||||
//微信未使用完整服务返回数据不能做登录使用
|
||||
//未使用完整服务和使用了完整服务返回的数据是不一样的
|
||||
if(isset($data['nickName']) && isset($data['avatarUrl']) && $data['nickName'] == '微信用户' && $data['avatarUrl'] == 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6WH3T48eljAMnoGDv722DOL7nO15FgEz64psqp2xiaZJCP2v71dOqS03hKjytzRrh3ZHg09mKNtXg/132'){
|
||||
return $this->error(null, '微信授权使用完整服务');
|
||||
}
|
||||
|
||||
$info = [];
|
||||
$auth_tag = '';
|
||||
foreach ($data as $key => $value) {
|
||||
if (in_array($key, ['wx_unionid', 'wx_openid', 'weapp_openid', 'qq_openid', 'ali_openid', 'baidu_openid', 'toutiao_openid'])) {
|
||||
$auth_tag = $key;
|
||||
if (empty($value)) return $this->error('', 'PARAMETER_ERROR');
|
||||
$info = model("member")->getFirstData(
|
||||
[
|
||||
[$key, '=', $value],
|
||||
['site_id', '=', $data['site_id']],
|
||||
['is_delete', '=', 0]
|
||||
], 'member_id,username, nickname, mobile, email, status, last_login_time, can_receive_registergift'
|
||||
);
|
||||
if (!empty($info)) break;
|
||||
}
|
||||
}
|
||||
if (empty($auth_tag)) return $this->error('', 'PARAMETER_ERROR');
|
||||
|
||||
if (empty($info)) {
|
||||
// 会员不存在 第三方自动注册开启 未开启绑定手机 则进行自动注册
|
||||
$config = new Config();
|
||||
$config_info = $config->getRegisterConfig($data['site_id'], 'shop');
|
||||
if ($config_info['data']['value']['third_party'] && !$config_info['data']['value']['bind_mobile']) {
|
||||
$register = new Register();
|
||||
$register_res = $register->authRegister($data);
|
||||
if ($register_res['code'] == 0) {
|
||||
$info = model("member")->getInfo([['member_id', '=', $register_res['data']]], 'member_id,username, nickname, mobile, email, status, last_login_time,can_receive_registergift');
|
||||
$info['is_register'] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($info)) {
|
||||
return $this->error('MEMBER_NOT_EXIST', 'MEMBER_NOT_EXIST');
|
||||
} elseif ($info['status'] == 0) {
|
||||
return $this->error('', 'MEMBER_IS_LOCKED');
|
||||
} else {
|
||||
if ($info['can_receive_registergift'] == 1) {
|
||||
event("MemberReceiveRegisterGift", ['member_id' => $info['member_id'], 'site_id' => $data['site_id']]);
|
||||
}
|
||||
//更新登录时间
|
||||
model("member")->update([
|
||||
'login_time' => time(),
|
||||
'last_login_time' => time(),
|
||||
'can_receive_registergift' => 0,
|
||||
'login_ip' => request()->ip(),
|
||||
'login_type' => $data['app_type'] ?? '',
|
||||
'login_type_name' => $data['app_type_name'] ?? '',
|
||||
], [['member_id', '=', $info['member_id']]]);
|
||||
|
||||
//执行登录奖励
|
||||
event("MemberLogin", ['member_id' => $info['member_id'], 'site_id' => $data['site_id']], true);
|
||||
|
||||
//用户第三方信息刷新
|
||||
if (!isset($info['is_register'])) $this->refreshAuth($info['member_id'], $data);
|
||||
|
||||
return $this->success($info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权登录仅登录
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function authOnlyLogin($data)
|
||||
{
|
||||
$info = [];
|
||||
$auth_tag = '';
|
||||
foreach ($data as $key => $value) {
|
||||
if (in_array($key, ['wx_unionid', 'wx_openid', 'weapp_openid', 'qq_openid', 'ali_openid', 'baidu_openid', 'toutiao_openid'])) {
|
||||
$auth_tag = $key;
|
||||
if (empty($value)) return $this->error('', 'PARAMETER_ERROR');
|
||||
$info = model("member")->getInfo(
|
||||
[
|
||||
[$key, '=', $value],
|
||||
['site_id', '=', $data['site_id']],
|
||||
['is_delete', '=', 0]
|
||||
], 'member_id,username, nickname, mobile, email, status, last_login_time, can_receive_registergift'
|
||||
);
|
||||
if (!empty($info)) break;
|
||||
}
|
||||
}
|
||||
if (empty($auth_tag)) return $this->error('', 'PARAMETER_ERROR');
|
||||
|
||||
if (empty($info)) {
|
||||
// 前端根据data值判断业务处理
|
||||
return $this->error('MEMBER_NOT_EXIST', 'MEMBER_NOT_EXIST');
|
||||
} elseif ($info['status'] == 0) {
|
||||
return $this->error('', 'MEMBER_IS_LOCKED');
|
||||
} else {
|
||||
//更新登录时间
|
||||
model("member")->update([
|
||||
'login_time' => time(),
|
||||
'last_login_time' => time(),
|
||||
'can_receive_registergift' => 0,
|
||||
'login_ip' => request()->ip(),
|
||||
'login_type' => $data['app_type'] ?? '',
|
||||
'login_type_name' => $data['app_type_name'] ?? '',
|
||||
], [['member_id', '=', $info['member_id']]]);
|
||||
|
||||
//执行登录奖励
|
||||
event("MemberLogin", ['member_id' => $info['member_id'], 'site_id' => $data['site_id']], true);
|
||||
//用户第三方信息刷新
|
||||
$this->refreshAuth($info['member_id'], $data);
|
||||
return $this->success($info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新第三方信息
|
||||
* @param $member_id
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
private function refreshAuth($member_id, $data)
|
||||
{
|
||||
Member::modifyLastVisitTime($member_id);
|
||||
$data = [
|
||||
'qq_openid' => $data['qq_openid'] ?? '',
|
||||
'wx_openid' => $data['wx_openid'] ?? '',
|
||||
'weapp_openid' => $data['weapp_openid'] ?? '',
|
||||
'wx_unionid' => $data['wx_unionid'] ?? '',
|
||||
'ali_openid' => $data['ali_openid'] ?? '',
|
||||
'baidu_openid' => $data['baidu_openid'] ?? '',
|
||||
'toutiao_openid' => $data['toutiao_openid'] ?? '',
|
||||
'site_id' => $data['site_id']
|
||||
];
|
||||
if (!empty($data['qq_openid'])) {
|
||||
model("member")->update(['qq_openid' => $data['qq_openid']], [['member_id', '=', $member_id], ['site_id', '=', $data['site_id']]]);
|
||||
}
|
||||
if (!empty($data['wx_openid'])) {
|
||||
model("member")->update(['wx_openid' => $data['wx_openid']], [['member_id', '=', $member_id], ['site_id', '=', $data['site_id']]]);
|
||||
}
|
||||
if (!empty($data['weapp_openid'])) {
|
||||
model("member")->update(['weapp_openid' => $data['weapp_openid']], [['member_id', '=', $member_id], ['site_id', '=', $data['site_id']]]);
|
||||
}
|
||||
if (!empty($data['wx_unionid'])) {
|
||||
model("member")->update(['wx_unionid' => $data['wx_unionid']], [['member_id', '=', $member_id], ['site_id', '=', $data['site_id']]]);
|
||||
}
|
||||
if (!empty($data['ali_openid'])) {
|
||||
model("member")->update(['ali_openid' => $data['ali_openid']], [['member_id', '=', $member_id], ['site_id', '=', $data['site_id']]]);
|
||||
}
|
||||
if (!empty($data['baidu_openid'])) {
|
||||
model("member")->update(['baidu_openid' => $data['baidu_openid']], [['member_id', '=', $member_id], ['site_id', '=', $data['site_id']]]);
|
||||
}
|
||||
if (!empty($data['toutiao_openid'])) {
|
||||
model("member")->update(['toutiao_openid' => $data['toutiao_openid']], [['member_id', '=', $member_id], ['site_id', '=', $data['site_id']]]);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测openid是否存在
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function openidIsExits(array $data)
|
||||
{
|
||||
if (isset($data['wx_unionid']) && !empty($data['wx_unionid'])) {
|
||||
$count = model("member")->getCount([['wx_unionid', '=', $data['wx_unionid']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($count) return $this->success($count);
|
||||
}
|
||||
if (isset($data['wx_openid']) && !empty($data['wx_openid'])) {
|
||||
$count = model("member")->getCount([['wx_openid', '=', $data['wx_openid']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($count) return $this->success($count);
|
||||
}
|
||||
if (isset($data['weapp_openid']) && !empty($data['weapp_openid'])) {
|
||||
$count = model("member")->getCount([['weapp_openid', '=', $data['weapp_openid']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($count) return $this->success($count);
|
||||
}
|
||||
if (isset($data['qq_openid']) && !empty($data['qq_openid'])) {
|
||||
$count = model("member")->getCount([['qq_openid', '=', $data['qq_openid']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($count) return $this->success($count);
|
||||
}
|
||||
if (isset($data['ali_openid']) && !empty($data['ali_openid'])) {
|
||||
$count = model("member")->getCount([['ali_openid', '=', $data['ali_openid']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($count) return $this->success($count);
|
||||
}
|
||||
if (isset($data['baidu_openid']) && !empty($data['baidu_openid'])) {
|
||||
$count = model("member")->getCount([['baidu_openid', '=', $data['baidu_openid']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($count) return $this->success($count);
|
||||
}
|
||||
if (isset($data['toutiao_openid']) && !empty($data['toutiao_openid'])) {
|
||||
$count = model("member")->getCount([['toutiao_openid', '=', $data['toutiao_openid']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($count) return $this->success($count);
|
||||
}
|
||||
return $this->success(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function mobileLogin($data)
|
||||
{
|
||||
$info = model("member")->getInfo([
|
||||
['mobile', '=', $data['mobile']],
|
||||
['site_id', '=', $data['site_id']],
|
||||
['is_delete', '=', 0]
|
||||
], 'member_id,username, nickname, mobile, email, status,last_login_time, can_receive_registergift');
|
||||
if (empty($info)) {
|
||||
return $this->error('MEMBER_NOT_EXIST', 'MEMBER_NOT_EXIST');
|
||||
} elseif ($info['status'] == 0) {
|
||||
return $this->error('', 'MEMBER_IS_LOCKED');
|
||||
} else {
|
||||
if ($info['can_receive_registergift'] == 1) {
|
||||
event("MemberReceiveRegisterGift", ['member_id' => $info['member_id'], 'site_id' => $data['site_id']]);
|
||||
}
|
||||
//更新登录时间
|
||||
model("member")->update([
|
||||
'login_time' => time(),
|
||||
'last_login_time' => time(),
|
||||
'can_receive_registergift' => 0,
|
||||
'login_ip' => request()->ip(),
|
||||
'login_type' => $data['app_type'] ?? '',
|
||||
'login_type_name' => $data['app_type_name'] ?? '',
|
||||
], [['member_id', '=', $info['member_id']]]);
|
||||
|
||||
event("MemberLogin", ['member_id' => $info['member_id'], 'site_id' => $data['site_id']], true);
|
||||
|
||||
//用户第三方信息刷新
|
||||
$this->refreshAuth($info['member_id'], $data);
|
||||
return $this->success($info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录动态码
|
||||
* @param $data
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function loginCode($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$var_parse = array (
|
||||
"code" => $data["code"],
|
||||
);
|
||||
$data["sms_account"] = $data["mobile"] ?? '';//手机号
|
||||
$data["var_parse"] = $var_parse;
|
||||
$sms_result = $sms_model->sendMessage($data);
|
||||
if ($sms_result["code"] < 0)
|
||||
return $sms_result;
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录通知
|
||||
* @param $data
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function loginSuccess($data)
|
||||
{
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([["member_id", "=", $data["member_id"]]], "username,mobile,email,reg_time,wx_openid,last_login_type,login_time, nickname");
|
||||
$member_info = $member_info_result["data"];
|
||||
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
$name = $member_info["nickname"] == '' ? $member_info["mobile"] : $member_info["nickname"];
|
||||
$var_parse = array (
|
||||
"name" => replaceSpecialChar($name),//验证码
|
||||
);
|
||||
$data["sms_account"] = $member_info["mobile"] ?? '';//手机号
|
||||
$data["var_parse"] = $var_parse;
|
||||
$sms_result = $sms_model->sendMessage($data);
|
||||
// if($sms_result["code"] < 0)
|
||||
// return $sms_result;
|
||||
|
||||
|
||||
//发送模板消息
|
||||
$wechat_model = new WechatMessage();
|
||||
$data["openid"] = $member_info["wx_openid"];
|
||||
|
||||
// if(!empty($member_info["username"])){
|
||||
// $user_account = $member_info["username"];
|
||||
// }else{
|
||||
// if(!empty($member_info["mobile"])){
|
||||
// $user_account = $member_info["mobile"];
|
||||
// }else{
|
||||
// $user_account = $member_info["email"];
|
||||
// }
|
||||
// }
|
||||
|
||||
$data["template_data"] = [
|
||||
'keyword1' => !empty($member_info["nickname"]) ? $member_info["nickname"] : $member_info["mobile"],
|
||||
'keyword2' => '登录成功',
|
||||
'keyword3' => time_to_date($member_info["login_time"]),
|
||||
];
|
||||
$data["page"] = '';
|
||||
$wechat_model->sendMessage($data);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
1128
app/model/member/Member.php
Executable file
1128
app/model/member/Member.php
Executable file
File diff suppressed because it is too large
Load Diff
612
app/model/member/MemberAccount.php
Executable file
612
app/model/member/MemberAccount.php
Executable file
@@ -0,0 +1,612 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use app\dict\member_account\AccountDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Message;
|
||||
use app\model\message\Sms;
|
||||
use addon\wechat\model\Message as WechatMessage;
|
||||
use app\model\member\Member as MemberModel;
|
||||
use addon\weapp\model\Message as WeappMessage;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 会员账户
|
||||
*/
|
||||
class MemberAccount extends BaseModel
|
||||
{
|
||||
//账户类型
|
||||
private $account_type = [
|
||||
'balance' => '储值余额',
|
||||
'balance_money' => '现金余额',
|
||||
'point' => '积分',
|
||||
'growth' => '成长值'
|
||||
];
|
||||
|
||||
//来源类型
|
||||
public $from_type = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$event_from_type = event('MemberAccountFromType', '');
|
||||
$from_type = [];
|
||||
foreach ($event_from_type as $info) {
|
||||
|
||||
if (isset($info[ 'balance' ])) {
|
||||
$balance = array_keys($info[ 'balance' ]);
|
||||
$from_type[ 'balance' ][ $balance[ 0 ] ] = $info[ 'balance' ][ $balance[ 0 ] ];
|
||||
}
|
||||
|
||||
if (isset($info[ 'point' ])) {
|
||||
$point = array_keys($info[ 'point' ]);
|
||||
$from_type[ 'point' ][ $point[ 0 ] ] = $info[ 'point' ][ $point[ 0 ] ];
|
||||
}
|
||||
|
||||
if (isset($info[ 'growth' ])) {
|
||||
$growth = array_keys($info[ 'growth' ]);
|
||||
$from_type[ 'growth' ][ $growth[ 0 ] ] = $info[ 'growth' ][ $growth[ 0 ] ];
|
||||
}
|
||||
|
||||
if (isset($info[ 'balance_money' ])) {
|
||||
$balance_money = array_keys($info[ 'balance_money' ]);
|
||||
$from_type[ 'balance_money' ][ $balance_money[ 0 ] ] = $info[ 'balance_money' ][ $balance_money[ 0 ] ];
|
||||
}
|
||||
}
|
||||
$from_type['point']['register'] = ['type_name' => '注册', 'type_url' => ''];
|
||||
$from_type['balance']['register'] = ['type_name' => '注册', 'type_url' => ''];
|
||||
$from_type['growth']['register'] = ['type_name' => '注册', 'type_url' => ''];
|
||||
$from_type[ 'balance' ][ 'adjust' ] = [ 'type_name' => '调整', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'adjust' ] = [ 'type_name' => '调整', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance' ][ 'order' ] = [ 'type_name' => '消费', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'order' ] = [ 'type_name' => '消费', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'order' ] = [ 'type_name' => '消费', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'point' ][ 'adjust' ] = [ 'type_name' => '调整', 'type_url' => '' ];
|
||||
$from_type[ 'growth' ][ 'adjust' ] = [ 'type_name' => '调整', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance' ][ 'upgrade' ] = [ 'type_name' => '升级', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'upgrade' ] = [ 'type_name' => '升级', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance' ][ 'membercode' ] = [ 'type_name' => '会员码扣款', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'membercode' ] = [ 'type_name' => '会员码扣款', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'point' ][ 'upgrade' ] = [ 'type_name' => '升级', 'type_url' => '' ];
|
||||
$from_type[ 'growth' ][ 'upgrade' ] = [ 'type_name' => '升级', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance' ][ 'refund' ] = [ 'type_name' => '退还', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'refund' ] = [ 'type_name' => '退还', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'refund' ] = [ 'type_name' => '退还', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'pointexchangerefund' ] = [ 'type_name' => '积分兑换退还', 'type_url' => '' ];
|
||||
|
||||
|
||||
|
||||
$from_type[ 'balance' ][ 'memberlevel' ] = [ 'type_name' => '开卡', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'memberlevel' ] = [ 'type_name' => '开卡', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance_money' ][ 'birthdaygift' ] = [ 'type_name' => '生日有礼', 'type_url' => '' ];
|
||||
$from_type[ 'balance' ][ 'birthdaygift' ] = [ 'type_name' => '生日有礼', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'birthdaygift' ] = [ 'type_name' => '生日有礼', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance_money' ][ 'scenefestival' ] = [ 'type_name' => '节日有礼', 'type_url' => '' ];
|
||||
$from_type[ 'balance' ][ 'scenefestival' ] = [ 'type_name' => '节日有礼', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'scenefestival' ] = [ 'type_name' => '节日有礼', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance_money' ][ 'pinfan' ] = [ 'type_name' => '拼团返利', 'type_url' => '' ];
|
||||
$from_type[ 'balance' ][ 'pinfan' ] = [ 'type_name' => '拼团返利', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'pinfan' ] = [ 'type_name' => '拼团返利', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance_money' ][ 'withdraw' ] = [ 'type_name' => '提现', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'withdraw_fail' ] = [ 'type_name' => '提现失败', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance' ][ 'giftcard' ] = [ 'type_name' => '礼品卡', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'giftcard' ] = [ 'type_name' => '礼品卡', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'giftcard' ] = [ 'type_name' => '礼品卡', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'balance' ][ 'hongbao' ] = [ 'type_name' => '裂变红包', 'type_url' => '' ];
|
||||
$from_type[ 'balance_money' ][ 'hongbao' ] = [ 'type_name' => '裂变红包', 'type_url' => '' ];
|
||||
|
||||
$from_type[ 'point' ][ 'point_expire' ] = [ 'type_name' => '积分到期', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'point_cancel' ] = [ 'type_name' => '积分取消', 'type_url' => '' ];
|
||||
$from_type[ 'point' ][ 'point_set_zero' ] = [ 'type_name' => '积分清零', 'type_url' => '' ];
|
||||
|
||||
$this->from_type = $from_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取账户类型
|
||||
*/
|
||||
public function getAccountType()
|
||||
{
|
||||
return $this->account_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取来源类型
|
||||
*/
|
||||
public function getFromType()
|
||||
{
|
||||
return $this->from_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加会员账户数据
|
||||
* @param $site_id
|
||||
* @param $member_id
|
||||
* @param $account_type
|
||||
* @param $account_data
|
||||
* @param $from_type
|
||||
* @param $relate_tag
|
||||
* @param $remark
|
||||
* @param int $related_id
|
||||
* @return array
|
||||
*/
|
||||
public function addMemberAccount($site_id, $member_id, $account_type, $account_data, $from_type, $relate_tag, $remark, $related_id = 0)
|
||||
{
|
||||
model('member_account')->startTrans();
|
||||
try {
|
||||
$msg = '';
|
||||
if ($account_type == AccountDict::balance) {
|
||||
$msg = '账户余额';
|
||||
} elseif ($account_type == AccountDict::point) {
|
||||
$msg = '账户积分';
|
||||
} elseif ($account_type == AccountDict::growth) {
|
||||
$msg = '账户成长值';
|
||||
}
|
||||
|
||||
//账户检测
|
||||
$member_account = Db::name("member")->where([
|
||||
[ 'member_id', '=', $member_id ],
|
||||
[ 'site_id', '=', $site_id ]
|
||||
])->field($account_type . ', username, mobile, nickname, email')->lock(true)->find();
|
||||
$account_new_data = round((float) $member_account[ $account_type ] + (float) $account_data, 2);
|
||||
if($account_new_data < 0){
|
||||
if(in_array($from_type,['point_cancel', 'point_expire'])){
|
||||
$account_data = -$member_account[ $account_type ];
|
||||
$remark = $this->from_type[AccountDict::point][$from_type]['type_name'].":" . $member_account[ $account_type ];
|
||||
$account_new_data = 0;
|
||||
}else{
|
||||
model('member_account')->rollback();
|
||||
return $this->error('', $msg.'不可为负数');
|
||||
}
|
||||
}
|
||||
if($account_new_data > 99999999){
|
||||
return $this->error('', $msg.'不可超出99999999');
|
||||
}
|
||||
//添加记录
|
||||
$type_info = $this->from_type[ $account_type ][ $from_type ];
|
||||
|
||||
$data = array (
|
||||
'site_id' => $site_id,
|
||||
'member_id' => $member_id,
|
||||
'account_type' => $account_type,
|
||||
'account_data' => $account_data,
|
||||
'from_type' => $from_type,
|
||||
'type_name' => $type_info[ 'type_name' ],
|
||||
'type_tag' => $relate_tag,
|
||||
'create_time' => time(),
|
||||
'username' => $member_account[ 'username' ],
|
||||
'mobile' => $member_account[ 'mobile' ],
|
||||
'email' => $member_account[ 'email' ],
|
||||
'remark' => $remark,
|
||||
'related_id' => $related_id,
|
||||
);
|
||||
|
||||
model('member_account')->add($data);
|
||||
//账户更新
|
||||
model('member')->update([
|
||||
$account_type => $account_new_data
|
||||
], [
|
||||
'member_id' => $member_id
|
||||
]);
|
||||
event('AddMemberAccount', $data);
|
||||
model('member_account')->commit();
|
||||
//发送消息通知(余额变动通知)
|
||||
|
||||
$account_type_arr = [
|
||||
'balance' => 'USER_BALANCE_CHANGE_NOTICE',
|
||||
'balance_money' => 'USER_BALANCE_CHANGE_NOTICE',
|
||||
'point' => 'USER_POINT_CHANGE_NOTICE'
|
||||
];
|
||||
if (array_key_exists($account_type, $account_type_arr)) {
|
||||
$data[ 'keywords' ] = $account_type_arr[$account_type];
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage($data);
|
||||
}
|
||||
return $this->success([ 'member_id' => $member_id, $account_type => sprintf("%.2f", $account_new_data) ]);
|
||||
} catch (\Exception $e) {
|
||||
model('member_account')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单支付处理会员相关账户业务(余额,可提现余额)
|
||||
* @param $order_pay_array
|
||||
* @return array|void
|
||||
*/
|
||||
public function addMemberAccountInOrderPay($order_pay_array)
|
||||
{
|
||||
$pay_info = $order_pay_array['pay_info'];
|
||||
$order_info = $order_pay_array['order_info'];
|
||||
$member_info = $order_pay_array['member_info'];
|
||||
|
||||
//订单余额账户处理
|
||||
if (( $pay_info[ 'balance' ] || $pay_info[ 'balance_money' ] ) && $order_info[ 'order_money' ]) {
|
||||
|
||||
//修改整体业务
|
||||
model('member_account')->startTrans();
|
||||
try{
|
||||
//账户变动
|
||||
$data_member_account = [
|
||||
'balance' => $member_info['balance'] - $pay_info['balance'],
|
||||
'balance_money' => $member_info['balance_money'] - $pay_info['balance_money'],
|
||||
'balance_lock' => $member_info['balance_lock'] - $pay_info['balance'],
|
||||
'balance_money_lock' => $member_info['balance_money_lock'] - $pay_info['balance_money']
|
||||
];
|
||||
model("member")->update($data_member_account, [['member_id', '=', $order_info['member_id']]]);
|
||||
|
||||
//账户记录
|
||||
$account_log = [];
|
||||
if($pay_info[ 'balance' ] > 0)
|
||||
{
|
||||
$account_log[] = [
|
||||
'site_id' => $order_info['site_id'],
|
||||
'member_id' => $member_info['member_id'],
|
||||
'account_type' => 'balance',
|
||||
'account_data' => $pay_info['balance'] * -1,
|
||||
'from_type' => 'order',
|
||||
'type_name' => $this->from_type[ 'balance' ][ 'order' ]['type_name'],
|
||||
'create_time' => time(),
|
||||
'username' => $member_info['username'],
|
||||
'mobile' => $member_info['mobile'],
|
||||
'email' => '',
|
||||
'remark' => '订单消费扣除',
|
||||
'related_id' => $order_info['order_id']
|
||||
];
|
||||
}
|
||||
if($pay_info[ 'balance_money' ] > 0)
|
||||
{
|
||||
$account_log[] = [
|
||||
'site_id' => $order_info['site_id'],
|
||||
'member_id' => $member_info['member_id'],
|
||||
'account_type' => 'balance',
|
||||
'account_data' => $pay_info['balance_money'] * -1,
|
||||
'from_type' => 'order',
|
||||
'type_name' => $this->from_type[ 'balance_money' ][ 'order' ]['type_name'],
|
||||
'create_time' => time(),
|
||||
'username' => $member_info['username'],
|
||||
'mobile' => $member_info['mobile'],
|
||||
'email' => '',
|
||||
'remark' => '订单消费扣除',
|
||||
'related_id' => $order_info['order_id']
|
||||
];
|
||||
}
|
||||
if(!empty($account_log))
|
||||
{
|
||||
model('member_account')->addList($account_log);
|
||||
|
||||
$total_balance = $pay_info[ 'balance' ] + $pay_info[ 'balance_money' ];
|
||||
if ($total_balance > 0) model('order')->update([ 'balance_money' => $total_balance, 'pay_money' => ( $order_info['pay_money'] - $total_balance ) ], [ [ 'order_id', '=', $order_info['order_id'] ] ]);
|
||||
}
|
||||
model('member_account')->commit();
|
||||
//发送消息通知(余额变动通知)
|
||||
if ($pay_info['balance_money'] + $pay_info['balance'] > 0) {
|
||||
$message_data = [
|
||||
'site_id' => $order_info['site_id'],
|
||||
'member_id' => $member_info['member_id'],
|
||||
'account_type' => 'balance',
|
||||
'account_data' => ($pay_info['balance_money'] + $pay_info['balance']) * -1,
|
||||
'from_type' => 'order',
|
||||
'type_name' => $this->from_type[ 'balance' ][ 'order' ]['type_name'],
|
||||
'create_time' => time(),
|
||||
'username' => $member_info['username'],
|
||||
'mobile' => $member_info['mobile'],
|
||||
'email' => '',
|
||||
'remark' => '订单消费扣除',
|
||||
'related_id' => $order_info['order_id'],
|
||||
'keywords' => 'USER_BALANCE_CHANGE_NOTICE',
|
||||
];
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage($message_data);
|
||||
}
|
||||
|
||||
return $this->success();
|
||||
}catch (\Exception $e)
|
||||
{
|
||||
model('member_account')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加会员账户,用于会员注册与添加
|
||||
* @param $member_data
|
||||
* @param $account_data
|
||||
*/
|
||||
public function addMemberAccountInRegister($member_data, $account_data)
|
||||
{
|
||||
$list = [];
|
||||
if($account_data['point'] > 0)
|
||||
{
|
||||
$list[] = array (
|
||||
'site_id' => $member_data['site_id'],
|
||||
'member_id' => $member_data['member_id'],
|
||||
'account_type' => 'point',
|
||||
'account_data' => $account_data['point'],
|
||||
'from_type' => 'register',
|
||||
'type_name' => '注册',
|
||||
'type_tag' => "会员注册奖励积分:".$account_data['point'],
|
||||
'create_time' => time(),
|
||||
'username' => $member_data[ 'username' ],
|
||||
'mobile' => $member_data[ 'mobile' ],
|
||||
'email' => '',
|
||||
'remark' => '会员注册奖励积分',
|
||||
'related_id' => 0,
|
||||
);
|
||||
}
|
||||
|
||||
if($account_data['balance'] > 0)
|
||||
{
|
||||
$list[] = array (
|
||||
'site_id' => $member_data['site_id'],
|
||||
'member_id' => $member_data['member_id'],
|
||||
'account_type' => 'balance',
|
||||
'account_data' => $account_data['balance'],
|
||||
'from_type' => 'register',
|
||||
'type_name' => '注册',
|
||||
'type_tag' => "会员注册奖励余额:".$account_data['balance'],
|
||||
'create_time' => time(),
|
||||
'username' => $member_data[ 'username' ],
|
||||
'mobile' => $member_data[ 'mobile' ],
|
||||
'email' => '',
|
||||
'remark' => '会员注册奖励余额',
|
||||
'related_id' => 0,
|
||||
);
|
||||
}
|
||||
|
||||
if($account_data['growth'] > 0)
|
||||
{
|
||||
$list[] = array (
|
||||
'site_id' => $member_data['site_id'],
|
||||
'member_id' => $member_data['member_id'],
|
||||
'account_type' => 'growth',
|
||||
'account_data' => $account_data['growth'],
|
||||
'from_type' => 'register',
|
||||
'type_name' => '注册',
|
||||
'type_tag' => "会员注册奖励成长值:".$account_data['growth'],
|
||||
'create_time' => time(),
|
||||
'username' => $member_data[ 'username' ],
|
||||
'mobile' => $member_data[ 'mobile' ],
|
||||
'email' => '',
|
||||
'remark' => '会员注册奖励成长值',
|
||||
'related_id' => 0,
|
||||
);
|
||||
}
|
||||
model("member_account")->addList($list);
|
||||
$account = [
|
||||
'point' => $account_data['point'],
|
||||
'balance' => $account_data['balance'],
|
||||
'growth' => $account_data['growth']
|
||||
];
|
||||
model("member")->update($account, [['member_id', '=', $member_data['member_id']]]);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取账户分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function getMemberAccountPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc,id desc', $field = '*', $alias = 'a', $join = [])
|
||||
{
|
||||
$list = model('member_account')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取账户列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function getMemberAccountList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('member_account')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取账户总额
|
||||
* @param array $where
|
||||
* @param string $field
|
||||
* @param string $alias
|
||||
* @param null $join
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberAccountSum($where = [], $field = '', $alias = 'a', $join = null)
|
||||
{
|
||||
$sum = model('member_account')->getSum($where, $field, $alias, $join);
|
||||
return $this->success($sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* api接口获取用户积分信息
|
||||
* @param $member_id
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberAccountPointInApi($member_id)
|
||||
{
|
||||
//当前可用积分
|
||||
$point = model("member")->getInfo([['member_id', '=', $member_id]], 'point');
|
||||
//累计获取积分
|
||||
$point_all = model("member_account")->getSum([['member_id', '=', $member_id], ['account_type', '=', 'point'], ['account_data', '>', 0]], 'account_data');
|
||||
//累计消费积分
|
||||
$point_use = $point_all - $point['point'];
|
||||
//今日获取积分
|
||||
$start_time = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
|
||||
$point_today = model("member_account")->getSum([['member_id', '=', $member_id], ['account_data', '>', 0],['account_type', '=', 'point'], [ 'create_time', 'between', [ $start_time, time() ] ] ], 'account_data');
|
||||
return $this->success([
|
||||
'point' => $point['point'],
|
||||
'point_all' => $point_all,
|
||||
'point_use' => $point_use,
|
||||
'point_today' => $point_today
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员账户余额变动通知
|
||||
* @param $data
|
||||
*/
|
||||
public function messageAccountChangeNotice($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
$member_model = new MemberModel();
|
||||
$member_info_result = $member_model->getMemberInfo([ [ "member_id", "=", $data[ "member_id" ] ] ]);
|
||||
$member_info = $member_info_result[ "data" ];
|
||||
|
||||
$remark = $data[ 'remark' ] == '' ? $data[ 'type_name' ] : $data[ 'remark' ];
|
||||
preg_match_all('/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u', $remark, $matches);
|
||||
|
||||
$username = empty($member_info[ "nickname" ]) ? $member_info[ "mobile" ] : $member_info[ "nickname" ];
|
||||
$username = !empty($username) ? $username : $member_info[ "username" ];
|
||||
$var_parse = array (
|
||||
'username' => str_replace(' ', '', $username),//会员名
|
||||
'balance' => $member_info[ 'balance' ],
|
||||
'balance_money' => $member_info[ 'balance_money' ],
|
||||
'change_money' => $data['account_data'],
|
||||
);
|
||||
$data[ "sms_account" ] = $member_info[ "mobile" ];//手机号
|
||||
$data[ "var_parse" ] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
// 【弃用,暂无模板信息,无法使用,等待后续微信支持后开发】绑定微信公众号才发送
|
||||
// if (!empty($member_info) && !empty($member_info["wx_openid"])) {
|
||||
// $money = abs($data['account_data']);
|
||||
// $wechat_model = new WechatMessage();
|
||||
// $data["openid"] = $member_info["wx_openid"];
|
||||
// $data["template_data"] = [
|
||||
// 'keyword1' => $data['type_name'],
|
||||
// 'keyword2' => $data['account_data'] > 0 ? '+¥'.$money : '-¥'.$money,
|
||||
// 'keyword3' => '¥'.($member_info['balance'] + $member_info['balance_money']),
|
||||
// 'remark' => $data['remark'],
|
||||
// ];
|
||||
// $data["page"] = "";
|
||||
// $wechat_model->sendMessage($data);
|
||||
// }
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info[ "weapp_openid" ])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data[ "openid" ] = $member_info[ "weapp_openid" ];
|
||||
$data[ "template_data" ] = [
|
||||
'amount6' => [
|
||||
'value' => $data[ 'account_data' ]
|
||||
],
|
||||
'phrase7' => [
|
||||
'value' => $data[ 'type_name' ]
|
||||
],
|
||||
'time8' => [
|
||||
'value' => time_to_date(time())
|
||||
]
|
||||
];
|
||||
$data[ "page" ] = "";
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员账户月份数据
|
||||
* @param $condition
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getMemberAccountMonthData($condition)
|
||||
{
|
||||
$list = Db::name('member_account')
|
||||
->where($condition)
|
||||
->field("DATE_FORMAT(FROM_UNIXTIME(create_time), '%Y-%m') as month")
|
||||
->order('id desc')
|
||||
->group('month')
|
||||
->select()
|
||||
->toArray();
|
||||
$data = array_column($list, 'month');
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 会员积分变动通知
|
||||
*/
|
||||
public function messagePointChangeNotice($data)
|
||||
{
|
||||
|
||||
//return $this->success();
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
$member_model = new MemberModel();
|
||||
$member_info_result = $member_model->getMemberInfo([ [ "member_id", "=", $data[ "member_id" ] ] ]);
|
||||
$member_info = $member_info_result[ "data" ];
|
||||
|
||||
$remark = $data[ 'remark' ] == '' ? $data[ 'type_name' ] : $data[ 'remark' ];
|
||||
preg_match_all('/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u', $remark, $matches);
|
||||
|
||||
$username = empty($member_info[ "nickname" ]) ? $member_info[ "mobile" ] : $member_info[ "nickname" ];
|
||||
$username = !empty($username) ? $username : $member_info[ "username" ];
|
||||
$var_parse = array (
|
||||
'username' => str_replace(' ', '', $username),//会员名
|
||||
'point' => $member_info[ 'point' ],
|
||||
'change_point' => $data['account_data'],
|
||||
);
|
||||
$data[ "sms_account" ] = $member_info[ "mobile" ];//手机号
|
||||
$data[ "var_parse" ] = $var_parse;
|
||||
|
||||
//短信通知
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
//公众号消息模版
|
||||
if (!empty($member_info) && !empty($member_info["wx_openid"])) {
|
||||
$money = abs($data['account_data']);
|
||||
$wechat_model = new WechatMessage();
|
||||
$data["openid"] = $member_info["wx_openid"];
|
||||
$data["template_data"] = [
|
||||
'keyword1' => $data['type_name'],
|
||||
'keyword2' => $data['account_data'] > 0 ? '+'.$money : '-'.$money,
|
||||
'keyword3' => $member_info['point'],
|
||||
'remark' => $data['remark'],
|
||||
];
|
||||
$data["page"] = "";
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
172
app/model/member/MemberAddress.php
Executable file
172
app/model/member/MemberAddress.php
Executable file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 会员地址
|
||||
*/
|
||||
class MemberAddress extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加会员地址
|
||||
* @param array $data
|
||||
*/
|
||||
public function addMemberAddress($data)
|
||||
{
|
||||
if (!empty($data[ 'longitude' ]) && !empty($data[ 'latitude' ])) {
|
||||
$data[ 'type' ] = 2;
|
||||
} else {
|
||||
$data[ 'type' ] = 1;
|
||||
}
|
||||
if ($data[ 'is_default' ] == 1) {
|
||||
model('member_address')->update([ 'is_default' => 0 ], [ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'type', '=', $data[ 'type' ] ] ]);
|
||||
}
|
||||
$res = model('member_address')->add($data);
|
||||
$count = model('member_address')->getCount([ 'member_id' => $data[ 'member_id' ] ]);
|
||||
if ($count == 1) {
|
||||
model('member_address')->update([ 'is_default' => 1 ], [ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'id', '=', $res ], [ 'type', '=', $data[ 'type' ] ] ]);
|
||||
// 设置会员地址
|
||||
model('member')->update([
|
||||
'province_id' => $data[ 'province_id' ],
|
||||
'city_id' => $data[ 'city_id' ],
|
||||
'district_id' => $data[ 'district_id' ],
|
||||
'community_id' => $data[ 'community_id' ],
|
||||
'address' => $data[ 'address' ],
|
||||
'full_address' => $data[ 'full_address' ],
|
||||
'longitude' => $data[ 'longitude' ],
|
||||
'latitude' => $data[ 'latitude' ],
|
||||
], [ [ 'member_id', '=', $data[ 'member_id' ] ] ]);
|
||||
}
|
||||
Cache::tag("member_address_" . $data[ 'member_id' ])->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改会员地址
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editMemberAddress($data)
|
||||
{
|
||||
$info = model('member_address')->getInfo([ 'id' => $data[ 'id' ], 'member_id' => $data[ 'member_id' ] ], 'type');
|
||||
$type = $info[ 'type' ] ?? 1;
|
||||
if (!empty($data[ 'longitude' ]) && !empty($data[ 'latitude' ])) {
|
||||
$data[ 'type' ] = 2;
|
||||
} else {
|
||||
$data[ 'type' ] = 1;
|
||||
if ($type == 2) {
|
||||
//定位地址不能修改为,非定位地址
|
||||
return $this->error([], '');
|
||||
}
|
||||
}
|
||||
if ($data[ 'is_default' ] == 1) {
|
||||
model('member_address')->update([ 'is_default' => 0 ], [ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'type', '=', $data[ 'type' ] ] ]);
|
||||
}
|
||||
$res = model('member_address')->update($data, [ 'id' => $data[ 'id' ], 'member_id' => $data[ 'member_id' ] ]);
|
||||
Cache::tag("member_address_" . $data[ 'member_id' ])->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除收获地址
|
||||
* @param array $condition
|
||||
*/
|
||||
public function deleteMemberAddress($condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$member_id = $check_condition['member_id'] ?? 0;
|
||||
if (empty($member_id)) {
|
||||
return $this->error("", "缺少必填参数会员id");
|
||||
}
|
||||
$res = model('member_address')->delete($condition);
|
||||
Cache::tag("member_address_" . $member_id)->clear();
|
||||
if ($res === false) {
|
||||
return $this->error('', 'RESULT_ERROR');
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认收货地址
|
||||
* @param $id
|
||||
* @param $member_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function setMemberDefaultAddress($id, $member_id)
|
||||
{
|
||||
model('member_address')->startTrans();
|
||||
try {
|
||||
$info = model('member_address')->getInfo([ 'id' => $id, 'member_id' => $member_id ], 'type');
|
||||
model('member_address')->update([ 'is_default' => 0 ], [ [ 'member_id', '=', $member_id ], [ 'type', '=', $info[ 'type' ] ] ]);
|
||||
$res = model('member_address')->update([ 'is_default' => 1 ], [ 'member_id' => $member_id, 'id' => $id ]);
|
||||
model('member_address')->commit();
|
||||
Cache::tag("member_address_" . $member_id)->clear();
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
model('member_address')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情收获地址
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberAddressInfo($condition, $field = '*')
|
||||
{
|
||||
$res = model('member_address')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取收获地址列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
* @return multitype:string mixed
|
||||
*/
|
||||
public function getMemberAddressList($condition = [], $field = '*', $order = 'is_default desc', $limit = null)
|
||||
{
|
||||
$list = model('member_address')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取收获地址分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return \multitype
|
||||
*/
|
||||
public function getMemberAddressPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'is_default desc,id desc', $field = 'id, member_id, name, mobile, telephone, address, full_address, is_default, type')
|
||||
{
|
||||
$list = model('member_address')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
//获取默认地址
|
||||
public function getMemberDefault($member_id)
|
||||
{
|
||||
return model('member_address')->getInfo(['member_id'=>$member_id,'is_default'=>1,'type'=>1], 'id');
|
||||
}
|
||||
}
|
||||
165
app/model/member/MemberAuth.php
Executable file
165
app/model/member/MemberAuth.php
Executable file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 实名认证
|
||||
*/
|
||||
class MemberAuth extends BaseModel
|
||||
{
|
||||
|
||||
//申请状态
|
||||
private $status = [
|
||||
1 => '审核通过',
|
||||
0 => '待审核',
|
||||
-1 => '审核失败',
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取实名认证分页列表
|
||||
*
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getMemberAuthPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('member_auth')->pageList($condition, $field, $order, $page, $page_size, '', '', '');
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实名认证列表
|
||||
*
|
||||
* @param array $where
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @param string $group
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberAuthList($where = [], $field = true, $order = '', $alias = 'a', $join = [], $group = '', $limit = null)
|
||||
{
|
||||
$res = model('member_auth')->getList($where, $field, $order, $alias, $join, $group, $limit);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实名认证信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return unknown
|
||||
*/
|
||||
public function getMemberAuthInfo($condition = [], $field = '*')
|
||||
{
|
||||
$member_info = model('member_auth')->getInfo($condition, $field);
|
||||
return $this->success($member_info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加实名认证
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function add($data)
|
||||
{
|
||||
|
||||
$member_id = $data['member_id'] ?? '';
|
||||
if ($member_id === '') {
|
||||
return $this->error('', 'REQUEST_SITE_ID');
|
||||
}
|
||||
|
||||
$data["create_time"] = time();
|
||||
$result = model("member_auth")->add($data);
|
||||
if ($result === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑实名认证
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function edit($data, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$member_id = $check_condition['member_id'] ?? '';
|
||||
$auth_id = $check_condition['auth_id'] ?? '';
|
||||
if ($member_id === '') {
|
||||
return $this->error('', '会员ID不能为空');
|
||||
}
|
||||
if ($auth_id === '') {
|
||||
return $this->error('', '实名认证ID不能为空');
|
||||
}
|
||||
$res = model("member_auth")->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实名认证 审核通过 后台审核
|
||||
* @param unknown $auth_id
|
||||
*/
|
||||
public function authPass($auth_id)
|
||||
{
|
||||
// 开启事务
|
||||
model('member_auth')->startTrans();
|
||||
try {
|
||||
//获取实名认证 信息
|
||||
$member_auth_info = model('member_auth')->getInfo([['auth_id', '=', $auth_id]]);
|
||||
//获取会员id
|
||||
$member_id = $member_auth_info['member_id'];
|
||||
|
||||
// 会员用户实名修改
|
||||
model('member')->update(['is_auth' => 1], [['member_id', '=', $member_id]]);
|
||||
// 会员实名认证通过修改
|
||||
$res = model('member_auth')->update(['status' => 1, 'audit_time' => time()], [['auth_id', '=', $auth_id]]);
|
||||
// 事务提交
|
||||
model('member_auth')->commit();
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
// 事务回滚
|
||||
model('member_auth')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核拒绝
|
||||
* @param unknown $auth_id
|
||||
* @param unknown $reason
|
||||
*/
|
||||
public function authReject($auth_id, $reason)
|
||||
{
|
||||
$res = model('member_auth')->update(['status' => -1, 'remark' => $reason], [['auth_id', '=', $auth_id]]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实名审核状态
|
||||
*/
|
||||
public function getAuthStatus()
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
}
|
||||
194
app/model/member/MemberBankAccount.php
Executable file
194
app/model/member/MemberBankAccount.php
Executable file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 会员提现账号
|
||||
*/
|
||||
class MemberBankAccount extends BaseModel
|
||||
{
|
||||
private $withdraw_type = [
|
||||
'alipay' => '支付宝',
|
||||
'bank' => '银行卡',
|
||||
'wechatpay' => '微信'
|
||||
];
|
||||
|
||||
public function getWithdrawType()
|
||||
{
|
||||
return $this->withdraw_type;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加会员提现账号
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addMemberBankAccount($data)
|
||||
{
|
||||
//获取提现设置
|
||||
$config_model = new Withdraw();
|
||||
$config_result = $config_model->getConfig(1, 'shop');
|
||||
$config = $config_result['data']['value'];
|
||||
if (!empty($config)) {
|
||||
|
||||
//提现方式为微信的时候 判断用户是否已关注公众号
|
||||
if ($data['withdraw_type'] == 'wechatpay') {
|
||||
//获取会员信息
|
||||
$member_info = model('member')->getInfo([['member_id', '=', $data['member_id']]], 'wx_openid,weapp_openid');
|
||||
if (empty($member_info['wx_openid']) && empty($member_info['weapp_openid'])) {
|
||||
return $this->error('', '请先绑定微信');
|
||||
}
|
||||
}
|
||||
|
||||
model('member_bank_account')->startTrans();
|
||||
try {
|
||||
|
||||
if ($data['is_default'] == 1) {
|
||||
model('member_bank_account')->update(['is_default' => 0], ['member_id' => $data['member_id']]);
|
||||
}
|
||||
$data['create_time'] = time();
|
||||
$id = model('member_bank_account')->add($data);
|
||||
$count = model('member_bank_account')->getCount(['member_id' => $data['member_id']]);
|
||||
if ($count == 1)
|
||||
model('member_bank_account')->update(['is_default' => 1], ['member_id' => $data['member_id'], 'id' => $id]);
|
||||
|
||||
Cache::tag("member_bank_account_" . $data['member_id'])->clear();
|
||||
|
||||
model('member_bank_account')->commit();
|
||||
return $this->success($id);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('member_bank_account')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
} else {
|
||||
return $this->error('', '平台未开启会员提现');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改会员提现账号
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function editMemberBankAccount($data)
|
||||
{
|
||||
//获取提现设置
|
||||
$config_model = new Withdraw();
|
||||
$config_result = $config_model->getConfig(1, 'shop');
|
||||
$config = $config_result['data']['value'];
|
||||
|
||||
if (!empty($config)) {
|
||||
|
||||
//提现方式为微信的时候 判断用户是否已关注公众号
|
||||
if ($data['withdraw_type'] == 'wechatpay') {
|
||||
//获取会员信息
|
||||
$member_info = model('member')->getInfo([['member_id', '=', $data['member_id']]], 'wx_openid,weapp_openid');
|
||||
if (empty($member_info['wx_openid']) && empty($member_info['weapp_openid'])) {
|
||||
return $this->error('', '请先绑定微信');
|
||||
}
|
||||
}
|
||||
|
||||
model('member_bank_account')->startTrans();
|
||||
try {
|
||||
|
||||
if ($data['is_default'] == 1) {
|
||||
model('member_bank_account')->update(['is_default' => 0], ['member_id' => $data['member_id']]);
|
||||
}
|
||||
$data['modify_time'] = time();
|
||||
$res = model('member_bank_account')->update($data, ['id' => $data['id']]);
|
||||
Cache::tag("member_bank_account_" . $data['member_id'])->clear();
|
||||
|
||||
model('member_bank_account')->commit();
|
||||
return $this->success($res);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('member_bank_account')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
} else {
|
||||
return $this->error('', '平台未开启会员提现');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会员提现账号
|
||||
* @param array $condition
|
||||
*/
|
||||
public function deleteMemberBankAccount($condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$res = model('member_bank_account')->delete($condition);
|
||||
Cache::tag("member_bank_account_" . $check_condition['member_id'])->clear();
|
||||
if ($res === false) {
|
||||
return $this->error('', 'RESULT_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认会员提现账号
|
||||
* @param $id
|
||||
* @param $member_id
|
||||
* @return \multitype
|
||||
*/
|
||||
public function modifyDefaultAccount($id, $member_id)
|
||||
{
|
||||
model('member_bank_account')->startTrans();
|
||||
try {
|
||||
model('member_bank_account')->update(['is_default' => 0], ['member_id' => $member_id]);
|
||||
$res = model('member_bank_account')->update(['is_default' => 1], ['member_id' => $member_id, 'id' => $id]);
|
||||
model('member_bank_account')->commit();
|
||||
Cache::tag("member_bank_account_" . $member_id)->clear();
|
||||
return $this->success($res);
|
||||
} catch (\Exception $e) {
|
||||
model('member_bank_account')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员提现账号信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberBankAccountInfo($condition, $field = '*')
|
||||
{
|
||||
$res = model('member_bank_account')->getInfo($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员提现账号分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array|\multitype
|
||||
*/
|
||||
public function getMemberBankAccountPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = '*')
|
||||
{
|
||||
$list = model('member_bank_account')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
421
app/model/member/MemberCluster.php
Executable file
421
app/model/member/MemberCluster.php
Executable file
@@ -0,0 +1,421 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use addon\coupon\model\Coupon;
|
||||
use app\model\system\Cron;
|
||||
|
||||
/**
|
||||
* 会员群体
|
||||
*/
|
||||
class MemberCluster extends BaseModel
|
||||
{
|
||||
public $basic = [
|
||||
'member_level' => '会员等级',
|
||||
'member_label' => '会员标签',
|
||||
'sex' => '性别',
|
||||
'birthday' => '生日',
|
||||
'reg_time' => '注册时间',
|
||||
'point' => '当前积分',
|
||||
'balance' => '当前余额',
|
||||
'growth' => '当前成长值',
|
||||
'sign_days_series' => '连续签到次数',
|
||||
'mobile' => '会员手机',
|
||||
];
|
||||
|
||||
public $consume = [
|
||||
'order_money' => '付款金额',
|
||||
'order_complete_money' => '消费金额',
|
||||
'order_num' => '付款次数',
|
||||
'order_complete_num' => '消费次数',
|
||||
'recharge_total' => '累计充值',
|
||||
'recharge_time' => '充值次数',
|
||||
];
|
||||
|
||||
public $promotion = [
|
||||
'coupon_num' => '优惠券数',
|
||||
];
|
||||
|
||||
/**
|
||||
* 查询规则
|
||||
*/
|
||||
public $rule = [
|
||||
'member_level' => [ 'field' => "member_level", 'query_method' => "FIND_IN_SET", "table" => "member" ],
|
||||
'member_label' => [ 'field' => "member_label", 'query_method' => "FIND_IN_SET", "table" => "member" ],
|
||||
'sex' => [ 'field' => "sex", 'query_method' => "in", "table" => "member" ],
|
||||
'birthday' => [ 'field' => "birthday", 'query_method' => "between time", "table" => "member" ],
|
||||
'reg_time' => [ 'field' => "reg_time", 'query_method' => "between time", "table" => "member" ],
|
||||
'point' => [ 'field' => "point", 'query_method' => "between", "table" => "member" ],
|
||||
'balance' => [ 'field' => "balance", 'query_method' => "between", "table" => "member" ],
|
||||
'growth' => [ 'field' => "growth", 'query_method' => "between", "table" => "member" ],
|
||||
'sign_days_series' => [ 'field' => "sign_days_series", 'query_method' => "between", "table" => "member" ],
|
||||
'mobile' => [ 'field' => "mobile", 'query_method' => "like", "table" => "member" ],
|
||||
'order_money' => [ 'field' => "order_money", 'query_method' => "between", "table" => "member" ],
|
||||
'order_complete_money' => [ 'field' => "order_complete_money", 'query_method' => "between", "table" => "member" ],
|
||||
'order_num' => [ 'field' => "order_num", 'query_method' => "between", "table" => "member" ],
|
||||
'order_complete_num' => [ 'field' => "order_complete_num", 'query_method' => "between", "table" => "member" ],
|
||||
'recharge_total' => [ 'field' => "recharge_total", 'query_method' => "between", "table" => "member_recharge_order" ],
|
||||
'recharge_time' => [ 'field' => "recharge_time", 'query_method' => "between", "table" => "member_recharge_order" ],
|
||||
'coupon_num' => [ 'field' => "coupon_num", 'query_method' => "between", "table" => "promotion_coupon" ]
|
||||
];
|
||||
|
||||
/**
|
||||
* 添加会员群体
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addMemberCluster($data)
|
||||
{
|
||||
//重新计算符合会员群体的会员
|
||||
$calculate_data = $this->calculate($data);
|
||||
|
||||
$data[ 'member_num' ] = $calculate_data[ 'data' ][ 'member_num' ];
|
||||
$data[ 'member_ids' ] = $calculate_data[ 'data' ][ 'member_ids' ];
|
||||
|
||||
$res = model('member_cluster')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑会员群体
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editMemberCluster($data, $condition)
|
||||
{
|
||||
//重新计算符合会员群体的会员
|
||||
$calculate_data = $this->calculate($data);
|
||||
|
||||
$data[ 'member_num' ] = $calculate_data[ 'data' ][ 'member_num' ];
|
||||
$data[ 'member_ids' ] = $calculate_data[ 'data' ][ 'member_ids' ];
|
||||
|
||||
$res = model('member_cluster')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会员群体
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteMemberCluster($condition)
|
||||
{
|
||||
$res = model('member_cluster')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员群体信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberClusterInfo($condition = [], $field = '*')
|
||||
{
|
||||
$info = model('member_cluster')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员群体详情
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberClusterDetail($condition = [], $field = '*')
|
||||
{
|
||||
$info = model('member_cluster')->getInfo($condition, $field);
|
||||
$info[ 'rule_arr' ] = json_decode($info[ 'rule_json' ], true);
|
||||
|
||||
//获取标签
|
||||
$member_label_content = $info[ 'rule_arr' ][ 'basic' ][ 'member_label' ][ 'content' ];
|
||||
$info[ 'rule_arr' ][ 'basic' ][ 'member_label' ][ 'content_name' ] = '';
|
||||
if (!empty($member_label_content)) {
|
||||
$member_label_list = model('member_label')->getColumn([ [ "label_id", "in", $member_label_content ] ], 'label_name');
|
||||
$info[ 'rule_arr' ][ 'basic' ][ 'member_label' ][ 'content_name' ] = implode(",", $member_label_list);
|
||||
}
|
||||
//获取等级
|
||||
$member_level_content = $info[ 'rule_arr' ][ 'basic' ][ 'member_level' ][ 'content' ];
|
||||
$info[ 'rule_arr' ][ 'basic' ][ 'member_level' ][ 'content_name' ] = '';
|
||||
if (!empty($member_level_content)) {
|
||||
$member_level_list = model('member_level')->getColumn([ [ "level_id", "in", $member_level_content ] ], 'level_name');
|
||||
$info[ 'rule_arr' ][ 'basic' ][ 'member_level' ][ 'content_name' ] = implode(",", $member_level_list);
|
||||
}
|
||||
|
||||
//获取性别
|
||||
$member_sex_content = $info[ 'rule_arr' ][ 'basic' ][ 'sex' ][ 'content' ];
|
||||
$info[ 'rule_arr' ][ 'basic' ][ 'sex' ][ 'content_arr' ] = [];
|
||||
if (!empty($member_sex_content)) {
|
||||
$info[ 'rule_arr' ][ 'basic' ][ 'sex' ][ 'content_arr' ] = explode(",", $member_sex_content);
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员群体列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberClusterList($condition = [], $field = '*', $order = 'cluster_id asc', $limit = null)
|
||||
{
|
||||
$list = model('member_cluster')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员群体分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberClusterPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'cluster_id asc', $field = '*')
|
||||
{
|
||||
$list = model('member_cluster')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算人数
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function calculate($data)
|
||||
{
|
||||
//根据规则获得条件
|
||||
$condition = $this->handleRule($data[ 'rule_json' ]);
|
||||
|
||||
$condition[] = [ "site_id", '=', $data[ 'site_id' ] ];
|
||||
//只查询正常用户
|
||||
$condition[] = [ 'is_delete', '=', 0 ];
|
||||
|
||||
$member_arr = model('member')->getColumn($condition, 'member_id');
|
||||
$member_ids = implode(',', $member_arr);
|
||||
$member_num = model('member')->getCount($condition);
|
||||
|
||||
return $this->success([ "member_num" => $member_num, "member_ids" => $member_ids ]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理规则条件
|
||||
* @param $rule_json
|
||||
* @return array
|
||||
*/
|
||||
public function handleRule($rule_json)
|
||||
{
|
||||
$rule_arr = json_decode($rule_json, true);
|
||||
|
||||
$all_field = array_merge($this->basic, $this->consume, $this->promotion);
|
||||
|
||||
$condition = [];
|
||||
foreach ($rule_arr as $key => $value) {
|
||||
//获取键
|
||||
$keys = array_keys($value);
|
||||
foreach ($keys as $key) {
|
||||
|
||||
if (array_key_exists($key, $all_field)) {
|
||||
$values = $value[ $key ];
|
||||
$query_method = $this->rule[ $key ][ 'query_method' ];
|
||||
$start = $values['start'] ?? '';
|
||||
$end = $values['end'] ?? '';
|
||||
$content = $values['content'] ?? '';
|
||||
$include = $values[ 'include' ] == 2 ? "not " : "";
|
||||
$is_show = $values[ 'is_show' ];
|
||||
if ($is_show == 1) {
|
||||
switch ( $query_method ) {
|
||||
case "between":
|
||||
$condition[] = [ $key, $include . 'between', [ $start, $end ] ];
|
||||
break;
|
||||
case "between time":
|
||||
if ($values[ 'include' ] == 1) {
|
||||
//注册时间
|
||||
if ($start != '' && $end != '') {
|
||||
$condition[] = [ $key, 'between', [ strtotime($start), strtotime($end) ] ];
|
||||
} else if ($start != '' && $end == '') {
|
||||
$condition[] = [ $key, '>=', strtotime($start) ];
|
||||
} else if ($start == '' && $end != '') {
|
||||
$condition[] = [ $key, '<=', strtotime($end) ];
|
||||
}
|
||||
} else if ($values[ 'include' ] == 2) {
|
||||
//注册时间
|
||||
if ($start != '' && $end != '') {
|
||||
$condition[] = [ $key, $include . 'between', [ strtotime($start), strtotime($end) ] ];
|
||||
} else if ($start != '' && $end == '') {
|
||||
$condition[] = [ $key, '<', strtotime($start) ];
|
||||
} else if ($start == '' && $end != '') {
|
||||
$condition[] = [ $key, '>', strtotime($end) ];
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "in":
|
||||
$condition[] = [ $key, $include . 'in', $content ];
|
||||
break;
|
||||
case "like":
|
||||
$condition[] = [ $key, $include . 'like', "%" . $content . "%" ];
|
||||
break;
|
||||
case "FIND_IN_SET":
|
||||
//使用like搜索 暂不使用find_in_set 不可用包含不包含
|
||||
// $content_arr = explode(",",$content);
|
||||
// $temp_sql = '';
|
||||
// foreach ($content_arr as $content_value){
|
||||
// if(empty($temp_sql)){
|
||||
// $temp_sql .= "FIND_IN_SET({$content_value}, member_label)";
|
||||
// }else{
|
||||
// $temp_sql .= " or FIND_IN_SET({$content_value}, member_label)";
|
||||
// }
|
||||
// }
|
||||
// $condition[] = [ "", 'exp', Db::raw($temp_sql) ];
|
||||
$content_arr = explode(",", $content);
|
||||
if ($values[ 'include' ] == 1) {
|
||||
$find_in_set = "or";
|
||||
$find_in_set_method = "like";
|
||||
} else if ($values[ 'include' ] == 2) {
|
||||
$find_in_set = "and";
|
||||
$find_in_set_method = "not like";
|
||||
}
|
||||
$temp_arr = [];
|
||||
foreach ($content_arr as $content_value) {
|
||||
$content_condition = [ $content_value, '%' . $content_value . ',%', '%' . $content_value, '%,' . $content_value . ',%' ];
|
||||
$temp_arr = array_merge($content_condition, $temp_arr);
|
||||
}
|
||||
$condition[] = [ $key, $find_in_set_method, $temp_arr, $find_in_set ];
|
||||
break;
|
||||
case 4://其他表数据
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 自动定时刷新数据
|
||||
* @return array
|
||||
*/
|
||||
public function refreshMemberCluster()
|
||||
{
|
||||
$list = model('member_cluster')->getList([], 'rule_json,site_id,cluster_id');
|
||||
//循环刷新事件
|
||||
foreach ($list as $value) {
|
||||
$calculate_data = $this->calculate($value)[ 'data' ];
|
||||
model("member_cluster")->update([ "member_num" => $calculate_data[ 'member_num' ], "member_ids" => $calculate_data[ 'member_ids' ], "update_time" => time() ], [ "cluster_id" => $value[ 'cluster_id' ] ]);
|
||||
}
|
||||
|
||||
return $this->success();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发放积分
|
||||
* @param $point
|
||||
* @param $cluster_id
|
||||
* @param $remark
|
||||
* @return array
|
||||
*/
|
||||
public function sendPoint($point, $cluster_id, $remark)
|
||||
{
|
||||
$member_cluster_info = model("member_cluster")->getInfo([ "cluster_id" => $cluster_id ], "member_ids");
|
||||
|
||||
$member_list = model("member")->getList([ [ "member_id", "in", $member_cluster_info[ "member_ids" ] ] ], 'site_id,member_id');
|
||||
$member_account_model = new MemberAccount();
|
||||
model('member_account')->startTrans();
|
||||
try {
|
||||
foreach ($member_list as $value) {
|
||||
$member_account_model->addMemberAccount($value[ 'site_id' ], $value[ 'member_id' ], 'point', $point, 'adjust', '会员群体发放积分' . $point, $remark);
|
||||
}
|
||||
model('member_account')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('member_account')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发放余额
|
||||
* @param $balance
|
||||
* @param $cluster_id
|
||||
* @param $remark
|
||||
* @return array
|
||||
*/
|
||||
public function sendBalance($balance, $cluster_id, $remark)
|
||||
{
|
||||
$member_cluster_info = model("member_cluster")->getInfo([ "cluster_id" => $cluster_id ], "member_ids");
|
||||
|
||||
$member_list = model("member")->getList([ [ "member_id", "in", $member_cluster_info[ "member_ids" ] ] ], 'site_id,member_id');
|
||||
$member_account_model = new MemberAccount();
|
||||
model('member_account')->startTrans();
|
||||
try {
|
||||
foreach ($member_list as $value) {
|
||||
$member_account_model->addMemberAccount($value[ 'site_id' ], $value[ 'member_id' ], 'balance', $balance, 'adjust', '会员群体发放红包' . $balance, $remark);
|
||||
}
|
||||
model('member_account')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
model('member_account')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发放优惠券
|
||||
* @param $coupon
|
||||
* @param $cluster_id
|
||||
* @return array
|
||||
*/
|
||||
public function sendCoupon($coupon, $cluster_id)
|
||||
{
|
||||
$member_cluster_info = model("member_cluster")->getInfo([ "cluster_id" => $cluster_id ], "member_ids");
|
||||
|
||||
$member_list = model("member")->getList([ [ "member_id", "in", $member_cluster_info[ "member_ids" ] ] ], 'site_id,member_id');
|
||||
model('promotion_coupon')->startTrans();
|
||||
try {
|
||||
$coupon_model = new Coupon();
|
||||
foreach ($member_list as $value) {
|
||||
$coupon_model->giveCoupon($coupon, $value[ 'site_id' ], $value[ 'member_id' ], Coupon::GET_TYPE_MERCHANT_GIVE);
|
||||
}
|
||||
model('promotion_coupon')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('promotion_coupon')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 店铺初始化的时候添加定时任务
|
||||
*/
|
||||
public function addMemberClusterCronRefresh()
|
||||
{
|
||||
//添加会员群体刷新时间
|
||||
$cron_model = new Cron();
|
||||
$execute_time = time() + 60;
|
||||
$res = $cron_model->addCron(2, 1, "会员群体定时刷新", "CronMemberClusterRefresh", $execute_time, 0);
|
||||
return $this->success($res);
|
||||
}
|
||||
}
|
||||
121
app/model/member/MemberLabel.php
Executable file
121
app/model/member/MemberLabel.php
Executable file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 会员标签
|
||||
*/
|
||||
class MemberLabel extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 添加会员标签
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
public function addMemberLabel($data)
|
||||
{
|
||||
|
||||
$res = model('member_label')->add($data);
|
||||
Cache::tag("member_label")->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改会员标签
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $condition
|
||||
*/
|
||||
public function editMemberLabel($data, $condition)
|
||||
{
|
||||
|
||||
$res = model('member_label')->update($data, $condition);
|
||||
Cache::tag("member_label")->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会员标签
|
||||
* @param array $condition
|
||||
*/
|
||||
public function deleteMemberLabel($condition)
|
||||
{
|
||||
$res = model('member_label')->delete($condition);
|
||||
Cache::tag("member_label")->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改标签排序
|
||||
* @param int $sort
|
||||
* @param int $label_id
|
||||
*/
|
||||
public function modifyMemberLabelSort($sort, $label_id)
|
||||
{
|
||||
//判断标签下有没有会员
|
||||
if(!empty($label_ids)){
|
||||
$label_id_array = explode(',',$label_ids);
|
||||
foreach($label_id_array as $val){
|
||||
$label_is_has_member = model('member')->getList([['member_label','like','%'.$val.'%'],['is_delete','=','0']],'member_id');
|
||||
if(!empty($label_is_has_member))return $this->error('','标签下有会员不能删除!');
|
||||
}
|
||||
}
|
||||
$res = model('member_label')->update(['sort' => $sort], [['label_id', '=', $label_id]]);
|
||||
Cache::tag("member_label")->clear();
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员标签信息
|
||||
*
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
*/
|
||||
public function getMemberLabelInfo($condition = [], $field = '*')
|
||||
{
|
||||
$info = model('member_label')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员标签列表
|
||||
*
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $limit
|
||||
*/
|
||||
public function getMemberLabelList($condition = [], $field = '*', $order = 'sort asc, label_id asc', $limit = null)
|
||||
{
|
||||
$list = model('member_label')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员标签分页列表
|
||||
*
|
||||
* @param array $condition
|
||||
* @param number $page
|
||||
* @param string $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
*/
|
||||
public function getMemberLabelPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'sort asc, level_id asc', $field = '*')
|
||||
{
|
||||
$list = model('member_label')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
}
|
||||
357
app/model/member/MemberLevel.php
Executable file
357
app/model/member/MemberLevel.php
Executable file
@@ -0,0 +1,357 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use addon\coupon\model\Coupon;
|
||||
use app\model\system\Cron;
|
||||
use app\model\BaseModel;
|
||||
use addon\coupon\model\CouponType;
|
||||
|
||||
/**
|
||||
* 会员等级
|
||||
*/
|
||||
class MemberLevel extends BaseModel
|
||||
{
|
||||
public $level_change_type = [
|
||||
'upgrade' => '自动升级',
|
||||
'buy' => '付费购卡',
|
||||
'adjust' => '商家调整',
|
||||
'expire' => '到期降级'
|
||||
];
|
||||
|
||||
public $level_time = [
|
||||
'week' => '一周',
|
||||
'month' => '一月',
|
||||
'quarter' => '一季',
|
||||
'year' => '一年',
|
||||
];
|
||||
|
||||
/**
|
||||
* 添加会员等级
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addMemberLevel($data)
|
||||
{
|
||||
$res = model('member_level')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改会员等级(不允许批量处理)
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editMemberLevel($data, $condition)
|
||||
{
|
||||
$res = model('member_level')->update($data, $condition);
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$level_id = $check_condition['level_id'] ?? 0;
|
||||
if (!empty($level_id) && isset($data['level_name'])) {
|
||||
model('member')->update(['member_level_name' => $data['level_name']], [['member_level', '=', $level_id]]);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新会员等级
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function startlevel($site_id)
|
||||
{
|
||||
$list = model('member_level')->getList([['level_type', '=', 0], ['site_id', '=', $site_id]], 'level_id, level_name, growth', 'growth asc');
|
||||
|
||||
foreach ($list as $key => $val) {
|
||||
$where = [
|
||||
['growth', '>=', $val['growth']],
|
||||
['is_delete', '=', 0],
|
||||
['member_level_type', '=', 0],
|
||||
['is_member', '=', 1]
|
||||
];
|
||||
if (!empty($list[$key + 1])) $where[] = ['growth', '<', $list[$key + 1]['growth']];
|
||||
model("member")->update(['member_level' => $val['level_id'], 'member_level_name' => $val['level_name']], $where);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新会员等级排序
|
||||
* @param $site_id
|
||||
*/
|
||||
private function refreshSort($site_id)
|
||||
{
|
||||
$list = model('member_level')->getList([['site_id', '=', $site_id]], 'level_id, growth', 'growth asc');
|
||||
foreach ($list as $k => $v) {
|
||||
model('member_level')->update(['sort' => $k], [['level_id', '=', $v['level_id']]]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新会员等级
|
||||
* @param $site_id
|
||||
*/
|
||||
private function refreshLevel($site_id)
|
||||
{
|
||||
model('member_level')->update(['is_default' => 0], [['is_default', '=', 1], ['site_id', '=', $site_id]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会员等级
|
||||
* @param $level_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function deleteMemberLevel($level_id, $site_id)
|
||||
{
|
||||
$count = model('member')->getCount([['member_level', '=', $level_id], ['is_delete', '=', 0]]);
|
||||
if ($count > 0) return $this->error('', '有会员正持有该等级不可删除');
|
||||
$condition = [
|
||||
['level_id', '=', $level_id],
|
||||
['site_id', '=', $site_id],
|
||||
];
|
||||
$res = model('member_level')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一条等级
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getFirstMemberLevel($condition, $field = '*', $order = "")
|
||||
{
|
||||
$data = model('member_level')->getFirstData($condition, $field, $order);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员等级信息
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberLevelInfo($condition = [], $field = '*')
|
||||
{
|
||||
$info = model('member_level')->getInfo($condition, $field);
|
||||
if ($info) {
|
||||
//获取优惠券信息
|
||||
if (isset($info['send_coupon']) && !empty($info['send_coupon'])) {
|
||||
//优惠券字段
|
||||
$coupon_field = 'coupon_type_id,type,coupon_name,image,money,discount,validity_type,fixed_term,status,is_limit,at_least,count,lead_count,end_time';
|
||||
|
||||
$model = new CouponType();
|
||||
$coupon = $model->getCouponTypeList([['coupon_type_id', 'in', $info['send_coupon']]], $coupon_field);
|
||||
$info['coupon_list'] = $coupon;
|
||||
}
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员等级列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberLevelList($condition = [], $field = '*', $order = 'sort asc, level_id asc', $limit = null)
|
||||
{
|
||||
|
||||
$list = model('member_level')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过会员等级查询会员数量
|
||||
*/
|
||||
public function getMemberCountGroupByLevel()
|
||||
{
|
||||
$list = model('member')->getList([], "count(*) as count, member_level", '', '', '', 'member_level');
|
||||
if (!empty($list)) {
|
||||
$key = array_column($list, 'member_level');
|
||||
$member_level_array = array_combine($key, $list);
|
||||
return $member_level_array;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员等级分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberLevelPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'sort asc, level_id asc', $field = '*')
|
||||
{
|
||||
|
||||
$list = model('member_level')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加会员卡变更记录
|
||||
* @param int $member_id 变更会员
|
||||
* @param $site_id
|
||||
* @param int $after_level 变更之后的会员卡
|
||||
* @param $expire_time
|
||||
* @param $change_type
|
||||
* @param int $action_uid 操作人
|
||||
* @param int $action_type 操作人类型
|
||||
* @param string $action_name 操作人昵称
|
||||
* @param string $action_desc
|
||||
* @return array
|
||||
*/
|
||||
public function addMemberLevelChangeRecord($member_id, $site_id, $after_level, $expire_time, $change_type, $action_uid, $action_type, $action_name, $action_desc = '')
|
||||
{
|
||||
model('member_level_records')->startTrans();
|
||||
|
||||
try {
|
||||
$member_info = model('member')->getInfo([['member_id', '=', $member_id]], 'member_level,member_level_name,member_level_type,level_expire_time,is_member');
|
||||
$member_level = model('member_level')->getInfo([['level_id', '=', $member_info['member_level']]], 'growth, level_type');
|
||||
$level_info = model('member_level')->getInfo([['level_id', '=', $after_level], ['site_id', '=', $site_id]], 'level_id,level_name,level_type,growth');
|
||||
if ($member_info['member_level'] == $level_info['level_id']) {
|
||||
model('member_level_records')->rollback();
|
||||
return $this->success('', '会员卡未发生变更');
|
||||
}
|
||||
|
||||
$prev_record = model('member_level_records')->getFirstData([['member_id', '=', $member_id]], 'id', 'change_time desc');
|
||||
// 添加变更记录
|
||||
$data = [
|
||||
'member_id' => $member_id,
|
||||
'site_id' => $site_id,
|
||||
'before_level_id' => $member_info['member_level'],
|
||||
'before_level_name' => $member_info['member_level_name'],
|
||||
'before_level_type' => $member_info['member_level_type'],
|
||||
'before_expire_time' => $member_info['level_expire_time'],
|
||||
'after_level_id' => $level_info['level_id'],
|
||||
'after_level_name' => $level_info['level_name'],
|
||||
'after_level_type' => $level_info['level_type'],
|
||||
'prev_id' => $prev_record['id'] ?? 0,
|
||||
'change_time' => time(),
|
||||
'action_uid' => $action_uid,
|
||||
'action_type' => $action_type,
|
||||
'action_name' => $action_name,
|
||||
'action_desc' => $action_desc,
|
||||
'change_type' => $change_type,
|
||||
'change_type_name' => $this->level_change_type[$change_type]
|
||||
];
|
||||
model('member_level_records')->add($data);
|
||||
|
||||
// 变更会员等级
|
||||
$edit_member_data = [
|
||||
'member_level' => $level_info['level_id'],
|
||||
'member_level_name' => $level_info['level_name'],
|
||||
'member_level_type' => $level_info['level_type'],
|
||||
'level_expire_time' => $level_info['level_type'] == 0 ? 0 : $expire_time
|
||||
];
|
||||
|
||||
if (!$member_info['is_member']) {
|
||||
$edit_member_data['is_member'] = 1;
|
||||
$edit_member_data['member_time'] = time();
|
||||
}
|
||||
model('member')->update($edit_member_data, [['member_id', '=', $member_id]]);
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([['event', '=', 'MemberLevelAutoExpire'], ['relate_id', '=', $member_id]]);
|
||||
if ($level_info['level_type']) {
|
||||
$cron->addCron(1, 0, "会员卡自动过期", "MemberLevelAutoExpire", $expire_time, $member_id);
|
||||
}
|
||||
model('member_level_records')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_level_records')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员会员卡变更记录
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @param null $group
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberLevelRecordPageList($condition = [], $page = 1, $list_rows = PAGE_LIST_ROWS, $field = '*', $order = 'change_time desc', $alias = 'a', $join = [], $group = null)
|
||||
{
|
||||
$list = model('member_level_records')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join, $group);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员卡过期
|
||||
* @param $member_id
|
||||
*/
|
||||
public function memberLevelExpire($member_id)
|
||||
{
|
||||
$member_info = model('member')->getInfo([['member_id', '=', $member_id]], 'member_id,site_id,nickname,member_level,level_expire_time,growth');
|
||||
if (!empty($member_info) && !empty($member_info['level_expire_time']) && $member_info['level_expire_time'] < time()) {
|
||||
$alias = 'mlr';
|
||||
$join = [
|
||||
['member_level ml', 'ml.level_id = mlr.before_level_id', 'inner']
|
||||
];
|
||||
// 如果会员还存在未过期的付费会员卡
|
||||
$level_info = model('member_level_records')->getFirstDataView([['before_expire_time', '>', time()], ['member_id', '=', $member_id]], 'mlr.*', 'change_time desc', $alias, $join);
|
||||
|
||||
if (!empty($level_info)) {
|
||||
$this->addMemberLevelChangeRecord($member_id, $member_info['site_id'], $level_info['before_level_id'], $level_info['before_expire_time'], 'expire', $member_id, 'member', $member_info['nickname']);
|
||||
} else {
|
||||
// 如果之前免费卡还存在
|
||||
$level_info = model('member_level_records')->getFirstDataView([['before_level_type', '=', 0], ['member_id', '=', $member_id]], 'mlr.*', 'change_time desc', $alias, $join);
|
||||
if (!empty($level_info)) {
|
||||
$this->addMemberLevelChangeRecord($member_id, $member_info['site_id'], $level_info['before_level_id'], $level_info['before_expire_time'], 'expire', $member_id, 'member', $member_info['nickname']);
|
||||
event("AddMemberAccount", ['account_type' => 'growth', 'member_id' => $member_id, 'site_id' => $member_info['site_id']]);
|
||||
} else {
|
||||
// 如果之前的免费卡不存在
|
||||
$level_info = model('member_level')->getFirstData([['site_id', '=', $member_info['site_id']], ['level_type', '=', 0], ['growth', '<=', $member_info['growth']]], '*', 'growth desc');
|
||||
|
||||
if (!empty($level_info)) {
|
||||
$this->addMemberLevelChangeRecord($member_id, $member_info['site_id'], $level_info['level_id'], 0, 'expire', $member_id, 'member', $member_info['nickname']);
|
||||
$member_account = new MemberAccount();
|
||||
//赠送红包
|
||||
if ($level_info['send_balance'] > 0) {
|
||||
$member_account->addMemberAccount($member_info['site_id'], $member_info['member_id'], 'balance', $level_info['send_balance'], 'upgrade', '会员升级得红包' . $level_info['send_balance'], '会员等级升级奖励');
|
||||
}
|
||||
//赠送积分
|
||||
if ($level_info['send_point'] > 0) {
|
||||
$member_account->addMemberAccount($member_info['site_id'], $member_info['member_id'], 'point', $level_info['send_point'], 'upgrade', '会员升级得积分' . $level_info['send_point'], '会员等级升级奖励');
|
||||
}
|
||||
//给用户发放优惠券
|
||||
if (!empty($level_info['send_coupon'])) {
|
||||
$coupon_array = explode(',', $level_info['send_coupon']);
|
||||
$coupon_model = new Coupon();
|
||||
$coupon_array = array_map(function ($value) {
|
||||
return ['coupon_type_id' => $value, 'num' => 1];
|
||||
}, $coupon_array);
|
||||
$coupon_model->giveCoupon($coupon_array, $member_info['site_id'], $member_info['member_id'], Coupon::GET_TYPE_ACTIVITY_GIVE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
137
app/model/member/MemberSignin.php
Executable file
137
app/model/member/MemberSignin.php
Executable file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use Carbon\Carbon;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 会员签到
|
||||
*/
|
||||
class MemberSignin extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 获取签到奖励规则
|
||||
*/
|
||||
public function getAward($site_id)
|
||||
{
|
||||
$award = event('MemberSigninAward', [ 'site_id' => $site_id ], true);
|
||||
return $this->success($award);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否已经签到
|
||||
* @param $member_id
|
||||
* @return array
|
||||
*/
|
||||
public function isSign($member_id)
|
||||
{
|
||||
$member_info = model("member")->getInfo([ [ 'member_id', '=', $member_id ] ], 'sign_time');
|
||||
if ($member_info[ 'sign_time' ] != 0) {
|
||||
$compare_time = Carbon::today()->timestamp;
|
||||
if ($member_info[ 'sign_time' ] < $compare_time) {
|
||||
$is_sign = 0;
|
||||
} else {
|
||||
$is_sign = 1;
|
||||
}
|
||||
|
||||
//纠正连签天数
|
||||
$compare_yesterday = Carbon::yesterday()->timestamp;
|
||||
if ($compare_yesterday > $member_info[ 'sign_time' ]) {
|
||||
model("member")->update([ 'sign_days_series' => 0, 'sign_time' => 0 ], [ [ 'member_id', '=', $member_id ] ]);
|
||||
}
|
||||
|
||||
} else {
|
||||
$is_sign = 0;
|
||||
}
|
||||
return $this->success($is_sign);
|
||||
}
|
||||
|
||||
/**
|
||||
* 签到
|
||||
* @param $member_id
|
||||
* @param $site_id
|
||||
* @return array|\multitype
|
||||
* @throws DbException
|
||||
*/
|
||||
public function signin($member_id, $site_id)
|
||||
{
|
||||
$member_info = model("member")->getInfo([ [ 'member_id', '=', $member_id ] ], 'sign_time,sign_days_series');
|
||||
if ($member_info[ 'sign_time' ] != 0) {
|
||||
$compare_time = Carbon::today()->timestamp;
|
||||
if ($member_info[ 'sign_time' ] < $compare_time) {
|
||||
$is_sign = 0;
|
||||
} else {
|
||||
$is_sign = 1;
|
||||
}
|
||||
} else {
|
||||
$is_sign = 0;
|
||||
}
|
||||
if ($is_sign == 1) {
|
||||
return $this->error('', "SIGNED_IN");
|
||||
} else {
|
||||
$data_log = [
|
||||
'member_id' => $member_id,
|
||||
'action' => 'membersignin',
|
||||
'action_name' => '会员签到',
|
||||
'create_time' => time(),
|
||||
'remark' => '会员签到'
|
||||
];
|
||||
model("member_log")->add($data_log);
|
||||
|
||||
//连续签到
|
||||
$compare_yesterday = Carbon::yesterday()->timestamp;
|
||||
if ($compare_yesterday < $member_info[ 'sign_time' ]) {
|
||||
model("member")->setInc([ [ 'member_id', '=', $member_id ] ], 'sign_days_series');
|
||||
model("member")->update([ 'sign_time' => time() ], [ [ 'member_id', '=', $member_id ] ]);
|
||||
} else {
|
||||
model("member")->update([ 'sign_days_series' => 1, 'sign_time' => time() ], [ [ 'member_id', '=', $member_id ] ]);
|
||||
}
|
||||
|
||||
//执行签到奖励
|
||||
$res = event("MemberSignin", [ 'member_id' => $member_id, 'site_id' => $site_id ], true);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签到记录
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberSigninList($condition = [], $field = '*', $order = 'create_time asc')
|
||||
{
|
||||
$list = model('member_log')->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连续签到天数
|
||||
* @param $member_id
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberSignDaysSeries($member_id)
|
||||
{
|
||||
$member_info = model("member")->getInfo([ [ 'member_id', '=', $member_id ] ], 'sign_time,sign_days_series');
|
||||
$sign_days_series = 0;
|
||||
if(!empty($member_info)){
|
||||
$compare_yesterday = Carbon::yesterday()->timestamp;
|
||||
if ($compare_yesterday < $member_info[ 'sign_time' ]) {
|
||||
$sign_days_series = $member_info['sign_days_series'];
|
||||
}
|
||||
}
|
||||
return $this->success($sign_days_series);
|
||||
}
|
||||
}
|
||||
383
app/model/member/Register.php
Executable file
383
app/model/member/Register.php
Executable file
@@ -0,0 +1,383 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use addon\coupon\model\CouponType;
|
||||
use addon\wechat\model\Message as WechatMessage;
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Sms;
|
||||
use addon\coupon\model\Coupon;
|
||||
use app\model\system\Stat;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 注册
|
||||
* Class Register
|
||||
* @package app\model\member
|
||||
*/
|
||||
class Register extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 用户名密码注册(必传username, password),之前检测重复性,判断用户名是否为手机,邮箱
|
||||
* @param $data
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function usernameRegister($data)
|
||||
{
|
||||
$examine_username_exit = $this->usernameExist($data['username'], $data['site_id']);
|
||||
if ($examine_username_exit) return $this->error('', '用户名已存在');
|
||||
|
||||
$nickname = $data['username']; // 默认昵称为用户名
|
||||
if (isset($data['nickname']) && !empty($data['nickname'])) {
|
||||
$nickname = preg_replace_callback('/./u', function(array $match) {
|
||||
return strlen($match[0]) >= 4 ? '' : $match[0];
|
||||
}, $data['nickname']);
|
||||
}
|
||||
$data['nickname'] = $nickname;
|
||||
return $this->memberRegister($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号密码注册(必传mobile, password),之前检测重复性
|
||||
* @param $data
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function mobileRegister($data)
|
||||
{
|
||||
$examine_mobile_exit = $this->mobileExist($data['mobile'], $data['site_id']);
|
||||
if ($examine_mobile_exit) return $this->error('', '手机号已存在');
|
||||
$data['username'] = $data['username'] ?? $this->createRandUsername($data['site_id']);
|
||||
$nickname = substr($data['mobile'], 0, 3).'****'.substr($data['mobile'], 7);
|
||||
if (isset($data['nickname']) && !empty($data['nickname'])) {
|
||||
$nickname = preg_replace_callback('/./u', function(array $match) {
|
||||
return strlen($match[0]) >= 4 ? '' : $match[0];
|
||||
}, $data['nickname']);
|
||||
}
|
||||
$data['nickname'] = $nickname;
|
||||
return $this->memberRegister($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 第三方注册
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function authRegister($data)
|
||||
{
|
||||
$data['username'] = $this->createRandUsername($data['site_id']);
|
||||
$nickname = $data['username'];
|
||||
if (isset($data['nickName']) && !empty($data['nickName'])) {
|
||||
$nickname = preg_replace_callback('/./u', function(array $match) {
|
||||
return strlen($match[0]) >= 4 ? '' : $match[0];
|
||||
}, $data['nickName']);
|
||||
}
|
||||
$data['nickname'] = $nickname;
|
||||
return $this->memberRegister($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员注册方法
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
private function memberRegister($data)
|
||||
{
|
||||
//会员注册相关奖励
|
||||
$award_array = [
|
||||
'point' => 0,
|
||||
'balance' => 0,
|
||||
'growth' => 0,
|
||||
'coupon' => 0,
|
||||
'coupon_list' => [],
|
||||
'coupon_list_config'=>[]
|
||||
];
|
||||
$reg_award_array = event("MemberRegisterAward", ['site_id' => $data['site_id']]);
|
||||
if (!empty($reg_award_array)) {
|
||||
foreach ($reg_award_array as $k => $v) {
|
||||
$award_array['point'] += $v['point'] ?? 0;
|
||||
$award_array['balance'] += $v['balance'] ?? 0;
|
||||
$award_array['growth'] += $v['growth'] ?? 0;
|
||||
$coupon_list = $v['coupon_list'] ?? [];
|
||||
if (!empty($coupon_list)) {
|
||||
$award_array['coupon_list'] = empty($award_array['coupon_list']) ? $coupon_list : array_merge($award_array['coupon_list'], $v['coupon_list']);
|
||||
$award_array['coupon_list_config'] = array_column($v['coupon_new'] ?? [],'num','id');
|
||||
}
|
||||
}
|
||||
}
|
||||
//查询会员等级相关(根据会员获取的成长值)
|
||||
$member_level = new MemberLevel();
|
||||
$member_level_info = $member_level->getFirstMemberLevel([['site_id', '=', $data['site_id']], ['level_type', '=', 0], ['growth', '<=', $award_array['growth']]], '*', 'growth desc')['data'];
|
||||
//查询会员等级相关奖励
|
||||
$data_reg = [
|
||||
'site_id' => $data['site_id'],
|
||||
'source_member' => $data['source_member'] ?? 0,
|
||||
'username' => $data['username'],
|
||||
'nickname' => $data['nickname'],
|
||||
'mobile' => $data['mobile'] ?? '',
|
||||
'password' => isset($data['password']) && !empty($data['password']) ? data_md5($data['password']) : '',
|
||||
'qq_openid' => $data['qq_openid'] ?? '',
|
||||
'wx_openid' => $data['wx_openid'] ?? '',
|
||||
'weapp_openid' => $data['weapp_openid'] ?? '',
|
||||
'wx_unionid' => $data['wx_unionid'] ?? '',
|
||||
'ali_openid' => $data['ali_openid'] ?? '',
|
||||
'baidu_openid' => $data['baidu_openid'] ?? '',
|
||||
'toutiao_openid' => $data['toutiao_openid'] ?? '',
|
||||
'headimg' => $data['avatarUrl'] ?? '',
|
||||
'member_level' => !empty($member_level_info) ? $member_level_info['level_id'] : 0,
|
||||
'member_level_name' => !empty($member_level_info) ? $member_level_info['level_name'] : '',
|
||||
'is_member' => !empty($member_level_info) ? 1 : 0,
|
||||
'member_time' => !empty($member_level_info) ? time() : 0,
|
||||
'reg_time' => time(),
|
||||
'login_time' => time(),
|
||||
'last_login_time' => time(),
|
||||
'is_edit_username' => 1,
|
||||
'last_visit_time' => time(),
|
||||
'login_type' => $data['app_type'] ?? '',
|
||||
'login_type_name' => $data['app_type_name'] ?? '',
|
||||
];
|
||||
$member_id = model("member")->add($data_reg);
|
||||
|
||||
if ($member_id) {
|
||||
if (!empty($member_level_info)) {
|
||||
$award_array['point'] += $member_level_info['send_point'];
|
||||
$award_array['balance'] += $member_level_info['send_balance'];
|
||||
//获取优惠券信息
|
||||
if (!empty($member_level_info['send_coupon'])) {
|
||||
//优惠券字段
|
||||
$coupon_field = '*';
|
||||
|
||||
$model = new CouponType();
|
||||
$coupon = $model->getCouponTypeList([['coupon_type_id', 'in', $member_level_info['send_coupon']]], $coupon_field);
|
||||
$member_level_info['coupon_list'] = $coupon['data'];
|
||||
}
|
||||
|
||||
$coupon_list = $member_level_info['coupon_list'] ?? [];
|
||||
if (!empty($coupon_list)) {
|
||||
$award_array['coupon_list'] = empty($award_array['coupon_list']) ? $coupon_list : array_merge($award_array['coupon_list'], $v['coupon_list']);
|
||||
}
|
||||
|
||||
}
|
||||
//会员注册奖励积分,优惠券,红包
|
||||
$data_reg['member_id'] = $member_id;
|
||||
$member_account_model = new MemberAccount();
|
||||
$member_account_model->addMemberAccountInRegister($data_reg, $award_array);
|
||||
//给用户发放优惠券
|
||||
if (!empty($award_array['coupon_list'])) {
|
||||
$coupon_model = new Coupon();
|
||||
$coupon_data = [];
|
||||
foreach($award_array['coupon_list'] as $val){
|
||||
$coupon_data[] = ['coupon_type_id' => $val['coupon_type_id'], 'num' => $award_array['coupon_list_config'][$val['coupon_type_id']] ?? 1];
|
||||
}
|
||||
$coupon_model->giveCoupon($coupon_data, $data['site_id'], $member_id, Coupon::GET_TYPE_ACTIVITY_GIVE);
|
||||
}
|
||||
//会员注册成功后续事件
|
||||
Queue::push('app\job\MemberRegisterAfter', $data_reg);
|
||||
|
||||
}
|
||||
|
||||
return $this->success($member_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员注册成功后续事件
|
||||
* @param $data_reg
|
||||
*/
|
||||
public function memberRegisterAfter($data_reg)
|
||||
{
|
||||
event("MemberRegister", ['member_id' => $data_reg['member_id'], 'site_id' => $data_reg['site_id']]);
|
||||
//添加统计
|
||||
$stat = new Stat();
|
||||
$stat->switchStat(['type' => 'add_member', 'data' => ['member_count' => 1, 'site_id' => $data_reg['site_id']]]);
|
||||
$this->pullHeadimg($data_reg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机用户名
|
||||
* @param $site_id
|
||||
* @return string
|
||||
*/
|
||||
private function createRandUsername($site_id)
|
||||
{
|
||||
$usernamer = 'u_' . random_keys(10);
|
||||
$count = model('member')->getCount([['username', '=', $usernamer], ['site_id', '=', $site_id]]);
|
||||
if ($count) {
|
||||
$usernamer = $this->createRandUsername($site_id);
|
||||
return $usernamer;
|
||||
} else {
|
||||
return $usernamer;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置用户微信openid
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function wxopenidBind($data)
|
||||
{
|
||||
$res = model("member")->update(['wx_openid' => $data['wx_openid']], [['member_id', '=', $data['member_id']], ['site_id', '=', $data['site_id']], ['is_delete', '=', 0]]);
|
||||
if ($res) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测用户存在性(用户名)
|
||||
* @param $username
|
||||
* @param $site_id
|
||||
* @return int
|
||||
*/
|
||||
public function usernameExist($username, $site_id)
|
||||
{
|
||||
$member_info = model("member")->getInfo([
|
||||
['username|mobile', '=', $username],
|
||||
['site_id', '=', $site_id],
|
||||
['is_delete', '=', 0]
|
||||
], 'member_id');
|
||||
if (!empty($member_info)) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测用户存在性(用户名) 存在返回1
|
||||
* @param $mobile
|
||||
* @param $site_id
|
||||
* @return int
|
||||
*/
|
||||
public function mobileExist($mobile, $site_id)
|
||||
{
|
||||
$member_info = model("member")->getInfo([
|
||||
['mobile', '=', $mobile],
|
||||
['site_id', '=', $site_id],
|
||||
['is_delete', '=', 0]], 'member_id');
|
||||
if (!empty($member_info)) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测用户存在性(wx_openid) 存在返回1 新增2021.06.18
|
||||
* @param $mobile
|
||||
* @param $site_id
|
||||
* @return int
|
||||
*/
|
||||
public function openidExist($mobile, $site_id)
|
||||
{
|
||||
$member_info = model("member")->getInfo([
|
||||
['mobile', '=', $mobile],
|
||||
['site_id', '=', $site_id],
|
||||
['is_delete', '=', 0]
|
||||
], 'wx_openid');
|
||||
if (!empty($member_info['wx_openid'])) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户ID 新增2021.06.18
|
||||
* @param $mobile
|
||||
* @param $site_id
|
||||
* @return int
|
||||
*/
|
||||
public function getMemberId($mobile, $site_id)
|
||||
{
|
||||
$member_info = model("member")->getInfo([
|
||||
['mobile', '=', $mobile],
|
||||
['site_id', '=', $site_id],
|
||||
['is_delete', '=', 0]
|
||||
], 'member_id');
|
||||
if (!empty($member_info)) {
|
||||
return $member_info['member_id'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册发送验证码
|
||||
* @param $data
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function registerCode($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$var_parse = array (
|
||||
"code" => $data["code"],//验证码
|
||||
);
|
||||
$data["sms_account"] = $data["mobile"] ?? '';//手机号
|
||||
$data["var_parse"] = $var_parse;
|
||||
$sms_result = $sms_model->sendMessage($data);
|
||||
if ($sms_result["code"] < 0)
|
||||
return $sms_result;
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册成功通知
|
||||
* @param $data
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function registerSuccess($data)
|
||||
{
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([["member_id", "=", $data["member_id"]]], "username,mobile,email,reg_time,wx_openid,last_login_type,nickname");
|
||||
$member_info = $member_info_result["data"];
|
||||
$name = $member_info["nickname"] == '' ? $member_info["mobile"] : $member_info["nickname"];
|
||||
//发送短信
|
||||
$var_parse = [
|
||||
"shopname" => replaceSpecialChar($data['site_info']['site_name']), //商城名称
|
||||
"username" => replaceSpecialChar($name), //会员名称
|
||||
];
|
||||
$data["sms_account"] = $member_info["mobile"] ?? '';//手机号
|
||||
$data["var_parse"] = $var_parse;
|
||||
$sms_model = new Sms();
|
||||
$sms_result = $sms_model->sendMessage($data);
|
||||
// if ($sms_result["code"] < 0) return $sms_result;
|
||||
|
||||
//发送模板消息
|
||||
$wechat_model = new WechatMessage();
|
||||
$data["openid"] = $member_info["wx_openid"];
|
||||
|
||||
$data["template_data"] = [
|
||||
'keyword1' => $member_info["nickname"],
|
||||
'keyword2' => time_to_date($member_info["reg_time"]),
|
||||
];
|
||||
$data["page"] = '';
|
||||
$wechat_model->sendMessage($data);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取用户头像
|
||||
* @param unknown $info
|
||||
*/
|
||||
private function pullHeadimg($data)
|
||||
{
|
||||
if (!empty($data['headimg']) && is_url($data['headimg'])) {
|
||||
$url = __ROOT__ . '/api/member/pullheadimg?member_id=' . $data['member_id'];
|
||||
http($url, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
774
app/model/member/Withdraw.php
Executable file
774
app/model/member/Withdraw.php
Executable file
@@ -0,0 +1,774 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\member;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Message;
|
||||
use app\model\message\Sms;
|
||||
use app\model\shop\ShopAcceptMessage;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\system\Pay;
|
||||
use app\model\system\PayTransfer;
|
||||
use app\model\system\Stat;
|
||||
use think\facade\Cache;
|
||||
use addon\memberwithdraw\model\Withdraw as MemberWithdraw;
|
||||
use addon\wechat\model\Message as WechatMessage;
|
||||
use addon\weapp\model\Message as WeappMessage;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 会员提现
|
||||
*/
|
||||
class Withdraw extends BaseModel
|
||||
{
|
||||
const STATUS_WAIT_AUDIT = 0;//待审核
|
||||
const STATUS_WAIT_TRANSFER = 1;//待转账
|
||||
const STATUS_SUCCESS = 2;//已转账
|
||||
const STATUS_IN_PROCESS = 3;//转账中
|
||||
const STATUS_FAIL = -2;//转账失败
|
||||
const STATUS_REFUSE = -1;//已拒绝
|
||||
|
||||
public $status = array (
|
||||
self::STATUS_WAIT_AUDIT => '待审核',
|
||||
self::STATUS_WAIT_TRANSFER => '待转账',
|
||||
self::STATUS_SUCCESS => '已转账',
|
||||
self::STATUS_IN_PROCESS => '转账中',
|
||||
self::STATUS_FAIL => '转账失败',
|
||||
self::STATUS_REFUSE => '已拒绝',
|
||||
);
|
||||
|
||||
/**************************************************************************** 会员提现设置 *************************************************************/
|
||||
/**
|
||||
* 会员提现设置
|
||||
* @param $data
|
||||
* @param $is_use
|
||||
* @param int $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setConfig($data, $is_use, $site_id = 0, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->setConfig($data, '会员提现设置', $is_use, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'MEMBER_WITHDRAW_CONFIG' ] ]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员提现设置
|
||||
* @param int $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getConfig($site_id = 0, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'MEMBER_WITHDRAW_CONFIG' ] ]);
|
||||
if (empty($res[ 'data' ][ 'value' ])) {
|
||||
$res[ 'data' ][ 'value' ] = [
|
||||
'is_auto_audit' => 0,
|
||||
'rate' => 0,
|
||||
'transfer_type' => '',
|
||||
'is_auto_transfer' => 0,
|
||||
'min' => 0,
|
||||
'max' => 0,
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
/**************************************************************************** 会员提现设置 *************************************************************/
|
||||
/**
|
||||
* 申请提现
|
||||
* @param $data
|
||||
* @param int $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function apply($data, $site_id = 0, $app_module = 'shop')
|
||||
{
|
||||
|
||||
$config_result = $this->getConfig($site_id, $app_module);
|
||||
$config = $config_result[ "data" ][ 'value' ];
|
||||
if ($config_result[ "data" ][ "is_use" ] == 0)
|
||||
return $this->error([], "提现未开启");
|
||||
|
||||
$withdraw_no = $this->createWithdrawNo();
|
||||
$apply_money = round($data[ "apply_money" ], 2);
|
||||
if ($apply_money < $config[ "min" ])
|
||||
return $this->error([], "申请提现金额不能小于最低提现额度" . $config[ "min" ]);
|
||||
if ($apply_money > $config[ 'max' ]) return $this->error([], "申请提现金额不能大于最高提现额度" . $config[ "max" ]);
|
||||
$member_id = $data[ "member_id" ];
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([ [ "member_id", "=", $member_id ] ], "balance_money,headimg,wx_openid,username,mobile,weapp_openid,nickname");
|
||||
$member_info = $member_info_result[ "data" ];
|
||||
if (empty($member_info))
|
||||
return $this->error([], "MEMBER_NOT_EXIST");
|
||||
|
||||
$balance_money = $member_info[ "balance_money" ];
|
||||
if ($apply_money > $balance_money)
|
||||
return $this->error([], "申请提现金额不能大于会员可提现金额");
|
||||
$transfer_type = $data[ "transfer_type" ];
|
||||
$transfer_type_list = $this->getTransferType($site_id, $app_module);
|
||||
$transfer_type_name = $transfer_type_list[ $transfer_type ] ?? '';
|
||||
if (empty($transfer_type_name))
|
||||
return $this->error([], "不支持的提现方式");
|
||||
|
||||
model('member_withdraw')->startTrans();
|
||||
try {
|
||||
$rate = $config[ "rate" ];
|
||||
$bank_name = "";
|
||||
$account_number = "";
|
||||
$applet_type = 0;
|
||||
switch ( $transfer_type ) {
|
||||
case "bank":
|
||||
$bank_name = $data[ "bank_name" ];
|
||||
$account_number = $data[ "account_number" ];
|
||||
|
||||
break;
|
||||
case "alipay":
|
||||
$bank_name = '';
|
||||
$account_number = $data[ "account_number" ];
|
||||
break;
|
||||
case "wechatpay":
|
||||
$bank_name = '';
|
||||
if (empty($member_info[ "wx_openid" ]) && empty($member_info[ "weapp_openid" ])) {
|
||||
return $this->error('', '请绑定微信或更换提现账户');
|
||||
}
|
||||
if ($data['app_type'] != 'weapp') {
|
||||
$account_number = $member_info[ "wx_openid" ];
|
||||
$applet_type = 0; // 公众号
|
||||
} else {
|
||||
$account_number = $member_info[ "weapp_openid" ];
|
||||
$applet_type = 1; // 小程序
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
$service_money = round($apply_money * $rate / 100, 2);//手续费
|
||||
$money = $apply_money - $service_money;
|
||||
$data = array (
|
||||
"site_id" => $site_id,
|
||||
"withdraw_no" => $withdraw_no,
|
||||
"member_name" => $member_info[ "username" ] == '' ? $member_info[ "mobile" ] : $member_info[ "username" ],
|
||||
"member_id" => $data[ "member_id" ],
|
||||
"transfer_type" => $data[ "transfer_type" ],
|
||||
"transfer_type_name" => $transfer_type_name,
|
||||
"apply_money" => $apply_money,
|
||||
"service_money" => $service_money,
|
||||
"rate" => $rate,
|
||||
"money" => $money,
|
||||
"apply_time" => time(),
|
||||
"status" => self::STATUS_WAIT_AUDIT,
|
||||
"status_name" => $this->status[self::STATUS_WAIT_AUDIT],
|
||||
"member_headimg" => $member_info[ "headimg" ],
|
||||
"realname" => $data[ "realname" ],
|
||||
"bank_name" => $bank_name,
|
||||
"account_number" => $account_number,
|
||||
"mobile" => $data[ "mobile" ],
|
||||
"applet_type" => $applet_type
|
||||
);
|
||||
|
||||
//减少现金余额
|
||||
$member_account = new MemberAccount();
|
||||
$account_res = $member_account->addMemberAccount($site_id, $member_id, 'balance_money', -$apply_money, 'withdraw', '会员提现', '会员提现扣除');
|
||||
if ($account_res[ 'code' ] < 0) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $account_res;
|
||||
}
|
||||
|
||||
//增加提现中余额
|
||||
model("member")->setInc([ [ "member_id", "=", $member_id ] ], "balance_withdraw_apply", $apply_money);
|
||||
|
||||
$result = model("member_withdraw")->add($data);
|
||||
|
||||
//添加转账记录
|
||||
$pay_transfer_model = new PayTransfer();
|
||||
$info = model('member_withdraw')->getInfo([['id', '=', $result]]);
|
||||
$pay_transfer_model->add([
|
||||
"real_name" => $info[ "realname" ],
|
||||
"amount" => $info[ "money" ],
|
||||
"desc" => "会员提现" . $info[ "memo" ],
|
||||
"transfer_type" => $info[ "transfer_type" ],
|
||||
"account_number" => $info[ "account_number" ],
|
||||
"site_id" => $info[ "site_id" ],
|
||||
"is_weapp" => $info[ "applet_type" ],
|
||||
"member_id" => $info[ 'member_id' ],
|
||||
'from_type' => 'member_withdraw',
|
||||
"relate_tag" => $info['id'],
|
||||
]);
|
||||
|
||||
//是否启用自动通过审核(必须是微信)
|
||||
if ($config[ "is_auto_audit" ] == 0) {
|
||||
$this->agree([ [ "id", "=", $result ], [ 'site_id', '=', $site_id ] ]);
|
||||
}
|
||||
|
||||
model('member_withdraw')->commit();
|
||||
|
||||
//申请提现发送消息
|
||||
$data[ 'keywords' ] = 'USER_WITHDRAWAL_APPLY';
|
||||
$data[ 'member_name' ] = $member_info[ 'nickname' ];
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage($data);
|
||||
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同意提现申请
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function agree($condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$site_id = $check_condition[ 'site_id' ];
|
||||
$app_module = $check_condition[ 'app_module' ] ?? 'shop';
|
||||
if (empty($site_id)) {
|
||||
return $this->error(-1, '参数错误');
|
||||
}
|
||||
$info = model("member_withdraw")->getInfo($condition);
|
||||
if (empty($info))
|
||||
return $this->error();
|
||||
|
||||
$config_result = $this->getConfig($site_id, $app_module);
|
||||
$config = $config_result[ "data" ];
|
||||
|
||||
model('member_withdraw')->startTrans();
|
||||
try {
|
||||
$data = array (
|
||||
"status" => self::STATUS_WAIT_TRANSFER,
|
||||
"status_name" => $this->status[self::STATUS_WAIT_TRANSFER],
|
||||
"audit_time" => time(),
|
||||
);
|
||||
$result = model("member_withdraw")->update($data, $condition);
|
||||
|
||||
//是否启用自动转账(必须是微信或支付宝)
|
||||
if ($config[ "value" ][ "is_auto_transfer" ] == 1) {
|
||||
$member_withdraw_model = new MemberWithdraw();
|
||||
$member_withdraw_model->transfer($info[ "id" ]);
|
||||
/*if ($transfer_res['code'] == 0) {
|
||||
//提现成功发送消息
|
||||
$info['keywords'] = 'USER_WITHDRAWAL_SUCCESS';
|
||||
$message_model = new Message();
|
||||
$res = $message_model->sendMessage($info);
|
||||
}*/
|
||||
}
|
||||
model('member_withdraw')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拒绝提现申请
|
||||
* @param $condition
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function refuse($condition, $param)
|
||||
{
|
||||
$info = model("member_withdraw")->getInfo($condition, "status,site_id,transfer_type,member_id,apply_money");
|
||||
if (empty($info)){
|
||||
return $this->error(null, '提现信息有误');
|
||||
}
|
||||
if (!in_array($info['status'], [self::STATUS_WAIT_AUDIT, self::STATUS_WAIT_TRANSFER])){
|
||||
return $this->error(null, '提现状态有误');
|
||||
}
|
||||
|
||||
model('member_withdraw')->startTrans();
|
||||
try {
|
||||
$data = array (
|
||||
"status" => self::STATUS_REFUSE,
|
||||
"status_name" => $this->status[self::STATUS_REFUSE],
|
||||
"refuse_reason" => $param[ "refuse_reason" ],
|
||||
"audit_time" => time(),
|
||||
);
|
||||
$result = model("member_withdraw")->update($data, $condition);
|
||||
|
||||
//增加现金余额
|
||||
$member_account = new MemberAccount();
|
||||
$account_res = $member_account->addMemberAccount($info[ 'site_id' ], $info[ 'member_id' ], 'balance_money', $info[ "apply_money" ], 'withdraw', '会员提现申请未通过', '提现申请未通过返还');
|
||||
if ($account_res[ 'code' ] != 0) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $account_res;
|
||||
}
|
||||
//减少提现中余额
|
||||
model("member")->setDec([ [ "member_id", "=", $info[ "member_id" ] ] ], "balance_withdraw_apply", $info[ "apply_money" ]);
|
||||
|
||||
model('member_withdraw')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现转账完成
|
||||
* @param array $param
|
||||
* @return array
|
||||
*/
|
||||
public function transferFinish($param = [])
|
||||
{
|
||||
$condition = [
|
||||
[ 'id', '=', $param[ 'id' ] ],
|
||||
[ 'site_id', '=', $param[ 'site_id' ] ],
|
||||
[ 'status', 'in', [self::STATUS_WAIT_TRANSFER, self::STATUS_IN_PROCESS] ],
|
||||
];
|
||||
$info = model("member_withdraw")->getInfo($condition);
|
||||
if (empty($info)) return $this->error();
|
||||
|
||||
$payment_time = time();
|
||||
model('member_withdraw')->startTrans();
|
||||
try {
|
||||
$data = [
|
||||
'status' => self::STATUS_SUCCESS,
|
||||
'status_name' => $this->status[self::STATUS_SUCCESS],
|
||||
'payment_time' => $payment_time,
|
||||
'certificate' => $param[ 'certificate' ] ?? '',
|
||||
'certificate_remark' => $param[ 'certificate_remark' ] ?? ''
|
||||
];
|
||||
$result = model("member_withdraw")->update($data, $condition);
|
||||
|
||||
//增加已提现余额
|
||||
model("member")->setInc([ [ "member_id", "=", $info[ "member_id" ] ] ], "balance_withdraw", $info[ "apply_money" ]);
|
||||
//减少提现中余额
|
||||
model("member")->setDec([ [ "member_id", "=", $info[ "member_id" ] ] ], "balance_withdraw_apply", $info[ "apply_money" ]);
|
||||
|
||||
model('member_withdraw')->commit();
|
||||
|
||||
$member_info = model("member")->getInfo([ [ "member_id", "=", $info[ "member_id" ] ] ], 'nickname');
|
||||
|
||||
//提现成功发送消息
|
||||
$info[ 'keywords' ] = 'USER_WITHDRAWAL_SUCCESS';
|
||||
$info[ 'payment_time' ] = $payment_time;
|
||||
$info[ 'member_name' ] = $member_info[ 'nickname' ];
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage($info);
|
||||
|
||||
$stat_model = new Stat();
|
||||
$stat_model->switchStat([ 'type' => 'member_withdraw', 'data' => [ 'site_id' => $info[ 'site_id' ], 'id' => $info[ 'id' ] ] ]);
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转账失败
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function transferFail($param)
|
||||
{
|
||||
$id = $param['id'];
|
||||
$site_id = $param['site_id'];
|
||||
$fail_reason = $param['fail_reason'];
|
||||
|
||||
$condition = [
|
||||
[ 'id', '=', $id ],
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'status', 'in', [self::STATUS_WAIT_TRANSFER, self::STATUS_IN_PROCESS] ],
|
||||
];
|
||||
$info = model("member_withdraw")->getInfo($condition);
|
||||
if (empty($info)) return $this->error(null, '提现信息有误');
|
||||
|
||||
model('member_withdraw')->startTrans();
|
||||
try {
|
||||
$data = [
|
||||
'status' => self::STATUS_FAIL,
|
||||
'status_name' => $this->status[self::STATUS_FAIL],
|
||||
'fail_reason' => $fail_reason,
|
||||
];
|
||||
model("member_withdraw")->update($data, $condition);
|
||||
|
||||
//减少提现中余额
|
||||
model("member")->setDec([ [ "member_id", "=", $info[ "member_id" ] ] ], "balance_withdraw_apply", $info[ "apply_money" ]);
|
||||
|
||||
//减少现金余额
|
||||
$member_account = new MemberAccount();
|
||||
$account_res = $member_account->addMemberAccount($site_id, $info[ "member_id" ], 'balance_money', $info[ "apply_money" ], 'withdraw_fail', $id, '会员提现失败退回');
|
||||
if ($account_res[ 'code' ] < 0) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $account_res;
|
||||
}
|
||||
|
||||
model('member_withdraw')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('member_withdraw')->rollback();
|
||||
return $this->error(['file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $e->getMessage()], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转账中
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function transferInProcess($param)
|
||||
{
|
||||
$id = $param['id'];
|
||||
$site_id = $param['site_id'];
|
||||
|
||||
$condition = [
|
||||
[ 'id', '=', $id ],
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'status', '=', self::STATUS_WAIT_TRANSFER ]
|
||||
];
|
||||
$info = model("member_withdraw")->getInfo($condition);
|
||||
if (empty($info)) return $this->error(null, '提现信息有误');
|
||||
|
||||
model("member_withdraw")->update([
|
||||
'status' => self::STATUS_IN_PROCESS,
|
||||
'status_name' => $this->status[self::STATUS_IN_PROCESS],
|
||||
], [['id', '=', $id]]);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberWithdrawInfo($condition, $field = "*")
|
||||
{
|
||||
$info = model('member_withdraw')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现详情
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberWithdrawDetail($condition)
|
||||
{
|
||||
$info = model('member_withdraw')->getInfo($condition, "*");
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现单数
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberWithdrawCount($condition)
|
||||
{
|
||||
$count = model('member_withdraw')->getCount($condition, "id");
|
||||
return $this->success($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现总和
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberWithdrawSum($condition, $field = 'apply_money')
|
||||
{
|
||||
$count = model('member_withdraw')->getSum($condition, $field);
|
||||
return $this->success($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员提现分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @param string $alias
|
||||
* @param string $join
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*',$alias='',$join='')
|
||||
{
|
||||
$list = model('member_withdraw')->pageList($condition, $field, $order, $page, $page_size, $alias, $join, '');
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员提现列表
|
||||
* @param array $where
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @param string $group
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberWithdrawList($where = [], $field = true, $order = '', $alias = 'a', $join = [], $group = '', $limit = null)
|
||||
{
|
||||
$res = model('member_withdraw')->getList($where, $field, $order, $alias, $join, $group, $limit);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现流水号
|
||||
*/
|
||||
private function createWithdrawNo()
|
||||
{
|
||||
$cache = Cache::get("member_withdraw_no" . time());
|
||||
if (empty($cache)) {
|
||||
Cache::set("niutk" . time(), 1000);
|
||||
$cache = Cache::get("member_withdraw_no" . time());
|
||||
} else {
|
||||
$cache = $cache + 1;
|
||||
Cache::set("member_withdraw_no" . time(), $cache);
|
||||
}
|
||||
$no = date('Ymdhis', time()) . rand(1000, 9999) . $cache;
|
||||
return $no;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转账方式
|
||||
* @param int $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getTransferType($site_id = 0, $app_module = 'shop')
|
||||
{
|
||||
$pay_model = new Pay();
|
||||
$transfer_type_list = $pay_model->getTransferType($site_id);
|
||||
$config_result = $this->getConfig($site_id, $app_module);
|
||||
$config = $config_result[ "data" ][ 'value' ];
|
||||
$data = [];
|
||||
$support_type = explode(",", $config[ "transfer_type" ]);
|
||||
foreach ($transfer_type_list as $k => $v) {
|
||||
if (in_array($k, $support_type)) {
|
||||
$data[ $k ] = $v;
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员提现成功通知
|
||||
* @param $data
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function messageUserWithdrawalSuccess($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
$var_parse = array (
|
||||
'username' => $data[ "member_name" ],//会员名
|
||||
'money' => $data[ 'apply_money' ]
|
||||
);
|
||||
|
||||
$data[ "sms_account" ] = $data[ "mobile" ];//手机号
|
||||
$data[ "var_parse" ] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([ [ "member_id", "=", $data[ "member_id" ] ] ]);
|
||||
$member_info = $member_info_result[ "data" ];
|
||||
|
||||
//绑定微信公众号才发送
|
||||
if (!empty($member_info) && !empty($member_info[ "wx_openid" ])) {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data[ "openid" ] = $member_info[ "wx_openid" ];
|
||||
$data[ "template_data" ] = [
|
||||
'amount1' => $data[ 'apply_money' ], // 提现金额
|
||||
'time3' => time_to_date($data[ 'payment_time' ]), // 提现日期
|
||||
];
|
||||
$data[ "page" ] = "";
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info[ "weapp_openid" ])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data[ "openid" ] = $member_info[ "weapp_openid" ];
|
||||
$data[ "template_data" ] = [
|
||||
'amount6' => [
|
||||
'value' => $data[ 'apply_money' ]
|
||||
],
|
||||
'date3' => [
|
||||
'value' => time_to_date(time())
|
||||
]
|
||||
];
|
||||
$data[ "page" ] = "";
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员提现失败通知
|
||||
* @param $data
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function messageUserWithdrawalError($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
$var_parse = array (
|
||||
'username' => $data[ "member_name" ],//会员名
|
||||
'money' => $data[ 'apply_money' ]
|
||||
);
|
||||
|
||||
$data[ "sms_account" ] = $data[ "mobile" ];//手机号
|
||||
$data[ "var_parse" ] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([ [ "member_id", "=", $data[ "member_id" ] ] ]);
|
||||
$member_info = $member_info_result[ "data" ];
|
||||
|
||||
//绑定微信公众号才发送
|
||||
if (!empty($member_info) && !empty($member_info[ "wx_openid" ])) {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data[ "openid" ] = $member_info[ "wx_openid" ];
|
||||
$data[ "template_data" ] = [
|
||||
'keyword1' => time_to_date($data[ 'create_time' ]),
|
||||
'keyword2' => '审核失败',
|
||||
'keyword3' => '会员申请提现',
|
||||
'keyword4' => $data[ 'apply_money' ],
|
||||
];
|
||||
$data[ "page" ] = "";
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info[ "weapp_openid" ])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data[ "openid" ] = $member_info[ "weapp_openid" ];
|
||||
$data[ "template_data" ] = [
|
||||
'amount3' => [
|
||||
'value' => $data[ 'apply_money' ]
|
||||
],
|
||||
'phrase4' => [
|
||||
'value' => '审核失败'
|
||||
],
|
||||
'date2' => [
|
||||
'value' => time_to_date(time())
|
||||
]
|
||||
];
|
||||
$data[ "page" ] = "";
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员申请提现通知,卖家通知
|
||||
* @param $data
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function messageUserWithdrawalApply($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
$var_parse = array (
|
||||
"username" => replaceSpecialChar($data[ "member_name" ]),//会员名
|
||||
"money" => $data[ "apply_money" ],//退款申请金额
|
||||
);
|
||||
// $site_id = $data['site_id'];
|
||||
// $shop_info = model("shop")->getInfo([["site_id", "=", $site_id]], "mobile,email");
|
||||
// $message_data["sms_account"] = $shop_info["mobile"];//手机号
|
||||
$data[ "var_parse" ] = $var_parse;
|
||||
|
||||
$shop_accept_message_model = new ShopAcceptMessage();
|
||||
$list = $shop_accept_message_model->getShopAcceptMessageList()[ 'data' ];
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $v) {
|
||||
$message_data = $data;
|
||||
$message_data[ "sms_account" ] = $v[ "mobile" ];//手机号
|
||||
$sms_model->sendMessage($message_data);
|
||||
|
||||
if ($v[ 'wx_openid' ] != "") {
|
||||
|
||||
$wechat_model = new WechatMessage();
|
||||
$data[ "openid" ] = $v[ 'wx_openid' ];
|
||||
$data[ "template_data" ] = [
|
||||
'thing3' => replaceSpecialChar($data[ "member_name" ]), // 客户名称
|
||||
'amount6' => $data[ "apply_money" ], // 提现金额
|
||||
'time8' => time_to_date($data[ 'apply_time' ]) // 提现时间
|
||||
];
|
||||
|
||||
$data[ "page" ] = "";
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function exportWithdraw($condition, $order)
|
||||
{
|
||||
try {
|
||||
$file_name = date('Y年m月d日-余额提现', time()) . '.csv';
|
||||
// $file_name = date('YmdHis').'.csv';//csv文件名
|
||||
//通过分批次执行数据导出(防止内存超出配置设置的)
|
||||
set_time_limit(0);
|
||||
ini_set('memory_limit', '256M');
|
||||
//设置header头
|
||||
header('Content-Description: File Transfer');
|
||||
header('Content-Type: application/vnd.ms-excel');
|
||||
header('Content-Disposition: attachment; filename="' . $file_name . '"');
|
||||
header('Expires: 0');
|
||||
header('Cache-Control: must-revalidate');
|
||||
header('Pragma: public');
|
||||
//打开php数据输入缓冲区
|
||||
$fp = fopen('php://output', 'a');
|
||||
// fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); // 添加 BOM
|
||||
$heade = [ '会员账号', '提现方式', '申请提现金额', '提现手续费', '实际转账金额', '提现状态', '申请时间', '银行名称', '收款账号', '真实姓名', '手机号' ];
|
||||
//将数据编码转换成GBK格式
|
||||
mb_convert_variables('GBK', 'UTF-8', $heade);
|
||||
//将数据格式化为CSV格式并写入到output流中
|
||||
fputcsv($fp, $heade);
|
||||
//写入第一行表头
|
||||
Db::name('member_withdraw')->where($condition)->order($order)->chunk(500, function($item_list) use ($fp) {
|
||||
//写入导出信息
|
||||
foreach ($item_list as $k => $item_v) {
|
||||
$temp_data = [
|
||||
$item_v[ 'member_name' ] . "\t",
|
||||
$item_v[ 'transfer_type_name' ] . "\t",
|
||||
(float) $item_v[ 'apply_money' ] . "\t",
|
||||
(float) $item_v[ 'service_money' ] . "\t",
|
||||
(float) $item_v[ 'money' ] . "\t",
|
||||
$item_v[ 'status_name' ] . "\t",
|
||||
time_to_date($item_v[ 'apply_time' ]) . "\t",
|
||||
$item_v[ 'bank_name' ] . "\t",
|
||||
$item_v[ 'account_number' ] . "\t",
|
||||
$item_v[ 'realname' ] . "\t",
|
||||
$item_v[ 'mobile' ] . "\t",
|
||||
];
|
||||
mb_convert_variables('GBK', 'UTF-8', $temp_data);
|
||||
fputcsv($fp, $temp_data);
|
||||
//将已经存储到csv中的变量数据销毁,释放内存
|
||||
unset($item_v);
|
||||
}
|
||||
unset($item_list);
|
||||
});
|
||||
|
||||
//关闭句柄
|
||||
fclose($fp);
|
||||
die;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $this->error([], $e->getMessage() . $e->getFile() . $e->getLine());
|
||||
}
|
||||
}
|
||||
}
|
||||
355
app/model/message/Message.php
Executable file
355
app/model/message/Message.php
Executable file
@@ -0,0 +1,355 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\message;
|
||||
|
||||
use addon\wechat\model\Wechat;
|
||||
use app\model\BaseModel;
|
||||
use app\model\shop\Shop;
|
||||
use app\model\system\Site;
|
||||
use think\facade\Db;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 消息管理类
|
||||
*/
|
||||
class Message extends BaseModel
|
||||
{
|
||||
|
||||
/********************************************************************* 平台消息类型 start *********************************************************************************/
|
||||
/**
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
function editMessage($data, $condition)
|
||||
{
|
||||
|
||||
$count = model('message')->getCount($condition);
|
||||
if ($count > 0) {
|
||||
$res = model('message')->update($data, $condition);
|
||||
} else {
|
||||
$res = model('message')->add($data);
|
||||
}
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑微信模板消息是否启动
|
||||
* @param $is_open
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyMessageWechatIsOpen($is_open, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$keywords = $check_condition['keywords'] ?? '';
|
||||
if ($keywords === '') {
|
||||
return $this->error('', 'REQUEST_KEYWORDS');
|
||||
}
|
||||
|
||||
$data = array(
|
||||
"wechat_is_open" => $is_open
|
||||
);
|
||||
$res = model('message')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息模本信息
|
||||
* @param $site_id
|
||||
* @param $keywords
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageInfo($site_id, $keywords, $field = "*")
|
||||
{
|
||||
$info = model("message_template")->getInfo([['keywords', '=', $keywords]], $field);
|
||||
if (!empty($info)) {
|
||||
$info["message_json_array"] = empty($info["message_json"]) ? [] : json_decode($info["message_json"], true);//消息配置
|
||||
$info["sms_json_array"] = event('SmsTemplateInfo', ['keywords' => $keywords, 'site_id' => $site_id], true);//短信配置
|
||||
$info["wechat_json_array"] = empty($info["wechat_json"]) ? [] : json_decode($info["wechat_json"], true);//微信模板消息配置
|
||||
$info["weapp_json_array"] = empty($info["weapp_json"]) ? [] : json_decode($info["weapp_json"], true);//微信小程序订阅消息配置
|
||||
$info["aliapp_json_array"] = empty($info["aliapp_json"]) ? [] : json_decode($info["aliapp_json"], true);//支付宝小程序订阅消息配置
|
||||
$message = model('message')->getInfo([['keywords', '=', $keywords], ['site_id', '=', $site_id]]);
|
||||
|
||||
if (empty($message)) {
|
||||
$data = [
|
||||
'keywords' => $keywords,
|
||||
'site_id' => $site_id,
|
||||
'sms_is_open' => 0,
|
||||
];
|
||||
model('message')->add($data);
|
||||
$message = model('message')->getInfo([['keywords', '=', $keywords], ['site_id', '=', $site_id]]);
|
||||
}
|
||||
$info = array_merge($info, $message);
|
||||
}
|
||||
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取消息详情
|
||||
* @param $site_id
|
||||
* @param $keywords
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageDetail($site_id, $keywords, $field = "*")
|
||||
{
|
||||
$message = model('message')->getInfo([['keywords', '=', $keywords], ['site_id', '=', $site_id]], $field);
|
||||
if (empty($message)) {
|
||||
$data = [
|
||||
'keywords' => $keywords,
|
||||
'site_id' => $site_id,
|
||||
'sms_is_open' => 0
|
||||
];
|
||||
model('message')->add($data);
|
||||
|
||||
$message = model('message')->getInfo([['keywords', '=', $keywords], ['site_id', '=', $site_id]], $field);
|
||||
return $this->success($message);
|
||||
}
|
||||
return $this->success($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息列表
|
||||
* @param int $site_id
|
||||
* @param int $message_type
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageList($site_id = 0, $message_type = 1, $order = '', $limit = null)
|
||||
{
|
||||
$field = 'nmt.id, nmt.addon, nmt.keywords, nmt.title, nmt.message_type, nmt.message_json, nmt.sms_addon, nmt.sms_json, nmt.sms_content, nmt.wechat_json, nmt.aliapp_json
|
||||
, nmt.weapp_json, nmt.aliapp_json, nmt.support_type, nmt.remark, nm.sms_is_open, nm.wechat_is_open, nm.wechat_template_id, nm.weapp_is_open, nm.weapp_template_id, nm.aliapp_is_open, nm.aliapp_template_id';
|
||||
switch ($message_type) {
|
||||
case 1:
|
||||
$order = Db::raw("field(nmt.keywords,'REGISTER_CODE','LOGIN_CODE','SET_PASSWORD','FIND_PASSWORD','MEMBER_BIND',
|
||||
'USER_CANCEL_SUCCESS','USER_CANCEL_FAIL','OFFLINEPAY_AUDIT_REFUSE','ORDER_URGE_PAYMENT','ORDER_CLOSE','ORDER_PAY',
|
||||
'ORDER_DELIVERY','ORDER_COMPLETE','ORDER_VERIFY_OUT_TIME','VERIFY_CODE_EXPIRE','VERIFY',
|
||||
'ORDER_REFUND_AGREE','ORDER_REFUND_REFUSE','USER_WITHDRAWAL_SUCCESS','USER_BALANCE_CHANGE_NOTICE'
|
||||
,'COMMISSION_GRANT','FENXIAO_WITHDRAWAL_SUCCESS','FENXIAO_WITHDRAWAL_ERROR','BARGAIN_COMPLETE', 'PINTUAN_COMPLETE', 'PINTUAN_FAIL', 'SECKILL_START')");
|
||||
break;
|
||||
case 2:
|
||||
$order = Db::raw("field(nmt.keywords,'OFFLINEPAY_WAIT_AUDIT','BUYER_PAY','BUYER_ORDER_COMPLETE','BUYER_REFUND','BUYER_DELIVERY_REFUND',
|
||||
'USER_WITHDRAWAL_APPLY','FENXIAO_WITHDRAWAL_APPLY','USER_CANCEL_APPLY')");
|
||||
break;
|
||||
}
|
||||
$join = [
|
||||
['message nm', 'nmt.keywords = nm.keywords', 'left']
|
||||
];
|
||||
$list = model('message_template')->getList([["nmt.message_type", "=", $message_type]], $field, $order, 'nmt', $join, '', $limit);
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $k => $v) {
|
||||
$list[$k]['support_type'] = explode(',', $v['support_type']);
|
||||
$list[$k]['sms_is_open'] = $v['sms_is_open'] == null ? 0 : $v['sms_is_open'];
|
||||
|
||||
$list[$k]['wechat_is_open'] = $v['wechat_is_open'] == null ? 0 : $v['wechat_is_open'];
|
||||
|
||||
$list[$k]['weapp_is_open'] = $v['weapp_is_open'] == null ? 0 : $v['weapp_is_open'];
|
||||
|
||||
$list[$k]['aliapp_is_open'] = $v['aliapp_is_open'] == null ? 0 : $v['aliapp_is_open'];
|
||||
}
|
||||
}
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息分页列表
|
||||
* @param array $condition
|
||||
* @param int $site_id
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMessagePageList($condition = [], $site_id = 0, $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('message_template')->pageList($condition, $field, $order, $page, $page_size);
|
||||
if ($site_id > 0) {
|
||||
if (!empty($list['list'])) {
|
||||
foreach ($list['list'] as $k => $v) {
|
||||
$message_info = model('message')->getInfo([["keywords", "=", $v['keywords']], ['site_id', '=', $site_id]], 'wechat_is_open,wechat_template_id');
|
||||
$list['list'][$k]['wechat_is_open'] = $message_info == null ? 0 : $message_info['wechat_is_open'];
|
||||
$list['list'][$k]['wechat_template_id'] = $message_info == null ? 0 : $message_info['wechat_template_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信模板消息id
|
||||
* @param string $keywords
|
||||
* @param $site_id
|
||||
* @param int $wechat_is_open
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getWechatTemplateNo(string $keywords, $site_id, $wechat_is_open = 0)
|
||||
{
|
||||
$keyword = explode(',', $keywords);
|
||||
$wechat = new Wechat($site_id);
|
||||
|
||||
if ($wechat_is_open == 1) {
|
||||
// 启用
|
||||
foreach ($keyword as $item) {
|
||||
$shop_message = model('message')->getInfo([['keywords', '=', $item], ["site_id", "=", $site_id]], 'wechat_template_id');
|
||||
$data = [
|
||||
'wechat_is_open' => $wechat_is_open,
|
||||
'site_id' => $site_id,
|
||||
'keywords' => $item,
|
||||
];
|
||||
$template_info = model('message_template')->getInfo([['keywords', '=', $item], ['wechat_json', '<>', '']], 'wechat_json');
|
||||
|
||||
// 开启时没有模板则进行添加
|
||||
if (!empty($shop_message)) {
|
||||
if (empty($shop_message['wechat_template_id'])) {
|
||||
if (!empty($template_info)) {
|
||||
$template = json_decode($template_info['wechat_json'], true);
|
||||
$res = $wechat->getTemplateId($template['template_id_short'], $template['keyword_name_list'] ?? []);
|
||||
if (isset($res['errcode']) && $res['errcode'] == 0) {
|
||||
$data['wechat_template_id'] = $res['template_id'];
|
||||
} else {
|
||||
return $this->error($res, $res['errmsg']);
|
||||
}
|
||||
}
|
||||
}
|
||||
model('message')->update($data, [['keywords', '=', $item], ["site_id", "=", $site_id]]);
|
||||
} else {
|
||||
if (!empty($template_info)) {
|
||||
$template = json_decode($template_info['wechat_json'], true);
|
||||
$res = $wechat->getTemplateId($template['template_id_short'], $template['keyword_name_list'] ?? []);
|
||||
if (isset($res['errcode']) && $res['errcode'] == 0) {
|
||||
$data['wechat_template_id'] = $res['template_id'];
|
||||
} else {
|
||||
return $this->error($res, $res['errmsg']);
|
||||
}
|
||||
}
|
||||
model('message')->add($data);
|
||||
}
|
||||
}
|
||||
} else if ($wechat_is_open == 0) {
|
||||
// 关闭
|
||||
foreach ($keyword as $item) {
|
||||
$shop_message = model('message')->getInfo([['keywords', '=', $item], ["site_id", "=", $site_id]], 'wechat_template_id');
|
||||
if (!empty($shop_message)) {
|
||||
$update_data = ['wechat_is_open' => $wechat_is_open];
|
||||
if ($shop_message['wechat_template_id']) {
|
||||
$res = $wechat->deleteTemplate($shop_message['wechat_template_id']);
|
||||
if (isset($res['errcode']) && $res['errcode'] == 0) {
|
||||
$update_data['wechat_template_id'] = '';
|
||||
} else {
|
||||
return $this->error($res, $res['errmsg']);
|
||||
}
|
||||
}
|
||||
|
||||
model('message')->update($update_data, [['keywords', '=', $item], ["site_id", "=", $site_id]]);
|
||||
} else {
|
||||
model('message')->add([
|
||||
'site_id' => $site_id,
|
||||
'keywords' => $item,
|
||||
'wechat_is_open' => $wechat_is_open
|
||||
]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 获取
|
||||
$list = model('message_template')->getList([['keywords', 'in', $keyword], ['wechat_json', '<>', '']], 'keywords,wechat_json');
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $item) {
|
||||
$template = json_decode($item['wechat_json'], true);
|
||||
$res = $wechat->getTemplateId($template['template_id_short'], $template['keyword_name_list'] ?? []);
|
||||
if (isset($res['errcode']) && $res['errcode'] != 0) return $this->error($res, $res['errmsg']);
|
||||
|
||||
$shop_message = model('message')->getInfo([['keywords', '=', $item['keywords']], ["site_id", "=", $site_id]], 'wechat_template_id');
|
||||
|
||||
if (!empty($shop_message)) {
|
||||
// 存在则清空模板,防止重复
|
||||
if ($shop_message['wechat_template_id']) {
|
||||
$wechat->deleteTemplate($shop_message['wechat_template_id']);
|
||||
}
|
||||
model('message')->update(['wechat_template_id' => $res['template_id']], [['keywords', '=', $item['keywords']], ["site_id", "=", $site_id]]);
|
||||
} else {
|
||||
model('message')->add([
|
||||
'site_id' => $site_id,
|
||||
'keywords' => $item['keywords'],
|
||||
'wechat_template_id' => $res['template_id']
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/********************************************************************* 平台消息类型 end *********************************************************************************/
|
||||
|
||||
/**
|
||||
* 消息发送
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function sendMessage($param)
|
||||
{
|
||||
try {
|
||||
|
||||
$keywords = $param["keywords"];
|
||||
$site_id = $param["site_id"];
|
||||
$message_info_result = $this->getMessageInfo($site_id, $keywords);
|
||||
$message_info = $message_info_result["data"];
|
||||
$param["message_info"] = $message_info;
|
||||
$shop_info = [];
|
||||
if ($site_id > 0) {
|
||||
$shop_model = new Shop();
|
||||
$shop_info_result = $shop_model->getShopInfo([['site_id', '=', $site_id]]);
|
||||
|
||||
$site_model = new Site();
|
||||
$site_info = $site_model->getSiteInfo([['site_id', '=', $site_id]]);
|
||||
$shop_info = array_merge($shop_info_result["data"], $site_info['data']);
|
||||
}
|
||||
$param["site_info"] = $shop_info;
|
||||
// $result = event("SendMessageTemplate", $param, true);//匹配消息模板 并发送
|
||||
|
||||
$result = checkQueue($param, function ($params) {
|
||||
$send_type = $params['type'] ?? 'later';
|
||||
// 如果为验证码同步发送
|
||||
if ($send_type == 'code') {
|
||||
$result = event('SendMessageTemplate', $params, true);//匹配消息模板 并发送
|
||||
return $result;
|
||||
} else {
|
||||
|
||||
// 是够延时发送
|
||||
if (isset($params['later']) && !empty($params['later'])) {
|
||||
Queue::later((time() + $params['later']), 'Sendmessage', $params);
|
||||
} else {
|
||||
Queue::push('Sendmessage', $params);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
}, function ($params) {
|
||||
$result = event('SendMessageTemplate', $params, true);//匹配消息模板 并发送
|
||||
return $result;
|
||||
});
|
||||
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
281
app/model/message/MessageTemplate.php
Executable file
281
app/model/message/MessageTemplate.php
Executable file
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\message;
|
||||
|
||||
use app\model\system\Site;
|
||||
use think\facade\Cache;
|
||||
use app\model\BaseModel;
|
||||
use addon\wechat\model\Wechat;
|
||||
|
||||
/**
|
||||
* 消息管理类
|
||||
*/
|
||||
class MessageTemplate extends BaseModel
|
||||
{
|
||||
|
||||
/********************************************************************* 平台消息类型 start *********************************************************************************/
|
||||
|
||||
/**
|
||||
* 编辑消息
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
function editMessageTemplate($data, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$keywords = $check_condition['keywords'] ?? '';
|
||||
if ($keywords === '') {
|
||||
return $this->error('', 'REQUEST_KEYWORDS');
|
||||
}
|
||||
|
||||
Cache::tag("message_template")->clear();
|
||||
$res = model('message_template')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑邮箱是否启动
|
||||
* @param $is_open
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyMessageTemplateEmailIsOpen($is_open, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$keywords = $check_condition['keywords'] ?? '';
|
||||
if ($keywords === '') {
|
||||
return $this->error('', 'REQUEST_KEYWORDS');
|
||||
}
|
||||
Cache::tag("message_template")->clear();
|
||||
$data = array (
|
||||
"email_is_open" => $is_open
|
||||
);
|
||||
$res = model('message_template')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑短信消息是否启动
|
||||
* @param $is_open
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyMessageTemplateSmsIsOpen($is_open, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$keywords = $check_condition['keywords'] ?? '';
|
||||
if ($keywords === '') {
|
||||
return $this->error('', 'REQUEST_KEYWORDS');
|
||||
}
|
||||
Cache::tag("message_template")->clear();
|
||||
$data = array (
|
||||
"sms_is_open" => $is_open
|
||||
);
|
||||
$res = model('message_template')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑微信模板消息是否启动
|
||||
* @param $is_open
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyMessageTemplateWechatIsOpen($is_open, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$keywords = $check_condition['keywords'] ?? '';
|
||||
if ($keywords === '') {
|
||||
return $this->error('', 'REQUEST_KEYWORDS');
|
||||
}
|
||||
Cache::tag("message_template")->clear();
|
||||
$data = array (
|
||||
"wechat_is_open" => $is_open
|
||||
);
|
||||
$res = model('message_template')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑微信小程序消息是否启动
|
||||
* @param $is_open
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyMessageTemplateWeappIsOpen($is_open, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$keywords = $check_condition['keywords'] ?? '';
|
||||
if ($keywords === '') {
|
||||
return $this->error('', 'REQUEST_KEYWORDS');
|
||||
}
|
||||
Cache::tag("message_template")->clear();
|
||||
$data = array (
|
||||
"weapp_is_open" => $is_open
|
||||
);
|
||||
$res = model('message_template')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑阿里小程序消息是否启动
|
||||
* @param $is_open
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function modifyMessageTemplateAliappIsOpen($is_open, $condition)
|
||||
{
|
||||
$check_condition = array_column($condition, 2, 0);
|
||||
$keywords = $check_condition['keywords'] ?? '';
|
||||
if ($keywords === '') {
|
||||
return $this->error('', 'REQUEST_KEYWORDS');
|
||||
}
|
||||
Cache::tag("message_template")->clear();
|
||||
$data = array (
|
||||
"aliapp_is_open" => $is_open
|
||||
);
|
||||
$res = model('message_template')->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息详情
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $ailas
|
||||
* @param null $join
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageTemplateInfo($condition, $field = "*", $ailas = 'a', $join = null)
|
||||
{
|
||||
$info = model("message_template")->getInfo($condition, $field, $ailas, $join);
|
||||
if (!empty($info)) {
|
||||
$info[ "message_json_array" ] = empty($info[ "message_json" ]) ? [] : json_decode($info[ "message_json" ], true);//消息配置
|
||||
$info[ "sms_json_array" ] = empty($info[ "sms_json" ]) ? [] : json_decode($info[ "sms_json" ], true);//短信配置
|
||||
$info[ "wechat_json_array" ] = empty($info[ "wechat_json" ]) ? [] : json_decode($info[ "wechat_json" ], true);//微信公众号配置
|
||||
$info[ "weapp_json_array" ] = empty($info[ "weapp_json" ]) ? [] : json_decode($info[ "weapp_json" ], true);//微信小程序配置
|
||||
$info[ "aliapp_json_array" ] = empty($info[ "aliapp_json" ]) ? [] : json_decode($info[ "aliapp_json" ], true);//阿里配置
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息列表
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageTemplateList($condition = [], $field = true, $order = '', $limit = null)
|
||||
{
|
||||
$list = model('message_template')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
foreach ($list as $k => $v) {
|
||||
$list[ $k ][ 'support_type' ] = explode(',', $v[ 'support_type' ]);
|
||||
}
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getMessageTemplatePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('message_template')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 【没有用到】获取微信模板消息id
|
||||
* @param string $keywords
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
// public function getWechatTemplateNo(string $keywords)
|
||||
// {
|
||||
// $keyword = explode(',', $keywords);
|
||||
// $list = model('message_template')->getList([ [ 'keywords', 'in', $keyword ], [ 'wechat_json', '<>', '' ] ], 'keywords,wechat_json');
|
||||
// if (!empty($list)) {
|
||||
// $wechat = new Wechat();
|
||||
// foreach ($list as $item) {
|
||||
// $template_info = json_decode($item[ 'wechat_json' ], true);
|
||||
// $res = $wechat->getTemplateId($template_info[ 'template_id_short' ], $template_info[ 'keyword_name_list' ]);
|
||||
// if (isset($res[ 'errcode' ]) && $res[ 'errcode' ] == 0) {
|
||||
// $template_info[ 'template_id' ] = $res[ 'template_id' ];
|
||||
// model('message_template')->update([ 'wechat_json' => json_encode($template_info, JSON_UNESCAPED_UNICODE) ], [ 'keywords' => $item[ 'keywords' ] ]);
|
||||
// } else {
|
||||
// return $this->error($res, $res[ 'errmsg' ]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Cache::clear('message_template');
|
||||
// return $this->success();
|
||||
// }
|
||||
|
||||
/********************************************************************* 平台消息类型 end *********************************************************************************/
|
||||
|
||||
/**
|
||||
* 消息发送
|
||||
* @param $param
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function sendMessageTemplate($param)
|
||||
{
|
||||
$keywords = $param[ "keywords" ];
|
||||
$site_id = $param[ "site_id" ];
|
||||
$condition = [
|
||||
[ "mt.keywords", "=", $keywords ],
|
||||
[ "mt.site_id", "=", $site_id ]
|
||||
];
|
||||
$field = 'mt.*,m.addon, m.title, m.message_type, m.message_json, m.sms_addon, m.sms_json, m.sms_content, m.wechat_is_open, m.wechat_json, m.weapp_is_open, m.weapp_json, m.aliapp_is_open, m.aliapp_json, m.support_type';
|
||||
$join = [
|
||||
[ 'message_template m', 'm.keywords = mt.keywords', 'left' ]
|
||||
];
|
||||
$message_template_info = $this->getMessageTemplateInfo($condition, $field, 'mt', $join)[ 'data' ];
|
||||
|
||||
if (empty($message_template_info)) return $this->error('', 'NOT_SET_SMS_TEMPLATE');
|
||||
|
||||
$param[ "message_template_info" ] = $message_template_info;
|
||||
|
||||
//平台配置信息
|
||||
$site_model = new Site();
|
||||
$shop_info = $site_model->getSiteInfo([ [ 'site_id', '=', $site_id ] ], 'site_name,logo,seo_keywords,seo_description, create_time');
|
||||
$param[ "site_name" ] = $shop_info[ "data" ][ "site_name" ];
|
||||
$result = event("SendMessageTemplate", $param, true);//匹配消息模板 并发送
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
184
app/model/message/Sms.php
Executable file
184
app/model/message/Sms.php
Executable file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\message;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use overtrue\EasySms\Strategies\OrderStrategy;
|
||||
|
||||
/**
|
||||
* 短信管理类
|
||||
*/
|
||||
class Sms extends BaseModel
|
||||
{
|
||||
public $config = [
|
||||
// HTTP 请求的超时时间(秒)
|
||||
'timeout' => 5.0,
|
||||
// 默认发送配置
|
||||
'default' => [
|
||||
// 网关调用策略,默认:顺序调用
|
||||
'strategy' => OrderStrategy::class,
|
||||
'gateways' => [],
|
||||
],
|
||||
];
|
||||
|
||||
/********************************************************************* 短信类型 start *********************************************************************************/
|
||||
|
||||
/**
|
||||
* 获取短信类型
|
||||
* @return array
|
||||
*/
|
||||
public function getSmsType($site_id = 1)
|
||||
{
|
||||
$res = event('SmsType', ['site_id' => $site_id]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取短信编辑地址
|
||||
* @return array
|
||||
*/
|
||||
public function doEditSmsMessage()
|
||||
{
|
||||
$res = event('DoEditSmsMessage', []);
|
||||
return $this->success($res);
|
||||
}
|
||||
/********************************************************************* 短信类型 end *********************************************************************************/
|
||||
|
||||
/********************************************************************* 短信发送记录 start *********************************************************************************/
|
||||
/**
|
||||
* 添加短信记录
|
||||
* @param $data
|
||||
* @return array|int|string
|
||||
*/
|
||||
public function addSmsRecords($data)
|
||||
{
|
||||
$res = model("message_sms_records")->add($data);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信记录编辑
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array|int
|
||||
*/
|
||||
public function editSmsRecords($data, $condition)
|
||||
{
|
||||
$res = model("message_sms_records")->update($data, $condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/*
|
||||
* 删除短信记录
|
||||
*/
|
||||
public function deleteSmsRecords($condition)
|
||||
{
|
||||
$res = model("message_sms_records")->delete($condition);
|
||||
if ($res === false) {
|
||||
return $this->error('', 'UNKNOW_ERROR');
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信记录分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getSmsRecordsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = '*')
|
||||
{
|
||||
$list = model('message_sms_records')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
/********************************************************************* 短信发送记录 end *********************************************************************************/
|
||||
|
||||
/**
|
||||
* 短信发送
|
||||
* @param array $param
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function sendMessage($param = [])
|
||||
{
|
||||
try {
|
||||
$support_type = $param["support_type"] ?? [];//支持的消息发送方式,优先级最高
|
||||
//验证是否支持短信发送
|
||||
if (!empty($support_type) && !in_array("sms", $support_type))
|
||||
return $this->success();
|
||||
|
||||
$message_info = $param["message_info"];
|
||||
|
||||
//短信是否开启
|
||||
if ($message_info["sms_is_open"] == 0) return $this->error('', '商家未启用该短信!');
|
||||
//手机号是否为空
|
||||
if (empty($param["sms_account"])) return $this->error('', '手机号不能为空!');
|
||||
|
||||
$result = event("SendSms", $param, true);
|
||||
if (empty($result)) {
|
||||
$result = $this->error([], "EMPTY_SMS_TYPE");
|
||||
}
|
||||
|
||||
//增加短信记录
|
||||
$status = $result["code"] >= 0 ? 1 : 0;
|
||||
$send_time = '';
|
||||
$message_result = "发送成功";
|
||||
$addon_name = "";
|
||||
$addon = "";
|
||||
|
||||
if ($result["code"] >= 0) {
|
||||
$send_time = time();
|
||||
$addon = $result["data"]["addon"];
|
||||
$addon_name = $result["data"]["addon_name"];
|
||||
$return_result = $this->success([], "SMS_SUCCESS");
|
||||
} else {
|
||||
$message_result = $result["message"] ?? '';
|
||||
$return_result = $this->error([], $message_result);
|
||||
}
|
||||
|
||||
if (!empty($result["data"])) {
|
||||
$content = $result["data"]["content"] ?? '';
|
||||
} else {
|
||||
$content = '';
|
||||
}
|
||||
$var_parse = $param["var_parse"];
|
||||
$records_data = [
|
||||
"account" => $param["sms_account"],
|
||||
"status" => $status,
|
||||
"addon" => $addon,
|
||||
"addon_name" => $addon_name,
|
||||
"content" => $content,
|
||||
"var_parse" => json_encode($var_parse, JSON_UNESCAPED_UNICODE),
|
||||
"keywords" => $param["keywords"],
|
||||
"create_time" => time(),
|
||||
"send_time" => $send_time,
|
||||
"result" => $message_result,
|
||||
"keywords_name" => $message_info["title"],
|
||||
"site_id" => $param['site_id'],
|
||||
"sys_uid" => $param['site_info']['sys_uid'] ?? 0,
|
||||
'site_name' => $param['site_info']['site_name'] ?? ''
|
||||
];
|
||||
$this->addSmsRecords($records_data);
|
||||
return $return_result;
|
||||
} catch (\Exception $e) {
|
||||
return $this->error(['file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $e->getMessage()], "SMS_FAIL");
|
||||
}
|
||||
}
|
||||
}
|
||||
275
app/model/order/Config.php
Executable file
275
app/model/order/Config.php
Executable file
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\system\Document;
|
||||
|
||||
/**
|
||||
* 订单交易设置
|
||||
*/
|
||||
class Config extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 获取订单事件时间设置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderEventTimeConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_EVENT_TIME_CONFIG']]);
|
||||
if (empty($res['data']['value'])) {
|
||||
$res['data']['value'] = [
|
||||
'auto_close' => 30,//订单未付款自动关闭时间 数字 单位(分钟)
|
||||
'auto_take_delivery' => 14,//订单发货后自动收货时间 数字 单位(天)
|
||||
'auto_complete' => 7,//订单收货后自动完成时间 数字 单位(天)
|
||||
'after_sales_time' => 0,//订单完成后可维权时间 数字 单位(天)
|
||||
'invoice_status' => 0,//发票状态(0关闭 1开启)
|
||||
'invoice_rate' => 0,//发票比率(0关闭 1开启)
|
||||
'invoice_content' => '',//发内容(0关闭 1开启)
|
||||
'invoice_money' => 0,//发票运费(0关闭 1开启)
|
||||
];
|
||||
}
|
||||
$res['data']['value']['invoice_type'] = $res['data']['value']['invoice_type'] ?? '1,2';
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 专用于订单事件相关的配置
|
||||
* @return void
|
||||
*/
|
||||
public static function getOrderConfig($site_id = 1){
|
||||
$config_model = new Config();
|
||||
$order_config = $config_model->getOrderEventTimeConfig($site_id)['data'] ?? [];
|
||||
return $order_config['value'] ?? [];
|
||||
}
|
||||
/**
|
||||
* 设置订单事件时间
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setOrderEventTimeConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->setConfig($data, '订单事件时间设置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_EVENT_TIME_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单返积分设置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderBackPointConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_BACK_POINT_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置订单返积分
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setOrderBackPointConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->setConfig($data, '订单返积分设置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_BACK_POINT_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单评价设置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderEvaluateConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_EVALUATE_CONFIG']]);
|
||||
if (empty($res['data']['value'])) {
|
||||
$res['data']['value'] = [
|
||||
'evaluate_status' => 1,//订单评价状态(0关闭 1开启)
|
||||
'evaluate_show' => 1,//显示评价(0关闭 1开启)
|
||||
'evaluate_audit' => 1,//评价审核状态(0关闭 1开启)
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置订单评价设置
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setOrderEvaluateConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->setConfig($data, '订单事件时间设置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_EVALUATE_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置余额支付配置
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setBalanceConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->setConfig($data, '余额支付配置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'BALANCE_SHOW_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取余额支付配置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getBalanceConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'BALANCE_SHOW_CONFIG']]);
|
||||
if (empty($res['data']['value'])) {
|
||||
$res['data']['value'] = [
|
||||
'balance_show' => 1 //余额支付配置(0关闭 1开启)
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置订单提醒配置
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setOrderRemindConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->setConfig($data, '订单提醒配置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_REMIND_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单提醒配置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderRemindConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_REMIND_CONFIG']]);
|
||||
$res['data']['value'] = assignData($res['data']['value'], [
|
||||
'order_pay_audio' => 'public/static/audio/order_pay_remind.mp3',
|
||||
'cashier_order_pay_audio' => 'public/static/audio/cashier_order_pay_remind.mp3',
|
||||
]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单退款配置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderRefundConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_REFUND_CONFIG']]);
|
||||
$res['data']['value'] = assignData($res['data']['value'], [
|
||||
'reason_type' => "未按约定时间发货\n拍错/多拍/不喜欢\n协商一致退款\n其他",
|
||||
'auto_refund'=> $res['data']['value']['auto_refund'] ?? 0
|
||||
]);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置订单退款配置
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setOrderRefundConfig($data, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->setConfig($data, '订单退款配置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_REFUND_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单核销设置
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
* @param $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setOrderVerifyConfig($data, $site_id, $app_module)
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
return $config->setConfig($data, '核销到期提醒', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_VERIFY_CONFIG']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单核销设置
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderVerifyConfig($site_id, $app_module = 'shop')
|
||||
{
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'ORDER_VERIFY_CONFIG']]);
|
||||
if (empty($res['data']['value'])) {
|
||||
$res['data']['value'] = [
|
||||
'order_verify_time_out' => 1,//核销临期提醒时间
|
||||
];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册协议
|
||||
* @param $title
|
||||
* @param $content
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function setTransactionDocument($title, $content, $site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
return $document->setDocument($title, $content, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['document_key', '=', 'TRANSACTION_AGREEMENT']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询注册协议
|
||||
* @param $site_id
|
||||
* @param string $app_module
|
||||
* @return array
|
||||
*/
|
||||
public function getTransactionDocument($site_id, $app_module = 'shop')
|
||||
{
|
||||
$document = new Document();
|
||||
return $document->getDocument([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['document_key', '=', 'TRANSACTION_AGREEMENT']]);
|
||||
}
|
||||
}
|
||||
460
app/model/order/LocalOrder.php
Executable file
460
app/model/order/LocalOrder.php
Executable file
@@ -0,0 +1,460 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\express\LocalPackage;
|
||||
use app\model\message\Message;
|
||||
use app\model\system\Pay as PayModel;
|
||||
use Exception;
|
||||
use think\facade\Db;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 外卖订单
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class LocalOrder extends OrderCommon
|
||||
{
|
||||
/*****************************************************************************************订单基础状态(其他使用)********************************/
|
||||
// 订单待付款
|
||||
public const ORDER_CREATE = 0;
|
||||
|
||||
// 订单已支付(待发货)
|
||||
public const ORDER_PAY = 1;
|
||||
|
||||
// 订单备货中
|
||||
public const ORDER_PENDING_DELIVERY = 2;
|
||||
|
||||
// 订单已发货(配货)
|
||||
public const ORDER_DELIVERY = 3;
|
||||
|
||||
// 订单已收货
|
||||
public const ORDER_TAKE_DELIVERY = 4;
|
||||
|
||||
// 订单已结算完成
|
||||
public const ORDER_COMPLETE = 10;
|
||||
|
||||
// 订单已关闭
|
||||
public const ORDER_CLOSE = -1;
|
||||
|
||||
/***********************************************************************************订单项 配送状态**************************************************/
|
||||
// 待发货
|
||||
public const DELIVERY_WAIT = 0;
|
||||
|
||||
// 已发货
|
||||
public const DELIVERY_DOING = 1;
|
||||
|
||||
// 已收货
|
||||
public const DELIVERY_FINISH = 2;
|
||||
|
||||
// 订单类型
|
||||
public $order_type = 3;
|
||||
|
||||
/**
|
||||
* 基础订单状态(不同类型的订单可以不使用这些状态,但是不能冲突)
|
||||
* @var array
|
||||
*/
|
||||
public $order_status = [
|
||||
self::ORDER_CREATE => [
|
||||
'status' => self::ORDER_CREATE,
|
||||
'name' => '待支付',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAddressUpdate',
|
||||
'title' => '修改地址',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAdjustMoney',
|
||||
'title' => '调整价格',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderPay',
|
||||
'title' => '支付',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_PAY => [
|
||||
'status' => self::ORDER_PAY,
|
||||
'name' => '待发货',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-send.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderLocalDelivery',
|
||||
'title' => '发货',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAddressUpdate',
|
||||
'title' => '修改地址',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_DELIVERY => [
|
||||
'status' => self::ORDER_DELIVERY,
|
||||
'name' => '已发货',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-receive.png',
|
||||
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'takeDelivery',
|
||||
'title' => '确认收货',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'memberTakeDelivery',
|
||||
'title' => '确认收货',
|
||||
'color' => ''
|
||||
],
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_TAKE_DELIVERY => [
|
||||
'status' => self::ORDER_TAKE_DELIVERY,
|
||||
'name' => '已收货',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_COMPLETE => [
|
||||
'status' => self::ORDER_COMPLETE,
|
||||
'name' => '已完成',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_CLOSE => [
|
||||
'status' => self::ORDER_CLOSE,
|
||||
'name' => '已关闭',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-close.png',
|
||||
'action' => [
|
||||
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
];
|
||||
|
||||
// 配送状态
|
||||
public $delivery_status = [
|
||||
self::DELIVERY_WAIT => [
|
||||
'status' => self::DELIVERY_WAIT,
|
||||
'name' => '待发货',
|
||||
'color' => ''
|
||||
],
|
||||
self::DELIVERY_DOING => [
|
||||
'status' => self::DELIVERY_DOING,
|
||||
'name' => '已发货',
|
||||
'color' => ''
|
||||
],
|
||||
self::DELIVERY_FINISH => [
|
||||
'status' => self::DELIVERY_FINISH,
|
||||
'name' => '已收货',
|
||||
'color' => ''
|
||||
]
|
||||
];
|
||||
|
||||
/**
|
||||
* 订单支付
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function orderPay($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$pay_type = $data['pay_type'];
|
||||
$log_data = $data['log_data'] ?? [];
|
||||
$member_info = $data['member_info'];
|
||||
if ($order_info['order_status'] != 0) {
|
||||
return $this->error();
|
||||
}
|
||||
$condition = [
|
||||
['order_id', '=', $order_info['order_id']],
|
||||
['order_status', '=', self::ORDER_CREATE],
|
||||
];
|
||||
$pay_type_list = $this->getPayType();
|
||||
$data = [
|
||||
'order_status' => self::ORDER_PAY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PAY]['name'],
|
||||
'pay_status' => 1,
|
||||
'order_status_action' => json_encode($this->order_status[self::ORDER_PAY], JSON_UNESCAPED_UNICODE),
|
||||
'pay_time' => time(),
|
||||
'is_enable_refund' => 1,
|
||||
'pay_type' => $pay_type,
|
||||
'pay_type_name' => $pay_type_list[$pay_type]
|
||||
];
|
||||
|
||||
//记录订单日志 start
|
||||
$action = '商家对订单进行了线下支付';
|
||||
//获取用户信息
|
||||
if (empty($log_data)) {
|
||||
$log_data = [
|
||||
'uid' => $order_info['member_id'],
|
||||
'nick_name' => $member_info['nickname'],
|
||||
'action_way' => 1
|
||||
];
|
||||
$action = '买家支付了订单';
|
||||
}
|
||||
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_info['order_id'],
|
||||
'action' => $action,
|
||||
'order_status' => self::ORDER_PAY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PAY]['name']
|
||||
]);
|
||||
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
//记录订单日志 end
|
||||
|
||||
$result = model('order')->update($data, $condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单项发货(物流)
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function orderGoodsDelivery($param)
|
||||
{
|
||||
|
||||
$delivery_no = $param['delivery_no'] ?? '';//物流单号
|
||||
$delivery_type = $param['delivery_type'] ?? 'default';
|
||||
$order_id = $param['order_id'] ?? 0;
|
||||
$site_id = $param['site_id'];
|
||||
$store_id = $param['store_id'] ?? 0;
|
||||
|
||||
$condition = [
|
||||
['site_id', '=', $site_id],
|
||||
['order_id', '=', $order_id],
|
||||
['refund_status', '<>', OrderRefundDict::REFUND_COMPLETE]
|
||||
];
|
||||
if ($store_id) $condition[] = ['store_id', '=', $store_id];
|
||||
|
||||
$order_goods_list = model('order_goods')->getList($condition, '*');
|
||||
if (empty($order_goods_list)) {
|
||||
return $this->error('', '发货货物不可为空!');
|
||||
}
|
||||
|
||||
$condition_refund[] = ['', 'exp', Db::raw('(refund_status=' . OrderRefundDict::REFUND_APPLY . ' or refund_status=' . OrderRefundDict::REFUND_CONFIRM . ') and site_id=' . $site_id . ' and order_id=' . $order_id)];
|
||||
$order_refund_goods_list = model('order_goods')->getList($condition_refund, 'order_goods_id');
|
||||
// 已退款的订单项不可发货
|
||||
if ($order_refund_goods_list) {
|
||||
return $this->error([], 'ORDER_GOODS_IN_REFUND');
|
||||
}
|
||||
|
||||
model('order_goods')->startTrans();
|
||||
try {
|
||||
|
||||
$order_goods_id_array = [];
|
||||
$goods_id_array = [];
|
||||
$member_id = 0;
|
||||
foreach ($order_goods_list as $order_goods_info) {
|
||||
$order_goods_id_array[] = $order_goods_info['order_goods_id'];
|
||||
$order_id = $order_goods_info['order_id'];
|
||||
$member_id = $order_goods_info['member_id'];
|
||||
$goods_id_array[] = $order_goods_info['sku_id'] . ':' . number_format($order_goods_info['num']) . ':' . $order_goods_info['sku_name'] . ':' . $order_goods_info['sku_image'];
|
||||
$data = ['delivery_status' => self::DELIVERY_DOING, 'delivery_status_name' => $this->delivery_status[self::DELIVERY_DOING]['name']];
|
||||
if (!empty($delivery_no)) {
|
||||
$data['delivery_no'] = $delivery_no;
|
||||
}
|
||||
model('order_goods')->update($data, [
|
||||
['order_goods_id', '=', $order_goods_info['order_goods_id']],
|
||||
['delivery_status', '=', self::DELIVERY_WAIT]
|
||||
]);
|
||||
}
|
||||
|
||||
$order_stock_model = new OrderStock();
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]]);
|
||||
$stock_result = $order_stock_model->decOrderStock([
|
||||
'store_id' => $order_info['store_id'],
|
||||
'site_id' => $order_info['site_id'],
|
||||
'goods_sku_list' => $order_goods_list,
|
||||
'user_info' => $param['user_info'] ?? []
|
||||
]);
|
||||
if ($stock_result['code'] < 0) {
|
||||
model('order_goods')->rollback();
|
||||
return $stock_result;
|
||||
}
|
||||
// 创建包裹
|
||||
$order_common_model = new OrderCommon();
|
||||
$lock_result = $order_common_model->verifyOrderLock($order_id);
|
||||
if ($lock_result['code'] < 0) {
|
||||
model('order_goods')->rollback();
|
||||
return $lock_result;
|
||||
}
|
||||
|
||||
$local_delivery_model = new LocalPackage();
|
||||
$delivery_data = [
|
||||
'order_id' => $order_id,
|
||||
'order_goods_id_array' => $order_goods_id_array,
|
||||
'goods_id_array' => $goods_id_array,
|
||||
'goods_array' => $goods_id_array,
|
||||
'site_id' => $site_id,
|
||||
'delivery_no' => $delivery_no,
|
||||
'member_id' => $member_id,
|
||||
'delivery_type' => $delivery_type,
|
||||
'deliverer' => $param['deliverer'],
|
||||
'deliverer_mobile' => $param['deliverer_mobile'],
|
||||
];
|
||||
$local_delivery_model->delivery($delivery_data);
|
||||
//检测整体, 订单中订单项是否全部发放完毕
|
||||
$res = $this->orderCommonDelivery($order_id);
|
||||
|
||||
//发送消息 发给配送员
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'MESSAGE_LOCAL_WAIT_DELIVERY', 'param' => $param, 'site_id' => $param['site_id']]);
|
||||
|
||||
|
||||
model('order_goods')->commit();
|
||||
return $this->success($res);
|
||||
} catch (Exception $e) {
|
||||
model('order_goods')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单发货
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function orderDelivery($order_id)
|
||||
{
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id], ['order_status', '=', self::ORDER_PAY]], '*');
|
||||
if (empty($order_info)) return $this->error([], '订单不存在或已发货!');
|
||||
//统计订单项目
|
||||
$count = model('order_goods')->getCount([['order_id', '=', $order_id], ['delivery_status', '=', self::DELIVERY_WAIT], ['refund_status', '<>', OrderRefundDict::REFUND_COMPLETE]], 'order_goods_id');
|
||||
// $delivery_count = model('order_goods')->getCount([['order_id', '=', $order_id], ['delivery_status', '=', self::DELIVERY_DOING], ['refund_status', '<>', OrderRefundDict::REFUND_COMPLETE]], 'order_goods_id');
|
||||
// if ($count == 0 && $delivery_count > 0) {
|
||||
if ($count == 0) {
|
||||
$site_id = $order_info['site_id'];
|
||||
model('order')->startTrans();
|
||||
try {
|
||||
//修改订单项的配送状态
|
||||
$order_data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'delivery_status' => self::DELIVERY_FINISH,
|
||||
'delivery_status_name' => $this->delivery_status[self::DELIVERY_FINISH]['name'],
|
||||
'order_status_action' => json_encode($this->order_status[self::ORDER_DELIVERY], JSON_UNESCAPED_UNICODE),
|
||||
'delivery_time' => time()
|
||||
];
|
||||
model('order')->update($order_data, [['order_id', '=', $order_id]]);
|
||||
model('order')->commit();
|
||||
//订单自动收货
|
||||
OrderCron::takeDelivery(['order_id' => $order_id, 'site_id' => $site_id]);
|
||||
// 订单发货完成,小程序发货信息录入,视频号,同城配送不需要验证
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_id, 'site_id' => $site_id]);
|
||||
//订单发货消息
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'ORDER_DELIVERY', 'order_id' => $order_id, 'site_id' => $site_id]);
|
||||
return $this->success();
|
||||
} catch (Exception $e) {
|
||||
model('order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单收货
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function orderTakeDelivery($order_id)
|
||||
{
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款完成操作
|
||||
* @param $order_goods_info
|
||||
*/
|
||||
public function refund($order_goods_info)
|
||||
{
|
||||
//是否入库
|
||||
$order_stock_model = new OrderStock();
|
||||
if ($order_goods_info['is_refund_stock'] == 1) {
|
||||
$order_stock_model->incOrderStock($order_goods_info);
|
||||
} else if ($order_goods_info['delivery_status'] == 0) {
|
||||
$order_stock_model->incOrderSaleStock([
|
||||
'store_id' => $order_goods_info['store_id'],
|
||||
'goods_sku_data' => [
|
||||
[
|
||||
'sku_id' => $order_goods_info['sku_id'],
|
||||
'num' => $order_goods_info['num'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param $order_info
|
||||
* @return array
|
||||
*/
|
||||
public function orderDetail($order_info)
|
||||
{
|
||||
$local_package_model = new LocalPackage();
|
||||
$package_info = $local_package_model->package(['order_id' => $order_info['order_id']])['data'] ?? [];
|
||||
$data['package_list'] = $package_info;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
927
app/model/order/Order.php
Executable file
927
app/model/order/Order.php
Executable file
@@ -0,0 +1,927 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use addon\electronicsheet\model\ElectronicsheetDelivery;
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\express\ExpressDelivery;
|
||||
use app\model\express\ExpressPackage;
|
||||
use app\model\message\Message;
|
||||
use app\model\system\Cron;
|
||||
use Exception;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 普通(快递)订单
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class Order extends OrderCommon
|
||||
{
|
||||
|
||||
/*****************************************************************************************订单状态***********************************************/
|
||||
// 订单创建
|
||||
public const ORDER_CREATE = 0;
|
||||
|
||||
// 订单已支付
|
||||
public const ORDER_PAY = 1;
|
||||
|
||||
// 订单备货中
|
||||
public const ORDER_PENDING_DELIVERY = 2;
|
||||
|
||||
// 订单已发货(配货)
|
||||
public const ORDER_DELIVERY = 3;
|
||||
|
||||
// 订单已收货
|
||||
public const ORDER_TAKE_DELIVERY = 4;
|
||||
|
||||
// 订单已结算完成
|
||||
public const ORDER_COMPLETE = 10;
|
||||
|
||||
// 订单已关闭
|
||||
public const ORDER_CLOSE = -1;
|
||||
|
||||
// 订单类型
|
||||
|
||||
public const DELIVERY_WAIT = 0;
|
||||
|
||||
|
||||
/***********************************************************************************订单项 配送状态**************************************************/
|
||||
// 待发货
|
||||
public const DELIVERY_DOING = 1;
|
||||
|
||||
// 已发货
|
||||
public const DELIVERY_FINISH = 2;
|
||||
|
||||
// 已收货
|
||||
public $order_type = 1;
|
||||
|
||||
// 订单状态
|
||||
public $order_status = [
|
||||
self::ORDER_CREATE => [
|
||||
'status' => self::ORDER_CREATE,
|
||||
'name' => '待支付',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAddressUpdate',
|
||||
'title' => '修改地址',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAdjustMoney',
|
||||
'title' => '调整价格',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => '',
|
||||
],
|
||||
[
|
||||
'action' => 'orderPay',
|
||||
'title' => '支付',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_PAY => [
|
||||
'status' => self::ORDER_PAY,
|
||||
'name' => '待发货',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-send.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderDelivery',
|
||||
'title' => '发货',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAddressUpdate',
|
||||
'title' => '修改地址',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_DELIVERY => [
|
||||
'status' => self::ORDER_DELIVERY,
|
||||
'name' => '已发货',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-receive.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'takeDelivery',
|
||||
'title' => '确认收货',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'memberTakeDelivery',
|
||||
'title' => '确认收货',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'trace',
|
||||
'title' => '查看物流',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_TAKE_DELIVERY => [
|
||||
'status' => self::ORDER_TAKE_DELIVERY,
|
||||
'name' => '已收货',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'action' => [],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'trace',
|
||||
'title' => '查看物流',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_COMPLETE => [
|
||||
'status' => self::ORDER_COMPLETE,
|
||||
'name' => '已完成',
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'is_allow_refund' => 1,
|
||||
'action' => [],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'trace',
|
||||
'title' => '查看物流',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_CLOSE => [
|
||||
'status' => self::ORDER_CLOSE,
|
||||
'name' => '已关闭',
|
||||
'icon' => 'public/uniapp/order/order-icon-close.png',
|
||||
|
||||
'is_allow_refund' => 0,
|
||||
'action' => [],
|
||||
'member_action' => [],
|
||||
'color' => ''
|
||||
]
|
||||
];
|
||||
|
||||
/**
|
||||
* 订单状态(发货列表)
|
||||
*/
|
||||
public $delivery_order_status = [
|
||||
self::ORDER_PAY => [
|
||||
'status' => self::ORDER_PAY,
|
||||
'name' => '待发货',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-send.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderDelivery',
|
||||
'title' => '发货',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAddressUpdate',
|
||||
'title' => '修改地址',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [],
|
||||
'color' => ''
|
||||
]
|
||||
];
|
||||
|
||||
// 配送状态
|
||||
public $delivery_status = [
|
||||
self::DELIVERY_WAIT => [
|
||||
'status' => self::DELIVERY_WAIT,
|
||||
'name' => '待发货',
|
||||
'color' => ''
|
||||
],
|
||||
self::DELIVERY_DOING => [
|
||||
'status' => self::DELIVERY_DOING,
|
||||
'name' => '已发货',
|
||||
'color' => ''
|
||||
],
|
||||
self::DELIVERY_FINISH => [
|
||||
'status' => self::DELIVERY_FINISH,
|
||||
'name' => '已收货',
|
||||
'color' => ''
|
||||
]
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 订单支付
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function orderPay($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$member_info = $data['member_info'];
|
||||
$pay_type = $data['pay_type'];
|
||||
|
||||
$pay_type_list = $this->getPayType();
|
||||
if ($order_info['order_status'] != self::ORDER_CREATE) return $this->error([], '当前订单未处于待付款状态,无法支付!');
|
||||
|
||||
$condition = [
|
||||
['order_id', '=', $order_info['order_id']],
|
||||
['order_status', '=', self::ORDER_CREATE],
|
||||
];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_PAY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PAY]['name'],
|
||||
'pay_status' => 1,
|
||||
'order_status_action' => json_encode($this->order_status[self::ORDER_PAY], JSON_UNESCAPED_UNICODE),
|
||||
'refund_status_action' => json_encode(OrderRefundDict::getStatus(OrderRefundDict::REFUND_NOT_APPLY), JSON_UNESCAPED_UNICODE),
|
||||
'pay_time' => time(),
|
||||
'is_enable_refund' => 1,
|
||||
'pay_type' => $pay_type,
|
||||
'pay_type_name' => $pay_type_list[$pay_type]
|
||||
];
|
||||
$result = model('order')->update($data, $condition);
|
||||
//记录订单日志 start
|
||||
$action = '商家对订单进行了线下支付';
|
||||
//获取用户信息
|
||||
if (empty($log_data)) {
|
||||
$log_data = [
|
||||
'uid' => $order_info['member_id'],
|
||||
'nick_name' => $member_info['nickname'],
|
||||
'action_way' => 1
|
||||
];
|
||||
$action = '买家支付了订单';
|
||||
}
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_info['order_id'],
|
||||
'action' => $action,
|
||||
'order_status' => self::ORDER_PAY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PAY]['name']
|
||||
]);
|
||||
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
//记录订单日志 end
|
||||
|
||||
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量订单发货(物流)
|
||||
* @param $param
|
||||
* @param $order_list
|
||||
* @return array|int
|
||||
*/
|
||||
public function orderBatchDelivery($param, $order_list)
|
||||
{
|
||||
model('express_delivery_package')->startTrans();
|
||||
|
||||
try {
|
||||
|
||||
if (empty($order_list)) {
|
||||
return $this->error('', '请先选择要发货的订单!');
|
||||
}
|
||||
|
||||
foreach ($order_list as $v) {
|
||||
$param['order_id'] = $v['order_id'];
|
||||
$param['order_goods_ids'] = '';
|
||||
|
||||
if ($param['type'] == 'electronicsheet') {//电子面单发货
|
||||
|
||||
$addon_is_exit = addon_is_exit('electronicsheet', $param['site_id']);
|
||||
if ($addon_is_exit != 1) {
|
||||
return $this->error('', '电子面单插件不存在!');
|
||||
}
|
||||
|
||||
$electronicsheet_model = new ElectronicsheetDelivery();
|
||||
$result = $electronicsheet_model->delivery($param);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
$param['delivery_no'] = $result['data']['Order']['LogisticCode'];
|
||||
} else {
|
||||
$param['delivery_no'] = $v['delivery_no'];
|
||||
}
|
||||
$result = $this->orderGoodsDelivery($param, 2);
|
||||
if ($result['code'] < 0) {
|
||||
model('express_delivery_package')->rollback();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
model('express_delivery_package')->commit();
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
model('express_delivery_package')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单项发货(物流)
|
||||
* @param $param
|
||||
* @param int $type 1:订单项发货,2:整体发货
|
||||
* @param array $log_data
|
||||
* @return array|int
|
||||
*/
|
||||
public function orderGoodsDelivery($param, $type = 1, $log_data = [])
|
||||
{
|
||||
$param['type'] = $param['type'] ?? 'manual';
|
||||
model('order_goods')->startTrans();
|
||||
try {
|
||||
$order_id = $param['order_id'];
|
||||
$delivery_no = $param['delivery_no']; // 物流单号
|
||||
$delivery_type = $param['delivery_type'];
|
||||
if ($delivery_type == 0) {
|
||||
$express_company_id = 0;
|
||||
} else {
|
||||
$express_company_id = $param['express_company_id'] ?? 0;
|
||||
}
|
||||
$site_id = $param['site_id'];
|
||||
if ($type == 1) {
|
||||
if (empty($param['order_goods_ids']))
|
||||
return $this->error('', '发货货物不可为空!');
|
||||
|
||||
$order_goods_id_array = explode(',', $param['order_goods_ids']);
|
||||
|
||||
} else {
|
||||
$order_goods_id_array = model('order_goods')->getColumn(
|
||||
[
|
||||
['order_id', '=', $order_id],
|
||||
['site_id', '=', $site_id],
|
||||
['delivery_status', '=', self::DELIVERY_WAIT],
|
||||
['refund_status', '<>', OrderRefundDict::REFUND_COMPLETE]
|
||||
],
|
||||
'order_goods_id'
|
||||
);
|
||||
}
|
||||
if (empty($order_goods_id_array))
|
||||
return $this->error('', '发货货物不可为空!');
|
||||
|
||||
$order_id = 0;
|
||||
$member_id = 0;
|
||||
$goods_id_array = [];
|
||||
$order_stock_model = new OrderStock();
|
||||
$stock_sku_list = [];
|
||||
foreach ($order_goods_id_array as $k => $v) {
|
||||
|
||||
$order_goods_info = model('order_goods')->getInfo([['order_goods_id', '=', $v], ['site_id', '=', $site_id]], '*');
|
||||
// 已退款的订单项不可发货
|
||||
if ($order_goods_info['refund_status'] == OrderRefundDict::REFUND_COMPLETE) {
|
||||
model('order_goods')->rollback();
|
||||
return $this->error([], 'ORDER_GOODS_IS_REFUND');
|
||||
}
|
||||
|
||||
if ($order_goods_info['delivery_status'] == self::DELIVERY_DOING) {
|
||||
model('order_goods')->rollback();
|
||||
return $this->error([], 'ORDER_GOODS_IS_DELIVERYED');
|
||||
}
|
||||
$order_goods_info['num'] = numberFormat($order_goods_info['num']);
|
||||
$member_id = $order_goods_info['member_id'];
|
||||
$goods_id_array[] = $order_goods_info['sku_id'] . ':' . $order_goods_info['num'] . ':' . $order_goods_info['sku_name'] . ':' . $order_goods_info['sku_image'];
|
||||
$data = ['delivery_status' => self::DELIVERY_DOING, 'delivery_status_name' => $this->delivery_status[self::DELIVERY_DOING]['name']];
|
||||
if (!empty($delivery_no)) {
|
||||
$data['delivery_no'] = $delivery_no;
|
||||
}
|
||||
$res = model('order_goods')->update($data, [
|
||||
['order_goods_id', '=', $v],
|
||||
['delivery_status', '=', self::DELIVERY_WAIT]
|
||||
]);
|
||||
$order_id = $order_goods_info['order_id'];
|
||||
//实际发货扣除库存
|
||||
$stock_sku_list[] = $order_goods_info;
|
||||
|
||||
}
|
||||
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'store_id,site_id');
|
||||
$stock_result = $order_stock_model->decOrderStock([
|
||||
'store_id' => $order_info['store_id'],
|
||||
'site_id' => $order_info['site_id'],
|
||||
'goods_sku_list' => $stock_sku_list,
|
||||
'user_info' => $param['user_info'] ?? []
|
||||
]);
|
||||
|
||||
if ($stock_result['code'] < 0) {
|
||||
model('order_goods')->rollback();
|
||||
return $stock_result;
|
||||
}
|
||||
|
||||
// 创建包裹
|
||||
$order_common_model = new OrderCommon();
|
||||
$lock_result = $order_common_model->verifyOrderLock($order_id);
|
||||
if ($lock_result['code'] < 0) {
|
||||
model('order_goods')->rollback();
|
||||
return $lock_result;
|
||||
}
|
||||
|
||||
$express_delivery_model = new ExpressDelivery();
|
||||
$delivery_data = [
|
||||
'order_id' => $order_id,
|
||||
'order_goods_id_array' => $order_goods_id_array,
|
||||
'goods_id_array' => $goods_id_array,
|
||||
'goods_array' => $goods_id_array,
|
||||
'site_id' => $site_id,
|
||||
'delivery_no' => $delivery_no,
|
||||
'member_id' => $member_id,
|
||||
'express_company_id' => $express_company_id,
|
||||
'delivery_type' => $delivery_type,
|
||||
'type' => $param['type'],
|
||||
'template_id' => $param['template_id']
|
||||
];
|
||||
|
||||
$delivery_id = $express_delivery_model->delivery($delivery_data);
|
||||
|
||||
// 检测整体, 订单中订单项是否全部发放完毕
|
||||
$res = $this->orderCommonDelivery($order_id, $log_data);
|
||||
if ($res['code'] < 0) {
|
||||
model('order_goods')->rollback();
|
||||
return $res;
|
||||
}
|
||||
|
||||
model('order_goods')->commit();
|
||||
return $this->success($delivery_id);
|
||||
} catch ( Exception $e ) {
|
||||
model('order_goods')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量订单发货(导入excel文件发货)
|
||||
* @param $param
|
||||
* @param $site_id
|
||||
* @param $uid
|
||||
* @return array
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Exception
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
|
||||
*/
|
||||
public function orderFileDelivery($param, $site_id, $uid)
|
||||
{
|
||||
//电子面单插件
|
||||
$addon_is_exit = addon_is_exit('electronicsheet', $site_id);
|
||||
|
||||
$PHPReader = IOFactory::createReader('Xlsx');
|
||||
//载入文件
|
||||
$PHPExcel = $PHPReader->load($param['path']);
|
||||
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
|
||||
$currentSheet = $PHPExcel->getSheet(0);
|
||||
//获取总行数
|
||||
$allRow = $currentSheet->getHighestRow();
|
||||
if ($allRow < 2) {
|
||||
return $this->error('', '导入了一个空文件!');
|
||||
}
|
||||
|
||||
$user_info = model('user')->getInfo([['uid', '=', $uid]], 'username');
|
||||
|
||||
//添加文件上传记录
|
||||
$success_num = $allRow - 1;
|
||||
$error_num = 0;
|
||||
$data = [
|
||||
'site_id' => $site_id,
|
||||
'filename' => $param['filename'],
|
||||
'path' => $param['path'],
|
||||
'order_num' => $allRow - 1,
|
||||
'success_num' => $success_num,
|
||||
'create_time' => time(),
|
||||
'uid' => $uid,
|
||||
'username' => $user_info['username']
|
||||
];
|
||||
$res = model('order_import_file')->add($data);
|
||||
if (!$res) {
|
||||
return $this->error('', '上传文件失败!');
|
||||
}
|
||||
|
||||
model('order_import_file')->startTrans();
|
||||
try {
|
||||
|
||||
for ($i = 2; $i <= $allRow; $i++) {
|
||||
|
||||
$delivery_data = [
|
||||
'type' => '',//发货方式(手动发货、电子面单)
|
||||
'express_company_id' => 0,//物流公司
|
||||
'delivery_type' => 1,//是否需要物流
|
||||
'site_id' => $site_id,
|
||||
'template_id' => 0,//电子面单模板id
|
||||
'delivery_no' => ''
|
||||
];
|
||||
|
||||
//订单编号
|
||||
$order_no = $PHPExcel->getActiveSheet()->getCell('A' . $i)->getValue();
|
||||
$order_no = trim($order_no, ' ');
|
||||
$order_no = preg_replace('/\s+/', '', $order_no);
|
||||
//订单内容
|
||||
$order_name = $PHPExcel->getActiveSheet()->getCell('B' . $i)->getValue();
|
||||
$order_name = trim($order_name, ' ');
|
||||
//发货方式
|
||||
$type = $PHPExcel->getActiveSheet()->getCell('F' . $i)->getValue();
|
||||
$type = trim($type, ' ');
|
||||
//物流公司名称或电子面单名称
|
||||
$name = $PHPExcel->getActiveSheet()->getCell('G' . $i)->getValue();
|
||||
$name = trim($name, ' ');
|
||||
//物流单号
|
||||
$delivery_no = $PHPExcel->getActiveSheet()->getCell('H' . $i)->getValue();
|
||||
$delivery_no = trim($delivery_no, ' ');
|
||||
|
||||
if (empty($type)) {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => '发货方式为空'
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if ($type == '电子面单' && $addon_is_exit == 1) {
|
||||
|
||||
if (empty($name)) {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => '电子面单模板为空'
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
$delivery_data['type'] = 'electronicsheet';
|
||||
$template_id = model('express_electronicsheet')->getValue([['template_name', '=', $name], ['site_id', '=', $site_id]], 'id');
|
||||
if (empty($template_id)) {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => '电子面单模板不存在'
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
$delivery_data['template_id'] = $template_id;
|
||||
} elseif ($type == '电子面单' && $addon_is_exit != 1) {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => '电子面单插件未安装']
|
||||
);
|
||||
continue;
|
||||
} else {
|
||||
$delivery_data['type'] = 'manual';
|
||||
|
||||
if (empty($delivery_no) || empty($name)) {//无需物流
|
||||
$delivery_data['delivery_type'] = 0;
|
||||
} else {
|
||||
$company_id = model('express_company')->getValue([['site_id', '=', $site_id], ['company_name', '=', $name]], 'company_id');
|
||||
if ($company_id == '') {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => '物流公司不存在'
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
$delivery_data['express_company_id'] = $company_id;
|
||||
$delivery_data['delivery_no'] = $delivery_no;
|
||||
}
|
||||
}
|
||||
//获取订单信息
|
||||
$order_info = model('order')->getInfo([['order_no', '=', $order_no], ['site_id', '=', $site_id]], 'order_id,order_status');
|
||||
if (empty($order_info) || $order_info['order_status'] != self::ORDER_PAY) {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => '订单不存在或者已发货'
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
$delivery_data['order_id'] = $order_info['order_id'];
|
||||
$delivery_data['order_goods_ids'] = '';
|
||||
|
||||
if ($delivery_data['type'] == 'electronicsheet') {//电子面单发货
|
||||
|
||||
$electronicsheet_model = new ElectronicsheetDelivery();
|
||||
$result = $electronicsheet_model->delivery($delivery_data);
|
||||
if ($result['code'] < 0) {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => $result['message']
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
$delivery_data['delivery_no'] = $result['data']['Order']['LogisticCode'];
|
||||
}
|
||||
|
||||
$result = $this->orderGoodsDelivery($delivery_data, 2);
|
||||
if ($result['code'] < 0) {
|
||||
$error_num += 1;
|
||||
$success_num -= 1;
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => $success_num, 'error_num' => $error_num], [['id', '=', $res]]);
|
||||
//添加失败记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => -1, 'reason' => $result['message']
|
||||
]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
//添加成功记录
|
||||
model('order_import_file_log')->add(
|
||||
[
|
||||
'site_id' => $site_id, 'file_id' => $res, 'order_no' => $order_no, 'order_name' => $order_name,
|
||||
'status' => 0, 'reason' => ''
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
model('order_import_file')->commit();
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
model('order_import_file')->rollback();
|
||||
//修改数量
|
||||
model('order_import_file')->update(['success_num' => 0, 'error_num' => $allRow - 1], [['id', '=', $res]]);
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单收货
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function orderTakeDelivery($order_id)
|
||||
{
|
||||
//保存物流轨迹信息
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'mobile');
|
||||
$express_package_model = new ExpressPackage();
|
||||
$express_package_model->package([['order_id', '=', $order_id]], $order_info['mobile'], true);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单收货地址修改
|
||||
* @param $param
|
||||
* @param $condition
|
||||
* @param array $log_data
|
||||
* @return array
|
||||
*/
|
||||
public function orderAddressUpdate($param, $condition, $log_data = [])
|
||||
{
|
||||
$province_id = $param['province_id'];
|
||||
$city_id = $param['city_id'];
|
||||
$district_id = $param['district_id'];
|
||||
$community_id = $param['community_id'];
|
||||
$address = $param['address'];
|
||||
$full_address = $param['full_address'];
|
||||
$longitude = $param['longitude'];
|
||||
$latitude = $param['latitude'];
|
||||
$mobile = $param['mobile'];
|
||||
$telephone = $param['telephone'];
|
||||
$name = $param['name'];
|
||||
$data = [
|
||||
'province_id' => $province_id,
|
||||
'city_id' => $city_id,
|
||||
'district_id' => $district_id,
|
||||
'community_id' => $community_id,
|
||||
'address' => $address,
|
||||
'full_address' => $full_address,
|
||||
'longitude' => $longitude,
|
||||
'latitude' => $latitude,
|
||||
'mobile' => $mobile,
|
||||
'telephone' => $telephone,
|
||||
'name' => $name,
|
||||
];
|
||||
$order_info = model('order')->getInfo($condition, '*');
|
||||
$order_status_array = [self::ORDER_PAY, self::ORDER_CREATE];
|
||||
if (!in_array($order_info['order_status'], $order_status_array))
|
||||
return $this->error('', '当前订单状态不可编辑收货地址!');
|
||||
//记录订单日志 start
|
||||
if ($log_data) {
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_status' => $order_info['order_status'],
|
||||
'order_status_name' => $order_info['order_status_name']
|
||||
]);
|
||||
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
}
|
||||
//记录订单日志 end
|
||||
$result = model('order')->update($data, $condition);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款完成操作
|
||||
* @param $order_goods_info
|
||||
*/
|
||||
public function refund($order_goods_info)
|
||||
{
|
||||
//是否入库
|
||||
$order_stock_model = new OrderStock();
|
||||
if ($order_goods_info['is_refund_stock'] == 1) {
|
||||
$order_stock_model->incOrderStock($order_goods_info);
|
||||
}else if($order_goods_info['delivery_status'] == 0){
|
||||
$order_stock_model->incOrderSaleStock([
|
||||
'store_id' => $order_goods_info['store_id'],
|
||||
'goods_sku_data' => [
|
||||
[
|
||||
'sku_id' => $order_goods_info['sku_id'],
|
||||
'num' => $order_goods_info['num'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
//检测订单项是否否全部发放完毕
|
||||
$this->orderDelivery($order_goods_info['order_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单发货
|
||||
* @param $order_id
|
||||
* @param array $log_data
|
||||
* @return array
|
||||
*/
|
||||
public function orderDelivery($order_id, $log_data = [])
|
||||
{
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], '*');
|
||||
$site_id = $order_info['site_id'];
|
||||
// 待发货状态
|
||||
if ($order_info['order_status'] == self::ORDER_PAY) {
|
||||
|
||||
// 统计订单项目
|
||||
$count = model('order_goods')->getCount([['order_id', '=', $order_id], ['delivery_status', '=', self::DELIVERY_WAIT], ['refund_status', '<>', OrderRefundDict::REFUND_COMPLETE]], 'order_goods_id');
|
||||
// $delivery_count = model('order_goods')->getCount([['order_id', '=', $order_id], ['delivery_status', '=', self::DELIVERY_DOING], ['refund_status', '<>', OrderRefundDict::REFUND_COMPLETE]], 'order_goods_id');
|
||||
// if ($count == 0 && $delivery_count > 0) {
|
||||
if ($count == 0) {
|
||||
// 全部发货
|
||||
model('order')->startTrans();
|
||||
try {
|
||||
// 修改订单项的配送状态
|
||||
$order_data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'delivery_status' => self::DELIVERY_FINISH,
|
||||
'delivery_status_name' => $this->delivery_status[self::DELIVERY_FINISH]['name'],
|
||||
'order_status_action' => json_encode($this->order_status[self::ORDER_DELIVERY], JSON_UNESCAPED_UNICODE),
|
||||
'delivery_time' => time()
|
||||
|
||||
];
|
||||
model('order')->update($order_data, [['order_id', '=', $order_id]]);
|
||||
if ($log_data) {
|
||||
// 记录订单日志 start
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_id,
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name']
|
||||
]);
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
//记录订单日志 end
|
||||
}
|
||||
|
||||
//订单自动收货
|
||||
OrderCron::takeDelivery(['order_id' => $order_id, 'site_id' => $site_id]);
|
||||
// 订单发货完成,小程序发货信息录入,视频号
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_id, 'site_id' => $site_id]);
|
||||
|
||||
model('order')->commit();
|
||||
// 订单发货消息
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'ORDER_DELIVERY', 'order_id' => $order_id, 'site_id' => $site_id]);
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
model('order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
if ($log_data) {
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_id,
|
||||
'order_status' => $order_info['order_status'],
|
||||
'order_status_name' => $this->order_status[$order_info['order_status']]['name']
|
||||
]);
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
} else {
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param $order_info
|
||||
* @return array
|
||||
*/
|
||||
public function orderDetail($order_info)
|
||||
{
|
||||
return $this->orderPartDeliveryHandle($order_info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单部分发货处理
|
||||
* @param $order_info
|
||||
* @return array
|
||||
*/
|
||||
public function orderPartDeliveryHandle($order_info)
|
||||
{
|
||||
$res = [];
|
||||
if(isset($order_info['order_status']) && $order_info['order_status'] == self::ORDER_PAY){
|
||||
$is_delivery = false;
|
||||
foreach($order_info['order_goods'] as $val){
|
||||
if($val['delivery_status'] == 1){
|
||||
$is_delivery = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($is_delivery){
|
||||
$res['order_status_name'] = '部分发货';
|
||||
$order_status_action = json_decode($order_info['order_status_action'], true);
|
||||
$order_status_action['member_action'][] = [
|
||||
'action' => 'trace',
|
||||
'title' => '查看物流',
|
||||
'color' => ''
|
||||
];
|
||||
$res['order_status_action'] = json_encode($order_status_action);
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算订单销售额
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderMoneySum($condition = [], $field = 'order_money')
|
||||
{
|
||||
$res = model('order')->getSum($condition, $field);
|
||||
return $this->success($res);
|
||||
}
|
||||
}
|
||||
1646
app/model/order/OrderCommon.php
Executable file
1646
app/model/order/OrderCommon.php
Executable file
File diff suppressed because it is too large
Load Diff
436
app/model/order/OrderCreate.php
Executable file
436
app/model/order/OrderCreate.php
Executable file
@@ -0,0 +1,436 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use addon\store\model\Config as StoreConfig;
|
||||
use app\model\BaseModel;
|
||||
use app\model\goods\Goods;
|
||||
use app\model\system\Pay;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* 订单创建(普通订单)
|
||||
*
|
||||
*/
|
||||
class OrderCreate extends BaseModel
|
||||
{
|
||||
use OrderCreateTool;
|
||||
|
||||
/**
|
||||
* @var int|mixed
|
||||
*/
|
||||
public $jielong_id;
|
||||
public $jielong_info = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->modules = [ 'coupon' ];
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
//读取之前的标识缓存
|
||||
$this->confirm();
|
||||
$error_result = $this->checkError();
|
||||
if ($error_result !== true) {
|
||||
return $error_result;
|
||||
}
|
||||
model('order')->startTrans();
|
||||
try {
|
||||
//订单创建数据
|
||||
$order_insert_data = $this->getOrderInsertData([ 'promotion' ], 'invert');
|
||||
$order_insert_data[ 'store_id' ] = $this->store_id;
|
||||
$order_insert_data[ 'create_time' ] = time();
|
||||
$order_insert_data[ 'is_enable_refund' ] = 0;
|
||||
//订单类型以及状态
|
||||
$this->orderType();
|
||||
$order_insert_data[ 'order_type' ] = $this->order_type[ 'order_type_id' ];
|
||||
$order_insert_data[ 'order_type_name' ] = $this->order_type[ 'order_type_name' ];
|
||||
$order_insert_data[ 'order_status_name' ] = $this->order_type[ 'order_status' ][ 'name' ];
|
||||
$order_insert_data[ 'order_status_action' ] = json_encode($this->order_type[ 'order_status' ], JSON_UNESCAPED_UNICODE);
|
||||
$this->order_id = model('order')->add($order_insert_data);
|
||||
|
||||
|
||||
$order_goods_insert_data = [];
|
||||
//订单项目表
|
||||
foreach ($this->goods_list as $order_goods_v) {
|
||||
$order_goods_insert_data[] = $this->getOrderGoodsInsertData($order_goods_v);
|
||||
}
|
||||
model('order_goods')->addList($order_goods_insert_data);
|
||||
//todo 满减送
|
||||
$this->createManjian();
|
||||
//todo 优惠券(新)
|
||||
$this->useCoupon();
|
||||
//扣除余额
|
||||
$this->useBalance();
|
||||
//扣除抵现积分
|
||||
$this->usePoint();
|
||||
//是否同时开通会员卡
|
||||
$this->createMemberCard();
|
||||
//使用次卡
|
||||
$this->useCard();
|
||||
|
||||
//库存转换处理
|
||||
$this->batchGoodsStockTransform();
|
||||
//库存处理(卡密商品支付后在扣出库存)//todo 可以再商品中设置扣除库存步骤
|
||||
$this->batchDecOrderGoodsStock();
|
||||
model('order')->commit();
|
||||
|
||||
//订单创建后事件
|
||||
$this->orderCreateAfter();
|
||||
//生成整体付费支付单据
|
||||
$pay_model = new Pay();
|
||||
$pay_model->addPay($this->site_id, $this->out_trade_no, $this->pay_type, $this->order_name, $this->order_name, $this->pay_money, '', 'OrderPayNotify', '', $this->order_id, $this->member_id);
|
||||
return $this->success($this->out_trade_no);
|
||||
} catch (Exception $e) {
|
||||
model('order')->rollback();
|
||||
return $this->error([
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'message' => $e->getMessage(),
|
||||
], $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算后的进一步计算(不存缓存,每次都是重新计算)
|
||||
* @return array
|
||||
*/
|
||||
public function confirm()
|
||||
{
|
||||
$order_key = $this->param[ 'order_key' ];
|
||||
$this->getOrderCache($order_key);
|
||||
//初始化地址
|
||||
$this->initMemberAddress();
|
||||
//初始化门店信息
|
||||
$this->initStore();
|
||||
//优惠运费计算
|
||||
$this->shopOrderCalculate();
|
||||
// 会员卡开卡金额
|
||||
$this->calculateMemberCardMoney();
|
||||
//配送计算
|
||||
$this->calculateDelivery();
|
||||
//批量校验配送方式
|
||||
$this->batchCheckDeliveryType();
|
||||
//优惠券活动(采用站点id:coupon_id) **
|
||||
$this->couponPromotion();
|
||||
|
||||
// 积分抵现
|
||||
if ($this->member_account[ 'point' ] > 0 && addon_is_exit('pointcash', $this->site_id)) {
|
||||
$this->getMaxUsablePoint();
|
||||
//计算积分
|
||||
$this->calculatePoint();
|
||||
}
|
||||
//计算发票相关
|
||||
$this->calculateInvoice();
|
||||
//计算余额
|
||||
$this->calculateBalcnce();
|
||||
$this->pay_money = $this->order_money - $this->balance_money;
|
||||
//设置过的商品项信息
|
||||
return get_object_vars($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 待付款订单
|
||||
* @return array
|
||||
*/
|
||||
public function orderPayment()
|
||||
{
|
||||
$this->calculate();
|
||||
$this->getDeliveryData();
|
||||
//todo 这儿认为只有自定义表单会用此钩子
|
||||
|
||||
return get_object_vars($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单计算
|
||||
* @return true
|
||||
*/
|
||||
public function calculate()
|
||||
{
|
||||
$this->initMemberAddress(); //初始化地址
|
||||
$this->initMemberAccount(); //初始化会员账户
|
||||
$this->getRecommendMemberCard();//查询推荐会员卡
|
||||
//初始化门店信息
|
||||
$this->initStore();
|
||||
//商品列表信息
|
||||
$this->getOrderGoodsCalculate();
|
||||
//优惠运费计算
|
||||
$this->shopOrderCalculate();
|
||||
//订单初始项
|
||||
event('OrderPayment', [ 'order_object' => $this ]);
|
||||
//获取发票相关
|
||||
$this->getInovice();
|
||||
//todo 统一检测库存(创建订单操作时扣除库存同理)
|
||||
// 商品限购判断
|
||||
$this->checkLimitPurchase();
|
||||
$this->order_key = create_no();
|
||||
$this->setOrderCache(get_object_vars($this), $this->order_key);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品的计算信息
|
||||
* @return true
|
||||
*/
|
||||
public function getOrderGoodsCalculate()
|
||||
{
|
||||
$this->getCartGoodsList();
|
||||
|
||||
// 会员卡项抵扣
|
||||
$this->getMemberGoodsCardPromotion();
|
||||
//满减优惠
|
||||
$this->manjianPromotion();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取购物车商品列表信息
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function getCartGoodsList()
|
||||
{
|
||||
$cart_ids = $this->param[ 'cart_ids' ] ?? [];
|
||||
$condition = [
|
||||
[ 'ngs.site_id', '=', $this->site_id ],
|
||||
[ 'ngs.is_delete', '=', 0 ],
|
||||
[ 'ngs.goods_state', '=', 1],
|
||||
[ 'ng.sale_channel', 'in', $this->sale_channel],
|
||||
];
|
||||
|
||||
|
||||
//组装商品列表
|
||||
$field = 'ngs.sku_id,ngs.is_limit, ngs.limit_type, ngs.sku_name, ngs.member_price, ngs.is_consume_discount, ngs.discount_config, ngs.discount_method, ngs.sku_no,
|
||||
ngs.price, ngs.discount_price, ngs.cost_price, ngs.stock, ngs.weight, ngs.volume, ngs.sku_image,
|
||||
ngs.site_id, ngs.goods_state, ngs.is_virtual, ngs.supplier_id,ngs.form_id,
|
||||
ngs.is_free_shipping, ngs.shipping_template, ngs.goods_class, ngs.goods_class_name, ngs.goods_id,ns.site_name,ngs.sku_spec_format,ngs.goods_name,ngs.max_buy,ngs.min_buy,ngs.support_trade_type, ngs.is_unify_price,
|
||||
ngs.sale_channel, ngs.sale_store, ng.category_id, ng.is_unify_price, ng.virtual_deliver_type';
|
||||
$alias = 'ngs';
|
||||
$join = [
|
||||
// [
|
||||
// 'goods_sku ngs',
|
||||
// 'ngc.sku_id = ngs.sku_id',
|
||||
// 'inner'
|
||||
// ],
|
||||
[
|
||||
'site ns',
|
||||
'ngs.site_id = ns.site_id',
|
||||
'inner'
|
||||
],
|
||||
[
|
||||
'goods ng',
|
||||
'ngs.goods_id = ng.goods_id',
|
||||
'inner',
|
||||
],
|
||||
];
|
||||
|
||||
//门店独立售价处理
|
||||
if (addon_is_exit('store', $this->site_id)) {
|
||||
$store_config = (new StoreConfig())->getStoreBusinessConfig($this->site_id)['data']['value'];
|
||||
$join[] = ['store_goods_sku sgs', 'sgs.sku_id = ngs.sku_id and sgs.store_id = '.$this->store_id, 'left'];
|
||||
if($store_config['store_business'] == 'store'){
|
||||
$field = str_replace('ngs.price', 'IFNULL(IF(ng.is_unify_price = 1,ngs.price,sgs.price), ngs.price) as price', $field);
|
||||
$field = str_replace('ngs.discount_price', 'IFNULL(IF(ng.is_unify_price = 1,ngs.discount_price,sgs.price), ngs.discount_price) as discount_price', $field);
|
||||
}
|
||||
//与门店相关且门店的库存为独立库存则成本价取门店商品的成本价
|
||||
$delivery_type = $this->param['delivery']['delivery_type'] ?? 'express';
|
||||
if($store_config['store_business'] == 'shop' && $delivery_type == 'express'){
|
||||
$delivery_store_id = 0;
|
||||
}else{
|
||||
$delivery_store_id = $this->param['delivery']['store_id'] ?? 0;
|
||||
}
|
||||
if(!empty($delivery_store_id)){
|
||||
$store_model = new \app\model\store\Store();
|
||||
$delivery_store_stock_type = $store_model->getStoreInfo([['store_id', '=', $delivery_store_id]], 'stock_type')['data']['stock_type'];
|
||||
if($delivery_store_stock_type == 'store'){
|
||||
$field = str_replace('ngs.cost_price', 'IFNULL(sgs.cost_price, ngs.cost_price) as cost_price', $field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->jielong_id = $this->param[ 'jielong_id' ] ?? 0;
|
||||
if (!empty($cart_ids)) {
|
||||
$this->cart_ids = $cart_ids;
|
||||
|
||||
$field .= ',ngc.member_id, ngc.sku_id, ngc.num';
|
||||
if ($this->jielong_id > 0) {
|
||||
$join[] = [
|
||||
'promotion_jielong_cart ngc',
|
||||
'ngc.sku_id = ngs.sku_id',
|
||||
'inner'
|
||||
];
|
||||
|
||||
} else {
|
||||
$field .= ',ngc.form_data';
|
||||
$join[] = [
|
||||
'goods_cart ngc',
|
||||
'ngc.sku_id = ngs.sku_id',
|
||||
'inner'
|
||||
];
|
||||
}
|
||||
$condition[] = [ 'ngc.cart_id', 'in', $cart_ids ];
|
||||
$condition[] = [ 'ngc.member_id', '=', $this->member_id ];
|
||||
} else {
|
||||
$sku_id = $this->param[ 'sku_id' ];
|
||||
$num = $this->param[ 'num' ];
|
||||
$field .= ',' . $num . ' as num';
|
||||
$condition[] = [
|
||||
'ngs.sku_id', '=', $sku_id
|
||||
];
|
||||
}
|
||||
|
||||
//只有存在收银插件的情况下才会进行吧此项业务(todo 钩子实现)
|
||||
$delivery_array = $this->param[ 'delivery' ] ?? [];
|
||||
$goods_list = model('goods_sku')->getList($condition, $field, '', $alias, $join);
|
||||
|
||||
if (!empty($goods_list)) {
|
||||
$this->goods_list = $goods_list;
|
||||
$available_store_ids = [];
|
||||
$available_store_is_all = true;
|
||||
//计算商品扩展业务相关项
|
||||
event('OrderGoodsCalculate', [ 'order_object' => $this ], true);
|
||||
foreach ($this->goods_list as $k => &$v) {
|
||||
if($v['sale_store'] != 'all'){
|
||||
$available_store_is_all = false;
|
||||
$sale_store = explode(',', trim($v['sale_store'], ','));
|
||||
if(empty($available_store_ids)){
|
||||
$available_store_ids = $sale_store;
|
||||
}else{
|
||||
$available_store_ids = array_intersect($available_store_ids, $sale_store);
|
||||
}
|
||||
}
|
||||
$this->site_name = $v[ 'site_name' ];
|
||||
$goods_id = $v[ 'goods_id' ];
|
||||
if ($v[ 'num' ] < 1) {
|
||||
$this->setError(1, '商品项的购买数量不能小于1');
|
||||
}
|
||||
$this->is_virtual = $v[ 'is_virtual' ];
|
||||
if($v['goods_class'] == \app\dict\goods\GoodsDict::virtual && $v['virtual_deliver_type'] == 'artificial_deliver'){
|
||||
$this->is_virtual_delivery = 1;
|
||||
}
|
||||
$goods_item = $v;
|
||||
$goods_item[ 'delivery' ] = $delivery_array;
|
||||
$goods_item[ 'store_id' ] = $this->store_id;
|
||||
// //整理创建订单时商品的相关价格库存, 有错误的话还回记录错误['error' => [''error_code' => 1, 'message' => '']]
|
||||
// $goods_calculate = event('OrderGoodsCalculate', $goods_item, true);
|
||||
// if (!empty($goods_calculate)) {
|
||||
// if ($goods_calculate['code'] < 0) {
|
||||
// return $goods_calculate;
|
||||
// }
|
||||
// $v = $goods_calculate['data'];
|
||||
// }
|
||||
//todo 要核验 当前门店 当前产品是否已经配置上架
|
||||
//todo 未上架未配置 要记录原因,并且不能生成订单
|
||||
$price = $this->getGoodsPrice($v)[ 'data' ] ?? 0;
|
||||
|
||||
// 是否存在推荐会员卡
|
||||
if (!empty($this->recommend_member_card)) {
|
||||
//todo 当前业务门店不存在,所以这儿的价格不作处理
|
||||
$card_price_info = $this->getMemberCardGoodsPrice($v)[ 'data' ] ?? [];
|
||||
$card_price = $card_price_info[ 'price' ];
|
||||
if ($card_price > 0 && $card_price < $price) {
|
||||
$this->recommend_member_card[ 'discount_money' ] += ($price - $card_price) * $v[ 'num' ];
|
||||
if ($this->recommend_member_card_data[ 'is_open_card' ]) $price = $card_price;//todo 这儿应该把discount_price 也同步一下的
|
||||
}
|
||||
}
|
||||
|
||||
$v[ 'form_data' ] = !empty($v[ 'form_data' ]) ? json_decode($v[ 'form_data' ], true) : '';
|
||||
$v[ 'price' ] = $price;
|
||||
//实现要注意 discount_price 字段只存在显示作用
|
||||
if ($this->store_id > 0) {
|
||||
$v[ 'discount_price' ] = $price;
|
||||
}
|
||||
$v[ 'goods_money' ] = $price * $v[ 'num' ];
|
||||
$v[ 'real_goods_money' ] = $v[ 'goods_money' ];
|
||||
$v[ 'coupon_money' ] = 0; //优惠券金额
|
||||
$v[ 'promotion_money' ] = 0; //优惠金额
|
||||
|
||||
// $this->goods_list[] = $v;
|
||||
$order_name = $this->order_name ?? '';
|
||||
if ($order_name) {
|
||||
$len = strlen_mb($order_name);
|
||||
if ($len > 200) {
|
||||
$this->order_name = str_sub($order_name, 200);
|
||||
} else {
|
||||
$this->order_name = string_split($order_name, ',', $v[ 'sku_name' ]);
|
||||
}
|
||||
} else {
|
||||
$this->order_name = string_split('', ',', $v[ 'sku_name' ]);
|
||||
}
|
||||
$this->goods_num += $v[ 'num' ];
|
||||
$this->goods_money += $v[ 'goods_money' ];
|
||||
//以;隔开的商品项
|
||||
$goods_list_str = $this->goods_list_str ?? '';
|
||||
if ($goods_list_str) {
|
||||
$this->goods_list_str = $goods_list_str . ';' . $v[ 'sku_id' ] . ':' . $v[ 'num' ];
|
||||
} else {
|
||||
$this->goods_list_str = $v[ 'sku_id' ] . ':' . $v[ 'num' ];
|
||||
}
|
||||
|
||||
// 商品限购处理
|
||||
if (isset($this->limit_purchase[ 'goods_' . $goods_id ])) {
|
||||
$this->limit_purchase[ 'goods_' . $goods_id ][ 'num' ] += $v[ 'num' ];
|
||||
} else {
|
||||
$this->limit_purchase[ 'goods_' . $goods_id ] = [
|
||||
'goods_id' => $v[ 'goods_id' ],
|
||||
'goods_name' => $v[ 'sku_name' ],
|
||||
'num' => $v[ 'num' ],
|
||||
'is_limit' => $v[ 'is_limit' ],
|
||||
'limit_type' => $v[ 'limit_type' ],
|
||||
'max_buy' => $v[ 'max_buy' ],
|
||||
'min_buy' => $v[ 'min_buy' ]
|
||||
];
|
||||
}
|
||||
//校验商品和配送方式
|
||||
// $this->checkDeliveryType($v);
|
||||
//有错误也会导致商品无法购买
|
||||
$item_error = $v[ 'error' ] ?? [];
|
||||
if (!empty($item_error)) {
|
||||
$this->setError(1, $item_error[ 'message' ]);
|
||||
}
|
||||
}
|
||||
if($available_store_is_all === false){
|
||||
$this->available_store_ids = join(',', $available_store_ids);
|
||||
}
|
||||
}else{
|
||||
$this->setError(1, '您要购买的商品已删除或已下架');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取店铺订单计算
|
||||
* @return true
|
||||
*/
|
||||
public function shopOrderCalculate()
|
||||
{
|
||||
//满额包邮插件
|
||||
$this->freeShippingCalculate();
|
||||
//会员等级包邮权益
|
||||
$this->memberLevelCalculate();
|
||||
//重新计算订单总额
|
||||
$this->getOrderMoney();
|
||||
//理论上是多余的操作
|
||||
if ($this->order_money < 0) {
|
||||
$this->order_money = 0;
|
||||
}
|
||||
//总结计算
|
||||
$this->pay_money = $this->order_money;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
631
app/model/order/OrderCreateTool.php
Executable file
631
app/model/order/OrderCreateTool.php
Executable file
@@ -0,0 +1,631 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use addon\pointcash\model\Config as PointCashConfig;
|
||||
use app\dict\order\OrderDict;
|
||||
use app\dict\order\OrderGoodsDict;
|
||||
use app\dict\order\OrderPayDict;
|
||||
use app\model\express\Config as ExpressConfig;
|
||||
use app\model\order\ordercreate\CommonTool;
|
||||
use app\model\order\ordercreate\DeliveryTool;
|
||||
use app\model\order\ordercreate\GoodsTool;
|
||||
use app\model\order\ordercreate\PromotionTool;
|
||||
use app\model\system\Cron;
|
||||
use app\model\system\Pay;
|
||||
use extend\exception\OrderException;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 订单创建 可调用的工具类
|
||||
*/
|
||||
trait OrderCreateTool
|
||||
{
|
||||
use CommonTool;
|
||||
use PromotionTool;
|
||||
use DeliveryTool;
|
||||
use GoodsTool;
|
||||
|
||||
public $site_id = 1;//站点id
|
||||
public $site_info;
|
||||
public $store_info;
|
||||
public $store_id = 0;//门店id
|
||||
public $available_store_ids = 'all';//可以用的门店
|
||||
public $cart_ids = [];
|
||||
public $order_no;
|
||||
public $out_trade_no;
|
||||
public $order_key;
|
||||
public $config = [];//配置
|
||||
public $coupon_id = 0;//优惠券
|
||||
public $coupon_money = 0;//优惠券金额
|
||||
public $order_type = [];//订单类型(array)
|
||||
public $is_point = 0;//是否使用积分
|
||||
public $member_id = 0;//会员id
|
||||
public $member_account = [];//会员账户
|
||||
public $member_level = [];//会员等级
|
||||
public $invoice = [];//发票
|
||||
public $delivery = [];
|
||||
public $is_limit_start_money = true;//是否限制起送金额
|
||||
public $is_free_delivery = false;//是否免邮
|
||||
public $buyer = [];//买家信息
|
||||
public $param = [];//参数
|
||||
public $order_data = [];
|
||||
public $goods_num = 0;//商品数量
|
||||
public $limit_purchase = [];//限购
|
||||
public $promotion = [];//活动优惠...
|
||||
public $manjian_rule_list = [];//满减优惠规则
|
||||
public $promotion_type;
|
||||
public $promotion_type_name;
|
||||
public $site_name = '';
|
||||
public $goods_money = 0; //商品金额
|
||||
public $delivery_money = 0; //配送费用
|
||||
public $adjust_money = 0; //调整金额
|
||||
public $invoice_money = 0; //发票费用
|
||||
public $promotion_money = 0; //优惠金额
|
||||
public $order_money = 0; //订单金额
|
||||
public $pay_money = 0; //支付总价
|
||||
public $is_virtual = 0; //是否是虚拟类订单
|
||||
public $is_virtual_delivery = 0; //虚拟商品是否发货
|
||||
public $order_name = ''; //订单详情
|
||||
public $goods_list_str = '';
|
||||
public $error = 0; //是否有错误
|
||||
public $error_msg = ''; //错误描述
|
||||
public $error_show = false;
|
||||
public $pay_type = OrderPayDict::online_pay;
|
||||
public $invoice_delivery_money = 0;
|
||||
public $balance_money = 0;
|
||||
public $member_balance_money = 0; //会员账户余额(计算过程中会逐次减少)
|
||||
|
||||
public $recommend_member_card = []; // 推荐会员卡
|
||||
|
||||
public $recommend_member_card_data = [];//推荐会员卡配置规格
|
||||
public $member_card_money = 0; // 会员卡开卡金额
|
||||
|
||||
public $member_goods_card = [];
|
||||
public $goods_list = [];//商品项数据结构
|
||||
public $order_id;
|
||||
public $point_money = 0; // 积分抵现金额
|
||||
public $order_goods_list = [];//订单项列表
|
||||
|
||||
public $order_object_data;
|
||||
|
||||
public $modules = [];
|
||||
public $log = [];
|
||||
public $order_from_type = 'order';
|
||||
public $sale_channel = 'all,online,offline';
|
||||
public $is_check_buyer_ask_delivery_time = true;//特殊业务比如预售不做时间检测
|
||||
|
||||
/**
|
||||
* 设置错误,优先级
|
||||
* @param $error
|
||||
* @param $error_msg
|
||||
* @param string $priority 报错优先级 0 创建时提示 1 计算时提示
|
||||
*/
|
||||
public function setError($error, $error_msg, $priority = '0')
|
||||
{
|
||||
$this->error = $error;
|
||||
$this->error_msg = $error_msg;
|
||||
if ($priority == 1) {
|
||||
$this->error_show = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验错误
|
||||
* @return array|true
|
||||
*/
|
||||
public function checkError()
|
||||
{
|
||||
if ($this->error > 0) {
|
||||
return $this->error(['error_code' => $this->error], $this->error_msg);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单创建缓存
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOrderCache($key)
|
||||
{
|
||||
$order_cache = Cache::get($key, []);
|
||||
if (empty($order_cache)) throw new OrderException('订单已过期');
|
||||
foreach ($order_cache as $k => $v) {
|
||||
$this->$k = $v;
|
||||
}
|
||||
return $order_cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置订单缓存
|
||||
* @param $data
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function setOrderCache($data, $key = '')
|
||||
{
|
||||
if (empty($key)) {
|
||||
$key = create_no();
|
||||
}
|
||||
unset($data['param']);
|
||||
Cache::tag('order_cache')->set($key, $data, 30000);
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除订单缓存
|
||||
* @param $key
|
||||
* @return true
|
||||
*/
|
||||
public function deleteOrderCache($key = '')
|
||||
{
|
||||
Cache::delete($key ?: $this->order_key);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义传入参数
|
||||
* @param $param
|
||||
* @return $this
|
||||
*/
|
||||
public function setParam($param)
|
||||
{
|
||||
$this->param = $param;
|
||||
$this->member_id = $this->param['member_id'];
|
||||
$this->site_id = $this->param['site_id'];
|
||||
$this->order_from = $this->param['order_from'] ?? '';//订单来源
|
||||
$this->order_from_name = $this->param['order_from_name'] ?? '';//订单来源名称
|
||||
$this->sale_channel = $this->param['sale_channel'] ?? 'all,online,offline';//销售渠道
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新计算订单总额
|
||||
* @return void
|
||||
*/
|
||||
public function getOrderMoney()
|
||||
{
|
||||
$this->order_money = round($this->goods_money + $this->delivery_money - $this->promotion_money + $this->member_card_money - $this->point_money - $this->coupon_money + $this->invoice_money + $this->invoice_delivery_money,2);
|
||||
$this->order_money = max($this->order_money, 0);
|
||||
return $this->order_money;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加订单自动关闭事件
|
||||
*/
|
||||
public function addOrderCronClose()
|
||||
{
|
||||
//计算订单自动关闭时间
|
||||
$order_config = $this->config('order');
|
||||
$now_time = time();
|
||||
if ($order_config['auto_close'] > 0) {
|
||||
$execute_time = $now_time + $order_config['auto_close'] * 60; //自动关闭时间
|
||||
$cron_model = new Cron();
|
||||
$cron_model->addCron(1, 0, '订单自动关闭', 'CronOrderClose', $execute_time, $this->order_id);
|
||||
// 订单催付通知
|
||||
// 未付款订单将会在订单关闭前10分钟对买家进行催付提醒
|
||||
if ($this->pay_money > 0) {
|
||||
$cron_model->addCron(1, 0, '订单催付通知', 'CronOrderUrgePayment', $execute_time - 600, $this->order_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置设置或查询
|
||||
* @param $key
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function config($key)
|
||||
{
|
||||
//查询购物配置
|
||||
$config = $this->config[$key] ?? [];
|
||||
if (empty($this->config[$key])) {
|
||||
switch ($key) {
|
||||
case 'order'://交易配置
|
||||
$config_model = new Config();
|
||||
$order_config = $config_model->getOrderEventTimeConfig($this->site_id)['data'] ?? [];
|
||||
$config = $order_config['value'] ?? [];
|
||||
break;
|
||||
case 'point'://积分交易配置
|
||||
$config_model = new PointCashConfig();
|
||||
$config = $config_model->getPointCashConfig($this->site_id)['data'];
|
||||
break;
|
||||
case 'balance'://余额交易配置
|
||||
$config_model = new Config();
|
||||
$config = $config_model->getBalanceConfig($this->site_id)['data']['value'] ?? [];
|
||||
break;
|
||||
case 'delivery_type':
|
||||
$config = (new ExpressConfig())->getExpressTypeList($this->site_id) ?? [];
|
||||
break;
|
||||
case 'delivery_type_sort':
|
||||
$config = (new ExpressConfig())->getDeliverTypeSort($this->site_id)['data'] ?? [];
|
||||
break;
|
||||
case 'express'://物流配置
|
||||
$express_config_model = new ExpressConfig();
|
||||
$config = $express_config_model->getExpressConfig($this->site_id)['data'] ?? [];
|
||||
break;
|
||||
case 'store'://门店自提配置
|
||||
$express_config_model = new ExpressConfig();
|
||||
$config = $express_config_model->getStoreConfig($this->site_id)['data'] ?? [];
|
||||
break;
|
||||
case 'local'://本地配送配置
|
||||
$express_config_model = new ExpressConfig();
|
||||
$config = $express_config_model->getLocalDeliveryConfig($this->site_id)['data'] ?? [];
|
||||
break;
|
||||
case 'store_business':
|
||||
$store_config_model = new \addon\store\model\Config();
|
||||
$config = $store_config_model->getStoreBusinessConfig($this->site_id)['data']['value'] ?? [];
|
||||
break;
|
||||
}
|
||||
$this->config[$key] = $config;
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单添加的公共数据
|
||||
* @param array $modules 数据组件标识
|
||||
* @param string $op and 包含 invert 无交集
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderInsertData($modules = [], $op = 'and')
|
||||
{
|
||||
$data = [];
|
||||
//公共的订单数据
|
||||
$this->order_no = $this->createOrderNo();
|
||||
$pay_model = new Pay();
|
||||
$this->out_trade_no = $pay_model->createOutTradeNo($this->member_id);
|
||||
$common_data = [
|
||||
'order_no' => $this->order_no,
|
||||
'out_trade_no' => $this->out_trade_no,
|
||||
'site_id' => $this->site_id,
|
||||
'site_name' => $this->site_name,
|
||||
'member_id' => $this->member_id,
|
||||
'order_from' => $this->order_from,
|
||||
'order_from_name' => $this->order_from_name,
|
||||
'buyer_ip' => request()->ip(),
|
||||
];
|
||||
$data = array_merge($data, $common_data);
|
||||
//传入数据
|
||||
$buyer_message = $this->param['buyer_message'];
|
||||
$input_data = [
|
||||
'buyer_message' => $buyer_message,
|
||||
];
|
||||
$data = array_merge($data, $input_data);
|
||||
//订单数据
|
||||
$order_data = [
|
||||
'goods_money' => $this->goods_money,
|
||||
'delivery_money' => $this->delivery_money,
|
||||
'coupon_money' => $this->coupon_money ?? 0,
|
||||
'point_money' => $this->point_money,
|
||||
'adjust_money' => $this->adjust_money,
|
||||
'invoice_money' => $this->invoice_money,
|
||||
'invoice_delivery_money' => $this->invoice_delivery_money,
|
||||
'promotion_money' => $this->promotion_money,
|
||||
'order_money' => $this->order_money,
|
||||
'balance_money' => $this->balance_money,
|
||||
'pay_money' => $this->pay_money,
|
||||
'member_card_money' => $this->member_card_money,
|
||||
|
||||
'order_name' => $this->order_name,
|
||||
'goods_num' => $this->goods_num,
|
||||
];
|
||||
$data = array_merge($data, $order_data);
|
||||
if ($this->getInsertDataWhereResult($modules, 'invoice', $op)) {
|
||||
//发票信息
|
||||
if (isset($this->param['is_invoice']) && $this->param['is_invoice'] == 1) {
|
||||
$invoice_data = [
|
||||
'taxpayer_number' => $this->invoice['taxpayer_number'] ?? '',
|
||||
'invoice_rate' => $this->invoice['invoice_rate'] ?? 0,
|
||||
'invoice_content' => $this->invoice['invoice_content'] ?? '',
|
||||
'invoice_full_address' => $this->invoice['invoice_full_address'] ?? '',
|
||||
'is_invoice' => $this->param['is_invoice'] ?? 0,
|
||||
'invoice_type' => $this->invoice['invoice_type'] ?? 0,
|
||||
'invoice_title' => $this->invoice['invoice_title'] ?? '',
|
||||
'is_tax_invoice' => $this->invoice['is_tax_invoice'] ?? '',
|
||||
'invoice_email' => $this->invoice['invoice_email'] ?? '',
|
||||
'invoice_title_type' => $this->invoice['invoice_title_type'] ?? 0,
|
||||
|
||||
];
|
||||
$data = array_merge($data, $invoice_data);
|
||||
}
|
||||
}
|
||||
if ($this->getInsertDataWhereResult($modules, 'delivery', $op)) {
|
||||
//配送数据
|
||||
$express_type_list = $this->config('delivery_type');
|
||||
$delivery_type_name = $express_type_list[$this->delivery['delivery_type']] ?? '';
|
||||
$buyer_ask_delivery_time = $this->delivery['buyer_ask_delivery_time'] ?? [];
|
||||
$delivery_data = [
|
||||
'delivery_type' => $this->delivery['delivery_type'],
|
||||
'delivery_type_name' => $delivery_type_name,
|
||||
'delivery_store_id' => $this->delivery['delivery_store_id'] ?? 0,
|
||||
'delivery_store_name' => $this->delivery['delivery_store_name'] ?? '',
|
||||
'delivery_store_info' => $this->delivery['delivery_store_info'] ?? '',
|
||||
'buyer_ask_delivery_time' => $buyer_ask_delivery_time['remark'] ?? '',//定时达
|
||||
'delivery_start_time' => $buyer_ask_delivery_time['start_time'] ?? '',//配送开始时间
|
||||
'delivery_end_time' => $buyer_ask_delivery_time['end_time'] ?? '',//配送结束时间
|
||||
];
|
||||
$data = array_merge($data, $delivery_data);
|
||||
}
|
||||
if ($this->getInsertDataWhereResult($modules, 'take', $op)) {
|
||||
$this->orderType();
|
||||
//允许门店配送和虚拟商品传入手机号
|
||||
$order_type = $this->order_type['order_type_id'] ?? '';
|
||||
if(in_array($order_type, [OrderDict::virtual, OrderDict::store])){
|
||||
$this->delivery['member_address']['name'] = $this->param['member_address']['name'] ?? '';
|
||||
$this->delivery['member_address']['mobile'] = $this->param['member_address']['mobile'] ?? '';
|
||||
}
|
||||
//收货人数据
|
||||
$take_data = [
|
||||
'name' => $this->delivery['member_address']['name'] ?? '',
|
||||
'mobile' => $this->delivery['member_address']['mobile'] ?? '',
|
||||
'telephone' => $this->delivery['member_address']['telephone'] ?? '',
|
||||
'province_id' => $this->delivery['member_address']['province_id'] ?? '',
|
||||
'city_id' => $this->delivery['member_address']['city_id'] ?? '',
|
||||
'district_id' => $this->delivery['member_address']['district_id'] ?? '',
|
||||
'community_id' => $this->delivery['member_address']['community_id'] ?? '',
|
||||
'address' => $this->delivery['member_address']['address'] ?? '',
|
||||
'full_address' => $this->delivery['member_address']['full_address'] ?? '',
|
||||
'longitude' => $this->delivery['member_address']['longitude'] ?? '',
|
||||
'latitude' => $this->delivery['member_address']['latitude'] ?? '',
|
||||
];
|
||||
$data = array_merge($data, $take_data);
|
||||
}
|
||||
|
||||
//活动数据
|
||||
if ($this->getInsertDataWhereResult($modules, 'promotion', $op)) {
|
||||
$promotion_data = [
|
||||
'promotion_type' => $this->promotion_type,
|
||||
'promotion_type_name' => $this->promotion_type_name,
|
||||
'promotion_status_name' => '',
|
||||
];
|
||||
$data = array_merge($data, $promotion_data);
|
||||
}
|
||||
//优惠数据
|
||||
if ($this->getInsertDataWhereResult($modules, 'discount', $op)) {
|
||||
$discount_data = [
|
||||
'coupon_id' => $this->coupon_id,
|
||||
];
|
||||
$data = array_merge($data, $discount_data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单编号
|
||||
* @return string
|
||||
*/
|
||||
public function createOrderNo()
|
||||
{
|
||||
$time_str = date('YmdHi');
|
||||
$max_no = Cache::get($this->site_id . '_' . $this->member_id . '_' . $time_str);
|
||||
if (empty($max_no)) {
|
||||
$max_no = 1;
|
||||
} else {
|
||||
$max_no = $max_no + 1;
|
||||
}
|
||||
$order_no = $time_str . $this->member_id . sprintf('%03d', $max_no);
|
||||
Cache::set($this->site_id . '_' . $this->member_id . '_' . $time_str, $max_no);
|
||||
return $order_no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $modules
|
||||
* @param $key
|
||||
* @param $op
|
||||
* @return bool
|
||||
*/
|
||||
public function getInsertDataWhereResult($modules, $key, $op)
|
||||
{
|
||||
if ($op == 'and') {
|
||||
if (!$modules || in_array($key, $modules)) {
|
||||
return true;
|
||||
}
|
||||
} else if ($op == 'invert') {
|
||||
if (!($modules && in_array($key, $modules))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单项数据整理
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderGoodsInsertData($data)
|
||||
{
|
||||
if ($data['num'] < 0) throw new OrderException('商品购买数量不能小于0');
|
||||
return [
|
||||
'order_id' => $this->order_id,
|
||||
'site_id' => $this->site_id,
|
||||
'store_id' => $this->store_id,
|
||||
'order_no' => $this->order_no,
|
||||
'member_id' => $this->member_id,
|
||||
'sku_id' => $data['sku_id'],
|
||||
'sku_name' => $data['sku_name'],
|
||||
'sku_image' => $data['sku_image'],
|
||||
'sku_no' => $data['sku_no'],
|
||||
'is_virtual' => $data['is_virtual'],
|
||||
'goods_class' => $data['goods_class'],
|
||||
'goods_class_name' => $data['goods_class_name'],
|
||||
'price' => $data['price'],
|
||||
'cost_price' => $data['cost_price'],
|
||||
'num' => $data['num'],
|
||||
'goods_money' => $data['goods_money'],
|
||||
'cost_money' => $data['cost_price'] * $data['num'],
|
||||
'goods_id' => $data['goods_id'],
|
||||
'delivery_status' => OrderGoodsDict::wait_delivery,
|
||||
'delivery_status_name' => OrderGoodsDict::getDeliveryStatus(OrderGoodsDict::wait_delivery),
|
||||
'real_goods_money' => $data['real_goods_money'],
|
||||
'coupon_money' => $data['coupon_money'] ?? 0,
|
||||
'promotion_money' => $data['promotion_money'],
|
||||
|
||||
'goods_name' => $data['goods_name'],
|
||||
'sku_spec_format' => $data['sku_spec_format'],
|
||||
|
||||
'supplier_id' => $data['supplier_id'] ?? 0,
|
||||
'is_fenxiao' => $data['is_fenxiao'] ?? 1,
|
||||
|
||||
|
||||
'use_point' => $data['use_point'] ?? 0,
|
||||
'point_money' => $data['point_money'] ?? 0.00,
|
||||
|
||||
'card_item_id' => $this->member_goods_card[$data['sku_id']] ?? 0,
|
||||
'card_promotion_money' => $data['card_promotion_money'] ?? 0.00,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单项列表(只适用于订单 订单项已创建之后的时机)
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function getOrderGoodsList()
|
||||
{
|
||||
if (!$this->order_goods_list) {
|
||||
$this->order_goods_list = model('order_goods')->getList([['order_id', '=', $this->order_id]]);
|
||||
}
|
||||
return $this->order_goods_list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取订单对象数据
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderObjectData()
|
||||
{
|
||||
if (!$this->order_object_data) {
|
||||
$this->order_object_data = get_object_vars($this);
|
||||
}
|
||||
return $this->order_object_data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 订单类型判断
|
||||
* @return true
|
||||
*/
|
||||
public function orderType()
|
||||
{
|
||||
if ($this->is_virtual == 1) {
|
||||
$order = new VirtualOrder();
|
||||
$this->order_type = [
|
||||
'order_type_id' => 4,
|
||||
'order_type_name' => '虚拟订单',
|
||||
'order_status' => $order->order_status[0]
|
||||
];
|
||||
} else {
|
||||
if ($this->delivery['delivery_type'] == 'express') {
|
||||
$order = new Order();
|
||||
$this->order_type = [
|
||||
'order_type_id' => 1,
|
||||
'order_type_name' => '普通订单',
|
||||
'order_status' => $order->order_status[0]
|
||||
];
|
||||
} elseif ($this->delivery['delivery_type'] == 'store') {
|
||||
$order = new StoreOrder();
|
||||
$this->order_type = [
|
||||
'order_type_id' => 2,
|
||||
'order_type_name' => '自提订单',
|
||||
'order_status' => $order->order_status[0]
|
||||
];
|
||||
} elseif ($this->delivery['delivery_type'] == 'local') {
|
||||
$order = new LocalOrder();
|
||||
$this->order_type = [
|
||||
'order_type_id' => 3,
|
||||
'order_type_name' => '外卖订单',
|
||||
'order_status' => $order->order_status[0]
|
||||
];
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单创建后事件
|
||||
* @return true
|
||||
*/
|
||||
public function orderCreateAfter()
|
||||
{
|
||||
|
||||
Queue::push('app\job\order\OrderCreateAfter', ['create_data' => get_object_vars($this), 'order_object' => $this]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入对象
|
||||
* @param $data
|
||||
* @return $this
|
||||
*/
|
||||
public function invokeClass($data)
|
||||
{
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
$this->$k = $v;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化订单开票基础信息
|
||||
*/
|
||||
public function initInvoice($param)
|
||||
{
|
||||
$this->order_id = $param['order_id'] ?? 0;
|
||||
if(empty($this->order_id)){
|
||||
return $this->error('', '缺少订单id');
|
||||
}
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $this->order_id]], 'order_id,order_status,goods_money,promotion_money,coupon_money,point_money');
|
||||
if (!$order_info) {
|
||||
return $this->error('', '订单不存在');
|
||||
}
|
||||
if($order_info['invoice_status'] == 1){
|
||||
return $this->error('', '订单已开票,请勿重复操作');
|
||||
}
|
||||
$this->param = $param;
|
||||
$this->invokeClass($order_info);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新发票数据
|
||||
*/
|
||||
public function saveInvoice(): array
|
||||
{
|
||||
$invoice_data = [
|
||||
'taxpayer_number' => $this->invoice['taxpayer_number'] ?? '',
|
||||
'invoice_rate' => $this->invoice['invoice_rate'] ?? 0,
|
||||
'invoice_content' => $this->invoice['invoice_content'] ?? '',
|
||||
'invoice_full_address' => $this->invoice['invoice_full_address'] ?? '',
|
||||
'is_invoice' => $this->param['is_invoice'] ?? 0,
|
||||
'invoice_type' => $this->invoice['invoice_type'] ?? 0,
|
||||
'invoice_title' => $this->invoice['invoice_title'] ?? '',
|
||||
'is_tax_invoice' => $this->invoice['is_tax_invoice'] ?? '',
|
||||
'invoice_email' => $this->invoice['invoice_email'] ?? '',
|
||||
'invoice_title_type' => $this->invoice['invoice_title_type'] ?? 0,
|
||||
];
|
||||
$result = model("order")->update($invoice_data,[['order_id','=',$this->order_id]]);
|
||||
if($result){
|
||||
return $this->success($result);
|
||||
}
|
||||
return $this->error([],'发票信息更新失败,请重试');
|
||||
}
|
||||
|
||||
public function setStoreId($store_id){
|
||||
$this->store_id = $store_id;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
104
app/model/order/OrderCron.php
Executable file
104
app/model/order/OrderCron.php
Executable file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\express\LocalPackage;
|
||||
use app\model\message\Message;
|
||||
use app\model\system\Cron;
|
||||
use Exception;
|
||||
use think\facade\Db;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 订单任务事件
|
||||
* @author Administrator
|
||||
*/
|
||||
class OrderCron extends BaseModel
|
||||
{
|
||||
public static function close($data){
|
||||
$order_id = $data['order_id'];
|
||||
$now_time = time();
|
||||
$order_config = Config::getOrderConfig($data['site_id']);
|
||||
if ($order_config['auto_close'] > 0) {
|
||||
$execute_time = $now_time + $order_config['auto_close'] * 60; //自动关闭时间
|
||||
}
|
||||
$cron_model = new Cron();
|
||||
$cron_model->addCron(1, 0, '订单自动关闭', 'CronOrderClose', $execute_time, $order_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public function urgepayment(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动完成事件
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public static function complete($data){
|
||||
$order_id = $data['order_id'];
|
||||
$site_id = $data['site_id'];
|
||||
//获取订单自动完成时间
|
||||
$order_config = Config::getOrderConfig($site_id);
|
||||
$now_time = time();
|
||||
if (!empty($order_config)) {
|
||||
$execute_time = $now_time + $order_config[ 'auto_complete' ] * 86400;//自动完成时间
|
||||
} else {
|
||||
$execute_time = $now_time + 86400;//尚未配置 默认一天
|
||||
}
|
||||
//设置订单自动完成事件
|
||||
$cron_model = new Cron();
|
||||
$cron_model->addCron(1, 0, '订单自动完成', 'CronOrderComplete', $execute_time, $order_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭售后
|
||||
* @param $data
|
||||
* @return void
|
||||
*/
|
||||
public static function afterSaleClose($data){
|
||||
$after_sales_time = $data['after_sales_time'];
|
||||
$order_id = $data['order_id'];
|
||||
$cron = new Cron();
|
||||
$execute_time = strtotime("+ {$after_sales_time} day");
|
||||
$cron->addCron(1, 0, '订单售后自动关闭', 'CronOrderAfterSaleClose', $execute_time, $order_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动收货
|
||||
* @param $data
|
||||
* @return true
|
||||
*/
|
||||
public static function takeDelivery($data){
|
||||
$order_id = $data['order_id'];
|
||||
$site_id = $data['site_id'];
|
||||
$expire_time = $data['expire_time'] ?? 0;
|
||||
if($expire_time == 0){
|
||||
// 获取订单自动收货时间
|
||||
$order_config = Config::getOrderConfig($site_id);
|
||||
$now_time = time(); //当前时间
|
||||
if ($order_config['auto_take_delivery'] > 0) {
|
||||
$execute_time = $now_time + $order_config['auto_take_delivery'] * 86400; // 自动收货时间
|
||||
}
|
||||
}
|
||||
if(!empty($execute_time)){
|
||||
$cron_model = new Cron();
|
||||
$cron_model->addCron(1, 0, '订单自动收货', 'CronOrderTakeDelivery', $execute_time, $order_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
700
app/model/order/OrderExport.php
Executable file
700
app/model/order/OrderExport.php
Executable file
@@ -0,0 +1,700 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use Exception;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 订单导出
|
||||
* @author Administrator
|
||||
*/
|
||||
class OrderExport extends BaseModel
|
||||
{
|
||||
public $order_field = [
|
||||
'order_no' => '订单编号',
|
||||
//单商户店铺名称没有必要导出
|
||||
//'site_name' => '店铺名称',
|
||||
'order_name' => '订单内容',
|
||||
'order_from_name' => '订单来源',
|
||||
'order_type_name' => '订单类型',
|
||||
'order_promotion_name' => '营销活动类型',
|
||||
'out_trade_no' => '支付流水号',
|
||||
'out_trade_no_2' => '支付流水号(多次支付)',
|
||||
'delivery_code' => '整体提货编码',
|
||||
'order_status_name' => '订单状态',
|
||||
'pay_status' => '支付状态',
|
||||
'delivery_status' => '配送状态',
|
||||
'pay_type_name' => '支付方式',
|
||||
'delivery_type_name' => '配送方式',
|
||||
'nickname' => '购买人',
|
||||
'name' => '客户姓名',
|
||||
'mobile' => '客户手机',
|
||||
'telephone' => '客户固定电话',
|
||||
'province_name' => '省',
|
||||
'city_name' => '市',
|
||||
'district_name' => '县',
|
||||
'full_address' => '详细地址',
|
||||
'buyer_ip' => '客户ip',
|
||||
'buyer_ask_delivery_time' => '客户要求配送时间',
|
||||
'buyer_message' => '客户留言信息',
|
||||
'goods_money' => '商品总金额',
|
||||
'delivery_money' => '配送费用',
|
||||
'promotion_money' => '订单优惠金额',
|
||||
'coupon_money' => '优惠券金额',
|
||||
'order_money' => '订单合计金额',
|
||||
'adjust_money' => '订单调整金额',
|
||||
'balance_money' => '余额支付金额',
|
||||
'pay_money' => '抵扣之后应付金额',
|
||||
'refund_money' => '订单退款金额',
|
||||
'create_time' => '下单时间',
|
||||
'pay_time' => '支付时间',
|
||||
'delivery_time' => '配送时间',
|
||||
'sign_time' => '签收时间',
|
||||
'finish_time' => '完成时间',
|
||||
'remark' => '卖家留言',
|
||||
'goods_num' => '商品件数',
|
||||
'delivery_status_name' => '发货状态',
|
||||
'is_settlement' => '是否进行结算',
|
||||
'delivery_store_name' => '门店名称',
|
||||
'promotion_type_name' => '营销类型',
|
||||
'form_data' => '表单数据'
|
||||
];
|
||||
|
||||
public $order_field_sample = [
|
||||
//单商户店铺名称没有必要导出
|
||||
//'site_name' => '店铺名称',
|
||||
'order_id' => '订单ID',
|
||||
'order_no' => '订单编号',
|
||||
'out_trade_no' => '支付流水号',
|
||||
'delivery_store_name' => '门店名称',
|
||||
'order_from_name' => '订单来源',
|
||||
'order_type_name' => '订单类型',
|
||||
'promotion_type_name' => '营销类型',
|
||||
'order_status_name' => '订单状态',
|
||||
'nickname' => '买家昵称',
|
||||
'name' => '收货人姓名',
|
||||
'mobile' => '收货人电话',
|
||||
'full_address' => '收货地址',
|
||||
'buyer_message' => '买家留言',
|
||||
'remark' => '商家备注',
|
||||
'goods_money' => '商品总金额',
|
||||
'goods_num' => '商品总件数',
|
||||
];
|
||||
|
||||
//订单商品信息
|
||||
public $order_goods_field = [
|
||||
'create_time' => '下单时间',
|
||||
'pay_time' => '支付时间',
|
||||
'sku_name' => '商品名称',
|
||||
'sku_no' => '商品编码',
|
||||
'goods_class_name' => '商品类型',
|
||||
'price' => '商品卖价',
|
||||
'cost_price' => '成本价',
|
||||
'num' => '购买数量',
|
||||
'goods_money' => '商品总价',
|
||||
'cost_money' => '成本总价',
|
||||
'delivery_status_name' => '配送状态',
|
||||
'delivery_no' => '配送单号',
|
||||
'refund_status_name' => '退款状态',
|
||||
'refund_no' => '退款编号',
|
||||
'refund_type' => '退货方式',
|
||||
'refund_apply_money' => '退款申请金额',
|
||||
'refund_reason' => '退款原因',
|
||||
'refund_real_money' => '实际退款金额',
|
||||
'refund_delivery_name' => '退款公司名称',
|
||||
'refund_delivery_no' => '退款单号',
|
||||
'refund_time' => '实际退款时间',
|
||||
'refund_refuse_reason' => '退款拒绝原因',
|
||||
'refund_action_time' => '申请退款时间',
|
||||
'real_goods_money' => '实际商品购买价',
|
||||
'refund_remark' => '退款说明',
|
||||
'refund_delivery_remark' => '买家退货说明',
|
||||
'refund_address' => '退货地址',
|
||||
'is_refund_stock' => '是否返还库存',
|
||||
'form_data' => '表单数据'
|
||||
];
|
||||
|
||||
|
||||
public $define_data = [
|
||||
'pay_status' => ['type' => 2, 'data' => ['未支付', '已支付']],//支付状态
|
||||
'delivery_status' => ['type' => 2, 'data' => ['待发货', '已发货', '已收货']],//配送状态
|
||||
'refund_status' => ['type' => 2, 'data' => ['未退款', '已退款']],//退款状态
|
||||
// 'buyer_ask_delivery_time' => [ 'type' => 1 ],//购买人要求配送时间
|
||||
'create_time' => ['type' => 1],//支付时间
|
||||
'pay_time' => ['type' => 1],//支付时间
|
||||
'delivery_time' => ['type' => 1],//订单配送时间
|
||||
'sign_time' => ['type' => 1],//订单签收时间
|
||||
'finish_time' => ['type' => 1],//订单完成时间
|
||||
'refund_time' => ['type' => 1],//退款到账时间
|
||||
'refund_action_time' => ['type' => 1],//实际退款时间
|
||||
'is_settlement' => ['type' => 2, 'data' => ['否', '是']],//是否进行结算
|
||||
'refund_type' => ['type' => 2, 'data' => [1 => '仅退款', 2 => '退款退货']],//退货方式
|
||||
'is_refund_stock' => ['type' => 2, 'data' => ['否', '是']],//是否返还库存
|
||||
'form_data' => ['type' => 3],//表单数据
|
||||
];
|
||||
|
||||
/**
|
||||
* 查询订单项数据并导出
|
||||
* @param $condition
|
||||
* @param $condition_desc
|
||||
* @param $site_id
|
||||
* @param $join
|
||||
* @param $is_verify
|
||||
* @param $order_label
|
||||
* @return array
|
||||
*/
|
||||
public function orderExport($condition, $condition_desc, $site_id, $join, $is_verify, $order_label)
|
||||
{
|
||||
set_time_limit(0);
|
||||
try {
|
||||
//预先创建导出的记录
|
||||
$data = array(
|
||||
'condition' => json_encode($condition_desc),
|
||||
'create_time' => time(),
|
||||
'type' => 1,//订单
|
||||
'status' => 0,
|
||||
'site_id' => $site_id
|
||||
);
|
||||
$records_result = $this->addExport($data);
|
||||
$export_id = $records_result['data'];
|
||||
if (empty($export_id)) return $this->error(null, '创建导出记录失败');
|
||||
|
||||
//导出字段预处理
|
||||
$export_field = $this->order_field;
|
||||
$field_value = [];
|
||||
$field_key = [];
|
||||
$field_key_array = [];
|
||||
foreach ($export_field as $k => $v) {
|
||||
$field_value[] = $v;
|
||||
$field_key[] = "{\$$k}";
|
||||
$field_key_array[] = $k;
|
||||
}
|
||||
|
||||
//创建目录
|
||||
$file_path = 'upload/order_csv/';
|
||||
if (!dir_mkdir($file_path)) return $this->error(null, '导出目录创建失败');
|
||||
|
||||
//创建并打开文件
|
||||
$file_name = date('YmdHis');
|
||||
$file_path = $file_path . $file_name . '.csv';
|
||||
$fp = fopen($file_path, 'w');
|
||||
fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
|
||||
|
||||
//写入第一行
|
||||
$first_line = implode(',', $field_value);
|
||||
fwrite($fp, $first_line . "\n");
|
||||
|
||||
//导出数据
|
||||
$alias = 'o';
|
||||
$join = [
|
||||
['member m', 'm.member_id = o.member_id', 'left'],
|
||||
['form_data fm', "fm.relation_id = o.order_id and scene = 'order'", 'left'],
|
||||
];
|
||||
if ($is_verify != 'all') {
|
||||
$join[] = ['verify v', 'v.verify_code = o.virtual_code', 'left'];
|
||||
}
|
||||
$order_table = Db::name('order')->where($condition)->alias($alias);
|
||||
$order_table = $this->parseJoin($order_table, $join);
|
||||
$temp_line = implode(',', $field_key) . "\n";
|
||||
$table_field = 'o.*,m.nickname,fm.form_data';
|
||||
$order_table->field($table_field)->chunk(5000, function ($item_list) use ($fp, $temp_line, $field_key_array) {
|
||||
//写入导出信息
|
||||
$this->itemExport($item_list, $field_key_array, $temp_line, $fp);
|
||||
unset($item_list);
|
||||
}, 'o.order_id');
|
||||
$order_table->removeOption();
|
||||
fclose($fp); //每生成一个文件关闭
|
||||
unset($order_table);
|
||||
|
||||
//更改导出记录
|
||||
$records_data = array(
|
||||
'path' => $file_path,
|
||||
'status' => 1
|
||||
);
|
||||
$records_condition = array(
|
||||
['export_id', '=', $export_id]
|
||||
);
|
||||
$this->editExport($records_data, $records_condition);
|
||||
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
return $this->error([], $e->getMessage() . $e->getFile() . $e->getLine());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单项数据并导出
|
||||
* @param $condition
|
||||
* @param $condition_desc
|
||||
* @param $site_id
|
||||
* @param $is_verify
|
||||
* @param $order_label
|
||||
* @return array
|
||||
*/
|
||||
public function orderGoodsExport($condition, $condition_desc, $site_id, $is_verify, $order_label)
|
||||
{
|
||||
set_time_limit(0);
|
||||
$is_install_supply = addon_is_exit('supply');
|
||||
try {
|
||||
//预先创建导出的记录
|
||||
$data = array(
|
||||
'condition' => json_encode($condition_desc),
|
||||
'create_time' => time(),
|
||||
'type' => 2,//订单项
|
||||
'status' => 0,
|
||||
'site_id' => $site_id
|
||||
);
|
||||
$records_result = $this->addExport($data);
|
||||
$export_id = $records_result['data'];
|
||||
if (empty($export_id)) return $this->error(null, '创建导出记录失败');
|
||||
|
||||
//导出字段预处理
|
||||
$export_field = array_merge($this->order_field_sample, $this->order_goods_field);
|
||||
if($is_install_supply) $export_field['supplier_name'] = '供应商';
|
||||
$field_value = [];
|
||||
$field_key = [];
|
||||
$field_key_array = [];
|
||||
foreach ($export_field as $k => $v) {
|
||||
$field_value[] = $v;
|
||||
$field_key[] = "{\$$k}"; //为了防止部分代码被筛选中替换, 给变量前后两边增加字符串
|
||||
$field_key_array[] = $k;
|
||||
}
|
||||
|
||||
//创建目录
|
||||
$file_path = 'upload/order_csv/';
|
||||
if (!dir_mkdir($file_path)) return $this->error(null, '导出目录创建失败');
|
||||
|
||||
//创建并打开文件
|
||||
$file_name = date('YmdHis');//csv文件名
|
||||
$file_path = $file_path . $file_name . '.csv';
|
||||
$fp = fopen($file_path, 'w');
|
||||
fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
|
||||
|
||||
//写入第一行表头
|
||||
$first_line = implode(',', $field_value);
|
||||
fwrite($fp, $first_line . "\n");
|
||||
|
||||
//导出数据
|
||||
$alias = 'og';
|
||||
$join = [
|
||||
['order o', 'o.order_id = og.order_id', 'left'],
|
||||
['member m', 'm.member_id = og.member_id', 'left'],
|
||||
['form_data fm', "fm.relation_id = og.order_goods_id and scene = 'goods'", 'left'],
|
||||
];
|
||||
if ($is_verify != 'all') {
|
||||
$join[] = ['verify v', 'v.verify_code = o.virtual_code', 'left'];
|
||||
}
|
||||
if($is_install_supply){
|
||||
$join[] = ['supplier s', 'og.supplier_id = s.supplier_id', 'left'];
|
||||
}
|
||||
//查询字段
|
||||
$order_field = 'o.create_time,o.pay_time,o.order_id,o.order_no,o.site_name,o.order_name,o.order_from_name,o.order_type_name,o.order_promotion_name,o.out_trade_no,o.out_trade_no_2,o.delivery_code,o.order_status_name,o.pay_status,o.delivery_status,o.refund_status,o.pay_type_name,o.delivery_type_name,o.name,o.mobile,o.telephone,o.full_address,o.buyer_ip,o.buyer_ask_delivery_time,o.buyer_message,o.goods_money,o.delivery_money,o.promotion_money,o.coupon_money,o.order_money,o.adjust_money,o.balance_money,o.pay_money,o.refund_money,o.pay_time,o.delivery_time,o.sign_time,o.finish_time,o.remark,o.goods_num,o.delivery_status_name,o.is_settlement,o.delivery_store_name,o.promotion_type_name,o.address,m.nickname';
|
||||
$order_goods_field = 'og.order_goods_id,og.sku_name,og.sku_no,og.is_virtual,og.goods_class_name,og.price,og.cost_price,og.num,og.goods_money,og.cost_money,og.delivery_no,og.refund_no,og.refund_type,og.refund_apply_money,og.refund_reason,og.refund_real_money,og.refund_delivery_name,og.refund_delivery_no,og.refund_time,og.refund_refuse_reason,og.refund_action_time,og.real_goods_money,og.refund_remark,og.refund_delivery_remark,og.refund_address,og.is_refund_stock,og.refund_status_name,fm.form_data';
|
||||
if($is_install_supply){
|
||||
$order_goods_field .= ',IF(s.title is null, "", s.title) as supplier_name';
|
||||
}
|
||||
$table_field = $order_field . ',' . $order_goods_field;
|
||||
$order_table = Db::name('order_goods')->where($condition)->alias($alias);
|
||||
$order_table = $this->parseJoin($order_table, $join);
|
||||
$temp_line = implode(',', $field_key) . "\n";
|
||||
$export_order_field = $this->order_field_sample;
|
||||
$export_order_id = 0;
|
||||
$order_table->field($table_field)->chunk(5, function ($item_list) use ($fp, $temp_line, $field_key_array, $export_order_field, &$export_order_id) {
|
||||
$item_list = $item_list->toArray();
|
||||
//数据合并处理
|
||||
foreach($item_list as &$val){
|
||||
if($val['order_id'] != $export_order_id){
|
||||
$export_order_id = $val['order_id'];
|
||||
}else{
|
||||
foreach($export_order_field as $field=>$field_name){
|
||||
$val[$field] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
//写入导出信息
|
||||
$this->itemExport($item_list, $field_key_array, $temp_line, $fp);
|
||||
unset($item_list);
|
||||
});
|
||||
$order_table->removeOption();
|
||||
fclose($fp); //每生成一个文件关闭
|
||||
unset($order_table);
|
||||
|
||||
//更新导出记录
|
||||
$records_data = array(
|
||||
'path' => $file_path,
|
||||
'status' => 1
|
||||
);
|
||||
$records_condition = array(
|
||||
['export_id', '=', $export_id]
|
||||
);
|
||||
$this->editExport($records_data, $records_condition);
|
||||
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
return $this->error([], $e->getMessage() . $e->getLine());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单项数据并导出
|
||||
* @param $condition
|
||||
* @param $condition_desc
|
||||
* @param int $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function orderRefundExport($condition, $condition_desc, $site_id = 0)
|
||||
{
|
||||
set_time_limit(0);
|
||||
try {
|
||||
//预先创建导出的记录
|
||||
$data = array(
|
||||
'condition' => json_encode($condition_desc),
|
||||
'create_time' => time(),
|
||||
'status' => 0,
|
||||
'site_id' => $site_id
|
||||
);
|
||||
$records_result = $this->addRefundExport($data);
|
||||
$export_id = $records_result['data'];
|
||||
if (empty($export_id)) return $this->error(null, '创建导出记录失败');
|
||||
|
||||
//创建目录
|
||||
$file_path = 'upload/order_csv/';
|
||||
if (!dir_mkdir($file_path)) return $this->error(null, '导出目录创建失败');
|
||||
|
||||
//创建并打开文件
|
||||
$file_name = date('YmdHis');
|
||||
$file_path = $file_path . $file_name . '.csv';
|
||||
$fp = fopen($file_path, 'w'); //生成临时文件
|
||||
fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
|
||||
|
||||
//导出字段预处理
|
||||
$export_field = array_merge($this->order_goods_field, $this->order_field);
|
||||
$field_value = [];
|
||||
$field_key = [];
|
||||
$field_key_array = [];
|
||||
foreach ($export_field as $k => $v) {
|
||||
$field_value[] = $v;
|
||||
$field_key[] = "{\$$k}";
|
||||
$field_key_array[] = $k;
|
||||
}
|
||||
|
||||
//写入第一行表头
|
||||
$first_line = implode(',', $field_value);
|
||||
fwrite($fp, $first_line . "\n");
|
||||
|
||||
//导出数据
|
||||
$alias = 'og';
|
||||
$join = [
|
||||
['order o', 'o.order_id = og.order_id', 'left']
|
||||
];
|
||||
$order_field = 'o.order_no,o.site_name,o.order_name,o.order_from_name,o.order_type_name,o.order_promotion_name,o.out_trade_no,o.out_trade_no_2,o.delivery_code,o.order_status_name,o.pay_status,o.delivery_status,o.refund_status,o.pay_type_name,o.delivery_type_name,o.name,o.mobile,o.telephone,o.full_address,o.buyer_ip,o.buyer_ask_delivery_time,o.buyer_message,o.goods_money,o.delivery_money,o.promotion_money,o.coupon_money,o.order_money,o.adjust_money,o.balance_money,o.pay_money,o.refund_money,o.pay_time,o.delivery_time,o.sign_time,o.finish_time,o.remark,o.goods_num,o.delivery_status_name,o.is_settlement,o.delivery_store_name,o.promotion_type_name,o.address';
|
||||
$order_goods_field = 'og.order_goods_id,og.sku_name,og.sku_no,og.is_virtual,og.goods_class_name,og.price,og.cost_price,og.num,og.goods_money,og.cost_money,og.delivery_no,og.refund_no,og.refund_type,og.refund_apply_money,og.refund_reason,og.refund_real_money,og.refund_delivery_name,og.refund_delivery_no,og.refund_time,og.refund_refuse_reason,og.refund_action_time,og.real_goods_money,og.refund_remark,og.refund_delivery_remark,og.refund_address,og.is_refund_stock,og.refund_status_name';
|
||||
$table_field = $order_field . ',' . $order_goods_field;
|
||||
$order_table = Db::name('order_goods')->where($condition)->alias($alias);
|
||||
$order_table = $this->parseJoin($order_table, $join);
|
||||
$temp_line = implode(',', $field_key) . "\n";
|
||||
$order_table->field($table_field)->chunk(5000, function ($item_list) use ($fp, $temp_line, $field_key_array) {
|
||||
//写入导出信息
|
||||
$this->itemExport($item_list, $field_key_array, $temp_line, $fp);
|
||||
unset($item_list);
|
||||
});
|
||||
$order_table->removeOption();
|
||||
fclose($fp);
|
||||
unset($order_table);
|
||||
|
||||
//将同步导出记录状态
|
||||
$records_data = array(
|
||||
'path' => $file_path,
|
||||
'status' => 1
|
||||
);
|
||||
$records_condition = array(
|
||||
['export_id', '=', $export_id]
|
||||
);
|
||||
$this->editRefundExport($records_data, $records_condition);
|
||||
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
return $this->error([], $e->getMessage() . $e->getLine());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $db_obj
|
||||
* @param $join
|
||||
* @return mixed
|
||||
*/
|
||||
public function parseJoin($db_obj, $join)
|
||||
{
|
||||
foreach ($join as $item) {
|
||||
list($table, $on, $type) = $item;
|
||||
$type = strtolower($type);
|
||||
switch ($type) {
|
||||
case 'left':
|
||||
$db_obj = $db_obj->leftJoin($table, $on);
|
||||
break;
|
||||
case 'inner':
|
||||
$db_obj = $db_obj->join($table, $on);
|
||||
break;
|
||||
case 'right':
|
||||
$db_obj = $db_obj->rightjoin($table, $on);
|
||||
break;
|
||||
case 'full':
|
||||
$db_obj = $db_obj->fulljoin($table, $on);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $db_obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给csv写入新的数据
|
||||
* @param $item_list
|
||||
* @param $field_key
|
||||
* @param $temp_line
|
||||
* @param $fp
|
||||
*/
|
||||
public function itemExport($item_list, $field_key, $temp_line, $fp)
|
||||
{
|
||||
if(method_exists($item_list, 'toArray')){
|
||||
$item_list = $item_list->toArray();
|
||||
}
|
||||
$item_list = $this->handleData($item_list, $field_key);
|
||||
foreach ($item_list as $k => $item_v) {
|
||||
$new_line_value = $temp_line;
|
||||
|
||||
if (isset($item_v['goods_num']) && $item_v['goods_num'] !== '') {
|
||||
$item_v['goods_num'] = numberFormat($item_v['goods_num']);
|
||||
}
|
||||
if (isset($item_v['num']) && $item_v['num'] !== '') {
|
||||
$item_v['num'] = numberFormat($item_v['num']);
|
||||
}
|
||||
|
||||
//省市县
|
||||
if($item_v['full_address'] !== ''){
|
||||
$address_arr = explode('-', $item_v['full_address']);
|
||||
$item_v['province_name'] = !empty($address_arr[0]) ? $address_arr[0] : '';
|
||||
$item_v['city_name'] = !empty($address_arr[1]) ? $address_arr[1] : '';
|
||||
$item_v['district_name'] = !empty($address_arr[2]) ? $address_arr[2] : '';
|
||||
}else{
|
||||
$item_v['province_name'] = '';
|
||||
$item_v['city_name'] = '';
|
||||
$item_v['district_name'] = '';
|
||||
}
|
||||
|
||||
foreach ($item_v as $key => $value) {
|
||||
$value = trim($value);
|
||||
|
||||
if ($key == 'full_address' && !empty($value)) {
|
||||
$address = $item_v['address'] ?? '';
|
||||
$value = $value . $address;
|
||||
}
|
||||
//CSV比较简单,记得转义 逗号就好
|
||||
$values = str_replace(',', '\\', $value . "\t");
|
||||
$values = str_replace("\n", '', $values);
|
||||
$values = str_replace("\r", '', $values);
|
||||
$new_line_value = str_replace("{\$$key}", $values, $new_line_value);
|
||||
}
|
||||
//写入第一行表头
|
||||
fwrite($fp, $new_line_value);
|
||||
//销毁变量, 防止内存溢出
|
||||
unset($new_line_value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据处理
|
||||
* @param $data
|
||||
* @param $field
|
||||
* @return array
|
||||
*/
|
||||
public function handleData($data, $field)
|
||||
{
|
||||
$define_data = $this->define_data;
|
||||
foreach ($data as $k => $v) {
|
||||
//获取键
|
||||
$keys = array_keys($v);
|
||||
foreach ($keys as $key) {
|
||||
if($v[$key] === '') continue;
|
||||
if (in_array($key, $field)) {
|
||||
if (array_key_exists($key, $define_data)) {
|
||||
$type = $define_data[$key]['type'];
|
||||
switch ($type) {
|
||||
case 1:
|
||||
$data[$k][$key] = time_to_date((int)$v[$key]);
|
||||
break;
|
||||
case 2:
|
||||
$define_data_data = $define_data[$key]['data'];
|
||||
$data[$k][$key] = !empty($v[$key]) ? $define_data_data[$v[$key]] : '';
|
||||
break;
|
||||
case 3:
|
||||
if (!empty($v[$key])) {
|
||||
$form_data = json_decode($v[$key], true);
|
||||
$form_content = '';
|
||||
if (is_array($form_data)) {
|
||||
foreach ($form_data as $item) {
|
||||
$form_content .= $item['value']['title'] . ':' . $item['val'] . ';';
|
||||
}
|
||||
}
|
||||
$data[$k][$key] = $form_content;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加导出记录
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addExport($data)
|
||||
{
|
||||
$res = model('order_export')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新导出记录
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editExport($data, $condition)
|
||||
{
|
||||
$res = model('order_export')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除导出记录
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteExport($condition)
|
||||
{
|
||||
//先查询数据
|
||||
$list = model('order_export')->getList($condition, '*');
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $k => $v) {
|
||||
if (file_exists($v['path'])) {
|
||||
//删除物理文件路径
|
||||
if (!unlink($v['path'])) {
|
||||
//失败
|
||||
} else {
|
||||
//成功
|
||||
}
|
||||
}
|
||||
}
|
||||
$res = model('order_export')->delete($condition);
|
||||
}
|
||||
|
||||
return $this->success($res ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导出记录
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getExport($condition, $field = '*', $order = '')
|
||||
{
|
||||
$list = model('order_export')->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出记录
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getExportPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('order_export')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除导出记录
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function deleteRefundExport($condition)
|
||||
{
|
||||
$res = model('order_refund_export')->delete($condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加导出记录
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addRefundExport($data)
|
||||
{
|
||||
$res = model('order_refund_export')->add($data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新导出记录
|
||||
* @param $data
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function editRefundExport($data, $condition)
|
||||
{
|
||||
$res = model('order_refund_export')->update($data, $condition);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导出记录
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getRefundExport($condition, $field = '*', $order = '')
|
||||
{
|
||||
$list = model('order_refund_export')->getList($condition, $field, $order);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出记录
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getRefundExportPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('order_refund_export')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
}
|
||||
71
app/model/order/OrderGoods.php
Executable file
71
app/model/order/OrderGoods.php
Executable file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 商品
|
||||
*/
|
||||
class OrderGoods extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 获取商品sku分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @param string $alias
|
||||
* @param string $join
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = '')
|
||||
{
|
||||
$res = model('order_goods')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
foreach ($res['list'] as &$v) {
|
||||
if (isset($v['num'])) {
|
||||
$v['num'] = numberFormat($v['num']);
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 订单项商品真实支付金额
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderRealPayGoodsMoney($params)
|
||||
{
|
||||
$order_goods_id = $params['order_goods_id'];
|
||||
$order_id = $params['order_id'];
|
||||
$condition = array(
|
||||
['order_id', '=', $order_id]
|
||||
);
|
||||
$order_info = model('order')->getInfo($condition, 'order_money, pay_money, delivery_money, coupon_money, balance_money, invoice_money, point_money');
|
||||
$order_money = $order_info['order_money'];
|
||||
$pay_money = $order_info['pay_money'];
|
||||
$delivery_money = $order_info['delivery_money'];
|
||||
$invoice_money = $order_info['invoice_money'];
|
||||
$balance_money = $order_info['balance_money'];
|
||||
$coupon_money = $order_info['coupon_money'];
|
||||
$point_money = $order_info['point_money'];
|
||||
$real_pay_goods_money = $pay_money - $delivery_money - $invoice_money;//总的商品真实支付金额
|
||||
|
||||
$real_goods_money = $order_money - $delivery_money - $invoice_money;
|
||||
$item_real_goods_money = $params['real_goods_money'];//订单项真实支付金额
|
||||
$item_real_pay_goods_money = $real_goods_money > 0 ? round($item_real_goods_money / $real_goods_money * $real_pay_goods_money, 2) : 0;//四舍五入可能会多
|
||||
return $this->success($item_real_pay_goods_money);
|
||||
|
||||
}
|
||||
}
|
||||
85
app/model/order/OrderImportFile.php
Executable file
85
app/model/order/OrderImportFile.php
Executable file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
|
||||
use app\model\BaseModel;
|
||||
use Exception;
|
||||
|
||||
class OrderImportFile extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 详情
|
||||
* @param array $condition
|
||||
* @param string $filed
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderImportFileInfo($condition = [], $filed = '*')
|
||||
{
|
||||
$info = model('order_import_file')->getInfo($condition, $filed);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param $id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function deleteOrderImportFile($id, $site_id)
|
||||
{
|
||||
model('order_import_file')->startTrans();
|
||||
try {
|
||||
model('order_import_file')->delete([['id', '=', $id], ['site_id', '=', $site_id]]);
|
||||
model('order_import_file_log')->delete([['file_id', '=', $id]]);
|
||||
|
||||
model('order_import_file')->commit();
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
|
||||
model('order_import_file')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导入文件列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderImportFilePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = '*')
|
||||
{
|
||||
$list = model('order_import_file')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导入文件列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderImportFilePageLogList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = '*', $alias = '', $join = '')
|
||||
{
|
||||
$list = model('order_import_file_log')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
}
|
||||
224
app/model/order/OrderLog.php
Executable file
224
app/model/order/OrderLog.php
Executable file
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use app\model\member\Member;
|
||||
use app\model\system\User;
|
||||
|
||||
/**
|
||||
* 订单日志
|
||||
* Class OrderLog
|
||||
* @package app\model\order
|
||||
*/
|
||||
class OrderLog extends OrderCommon
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取订单日志列表
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param OrderCommon $instance
|
||||
* @return array
|
||||
*/
|
||||
public static function getOrderLogList($condition, $field = '*', $order = 'action_time desc', OrderCommon $instance)
|
||||
{
|
||||
$res = model('order_log')->getList($condition, $field, $order);
|
||||
return $instance->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单日志数量
|
||||
* @param $condition
|
||||
* @param OrderCommon $instance
|
||||
* @return array
|
||||
*/
|
||||
public static function getOrderLogCount($condition, OrderCommon $instance)
|
||||
{
|
||||
$res = model('order_log')->getCount($condition);
|
||||
return $instance->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加订单日志
|
||||
* @param array $data
|
||||
* @param OrderCommon $instance
|
||||
* @return array
|
||||
*/
|
||||
public static function addOrderLog($data, OrderCommon $instance)
|
||||
{
|
||||
$data['action_time'] = time();//操作时间
|
||||
$res = model('order_log')->add($data);
|
||||
return $instance->success($res);
|
||||
}
|
||||
|
||||
public function addLog($params)
|
||||
{
|
||||
$action = $params['action'];//操作类型
|
||||
$is_auto = $params['is_auto'] ?? false;//是否是自动任务,如果是自动任务的话调用的时候就需要传递此值
|
||||
$site_id = $params['site_id'];//站点id
|
||||
$order_id = $params['order_id'];//订单id
|
||||
$member_id = $params['member_id'] ?? 0;//操作会员id
|
||||
$member_model = new Member();
|
||||
|
||||
$scene = $params['scene'] ?? '';//场景值 shop 店铺管理员 store 门店管理员 member 会员操作 cron 自动任务
|
||||
if ($is_auto) {
|
||||
$actioner_mode = '3';
|
||||
$actioner_id = 0;
|
||||
$actioner_name = '系统任务';
|
||||
} else {
|
||||
//todo 散客日志
|
||||
$actioner_mode = 1;
|
||||
$actioner_id = 0;
|
||||
$actioner_name = '';
|
||||
if ($member_id > 0) {
|
||||
|
||||
$member_condition = array(
|
||||
['member_id', '=', $member_id],
|
||||
);
|
||||
$member_info = $member_model->getMemberInfo($member_condition, 'nickname,headimg')['data'] ?? [];
|
||||
$actioner_mode = '1';
|
||||
$actioner_id = $member_id;
|
||||
$actioner_name = $member_info['nickname'] ?? '';
|
||||
}
|
||||
$operater_array = $params['operater'] ?? [];//操作管理员id
|
||||
if (!empty($operater_array)) {
|
||||
$user_model = new User();
|
||||
$operater = $operater_array['uid'];
|
||||
$user_condition = array(
|
||||
['uid', '=', $operater],
|
||||
['site_id', '=', $site_id]
|
||||
);
|
||||
$user_info = $user_model->getUserInfo($user_condition, 'username')['data'] ?? [];
|
||||
$actioner_mode = 2;
|
||||
$actioner_id = $operater;
|
||||
$actioner_name = $user_info['username'];
|
||||
}
|
||||
}
|
||||
$order_info = $this->getOrderDetail($order_id)['data'] ?? [];
|
||||
if (empty($order_info))
|
||||
return $this->error([], '订单不存在!');
|
||||
|
||||
// $balance_money = $order_info['balance_money'];//余额
|
||||
// $coupon_money = $order_info['coupon_money'];//优惠券金额
|
||||
// $point_money = $order_info['point_money'];//积分抵扣金额
|
||||
// $hongbao_money = $order_info['hongbao_money'];//红包金额
|
||||
$buyer_id = $order_info['member_id'];//买家
|
||||
if (!empty($member_info) && $buyer_id == $member_id) {
|
||||
$buyer_name = $member_info['nickname'];
|
||||
} else {
|
||||
$buyer_member_condition = array(
|
||||
['member_id', '=', $member_id],
|
||||
);
|
||||
$buyer_member_info = $member_model->getMemberInfo($buyer_member_condition, 'nickname,headimg')['data'] ?? [];
|
||||
$buyer_name = $buyer_member_info['nickname'] ?? '';
|
||||
}
|
||||
|
||||
// $shipping_money = $order_info['delivery_money'];//运费
|
||||
// $order_money = $order_info['order_money'];//订单金额
|
||||
// $pay_money = $order_info['pay_money'];//支付金额
|
||||
// $adjust_money = $order_info['adjust_money'];//调整金额
|
||||
$close_time = time_to_date($order_info['close_time']);
|
||||
// $trade_time = time_to_date($order_info['trade_time']);
|
||||
// $send_time = time_to_date($order_info['send_time']);
|
||||
$complete_time = time_to_date($order_info['finish_time']);
|
||||
// $pay_type_name = $order_info['pay_type_name'];
|
||||
$remark = $order_info['remark'];
|
||||
// $trade_type = $order_info['trade_type'];
|
||||
$close_cause = $order_info['close_cause'] ?? '';
|
||||
$order_scene = $order_info['order_scene'];
|
||||
|
||||
// $full_address = $order_info['full_address'];//详细地址
|
||||
|
||||
$order_info['action'] = $action;
|
||||
|
||||
$order_log = event('OrderLog', $order_info, true);
|
||||
if ($buyer_id > 0) {
|
||||
if (!empty($buyer_name)) {
|
||||
$buyer_name = '买家' . '【' . $buyer_name . '】';
|
||||
} else {
|
||||
$buyer_name = '买家';
|
||||
}
|
||||
} else {
|
||||
$buyer_name = '散客';
|
||||
}
|
||||
if (empty($order_log)) {
|
||||
switch ($action) {
|
||||
case 'create'://订单创建
|
||||
$content = $buyer_name . '下单了';
|
||||
break;
|
||||
case 'close'://订单关闭
|
||||
$content = '订单被关闭';
|
||||
if (!empty($close_cause)) {
|
||||
$content .= '关闭原因';
|
||||
}
|
||||
break;
|
||||
// case 'editaddress'://修改地址
|
||||
// $content = "订单修改收货地址,新收货地址为:{$full_address}";
|
||||
// break;
|
||||
// case 'adjust'://订单调价
|
||||
// $content = "卖家操作订单调价,调整金额:{$adjust_money},当前订单总额为:{$order_money}";
|
||||
// break;
|
||||
case 'pay'://订单支付
|
||||
if (isset($params['operater']) && !empty($params['operater'])) {
|
||||
$content = '收银员【' . $actioner_name . '】收款,订单支付成功';
|
||||
} else {
|
||||
$content = $buyer_name . '已支付订单';
|
||||
}
|
||||
break;
|
||||
// case 'delivery'://发货
|
||||
// $content = '卖家已发货,发货时间:' . $send_time;
|
||||
// break;
|
||||
// case 'receive'://收货
|
||||
// $content = '买家已收货,收货时间:' . $trade_time;
|
||||
// break;
|
||||
// case 'local'://配送
|
||||
// $content = '订单已经开始配送';
|
||||
// break;
|
||||
// case 'takelocal'://接收配送
|
||||
// $content = '买家已接收配送';
|
||||
// break;
|
||||
// case 'store'://提货
|
||||
// $content = '已提货';
|
||||
// break;
|
||||
case 'complete'://完成
|
||||
$content = '订单已完成';
|
||||
break;
|
||||
case 'remark'://卖家备注
|
||||
$content = '卖家备注:' . $remark;
|
||||
break;
|
||||
case 'refund':
|
||||
$content = $params['content'] ?? '';
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if ($order_log['code'] < 0) {
|
||||
return $order_log;
|
||||
}
|
||||
$content = $order_log['data']['content'];
|
||||
}
|
||||
$order_status_name = $order_info['order_status_name'];
|
||||
$order_status = $order_info['order_status'];
|
||||
$data = [
|
||||
'order_id' => $order_id,
|
||||
'order_status' => $order_status,
|
||||
'order_status_name' => $order_status_name,
|
||||
'action_way' => $actioner_mode,
|
||||
'uid' => $actioner_id,
|
||||
'nick_name' => $actioner_name,
|
||||
'action_time' => time(),
|
||||
'action' => $content,
|
||||
];
|
||||
$res = model('order_log')->add($data);
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
984
app/model/order/OrderMessage.php
Executable file
984
app/model/order/OrderMessage.php
Executable file
@@ -0,0 +1,984 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use addon\aliapp\model\Message as AliappMessage;
|
||||
use addon\weapp\model\Message as WeappMessage;
|
||||
use addon\wechat\model\Message as WechatMessage;
|
||||
use app\dict\order\OrderDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\member\Member;
|
||||
use app\model\message\Sms;
|
||||
use app\model\shop\ShopAcceptMessage;
|
||||
use app\model\system\Pay as PayModel;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
/**
|
||||
* 订单消息操作
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class OrderMessage extends BaseModel
|
||||
{
|
||||
|
||||
/************************************************ 会员消息 start ************************************************************/
|
||||
|
||||
/**
|
||||
* 订单催付通知
|
||||
* @param $data
|
||||
*/
|
||||
public function messageOrderUrgePayment($data)
|
||||
{
|
||||
trace('进入订单催付消息发送');
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $data['order_id']]], 'full_address,site_id,create_time,address,order_no,mobile,member_id,order_type,create_time,order_name,order_money');
|
||||
//计算订单自动关闭时间
|
||||
$order_config = Config::getOrderConfig();
|
||||
$execute_time = $order_info['create_time'] + $order_config['auto_close'] * 60; //自动关闭时间
|
||||
|
||||
//会员信息
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]])['data'];
|
||||
|
||||
// 发送短信
|
||||
if (!empty($member_info) && !empty($member_info['mobile'])) {
|
||||
$var_parse = [
|
||||
'goodsname' => replaceSpecialChar(str_sub($order_info['order_name'])),//商品名称
|
||||
'expiretime' => date('d', $execute_time) . '日' . date('H', $execute_time) . '时' . date('i', $execute_time) . '分'
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model = new Sms();
|
||||
$res = $sms_model->sendMessage($data);
|
||||
trace($res, '订单催付短信发送结果');
|
||||
}
|
||||
|
||||
// 【弃用,暂无模板信息,无法使用,等待后续微信支持后开发】公众号模板消息
|
||||
// if (!empty($member_info) && !empty($member_info[ "wx_openid" ])) {
|
||||
// $wechat_model = new WechatMessage();
|
||||
// $data[ "openid" ] = $member_info[ "wx_openid" ];
|
||||
// $data[ "template_data" ] = [
|
||||
// 'keyword1' => $order_info[ 'order_no' ],
|
||||
// 'keyword2' => $order_info[ 'order_name' ],
|
||||
// 'keyword3' => '待支付',
|
||||
// 'keyword4' => '请在' . time_to_date($execute_time) . '前完成支付'
|
||||
// ];
|
||||
// $data[ "page" ] = $this->handleUrl($order_info[ 'order_type' ], $data[ 'order_id' ]);
|
||||
// $res = $wechat_model->sendMessage($data);
|
||||
// trace($res, '订单催付公众号模板消息发送结果');
|
||||
// }
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string1' => [
|
||||
'value' => $order_info['order_no']
|
||||
],
|
||||
'thing2' => [
|
||||
'value' => $order_info['order_name']
|
||||
],
|
||||
'character_string3' => [
|
||||
'value' => $order_info['order_money'],
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $data['order_id']);
|
||||
$res = $weapp_model->sendMessage($data);
|
||||
trace($res, '订单催付小程序订阅消息发送结果');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理订单链接
|
||||
* @param $order_type
|
||||
* @param $order_id
|
||||
* @return string
|
||||
*/
|
||||
public function handleUrl($order_type, $order_id)
|
||||
{
|
||||
return 'pages/order/detail?order_id=' . $order_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送——支付成功
|
||||
* @param $params
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messagePaySuccess($params)
|
||||
{
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([['member_id', '=', $params['member_id']]]);
|
||||
$member_info = $member_info_result['data'];
|
||||
|
||||
// 发送短信
|
||||
if (!empty($member_info) && !empty($member_info['mobile'])) {
|
||||
$var_parse = [
|
||||
'orderno' => $params['order_no'],
|
||||
'username' => replaceSpecialChar($member_info['nickname']),
|
||||
'ordermoney' => $params['order_money'],
|
||||
];
|
||||
$params['sms_account'] = $member_info['mobile'] ?? '';//手机号
|
||||
$params['var_parse'] = $var_parse;
|
||||
$sms_model = new Sms();
|
||||
$res = $sms_model->sendMessage($params);
|
||||
trace($res, '订单支付短信发送结果');
|
||||
}
|
||||
|
||||
// 绑定微信公众号才发送
|
||||
if (!empty($member_info) && !empty($member_info['wx_openid'])) {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data = $params;
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'time4' => time_to_date($params['create_time']), // 下单时间
|
||||
'character_string2' => $params['order_no'], // 订单号
|
||||
'thing3' => str_sub($params['order_name']), // 商品名称
|
||||
'amount5' => $params['order_money'], // 支付金额
|
||||
];
|
||||
|
||||
$data['page'] = $this->handleUrl($params['order_type'], $params['order_id']);
|
||||
$res = $wechat_model->sendMessage($data);
|
||||
trace($res, '订单支付公众号发送结果');
|
||||
}
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data = $params;
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string1' => [
|
||||
'value' => $params['order_no']
|
||||
],
|
||||
'time2' => [
|
||||
'value' => time_to_date($params['create_time'])
|
||||
],
|
||||
'thing4' => [
|
||||
'value' => str_sub($params['order_name'])
|
||||
],
|
||||
'amount3' => [
|
||||
'value' => $params['order_money']
|
||||
],
|
||||
];
|
||||
$data['page'] = $this->handleUrl($params['order_type'], $params['order_id']);
|
||||
$res = $weapp_model->sendMessage($data);
|
||||
trace($res, '订单支付小程序订阅号发送结果');
|
||||
}
|
||||
|
||||
//发送支付宝订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['ali_openid']) && addon_is_exit('aliapp', $params['site_id'])) {
|
||||
$aliapp_model = new AliappMessage();
|
||||
$data = $params;
|
||||
$data['openid'] = $member_info['ali_openid'];
|
||||
$data['template_data'] = [
|
||||
'keyword1' => [
|
||||
'value' => '已支付'
|
||||
],
|
||||
'keyword2' => [
|
||||
'value' => time_to_date($params['create_time'])
|
||||
],
|
||||
'keyword3' => [
|
||||
'value' => str_sub($params['order_name'])
|
||||
],
|
||||
'keyword4' => [
|
||||
'value' => '¥' . $params['order_money']
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($params['order_type'], $params['order_id']);
|
||||
$res = $aliapp_model->sendMessage($data);
|
||||
trace($res, '订单支付支付宝小程序订阅号发送结果');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单关闭提醒
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderClose($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id,order_name,create_time,order_money,close_time');
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]])['data'];
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//商品名称
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
// 发送微信公众号模板消息
|
||||
if (!empty($member_info) && !empty($member_info['wx_openid'])) {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string1' => $order_info['order_no'], // 订单号
|
||||
'thing7' => str_sub($order_info['order_name']), // 商品名称
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
|
||||
// 发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'thing4' => [
|
||||
'value' => str_sub($order_info['order_name'])
|
||||
],
|
||||
'character_string1' => [
|
||||
'value' => $order_info['order_no']
|
||||
],
|
||||
'time3' => [
|
||||
'value' => time_to_date($order_info['create_time'])
|
||||
],
|
||||
'amount6' => [
|
||||
'value' => $order_info['order_money']
|
||||
],
|
||||
'time5' => [
|
||||
'value' => time_to_date($order_info['close_time'])
|
||||
],
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 买家订单完成通知商家
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageBuyerOrderComplete($data)
|
||||
{
|
||||
//发送短信
|
||||
// $sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id,order_name,create_time,finish_time');
|
||||
|
||||
$shop_accept_message_model = new ShopAcceptMessage();
|
||||
$result = $shop_accept_message_model->getShopAcceptMessageList();
|
||||
$list = $result['data'];
|
||||
|
||||
//发送模板消息
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $v) {
|
||||
if (!empty($v['wx_openid'])) {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $v['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string7' => $order_info['order_no'], // 订单号
|
||||
'thing5' => str_sub($order_info['order_name']), // 商品名称
|
||||
'time10' => time_to_date($order_info['finish_time']), // 完成时间
|
||||
];
|
||||
$data['page'] = $this->handleMobileShopUrl($order_info['order_type'], $order_id);
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理商家端订单页面路径
|
||||
* @param $order_type
|
||||
* @param $order_id
|
||||
* @return string
|
||||
*/
|
||||
public function handleMobileShopUrl($order_type, $order_id)
|
||||
{
|
||||
switch ($order_type) {
|
||||
case 2:
|
||||
return 'pages/order/detail/store?order_id=' . $order_id . '&template=store';
|
||||
case 3:
|
||||
return 'pages/order/detail/local?order_id=' . $order_id . '&template=local';
|
||||
case 4:
|
||||
return 'pages/order/detail/virtual?order_id=' . $order_id . '&template=virtual';
|
||||
default:
|
||||
return 'pages/order/detail/basis?order_id=' . $order_id . '&template=basis';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单完成提醒
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderComplete($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id,order_name,create_time,finish_time');
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]])['data'];
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//商品名称
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
//发送模板消息
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string7' => $order_info['order_no'],
|
||||
'thing5' => str_sub($order_info['order_name']),
|
||||
'time10' => time_to_date($order_info['create_time']),
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$wechat_model->sendMessage($data);
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string1' => [
|
||||
'value' => $order_info['order_no']
|
||||
],
|
||||
'thing2' => [
|
||||
'value' => str_sub($order_info['order_name'])
|
||||
],
|
||||
'time4' => [
|
||||
'value' => time_to_date($order_info['finish_time'])
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单发货提醒
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderDelivery($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id,order_name,goods_num,order_money,delivery_time');
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]]);
|
||||
$member_info = $member_info_result['data'];
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//商品名称
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
// 发送模板消息
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string2' => $order_info['order_no'], // 订单编号
|
||||
'thing4' => str_sub($order_info['order_name']), // 商品名称
|
||||
'number5' => numberFormat($order_info['goods_num']), // 商品数量
|
||||
'amount8' => $order_info['order_money'], // 订单金额
|
||||
'time12' => time_to_date($order_info['delivery_time']), // 发货时间
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$wechat_model->sendMessage($data);
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string2' => [
|
||||
'value' => $order_info['order_no']
|
||||
],
|
||||
'thing1' => [
|
||||
'value' => str_sub($order_info['order_name'])
|
||||
],
|
||||
'amount7' => [
|
||||
'value' => $order_info['order_money']
|
||||
],
|
||||
'date3' => [
|
||||
'value' => time_to_date($order_info['delivery_time'] ? $order_info['delivery_time'] : time())
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单收货提醒
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderTakeDelivery($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id,full_address,address,name,order_name,sign_time');
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]]);
|
||||
$member_info = $member_info_result['data'];
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//商品名称
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
//发送模板消息
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'keyword1' => $order_info['full_address'] . $order_info['address'],
|
||||
'keyword2' => $order_info['name'],
|
||||
'keyword3' => $order_info['order_no'],
|
||||
'keyword4' => str_sub($order_info['order_name']),
|
||||
'keyword5' => time_to_date($order_info['sign_time']),
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$wechat_model->sendMessage($data);
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string1' => [
|
||||
'value' => $order_info['order_no']
|
||||
],
|
||||
'thing2' => [
|
||||
'value' => str_sub($order_info['order_name'])
|
||||
],
|
||||
'time7' => [
|
||||
'value' => time_to_date($order_info['sign_time'])
|
||||
],
|
||||
'thing9' => [
|
||||
'value' => str_sub($order_info['name'])
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单退款同意提醒
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderRefundAgree($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id');
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]]);
|
||||
$member_info = $member_info_result['data'];
|
||||
|
||||
$order_goods_info = model('order_goods')->getInfo([['order_goods_id', '=', $data['order_goods_id']]], 'refund_apply_money,refund_time,refund_action_time');
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//商品名称
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
//发送模板消息
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string5' => $order_info['order_no'], // 订单编号
|
||||
'amount2' => $order_goods_info['refund_apply_money'], // 退款金额
|
||||
'time4' => time_to_date(time()), // 退款时间
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$wechat_model->sendMessage($data);
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string3' => [
|
||||
'value' => $order_info['order_no']
|
||||
],
|
||||
'amount1' => [
|
||||
'value' => $order_goods_info['refund_apply_money']
|
||||
],
|
||||
'phrase7' => [
|
||||
'value' => '成功'
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单退款拒绝提醒
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderRefundRefuse($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id');
|
||||
$order_goods_info = model('order_goods')->getInfo([['order_goods_id', '=', $data['order_goods_id']]], 'refund_apply_money,refund_time,refund_action_time');
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info_result = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]]);
|
||||
$member_info = $member_info_result['data'];
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//商品名称
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
//发送模板消息
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string1' => $order_info['order_no'], // 订单编号
|
||||
'amount2' => $order_goods_info['refund_apply_money'], // 退款金额
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$wechat_model->sendMessage($data);
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string4' => [
|
||||
'value' => $order_info['order_no']
|
||||
],
|
||||
'amount3' => [
|
||||
'value' => $order_goods_info['refund_apply_money']
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $order_id);
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 核销码过期提醒
|
||||
* @param $data
|
||||
*/
|
||||
public function messageVerifyCodeExpire($data)
|
||||
{
|
||||
// 发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
// 商品表
|
||||
$goods_virtual_info = model('goods_virtual')->getInfo([['order_id', '=', $data['relate_id']]]);
|
||||
|
||||
// 总核销次数
|
||||
$total_verify_num = model('goods_virtual')->getCount([['order_id', '=', $data['relate_id']]]);
|
||||
|
||||
// 已核销次数
|
||||
$verify_num = model('goods_virtual')->getCount([['order_id', '=', $data['relate_id']], ['is_veirfy', '=', 1]]);
|
||||
|
||||
// 剩余次数
|
||||
$residue = $total_verify_num - $verify_num;
|
||||
|
||||
// 用户信息
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $goods_virtual_info['member_id']]])['data'];
|
||||
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $goods_virtual_info['order_id']]], 'mobile,order_no,order_name,order_type,pay_time');
|
||||
trace($residue);
|
||||
if ($residue > 0) {
|
||||
|
||||
// 公众号模板消息
|
||||
// 【弃用,暂无模板信息,无法使用,等待后续微信支持后开发】绑定微信公众号才发送
|
||||
// if (!empty($member_info) && !empty($member_info[ "wx_openid" ])) {
|
||||
// $wechat_model = new WechatMessage();
|
||||
// $data[ "openid" ] = $member_info[ "wx_openid" ];
|
||||
// $data[ "template_data" ] = [
|
||||
// 'keyword1' => $order_info[ 'order_no' ],
|
||||
// 'keyword2' => $order_info[ 'order_name' ],
|
||||
// 'keyword3' => '已过期'
|
||||
// ];
|
||||
// $wechat_model->sendMessage($data);
|
||||
// }
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string1' => [
|
||||
'value' => $order_info['order_no'],
|
||||
],
|
||||
'time2' => [
|
||||
'value' => time_to_date($order_info['pay_time'])
|
||||
],
|
||||
'time3' => [
|
||||
'value' => time_to_date(time())
|
||||
],
|
||||
'thing5' => [
|
||||
'value' => '您的订单核销码已过期',
|
||||
],
|
||||
];
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
// 短信通知
|
||||
if (!empty($member_info) && !empty($member_info['mobile'])) {
|
||||
//"desc" => '您购买的'.$goods_virtual_info['sku_name'].'将在'.date('Y-m-d H:i:s',$goods_virtual_info['expire_time']).'到期',//商品名称,
|
||||
|
||||
$var_parse = [
|
||||
'sitename' => replaceSpecialChar($data['site_info']['site_name']),
|
||||
'sku_name' => $goods_virtual_info['sku_name']
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/************************************************ 会员消息 end ************************************************************/
|
||||
|
||||
/**
|
||||
* 核销商品临期提醒
|
||||
* @param $data
|
||||
*/
|
||||
public function messageVerifyOrderOutTime($data)
|
||||
{
|
||||
// 商品表
|
||||
$goods_virtual_info = model('goods_virtual')->getInfo([['order_id', '=', $data['order_id']]]);
|
||||
// 总核销次数
|
||||
$total_verify_num = model('goods_virtual')->getCount([['order_id', '=', $data['order_id']]]);
|
||||
// 已核销次数
|
||||
$verify_num = model('goods_virtual')->getCount([['order_id', '=', $data['order_id']], ['is_veirfy', '=', 1]]);
|
||||
// 剩余次数
|
||||
$residue = $total_verify_num - $verify_num;
|
||||
|
||||
// 用户信息
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $goods_virtual_info['member_id']]])['data'];
|
||||
|
||||
// 手机号
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $data['order_id']]], 'order_type,mobile,order_no,order_name');
|
||||
if ($residue > 0) {
|
||||
// 公众号模板消息
|
||||
// 【弃用,暂无模板信息,无法使用,等待后续微信支持后开发】绑定微信公众号才发送
|
||||
// if (!empty($member_info) && !empty($member_info[ "wx_openid" ])) {
|
||||
// $wechat_model = new WechatMessage();
|
||||
// $data[ "openid" ] = $member_info[ "wx_openid" ];
|
||||
// $data[ "template_data" ] = [
|
||||
// 'keyword1' => $order_info[ 'order_no' ],
|
||||
// 'keyword2' => str_sub($order_info[ 'order_name' ]),
|
||||
// 'keyword3' => '未核销'
|
||||
// ];
|
||||
// $wechat_model->sendMessage($data);
|
||||
// }
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
// 核销码临近到期时间(小时)
|
||||
$config_model = new Config();
|
||||
$verify_config = $config_model->getOrderVerifyConfig($data['site_id'])['data']['value'];
|
||||
$order_verify_out_time = $verify_config['order_verify_time_out'] ?? 24;
|
||||
$time_strtime = $order_verify_out_time * 3600;
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
|
||||
$data['template_data'] = [
|
||||
'thing1' => [
|
||||
'value' => str_sub($order_info['order_name'])
|
||||
],
|
||||
'date2' => [
|
||||
'value' => time_to_date(time() + $time_strtime),
|
||||
],
|
||||
'thing3' => [
|
||||
'value' => '请在到期前核销,以免影响您的使用!',
|
||||
]
|
||||
];
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
|
||||
// 短信消息
|
||||
$sms = new Sms();
|
||||
|
||||
$var_parse = [
|
||||
'username' => $member_info['username'],//用户名称
|
||||
'sku_name' => $goods_virtual_info['sku_name'],//商品名称
|
||||
'expire_time' => date('Y-m-d H:i:s', $goods_virtual_info['expire_time'])//到期时间
|
||||
];
|
||||
$data['sms_account'] = $order_info['phone'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms->sendMessage($data);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单核销通知
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderVerify($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_type,order_no,mobile,member_id,order_name,goods_num,sign_time,delivery_store_name');
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]])['data'];
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//订单编号
|
||||
];
|
||||
$data['sms_account'] = $member_info['mobile'];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
$sms_model->sendMessage($data);
|
||||
|
||||
// 公众号模板消息
|
||||
//绑定微信公众号才发送
|
||||
if (!empty($member_info) && !empty($member_info['wx_openid'])) {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $member_info['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'thing2' => str_sub($order_info['order_name']),// 商品名称
|
||||
'character_string6' => 1, // 核销数量
|
||||
'time4' => time_to_date(time()), // 核销时间
|
||||
];
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
|
||||
//发送订阅消息
|
||||
if (!empty($member_info) && !empty($member_info['weapp_openid'])) {
|
||||
$weapp_model = new WeappMessage();
|
||||
$data['openid'] = $member_info['weapp_openid'];
|
||||
$data['template_data'] = [
|
||||
'phrase1' => [
|
||||
'value' => '已核销'
|
||||
],
|
||||
'time2' => [
|
||||
'value' => time_to_date(time()),
|
||||
],
|
||||
'character_string3' => [
|
||||
'value' => $order_info['order_no'],
|
||||
]
|
||||
];
|
||||
$data['page'] = $this->handleUrl($order_info['order_type'], $data['order_id']);
|
||||
$weapp_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 买家发起退款,卖家通知
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderRefundApply($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_goods_id = $data['order_goods_id'];
|
||||
$order_goods_info = model('order_goods')->getInfo(['order_goods_id' => $order_goods_id], '*');
|
||||
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_goods_info['order_id']]], 'order_no,mobile,member_id,site_id,name,order_type');
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $order_info['member_id']]])['data'];
|
||||
|
||||
$var_parse = [
|
||||
'username' => replaceSpecialChar($member_info['nickname']),//会员名
|
||||
'orderno' => $order_info['order_no'],//订单编号
|
||||
'goodsname' => mb_substr(replaceSpecialChar($order_goods_info['sku_name']), 0, 25, 'UTF8'),//商品名称
|
||||
'refundno' => $order_goods_info['refund_no'],//退款编号
|
||||
'refundmoney' => $order_goods_info['refund_apply_money'],//退款申请金额
|
||||
'refundreason' => replaceSpecialChar($order_goods_info['refund_reason']),//退款原因
|
||||
];
|
||||
$data['var_parse'] = $var_parse;
|
||||
|
||||
// $site_id = $data['site_id'];
|
||||
// $shop_info = model("shop")->getInfo([["site_id", "=", $site_id]], "mobile,email");
|
||||
// $message_data["sms_account"] = $shop_info["mobile"];//手机号
|
||||
$shop_accept_message_model = new ShopAcceptMessage();
|
||||
$result = $shop_accept_message_model->getShopAcceptMessageList();
|
||||
$list = $result['data'];
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $v) {
|
||||
$message_data = $data;
|
||||
$message_data['sms_account'] = $v['mobile'];//手机号
|
||||
$sms_model->sendMessage($message_data);
|
||||
|
||||
if ($v['wx_openid'] != '') {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $v['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string3' => $order_goods_info['order_no'], // 订单编号
|
||||
'time10' => time_to_date($order_goods_info['refund_action_time']), // 申请时间
|
||||
'thing2' => str_sub($order_goods_info['sku_name']), // 商品名称
|
||||
'amount1' => $order_goods_info['refund_apply_money'], // 退款金额
|
||||
];
|
||||
$data['page'] = 'pages/order/refund/detail?order_goods_id=' . $order_goods_id;
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 买家已退款,卖家通知
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageOrderRefundDelivery($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
$order_id = $data['order_goods_info']['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_no');
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $order_info['order_no'],//商品名称
|
||||
];
|
||||
|
||||
// $site_id = $data['site_id'];
|
||||
// $shop_info = model("shop")->getInfo([["site_id", "=", $site_id]], "mobile,email");
|
||||
// $message_data["sms_account"] = $shop_info["mobile"];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
|
||||
$shop_accept_message_model = new ShopAcceptMessage();
|
||||
$result = $shop_accept_message_model->getShopAcceptMessageList();
|
||||
$list = $result['data'];
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $v) {
|
||||
$message_data = $data;
|
||||
$message_data['sms_account'] = $v['mobile'];//手机号
|
||||
$sms_model->sendMessage($message_data);
|
||||
|
||||
if ($v['wx_openid'] != '') {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $v['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'character_string6' => $data['order_goods_info']['order_no'], // 订单号
|
||||
'thing3' => str_sub($data['order_goods_info']['sku_name']), // 商品名称
|
||||
'time4' => time_to_date(time()), // 申请时间
|
||||
];
|
||||
$data['page'] = 'pages/order/refund/detail?order_goods_id=' . $data['order_goods_info']['order_goods_id'];
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 买家支付成功,卖家通知
|
||||
* @param $data
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function messageBuyerPaySuccess($data)
|
||||
{
|
||||
//发送短信
|
||||
$sms_model = new Sms();
|
||||
|
||||
$var_parse = [
|
||||
'orderno' => $data['order_no'],//订单编号
|
||||
'ordermoney' => $data['order_money'],//退款申请金额
|
||||
];
|
||||
// $site_id = $data['site_id'];
|
||||
// $shop_info = model("shop")->getInfo([["site_id", "=", $site_id]], "mobile,email");
|
||||
// $message_data["sms_account"] = $shop_info["mobile"];//手机号
|
||||
$data['var_parse'] = $var_parse;
|
||||
|
||||
$shop_accept_message_model = new ShopAcceptMessage();
|
||||
$result = $shop_accept_message_model->getShopAcceptMessageList();
|
||||
$list = $result['data'];
|
||||
if (!empty($list)) {
|
||||
foreach ($list as $v) {
|
||||
$message_data = $data;
|
||||
$message_data['sms_account'] = $v['mobile'];//手机号
|
||||
$sms_model->sendMessage($message_data);
|
||||
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberInfo([['member_id', '=', $data['member_id']]])['data'];
|
||||
|
||||
if ($v['wx_openid'] != '') {
|
||||
$wechat_model = new WechatMessage();
|
||||
$data['openid'] = $v['wx_openid'];
|
||||
$data['template_data'] = [
|
||||
'time4' => time_to_date($data['pay_time']), // 支付时间
|
||||
'character_string2' => $data['order_no'], // 订单编号
|
||||
'thing1' => str_sub($member_info['nickname'], 16), // 客户名称
|
||||
'thing8' => str_sub($data['order_name']), // 产品名称
|
||||
'amount3' => $data['order_money'], // 订单金额
|
||||
];
|
||||
$data['page'] = $this->handleMobileShopUrl($data['order_type'], $data['order_id']);
|
||||
$wechat_model->sendMessage($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 外卖订单 指定配送员后 同步短信推送
|
||||
* @param $param
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function messageLocalWaitDelivery($param)
|
||||
{
|
||||
//短信推送业务
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $param['param']['order_id']]], 'out_trade_no');
|
||||
$pay_model = new PayModel();
|
||||
$pay_info = $pay_model->getPayInfo($order_info['out_trade_no'])['data'];
|
||||
if (!empty($pay_info)) {
|
||||
$sms_model = new Sms();
|
||||
if (!empty($param['param']['deliverer_mobile'])) {
|
||||
$message_data = [
|
||||
'var_parse' => [
|
||||
'order_name' => str_sub(replaceSpecialChar($pay_info['pay_body']), 25),
|
||||
'pay_money' => $pay_info['pay_money'],
|
||||
'out_trade_no' => $pay_info['out_trade_no'],
|
||||
],
|
||||
'sms_account' => $param['param']['deliverer_mobile'],
|
||||
];
|
||||
$sms_model->sendMessage(array_merge($param, $message_data));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
61
app/model/order/OrderPay.php
Executable file
61
app/model/order/OrderPay.php
Executable file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
|
||||
use app\model\system\Pay;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 订单支付相关
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class OrderPay extends OrderCommon
|
||||
{
|
||||
/**
|
||||
* 改变订单的交易流水号
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function resetOrderTradeNo($params)
|
||||
{
|
||||
$out_trade_no = $params['out_trade_no'];
|
||||
$order_condition = array(
|
||||
['pay_status', '=', 0]
|
||||
);
|
||||
$order_condition[] = ['out_trade_no', '=', $out_trade_no];
|
||||
$order_info = model('order')->getInfo($order_condition, 'pay_money,order_name,out_trade_no,order_id,pay_status,site_id,member_id,member_card_order');
|
||||
//判断订单数是否匹配
|
||||
if (empty($order_info))
|
||||
return $this->error([], '没有可支付订单!');
|
||||
|
||||
$pay_model = new Pay();
|
||||
$result = $pay_model->closePay($out_trade_no);//关闭旧支付单据
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$member_id = $order_info['member_id'];
|
||||
$new_out_trade_no = $pay_model->createOutTradeNo($member_id ?? 0);
|
||||
$update_data = array(
|
||||
'out_trade_no' => $new_out_trade_no
|
||||
);
|
||||
model('order')->update($update_data, [['out_trade_no', '=', $out_trade_no], ['pay_status', '=', 0]]);
|
||||
model('member_level_order')->update($update_data, [['out_trade_no', '=', $out_trade_no], ['pay_status', '=', 0]]);
|
||||
Log::write('resetOrderTradeNo_old_'.$out_trade_no.'_new_'.$new_out_trade_no);
|
||||
|
||||
$pay_model->addPay($order_info['site_id'], $new_out_trade_no, '', $order_info['order_name'], $order_info['order_name'], $order_info['pay_money'], '', 'OrderPayNotify', '', $order_info['order_id'], $order_info['member_id']);
|
||||
return $this->success($new_out_trade_no);
|
||||
}
|
||||
}
|
||||
1440
app/model/order/OrderRefund.php
Executable file
1440
app/model/order/OrderRefund.php
Executable file
File diff suppressed because it is too large
Load Diff
286
app/model/order/OrderStock.php
Executable file
286
app/model/order/OrderStock.php
Executable file
@@ -0,0 +1,286 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use addon\stock\model\stock\Stock as StockAddonModel;
|
||||
use app\dict\goods\GoodsDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\stock\GoodsStock;
|
||||
use app\model\stock\SaleStock;
|
||||
use app\model\store\Store;
|
||||
use app\model\storegoods\StoreGoods;
|
||||
use extend\exception\StockException;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 商品库存
|
||||
*/
|
||||
class OrderStock extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 扣除订单库存
|
||||
* @param $sku_id
|
||||
* @param $num
|
||||
* @param int $store_id
|
||||
* @param array $sku_info
|
||||
* @param array $create_data
|
||||
* @return array
|
||||
*/
|
||||
public function decOrderSaleStock($params)
|
||||
{
|
||||
$store_id = $params['store_id'] ?? 0;
|
||||
$goods_sku_data = $params['goods_sku_data'] ?? [];
|
||||
$sale_stock_model = new SaleStock();
|
||||
$create_order_data = $params['create_order_data'];
|
||||
if ($store_id > 0) {
|
||||
$store_info = $create_order_data['store_info'] ?? [];
|
||||
if (empty($store_info)) {
|
||||
$store_model = new Store();
|
||||
$store_condition = [
|
||||
['store_id', '=', $store_id]
|
||||
];
|
||||
$store_info = $store_model->getStoreInfo($store_condition)['data'] ?? [];
|
||||
}
|
||||
$stock_type = $store_info['stock_type'];
|
||||
if ($stock_type == 'all') {//如果总部统一库存的话就扣除总店的
|
||||
$store_id = 0;
|
||||
}
|
||||
}
|
||||
$params = [
|
||||
'goods_sku_list' => $goods_sku_data,
|
||||
'store_id' => $store_id,
|
||||
'is_allow_negative' => false
|
||||
];
|
||||
$sale_stock_result = $sale_stock_model->decGoodsStock($params);
|
||||
if ($sale_stock_result['code'] < 0) {
|
||||
return $sale_stock_result;
|
||||
}
|
||||
return $this->success();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 返还订单库存
|
||||
* @param $sku_id
|
||||
* @param $num
|
||||
* @param int $store_id
|
||||
* @return array
|
||||
*/
|
||||
public function incOrderSaleStock($params)
|
||||
{
|
||||
$store_id = $params['store_id'] ?? 0;
|
||||
$goods_sku_data = $params['goods_sku_data'] ?? [];
|
||||
$sale_stock_model = new SaleStock();
|
||||
if ($store_id > 0) {
|
||||
$store_model = new Store();
|
||||
$store_condition = [
|
||||
['store_id', '=', $store_id]
|
||||
];
|
||||
$store_info = $store_model->getStoreInfo($store_condition)['data'] ?? [];
|
||||
$stock_type = $store_info['stock_type'];
|
||||
if ($stock_type == 'all') {//如果总部统一库存的话就扣除总店的
|
||||
$store_id = 0;
|
||||
}
|
||||
}
|
||||
$params = [
|
||||
'goods_sku_list' => $goods_sku_data,
|
||||
'store_id' => $store_id
|
||||
];
|
||||
$sale_stock_result = $sale_stock_model->incGoodsStock($params);
|
||||
if ($sale_stock_result['code'] < 0) {
|
||||
return $sale_stock_result;
|
||||
}
|
||||
return $this->success();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 扣除库存(用于订单)
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function decOrderStock($params)
|
||||
{
|
||||
$params['is_out_stock'] = $params['is_out_stock'] ?? 0;//不再改变销售库存
|
||||
$store_id = $params['store_id'] ?? 0;
|
||||
//是否允许负库存
|
||||
$is_allow_negative = $params['is_allow_negative'] ?? false;
|
||||
if ($store_id > 0) {
|
||||
$store_model = new Store();
|
||||
$store_condition = [
|
||||
['store_id', '=', $store_id]
|
||||
];
|
||||
$store_info = $store_model->getStoreInfo($store_condition)['data'] ?? [];
|
||||
$stock_type = $store_info['stock_type'];
|
||||
if ($stock_type == 'all') {//如果总部统一库存的话就扣除总店的
|
||||
$params['store_id'] = 0;
|
||||
}
|
||||
}
|
||||
$is_exist = addon_is_exit('stock');
|
||||
if ($is_exist) {
|
||||
$stock_model = new StockAddonModel();
|
||||
}
|
||||
$goods_sku_list = $params['goods_sku_list'] ?? [];
|
||||
if (!empty($goods_sku_list)) {
|
||||
$goods_sku_list_1 = [];
|
||||
$goods_sku_list_2 = [];
|
||||
|
||||
foreach ($goods_sku_list as $k => $v) {
|
||||
if ($is_exist && $v['goods_class'] == GoodsDict::real) {
|
||||
$goods_sku_list_1[] = $v;
|
||||
} else {
|
||||
if (in_array($v['goods_class'], [GoodsDict::real, GoodsDict::virtual, GoodsDict::virtualcard, GoodsDict::service, GoodsDict::card, GoodsDict::weigh])) {
|
||||
$goods_sku_list_2[] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($goods_sku_list_1)) {
|
||||
$params['goods_sku_list'] = $goods_sku_list_1;
|
||||
$params['key'] = 'SEAILCK';
|
||||
$result = $stock_model->changeStock($params);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
if (!empty($goods_sku_list_2)) {
|
||||
$params['goods_sku_list'] = $goods_sku_list_2;
|
||||
$goods_stock_model = new GoodsStock();
|
||||
$result = $goods_stock_model->decGoodsStock($params);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$goods_class = $params['goods_class'];
|
||||
$params['stock'] = $params['num'] ?? $params['stock'];
|
||||
if ($is_exist && $goods_class == GoodsDict::real) {
|
||||
$params['key'] = 'SEAILCK';
|
||||
$result = $stock_model->changeStock($params);
|
||||
} else {//没有的话直接生成支付单据
|
||||
$goods_stock_model = new GoodsStock();
|
||||
$result = $goods_stock_model->decGoodsStock($params);
|
||||
}
|
||||
}
|
||||
|
||||
return $result ?? $this->success();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 返还库存(用于订单)
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function incOrderStock($params)
|
||||
{
|
||||
$store_id = $params['store_id'] ?? 0;
|
||||
if ($store_id > 0) {
|
||||
$store_model = new Store();
|
||||
$store_condition = [
|
||||
['store_id', '=', $store_id]
|
||||
];
|
||||
$store_info = $store_model->getStoreInfo($store_condition)['data'] ?? [];
|
||||
$stock_type = $store_info['stock_type'];
|
||||
if ($stock_type == 'all') {//如果总部统一库存的话就返还总店的
|
||||
$params['store_id'] = 0;
|
||||
}
|
||||
}
|
||||
$is_exist = addon_is_exit('stock');
|
||||
if ($is_exist) {
|
||||
$stock_model = new StockAddonModel();
|
||||
}
|
||||
$goods_sku_list = $params['goods_sku_list'] ?? [];
|
||||
if (!empty($goods_sku_list)) {
|
||||
$goods_sku_list_1 = [];
|
||||
$goods_sku_list_2 = [];
|
||||
|
||||
foreach ($goods_sku_list as $v) {
|
||||
if ($is_exist && $v['goods_class'] == GoodsDict::real) {
|
||||
$goods_sku_list_1[] = $v;
|
||||
} else {
|
||||
if (in_array($v['goods_class'], [GoodsDict::real, GoodsDict::virtual, GoodsDict::virtualcard, GoodsDict::service, GoodsDict::card, GoodsDict::weigh])) {
|
||||
$goods_sku_list_2[] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($goods_sku_list_1)) {
|
||||
|
||||
$params['goods_sku_list'] = $goods_sku_list_1;
|
||||
$params['key'] = 'REFUND';
|
||||
$result = $stock_model->changeStock($params);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
if (!empty($goods_sku_list_2)) {
|
||||
$params['goods_sku_list'] = $goods_sku_list_2;
|
||||
$goods_stock_model = new GoodsStock();
|
||||
$result = $goods_stock_model->incGoodsStock($params);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$goods_class = $params['goods_class'];
|
||||
$params['stock'] = $params['num'] ?? $params['stock'];
|
||||
if ($is_exist && $goods_class == GoodsDict::real) {
|
||||
$params['key'] = 'REFUND';
|
||||
$result = $stock_model->changeStock($params);
|
||||
} else {//没有的话直接生成支付单据
|
||||
$goods_stock_model = new GoodsStock();
|
||||
$result = $goods_stock_model->incGoodsStock($params);
|
||||
}
|
||||
}
|
||||
return $result ?? $this->success();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 校验商品库存是否足够
|
||||
* @return void
|
||||
*/
|
||||
public function checkStock($params){
|
||||
$store_id = $params['store_id'] ?? 0;
|
||||
$goods_sku_data = $params['goods_sku_data'];
|
||||
$store_model = new Store();
|
||||
if ($store_id > 0) {
|
||||
$store_condition = [
|
||||
['store_id', '=', $store_id]
|
||||
];
|
||||
$store_info = $store_model->getStoreInfo($store_condition)['data'] ?? [];
|
||||
$stock_type = $store_info['stock_type'];
|
||||
if ($stock_type == 'all') {//如果总部统一库存的话就扣除总店的
|
||||
$store_id = 0;
|
||||
}
|
||||
}
|
||||
if($store_id == 0){
|
||||
$store_info = $store_model->getDefaultStore()['data'] ?? [];
|
||||
$store_id = $store_info['store_id'];
|
||||
}
|
||||
$goods_sku_ids = array_column($goods_sku_data, 'sku_id');
|
||||
$goods_sku_list = model('store_goods_sku')->getColumn([['sku_id', 'in', $goods_sku_ids], ['store_id', '=', $store_id]], '*', 'sku_id');
|
||||
foreach($goods_sku_data as $v){
|
||||
$item_sku_id = $v['sku_id'];
|
||||
$item_sku_name = $v['sku_name'];
|
||||
$item_goods_sku = $goods_sku_list[$item_sku_id] ?? [];
|
||||
|
||||
if(!$item_goods_sku) throw new StockException('商品:【'.$item_sku_name.'】 库存不足!');
|
||||
|
||||
$item_num = $v['num'];
|
||||
if($item_num > $item_goods_sku['stock']) throw new StockException('商品:【'.$item_sku_name.'】 库存不足!');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
345
app/model/order/StoreOrder.php
Executable file
345
app/model/order/StoreOrder.php
Executable file
@@ -0,0 +1,345 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use app\dict\order\OrderGoodsDict;
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\message\Message;
|
||||
use app\model\verify\Verify;
|
||||
use think\db\exception\DbException;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 门店自提订单
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
class StoreOrder extends OrderCommon
|
||||
{
|
||||
|
||||
/************************************************************* 订单状态 ***********************************************/
|
||||
// 订单创建
|
||||
public const ORDER_CREATE = 0;
|
||||
|
||||
// 订单已支付
|
||||
public const ORDER_PAY = 1;
|
||||
|
||||
// 订单待提货
|
||||
public const ORDER_PENDING_DELIVERY = 2;
|
||||
|
||||
// 订单已发货(配货)
|
||||
public const ORDER_DELIVERY = 3;
|
||||
|
||||
// 订单已收货
|
||||
public const ORDER_TAKE_DELIVERY = 4;
|
||||
|
||||
// 订单已结算完成
|
||||
public const ORDER_COMPLETE = 10;
|
||||
|
||||
// 订单已关闭
|
||||
public const ORDER_CLOSE = -1;
|
||||
|
||||
// 订单类型
|
||||
public $order_type = 2;
|
||||
|
||||
// 订单状态
|
||||
public $order_status = [
|
||||
self::ORDER_CREATE => [
|
||||
'status' => self::ORDER_CREATE,
|
||||
'name' => '待支付',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAdjustMoney',
|
||||
'title' => '调整价格',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderPay',
|
||||
'title' => '支付',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_PENDING_DELIVERY => [
|
||||
'status' => self::ORDER_PENDING_DELIVERY,
|
||||
'name' => '待提货',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-send.png',
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_TAKE_DELIVERY => [
|
||||
'status' => self::ORDER_TAKE_DELIVERY,
|
||||
'name' => '已提货',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_COMPLETE => [
|
||||
'status' => self::ORDER_COMPLETE,
|
||||
'name' => '已完成',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_CLOSE => [
|
||||
'status' => self::ORDER_CLOSE,
|
||||
'name' => '已关闭',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-close.png',
|
||||
'action' => [
|
||||
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* 门店订单
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws DbException
|
||||
*/
|
||||
public function orderPay($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$member_info = $data['member_info'];
|
||||
$pay_type = $data['pay_type'];
|
||||
$member_id = $order_info['member_id'] ?? 0;
|
||||
$log_data = $data['log_data'] ?? [];
|
||||
$condition = [
|
||||
['order_id', '=', $order_info['order_id']],
|
||||
['order_status', '=', self::ORDER_CREATE],
|
||||
];
|
||||
$verify = new Verify();
|
||||
$order_goods_list = model('order_goods')->getList([['order_id', '=', $order_info['order_id']]], 'sku_image,sku_name,price,num,order_goods_id,goods_id,sku_id');
|
||||
$item_array = [];
|
||||
foreach ($order_goods_list as $k => $v) {
|
||||
$item_array[] = [
|
||||
'img' => $v['sku_image'],
|
||||
'name' => $v['sku_name'],
|
||||
'price' => $v['price'],
|
||||
'num' => numberFormat($v['num']),
|
||||
'order_goods_id' => $v['order_goods_id'],
|
||||
'remark_array' => [
|
||||
|
||||
]
|
||||
];
|
||||
// 增加门店商品销量
|
||||
model('store_goods')->setInc([['goods_id', '=', $v['goods_id']], ['store_id', '=', $order_info['delivery_store_id']]], 'sale_num', $v['num']);
|
||||
model('store_goods_sku')->setInc([['sku_id', '=', $v['sku_id']], ['store_id', '=', $order_info['delivery_store_id']]], 'sale_num', $v['num']);
|
||||
}
|
||||
$pay_time = time();
|
||||
$remark_array = [
|
||||
['title' => '订单金额', 'value' => $order_info['order_money']],
|
||||
['title' => '订单编号', 'value' => $order_info['order_no']],
|
||||
['title' => '创建时间', 'value' => time_to_date($order_info['create_time'])],
|
||||
['title' => '付款时间', 'value' => time_to_date($pay_time)],
|
||||
['title' => '收货地址', 'value' => $order_info['full_address']],
|
||||
['title' => '选择门店', 'value' => $order_info['delivery_store_name']],
|
||||
];
|
||||
$verify_content_json = $verify->getVerifyJson($item_array, $remark_array);
|
||||
|
||||
$code = $verify->addVerify('pickup', $order_info['site_id'], $order_info['site_name'], $verify_content_json, 0, 1, $order_info['delivery_store_id'], $member_id);
|
||||
$pay_type_list = $this->getPayType();
|
||||
$data = [
|
||||
'order_status' => self::ORDER_PENDING_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PENDING_DELIVERY]['name'],
|
||||
'pay_status' => 1,
|
||||
'order_status_action' => json_encode($this->order_status[self::ORDER_PENDING_DELIVERY], JSON_UNESCAPED_UNICODE),
|
||||
'delivery_code' => $code['data']['verify_code'],
|
||||
'pay_time' => $pay_time,
|
||||
'is_enable_refund' => 1,
|
||||
'pay_type' => $pay_type,
|
||||
'pay_type_name' => $pay_type_list[$pay_type]
|
||||
];
|
||||
|
||||
//记录订单日志 start
|
||||
$action = '商家对订单进行了线下支付';
|
||||
//获取用户信息
|
||||
if (empty($log_data)) {
|
||||
$log_data = [
|
||||
'uid' => $order_info['member_id'],
|
||||
'nick_name' => $order_info['name'],
|
||||
'action_way' => 1
|
||||
];
|
||||
$action = '买家支付了订单';
|
||||
}
|
||||
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_info['order_id'],
|
||||
'action' => $action,
|
||||
'order_status' => self::ORDER_PENDING_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PENDING_DELIVERY]['name']
|
||||
]);
|
||||
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
//记录订单日志 end
|
||||
|
||||
model('order')->update($data, $condition);
|
||||
|
||||
$order_goods_data = [
|
||||
'delivery_status_name' => '待提货'
|
||||
];
|
||||
|
||||
$res = model('order_goods')->update($order_goods_data, [['order_id', '=', $order_info['order_id']]]);
|
||||
$verify->qrcode($code['data']['verify_code'], 'all', 'pickup', $order_info['site_id']);
|
||||
|
||||
// 订单发货完成,小程序发货信息录入,视频号
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主动提货
|
||||
* @param $delivery_code
|
||||
* @return array
|
||||
*/
|
||||
public function verify($delivery_code)
|
||||
{
|
||||
$order_info = model('order')->getInfo([['delivery_code', '=', $delivery_code]], 'order_id, order_type, sign_time, order_status, delivery_code,site_id');
|
||||
if (empty($order_info))
|
||||
return $this->error([], 'ORDER_EMPTY');
|
||||
|
||||
$result = $this->activeTakeDelivery($order_info['order_id']);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
//核销发送通知
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'VERIFY', 'order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主动提货
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function activeTakeDelivery($order_id)
|
||||
{
|
||||
$order_condition = [
|
||||
['order_id', '=', $order_id],
|
||||
['order_type', '=', 2]
|
||||
];
|
||||
$order_info = model('order')->getInfo($order_condition, 'delivery_code, order_status, site_id');
|
||||
|
||||
//应该在这儿主动调用核销的方法函数
|
||||
if (empty($order_info))
|
||||
return $this->error([], '订单不存在!');
|
||||
|
||||
if ($order_info['order_status'] != self::ORDER_PENDING_DELIVERY)
|
||||
return $this->error([], '只有待提货状态的订单才可以提货!');
|
||||
|
||||
$result = $this->orderCommonTakeDelivery($order_id);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
//核销发送通知
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'VERIFY', 'order_id' => $order_id, 'site_id' => $order_info['site_id']]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单提货
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function orderTakeDelivery($order_id)
|
||||
{
|
||||
$res = model('order_goods')->update(['delivery_status' => OrderGoodsDict::delivery, 'delivery_status_name' => '已提货'], [['order_id', '=', $order_id], ['refund_status', '<>', OrderRefundDict::REFUND_COMPLETE]]);
|
||||
|
||||
$order_goods_list = model('order_goods')->getList([['order_id', '=', $order_id]]);
|
||||
foreach ($order_goods_list as &$v) {
|
||||
$v['num'] = numberFormat($v['num']);
|
||||
}
|
||||
//todo 默认先将提货的发货和收货一体化,将扣除库存统一放在这
|
||||
$order_stock_model = new OrderStock();
|
||||
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'store_id,site_id');
|
||||
$stock_result = $order_stock_model->decOrderStock([
|
||||
'store_id' => $order_info['store_id'],
|
||||
'site_id' => $order_info['site_id'],
|
||||
'goods_sku_list' => $order_goods_list
|
||||
]);
|
||||
if ($stock_result['code'] < 0) {
|
||||
return $stock_result;
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款完成操作
|
||||
* @param $order_goods_info
|
||||
*/
|
||||
public function refund($order_goods_info)
|
||||
{
|
||||
//是否入库
|
||||
$order_stock_model = new OrderStock();
|
||||
if ($order_goods_info['is_refund_stock'] == 1) {
|
||||
$order_stock_model->incOrderStock($order_goods_info);
|
||||
}else if($order_goods_info['delivery_status'] == 0){
|
||||
$order_stock_model->incOrderSaleStock([
|
||||
'store_id' => $order_goods_info['store_id'],
|
||||
'goods_sku_data' => [
|
||||
[
|
||||
'sku_id' => $order_goods_info['sku_id'],
|
||||
'num' => $order_goods_info['num'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param $order_info
|
||||
* @return array
|
||||
*/
|
||||
public function orderDetail($order_info)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
943
app/model/order/VirtualOrder.php
Executable file
943
app/model/order/VirtualOrder.php
Executable file
@@ -0,0 +1,943 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order;
|
||||
|
||||
use addon\cardservice\model\MemberCard;
|
||||
use app\dict\goods\GoodsDict;
|
||||
use app\dict\order\OrderGoodsDict;
|
||||
use app\model\goods\VirtualGoods;
|
||||
use app\model\message\Message;
|
||||
use app\model\system\Cron;
|
||||
use app\model\verify\Verify as VerifyModel;
|
||||
use app\model\verify\VerifyRecord;
|
||||
use Exception;
|
||||
use think\facade\Db;
|
||||
use think\facade\Queue;
|
||||
|
||||
/**
|
||||
* 虚拟订单
|
||||
* Class VirtualOrder
|
||||
* @package app\model\order
|
||||
*/
|
||||
class VirtualOrder extends OrderCommon
|
||||
{
|
||||
|
||||
/*****************************************************************************************订单状态***********************************************/
|
||||
// 订单创建
|
||||
public const ORDER_CREATE = 0;
|
||||
|
||||
// 订单已支付
|
||||
public const ORDER_PAY = 1;
|
||||
|
||||
// 订单待收货
|
||||
public const ORDER_DELIVERY = 3;
|
||||
// 订单已收货
|
||||
public const ORDER_TAKE_DELIVERY = 4;
|
||||
|
||||
// 订单已结算完成
|
||||
public const ORDER_COMPLETE = 10;
|
||||
|
||||
// 订单已关闭
|
||||
public const ORDER_CLOSE = -1;
|
||||
|
||||
// 订单待使用
|
||||
public const ORDER_WAIT_VERIFY = 11;
|
||||
|
||||
//订单已使用
|
||||
public const ORDER_VERIFYED = 12;
|
||||
|
||||
// 订单类型
|
||||
public $order_type = 4;
|
||||
|
||||
// 订单状态
|
||||
public $order_status = [
|
||||
self::ORDER_CREATE => [
|
||||
'status' => self::ORDER_CREATE,
|
||||
'name' => '待支付',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderAdjustMoney',
|
||||
'title' => '调整价格',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'orderClose',
|
||||
'title' => '关闭订单',
|
||||
'color' => ''
|
||||
],
|
||||
[
|
||||
'action' => 'orderPay',
|
||||
'title' => '支付',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
|
||||
self::ORDER_PAY => [
|
||||
'status' => self::ORDER_PAY,
|
||||
'name' => '待发货',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-send.png',
|
||||
'action' => [
|
||||
[
|
||||
'action' => 'orderVirtualDelivery',
|
||||
'title' => '发货',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'member_action' => [],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_DELIVERY => [
|
||||
'status' => self::ORDER_DELIVERY,
|
||||
'name' => '待收货',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
[
|
||||
'action' => 'memberVirtualTakeDelivery',
|
||||
'title' => '确认使用',
|
||||
'color' => ''
|
||||
],
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
|
||||
self::ORDER_TAKE_DELIVERY => [
|
||||
'status' => self::ORDER_TAKE_DELIVERY,
|
||||
'name' => '已收货',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_WAIT_VERIFY => [
|
||||
'status' => self::ORDER_WAIT_VERIFY,
|
||||
'name' => '待使用',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-close.png',
|
||||
'action' => [
|
||||
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_VERIFYED => [
|
||||
'status' => self::ORDER_VERIFYED,
|
||||
'name' => '已使用',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-close.png',
|
||||
'action' => [
|
||||
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_COMPLETE => [
|
||||
'status' => self::ORDER_COMPLETE,
|
||||
'name' => '已完成',
|
||||
'is_allow_refund' => 1,
|
||||
'icon' => 'public/uniapp/order/order-icon-received.png',
|
||||
'action' => [
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
self::ORDER_CLOSE => [
|
||||
'status' => self::ORDER_CLOSE,
|
||||
'name' => '已关闭',
|
||||
'is_allow_refund' => 0,
|
||||
'icon' => 'public/uniapp/order/order-icon-close.png',
|
||||
'action' => [
|
||||
|
||||
],
|
||||
'member_action' => [
|
||||
|
||||
],
|
||||
'color' => ''
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* 订单支付
|
||||
* @param $data
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function orderPay($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
$pay_type = $data['pay_type'];
|
||||
$log_data = $data['log_data'] ?? [];
|
||||
$pay_type_list = $this->getPayType();
|
||||
$member_info = $data['member_info'];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_PAY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PAY]['name'],
|
||||
'order_status_action' => json_encode($this->order_status[self::ORDER_PAY], JSON_UNESCAPED_UNICODE),
|
||||
'pay_status' => 1,
|
||||
'pay_time' => time(),
|
||||
'is_enable_refund' => 1,
|
||||
'pay_type' => $pay_type,
|
||||
'pay_type_name' => $pay_type_list[$pay_type]
|
||||
];
|
||||
|
||||
//记录订单日志 start
|
||||
$action = '商家对订单进行了线下支付';
|
||||
//获取用户信息
|
||||
if (empty($log_data)) {
|
||||
$log_data = [
|
||||
'uid' => $order_info['member_id'],
|
||||
'nick_name' => $member_info['nickname'],
|
||||
'action_way' => 1
|
||||
];
|
||||
$action = '买家支付了订单';
|
||||
}
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_id,
|
||||
'action' => $action,
|
||||
'order_status' => self::ORDER_PAY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_PAY]['name'],
|
||||
]);
|
||||
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
//记录订单日志 end
|
||||
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
|
||||
$goods_id = model('order_goods')->getValue([['order_id', '=', $order_id]], 'goods_id');
|
||||
//判断商品是否需要核销
|
||||
$goods_info = model('goods')->getInfo([['goods_id', '=', $goods_id]], '*');
|
||||
|
||||
if ($order_info['is_lock'] == 0) {
|
||||
if (!in_array($order_info['promotion_type'], ['pintuan', 'pinfan'])) {
|
||||
switch($goods_info['goods_class']){
|
||||
case GoodsDict::virtual://虚拟商品
|
||||
switch ($goods_info['virtual_deliver_type']) {
|
||||
case GoodsDict::virtual_auto_deliver:// 自动发货
|
||||
$this->virtualDelivery($order_info, [
|
||||
'uid' => 0,
|
||||
'nick_name' => '系统操作',
|
||||
'action' => '系统自动发货',
|
||||
'action_way' => 2,
|
||||
]);
|
||||
break;
|
||||
case GoodsDict::virtual_artificial_deliver:// 手动发货
|
||||
break;
|
||||
case GoodsDict::virtual_verify:// 到店核销
|
||||
$order_status_item = $this->order_status[self::ORDER_WAIT_VERIFY];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_WAIT_VERIFY,
|
||||
'order_status_name' => $order_status_item['name'],
|
||||
'order_status_action' => json_encode($order_status_item, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
// 是否需要核销
|
||||
if ($goods_info['is_need_verify']) {
|
||||
//虚拟产品发货
|
||||
$this->virtualOrderTakeDelivery($order_id);
|
||||
} else {
|
||||
//虚拟产品收货
|
||||
$this->orderCommonTakeDelivery($order_id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case GoodsDict::virtualcard://卡密
|
||||
$order_status_item = $this->order_status[self::ORDER_DELIVERY];
|
||||
$order_status_item['action'] = [];
|
||||
$order_status_item['member_action'] = [];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'order_status_action' => json_encode($order_status_item, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
// 卡密商品发货
|
||||
$result = $this->virtualCardTakeDelivery($order_id);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
break;
|
||||
case GoodsDict::service://服务商品
|
||||
$order_status_item = $this->order_status[self::ORDER_DELIVERY];
|
||||
$order_status_item['action'] = [];
|
||||
$order_status_item['member_action'] = [];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'order_status_action' => json_encode($order_status_item, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
$this->serviceGoodsTakeDelivery($order_id);
|
||||
break;
|
||||
case GoodsDict::card://卡项
|
||||
$order_status_item = $this->order_status[self::ORDER_DELIVERY];
|
||||
$order_status_item['action'] = [];
|
||||
$order_status_item['member_action'] = [];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'order_status_action' => json_encode($order_status_item, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
$this->cardServiceTakeDelivery($order_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 虚拟发货
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function virtualDelivery($params, $log_data = [])
|
||||
{
|
||||
$order_id = $params['order_id'] ?? 0;
|
||||
//todo 核验订单是否处于锁定
|
||||
$local_result = $this->verifyOrderLock($order_id);
|
||||
if ($local_result['code'] < 0)
|
||||
return $local_result;
|
||||
|
||||
$site_id = $params['site_id'] ?? 0;
|
||||
$condition = [['order_id', '=', $params['order_id']]];
|
||||
if ($site_id > 0) {
|
||||
$condition[] = ['site_id', '=', $site_id];
|
||||
}
|
||||
$order_info = $this->getOrderInfo($condition)['data'] ?? [];
|
||||
if (empty($order_info))
|
||||
return $this->error([], '找不到订单!');
|
||||
|
||||
$data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'order_status_action' => json_encode($this->order_status[self::ORDER_DELIVERY], JSON_UNESCAPED_UNICODE),
|
||||
'delivery_time' => time(),
|
||||
];
|
||||
$res = model('order')->update($data, $condition);
|
||||
|
||||
$goods_id = model('order_goods')->getValue([['order_id', '=', $order_id]], 'goods_id');
|
||||
//判断商品是否需要核销
|
||||
$goods_info = model('goods')->getInfo([['goods_id', '=', $goods_id]], 'virtual_receive_type,virtual_deliver_type');
|
||||
// 记录订单日志 start
|
||||
if(!empty($log_data)){
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_id,
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name']
|
||||
]);
|
||||
OrderLog::addOrderLog($log_data, $this);
|
||||
}
|
||||
//增加卡项商品的自动发货
|
||||
Queue::push('app\job\order\OrderDeliveryAfter', ['order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
if (!empty($goods_info)) {
|
||||
$virtual_receive_type = $goods_info['virtual_receive_type'];
|
||||
if ($virtual_receive_type == 'auto_receive') {
|
||||
// 自动收货
|
||||
$result = $this->virtualTakeDelivery($params, [
|
||||
'uid' => 0,
|
||||
'nick_name' => '系统操作',
|
||||
'action' => '系统自动收货',
|
||||
'action_way' => 2,
|
||||
]);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
}else{
|
||||
//定时任务收货
|
||||
OrderCron::takeDelivery(['order_id' => $order_id, 'site_id' => $site_id]);
|
||||
}
|
||||
}
|
||||
// 订单发货消息
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'ORDER_DELIVERY', 'order_id' => $order_id, 'site_id' => $site_id]);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
public function virtualTakeDelivery($params, $log_data = [])
|
||||
{
|
||||
$order_id = $params['order_id'] ?? 0;
|
||||
$site_id = $params['site_id'] ?? 0;
|
||||
$member_id = $params['member_id'] ?? 0;
|
||||
$condition = [['order_id', '=', $order_id]];
|
||||
if ($site_id > 0) {
|
||||
$condition[] = ['site_id', '=', $site_id];
|
||||
}
|
||||
if ($member_id > 0) {
|
||||
$condition[] = ['member_id', '=', $member_id];
|
||||
}
|
||||
|
||||
$order_info = $this->getOrderInfo($condition)['data'] ?? [];
|
||||
if (empty($order_info))
|
||||
return $this->error([], '找不到订单!');
|
||||
|
||||
//虚拟产品收货
|
||||
$result = $this->orderCommonTakeDelivery($order_id, $log_data);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单自动发货
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function virtualOrderTakeDelivery($order_id)
|
||||
{
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], '*');
|
||||
if (empty($order_info))
|
||||
return $this->error([], 'ORDER_EMPTY');
|
||||
|
||||
$site_id = $order_info['site_id'];
|
||||
model('order')->startTrans();
|
||||
try {
|
||||
//订单项变为已发货
|
||||
model('order_goods')->update(['delivery_status' => OrderGoodsDict::delivery, 'delivery_status_name' => '待使用'], [['order_id', '=', $order_id]]);
|
||||
$order_goods_info = model('order_goods')->getInfo([['order_id', '=', $order_id]], 'sku_id,sku_name,sku_image,price,num,goods_id,order_goods_id');//订单项详情
|
||||
if (!empty($order_goods_info)) {
|
||||
$order_goods_info['num'] = numberFormat($order_goods_info['num']);
|
||||
}
|
||||
|
||||
//判断商品是否需要核销
|
||||
$goods_info = model('goods_sku')->getInfo([['gs.sku_id', '=', $order_goods_info['sku_id']]], 'g.is_need_verify,g.verify_validity_type,g.virtual_indate,gs.verify_num', 'gs', [['goods g', 'g.goods_id = gs.goods_id', 'left']]);
|
||||
if ($goods_info['is_need_verify']) {
|
||||
|
||||
switch ($goods_info['verify_validity_type']) {
|
||||
case 0:
|
||||
$expire_time = 0;
|
||||
break;
|
||||
case 1:
|
||||
$expire_time = strtotime('+' . $goods_info['virtual_indate'] . 'days');
|
||||
break;
|
||||
case 2:
|
||||
$expire_time = $goods_info['virtual_indate'];
|
||||
break;
|
||||
}
|
||||
|
||||
if ($expire_time > 0) {
|
||||
//添加自动收货事件
|
||||
OrderCron::takeDelivery(['order_id' => $order_id, 'site_id' => $site_id, 'expire_time' => $expire_time]);
|
||||
// 核销码临近到期时间(小时)
|
||||
$config_model = new Config();
|
||||
$verify_config = $config_model->getOrderVerifyConfig($site_id)['data']['value'];
|
||||
|
||||
$order_verify_out_time = $verify_config['order_verify_time_out'] ?? 24;
|
||||
$time_strtime = $order_verify_out_time * 3600;
|
||||
$cron_model = new Cron();
|
||||
//核销商品临期提醒
|
||||
$cron_model->addCron(1, 0, '核销商品临期提醒', 'VerifyOrderOutTime', $expire_time - $time_strtime, $order_id);
|
||||
//核销码过期提醒
|
||||
$cron_model->addCron(1, 0, '核销码过期提醒', 'CronVerifyCodeExpire', $expire_time, $order_id);
|
||||
}
|
||||
|
||||
$count = $goods_info['verify_num'] * $order_goods_info['num'];
|
||||
// for ($i = 1; $i <= $count; $i++) {
|
||||
//创建待核销记录
|
||||
$verify_model = new VerifyModel();
|
||||
$item_array = [
|
||||
[
|
||||
'img' => $order_goods_info['sku_image'],
|
||||
'name' => $order_goods_info['sku_name'],
|
||||
'price' => $order_goods_info['price'],
|
||||
'num' => 1,
|
||||
'order_goods_id' => $order_goods_info['order_goods_id'],
|
||||
'remark_array' => [
|
||||
['title' => '联系人', 'value' => $order_info['name'] . $order_info['mobile']]
|
||||
]
|
||||
],
|
||||
];
|
||||
$remark_array = [
|
||||
['title' => '订单金额', 'value' => $order_info['order_money']],
|
||||
['title' => '订单编号', 'value' => $order_info['order_no']],
|
||||
['title' => '创建时间', 'value' => time_to_date($order_info['create_time'])],
|
||||
['title' => '付款时间', 'value' => time_to_date($order_info['pay_time'])],
|
||||
];
|
||||
$verify_content_json = $verify_model->getVerifyJson($item_array, $remark_array);
|
||||
$code_result = $verify_model->addVerify('virtualgoods', $site_id, $order_info['site_name'], $verify_content_json, $expire_time, $count, 0, $order_info['member_id']);
|
||||
$code = $code_result['data']['verify_code'];
|
||||
$verify_model->qrcode($code, 'all', 'virtualgoods', $site_id);//生成二维码
|
||||
|
||||
//自动收发货
|
||||
$order_data = [
|
||||
'virtual_code' => $code,
|
||||
];
|
||||
$res = model('order')->update($order_data, [['order_id', '=', $order_id]]);
|
||||
|
||||
//生成所购买的产品
|
||||
$virtual_goods_model = new VirtualGoods();
|
||||
|
||||
$goods_virtual_data = [
|
||||
'site_id' => $site_id,
|
||||
'order_id' => $order_id,
|
||||
'order_no' => $order_info['order_no'],
|
||||
'sku_id' => $order_goods_info['sku_id'],
|
||||
'sku_name' => $order_goods_info['sku_name'],
|
||||
'code' => $code,
|
||||
'member_id' => $order_info['member_id'],
|
||||
'sku_image' => $order_goods_info['sku_image'],
|
||||
'expire_time' => $expire_time,
|
||||
'sold_time' => time(),
|
||||
'goods_id' => $order_goods_info['goods_id'],
|
||||
'verify_total_count' => $count
|
||||
];
|
||||
$virtual_goods_model->addGoodsVirtual($goods_virtual_data);
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
model('order')->commit();
|
||||
return $this->success($res);
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
model('order')->rollback();
|
||||
throw new \Exception('虚拟订单提货错误:'.$e->getMessage());
|
||||
//return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡密商品自动发货 收货(可以分为两个函数 一个发货一个收货)
|
||||
* @param $order_id
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function virtualCardTakeDelivery($order_id)
|
||||
{
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_no,site_id,site_name,member_id,order_type,
|
||||
sign_time,order_status,delivery_code,create_time,name,pay_time,pay_money,mobile,is_lock,order_money');
|
||||
if (empty($order_info))
|
||||
return $this->error([], 'ORDER_EMPTY');
|
||||
|
||||
model('order')->startTrans();
|
||||
try {
|
||||
//订单项变为已发货
|
||||
model('order_goods')->update(['delivery_status' => OrderGoodsDict::delivery, 'delivery_status_name' => '已发货'], [['order_id', '=', $order_id]]);
|
||||
$order_goods_info = model('order_goods')->getInfo([['order_id', '=', $order_id]], '*');//订单项详情
|
||||
if (!empty($order_goods_info)) {
|
||||
$order_goods_info['num'] = numberFormat($order_goods_info['num']);
|
||||
}
|
||||
//库存处理(卡密商品支付后在扣出库存)//todo 可以再商品中设置扣除库存步骤
|
||||
$order_stock_model = new OrderStock();
|
||||
$item_goods_class = $order_goods_info['goods_class'];
|
||||
if ($item_goods_class == GoodsDict::virtualcard) {
|
||||
//库存变化
|
||||
$order_stock_model->decOrderStock($order_goods_info);
|
||||
}
|
||||
//TODO 并发情况下 同时查询会触发覆盖,比如A找到id为8的数据修改,B也找到id为8的数修改,那么最后只能有一个用户关联到
|
||||
//$goods_virtual_list = model('goods_virtual')->pageList([ [ 'order_id', '=', 0 ], [ 'sku_id', '=', $order_goods_info[ 'sku_id' ] ] ], 'id', 'id asc', 1, $order_goods_info[ 'num' ])['list'];
|
||||
$goods_virtual_list = Db::name('goods_virtual')
|
||||
->where([['order_id', '=', 0], ['sku_id', '=', $order_goods_info['sku_id']]])
|
||||
->limit($order_goods_info['num'])
|
||||
->field('id')
|
||||
->order('id asc')
|
||||
->lock(true)
|
||||
->select()
|
||||
->toArray();
|
||||
if (count($goods_virtual_list) != $order_goods_info['num']) {
|
||||
//主动退款
|
||||
$order_refund_model = new OrderRefund();
|
||||
$refund_result = $order_refund_model->activeRefund($order_id, '', '卡密库存不足主动退款');
|
||||
if ($refund_result['code'] < 0) {
|
||||
model('order')->rollback();
|
||||
return $refund_result;
|
||||
}
|
||||
model('order')->commit();
|
||||
return $this->error('', '卡密库存不足!');
|
||||
}
|
||||
$ids = array_column($goods_virtual_list, 'id');
|
||||
model('goods_virtual')->update([
|
||||
'order_id' => $order_id,
|
||||
'order_no' => $order_info['order_no'],
|
||||
'member_id' => $order_info['member_id'],
|
||||
'sold_time' => time()
|
||||
], [['id', 'in', $ids]]);
|
||||
|
||||
// 订单自动收货
|
||||
$this->orderCommonTakeDelivery($order_id);
|
||||
|
||||
model('order')->commit();
|
||||
return $this->success();
|
||||
} catch ( Exception $e ) {
|
||||
model('order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单自动发货(服务商品)
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function serviceGoodsTakeDelivery($order_id)
|
||||
{
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_no,site_id,site_name,member_id,order_type,
|
||||
sign_time,order_status,delivery_code,create_time,name,pay_time,pay_money,mobile,is_lock,order_money');
|
||||
if (empty($order_info))
|
||||
return $this->error([], 'ORDER_EMPTY');
|
||||
|
||||
$site_id = $order_info['site_id'];
|
||||
model('order')->startTrans();
|
||||
try {
|
||||
//订单项变为已发货
|
||||
model('order_goods')->update(['delivery_status' => OrderGoodsDict::delivery, 'delivery_status_name' => '已发货'], [['order_id', '=', $order_id]]);
|
||||
$order_goods_info = model('order_goods')->getInfo([['order_id', '=', $order_id]], 'sku_id,sku_name,sku_image,price,num,goods_id,order_goods_id');//订单项详情
|
||||
if (!empty($order_goods_info)) {
|
||||
$order_goods_info['num'] = numberFormat($order_goods_info['num']);
|
||||
}
|
||||
|
||||
//判断商品是否需要核销
|
||||
$goods_info = model('goods_sku')->getInfo([['gs.sku_id', '=', $order_goods_info['sku_id']]], 'g.is_need_verify,g.verify_validity_type,g.virtual_indate,gs.verify_num', 'gs', [['goods g', 'g.goods_id = gs.goods_id', 'left']]);
|
||||
|
||||
switch ($goods_info['verify_validity_type']) {
|
||||
case GoodsDict::service_permanent://永久
|
||||
$expire_time = 0;
|
||||
break;
|
||||
case GoodsDict::service_day://购买后几日有效
|
||||
$expire_time = strtotime('+' . $goods_info['virtual_indate'] . 'days');
|
||||
break;
|
||||
case GoodsDict::service_day_expire://指定时间过期1
|
||||
$expire_time = $goods_info['virtual_indate'];
|
||||
break;
|
||||
}
|
||||
//添加自动收货事件
|
||||
if ($expire_time > 0) {
|
||||
$cron = new Cron();
|
||||
OrderCron::takeDelivery(['order_id' => $order_id, 'site_id' => $site_id, 'expire_time' => $expire_time]);
|
||||
// 核销码临近到期时间(小时)
|
||||
$config_model = new Config();
|
||||
$verify_config = $config_model->getOrderVerifyConfig($site_id)['data']['value'];
|
||||
|
||||
$order_verify_out_time = $verify_config['order_verify_time_out'] ?? 24;
|
||||
$time_strtime = $order_verify_out_time * 3600;
|
||||
//核销商品临期提醒
|
||||
$cron->addCron(1, 0, '核销商品临期提醒', 'VerifyOrderOutTime', $expire_time - $time_strtime, $order_id);
|
||||
//核销码过期提醒
|
||||
$cron->addCron(1, 0, '核销码过期提醒', 'CronVerifyCodeExpire', $expire_time, $order_id);
|
||||
}
|
||||
|
||||
$count = $order_goods_info['num'];
|
||||
//创建待核销记录
|
||||
$verify_model = new VerifyModel();
|
||||
$item_array = [
|
||||
[
|
||||
'img' => $order_goods_info['sku_image'],
|
||||
'name' => $order_goods_info['sku_name'],
|
||||
'price' => $order_goods_info['price'],
|
||||
'num' => 1,
|
||||
'order_goods_id' => $order_goods_info['order_goods_id'],
|
||||
'remark_array' => [
|
||||
['title' => '联系人', 'value' => $order_info['name'] . $order_info['mobile']]
|
||||
]
|
||||
],
|
||||
];
|
||||
$remark_array = [
|
||||
['title' => '订单金额', 'value' => $order_info['order_money']],
|
||||
['title' => '订单编号', 'value' => $order_info['order_no']],
|
||||
['title' => '创建时间', 'value' => time_to_date($order_info['create_time'])],
|
||||
['title' => '付款时间', 'value' => time_to_date($order_info['pay_time'])],
|
||||
];
|
||||
$verify_content_json = $verify_model->getVerifyJson($item_array, $remark_array);
|
||||
$code_result = $verify_model->addVerify('virtualgoods', $site_id, $order_info['site_name'], $verify_content_json, $expire_time, $count, 0, $order_info['member_id']);
|
||||
$code = $code_result['data']['verify_code'];
|
||||
$verify_model->qrcode($code, 'all', 'virtualgoods', $site_id);//生成二维码
|
||||
|
||||
//自动收发货
|
||||
$order_data = [
|
||||
'virtual_code' => $code,
|
||||
];
|
||||
$res = model('order')->update($order_data, [['order_id', '=', $order_id]]);
|
||||
|
||||
//生成所购买的产品
|
||||
$virtual_goods_model = new VirtualGoods();
|
||||
|
||||
$goods_virtual_data = [
|
||||
'site_id' => $site_id,
|
||||
'order_id' => $order_id,
|
||||
'order_no' => $order_info['order_no'],
|
||||
'sku_id' => $order_goods_info['sku_id'],
|
||||
'sku_name' => $order_goods_info['sku_name'],
|
||||
'code' => $code,
|
||||
'member_id' => $order_info['member_id'],
|
||||
'sku_image' => $order_goods_info['sku_image'],
|
||||
'expire_time' => $expire_time,
|
||||
'sold_time' => time(),
|
||||
'goods_id' => $order_goods_info['goods_id'],
|
||||
'verify_total_count' => $count
|
||||
];
|
||||
$virtual_goods_model->addGoodsVirtual($goods_virtual_data);
|
||||
|
||||
|
||||
model('order')->commit();
|
||||
return $this->success($res);
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
model('order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡项发货设置(需要增加卡项核销)
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function cardServiceTakeDelivery($order_id)
|
||||
{
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], '*');
|
||||
if (empty($order_info))
|
||||
return $this->error([], 'ORDER_EMPTY');
|
||||
|
||||
$site_id = $order_info['site_id'];
|
||||
model('order')->startTrans();
|
||||
try {
|
||||
//订单项变为已发货
|
||||
model('order_goods')->update(['delivery_status' => OrderGoodsDict::delivery, 'delivery_status_name' => '已发货'], [['order_id', '=', $order_id]]);
|
||||
$order_goods_info = model('order_goods')->getInfo([['order_id', '=', $order_id]], 'sku_id,sku_name,sku_image,price,num,goods_id,order_goods_id');//订单项详情
|
||||
if (!empty($order_goods_info)) {
|
||||
$order_goods_info['num'] = numberFormat($order_goods_info['num']);
|
||||
}
|
||||
//创建卡项
|
||||
$member_card = new MemberCard();
|
||||
$card_data = [
|
||||
'store_id' => 0,
|
||||
'goods_id' => $order_goods_info['goods_id'],
|
||||
'num' => $order_goods_info['num'],
|
||||
'member_id' => $order_info['member_id'],
|
||||
'site_id' => $site_id,
|
||||
'order_id' => $order_id
|
||||
];
|
||||
$res_card = $member_card->create($card_data);
|
||||
if ($res_card['code'] < 0) {
|
||||
model('order')->rollback();
|
||||
return $this->error('', $res_card['message']);
|
||||
}
|
||||
|
||||
// 订单自动收货
|
||||
$this->orderCommonTakeDelivery($order_id);
|
||||
model('order')->commit();
|
||||
return $this->success(1);
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
model('order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动发货
|
||||
* @param $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function orderTakeDelivery($order_id)
|
||||
{
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], 'order_no,site_id,site_name,member_id,order_type,
|
||||
sign_time,order_status,delivery_code,create_time,name,pay_time,pay_money,mobile,is_lock,order_money');
|
||||
if (empty($order_info))
|
||||
return $this->error([], 'ORDER_EMPTY');
|
||||
|
||||
$res = model('order_goods')->update(['delivery_status' => OrderGoodsDict::delivery, 'delivery_status_name' => '已发货'], [['order_id', '=', $order_id]]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款完成操作
|
||||
* @param $order_goods_info
|
||||
*/
|
||||
public function refund($order_goods_info)
|
||||
{
|
||||
$type = true;
|
||||
if (addon_is_exit('virtualcard')) {
|
||||
$virtual_card = (new \addon\virtualcard\model\VirtualGoods())->getGoodsClass();
|
||||
if ($order_goods_info['goods_class'] == $virtual_card['id']) $type = false;
|
||||
}
|
||||
if ($type) {
|
||||
//删除已退款订单项会员虚拟商品, 并退回商品库存
|
||||
//无需判断订单项是否需要入库
|
||||
$item_param = [
|
||||
'sku_id' => $order_goods_info['sku_id'],
|
||||
'num' => $order_goods_info['num'],
|
||||
];
|
||||
//返还库存
|
||||
$order_stock_model = new OrderStock();
|
||||
$order_stock_model->incOrderStock($order_goods_info);
|
||||
//$goods_stock_model->incStock($item_param);
|
||||
//删除用户的这条虚拟商品
|
||||
$goods_virtual_model = new VirtualGoods();
|
||||
$goods_virtual_model->deleteGoodsVirtual([['order_id', '=', $order_goods_info['order_id']], ['member_id', '=', $order_goods_info['member_id']]]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param $order_info
|
||||
* @return array
|
||||
*/
|
||||
public function orderDetail($order_info)
|
||||
{
|
||||
$order_id = $order_info['order_id'];
|
||||
$verify_record_model = new VerifyRecord();
|
||||
$data = [
|
||||
'goods_class' => $order_info['order_goods'][0]['goods_class']
|
||||
];
|
||||
if ($data['goods_class'] == GoodsDict::virtual) {
|
||||
$virtual_goods = model('goods_virtual')->getFirstData([['order_id', '=', $order_id]], '*', 'is_veirfy asc');
|
||||
if (!empty($virtual_goods)) {
|
||||
$virtual_goods['total_verify_num'] = model('goods_virtual')->getCount([['order_id', '=', $order_id]]);
|
||||
$virtual_goods['verify_num'] = model('goods_virtual')->getCount([['order_id', '=', $order_id], ['is_veirfy', '=', 1]]);
|
||||
$verify_code = model('goods_virtual')->getColumn([['order_id', '=', $order_id]], 'code');
|
||||
$virtual_goods['verify_record'] = [];
|
||||
if (!empty($verify_code)) {
|
||||
$virtual_goods['verify_record'] = $verify_record_model->getVerifyRecordsViewList([['verify_code', 'in', $verify_code]], '*', 'verify_time desc')['data'] ?? [];
|
||||
}
|
||||
$data['virtual_goods'] = $virtual_goods;
|
||||
$data['virtual_code'] = $virtual_goods['code'];
|
||||
}
|
||||
} else if ($data['goods_class'] == GoodsDict::virtualcard) {
|
||||
$virtual_goods = model('goods_virtual')->getList([['order_id', '=', $order_id]], '*', 'id asc');
|
||||
if (!empty($virtual_goods)) {
|
||||
foreach ($virtual_goods as $key => $item) {
|
||||
$virtual_goods[$key]['card_info'] = json_decode($item['card_info'], true);
|
||||
}
|
||||
$data['virtual_goods'] = $virtual_goods;
|
||||
}
|
||||
}
|
||||
if ($data['goods_class'] == GoodsDict::service) {
|
||||
$virtual_goods = model('goods_virtual')->getFirstData([['order_id', '=', $order_id]], '*', 'is_veirfy asc');
|
||||
if (!empty($virtual_goods)) {
|
||||
$virtual_goods['total_verify_num'] = model('goods_virtual')->getCount([['order_id', '=', $order_id]]);
|
||||
$virtual_goods['verify_num'] = model('goods_virtual')->getCount([['order_id', '=', $order_id], ['is_veirfy', '=', 1]]);
|
||||
$verify_code = model('goods_virtual')->getColumn([['order_id', '=', $order_id]], 'code');
|
||||
$virtual_goods['verify_record'] = [];
|
||||
if (!empty($verify_code)) {
|
||||
$virtual_goods['verify_record'] = $verify_record_model->getVerifyRecordsViewList([['verify_code', 'in', $verify_code]], '*', 'verify_time desc')['data'] ?? [];
|
||||
}
|
||||
$data['virtual_goods'] = $virtual_goods;
|
||||
$data['virtual_code'] = $virtual_goods['code'];
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* to发送
|
||||
* @param $params
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function toSend($params)
|
||||
{
|
||||
$order_id = $params['order_id'];
|
||||
$goods_id = model('order_goods')->getValue([['order_id', '=', $order_id]], 'goods_id');
|
||||
//判断商品是否需要核销
|
||||
$goods_info = model('goods')->getInfo([['goods_id', '=', $goods_id]], 'goods_class,is_need_verify,virtual_deliver_type');
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]]);
|
||||
|
||||
// 虚拟商品
|
||||
if ($goods_info['goods_class'] == GoodsDict::virtual) {
|
||||
switch ($goods_info['virtual_deliver_type']) {
|
||||
case 'auto_deliver':
|
||||
// 自动发货
|
||||
$this->virtualDelivery($order_info);
|
||||
break;
|
||||
case 'artificial_deliver':
|
||||
// 手动发货
|
||||
break;
|
||||
case 'verify':
|
||||
// 到店核销
|
||||
$order_status_item = $this->order_status[self::ORDER_WAIT_VERIFY];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_WAIT_VERIFY,
|
||||
'order_status_name' => $order_status_item['name'],
|
||||
'order_status_action' => json_encode($order_status_item, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
|
||||
if ($goods_info['is_need_verify']) {
|
||||
//虚拟产品发货
|
||||
$this->virtualOrderTakeDelivery($order_id);
|
||||
} else {
|
||||
//虚拟产品收货
|
||||
$this->orderCommonTakeDelivery($order_id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
} elseif ($goods_info['goods_class'] == GoodsDict::virtualcard) {
|
||||
// 电子卡密商品
|
||||
$order_status_item = $this->order_status[self::ORDER_DELIVERY];
|
||||
$order_status_item['action'] = [];
|
||||
$order_status_item['member_action'] = [];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'order_status_action' => json_encode($order_status_item, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
// 卡密商品发货
|
||||
$result = $this->virtualCardTakeDelivery($order_id);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
} elseif ($goods_info['goods_class'] == GoodsDict::service) {
|
||||
// 服务项目商品
|
||||
$order_status_item = $this->order_status[self::ORDER_DELIVERY];
|
||||
$order_status_item['action'] = [];
|
||||
$order_status_item['member_action'] = [];
|
||||
$data = [
|
||||
'order_status' => self::ORDER_DELIVERY,
|
||||
'order_status_name' => $this->order_status[self::ORDER_DELIVERY]['name'],
|
||||
'order_status_action' => json_encode($order_status_item, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
model('order')->update($data, [['order_id', '=', $order_id]]);
|
||||
$result = $this->serviceGoodsTakeDelivery($order_id);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
} elseif ($goods_info['goods_class'] == GoodsDict::card) {
|
||||
// 卡项套餐商品
|
||||
$result = $this->cardServiceTakeDelivery($order_id);
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
205
app/model/order/event/OrderClose.php
Executable file
205
app/model/order/event/OrderClose.php
Executable file
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\event;
|
||||
|
||||
use addon\cardservice\model\MemberCard;
|
||||
use addon\coupon\model\Coupon;
|
||||
use app\dict\goods\GoodsDict;
|
||||
use app\dict\order\OrderDict;
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\goods\Goods;
|
||||
use app\model\member\MemberAccount;
|
||||
use app\model\message\Message;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
use app\model\order\OrderRefund;
|
||||
use app\model\order\OrderStock;
|
||||
use app\model\system\Pay;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 订单交易设置
|
||||
*/
|
||||
class OrderClose extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws DbException
|
||||
*/
|
||||
public function check($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
//尝试关闭支付
|
||||
if ($order_info['pay_status'] == 0) {
|
||||
$pay_model = new Pay();
|
||||
$result = $pay_model->closePay($order_info['out_trade_no']);//关闭旧支付单据
|
||||
if ($result['code'] < 0) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
//订单关闭校验
|
||||
event('OrderCloseCheck', []);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
|
||||
public function event($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
// $is_exist_refund = $data['is_exist_refund'];
|
||||
// $refund_goods_card = $data['refund_goods_card'];
|
||||
// $refund_point = $data['refund_point'];
|
||||
|
||||
|
||||
/******************************************************* 库存相关 **********************************************************/
|
||||
//库存处理
|
||||
$condition = array(
|
||||
['order_id', '=', $order_id]
|
||||
);
|
||||
//循环订单项 依次返还库存 并修改状态
|
||||
$order_goods_list = model('order_goods')->getList($condition, '*');
|
||||
$order_refund_model = new OrderRefund();
|
||||
$goods_model = new Goods();
|
||||
|
||||
$is_exist_refund = false;//是否存在退款
|
||||
$refund_goods_card = [];
|
||||
$refund_point = 0;
|
||||
$order_stock = new OrderStock();
|
||||
//用于存放退款库存的数组
|
||||
$stock_goods_sku_data = [];
|
||||
foreach ($order_goods_list as $k => $v) {
|
||||
//如果是已维权完毕的订单项, 库存不必再次返还(todo 收银台订单创建不再扣除库存了)
|
||||
if ($v['refund_status'] != OrderRefundDict::REFUND_COMPLETE && $order_info['order_type'] != OrderDict::cashier) {
|
||||
$goods_class = $v['goods_class'] ?? 0;
|
||||
|
||||
if (in_array($goods_class, [GoodsDict::real, GoodsDict::virtual, GoodsDict::virtualcard, GoodsDict::service, GoodsDict::card, GoodsDict::weigh])) {
|
||||
$stock_goods_sku_data[] = [
|
||||
'sku_id' => $v['sku_id'],
|
||||
'num' => $v['num']
|
||||
];
|
||||
}
|
||||
//返还积分
|
||||
$refund_point += $v['use_point'];
|
||||
// 是否有使用次卡
|
||||
if ($v['card_item_id']) {
|
||||
$refund_goods_card[] = ['type' => 'order', 'relation_id' => $v['order_goods_id']];
|
||||
}
|
||||
}
|
||||
if ($v['refund_status'] == OrderRefundDict::REFUND_COMPLETE) {
|
||||
$is_exist_refund = true;
|
||||
}
|
||||
//减少商品销量(必须支付过)
|
||||
if ($order_info['pay_status'] > 0) {
|
||||
$goods_model->decGoodsSaleNum($v['sku_id'], $v['num'], $order_info['store_id']);
|
||||
}
|
||||
}
|
||||
/******************************************************* 返还库存 **********************************************************/
|
||||
if($stock_goods_sku_data){
|
||||
//返还销售库存
|
||||
$order_stock->incOrderSaleStock([
|
||||
'goods_sku_data' => $stock_goods_sku_data,
|
||||
'store_id' => $order_info['store_id']
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************* 优惠券相关 **********************************************************/
|
||||
//返还店铺优惠券
|
||||
$coupon_id = $order_info['coupon_id'];
|
||||
if ($coupon_id > 0) {
|
||||
$coupon_model = new Coupon();
|
||||
$coupon_model->refundCoupon($coupon_id, $order_info['member_id']);
|
||||
}
|
||||
//平台优惠券
|
||||
/******************************************************* 退还余额相关 **********************************************************/
|
||||
//平台余额 退还余额
|
||||
if (!$is_exist_refund) {//因为订单完成后 只有全部退款完毕订单才会关闭
|
||||
$member_account_model = new MemberAccount();
|
||||
if ($order_info['balance_money'] > 0) {
|
||||
$result = $member_account_model->addMemberAccount($order_info['site_id'], $order_info['member_id'], 'balance', $order_info['balance_money'], 'refund', $order_id, '订单关闭返还');
|
||||
}
|
||||
// 订单关闭返还积分
|
||||
if ($refund_point > 0) {
|
||||
$result = $member_account_model->addMemberAccount($order_info['site_id'], $order_info['member_id'], 'point', $refund_point, 'refund', $order_id, '订单关闭返还');
|
||||
}
|
||||
}
|
||||
/******************************************************* 关闭后各插件相关 **********************************************************/
|
||||
//订单关闭后操作
|
||||
$close_result = event('OrderClose', $order_info);
|
||||
if (empty($close_result)) {
|
||||
foreach ($close_result as $k => $v) {
|
||||
if (!empty($v) && $v['code'] < 0) {
|
||||
return $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$log_data = $data['log_data'];
|
||||
$close_cause = $data['close_cause'] ?? '';
|
||||
$order_common_model = new OrderCommon();
|
||||
/******************************************************* 会员相关 **********************************************************/
|
||||
|
||||
/******************************************************* 日志相关 **********************************************************/
|
||||
//记录订单日志 start
|
||||
$close_status = OrderCommon::ORDER_CLOSE;
|
||||
if (!empty($log_data)) {
|
||||
if ($log_data['action_way'] == 1) {
|
||||
$member_info = model('member')->getInfo(['member_id' => $log_data['uid']], 'nickname');
|
||||
$buyer_name = empty($member_info['nickname']) ? '' : '【' . $member_info['nickname'] . '】';
|
||||
$log_data['nick_name'] = $buyer_name;
|
||||
$action = '买家关闭了订单';
|
||||
} else {
|
||||
$action = '商家关闭了订单';
|
||||
}
|
||||
} else {
|
||||
$action = !empty($close_cause) ? $close_cause : '系统自动关闭了订单(长时间未支付)';
|
||||
$log_data = [
|
||||
'uid' => 0,
|
||||
'nick_name' => '系统',
|
||||
'action_way' => 2
|
||||
];
|
||||
|
||||
}
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_info['order_id'],
|
||||
'action' => $action,
|
||||
'order_status' => $close_status,
|
||||
'order_status_name' => $order_common_model->order_status[$close_status]['name']
|
||||
]);
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
//记录订单日志 end
|
||||
/******************************************************* 发送消息 **********************************************************/
|
||||
//订单关闭消息
|
||||
$message_model = new Message();
|
||||
$res = $message_model->sendMessage(['keywords' => 'ORDER_CLOSE', 'order_id' => $order_info['order_id'], 'site_id' => $order_info['site_id']]);
|
||||
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
83
app/model/order/event/OrderComplete.php
Executable file
83
app/model/order/event/OrderComplete.php
Executable file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\event;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Message;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
|
||||
/**
|
||||
* 订单完成设置
|
||||
*/
|
||||
class OrderComplete extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function check($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
/******************************************************* 校验订单锁定相关 **********************************************************/
|
||||
$lock_result = (new OrderCommon())->verifyOrderLock($order_id);
|
||||
if ($lock_result['code'] < 0)
|
||||
return $lock_result;
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
|
||||
public function event($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
/******************************************************* 会员账户相关 **********************************************************/
|
||||
//修改用户表order_complete_money和order_complete_num
|
||||
model('member')->setInc([['member_id', '=', $order_info['member_id']]], 'order_complete_money', $order_info['order_money'] - $order_info['refund_money']);
|
||||
model('member')->setInc([['member_id', '=', $order_info['member_id']]], 'order_complete_num');
|
||||
|
||||
/******************************************************* 插件相关 **********************************************************/
|
||||
event('OrderComplete', ['order_id' => $order_id]);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
$site_id = $order_info['site_id'];
|
||||
|
||||
/******************************************************* 日志相关 **********************************************************/
|
||||
$log_data = array(
|
||||
'order_id' => $order_id,
|
||||
'action' => 'complete',
|
||||
'site_id' => $site_id,
|
||||
'is_auto' => 1,// todo 当前业务默认是系统任务完成订单
|
||||
);
|
||||
(new OrderLog())->addLog($log_data);
|
||||
/******************************************************* 消息相关 **********************************************************/
|
||||
$message_model = new Message();
|
||||
//订单完成
|
||||
$message_model->sendMessage(['keywords' => 'ORDER_COMPLETE', 'order_id' => $order_id, 'site_id' => $site_id]);
|
||||
// 买家订单完成通知商家
|
||||
$message_model->sendMessage(['keywords' => 'BUYER_ORDER_COMPLETE', 'order_id' => $order_id, 'site_id' => $site_id]);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
54
app/model/order/event/OrderDelivery.php
Executable file
54
app/model/order/event/OrderDelivery.php
Executable file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\event;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 订单发货设置
|
||||
*/
|
||||
class OrderDelivery extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function check($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
|
||||
public function event($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
88
app/model/order/event/OrderPay.php
Executable file
88
app/model/order/event/OrderPay.php
Executable file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\event;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\goods\Goods;
|
||||
use app\model\member\MemberAccount;
|
||||
use app\model\message\Message;
|
||||
use app\model\order\OrderCommon;
|
||||
use Exception;
|
||||
use app\model\system\Cron;
|
||||
|
||||
/**
|
||||
* 订单交易设置
|
||||
*/
|
||||
class OrderPay extends BaseModel
|
||||
{
|
||||
|
||||
public function check($data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function event($data)
|
||||
{
|
||||
$pay_info = $data['pay_info'];
|
||||
$order_info = $data['order_info'];
|
||||
|
||||
//订单余额账户处理
|
||||
if (($pay_info['balance'] || $pay_info['balance_money']) && $order_info['order_money']) {
|
||||
//订单处理会员消费业务
|
||||
$account_result = (new MemberAccount())->addMemberAccountInOrderPay($data);
|
||||
if (!empty($account_result) && $account_result['code'] < 0) throw new Exception($account_result['message']);
|
||||
}
|
||||
|
||||
//会员业务
|
||||
$member_info = $data['member_info'];
|
||||
$data_member = [
|
||||
'order_money' => $member_info['order_money'] + $order_info['order_money'],
|
||||
'order_num' => $member_info['order_num'] + 1,
|
||||
'last_consum_time' => time()
|
||||
];
|
||||
model('member')->update($data_member, [['member_id', '=', $member_info['member_id']]]);
|
||||
|
||||
$order_info['pay_info'] = $pay_info;
|
||||
event('OrderPay', $order_info);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
public function after($data)
|
||||
{
|
||||
$order_id = $data['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], '*');
|
||||
|
||||
event('OrderPayAfter', $order_info);
|
||||
|
||||
//商品业务
|
||||
$order_goods_list = model('order_goods')->getList([['order_id', '=', $order_id]], 'sku_id,num,goods_class,store_id');
|
||||
$goods_model = new Goods();
|
||||
foreach ($order_goods_list as $v) {
|
||||
$goods_model->incGoodsSaleNum($v['sku_id'], $v['num']);
|
||||
}
|
||||
|
||||
//发送消息
|
||||
$message_model = new Message();
|
||||
$param = ['keywords' => 'ORDER_PAY'];
|
||||
$param = array_merge($param, $order_info);
|
||||
$message_model->sendMessage($param);
|
||||
$param = ['keywords' => 'BUYER_PAY'];
|
||||
$param = array_merge($param, $order_info);
|
||||
$message_model->sendMessage($param);
|
||||
|
||||
//删除定时任务
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronOrderClose' ], [ 'relate_id', '=', $order_id ] ]);
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronOrderUrgePayment' ], [ 'relate_id', '=', $order_id ] ]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
117
app/model/order/event/OrderTake.php
Executable file
117
app/model/order/event/OrderTake.php
Executable file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\event;
|
||||
|
||||
use app\dict\order\OrderDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderCron;
|
||||
use app\model\order\OrderLog;
|
||||
use app\model\order\VirtualOrder;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 订单交易设置
|
||||
*/
|
||||
class OrderTake extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array|void
|
||||
* @throws DbException
|
||||
*/
|
||||
public function check($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
$order_info = model('order')->getInfo([['order_id', '=', $order_id]], '*');
|
||||
if (empty($order_info))
|
||||
return $this->error([], 'ORDER_EMPTY');
|
||||
$order_common_model = new OrderCommon();
|
||||
$order_info['goods_num'] = numberFormat($order_info['goods_num']);
|
||||
$local_result = $this->verifyOrderLock($order_id);
|
||||
if ($local_result['code'] < 0)
|
||||
return $local_result;
|
||||
|
||||
$order_status = $order_info['order_status'];
|
||||
$virtual_order_model = new VirtualOrder();
|
||||
if ($order_status == $order_common_model::ORDER_TAKE_DELIVERY || $order_status == $virtual_order_model::ORDER_VERIFYED) {
|
||||
// 虚拟商品无需确认收货
|
||||
return $this->error('', '该订单已收货!');
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
|
||||
public function event($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
|
||||
$order_status = $data['order_status'];
|
||||
//改变订单状态
|
||||
$virtual_order_model = new VirtualOrder();
|
||||
$order_common_model = new OrderCommon();
|
||||
//todo 如果是虚拟商品并且有虚拟码的话, 订单状态应该为已使用
|
||||
$order_model = $order_common_model->getOrderModel($order_info);
|
||||
if ($order_status == $virtual_order_model::ORDER_WAIT_VERIFY) {
|
||||
$order_action_array = $order_common_model->getOrderCommonAction($order_model, $virtual_order_model::ORDER_VERIFYED);
|
||||
} else {
|
||||
$order_action_array = $order_common_model->getOrderCommonAction($order_model, $order_model::ORDER_TAKE_DELIVERY);
|
||||
}
|
||||
|
||||
$order_data = array(
|
||||
'order_status' => $order_action_array['order_status'],
|
||||
'order_status_name' => $order_action_array['order_status_name'],
|
||||
'order_status_action' => $order_action_array['order_status_action'],
|
||||
'is_evaluate' => 1,
|
||||
'evaluate_status' => OrderDict::evaluate_wait,
|
||||
'evaluate_status_name' => OrderDict::getEvaluateStatus(OrderDict::evaluate_wait),
|
||||
'sign_time' => time()
|
||||
);
|
||||
model('order')->update($order_data, [['order_id', '=', $order_id]]);
|
||||
OrderCron::complete(['order_id' => $order_id, 'site_id' => $order_info['site_id']]);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
|
||||
$order_action_array = $data['order_action_array'];
|
||||
$log_data = $data['data'];
|
||||
$order_common_model = new OrderCommon();
|
||||
//记录订单日志 start
|
||||
if ($log_data) {
|
||||
$action = '商家对订单进行了确认收货';
|
||||
if ($log_data['action_way'] == 1) {
|
||||
$member_info = model('member')->getInfo(['member_id' => $log_data['uid']], 'nickname');
|
||||
$buyer_name = empty($member_info['nickname']) ? '' : '【' . $member_info['nickname'] . '】';
|
||||
$log_data['nick_name'] = $buyer_name;
|
||||
$action = '买家确认收到货物';
|
||||
}
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_info['order_id'],
|
||||
'action' => $action,
|
||||
'order_status' => $order_action_array['order_status'],
|
||||
'order_status_name' => $order_action_array['order_status_name'],
|
||||
]);
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
}
|
||||
336
app/model/order/ordercreate/CommonTool.php
Executable file
336
app/model/order/ordercreate/CommonTool.php
Executable file
@@ -0,0 +1,336 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\ordercreate;
|
||||
|
||||
use app\model\member\Member;
|
||||
use app\model\member\MemberAddress;
|
||||
use app\model\store\Store;
|
||||
use app\model\system\Site;
|
||||
|
||||
/**
|
||||
* 订单创建 可调用的工具类
|
||||
*/
|
||||
trait CommonTool
|
||||
{
|
||||
|
||||
/****************************************************************************** 站点 start *****************************************************************************/
|
||||
/**
|
||||
* 初始化站点信息
|
||||
* @return true
|
||||
*/
|
||||
public function initSiteData()
|
||||
{
|
||||
$site_model = new Site();
|
||||
$site_condition = array(
|
||||
['site_id', '=', $this->site_id]
|
||||
);
|
||||
$site_info = $site_model->getSiteInfo($site_condition)['data'] ?? [];
|
||||
$this->site_info = $site_info;
|
||||
$this->site_name = $site_info['site_name'] ?? '';
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 站点 end *****************************************************************************/
|
||||
/****************************************************************************** 门店 start *****************************************************************************/
|
||||
/**
|
||||
* 初始化门店
|
||||
* @return true
|
||||
*/
|
||||
public function initStore()
|
||||
{
|
||||
$store_id = $this->param['store_id'] ?? 0;
|
||||
$store_model = new Store();
|
||||
if ($store_id == 0) {
|
||||
|
||||
// $is_allow_store = false;
|
||||
// //是否安装门店插件
|
||||
// //todo 只有存在门店插件,并且开启门店连锁模式,才可以所以传递store_id, 否则就只有门店配送和本地配送就可以接收store_id
|
||||
// if (addon_is_exit('store')) {
|
||||
// //查询门店运营插件
|
||||
// $store_config_model = new \addon\store\model\Config();
|
||||
// $store_config = $store_config_model->getStoreBusinessConfig($site_id)[ 'data' ][ 'value' ] ?? [];
|
||||
// if ($store_config[ 'store_business' ] == 'store') {
|
||||
// $is_allow_store = true;
|
||||
// }
|
||||
// }
|
||||
// if($is_allow_store){
|
||||
// $store_info = $store_model->getDefaultStore($site_id)[ 'data' ] ?? [];
|
||||
// $data[ 'store_info' ] = $store_info;
|
||||
// $data[ 'store_id' ] = $store_info[ 'store_id' ];
|
||||
// }
|
||||
|
||||
} else {
|
||||
$cashier_type = $this->param['cashier_type'] ?? '';
|
||||
if ($cashier_type == 'cashier') {
|
||||
|
||||
} else {
|
||||
$is_allow_store = false;
|
||||
//是否安装门店插件
|
||||
//todo 只有存在门店插件,并且开启门店连锁模式,才可以所以传递store_id, 否则就只有门店配送和本地配送就可以接收store_id
|
||||
if (addon_is_exit('store')) {
|
||||
//查询门店运营插件
|
||||
$store_config = $this->config('store_business');
|
||||
if ($store_config['store_business'] == 'store') {
|
||||
$is_allow_store = true;
|
||||
}
|
||||
}
|
||||
if (!$is_allow_store) {
|
||||
$delivery_array = $this->param['delivery'] ?? [];
|
||||
$delivery_type = $delivery_array['delivery_type'] ?? 'express';
|
||||
if (!in_array($delivery_type, ['local', 'store'])) {
|
||||
$store_id = 0;
|
||||
} else {
|
||||
$store_id = $this->param['delivery']['store_id'] ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->store_info = $store_model->getStoreInfo([['site_id', '=', $this->site_id], ['store_id', '=', $store_id]])['data'] ?? [];
|
||||
if (empty($this->store_info)) {
|
||||
$store_id = 0;
|
||||
}else{
|
||||
if($this->store_info['status'] == 0){
|
||||
if($cashier_type != 'cashier'){
|
||||
$this->setError(1, '当前门店休息中!');
|
||||
}
|
||||
}else{
|
||||
$delivery_array = $this->param['delivery'] ?? [];
|
||||
$delivery_type = $delivery_array['delivery_type'] ?? 'express';
|
||||
if($delivery_type == 'local'){
|
||||
if(addon_is_exit('store') && $this->store_info['is_o2o'] == 0){
|
||||
$this->setError(1, '同城配送未开启');
|
||||
}
|
||||
if($this->store_info['out_open_date_o2o_pay'] == 0){
|
||||
$curr_time = time() - strtotime(date('Y-m-d'));
|
||||
$open_date_config = json_decode($this->store_info['open_date_config'], true);
|
||||
$o2o_pay = false;
|
||||
if(!empty($open_date_config)){
|
||||
foreach($open_date_config as $item){
|
||||
if($item['start_time'] <= $curr_time && $item['end_time'] >= $curr_time){
|
||||
$o2o_pay = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$o2o_pay = true;
|
||||
}
|
||||
if(!$o2o_pay){
|
||||
$this->setError(1, '请在营业时间内下单');
|
||||
}
|
||||
}
|
||||
}
|
||||
if($delivery_type == 'pickup'){
|
||||
if(addon_is_exit('store') && $this->store_info['is_pickup'] == 0){
|
||||
$this->setError(1, '门店自提未开启');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->delivery['store_info'] = $this->store_info;
|
||||
$this->store_id = $store_id;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 补齐门店数据
|
||||
* @return true
|
||||
*/
|
||||
public function storeOrderData()
|
||||
{
|
||||
$temp_data = [];
|
||||
$delivery_store_id = $this->delivery['store_id'] ?? 0; //门店id
|
||||
|
||||
if ($delivery_store_id > 0) {
|
||||
$store_model = new Store();
|
||||
$condition = array(
|
||||
['store_id', '=', $delivery_store_id],
|
||||
['site_id', '=', $this->site_id],
|
||||
);
|
||||
$store_info = $store_model->getStoreInfo($condition)['data'] ?? [];
|
||||
if (empty($store_info)) {
|
||||
$this->setError(1, '当前门店不存在!');
|
||||
}else if($store_info['status'] == 0){
|
||||
$this->setError(1, '当前门店休息中');
|
||||
}else if($store_info['is_pickup'] == 0){
|
||||
$this->setError(1, '当前门店未开启自提');
|
||||
} else {
|
||||
$this->delivery['delivery_store_id'] = $delivery_store_id;
|
||||
$delivery_store_name = $store_info['store_name'];
|
||||
$this->delivery['delivery_store_name'] = $delivery_store_name;
|
||||
$delivery_store_info = array(
|
||||
'open_date' => $store_info['open_date'],
|
||||
'full_address' => $store_info['full_address'] . $store_info['address'],
|
||||
'longitude' => $store_info['longitude'],
|
||||
'latitude' => $store_info['latitude'],
|
||||
'telphone' => $store_info['telphone'],
|
||||
'store_image' => $store_info['store_image'],
|
||||
'time_type' => $store_info['time_type'],
|
||||
'time_week' => $store_info['time_week'],
|
||||
'start_time' => $store_info['start_time'],
|
||||
'end_time' => $store_info['end_time'],
|
||||
);
|
||||
$this->delivery['delivery_store_info'] = json_encode($delivery_store_info, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
} else {
|
||||
$this->setError(1, '配送门店不可为空!');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/****************************************************************************** 门店 end *****************************************************************************/
|
||||
/****************************************************************************** 发票 start *****************************************************************************/
|
||||
/**
|
||||
* 获取发票信息
|
||||
* @return true
|
||||
*/
|
||||
public function getInovice()
|
||||
{
|
||||
$order_config = $this->config('order');
|
||||
$invoice_status = $order_config['invoice_status'] ?? 0;
|
||||
$this->invoice['invoice_status'] = $invoice_status;
|
||||
if ($invoice_status == 1) {
|
||||
$invoice_content = $order_config['invoice_content'] ?? '';
|
||||
$invoice_content_array = explode(',', $invoice_content);
|
||||
$this->invoice['invoice_content_array'] = $invoice_content_array;
|
||||
$this->invoice['invoice_delivery_money'] = $order_config['invoice_money'] ?? 0;
|
||||
$this->invoice['invoice_rate'] = $order_config['invoice_rate'] ?? 0;
|
||||
$this->invoice['invoice_type'] = $order_config['invoice_type'] ?? '1,2';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算发票信息
|
||||
* @return true
|
||||
*/
|
||||
public function calculateInvoice()
|
||||
{
|
||||
$order_config = $this->config('order');
|
||||
$invoice_status = $order_config['invoice_status'] ?? 0;
|
||||
$this->invoice['invoice_status'] = $invoice_status;
|
||||
$invoice_money = 0;
|
||||
$invoice_delivery_money = 0;
|
||||
if ($invoice_status == 1) {
|
||||
$is_invoice = $this->param['is_invoice'] ?? 0;
|
||||
//是否需要发票
|
||||
if ($is_invoice) {
|
||||
$real_goods_money = $this->goods_money - $this->promotion_money - $this->coupon_money - $this->point_money;
|
||||
$invoice_money = round($real_goods_money * $this->invoice['invoice_rate'] / 100, 2);
|
||||
$invoice_type = $this->param['invoice_type'] ?? 1;
|
||||
$this->invoice = [
|
||||
'invoice_title_type' => $this->param['invoice_title_type'],
|
||||
'is_tax_invoice' => $this->param['is_tax_invoice'],
|
||||
'taxpayer_number' => $this->param['taxpayer_number'],
|
||||
'invoice_title' => $this->param['invoice_title'],
|
||||
'invoice_type' => $this->param['invoice_type'],
|
||||
'invoice_content' => $this->param['invoice_content'],
|
||||
'invoice_rate' => $order_config['invoice_rate'],
|
||||
];
|
||||
|
||||
if ($invoice_type == 1) {
|
||||
$invoice_delivery_money = $order_config['invoice_money'];
|
||||
//未定义发票收货地址的话,会默认使用收发货地址
|
||||
if (empty($this->param['invoice_full_address'])) {
|
||||
if ($this->delivery['delivery_type'] == 'express' || $this->delivery['delivery_type'] == 'local') {
|
||||
$invoice_full_address = $this->delivery['member_address']['full_address'] . $this->delivery['member_address']['address'];
|
||||
} else if ($this->delivery['delivery_type'] == 'store') { //门店
|
||||
$delivery_store_info = json_decode($this->delivery['delivery_store_info'], true);
|
||||
$invoice_full_address = $delivery_store_info['full_address'];
|
||||
}
|
||||
} else {
|
||||
$invoice_full_address = $this->param['invoice_full_address'] ?? '';
|
||||
}
|
||||
$this->invoice['invoice_full_address'] = $invoice_full_address ?? '';
|
||||
} else {
|
||||
if (empty($this->param['invoice_email'])) {
|
||||
$this->setError(1, '发票邮箱不能为空!');
|
||||
} else {
|
||||
$this->invoice['invoice_email'] = $this->param['invoice_email'];
|
||||
}
|
||||
}
|
||||
if (empty($this->param['invoice_title']) || empty($this->param['invoice_type']) || empty($this->param['invoice_content'] || $this->param['invoice_title_type'] == 0)) {
|
||||
$this->setError(1, '发票相关项不能为空!');
|
||||
}
|
||||
//企业抬头 必须填写税号
|
||||
if ($this->param['invoice_title_type'] == 2 && empty($this->param['taxpayer_number'])) {
|
||||
$this->setError(1, '发票相关项不能为空!');
|
||||
}
|
||||
}
|
||||
}
|
||||
//发票费用和发票邮寄费用
|
||||
$this->invoice_money = $invoice_money;
|
||||
$this->invoice_delivery_money = $invoice_delivery_money;
|
||||
$this->order_money += $this->invoice_money + $this->invoice_delivery_money;
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 发票 end *****************************************************************************/
|
||||
|
||||
/****************************************************************************** 杂项 start *****************************************************************************/
|
||||
/**
|
||||
* 初始化收货地址
|
||||
* @return true
|
||||
*/
|
||||
public function initMemberAddress()
|
||||
{
|
||||
$delivery_type = $this->param['delivery']['delivery_type'] ?? '';
|
||||
if (empty($this->param['delivery']['member_address'])) {
|
||||
$member_address = new MemberAddress();
|
||||
$type = 1;
|
||||
if ($delivery_type == 'local') {
|
||||
$type = 2;
|
||||
}
|
||||
$this->delivery['member_address'] = $member_address->getMemberAddressInfo([['member_id', '=', $this->member_id], ['is_default', '=', 1], ['type', 'in', $type]])['data'];
|
||||
} else {
|
||||
$this->delivery['member_address'] = $this->param['delivery']['member_address'];
|
||||
}
|
||||
if (!empty($this->delivery['member_address'])) {
|
||||
if ($delivery_type == 'local') {
|
||||
//外卖订单 如果收货地址没有定位的话,就不取用地址
|
||||
$type = $this->delivery['member_address']['type'] ?? 1;
|
||||
if ($type == 1) {
|
||||
$this->delivery['member_address'] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化会员账户
|
||||
* @return true
|
||||
*/
|
||||
public function initMemberAccount()
|
||||
{
|
||||
$member_model = new Member();
|
||||
$member_info = $member_model->getMemberDetail($this->member_id, $this->site_id)['data'] ?? [];
|
||||
if (!empty($member_info)) {
|
||||
if (!empty($member_info['pay_password'])) {
|
||||
$is_pay_password = 1;
|
||||
} else {
|
||||
$is_pay_password = 0;
|
||||
}
|
||||
unset($member_info['pay_password']);
|
||||
$member_info['is_pay_password'] = $is_pay_password;
|
||||
$this->member_account = $member_info;
|
||||
|
||||
//初始化会员等级
|
||||
$this->member_level = model('member_level')->getInfo([
|
||||
['level_id', '=', $this->member_account['member_level']]
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 杂项 end *****************************************************************************/
|
||||
}
|
||||
444
app/model/order/ordercreate/DeliveryTool.php
Executable file
444
app/model/order/ordercreate/DeliveryTool.php
Executable file
@@ -0,0 +1,444 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\ordercreate;
|
||||
|
||||
use app\model\express\Express;
|
||||
use app\model\express\Local;
|
||||
use app\model\store\Store;
|
||||
|
||||
/**
|
||||
* 订单创建 可调用的工具类
|
||||
*/
|
||||
trait DeliveryTool
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取配送配置数据
|
||||
* @return true
|
||||
*/
|
||||
public function getDeliveryData()
|
||||
{
|
||||
$curr_store_id = $this->param['store_id'] ?? 0;
|
||||
$jielong_id = $this->param['jielong_id'] ?? 0;
|
||||
$express_type = [];
|
||||
if ($this->is_virtual == 0) {
|
||||
//todo 没有活动才会参与计算
|
||||
if (empty($this->promotion_type)) $trade_result = event('OrderCreateCommonData', ['type' => 'trade', 'order_object' => $this], true);
|
||||
if (!empty($trade_result)) {
|
||||
if ($trade_result['code'] >= 0) {
|
||||
$express_type = $trade_result['data'];
|
||||
}
|
||||
} else {
|
||||
$deliver_type_sort = $this->config('delivery_type_sort');
|
||||
$deliver_sort_list = explode(',', $deliver_type_sort['value']['deliver_type']);
|
||||
//根据当前的定位查询本地配送门店或自提门店
|
||||
$latitude = $this->param['latitude'] ?? 0;
|
||||
$longitude = $this->param['longitude'] ?? 0;
|
||||
foreach ($deliver_sort_list as $type) {
|
||||
// 物流
|
||||
if ($type == 'express') {
|
||||
$express_config = $this->config('express');
|
||||
if ($express_config['is_use'] == 1) {
|
||||
$title = $express_config['value']['express_name'];
|
||||
if ($title == '') {
|
||||
$title = Express::express_type['express']['title'];
|
||||
}
|
||||
|
||||
$express_type[] = ['title' => $title, 'name' => 'express'];
|
||||
}
|
||||
|
||||
}
|
||||
// 自提
|
||||
if ($type == 'store') {
|
||||
$store_config = $this->config('store');
|
||||
if ($store_config['is_use'] == 1) {
|
||||
//根据坐标查询门店
|
||||
$store_model = new Store();
|
||||
$store_condition = [
|
||||
['site_id', '=', $this->site_id],
|
||||
//['is_pickup', '=', 1],
|
||||
//['status', '=', 1],
|
||||
['', 'exp', \think\facade\Db::raw('(is_pickup = 1 && status = 1) or store_id = '.$curr_store_id)],
|
||||
['is_frozen', '=', 0],
|
||||
];
|
||||
//考虑门店库存和上下架,进一步过滤可以选择的门店
|
||||
$this->getAvailableStoreIds();
|
||||
if($this->available_store_ids != 'all'){
|
||||
$store_condition[] = ['store_id', 'in', $this->available_store_ids];
|
||||
}
|
||||
$latlng = [
|
||||
'lat' => $latitude,
|
||||
'lng' => $longitude,
|
||||
];
|
||||
$store_list = $store_model->getLocationStoreList($store_condition, '*', $latlng)['data'] ?? [];
|
||||
$store_list = $this->currStoreToFirstData($store_list);
|
||||
$title = $store_config['value']['store_name'];
|
||||
if ($title == '') {
|
||||
$title = Express::express_type['store']['title'];
|
||||
}
|
||||
$express_type[] = ['title' => $title, 'name' => 'store', 'store_list' => $store_list];
|
||||
}
|
||||
|
||||
}
|
||||
// 外卖
|
||||
if ($type == 'local') {
|
||||
$local_config = $this->config('local');
|
||||
if ($local_config['is_use'] == 1) {
|
||||
//查询本店的通讯地址
|
||||
$title = $local_config['value']['local_name'];
|
||||
if ($title == '') {
|
||||
$title = '外卖配送';
|
||||
}
|
||||
$store_model = new Store();
|
||||
$store_condition = [
|
||||
['site_id', '=', $this->site_id],
|
||||
];
|
||||
if (addon_is_exit('store', $this->site_id)) {
|
||||
//$store_condition[] = ['is_o2o', '=', 1];
|
||||
//$store_condition[] = ['status', '=', 1];
|
||||
$store_condition[] = ['', 'exp', \think\facade\Db::raw('(is_o2o = 1 && status = 1) or store_id = '.$curr_store_id)];
|
||||
$store_condition[] = ['is_frozen', '=', 0];
|
||||
} else {
|
||||
$store_condition[] = ['is_default', '=', 1];
|
||||
}
|
||||
//考虑门店库存和上下架,进一步过滤可以选择的门店
|
||||
$this->getAvailableStoreIds();
|
||||
if($this->available_store_ids != 'all'){
|
||||
$store_condition[] = ['store_id', 'in', $this->available_store_ids];
|
||||
}
|
||||
$latlng = [
|
||||
'lat' => $latitude,
|
||||
'lng' => $longitude,
|
||||
];
|
||||
$store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
|
||||
$store_list = $store_list_result['data'];
|
||||
$store_list = $this->currStoreToFirstData($store_list);
|
||||
$express_type[] = ['title' => $title, 'name' => 'local', 'store_list' => $store_list];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($jielong_id)){
|
||||
//社群接龙只保留门店自提
|
||||
foreach ($express_type as $key => $value){
|
||||
if($value['name'] != 'store'){
|
||||
unset($express_type[$key]);
|
||||
continue;
|
||||
}
|
||||
$store_list = $value['store_list'] ?? [];
|
||||
if(empty($store_list)){
|
||||
$this->setError(1, '没有可以自提的门店!');
|
||||
}
|
||||
}
|
||||
$this->jielong_info = model('promotion_jielong')->getInfo([ [ 'jielong_id', '=', $jielong_id ] ], '*');
|
||||
$express_type = array_values($express_type);
|
||||
}
|
||||
$this->delivery['express_type'] = $express_type;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配送计算
|
||||
* @return true
|
||||
*/
|
||||
public function calculateDelivery()
|
||||
{
|
||||
//整理配送时间格式
|
||||
$this->getDeliveryTime();
|
||||
//计算邮费
|
||||
if ($this->is_virtual == 1) {
|
||||
//虚拟订单 运费为0
|
||||
$delivery_money = 0;
|
||||
$this->delivery['delivery_type'] = '';
|
||||
} else {
|
||||
$deliver_type_sort = $this->config('delivery_type_sort');
|
||||
$deliver_sort_list = explode(',', $deliver_type_sort['value']['deliver_type']);
|
||||
//查询店铺是否开启快递配送
|
||||
$express_config = $this->config('express');
|
||||
|
||||
//查询店铺是否开启门店自提
|
||||
$store_config = $this->config('store');
|
||||
|
||||
//查询店铺是否开启外卖配送
|
||||
$local_config = $this->config('local');
|
||||
//todo 没有活动才会参与计算
|
||||
if (empty($this->promotion_type)) {
|
||||
$trade_calc_result = event('OrderCreateCommonData', ['type' => 'trade_calc', 'order_object' => $this, 'deliver_sort_list' => $deliver_sort_list], true);
|
||||
}
|
||||
if (empty($trade_calc_result)) {
|
||||
//如果本地配送开启, 则查询出本地配送的配置
|
||||
if ($local_config['is_use'] == 1 && isset($this->param['delivery']['store_id'])) {
|
||||
$local_model = new Local();
|
||||
$local_info = $local_model->getLocalInfo([['site_id', '=', $this->site_id], ['store_id', '=', $this->param['delivery']['store_id']]])['data'];
|
||||
$this->delivery['local']['info'] = $local_info;
|
||||
} else {
|
||||
$this->delivery['local']['info'] = [];
|
||||
}
|
||||
$delivery_array = $this->param['delivery'] ?? [];
|
||||
$delivery_type = $delivery_array['delivery_type'] ?? 'express';
|
||||
if ($delivery_type == 'store') {
|
||||
//门店自提
|
||||
$delivery_money = 0;
|
||||
$this->delivery['delivery_type'] = 'store';
|
||||
if ($store_config['is_use'] == 0) {
|
||||
$this->setError(1, '门店自提方式未开启!');
|
||||
}
|
||||
if (empty($this->param['delivery']['store_id'])) {
|
||||
$this->setError(1, '门店未选择!');
|
||||
}
|
||||
$this->delivery['store_id'] = $this->param['delivery']['store_id'] ?? 0;
|
||||
$this->storeOrderData();
|
||||
$this->store_id = $this->param['delivery']['store_id'] ?? 0;
|
||||
|
||||
} else {
|
||||
if (empty($this->delivery['member_address'])) {
|
||||
$delivery_money = 0;
|
||||
$this->delivery['delivery_type'] = 'express';
|
||||
$this->setError(1, '未配置默认收货地址!');
|
||||
} else {
|
||||
if ($delivery_type == 'express') {
|
||||
if ($express_config['is_use'] == 1) {
|
||||
//物流配送
|
||||
$express = new Express();
|
||||
$express_fee_result = $express->calculate(['order_object' => $this]);
|
||||
if ($express_fee_result['code'] < 0) {
|
||||
$this->setError(1, $express_fee_result['message']);
|
||||
$delivery_fee = 0;
|
||||
} else {
|
||||
$delivery_fee = $express_fee_result['data']['delivery_fee'];
|
||||
}
|
||||
} else {
|
||||
$this->setError(1, '物流配送方式未开启!');
|
||||
$delivery_fee = 0;
|
||||
}
|
||||
$this->delivery_money = $delivery_fee;
|
||||
$this->delivery['delivery_type'] = 'express';
|
||||
} else if ($delivery_type == 'local') {
|
||||
//外卖配送
|
||||
$delivery_money = 0;
|
||||
$this->delivery['delivery_type'] = 'local';
|
||||
if ($local_config['is_use'] == 0) {
|
||||
$this->setError(1, '外卖配送方式未开启!');
|
||||
} else {
|
||||
if (empty($this->param['delivery']['store_id'])) {
|
||||
$this->setError(1, '门店未选择!');
|
||||
}
|
||||
|
||||
$this->store_id = $this->param['delivery']['store_id'] ?? 0;
|
||||
|
||||
$local_model = new Local();
|
||||
$local_result = $local_model->calculate(['order_object' => $this]);
|
||||
|
||||
$this->delivery['start_money'] = 0;
|
||||
if ($local_result['code'] < 0) {
|
||||
$this->delivery['start_money'] = $local_result['data']['start_money_array'][0] ?? 0;
|
||||
$this->setError($local_result['data']['code'], $local_result['message'], 1);
|
||||
} else {
|
||||
$this->delivery_money = $local_result['data']['delivery_money'];
|
||||
if (!empty($local_result['data']['code'])) {
|
||||
$this->setError($local_result['data']['code'], $local_result['data']['error'], 1);
|
||||
}
|
||||
}
|
||||
|
||||
$this->delivery['error'] = $this->error;
|
||||
$this->delivery['error_msg'] = $this->error_msg;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//检测门店是否可用
|
||||
if($this->available_store_ids != 'all' && !empty($this->store_id)){
|
||||
$available_store_ids = $this->available_store_ids ? explode(',', trim($this->available_store_ids, ',')) : [];
|
||||
if(!in_array($this->store_id,$available_store_ids)){
|
||||
$this->setError(1, '所选门店不可用!');
|
||||
}
|
||||
}
|
||||
|
||||
//是否符合免邮
|
||||
if ($this->is_free_delivery) {
|
||||
$this->delivery_money = 0;
|
||||
}
|
||||
//重新计算订单总额
|
||||
$this->getOrderMoney();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 校验商品项的配送方式支持方式
|
||||
* @param $goods
|
||||
* @return true
|
||||
*/
|
||||
public function checkDeliveryType($goods)
|
||||
{
|
||||
$delivery_type = $this->param['delivery']['delivery_type'] ?? '';
|
||||
if ($delivery_type && strpos($goods['support_trade_type'], $delivery_type) === false) {
|
||||
$express_type_list = $this->config('delivery_type');
|
||||
$delivery_type_name = $express_type_list[$delivery_type] ?? '';
|
||||
$this->error = 1;
|
||||
$this->error_msg = '有商品不支持' . $delivery_type_name;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量校验配送方式
|
||||
* @return true
|
||||
*/
|
||||
public function batchCheckDeliveryType()
|
||||
{
|
||||
$delivery_type = $this->param['delivery']['delivery_type'] ?? '';
|
||||
if (!$this->is_virtual) {
|
||||
if (!$delivery_type) {
|
||||
$this->error = 1;
|
||||
$this->error_msg = '请选择有效的配送方式';
|
||||
} else {
|
||||
$express_type_list = $this->config('delivery_type');
|
||||
$delivery_type_name = $express_type_list[$delivery_type] ?? '';
|
||||
foreach ($this->goods_list as $v) {
|
||||
if (strpos($v['support_trade_type'], $delivery_type) === false) {
|
||||
$this->error = 1;
|
||||
//$this->error_msg = '商品' . $v['goods_name'] . '不支持' . $delivery_type_name;
|
||||
$this->error_msg = '部分商品不支持' . $delivery_type_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配送时间格式
|
||||
* @return true
|
||||
*/
|
||||
public function getDeliveryTime(){
|
||||
$delivery_time = $this->param['delivery']['buyer_ask_delivery_time'] ?? [];
|
||||
$remark = '';
|
||||
$start_date = $delivery_time['start_date'] ?? '';
|
||||
$end_date = $delivery_time['end_date'] ?? '';
|
||||
if($start_date && $end_date){
|
||||
$start_time = strtotime($start_date);
|
||||
$end_time = strtotime($end_date);
|
||||
$start_ymd = date('Y-m-d', $start_time);
|
||||
$end_ymd = date('Y-m-d', $end_time);
|
||||
if($start_ymd == $end_ymd){
|
||||
$remark = $start_ymd.' '.date('H:i:s', $start_time).' ~ '.date('H:i:s', $end_time);
|
||||
}else{
|
||||
$remark = $start_date .' ~ '. $end_date;
|
||||
}
|
||||
|
||||
}else if($start_date && !$end_date){
|
||||
$remark = $start_date;
|
||||
$start_time = strtotime($start_date);
|
||||
}else if(!$start_date && $end_date){
|
||||
$remark = $end_date;
|
||||
$end_time = strtotime($end_date);
|
||||
}
|
||||
|
||||
if(!empty($this->jielong_id)){
|
||||
$jielong_info = model('promotion_jielong')->getInfo([ [ 'jielong_id', '=', $this->jielong_id ] ], '*');
|
||||
if(!empty($jielong_info)){
|
||||
$start_time = $jielong_info['take_start_time'];
|
||||
$start_date = date("Y-m-d",$jielong_info['take_start_time']);
|
||||
$end_time = $jielong_info['take_end_time'];
|
||||
$end_date = date("Y-m-d",$jielong_info['take_end_time']);
|
||||
$remark = $start_date.' ~ '.$end_date;
|
||||
}
|
||||
}
|
||||
|
||||
$this->delivery['buyer_ask_delivery_time'] = [
|
||||
'start_date' => $start_date,
|
||||
'start_time' => $start_time ?? '',
|
||||
'end_date' => $end_date,
|
||||
'end_time' => $end_time ?? '',
|
||||
'remark' => $remark
|
||||
];
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取可以购买的门店数据
|
||||
* @return int[]|string[]
|
||||
*/
|
||||
protected function getAvailableStoreIds()
|
||||
{
|
||||
if(addon_is_exit('store')){
|
||||
//要注意是独立库存还是总部库存
|
||||
$alias = 'sgs';
|
||||
$join = [
|
||||
['store s', 'sgs.store_id = s.store_id', 'inner'],
|
||||
['goods_sku gs', 'sgs.sku_id = gs.sku_id', 'inner'],
|
||||
];
|
||||
$condition = [
|
||||
['sgs.sku_id', 'in', array_column($this->goods_list, 'sku_id')],
|
||||
['sgs.status', '=', 1],
|
||||
];
|
||||
if($this->available_store_ids != 'all'){
|
||||
$condition[] = ['sgs.store_id', 'in', $this->available_store_ids];
|
||||
}
|
||||
$field = "sgs.sku_id,sgs.store_id,IF(s.stock_type = 'store', sgs.stock, gs.stock) as stock";
|
||||
$store_sku_list = model('store_goods_sku')->getList($condition, $field, '', $alias, $join);
|
||||
|
||||
$store_id_data = [];
|
||||
$sku_num_data = array_column($this->goods_list, 'num','sku_id');
|
||||
$goods_model = new \app\model\goods\Goods();
|
||||
foreach($store_sku_list as $store_sku_info){
|
||||
//如果库存不足尝试库存转换
|
||||
if($store_sku_info['stock'] < $sku_num_data[$store_sku_info['sku_id']]){
|
||||
$store_sku_info = $goods_model->goodsStockTransform([$store_sku_info], $store_sku_info['store_id'], 'store')[0];
|
||||
}
|
||||
if($store_sku_info['stock'] >= $sku_num_data[$store_sku_info['sku_id']]){
|
||||
$store_id_data[$store_sku_info['store_id']][$store_sku_info['sku_id']] = $store_sku_info['sku_id'];
|
||||
}
|
||||
}
|
||||
foreach($store_id_data as $store_id=>$data){
|
||||
if(count($data) < count($sku_num_data)){
|
||||
unset($store_id_data[$store_id]);
|
||||
}
|
||||
}
|
||||
$this->available_store_ids = array_keys($store_id_data);
|
||||
$curr_store_id = $this->param['store_id'] ?? 0;
|
||||
if(!in_array($curr_store_id, $this->available_store_ids)){
|
||||
$this->available_store_ids[] = $curr_store_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前门店排到首位
|
||||
* @param $store_list
|
||||
* @return array
|
||||
*/
|
||||
protected function currStoreToFirstData($store_list)
|
||||
{
|
||||
$curr_store_id = $this->param['store_id'] ?? 0;
|
||||
$curr_store = null;
|
||||
foreach($store_list as $key=>$store_info){
|
||||
if($store_info['store_id'] == $curr_store_id){
|
||||
$curr_store = $store_info;
|
||||
unset($store_list[$key]);
|
||||
}
|
||||
}
|
||||
if(!is_null($curr_store)){
|
||||
array_unshift($store_list, $curr_store);
|
||||
}
|
||||
return array_values($store_list);
|
||||
}
|
||||
}
|
||||
254
app/model/order/ordercreate/GoodsTool.php
Executable file
254
app/model/order/ordercreate/GoodsTool.php
Executable file
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\ordercreate;
|
||||
|
||||
use app\dict\goods\GoodsDict;
|
||||
use app\model\goods\Goods;
|
||||
use app\model\order\OrderStock;
|
||||
use extend\exception\OrderException;
|
||||
|
||||
/**
|
||||
* 订单创建 可调用的工具类
|
||||
*/
|
||||
trait GoodsTool
|
||||
{
|
||||
|
||||
/**
|
||||
* 校验限购
|
||||
* @return true
|
||||
*/
|
||||
public function checkLimitPurchase()
|
||||
{
|
||||
if ($this->limit_purchase) {
|
||||
foreach ($this->limit_purchase as $item) {
|
||||
//商品长度处理
|
||||
$goods_name = str_sub($item['goods_name'], 12, true, 'end');
|
||||
if ($item['min_buy'] > 0 && $item['num'] < $item['min_buy']) {
|
||||
$this->setError(1, "商品“{$goods_name}”{$item['min_buy']}件起售");
|
||||
break;
|
||||
}
|
||||
|
||||
if ($item['is_limit'] == 1 && $item['max_buy'] > 0) { // 商品做限制购买
|
||||
if ($item['limit_type'] == 1) { // 单次限制
|
||||
if ($item['num'] > $item['max_buy']) {
|
||||
$this->setError(1, "商品“{$goods_name}”每人限购{$item['max_buy']}件");
|
||||
break;
|
||||
}
|
||||
} else { // 长期限制
|
||||
$goods_model = new Goods();
|
||||
$purchased_num = $goods_model->getGoodsPurchasedNum($item['goods_id'], $this->member_id);
|
||||
if (($purchased_num + $item['num']) > $item['max_buy']) {
|
||||
$this->setError(1, "商品“{$goods_name}”每人限购{$item['max_buy']}件,您已购买{$purchased_num}件");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算商品的单价
|
||||
* @param $goods_info
|
||||
* @return array
|
||||
*/
|
||||
public function getGoodsPrice($goods_info)
|
||||
{
|
||||
//判断是否存在限时折扣
|
||||
$discount_price = $goods_info['discount_price'];
|
||||
$price = $discount_price;
|
||||
//计算当前会员的会员购买价
|
||||
$member_result = $this->getGoodsMemberPrice($goods_info);
|
||||
if ($member_result['code'] >= 0) {
|
||||
$member_price = $member_result['data'];
|
||||
if ($member_price < $price) {
|
||||
$price = $member_price;
|
||||
}
|
||||
}
|
||||
return $this->success($price);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品会员价格
|
||||
* @param $goods_info
|
||||
* @return array
|
||||
*/
|
||||
public function getGoodsMemberPrice($goods_info)
|
||||
{
|
||||
if ($this->member_id > 0) {
|
||||
if (addon_is_exit('memberprice') && !empty($this->member_level)) {
|
||||
if ($goods_info['is_consume_discount']) {
|
||||
$price = $goods_info['price'];
|
||||
if ($goods_info['discount_config'] == 1) {
|
||||
// 自定义优惠
|
||||
$goods_info['member_price'] = json_decode($goods_info['member_price'], true);
|
||||
$value = $goods_info['member_price'][$goods_info['discount_method']][$this->member_account['member_level']] ?? 0;
|
||||
switch ($goods_info['discount_method']) {
|
||||
case 'discount':
|
||||
// 打折
|
||||
if ($value == 0) {
|
||||
$member_price = $price;
|
||||
} else{
|
||||
$member_price = number_format($price * $value / 10, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case 'manjian':
|
||||
if ($value == 0) {
|
||||
$member_price = $price;
|
||||
} else{
|
||||
// 满减
|
||||
$member_price = number_format($price - $value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case 'fixed_price':
|
||||
if ($value == 0) {
|
||||
$member_price = $goods_info['price'];
|
||||
} else{
|
||||
// 指定价格
|
||||
$member_price = number_format($value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// 默认按会员享受折扣计算
|
||||
$member_price = number_format($price * $this->member_level['consume_discount'] / 100, 2, '.', '');
|
||||
}
|
||||
return $this->success($member_price);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品库存批量转换
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function batchGoodsStockTransform()
|
||||
{
|
||||
//自动库存转换
|
||||
if(addon_is_exit('stock')){
|
||||
$store_id = $this->param['delivery']['store_id'] ?? $this->param['store_id'];
|
||||
$transform_model = new \addon\stock\model\stock\Transform();
|
||||
$transform_stock_data = $transform_model->getGoodsStockTransformData([
|
||||
'sku_ids' => array_column($this->goods_list, 'sku_id'),
|
||||
'store_id' => $store_id,
|
||||
'store_business' => 'store',
|
||||
]);
|
||||
foreach ($this->goods_list as $v) {
|
||||
$goods_class = $v['goods_class'] ?? 0;
|
||||
if (in_array($goods_class, [GoodsDict::real, GoodsDict::weigh])) {
|
||||
$transform_data = $transform_stock_data[$v['sku_id']] ?? null;
|
||||
if(!is_null($transform_data) && $v['stock'] < $v['num'] && $transform_data['transform_stock'] >= $v['num']){
|
||||
$res = $transform_model->autoGoodsStockTransform([
|
||||
'transform_data' => $transform_data,
|
||||
'buy_num' => $v['num'],
|
||||
'site_id' => $this->site_id,
|
||||
'store_id' => $store_id,
|
||||
]);
|
||||
if($res['code'] < 0) throw new \Exception('库存转换错误:'.$res['message']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单项扣除库存
|
||||
* @return true
|
||||
*/
|
||||
public function batchDecOrderGoodsStock()
|
||||
{
|
||||
$goods_sku_data = [];
|
||||
foreach ($this->goods_list as $v) {
|
||||
$goods_class = $v['goods_class'] ?? 0;
|
||||
if (in_array($goods_class, [GoodsDict::real, GoodsDict::virtual, GoodsDict::virtualcard, GoodsDict::service, GoodsDict::card, GoodsDict::weigh])) {
|
||||
$goods_sku_data[] = [
|
||||
'sku_id' => $v['sku_id'],
|
||||
'num' => $v['num'],
|
||||
];
|
||||
}
|
||||
// $stock_result = $this->skuDecStock($v, $this->store_id);
|
||||
// if ($stock_result['code'] != 0) throw new OrderException($stock_result['message']);
|
||||
}
|
||||
|
||||
if($goods_sku_data){
|
||||
$order_stock = new OrderStock();
|
||||
$stock_result = $order_stock->decOrderSaleStock([
|
||||
'goods_sku_data' => $goods_sku_data,
|
||||
'store_id' => $this->store_id,
|
||||
'create_order_data' => get_object_vars($this)
|
||||
]);
|
||||
if ($stock_result['code'] < 0) throw new OrderException($stock_result['message']);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 扣除商品库存
|
||||
* @param $goods_info
|
||||
* @param int $store_id
|
||||
* @return array
|
||||
*/
|
||||
public function skuDecStock($goods_info, $store_id = 0)
|
||||
{
|
||||
$goods_class = $goods_info['goods_class'] ?? 0;
|
||||
if (!empty($goods_class)) {
|
||||
if (in_array($goods_class, [GoodsDict::real, GoodsDict::virtual, GoodsDict::virtualcard, GoodsDict::service, GoodsDict::card, GoodsDict::weigh])) {
|
||||
$order_stock = new OrderStock();
|
||||
$goods_sku_data = [
|
||||
[
|
||||
'sku_id' => $goods_info['sku_id'],
|
||||
'num' => $goods_info['num']
|
||||
]
|
||||
];
|
||||
$stock_result = $order_stock->decOrderSaleStock([
|
||||
'goods_sku_data' => $goods_sku_data,
|
||||
'store_id' => $store_id,
|
||||
'create_order_data' => get_object_vars($this)
|
||||
]);
|
||||
if ($stock_result['code'] < 0) {
|
||||
return $stock_result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 库存校验是否足够
|
||||
* @return void
|
||||
*/
|
||||
public function checkStock(){
|
||||
$goods_sku_data = [];
|
||||
foreach ($this->goods_list as $v) {
|
||||
$goods_class = $v['goods_class'] ?? 0;
|
||||
if (in_array($goods_class, [GoodsDict::real, GoodsDict::virtual, GoodsDict::virtualcard, GoodsDict::service, GoodsDict::card, GoodsDict::weigh])) {
|
||||
$goods_sku_data[] = [
|
||||
'sku_id' => $v['sku_id'],
|
||||
'num' => $v['num'],
|
||||
'sku_name' => $v['sku_name']
|
||||
];
|
||||
}
|
||||
}
|
||||
if($goods_sku_data){
|
||||
$order_stock = new OrderStock();
|
||||
$order_stock->checkStock([
|
||||
'goods_sku_data' => $goods_sku_data,
|
||||
'store_id' => $this->store_id,
|
||||
'create_order_data' => get_object_vars($this)
|
||||
]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
934
app/model/order/ordercreate/PromotionTool.php
Executable file
934
app/model/order/ordercreate/PromotionTool.php
Executable file
@@ -0,0 +1,934 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\ordercreate;
|
||||
|
||||
use addon\coupon\dict\CouponDict;
|
||||
use addon\coupon\model\Coupon;
|
||||
use addon\freeshipping\model\Freeshipping;
|
||||
use addon\manjian\model\Manjian;
|
||||
use addon\supermember\model\MemberCard;
|
||||
use addon\supermember\model\MemberLevelOrder;
|
||||
use app\dict\member_account\AccountDict;
|
||||
use app\dict\order\OrderPayDict;
|
||||
use app\model\goods\GoodsCategory as GoodsCategoryModel;
|
||||
use app\model\member\MemberAccount;
|
||||
use Exception;
|
||||
use extend\exception\OrderException;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 订单创建 活动优惠项
|
||||
*/
|
||||
trait PromotionTool
|
||||
{
|
||||
|
||||
/****************************************************************************** 余额 start *****************************************************************************/
|
||||
/**
|
||||
* 使用余额
|
||||
* @return array
|
||||
*/
|
||||
public function useBalance()
|
||||
{
|
||||
$balance_config = $this->config('balance');
|
||||
//扣除余额(统一扣除)
|
||||
if ($this->balance_money > 0 && $balance_config['balance_show'] == 1) {
|
||||
$from_type = $this->order_from_type ?: 'order';
|
||||
$this->pay_type = OrderPayDict::balance;
|
||||
$balance_money = $this->member_account['balance_money']; //储值余额
|
||||
$member_balance = $this->member_account['balance']; //现金余额
|
||||
$member_account_model = new MemberAccount();
|
||||
$surplus_balance = $this->balance_money;
|
||||
//优先扣除储值余额
|
||||
if ($member_balance > 0) {
|
||||
$real_balance = min($member_balance, $surplus_balance);
|
||||
$result = $member_account_model->addMemberAccount($this->site_id, $this->member_id, AccountDict::balance, -$real_balance, $from_type, $this->order_id, '订单消费扣除');
|
||||
if ($result['code'] < 0) throw new OrderException($result['message']);
|
||||
|
||||
$surplus_balance -= $real_balance;
|
||||
}
|
||||
if ($surplus_balance > 0) {
|
||||
$result = $member_account_model->addMemberAccount($this->site_id, $this->member_id, AccountDict::balance_money, -$surplus_balance, $from_type, $this->order_id, '订单消费扣除');
|
||||
if ($result['code'] < 0) throw new OrderException($result['message']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算余额优惠
|
||||
* @return true
|
||||
*/
|
||||
public function calculateBalcnce()
|
||||
{
|
||||
//重新计算订单总额
|
||||
$this->getOrderMoney();
|
||||
//使用余额
|
||||
$is_use_balance = $this->param['is_balance'] ?? 0;
|
||||
if ($is_use_balance > 0) {
|
||||
//余额付款
|
||||
$this->member_balance_money = $this->member_account['balance_total'] ?? 0;
|
||||
if ($this->member_balance_money > 0) {
|
||||
$temp_order_money = $this->order_money;
|
||||
if ($temp_order_money <= $this->member_balance_money) {
|
||||
$balance_money = $temp_order_money;
|
||||
} else {
|
||||
$balance_money = $this->member_balance_money;
|
||||
}
|
||||
} else {
|
||||
$balance_money = 0;
|
||||
}
|
||||
$this->balance_money = $balance_money;
|
||||
$this->member_balance_money -= $this->balance_money;//预减少账户余额,还没有实际扣除
|
||||
$this->pay_money = $this->order_money - $this->balance_money;
|
||||
} else {
|
||||
$this->pay_money = $this->order_money;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 余额 end *****************************************************************************/
|
||||
/****************************************************************************** 满额包邮 start *****************************************************************************/
|
||||
/**
|
||||
* 满额包邮
|
||||
* @return true
|
||||
*/
|
||||
public function freeShippingCalculate()
|
||||
{
|
||||
if (addon_is_exit('freeshipping', $this->site_id)) {
|
||||
$free_shipping_model = new Freeshipping();
|
||||
$district_id = $this->delivery['member_address']['district_id'] ?? 0;
|
||||
$free_result = $free_shipping_model->calculate($this->goods_money, $district_id, $this->site_id);
|
||||
if ($free_result['code'] >= 0) {
|
||||
$this->promotion['freeshipping'] = $free_result['data']; //优惠活动 满额包邮
|
||||
$this->is_free_delivery = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 满额包邮 end *****************************************************************************/
|
||||
/****************************************************************************** 会员等级 start *****************************************************************************/
|
||||
/**
|
||||
* 会员等级免邮
|
||||
* @return true
|
||||
*/
|
||||
public function memberLevelCalculate()
|
||||
{
|
||||
if ($this->member_level) {
|
||||
$is_free_shipping = $this->member_level['is_free_shipping'] ?? 0;
|
||||
if ($is_free_shipping > 0) {
|
||||
$this->promotion['member_level'] = $this->member_level; //优惠活动 满额包邮
|
||||
$this->is_free_delivery = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 会员等级 end *****************************************************************************/
|
||||
/****************************************************************************** 订单优惠券 start *****************************************************************************/
|
||||
/**
|
||||
* 查询可用优惠券
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getOrderCouponList()
|
||||
{
|
||||
$this->getOrderCache($this->param['order_key']);
|
||||
$store_id = $this->store_id;
|
||||
$condition = array(
|
||||
['member_id', '=', $this->member_id],
|
||||
['state', '=', 1],
|
||||
['site_id', '=', $this->site_id],
|
||||
//只查看线上的
|
||||
['use_channel', '<>', 'offline'],
|
||||
['', 'exp', Db::raw("use_store = 'all' or FIND_IN_SET({$store_id}, use_store)")],
|
||||
);
|
||||
$member_coupon_model = new Coupon();
|
||||
$goods_category_model = new GoodsCategoryModel();
|
||||
$member_coupon_list = $member_coupon_model->getCouponList($condition)['data'];
|
||||
$coupon_array = [];
|
||||
foreach ($member_coupon_list as $k => $v) {
|
||||
switch ($v['goods_type']) {
|
||||
//全场优惠券
|
||||
case CouponDict::all:
|
||||
if ($v['at_least'] <= $this->goods_money) {
|
||||
$coupon_array[] = $v;
|
||||
}
|
||||
break;
|
||||
//指定商品可用/不可用优惠券
|
||||
case CouponDict::selected:
|
||||
case CouponDict::selected_out:
|
||||
$coupon_goods_array = explode(',', trim($v['goods_ids'], ','));
|
||||
$least_money = 0;
|
||||
$is_support = false;
|
||||
$judge_res = $v['goods_type'] == CouponDict::selected;
|
||||
foreach ($this->goods_list as $v_goods) {
|
||||
if (in_array($v_goods['goods_id'], $coupon_goods_array) == $judge_res) {
|
||||
$least_money += $v_goods['goods_money'];
|
||||
$is_support = true;
|
||||
}
|
||||
}
|
||||
if ($is_support && $v['at_least'] <= $least_money) {
|
||||
$coupon_array[] = $v;
|
||||
}
|
||||
break;
|
||||
//指定分类可用/不可用优惠券
|
||||
case CouponDict::category_selected:
|
||||
case CouponDict::category_selected_out:
|
||||
$category_leaf_ids = $goods_category_model->getGoodsCategoryLeafIds($v['goods_ids'])['data'];
|
||||
$least_money = 0;
|
||||
$is_support = false;
|
||||
foreach ($this->goods_list as $v_goods) {
|
||||
$goods_category_ids = explode(',', trim($v_goods['category_id'], ','));
|
||||
$array_intersect = array_intersect($category_leaf_ids, $goods_category_ids);
|
||||
if ($v['goods_type'] == CouponDict::category_selected) {
|
||||
$judge_res = count($array_intersect) > 0;
|
||||
} else {
|
||||
$judge_res = count($array_intersect) == 0;
|
||||
}
|
||||
if ($judge_res) {
|
||||
$least_money += $v_goods['goods_money'];
|
||||
$is_support = true;
|
||||
}
|
||||
}
|
||||
if ($is_support && $v['at_least'] <= $least_money) {
|
||||
$coupon_array[] = $v;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!empty($coupon_array)) {
|
||||
array_multisort(array_column($coupon_array, 'money'), SORT_DESC, $coupon_array);
|
||||
}
|
||||
|
||||
return $coupon_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用优惠券
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function useCoupon()
|
||||
{
|
||||
if ($this->coupon_id > 0 && $this->coupon_money > 0) {
|
||||
//优惠券处理方案
|
||||
$member_coupon_model = new Coupon();
|
||||
$coupon_use_result = $member_coupon_model->useCoupon($this->coupon_id, $this->member_id, $this->order_id); //使用优惠券
|
||||
if ($coupon_use_result['code'] < 0) {
|
||||
throw new OrderException('COUPON_ERROR');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券活动
|
||||
* @return true
|
||||
*/
|
||||
public function couponPromotion()
|
||||
{
|
||||
$coupon_money = 0;
|
||||
$coupon_id = $this->param['coupon']['coupon_id'] ?? 0;
|
||||
if ($coupon_id > 0) {
|
||||
//查询优惠券信息,计算优惠券费用
|
||||
$coupon_model = new Coupon();
|
||||
$coupon_info = $coupon_model->getCouponInfo(
|
||||
[
|
||||
['coupon_id', '=', $coupon_id],
|
||||
['site_id', '=', $this->site_id],
|
||||
['use_channel', '<>', 'offline'],
|
||||
['', 'exp', Db::raw("use_store = 'all' or FIND_IN_SET({$this->store_id}, use_store)")],
|
||||
], '*')['data'] ?? [];
|
||||
$is_coupon = false;
|
||||
$coupon_goods_money = 0;
|
||||
$goods_list = $this->goods_list;
|
||||
|
||||
if (empty($coupon_info)) {
|
||||
$this->setError(1, '优惠券不存在!');
|
||||
} else if ($coupon_info['member_id'] == $this->member_id && $coupon_info['state'] == 1) {
|
||||
$goods_category_model = new GoodsCategoryModel();
|
||||
$coupon_goods_list = [];
|
||||
switch ($coupon_info['goods_type']) {
|
||||
//全场通用优惠券
|
||||
case CouponDict::all:
|
||||
if ($coupon_info['at_least'] <= $this->goods_money) {
|
||||
$is_coupon = true;
|
||||
} else {
|
||||
$this->setError(1, '优惠券不可用!');
|
||||
}
|
||||
$coupon_goods_money = $this->goods_money;
|
||||
$coupon_goods_list = $goods_list;
|
||||
$goods_list = [];
|
||||
break;
|
||||
//指定商品可用/不可用
|
||||
case CouponDict::selected:
|
||||
case CouponDict::selected_out:
|
||||
// 指定商品
|
||||
$coupon_goods_ids = explode(',', $coupon_info['goods_ids']);
|
||||
$temp_money = 0;
|
||||
$is_support = false;
|
||||
$judge_res = $coupon_info['goods_type'] == CouponDict::selected;
|
||||
foreach ($goods_list as $goods_k => $goods_v) {
|
||||
if (in_array($goods_v['goods_id'], $coupon_goods_ids) == $judge_res) {
|
||||
$temp_money += $goods_v['goods_money'];
|
||||
$coupon_goods_list[] = $goods_v;
|
||||
unset($goods_list[$goods_k]);
|
||||
$is_support = true;
|
||||
}
|
||||
}
|
||||
if ($is_support && $temp_money >= $coupon_info['at_least']) {
|
||||
$is_coupon = true;
|
||||
}
|
||||
$coupon_goods_money = $temp_money;
|
||||
break;
|
||||
//指定分类可用/不可用
|
||||
case CouponDict::category_selected:
|
||||
case CouponDict::category_selected_out:
|
||||
// 指定商品
|
||||
$coupon_category_ids = $goods_category_model->getGoodsCategoryLeafIds($coupon_info['goods_ids'])['data'];
|
||||
$temp_money = 0;
|
||||
$is_support = false;
|
||||
foreach ($goods_list as $goods_k => $goods_v) {
|
||||
$goods_category_ids = explode(',', trim($goods_v['category_id'], ','));
|
||||
$array_intersect = array_intersect($coupon_category_ids, $goods_category_ids);
|
||||
if ($coupon_info['goods_type'] == CouponDict::category_selected) {
|
||||
$judge_res = count($array_intersect) > 0;
|
||||
} else {
|
||||
$judge_res = count($array_intersect) == 0;
|
||||
}
|
||||
if ($judge_res) {
|
||||
$temp_money += $goods_v['goods_money'];
|
||||
$coupon_goods_list[] = $goods_v;
|
||||
unset($goods_list[$goods_k]);
|
||||
$is_support = true;
|
||||
}
|
||||
}
|
||||
if ($is_support && $temp_money >= $coupon_info['at_least']) {
|
||||
$is_coupon = true;
|
||||
}
|
||||
$coupon_goods_money = $temp_money;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($is_coupon) {
|
||||
$coupon_money = 0;
|
||||
if ($coupon_info['type'] == 'reward') {//满减优惠券
|
||||
$coupon_money = min($coupon_info['money'], $coupon_goods_money);
|
||||
} else if ($coupon_info['type'] == 'divideticket') {//瓜分优惠券
|
||||
$coupon_money = min($coupon_info['money'], $coupon_goods_money);
|
||||
} else if ($coupon_info['type'] == 'discount') {//折扣优惠券
|
||||
//计算折扣优惠金额
|
||||
$coupon_money = $coupon_goods_money * (10 - $coupon_info['discount']) / 10;
|
||||
$coupon_money = $coupon_money > $coupon_info['discount_limit'] && $coupon_info['discount_limit'] != 0 ? $coupon_info['discount_limit'] : $coupon_money;
|
||||
$coupon_money = min($coupon_money, $coupon_goods_money);
|
||||
$coupon_money = round($coupon_money, 2);
|
||||
}
|
||||
//计算订单项的金额
|
||||
$temp_goods_list = $this->distributionGoodsCouponMoney($coupon_goods_list, $coupon_goods_money, $coupon_money);
|
||||
$goods_list = array_merge($goods_list, $temp_goods_list);
|
||||
$this->goods_list = $goods_list;
|
||||
} else {
|
||||
$this->setError(1, '优惠券不可用!');
|
||||
}
|
||||
}
|
||||
if ($coupon_money > 0) {
|
||||
if ($coupon_money > $this->order_money) {
|
||||
$coupon_money = $this->order_money;
|
||||
}
|
||||
$this->order_money -= $coupon_money;
|
||||
$this->coupon_money = $coupon_money;
|
||||
if ($coupon_id > 0) {
|
||||
$this->coupon_id = $coupon_id;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按比例摊派优惠券优惠
|
||||
* @param $goods_list
|
||||
* @param $goods_money
|
||||
* @param $coupon_money
|
||||
* @return mixed
|
||||
*/
|
||||
public function distributionGoodsCouponMoney($goods_list, $goods_money, &$coupon_money)
|
||||
{
|
||||
$temp_coupon_money = $coupon_money;
|
||||
$last_key = count($goods_list) - 1;
|
||||
foreach ($goods_list as $k => $v) {
|
||||
if ($last_key != $k) {
|
||||
$item_coupon_money = round($v['real_goods_money'] / $goods_money * $coupon_money, 2);
|
||||
} else {
|
||||
$item_coupon_money = $temp_coupon_money;
|
||||
}
|
||||
$item_coupon_money = min($item_coupon_money, $v['real_goods_money']);
|
||||
$temp_coupon_money -= $item_coupon_money;
|
||||
$goods_list[$k]['coupon_money'] = $item_coupon_money;
|
||||
$goods_list[$k]['real_goods_money'] -= $item_coupon_money; //真实订单项金额
|
||||
}
|
||||
// 如果优惠券没有可抵扣金额
|
||||
if ($temp_coupon_money == $coupon_money) $coupon_money = 0;
|
||||
return $goods_list;
|
||||
}
|
||||
/****************************************************************************** 订单优惠券 end *****************************************************************************/
|
||||
/****************************************************************************** 积分 start *****************************************************************************/
|
||||
/**
|
||||
* 扣除积分
|
||||
* @return void
|
||||
*/
|
||||
public function usePoint()
|
||||
{
|
||||
if ($this->is_point && $this->point > 0) {
|
||||
$member_account_model = new MemberAccount();
|
||||
$point_result = $member_account_model->addMemberAccount($this->site_id, $this->member_id, AccountDict::point, -$this->point, 'pointcash', $this->order_id, '订单消费扣除');
|
||||
if ($point_result['code'] < 0) {
|
||||
throw new OrderException('积分余额不足');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单最大可用积分
|
||||
* @return true
|
||||
*/
|
||||
public function getMaxUsablePoint()
|
||||
{
|
||||
$point = 0;
|
||||
// 获取积分抵现配置
|
||||
$point_config = $this->config('point');
|
||||
$config = ['is_use' => $point_config['is_use']];
|
||||
$config = array_merge($config, $point_config['value']);
|
||||
|
||||
$order_money = $this->delivery_money > 0 ? $this->order_money - $this->delivery_money : $this->order_money;
|
||||
if ($config['is_use']) {
|
||||
if ($config['is_limit'] == 1 && $order_money < $config['limit']) {
|
||||
$this->max_usable_point = $point;
|
||||
return true;
|
||||
}
|
||||
$deduction_money = $order_money;
|
||||
if ($config['is_limit_use'] == 1) {
|
||||
if ($config['type'] == 0) {
|
||||
$deduction_money = $config['max_use'];
|
||||
} else {
|
||||
$ratio = $config['max_use'] / 100;
|
||||
$deduction_money = round(($order_money * $ratio), 2);
|
||||
}
|
||||
if ($deduction_money > $order_money) {
|
||||
$deduction_money = $order_money;
|
||||
}
|
||||
}
|
||||
$max_point = round($deduction_money * $config['cash_rate']);
|
||||
$point = min($max_point, $this->member_account['point']);
|
||||
}
|
||||
|
||||
$this->max_usable_point = $point;
|
||||
$this->point = $point;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算积分优惠
|
||||
* @return void
|
||||
*/
|
||||
public function calculatePoint()
|
||||
{
|
||||
$config = $this->config('point')['value'] ?? [];
|
||||
if ($this->param['is_point'] && $this->max_usable_point > 0) {
|
||||
$point_money = round(($this->max_usable_point * (1 / $config['cash_rate'])), 2);
|
||||
if ($point_money > $this->order_money) {
|
||||
$point_money = $this->order_money;
|
||||
}
|
||||
$this->is_point = 1;
|
||||
$this->point = $this->max_usable_point;
|
||||
$this->order_money -= $point_money;
|
||||
$this->point_money = $point_money;
|
||||
//计算订单项积分
|
||||
$this->distributionGoodsPoint();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按比例摊派积分
|
||||
* @return true
|
||||
*/
|
||||
public function distributionGoodsPoint()
|
||||
{
|
||||
$temp_point = $this->point;
|
||||
$temp_point_money = $this->point_money;
|
||||
$last_key = count($this->goods_list) - 1;
|
||||
foreach ($this->goods_list as $k => &$v) {
|
||||
if ($last_key != $k) {
|
||||
$use_point = round($v['goods_money'] / $this->goods_money * $this->point);
|
||||
$item_point_money = round($v['goods_money'] / $this->goods_money * $this->point_money, 2);
|
||||
} else {
|
||||
$use_point = $temp_point;
|
||||
$item_point_money = $temp_point_money;
|
||||
}
|
||||
$temp_point -= $use_point;
|
||||
$temp_point_money -= $item_point_money;
|
||||
$v['use_point'] = $use_point;
|
||||
$v['point_money'] = $item_point_money;
|
||||
$real_goods_money = $v['real_goods_money'] - $item_point_money;
|
||||
$real_goods_money = max($real_goods_money, 0);
|
||||
$v['real_goods_money'] = $real_goods_money; //真实订单项金额
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/****************************************************************************** 积分 end *****************************************************************************/
|
||||
/****************************************************************************** 满减 start *****************************************************************************/
|
||||
/**
|
||||
* 满减优惠
|
||||
* @return true
|
||||
*/
|
||||
public function manjianPromotion()
|
||||
{
|
||||
//先查询全部商品的满减套餐 进行中
|
||||
$manjian_model = new Manjian();
|
||||
$all_info = $manjian_model->getManjianInfo([['manjian_type', '=', 1], ['site_id', '=', $this->site_id], ['status', '=', 1]], 'manjian_name,type,goods_ids,rule_json,manjian_id')['data'];
|
||||
$goods_list = $this->goods_list;
|
||||
|
||||
//存在全场满减(不考虑部分满减情况)
|
||||
if (!empty($all_info)) {
|
||||
$discount_array = $this->getManjianDiscountMoney($all_info);
|
||||
$all_info['discount_array'] = $discount_array;
|
||||
$all_info['rule'] = json_decode($all_info['rule_json'], true);
|
||||
//判断有没有优惠
|
||||
$temp_goods_list = $this->distributionGoodsDiscount($goods_list, $this->goods_money, $discount_array['real_discount_money'], isset($discount_array['rule']['free_shipping']));
|
||||
$goods_list = $temp_goods_list;
|
||||
|
||||
$manjian_list[] = $all_info;
|
||||
|
||||
$discount_money = $discount_array['real_discount_money'];
|
||||
$this->goods_list = $goods_list;
|
||||
$this->promotion_money += $discount_money;
|
||||
|
||||
if (!empty($discount_array['rule'])) {
|
||||
$this->manjian_rule_list[] = [
|
||||
'manjian_info' => $all_info,
|
||||
'rule' => $discount_array['rule'],
|
||||
'sku_ids' => ''
|
||||
];
|
||||
$this->promotion['manjian'] = $manjian_list;
|
||||
}
|
||||
} else {
|
||||
$goods_ids = array_unique(array_column($this->goods_list, 'goods_id'));
|
||||
|
||||
$manjian_condition = array(
|
||||
['goods_id', 'in', $goods_ids],
|
||||
['status', '=', 1]
|
||||
);
|
||||
$manjian_goods_list = $manjian_model->getManjianGoodsList($manjian_condition, 'manjian_id')['data'];
|
||||
if (!empty($manjian_goods_list)) {
|
||||
$discount_money = 0;
|
||||
$manjian_goods_list = array_column($manjian_goods_list, 'manjian_id');
|
||||
$manjian_goods_list = array_unique($manjian_goods_list); //去重
|
||||
sort($manjian_goods_list);
|
||||
$manjian_list_result = $manjian_model->getManjianList([['manjian_id', 'in', $manjian_goods_list], ['status', '=', 1]]);
|
||||
$manjian_list = $manjian_list_result['data'];
|
||||
$show_manjian_list = [];
|
||||
foreach ($manjian_list as $k => $v) {
|
||||
$manjian_goods_ids = explode(',', $v['goods_ids']);
|
||||
$item_goods_data = [
|
||||
'goods_money' => 0,
|
||||
'goods_num' => 0
|
||||
];
|
||||
$item_goods_list = [];
|
||||
$sku_ids = [];
|
||||
foreach ($goods_list as $goods_k => $goods_item) {
|
||||
if (in_array($goods_item['goods_id'], $manjian_goods_ids)) {
|
||||
$item_goods_data['goods_money'] += $goods_item['goods_money'];
|
||||
$item_goods_data['goods_num'] += $goods_item['num'];
|
||||
$item_goods_list[] = $goods_item;
|
||||
$sku_ids[] = $goods_item['sku_id'];
|
||||
unset($goods_list[$goods_k]);
|
||||
}
|
||||
}
|
||||
$discount_array = $this->getManjianDiscountMoney($v, $item_goods_list);
|
||||
$temp_goods_list = $this->distributionGoodsDiscount($item_goods_list, $item_goods_data['goods_money'], $discount_array['real_discount_money'], isset($discount_array['rule']['free_shipping']), $sku_ids);
|
||||
$goods_list = array_merge($goods_list, $temp_goods_list);
|
||||
$manjian_list[$k]['rule'] = json_decode($v['rule_json'], true);
|
||||
$manjian_list[$k]['discount_array'] = $discount_array;
|
||||
$discount_money += $discount_array['real_discount_money'];
|
||||
if (!empty($discount_array['rule'])) {
|
||||
$this->manjian_rule_list[] = [
|
||||
'manjian_info' => $v,
|
||||
'rule' => $discount_array['rule'],
|
||||
'sku_ids' => $sku_ids
|
||||
];
|
||||
//只显示符合条件的满减活动
|
||||
$show_manjian_list[] = $v;
|
||||
}
|
||||
}
|
||||
$this->promotion['manjian'] = $show_manjian_list;
|
||||
$this->goods_list = $goods_list;
|
||||
$this->promotion_money += $discount_money;
|
||||
}
|
||||
}
|
||||
if (!empty($this->promotion['manjian'])) {
|
||||
foreach ($this->promotion['manjian'] as &$v) {
|
||||
$discount_array = $v['discount_array'] ?? [];
|
||||
if ($discount_array) {
|
||||
$rule = $discount_array['rule'];
|
||||
if ($rule) {
|
||||
$coupon_ids = $rule['coupon'] ?? '';
|
||||
if ($coupon_ids) {
|
||||
$coupon_ids = explode(',', $coupon_ids);
|
||||
$coupon_num_arr = explode(',', $rule['coupon_num']);
|
||||
$coupon_list = (new Coupon())->getCouponTypeList([['coupon_type_id', 'in', $coupon_ids]])['data'] ?? [];
|
||||
// $coupon_list = array_column($coupon_list, null, 'coupon_type_id');
|
||||
foreach ($coupon_list as &$coupon_v) {
|
||||
$item_coupon_type_id = $coupon_v['coupon_type_id'];
|
||||
$coupon_v['give_num'] = $coupon_num_arr[array_search($item_coupon_type_id, $coupon_ids)] ?? 1;
|
||||
}
|
||||
$v['discount_array']['rule']['coupon_list'] = $coupon_list;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 满减优惠金额
|
||||
* @param $manjian_info
|
||||
* @return array
|
||||
*/
|
||||
public function getManjianDiscountMoney($manjian_info, $promotion_goods = [])
|
||||
{
|
||||
if (empty($promotion_goods)) {
|
||||
$goods_money = $this->goods_money;
|
||||
$value = $manjian_info['type'] == 0 ? $this->goods_money : $this->goods_num;
|
||||
} else {
|
||||
$goods_money = array_sum(array_column($promotion_goods, 'goods_money'));
|
||||
$goods_num = array_sum(array_column($promotion_goods, 'num'));
|
||||
$value = $manjian_info['type'] == 0 ? $goods_money : $goods_num;
|
||||
}
|
||||
//阶梯计算优惠
|
||||
$rule_item = json_decode($manjian_info['rule_json'], true);
|
||||
$discount_money = 0;
|
||||
$money = 0;
|
||||
$rule = []; // 符合条件的优惠规则
|
||||
array_multisort(array_column($rule_item, 'limit'), SORT_ASC, $rule_item); //排序,根据num 排序
|
||||
foreach ($rule_item as $k => $v) {
|
||||
if ($value >= $v['limit']) {
|
||||
$rule = $v;
|
||||
if (isset($v['discount_money'])) {
|
||||
$discount_money = $v['discount_money'];
|
||||
$money = $v['limit'];
|
||||
}
|
||||
}
|
||||
}
|
||||
$real_discount_money = min($discount_money, $goods_money);
|
||||
return ['discount_money' => $discount_money, 'money' => $money, 'real_discount_money' => $real_discount_money, 'rule' => $rule];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理商品满减
|
||||
* @param $goods_list
|
||||
* @param $goods_money
|
||||
* @param $discount_money
|
||||
* @param bool $is_free_shipping
|
||||
* @param array $sku_ids
|
||||
* @return mixed
|
||||
*/
|
||||
public function distributionGoodsDiscount($goods_list, $goods_money, $discount_money, $is_free_shipping = false, $sku_ids = [])
|
||||
{
|
||||
$temp_discount_money = $discount_money;
|
||||
$last_key = count($goods_list) - 1;
|
||||
foreach ($goods_list as $k => $v) {
|
||||
if ($last_key != $k) {
|
||||
$item_discount_money = round($v['goods_money'] / $goods_money * $discount_money, 2);
|
||||
} else {
|
||||
$item_discount_money = $temp_discount_money;
|
||||
}
|
||||
$item_discount_money = min($item_discount_money, $v['real_goods_money']);
|
||||
$temp_discount_money -= $item_discount_money;
|
||||
$goods_list[$k]['promotion_money'] += $item_discount_money;
|
||||
$goods_list[$k]['real_goods_money'] -= $item_discount_money; //真实订单项金额
|
||||
// 满减送包邮
|
||||
if ($is_free_shipping) {
|
||||
if (empty($sku_ids) || in_array($v['sku_id'], $sku_ids)) {
|
||||
$goods_list[$k]['is_free_shipping'] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $goods_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录满减
|
||||
* @return true
|
||||
*/
|
||||
public function createManjian()
|
||||
{
|
||||
if (!empty($this->manjian_rule_list)) {
|
||||
$mansong_data = [];
|
||||
foreach ($this->manjian_rule_list as $item) {
|
||||
// 检测是否有赠送内容
|
||||
if (isset($item['rule']['point']) || isset($item['rule']['coupon'])) {
|
||||
$mansong_data[] = [
|
||||
'manjian_id' => $item['manjian_info']['manjian_id'],
|
||||
'site_id' => $this->site_id,
|
||||
'manjian_name' => $item['manjian_info']['manjian_name'],
|
||||
'point' => isset($item['rule']['point']) ? round($item['rule']['point']) : 0,
|
||||
'coupon' => $item['rule']['coupon'] ?? 0,
|
||||
'coupon_num' => $item['rule']['coupon_num'] ?? '',
|
||||
'order_id' => $this->order_id,
|
||||
'member_id' => $this->member_id,
|
||||
'order_sku_ids' => !empty($item['sku_ids']) ? implode($item['sku_ids']) : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
if (!empty($mansong_data)) {
|
||||
model('promotion_mansong_record')->addList($mansong_data);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 满减 end *****************************************************************************/
|
||||
|
||||
|
||||
/****************************************************************************** 商品次卡 start *****************************************************************************/
|
||||
/**
|
||||
* 查询商品可用次卡
|
||||
* @return true
|
||||
*/
|
||||
public function getMemberGoodsCardPromotion()
|
||||
{
|
||||
if (addon_is_exit('cardservice', $this->site_id)) {
|
||||
$member_card = new \addon\cardservice\model\MemberCard();
|
||||
$common_card = [];
|
||||
foreach ($this->goods_list as &$goods_item) {
|
||||
$sku_id = $goods_item['sku_id'];
|
||||
$condition = [
|
||||
['mgci.member_id', '=', $this->member_id],
|
||||
['mgci.sku_id', '=', $sku_id],
|
||||
['mgc.status', '=', 1],
|
||||
['', 'exp', Db::raw("( (mgc.card_type = 'timercard') OR (mgc.card_type = 'oncecard' AND mgci.num > mgci.use_num) OR (mgc.card_type = 'commoncard' AND mgc.total_num > mgc.total_use_num) )")]
|
||||
];
|
||||
$card_ids = array_filter(array_map(function ($item) {
|
||||
if ($item['total_use_num'] >= $item['total_num']) return $item['card_id'];
|
||||
}, $common_card));
|
||||
if (!empty($card_ids)) $condition[] = ['mgci.card_id', 'not in', $card_ids];
|
||||
|
||||
// 查询可用的卡项
|
||||
$card_list = $member_card->getCartItemList($condition, 'mgci.item_id,mgci.card_id,mgci.num,mgci.use_num,mgci.member_verify_id,mgc.end_time,mgc.total_num,mgc.total_use_num,mgc.card_type,mgc.goods_name', '', 'mgci', [
|
||||
['member_goods_card mgc', 'mgc.card_id = mgci.card_id', 'inner'],
|
||||
])['data'];
|
||||
if (!empty($card_list)) {
|
||||
$card_item_id = $this->param['member_goods_card'] && isset($this->param['member_goods_card'][$sku_id]) ? $this->param['member_goods_card'][$sku_id] : 0;
|
||||
$card_list = array_column($card_list, null, 'item_id');
|
||||
// 抵扣判断
|
||||
if (isset($card_list[$card_item_id])) {
|
||||
$card_item = $card_list[$card_item_id];
|
||||
$card_id = $card_item['card_id'];
|
||||
if ($card_item['card_type'] == 'commoncard') {
|
||||
if (isset($common_card[$card_id])) {
|
||||
$card_item['num'] = $common_card[$card_id]['total_num'] - $common_card[$card_id]['total_use_num'];
|
||||
} else {
|
||||
$card_item['num'] = $card_item['total_num'] - $card_item['total_use_num'];
|
||||
}
|
||||
} else if ($card_item['card_type'] == 'timecard') {
|
||||
$card_item['num'] = $goods_item['num'];
|
||||
} else {
|
||||
$card_item['num'] -= $card_item['use_num'];
|
||||
}
|
||||
$num = min($card_item['num'], $goods_item['num']);
|
||||
$promotion_money = round($goods_item['price'] * $num, 2);
|
||||
//定义商品项的属性
|
||||
$goods_item['promotion_money'] += $promotion_money;
|
||||
$goods_item['card_promotion_money'] = $promotion_money;
|
||||
$goods_item['real_goods_money'] = round($goods_item['real_goods_money'] - $promotion_money, 2);
|
||||
$goods_item['card_use_num'] = $num;
|
||||
// 针对通卡进行处理
|
||||
if ($card_item['card_type'] == 'commoncard') {
|
||||
if (isset($common_card[$card_id])) {
|
||||
$common_card[$card_id]['total_use_num'] += $num;
|
||||
} else {
|
||||
$common_card[$card_id] = [
|
||||
'card_id' => $card_id,
|
||||
'total_num' => $card_item['total_num'],
|
||||
'total_use_num' => $card_item['total_use_num'] + $num
|
||||
];
|
||||
}
|
||||
}
|
||||
$this->promotion_money += $promotion_money;
|
||||
} else {
|
||||
unset($this->param['member_goods_card'][$sku_id]);
|
||||
}
|
||||
$goods_item['member_card_list'] = $card_list;
|
||||
}
|
||||
}
|
||||
//使用的会员卡
|
||||
$this->member_goods_card = $this->param['member_goods_card'] ?? [];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用次卡
|
||||
* @return true
|
||||
*/
|
||||
public function useCard()
|
||||
{
|
||||
$this->getOrderGoodsList();
|
||||
foreach ($this->order_goods_list as $k => $v) {
|
||||
// 使用次卡
|
||||
if ($v['card_item_id']) {
|
||||
$card_use_res = (new \addon\cardservice\model\MemberCard())->cardUse([
|
||||
'item_id' => $v['card_item_id'],
|
||||
'num' => $this->goods_list[$k]['card_use_num'],
|
||||
'type' => 'order',
|
||||
'relation_id' => $v['order_goods_id'],
|
||||
'store_id' => $this->store_id
|
||||
]);
|
||||
if ($card_use_res['code'] != 0) throw new OrderException($card_use_res['message']);
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取会员卡商品价格
|
||||
* @param $goods_sku_info
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCardGoodsPrice($goods_sku_info)
|
||||
{
|
||||
$res = [
|
||||
'discount_price' => 0, // 折扣价(默认等于单价)
|
||||
'member_price' => 0, // 会员价
|
||||
'price' => 0 // 最低价格
|
||||
];
|
||||
$res['discount_price'] = $goods_sku_info['discount_price'];
|
||||
$res['price'] = $goods_sku_info['discount_price'];
|
||||
if (!addon_is_exit('memberprice') || empty($this->recommend_member_card)) return $this->success($res);
|
||||
$level_id = $this->recommend_member_card['level_id'];
|
||||
if ($goods_sku_info['is_consume_discount']) {
|
||||
if ($goods_sku_info['discount_config'] == 1) {
|
||||
// 自定义优惠
|
||||
$goods_sku_info['member_price'] = json_decode($goods_sku_info['member_price'], true);
|
||||
$value = $goods_sku_info['member_price'][$goods_sku_info['discount_method']][$level_id] ?? 0;
|
||||
switch ($goods_sku_info['discount_method']) {
|
||||
case 'discount':
|
||||
// 打折
|
||||
if ($value == 0) {
|
||||
$res['member_price'] = $goods_sku_info['price'];
|
||||
} else {
|
||||
$res['member_price'] = number_format($goods_sku_info['price'] * $value / 10, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case 'manjian':
|
||||
if ($value == 0) {
|
||||
$res['member_price'] = $goods_sku_info['price'];
|
||||
} else {
|
||||
// 满减
|
||||
$res['member_price'] = number_format($goods_sku_info['price'] - $value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
case 'fixed_price':
|
||||
if ($value == 0) {
|
||||
$res['member_price'] = $goods_sku_info['price'];
|
||||
} else {
|
||||
// 指定价格
|
||||
$res['member_price'] = number_format($value, 2, '.', '');
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// 默认按会员享受折扣计算
|
||||
$res['member_price'] = number_format($goods_sku_info['price'] * $this->recommend_member_card['consume_discount'] / 100, 2, '.', '');
|
||||
}
|
||||
if ($res['member_price'] < $res['price']) {
|
||||
$res['price'] = $res['member_price'];
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
/****************************************************************************** 商品次卡 end *****************************************************************************/
|
||||
|
||||
|
||||
/****************************************************************************** 超级会员卡 end *****************************************************************************/
|
||||
|
||||
/**
|
||||
* 获取推荐会员卡
|
||||
* @return true
|
||||
*/
|
||||
public function getRecommendMemberCard()
|
||||
{
|
||||
if (!empty($this->member_account)) {
|
||||
if (addon_is_exit('supermember', $this->site_id)) {
|
||||
if (!$this->member_account['member_level_type']) {
|
||||
$store_id = $this->param['store_id'] ?? 0;
|
||||
//todo 门店线上不参与推荐会员卡关联购买
|
||||
// if (addon_is_exit('store') && $store_id > 0) {
|
||||
$member_card_model = new MemberCard();
|
||||
$recommend_member_card = $member_card_model->getRecommendMemberCard($this->site_id)['data'] ?? [];
|
||||
if (!empty($recommend_member_card)) {
|
||||
$recommend_member_card['discount_money'] = 0;
|
||||
$recommend_member_card['charge_rule'] = json_decode($recommend_member_card['charge_rule'], true);
|
||||
$this->recommend_member_card = $recommend_member_card;
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
//是否使用推荐会员卡
|
||||
$this->recommend_member_card_data['is_open_card'] = $this->param['is_open_card'] ?? 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算会员卡开卡金额
|
||||
* @return true
|
||||
*/
|
||||
public function calculateMemberCardMoney()
|
||||
{
|
||||
$money = 0;
|
||||
$is_open_card = $this->recommend_member_card_data['is_open_card'] ?? 0;
|
||||
if (!empty($this->recommend_member_card) && $is_open_card) {
|
||||
$charge_rule = $this->recommend_member_card['charge_rule'];
|
||||
$member_card_unit = $this->param['member_card_unit'];
|
||||
$this->member_card_money = $charge_rule[$member_card_unit] ?? 0;
|
||||
$this->recommend_member_card_data['member_card_unit'] = $member_card_unit;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步创建会员卡订单
|
||||
* @return true
|
||||
*/
|
||||
public function createMemberCard()
|
||||
{
|
||||
if (!empty($this->recommend_member_card) && $this->recommend_member_card_data['is_open_card']) {
|
||||
$member_level_order = new MemberLevelOrder();
|
||||
$member_card_unit = $this->recommend_member_card_data['member_card_unit'];
|
||||
$level_order_result = $member_level_order->create(
|
||||
['out_trade_no' => $this->out_trade_no,
|
||||
'member_id' => $this->member_id,
|
||||
'site_id' => $this->site_id,
|
||||
'level_id' => $this->recommend_member_card['level_id'],
|
||||
'period_unit' => $member_card_unit
|
||||
]
|
||||
);
|
||||
if ($level_order_result['code'] < 0) throw new OrderException($level_order_result['message']);
|
||||
$level_order = $level_order_result['data'];
|
||||
model('order')->update(['member_card_order' => $level_order['order_id']], [['order_id', '=', $this->order_id]]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/****************************************************************************** 超级会员卡 end *****************************************************************************/
|
||||
}
|
||||
163
app/model/order/orderrefund/ActiveRefund.php
Executable file
163
app/model/order/orderrefund/ActiveRefund.php
Executable file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\orderrefund;
|
||||
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
use app\model\order\OrderRefund;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 商家主动退款
|
||||
*/
|
||||
class ActiveRefund extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $param
|
||||
* @return mixed
|
||||
*/
|
||||
public static function check($param)
|
||||
{
|
||||
$instance = new self;
|
||||
$order_goods_id = $param[ 'order_goods_id' ];
|
||||
$shop_active_refund_money_type = $param[ 'shop_active_refund_money_type' ];
|
||||
$shop_active_refund_money = $param[ 'shop_active_refund_money' ];
|
||||
|
||||
//退款方式
|
||||
$refund_money_type_arr = OrderRefundDict::getRefundMoneyType();
|
||||
if(!isset($refund_money_type_arr[$shop_active_refund_money_type])){
|
||||
return $instance->error(null, '退款方式有误!');
|
||||
}
|
||||
|
||||
//订单项检测
|
||||
$order_goods_info = model('order_goods')->getInfo([ 'order_goods_id' => $order_goods_id ]);
|
||||
if (empty($order_goods_info)) return $instance->error(null, '订单项不存在!');
|
||||
if($order_goods_info['shop_active_refund'] == 1) return $instance->error(null, '已操作过主动退款');
|
||||
if ($order_goods_info[ 'refund_status' ] != OrderRefundDict::REFUND_NOT_APPLY &&
|
||||
$order_goods_info[ 'refund_status' ] != OrderRefundDict::REFUND_DIEAGREE) {
|
||||
return $instance->error(null, '存在进行中的退款!');
|
||||
}
|
||||
|
||||
//订单检测
|
||||
$order_id = $order_goods_info[ 'order_id' ];
|
||||
$order_info = model('order')->getInfo([ 'order_id' => $order_id ]);
|
||||
if (empty($order_info)) return $instance->error([], '订单不存在!');
|
||||
if ($order_info[ 'is_enable_refund' ] == 0) {
|
||||
if ($order_info[ 'promotion_type' ] == 'pinfan') {
|
||||
return $instance->error(null, '拼团活动正在进行中,待拼成功后可发起退款!');
|
||||
}
|
||||
return $instance->error(null, '当前订单不允许退款!');
|
||||
}
|
||||
|
||||
//退款金额检测
|
||||
$order_refund_model = new OrderRefund();
|
||||
$refund_apply_money_array = $order_refund_model->getOrderRefundMoney($order_goods_id);//可退款金额 通过计算获得
|
||||
$refund_apply_money = $refund_apply_money_array[ 'refund_money' ];
|
||||
if($shop_active_refund_money < 0){
|
||||
return $instance->error(null, '主动退款金额不可小于0!');
|
||||
}
|
||||
if ($shop_active_refund_money > $refund_apply_money){
|
||||
return $instance->error(null, '主动退款金额不能大于可退款总额!');
|
||||
}
|
||||
if($refund_apply_money > 0 && $shop_active_refund_money == 0){
|
||||
return $instance->error(null, '主动退款金额不可为0!');
|
||||
}
|
||||
|
||||
//退货数量检测
|
||||
$param['is_refund_stock'] = $param['is_refund_stock'] ?? 0;
|
||||
$param['refund_stock_num'] = $param['refund_stock_num'] ?? $order_goods_info['num'];
|
||||
if($param['is_refund_stock'] == 1){
|
||||
if($param['refund_stock_num'] > $order_goods_info['num']){
|
||||
return $instance->error(null, '主动退货数量不可大于订单数量!');
|
||||
}
|
||||
$order_goods_info['refund_stock_num'] = $param['refund_stock_num'];
|
||||
}
|
||||
|
||||
return $instance->success([
|
||||
'order_goods_info' => $order_goods_info,
|
||||
'order_info' => $order_info,
|
||||
'refund_apply_money' => $refund_apply_money,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行事件
|
||||
* @param $param
|
||||
* @return true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function event($param)
|
||||
{
|
||||
$order_goods_info = $param['order_goods_info'];
|
||||
// if($order_goods_info['refund_status'] == OrderRefundDict::REFUND_COMPLETE){
|
||||
// $order_refund_model = new OrderRefund();
|
||||
// $order_refund_model->orderGoodsRefund($order_goods_info);
|
||||
// }
|
||||
|
||||
//返还库存
|
||||
$param['is_refund_stock'] = $param['is_refund_stock'] ?? 0;
|
||||
$param['refund_stock_num'] = isset($param['refund_stock_num']) && $param['refund_stock_num']>0 ? $param['refund_stock_num'] : $order_goods_info['num'];
|
||||
$order_refund_model = new OrderRefund();
|
||||
$order_goods_info['num'] = $param['refund_stock_num'] ?:$order_goods_info['num'];
|
||||
$order_refund_model->orderGoodsRefund($order_goods_info);
|
||||
|
||||
|
||||
//更新退款金额
|
||||
$order_info = $param['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
$refund_total_real_money = model('order_goods')->getSum([ [ 'order_id', '=', $order_id ], [ 'refund_status', '=', OrderRefundDict::REFUND_COMPLETE ] ], 'refund_real_money');
|
||||
$refund_total_real_money += model('order_goods')->getSum([ [ 'order_id', '=', $order_id ], [ 'shop_active_refund', '=', 1 ] ], 'shop_active_refund_money');
|
||||
model('order')->update(['refund_money' => $refund_total_real_money], [['order_id', '=', $order_id]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $param
|
||||
* @return array|true
|
||||
*/
|
||||
public static function after($param)
|
||||
{
|
||||
$shop_active_refund_money = $param[ 'shop_active_refund_money' ];
|
||||
$shop_active_refund_remark = $param[ 'shop_active_refund_remark' ];
|
||||
$user_info = $param['user_info'];
|
||||
$order_goods_info = $param['order_goods_info'];
|
||||
$order_info = $param['order_info'];
|
||||
|
||||
//记录订单日志
|
||||
$log_data = [
|
||||
'uid' => $user_info['uid'],
|
||||
'nick_name' => $user_info['username'],
|
||||
'action' => '商品【'.$order_goods_info['sku_name'].'】商家主动退款,退款金额:'.$shop_active_refund_money.'元,退款说明:'.$shop_active_refund_remark,
|
||||
'action_way' => 2,
|
||||
'order_id' => $order_goods_info['order_id'],
|
||||
'order_status' => $order_info['order_status'],
|
||||
'order_status_name' => $order_info['order_status_name']
|
||||
];
|
||||
$order_common_model = new OrderCommon();
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
|
||||
//记录退款日志
|
||||
$order_refund_model = new OrderRefund();
|
||||
$order_refund_model->addOrderRefundLog(
|
||||
$order_goods_info['order_goods_id'],
|
||||
$order_goods_info['refund_status'],
|
||||
'商家主动退款',
|
||||
2,
|
||||
$user_info['uid'],
|
||||
$user_info['username'],
|
||||
'退款金额:'.$shop_active_refund_money.'元,退款说明:'.$shop_active_refund_remark,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
95
app/model/order/orderrefund/Apply.php
Executable file
95
app/model/order/orderrefund/Apply.php
Executable file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\orderrefund;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Message;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
use app\model\order\OrderRefund;
|
||||
use extend\exception\RefundException;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 订单申请退款
|
||||
*/
|
||||
class Apply extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array|true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function check($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
|
||||
//判断是否允许申请退款
|
||||
if ($order_info['is_enable_refund'] == 0) {
|
||||
if ($order_info['promotion_type'] == 'pinfan' || $order_info['promotion_type'] == 'pintuan') throw new RefundException('拼团活动正在进行中,拼团成功后可再次发起退款!');
|
||||
|
||||
throw new RefundException('当前订单不支持退款!');
|
||||
}
|
||||
//订单申请退款校验
|
||||
event('OrderRefundApplyCheck', []);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款执行事件
|
||||
* @param $data
|
||||
* @return true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function event($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_refund_model = new OrderRefund();
|
||||
//校验订单是否需要锁定
|
||||
$order_refund_model->verifyOrderLock($order_info['order_id']);
|
||||
event('orderRefundApply', $data);//传入订单类型以及订单项id
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array|true
|
||||
*/
|
||||
public static function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
$log_data = $data['log_data'] ?? [];
|
||||
$refund_log_data = $data['refund_log_data'] ?? [];
|
||||
$order_refund_model = new OrderRefund();
|
||||
$order_refund_model->addOrderRefundLog(...$refund_log_data);
|
||||
|
||||
// 发起维权 关闭订单评价
|
||||
model('order')->update(['is_evaluate' => 0], ['order_id' => $order_goods_info['order_id']]);
|
||||
//记录订单日志 start
|
||||
if ($log_data) {
|
||||
$order_common_model = new OrderCommon();
|
||||
$log_data['action'] = '商品【'.$order_goods_info['sku_name'].'】'.$log_data['action'];
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_goods_info['order_id'],
|
||||
'order_status' => $order_info['order_status'],
|
||||
'order_status_name' => $order_info['order_status_name']
|
||||
]);
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
}
|
||||
//订单会员申请退款消息
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'BUYER_REFUND', 'order_goods_id' => $order_goods_info['order_goods_id'], 'site_id' => $order_goods_info['site_id']]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
87
app/model/order/orderrefund/Cancel.php
Executable file
87
app/model/order/orderrefund/Cancel.php
Executable file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\orderrefund;
|
||||
|
||||
use app\dict\order\OrderDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\Order as OrderModel;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
use app\model\order\OrderRefund;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 订单取消退款
|
||||
*/
|
||||
class Cancel extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array|true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function check($data)
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款执行事件
|
||||
* @param $data
|
||||
* @return true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function event($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_refund_model = new OrderRefund();
|
||||
//订单锁定
|
||||
$order_refund_model->verifyOrderLock($order_info['order_id']);
|
||||
// 维权拒绝 评价锁定放开
|
||||
if ($order_info['evaluate_status'] != OrderDict::evaluate_again) {
|
||||
model('order')->update(['is_evaluate' => 1], [['order_id', '=', $order_info['order_id']], ['order_status', 'in', [OrderModel::ORDER_TAKE_DELIVERY, OrderModel::ORDER_COMPLETE]]]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array|true
|
||||
*/
|
||||
public static function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$order_id = $order_info['order_id'];
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
$log_data = $data['log_data'] ?? [];
|
||||
$member_info = $data['member_info'];
|
||||
$order_refund_model = new OrderRefund();
|
||||
//记录订单日志 start
|
||||
if ($log_data) {
|
||||
$order_common_model = new OrderCommon();
|
||||
// $order_info = model('order')->getInfo(['order_id' => $order_id], 'order_status,order_status_name,member_id,site_id,is_video_number');
|
||||
$log_data['action'] = '商品【'.$order_goods_info['sku_name'].'】'.$log_data['action'];
|
||||
$log_data = array_merge($log_data, [
|
||||
'order_id' => $order_id,
|
||||
'order_status' => $order_info['order_status'],
|
||||
'order_status_name' => $order_info['order_status_name']
|
||||
]);
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
}
|
||||
//记录退款日志 end
|
||||
$order_refund_model->addOrderRefundLog($order_goods_info['order_goods_id'], 0, '买家撤销退款申请', 1, $member_info['member_id'], $member_info['nickname']);
|
||||
event('MemberCancelRefund', $data);//传入订单类型以及订单项id
|
||||
return true;
|
||||
}
|
||||
}
|
||||
86
app/model/order/orderrefund/Close.php
Executable file
86
app/model/order/orderrefund/Close.php
Executable file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\orderrefund;
|
||||
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
use app\model\order\OrderRefund;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 订单关闭退款
|
||||
*/
|
||||
class Close extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array|true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function check($data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款执行事件
|
||||
* @param $data
|
||||
* @return true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function event($data)
|
||||
{
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
$order_refund_model = new OrderRefund();
|
||||
$order_refund_model->verifyOrderLock($order_goods_info['order_id']);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array|true
|
||||
*/
|
||||
public static function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$user_info = $data['user_info'];
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
$order_common_model = new OrderCommon();
|
||||
//记录订单日志 start
|
||||
$log_data = [
|
||||
'uid' => $user_info['uid'],
|
||||
'nick_name' => $user_info['username'],
|
||||
'action' => '商家关闭了维权',
|
||||
'action_way' => 2,
|
||||
'order_id' => $order_goods_info['order_id'],
|
||||
'order_status' => $order_info['order_status'],
|
||||
'order_status_name' => $order_info['order_status_name']
|
||||
];
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
//记录订单日志 end
|
||||
//退款日志
|
||||
$order_refund_model = new OrderRefund();
|
||||
$order_refund_model->addOrderRefundLog(
|
||||
$order_goods_info['order_goods_id'],
|
||||
OrderRefundDict::REFUND_NOT_APPLY,
|
||||
'卖家关闭本次维权',
|
||||
2,
|
||||
$user_info['uid'],
|
||||
$user_info['username']
|
||||
);
|
||||
event('MemberCancelRefund', $data);//传入订单类型以及订单项id
|
||||
return true;
|
||||
}
|
||||
}
|
||||
81
app/model/order/orderrefund/Confirm.php
Executable file
81
app/model/order/orderrefund/Confirm.php
Executable file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\orderrefund;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Message;
|
||||
use app\model\order\OrderCommon;
|
||||
use app\model\order\OrderLog;
|
||||
use app\model\order\OrderRefund;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 订单审核退款
|
||||
*/
|
||||
class Confirm extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array|true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function check($data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款执行事件
|
||||
* @param $data
|
||||
* @return true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function event($data)
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array|true
|
||||
*/
|
||||
public static function after($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$user_info = $data['user_info'];
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
// $log_data = $data['log_data'] ?? [];
|
||||
$refund_status = $data['refund_status'];
|
||||
$order_common_model = new OrderCommon();
|
||||
$log_data = [
|
||||
'uid' => $user_info['uid'],
|
||||
'nick_name' => $user_info['username'],
|
||||
'action' => '商品【'.$order_goods_info['sku_name'].'】商家同意了退款申请,等待转账',
|
||||
'action_way' => 2,
|
||||
'order_id' => $order_goods_info['order_id'],
|
||||
'order_status' => $order_info['order_status'],
|
||||
'order_status_name' => $order_info['order_status_name']
|
||||
];
|
||||
OrderLog::addOrderLog($log_data, $order_common_model);
|
||||
$order_refund_model = new OrderRefund();
|
||||
//记录订单日志 end
|
||||
$order_refund_model->addOrderRefundLog($order_goods_info['order_goods_id'], $refund_status, '卖家确认退款', 2, $user_info['uid'], $user_info['username']);
|
||||
//订单退款同意消息
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'ORDER_REFUND_AGREE', 'order_id' => $order_goods_info['order_id'], 'order_goods_id' => $order_goods_info['order_goods_id'], 'site_id' => $order_goods_info['site_id']]);
|
||||
//后续事件
|
||||
event('OrderRefundConfirmAfter', $data);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
82
app/model/order/orderrefund/Delivery.php
Executable file
82
app/model/order/orderrefund/Delivery.php
Executable file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\orderrefund;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\message\Message;
|
||||
use app\model\order\OrderRefund;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 买家退货
|
||||
*/
|
||||
class Delivery extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array|true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function check($data)
|
||||
{
|
||||
$order_info = $data['order_info'];
|
||||
$member_info = $data['member_info'];
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
|
||||
|
||||
event('OrderRefundDeliveryCheck', []);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款执行事件
|
||||
* @param $data
|
||||
* @return true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function event($data)
|
||||
{
|
||||
$order_info = $data['order_info'] ?? [];
|
||||
$member_info = $data['member_info'] ?? [];
|
||||
$order_goods_info = $data['order_goods_info'] ?? [];
|
||||
// $order_refund_model = new OrderRefund();
|
||||
|
||||
event('orderRefundDelivery', $data);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array|true
|
||||
*/
|
||||
public static function after($data)
|
||||
{
|
||||
// $order_info = $data['order_info'];
|
||||
$member_info = $data['member_info'];
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
// $log_data = $data['log_data'] ?? [];
|
||||
$order_refund_model = new OrderRefund();
|
||||
|
||||
$order_refund_model->addOrderRefundLog(
|
||||
$order_goods_info['order_goods_id'],
|
||||
$data['refund_status'],
|
||||
$data['refund_delivery_name'] . ':' . $data['refund_delivery_no'],
|
||||
1,
|
||||
$member_info['member_id'],
|
||||
$member_info['nickname']);
|
||||
//买家已退货提醒
|
||||
$message_model = new Message();
|
||||
$message_model->sendMessage(['keywords' => 'BUYER_DELIVERY_REFUND', 'order_goods_info' => $order_goods_info, 'site_id' => $order_goods_info['site_id']]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
88
app/model/order/orderrefund/Finish.php
Executable file
88
app/model/order/orderrefund/Finish.php
Executable file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace app\model\order\orderrefund;
|
||||
|
||||
use app\dict\order_refund\OrderRefundDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\order\OrderRefund;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
/**
|
||||
* 订单退款完成
|
||||
*/
|
||||
class Finish extends BaseModel
|
||||
{
|
||||
/**
|
||||
* 校验
|
||||
* @param $data
|
||||
* @return array|true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function check($data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款执行事件
|
||||
* @param $data
|
||||
* @return true
|
||||
* @throws DbException
|
||||
*/
|
||||
public static function event($data)
|
||||
{
|
||||
$user_info = $data['user_info'] ?? [];
|
||||
$order_goods_info = $data['order_goods_info'] ?? [];
|
||||
$order_goods_id = $order_goods_info['order_goods_id'];
|
||||
$order_info = $data['order_info'] ?? [];
|
||||
$order_refund_model = new OrderRefund();
|
||||
//订单项退款后各种订单操作
|
||||
$order_refund_model->orderGoodsRefund($order_goods_info);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后续事件
|
||||
* @param $data
|
||||
* @return array|true
|
||||
*/
|
||||
public static function after($data)
|
||||
{
|
||||
$user_info = $data['user_info'];
|
||||
$order_goods_info = $data['order_goods_info'];
|
||||
$order_goods_id = $order_goods_info['order_goods_id'];
|
||||
$order_info = $data['order_info'];
|
||||
$member_id = $order_info['member_id'];
|
||||
|
||||
$refund_real_money = $order_goods_info['refund_real_money'];
|
||||
$order_refund_model = new OrderRefund();
|
||||
//累加会员销售额
|
||||
model('member')->setDec(
|
||||
[['member_id', '=', $member_id]],
|
||||
'order_money',
|
||||
$refund_real_money
|
||||
);
|
||||
//退货日志
|
||||
$order_refund_model->addOrderRefundLog(
|
||||
$order_goods_id,
|
||||
OrderRefundDict::REFUND_COMPLETE,
|
||||
'维权完成',
|
||||
2,
|
||||
$user_info['uid'],
|
||||
$user_info['username'],
|
||||
'维权完成,退款金额:¥' . $refund_real_money
|
||||
);
|
||||
|
||||
//后续事件
|
||||
event('OrderRefundFinishAfter', $data);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user