From 4da5dc858a6022c7272e3fa02d061eebf33bd74d Mon Sep 17 00:00:00 2001 From: xucong <850806214@qq.com> Date: Wed, 7 May 2025 12:10:19 +0800 Subject: [PATCH] =?UTF-8?q?1.=E7=BC=BA=E5=A4=B1=E4=BF=A1=E6=81=AF=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/cache/TokenCache.php | 57 ++ app/api/controller/AccountController.php | 6 +- app/api/logic/DistributionLogic.php | 642 ++++++++++++++++++ app/api/logic/LoginLogic.php | 176 ++++- app/api/service/UserServer.php | 246 +++++++ app/common.php | 76 ++- app/common/logic/AccountLogLogic.php | 59 ++ app/common/logic/DistributionLogic.php | 60 ++ app/common/logic/UserLevelLogic.php | 64 ++ app/common/model/Client_.php | 70 ++ app/common/model/account/AccountLog.php | 170 +++++ .../model/distribution/Distribution.php | 16 + .../model/distribution/DistributionLevel.php | 64 ++ .../model/noticesetting/NoticeSetting.php | 269 ++++++++ app/common/model/user/User.php | 13 +- app/common/service/WeChatServer.php | 190 ++++++ .../framework/src/think/facade/Hook.php | 37 + 17 files changed, 2209 insertions(+), 6 deletions(-) create mode 100644 app/api/cache/TokenCache.php create mode 100644 app/api/logic/DistributionLogic.php create mode 100644 app/api/service/UserServer.php create mode 100644 app/common/logic/DistributionLogic.php create mode 100644 app/common/logic/UserLevelLogic.php create mode 100644 app/common/model/Client_.php create mode 100644 app/common/model/account/AccountLog.php create mode 100644 app/common/model/distribution/Distribution.php create mode 100644 app/common/model/distribution/DistributionLevel.php create mode 100644 app/common/model/noticesetting/NoticeSetting.php create mode 100644 app/common/service/WeChatServer.php create mode 100644 vendor/topthink/framework/src/think/facade/Hook.php diff --git a/app/api/cache/TokenCache.php b/app/api/cache/TokenCache.php new file mode 100644 index 0000000..1f64d1f --- /dev/null +++ b/app/api/cache/TokenCache.php @@ -0,0 +1,57 @@ +where(['token' => $this->extend['token']]) + ->update(['update_time' => $time, 'expire_time' => $expire_time]); + + //返回用户信息 + $user_info = Db::name('user u') + ->join('session s', 'u.id=s.user_id') + ->where(['s.token' => $this->extend['token']]) + ->field('u.*,s.token,s.client') + ->find(); + return $user_info; + } +} \ No newline at end of file diff --git a/app/api/controller/AccountController.php b/app/api/controller/AccountController.php index 53848da..578ea22 100644 --- a/app/api/controller/AccountController.php +++ b/app/api/controller/AccountController.php @@ -21,7 +21,7 @@ namespace app\api\controller; use app\api\logic\LoginLogic; use app\api\validate\WechatLoginValidate; -use app\common\server\ConfigServer; +use app\common\service\ConfigServer; class AccountController extends BaseApiController { @@ -250,10 +250,10 @@ class AccountController extends BaseApiController { $post = $this->request->post(); if (empty($post['code']) || empty($post['nickname'])) { - $this->_error('参数缺失'); + return $this->fail('参数缺失'); } $data = LoginLogic::authLogin($post); - $this->_success($data['msg'], $data['data'], $data['code'], $data['show']); + return $this->success("登录成功",$data); } /** diff --git a/app/api/logic/DistributionLogic.php b/app/api/logic/DistributionLogic.php new file mode 100644 index 0000000..4bc6861 --- /dev/null +++ b/app/api/logic/DistributionLogic.php @@ -0,0 +1,642 @@ +field(['id', 'first_leader', 'second_leader', 'third_leader', 'ancestor_relation']) + ->where(['distribution_code' => $code]) + ->find(); + + //更新我的第一上级、第二上级、第三上级、关系链 + $my_leader_id = $my_leader['id']; + $my_first_leader = $my_leader['first_leader']; + $my_third_leader = $my_leader['second_leader']; + $my_leader['ancestor_relation'] = boolval($my_leader['ancestor_relation']) ? $my_leader['ancestor_relation'] : ''; + $my_ancestor_relation = trim("{$my_leader_id},{$my_leader['ancestor_relation']}", ','); + + $user = User::findOrEmpty($my_id); + // 旧关系链 + if (!empty($user->ancestor_relation)) { + $old_ancestor_relation = $user->id . ',' .$user->ancestor_relation; + } else { + $old_ancestor_relation = $user->id; + } + + $data = [ + 'first_leader' => $my_leader_id, + 'second_leader' => $my_first_leader, + 'third_leader' => $my_third_leader, + 'ancestor_relation' => $my_ancestor_relation, + ]; + Db::name('user') + ->where(['id' => $my_id]) + ->update($data); + + //更新我向下一级的第二上级、第三上级 + $data = [ + 'second_leader' => $my_leader_id, + 'third_leader' => $my_first_leader, + ]; + Db::name('user') + ->where(['first_leader' => $my_id]) + ->update($data); + + //更新我向下二级的第三级 + $data = [ + 'third_leader' => $my_leader_id, + ]; + Db::name('user') + ->where(['second_leader' => $my_id]) + ->update($data); + + + //更新与我相关的所有关系链 + Db::name('user') + ->where("find_in_set({$my_id},ancestor_relation)") + ->exp('ancestor_relation', "replace(ancestor_relation,'{$old_ancestor_relation}','" . trim("{$my_id},{$my_ancestor_relation}", ',') . "')") + ->update(); + + //邀请会员赠送积分 + $invited_award_integral = ConfigServer::get('marketing','invited_award_integral',0); + if($invited_award_integral > 0){ + Db::name('user')->where(['id'=>$my_leader['id']])->setInc('user_integral',$invited_award_integral); + AccountLogLogic::AccountRecord($my_leader['id'],$invited_award_integral,1, AccountLog::invite_add_integral); + } + + //消息通知 + Hook::listen('notice', [ + 'user_id' => $my_leader_id, + 'lower_id' => $my_id, + 'scene' => NoticeSetting::INVITE_SUCCESS_NOTICE, + ]); + + Db::commit(); + return true; + } catch (Exception $e) { + Db::rollback(); + return $e->getMessage(); + } + + } + + + /** + * 填写申请记录 + * @param $post + * @param $user_id + * @return bool|string + */ + public static function apple($post, $user_id) + { + try { + $time = time(); + $data = [ + 'user_id' => $user_id, + 'real_name' => $post['real_name'], + 'province' => $post['province'], + 'city' => $post['city'], + 'district' => $post['district'], + 'reason' => $post['reason'], + 'create_time' => $time, + 'update_time' => $time, + ]; + Db::name('distribution_member_apply') + ->insert($data); + return true; + } catch (Exception $e) { + return $e->getMessage(); + } + } + + /** + * 获取最新申请详情 + * @param $user_id + * @return array|\PDOStatement|string|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function appleDetail($user_id) + { + $result = Db::name('distribution_member_apply') + ->field(['real_name', 'province', 'city', 'district', 'reason', 'status']) + ->where('user_id', $user_id) + ->order('id', 'desc') + ->find(); + + if (empty($result)) { + return []; + } + + $result['province'] = AreaServer::getAddress($result['province']); + $result['city'] = AreaServer::getAddress($result['city']); + $result['district'] = AreaServer::getAddress($result['district']); + switch ($result['status']) { + case 0: + $result['status_str'] = '已提交,等待客服审核...'; + break; + case 1: + $result['status_str'] = ''; + break; + case 2: + $result['status_str'] = '审核失败,请重新提交审核'; + break; + } + return $result; + } + + /** + * 上级邀请人信息 + * @param $user_id + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function myLeader($user_id) + { + + $field = 'nickname,avatar,is_distribution,mobile,first_leader,distribution_code,earnings'; + + $user = Db::name('user') + ->field($field) + ->where(['id' => $user_id]) + ->find(); + + $first_leader = Db::name('user') + ->field('nickname,mobile') + ->where(['id' => $user['first_leader']]) + ->findOrEmpty(); + + $user['avatar'] = UrlServer::getFileUrl($user['avatar'], 'local'); + return [ + 'user' => $user, + 'leader' => $first_leader, + ]; + } + + /** + * 分销推广主页信息 + * @param $user_id + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function index($user_id) + { + $user_info = self::myLeader($user_id);//用户信息 + $fans = Db::name('user') + ->where('first_leader|second_leader', '=', $user_id) + ->count(); + + //今天的预估收益 + $today_earnings = Db::name('distribution_order_goods') + ->whereTime('create_time', 'today') + ->where(['status' => 1, 'user_id' => $user_id]) + ->sum('money'); + + //本月预估收益 + $month_earnings = Db::name('distribution_order_goods') + ->whereTime('create_time', 'month') + ->where(['status' => 1, 'user_id' => $user_id]) + ->sum('money'); + + //累计收益 + $history_earnings = Db::name('distribution_order_goods') + ->where(['status' => 2, 'user_id' => $user_id]) + ->sum('money'); + + $data = [ + 'user' => $user_info['user'], + 'leader' => $user_info['leader'], + 'fans' => $fans,//粉丝数量 + 'able_withdrawal' => $user_info['user']['earnings'],//可提现佣金 + 'today_earnings' => round($today_earnings, 2),//今天预估收益 + 'month_earnings' => round($month_earnings, 2),//本月预估收益 + 'history_earnings' => round($history_earnings, 2),//累计收益 + ]; + return $data; + } + + + //可提现佣金 + public static function getAbleWithdrawal($user_id) + { + $after_sale_time = ConfigServer::get('after_sale', 'refund_days'); + $time = time() - ($after_sale_time * 24 * 60 * 60); + + //可提现佣金 + $orders = Db::name('order o') + ->field('o.id, d.money') + ->join('order_goods g', 'o.id = g.order_id') + ->join('distribution_order_goods d', 'd.order_goods_id = g.id') + ->where('o.create_time', '<', $time) + ->where('o.order_status', Order::STATUS_FINISH) + ->where('d.user_id', $user_id) + ->where('d.status', DistributionOrder::STATUS_WAIT_HANDLE) + ->select(); + + $able_withdrawal = 0; + if (!$orders) { + return $able_withdrawal; + } + + foreach ($orders as $order) { + $check = Db::name('after_sale') + ->where(['order_id' => $order['id']]) + ->where('status', 'in', [AfterSale::STATUS_WAIT_REFUND, AfterSale::STATUS_SUCCESS_REFUND]) + ->find(); + + if ($check) { + continue; + } + $able_withdrawal += $order['money']; + } + return $able_withdrawal; + } + + + /** + * 分销订单列表(待返佣,已结算,已失效) + * @param $user_id + * @param $get + * @return array|\PDOStatement|string|\think\Collection + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function order($user_id, $get, $page, $size) + { + $where = []; + $where[] = ['d.user_id', '=', $user_id]; + + $status = $get['status'] ?? 0; + if ($status != 0) { + $where[] = ['d.status', '=', $status]; + } + + $field = 'o.order_sn, og.total_pay_price as pay_price, d.create_time, d.money, og.item_id, og.goods_num, d.status'; + + $count = Db::name('distribution_order_goods d') + ->field($field) + ->join('order_goods og', 'og.id = d.order_goods_id') + ->join('order o', 'o.id = og.order_id') + ->where($where) + ->count(); + + $lists = Db::name('distribution_order_goods d') + ->field($field) + ->join('order_goods og', 'og.id = d.order_goods_id') + ->join('order o', 'o.id = og.order_id') + ->where($where) + ->order('o.id desc') + ->page($page, $size) + ->select(); + + $item_ids = array_column($lists, 'item_id'); + $goods = Db::name('goods_item i') + ->join('goods g', 'i.goods_id = g.id') + ->where('i.id', 'in', $item_ids) + ->column('g.name, g.image, i.image as spec_image, i.spec_value_str', 'i.id'); + + foreach ($lists as &$item) { + $item_id = $item['item_id']; + if (!isset($goods[$item_id])) { + continue; + } + $info = $goods[$item_id]; + $item['goods_name'] = $info['name']; + $item['spec_name'] = $info['spec_value_str']; + $item['image'] = UrlServer::getFileUrl(empty($info['spec_image']) ? $info['image'] : $info['spec_image']); + $item['status'] = DistributionOrder::getOrderStatus($item['status']); + $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); + } + + $data = [ + 'list' => $lists, + 'page' => $page, + 'size' => $size, + 'count' => $count, + 'more' => is_more($count, $page, $size) + ]; + return $data; + } + + + /** + * 月度账单 + * @param $user_id + * @param $get + * @param $page + * @param $size + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getMonthBill($user_id, $page, $size) + { + $field = [ + "FROM_UNIXTIME(d.create_time,'%Y年%m月') as date", + "FROM_UNIXTIME(d.create_time,'%Y') as year", + "FROM_UNIXTIME(d.create_time,'%m') as month", + 'sum(d.money) as total_money', + 'count(o.id) as order_num' + ]; + $count = Db::name('distribution_order_goods d') + ->field($field) + ->join('order_goods g', 'g.id = d.order_goods_id') + ->join('order o', 'o.id = g.order_id') + ->where(['d.user_id' => $user_id]) + ->where('d.status', 'in', [DistributionOrder::STATUS_WAIT_HANDLE, DistributionOrder::STATUS_SUCCESS]) + ->group('date') + ->count(); + + $lists = Db::name('distribution_order_goods d') + ->field($field) + ->join('order_goods g', 'g.id = d.order_goods_id') + ->join('order o', 'o.id = g.order_id') + ->where(['d.user_id' => $user_id]) + ->where('d.status', 'in', [DistributionOrder::STATUS_WAIT_HANDLE, DistributionOrder::STATUS_SUCCESS]) + ->page($page, $size) + ->group('date') + ->select(); + + $data = [ + 'list' => $lists, + 'page' => $page, + 'size' => $size, + 'count' => $count, + 'more' => is_more($count, $page, $size) + ]; + return $data; + } + + public static function getMonth($get, $user_id, $page, $size) + { + $year = $get['year'] ?? date('Y'); + $month = $get['month'] ?? date('m'); + //指定月份 + list($y, $m, $t) = explode('-', date("$year-$month-t")); + $time_range = [ + mktime(0, 0, 0, $m, 1, $y), + mktime(23, 59, 59, $m, $t, $y) + ]; + + $field = 'o.order_sn, o.id as order_id, g.total_pay_price as pay_price, d.create_time, d.money, g.item_id, g.goods_num, d.status'; + + + $count = Db::name('distribution_order_goods')->alias('d') + ->field($field) + ->join('order_goods g', 'g.id = d.order_goods_id') + ->join('order o', 'o.id = g.order_id') + ->where(['d.user_id' => $user_id]) + ->where('d.status', 'in', [DistributionOrder::STATUS_WAIT_HANDLE, DistributionOrder::STATUS_SUCCESS]) + ->where('d.create_time', 'between time', $time_range) + ->count(); + + $lists = Db::name('distribution_order_goods')->alias('d') + ->field($field) + ->join('order_goods g', 'g.id = d.order_goods_id') + ->join('order o', 'o.id = g.order_id') + ->where(['d.user_id' => $user_id]) + ->where('d.status', 'in', [DistributionOrder::STATUS_WAIT_HANDLE, DistributionOrder::STATUS_SUCCESS]) + ->where('d.create_time', 'between time', $time_range) + ->page($page, $size) + ->select(); + + + $item_ids = array_column($lists, 'item_id'); + $goods = Db::name('goods_item i') + ->join('goods g', 'i.goods_id = g.id') + ->where('i.id', 'in', $item_ids) + ->column('g.name, g.image, i.image as spec_image, i.spec_value_str', 'i.id'); + + $total_money = 0; + $order_ids = []; + foreach ($lists as &$item) { + $item_id = $item['item_id']; + if (!isset($goods[$item_id])) { + continue; + } + $info = $goods[$item_id]; + $item['goods_name'] = $info['name']; + $item['spec_name'] = $info['spec_value_str']; + $item['image'] = UrlServer::getFileUrl(empty($info['spec_image']) ? $info['image'] : $info['spec_image']); + $item['status'] = DistributionOrder::getOrderStatus($item['status']); + $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); + $total_money += $item['money']; + if (!in_array($item['order_id'], $order_ids)) { + array_push($order_ids, $item['order_id']); + } + } + + $data = [ + 'year' => $year, + 'month' => $month, + 'total_money' => $total_money, + 'total_order' => count($order_ids), + 'list' => $lists, + 'page' => $page, + 'size' => $size, + 'count' => $count, + 'more' => is_more($count, $page, $size) + ]; + return $data; + + } + + /** + * Desc: 生成用户扩展表 + * @param $user_id + * @return bool + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function createUserDistribution($user_id) + { + $user_distribution = Db::name('user_distribution') + ->where(['user_id' => $user_id]) + ->find(); + + if ($user_distribution) { + return true; + } + + $data = [ + 'user_id' => $user_id, + 'distribution_order_num' => 0, + 'distribution_money' => 0, + 'fans' => 0, + 'create_time' => time(), + ]; + Db::name('user_distribution')->insert($data); + return true; + } + + + /** + * Desc: 取消订单后更新分销订单为已失效 + * @param $order_id + * @throws Exception + * @throws \think\exception\PDOException + */ + public static function setDistributionOrderFail($order_id) + { + //订单取消后更新分销订单为已失效状态 + return Db::name('distribution_order_goods d') + ->join('order_goods og', 'og.id = d.order_goods_id') + ->join('order o', 'o.id = og.order_id') + ->where('o.id', $order_id) + ->update([ + 'd.status' => DistributionOrder::STATUS_ERROR, + 'd.update_time' => time(), + ]); + } + + + /** + * Notes: 发生售后后,更新分销订单为已失效 + * @param $order_goods_id + * @author 段誉(2021/5/13 17:38) + * @return int|string + * @throws Exception + * @throws \think\exception\PDOException + */ + public static function setFailByAfterSale($order_goods_id) + { + return Db::name('distribution_order_goods') + ->where('order_goods_id', $order_goods_id) + ->update([ + 'status' => DistributionOrder::STATUS_ERROR, + 'update_time' => time(), + ]); + } + + + /** + * Notes: 根据后台设置返回当前生成用户的分销会员状态(设置了全员分销,新生成的用户即为分销会员) + * @author 段誉(2021/4/7 14:48) + * @return int + */ + public static function isDistributionMember() + { + $is_distribution = 0; + //分销会员申请--1,申请分销; 2-全员分销; + $distribution = ConfigServer::get('distribution', 'member_apply', 1); + if ($distribution == 2) { + $is_distribution = 1; + } + return $is_distribution; + } + + public static function fixAncestorRelation() + { + Db::startTrans(); + try { + $userList = User::select()->toArray(); + if (empty($userList)) { + throw new \Exception('没有用户,无需修复'); + } + + $updateEmptyData = []; + $updateData = []; + foreach($userList as $user) { + $my_ancestor_relation = self::myAncestorRelation($user); + $updateEmptyData[] = ['id' => $user['id'], 'ancestor_relation' => '']; + $updateData[] = ['id' => $user['id'], 'ancestor_relation' => $my_ancestor_relation]; + } + // 先清除所有关系链 + (new User())->saveAll($updateEmptyData); + // 重新设置关系链 + (new User())->saveAll($updateData); + + Db::commit(); + return [ + "flag" => true, + "msg" => '修复成功' + ]; + } catch (\Exception $e) { + Db::rollback(); + return [ + "flag" => false, + "msg" => $e->getMessage() + ]; + } + } + + public static function myAncestorRelation($user) + { + if (empty($user['first_leader'])) { + return ''; + } + + return trim(self::findAncestorRelation($user['first_leader']), ','); + } + + public static function findAncestorRelation($id, $flag = true) + { + static $ancestor_relation = ''; + if ($flag) { + $ancestor_relation = ''; + } + $ancestor_relation .= $id . ','; + $user = User::findOrEmpty($id)->toArray(); + if (empty($user['first_leader'])) { + return $ancestor_relation; + } + return self::findAncestorRelation($user['first_leader'], false); + } + + /** + * @notes 获取背景海报 + * @return array + * @author cjhao + * @date 2021/11/29 12:00 + */ + public static function getPoster(){ + $poster = ConfigServer::get('invite', 'poster', '/images/share/share_user_bg.png'); + $poster = empty($poster) ? $poster : UrlServer::getFileUrl($poster); + return ['poster'=>$poster]; + } +} \ No newline at end of file diff --git a/app/api/logic/LoginLogic.php b/app/api/logic/LoginLogic.php index 1a9263b..77e2095 100755 --- a/app/api/logic/LoginLogic.php +++ b/app/api/logic/LoginLogic.php @@ -14,8 +14,17 @@ namespace app\api\logic; +use Alipay\EasySDK\Kernel\Factory; +use app\api\cache\TokenCache; +use app\api\service\UserServer; use app\common\cache\WebScanLoginCache; +use app\common\logic\AccountLogLogic; use app\common\logic\BaseLogic; +use app\common\model\account\AccountLog; +use app\common\model\Client_; +use app\common\service\WeChatServer; +use app\common\service\ConfigServer; +use EasyWeChat\Kernel\Exceptions\Exception; use app\api\service\{UserTokenService, WechatUserService}; use app\common\enum\{LoginEnum, user\UserTerminalEnum, YesNoEnum}; use app\common\service\{ @@ -27,7 +36,7 @@ use app\common\service\{ wechat\WeChatRequestService }; use app\common\model\user\{User, UserAuth}; -use think\facade\{Db, Config}; +use think\facade\{Cache, Db, Config}; /** * 登录逻辑 @@ -115,8 +124,173 @@ class LoginLogic extends BaseLogic return false; } } + public static function authLogin($post) + { + try { + //通过code获取微信 openid + $response = self::getWechatResByCode($post); + $response['headimgurl'] = $post['headimgurl'] ?? ''; + $response['nickname'] = $post['nickname'] ?? ''; + //通过获取到的openID或unionid获取当前 系统 用户id + $user_id = self::getUserByWechatResponse($response); + } catch (Exception $e) { + self::setError('登录失败:' . $e->getMessage()); + return false; + } catch (\think\Exception $e) { + self::setError('登录失败:' . $e->getMessage()); + return false; + } + if (empty($user_id)) { + $user_info = UserServer::createUser($response, Client_::mnp); + } else { + $user_info = UserServer::updateUser($response, Client_::mnp, $user_id); + } + + //验证用户信息 + $check_res = self::checkUserInfo($user_info); + if (true !== $check_res) { + return self::setError($check_res); + + } + + //创建会话 + $user_info['token'] = self::createSession($user_info['id'], Client_::mnp); + + unset($user_info['id'], $user_info['disable']); + return $user_info; + } + public static function registerAward($user_id){ + $register_award_integral_status = ConfigServer::get('marketing','register_award_integral_status',0); + $register_award_coupon_status = ConfigServer::get('marketing','register_award_coupon_status',0); + //赠送积分 + if($register_award_integral_status){ + $register_award_integral = ConfigServer::get('marketing','register_award_integral',0); + //赠送的积分 + if($register_award_integral > 0){ + \think\Db::name('user')->where(['id'=>$user_id])->setInc('user_integral',$register_award_integral); + AccountLogLogic::AccountRecord($user_id,$register_award_integral,1,AccountLog::register_add_integral,''); + } + } + //注册账号,首次进入首页时领取优惠券 + $register_award_coupon = ConfigServer::get('marketing','register_award_coupon',''); + if($register_award_coupon_status && $register_award_coupon){ + Cache::tag('register_coupon')->set('register_coupon_'.$user_id,$register_award_coupon); + } + //会员等级 + $user_level = Db::name('user_level')->where(['del'=>0,'growth_value'=>0])->find(); + if($user_level){ + Db::name('user')->where(['id'=>$user_id])->update(['level'=>$user_level['id']]); + } + } + /** + * Notes: 根据code 获取微信信息(openid, unionid) + * @param $post + * @author 段誉(2021/4/19 16:52) + * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string + * @throws Exception + * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException + */ + public static function getWechatResByCode($post) + { +// $config = WeChatServer::getMnpConfig(); + $response = (new WeChatMnpService())->getMnpResByCode($post['code']); +// $app = Factory::miniProgram($config); +// $response = $app->auth->session($post['code']); + if (!isset($response['openid']) || empty($response['openid'])) { + throw new Exception('获取openID失败'); + } + return $response; + } + /** + * Notes: 根据微信返回信息查询当前用户id + * @param $response + * @author 段誉(2021/4/19 16:52) + * @return mixed + */ + public static function getUserByWechatResponse($response) + { + $user_id = Db::name('user_auth au') + ->join('user u', 'au.user_id=u.id') + ->where(['u.del' => 0]) + ->where(function ($query) use ($response) { + $query->whereOr(['au.openid' => $response['openid']]); + if(isset($response['unionid']) && !empty($response['unionid'])){ + $query->whereOr(['au.unionid' => $response['unionid']]); + } + }) + ->value('user_id'); + return $user_id; + } + public static function checkUserInfo($user_info) + { + if (empty($user_info)) { + return '登录失败:user'; + } + + if ($user_info['disable']) { + return '账号已被禁用'; + } + + return true; + } + /** + * 创建会话 + * @param $user_id + * @param $client + * @return string + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + * @throws \think\exception\PDOException + */ + public static function createSession($user_id, $client) + { + + //清除之前缓存 + $token = Db::name('session') + ->where(['user_id' => $user_id, 'client' => $client]) + ->value('token'); + if($token) { + $token_cache = new TokenCache($token); + $token_cache->del(); + } + + $result = Db::name('session') + ->where(['user_id' => $user_id, 'client' => $client]) + ->find(); + + $time = time(); + $expire_time = $time + Config::get('project.token_expire_time'); + $token = md5($user_id . $client . $time); + $data = [ + 'user_id' => $user_id, + 'token' => $token, + 'client' => $client, + 'update_time' => $time, + 'expire_time' => $expire_time, + ]; + + if (empty($result)) { + Db::name('session')->insert($data); + } else { + Db::name('session') + ->where(['user_id' => $user_id, 'client' => $client]) + ->update($data); + } + + //更新登录信息 + $login_ip = $ip = request()->ip(); + Db::name('user') + ->where(['id' => $user_id]) + ->update(['login_time' => $time, 'login_ip' => $login_ip]); + + //创建新的缓存 + (new TokenCache($token, ['token' => $token]))->set(300); + return $token; + } /** * @notes 退出登录 * @param $userInfo diff --git a/app/api/service/UserServer.php b/app/api/service/UserServer.php new file mode 100644 index 0000000..0d63270 --- /dev/null +++ b/app/api/service/UserServer.php @@ -0,0 +1,246 @@ + ConfigServer::get('storage', 'default', 'local'), + 'engine' => ConfigServer::get('storage_engine') + ]; + + $time = time(); //创建时间 + $avatar = ''; //头像路径 + + if (empty($avatar_url)) { + $avatar = ConfigServer::get('website', 'user_image'); + } else { + if ($config['default'] == 'local') { + $file_name = md5($openid . $time) . '.jpeg'; + $avatar = download_file($avatar_url, 'uploads/user/avatar/', $file_name); + } else { + $avatar = 'uploads/user/avatar/' . md5($openid . $time) . '.jpeg'; + $StorageDriver = new StorageDriver($config); + if (!$StorageDriver->fetch($avatar_url, $avatar)) { + throw new Exception( '头像保存失败:'. $StorageDriver->getError()); + } + } + } + $data = [ + 'nickname' => $nickname, + 'sn' => create_user_sn(), + 'avatar' => $avatar, + 'create_time' => $time, + 'distribution_code' => generate_invite_code(),//分销邀请码 + 'is_distribution' => DistributionLogic::isDistributionMember(), + // 微信新用户 + 'is_new_user' => 1, + ]; + + if (empty($nickname)) { + $data['nickname'] = '用户'.$data['sn']; + } + + $user_id = Db::name('user') + ->insertGetId($data); + + $data = [ + 'user_id' => $user_id, + 'openid' => $openid, + 'create_time' => $time, + 'unionid' => $unionid, + 'client' => $client, + ]; + Db::name('user_auth') + ->insert($data); + + //生成会员分销扩展表 + DistributionLogic::createUserDistribution($user_id); + // 生成分销会员基础表 + \app\common\logic\DistributionLogic::add($user_id); + //消息通知 + Hook::listen('notice', ['user_id' => $user_id, 'scene' => NoticeSetting::REGISTER_SUCCESS_NOTICE]); + + //注册赠送优惠 + LoginLogic::registerAward($user_id); + + Db::commit(); + + $user_info = Db::name('user') + ->field(['id', 'nickname', 'avatar', 'level', 'disable', 'distribution_code','is_new_user']) + ->where(['id' => $user_id]) + ->find(); + if (empty($user_info['avatar'])) { + $user_info['avatar'] = UrlServer::getFileUrl(ConfigServer::get('website', 'user_image')); + } else { + $user_info['avatar'] = UrlServer::getFileUrl($user_info['avatar']); + } + + } catch (Exception $e) { + Db::rollback(); + throw new Exception($e->getMessage()); + } + + return $user_info; + } + + /** + * 更新用户信息 + * @param $response + * @param $client + * @param $user_id + * @return array|\PDOStatement|string|\think\Model|null + */ + public static function updateUser($response, $client, $user_id) + { + $time = time(); + try { + + $openid = $response['openid']; + $unionid = $response['unionid'] ?? ''; + $avatar_url = $response['headimgurl'] ?? ''; + $nickname = $response['nickname'] ?? ''; + + Db::startTrans(); + + //ios,android + if (in_array($client, [Client_::ios, Client_::android])) { + Db::name('user_auth') + ->where(['openid' => $openid]) + ->update(['client' => $client]); + } + + //用户已存在,但是无该端的授权信息,保存数据 + $user_auth_id = Db::name('user_auth') + ->where(['user_id' => $user_id, 'openid' => $openid]) + ->value('id'); + + if (empty($user_auth_id)) { + $data = [ + 'create_time' => $time, + 'openid' => $openid, + 'unionid' => $unionid, + 'user_id' => $user_id, + 'client' => $client, + ]; + Db::name('user_auth') + ->insert($data); + } + + $user_info = Db::name('user u') + ->field(['u.nickname', 'u.avatar', 'u.level', 'u.id', 'au.unionid']) + ->join('user_auth au', 'u.id=au.user_id') + ->where(['au.openid' => $openid]) + ->find(); + + //无头像需要更新头像 + if (empty($user_info['avatar'])) { + // 获取存储引擎 + $config = [ + 'default' => ConfigServer::get('storage', 'default', 'local'), + 'engine' => ConfigServer::get('storage_engine') + ]; + + $avatar = ''; //头像路径 + if ($config['default'] == 'local') { + $file_name = md5($openid . $time) . '.jpeg'; + $avatar = download_file($avatar_url, 'uploads/user/avatar/', $file_name); + } else { + $avatar = 'uploads/user/avatar/' . md5($openid . $time) . '.jpeg'; + $StorageDriver = new StorageDriver($config); + if (!$StorageDriver->fetch($avatar_url, $avatar)) { + throw new Exception( '头像保存失败:'. $StorageDriver->getError()); + } + } + + $data['avatar'] = $avatar; + $data['update_time'] = $time; + $data['nickname'] = $nickname; + Db::name('user') + ->where(['id' => $user_info['id']]) + ->update($data); + } + + //之前无unionid需要更新 + if (empty($user_info['unionid']) && isset($unionid)) { + $data = []; + $data['unionid'] = $unionid; + $data['update_time'] = $time; + Db::name('user_auth') + ->where(['user_id' => $user_info['id']]) + ->update($data); + } + + $user_info = Db::name('user') + ->where(['id' => $user_info['id']]) + ->field(['id', 'nickname', 'avatar', 'level', 'disable', 'distribution_code','is_new_user']) + ->find(); + + if (empty($user_info['avatar'])) { + $user_info['avatar'] = UrlServer::getFileUrl(ConfigServer::get('website', 'user_image')); + } else { + $user_info['avatar'] = UrlServer::getFileUrl($user_info['avatar']); + } + Db::commit(); + + } catch (Exception $e) { + Db::rollback(); + throw new Exception($e->getMessage()); + } + + return $user_info; + } +} \ No newline at end of file diff --git a/app/common.php b/app/common.php index a66b62c..f0751ca 100755 --- a/app/common.php +++ b/app/common.php @@ -2,7 +2,7 @@ // 应用公共文件 use app\common\service\FileService; use think\helper\Str; - +use think\facade\{Db, Config}; /** * @notes 生成密码加密密钥 * @param string $plaintext @@ -122,6 +122,80 @@ function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_i return $tree; } +/** + * 生成会员码 + * @return 会员码 + */ +function create_user_sn($prefix = '', $length = 8) +{ + $rand_str = ''; + for ($i = 0; $i < $length; $i++) { + $rand_str .= mt_rand(0, 9); + } + $sn = $prefix . $rand_str; + if (Db::name('user')->where(['sn' => $sn])->find()) { + return create_user_sn($prefix, $length); + } + return $sn; +} + +//生成用户邀请码 +function generate_invite_code() +{ + + $letter_all = range('A', 'Z'); + shuffle($letter_all); + //排除I、O字母 + $letter_array = array_diff($letter_all, ['I', 'O', 'D']); + //排除1、0 + $num_array = range('2', '9'); + shuffle($num_array); + + $pattern = array_merge($num_array, $letter_array, $num_array); + shuffle($pattern); + $pattern = array_values($pattern); + + $code = ''; + for ($i = 0; $i < 6; $i++) { + $code .= $pattern[mt_rand(0, count($pattern) - 1)]; + } + + $code = strtoupper($code); + $check = Db::name('user')->where('distribution_code', $code)->find(); + if ($check) { + return generate_invite_code(); + } + return $code; +} +/** + * User: 意象信息科技 mjf + * Desc: 用时间生成订单编号 + * @param $table + * @param $field + * @param string $prefix + * @param int $rand_suffix_length + * @param array $pool + * @return string + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ +function createSn($table, $field, $prefix = '', $rand_suffix_length = 4, $pool = []) +{ + $suffix = ''; + for ($i = 0; $i < $rand_suffix_length; $i++) { + if (empty($pool)) { + $suffix .= rand(0, 9); + } else { + $suffix .= $pool[array_rand($pool)]; + } + } + $sn = $prefix . date('YmdHis') . $suffix; + if (Db::name($table)->where($field, $sn)->find()) { + return createSn($table, $field, $prefix, $rand_suffix_length, $pool); + } + return $sn; +} /** * @notes 删除目标目录 diff --git a/app/common/logic/AccountLogLogic.php b/app/common/logic/AccountLogLogic.php index 9aeafe0..4d04494 100755 --- a/app/common/logic/AccountLogLogic.php +++ b/app/common/logic/AccountLogLogic.php @@ -15,6 +15,7 @@ namespace app\common\logic; use app\common\enum\user\AccountLogEnum; +use app\common\model\account\AccountLog; use app\common\model\user\UserAccountLog; use app\common\model\user\User; @@ -73,4 +74,62 @@ class AccountLogLogic extends BaseLogic ]; return UserAccountLog::create($data); } + + /** + * Notes:记录会员账户流水,如果变动类型是成长值,且是增加的,则调用更新会员等级方法。该方法应在添加用户账户后调用 + * @author: cjh 2020/12/15 11:49 + * @param int $user_id 用户id + * @param float $amount 变动数量 + * @param int $change_type 变动类型:1-增加;2-减少 + * @param int $source_type 来源类型 + * @param string $remark 说明 + * @param string $source_id 来源id + * @param string $source_sn 来源单号 + * @param string $extra 额外字段说明 + * @return bool + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function AccountRecord($user_id,$amount,$change_type,$source_type,$remark ='',$source_id ='',$source_sn='',$extra=''){ + $user = User::get($user_id); + if(empty($user)){ + return false; + } + $type = AccountLog::getChangeType($source_type); + $left_amount = ''; + switch ($type){ + case 'money': + $left_amount = $user->user_money; + break; + case 'integral': + $left_amount = $user->user_integral; + break; + case 'growth': + $left_amount = $user->user_growth; + break; + case 'earnings': + $left_amount = $user->earnings; + } + $account_log = new AccountLog(); + $account_log->log_sn = createSn('account_log','log_sn','',4); + $account_log->user_id = $user_id; + $account_log->source_type = $source_type; + $account_log->source_id = $source_id; + $account_log->source_sn = $source_sn; + $account_log->change_amount = $amount; + $account_log->left_amount = $left_amount; + $account_log->remark = AccountLog::getRemarkDesc($source_type,$source_sn,$remark); + $account_log->extra = $extra; + $account_log->change_type = $change_type; + $account_log->create_time = time(); + $account_log->save(); + + //更新会员等级 + if($type === 'growth' && $change_type == 1){ + UserLevelLogic::updateUserLevel($user_id); + } + return true; + + } } \ No newline at end of file diff --git a/app/common/logic/DistributionLogic.php b/app/common/logic/DistributionLogic.php new file mode 100644 index 0000000..42df4af --- /dev/null +++ b/app/common/logic/DistributionLogic.php @@ -0,0 +1,60 @@ +value('id'); + // 分销会员开通方式 + $apply_condition = ConfigServer::get('distribution', 'apply_condition', 2); + $isDistribution = $apply_condition == 1 ? 1 : 0; + + $data = [ + 'user_id' => $userId, + 'level_id' => $defaultLevelId, + 'is_distribution' => $isDistribution, + 'is_freeze' => 0, + 'remark' => '', + ]; + + if($isDistribution) { + // 成为分销会员时间 + $data['distribution_time'] = time(); + } + + Distribution::create($data); + } +} \ No newline at end of file diff --git a/app/common/logic/UserLevelLogic.php b/app/common/logic/UserLevelLogic.php new file mode 100644 index 0000000..59228d0 --- /dev/null +++ b/app/common/logic/UserLevelLogic.php @@ -0,0 +1,64 @@ +where(['id'=>$id])->field('user_growth,level')->find(); + $level = Db::name('user_level') + ->where([['growth_value','<=',$user['user_growth']],['del','=',0]]) + ->order('growth_value desc') + ->find(); + + if($level){ + $growth_value = 0; + $user['level'] > 0 && $growth_value = Db::name('user_level')->where(['id'=>$user['level']])->value('growth_value'); + if($level['growth_value'] > $growth_value){ + Db::name('user')->where(['id'=>$id])->update(['level'=>$level['id'],'update_time'=>time()]); + } + } + return true; + + } + /** + * note 用户升级 更新所有用户的等级 + * create_time 2020/11/26 19:11 + */ + public static function updateAllUserLevel($level_id){ + $growth_value = Db::name('user_level')->where(['id'=>$level_id])->value('growth_value'); + $no_update_user_ids = Db::name('user')->alias('U') + ->join('user_level L','U.level = L.id') + ->where('L.growth_value','>',$growth_value) + ->column('U.id'); + + return Db::name('user') + ->where([ + ['id','not in',$no_update_user_ids], + ['del','=',0], + ['user_growth','>=',$growth_value], + ])->update(['level'=>$level_id,'update_time'=>time()]); + + } +} \ No newline at end of file diff --git a/app/common/model/Client_.php b/app/common/model/Client_.php new file mode 100644 index 0000000..2ab1a8a --- /dev/null +++ b/app/common/model/Client_.php @@ -0,0 +1,70 @@ + 'pc商城', + self::h5 => 'h5商城', + self::oa => '公众号商城', + self::mnp => '小程序商城', + self::ios => '苹果APP商城', + self::android => '安卓APP商城', + ]; + if ($type === true) { + return $desc; + } + return $desc[$type] ?? '未知'; + } +} \ No newline at end of file diff --git a/app/common/model/account/AccountLog.php b/app/common/model/account/AccountLog.php new file mode 100644 index 0000000..e12b663 --- /dev/null +++ b/app/common/model/account/AccountLog.php @@ -0,0 +1,170 @@ + '系统增加余额', + self::admin_reduce_money => '系统扣减余额', + self::recharge_money => '用户充值余额', + self::admin_add_integral => '系统增加积分', + self::admin_reduce_integral => '系统扣减积分', + self::sign_in_integral => '每日签到赠送积分', + self::recharge_give_integral => '充值赠送积分', + self::order_add_integral => '下单赠送积分', + self::order_deduction_integral => '下单积分抵扣', + self::register_add_integral => '注册赠送积分', + self::invite_add_integral => '邀请会员赠送积分', + self::admin_add_growth => '系统增加成长值', + self::admin_reduce_growth => '系统扣减成长值', + self::sign_give_growth => '每日签到赠送成长值', + self::recharge_give_growth => '充值赠送成长值', + self::balance_pay_order => '下单扣减余额', + self::cancel_order_refund => '取消订单退回余额', + self::after_sale_refund => '售后退回余额', + self::withdraw_to_balance => '佣金提现', + self::withdraw_dec_earnings => '提现扣减佣金', + self::withdraw_back_earnings => '拒绝提现返还佣金', + self::distribution_inc_earnings => '订单结算获得佣金', + self::cancel_order_refund_integral => '取消订单退回积分', + self::deduct_order_first_integral => '扣除首单积分', + self::luck_draw_integral => '积分抽奖中奖', + self::order_goods_give_integral => '购买商品赠送积分', + self::user_transfer_inc_balance => '会员转账(收入方)', + self::user_transfer_dec_balance => '会员转账(支出方)', + self::order_give_growth => '下单赠送成长值', + self::admin_inc_earnings => '后台增加佣金', + self::admin_reduce_earnings => '后台减少佣金', + self::luck_draw_dec_integral => '积分抽奖消耗积分', + self::luck_draw_inc_balance => '积分抽奖中奖余额', + ]; + if($from === true){ + return $desc; + } + return $desc[$from] ?? ''; + } + //返回变动类型 + public static function getChangeType($from){ + $type = ''; + if(in_array($from,self::money_change)){ + $type = 'money'; + } + if(in_array($from,self::integral_change)){ + $type = 'integral'; + } + if(in_array($from,self::growth_change)){ + $type = 'growth'; + } + if(in_array($from,self::earnings_change)){ + $type = 'earnings'; + } + return $type; + } + + public static function getRemarkDesc($from,$source_sn,$remark =''){ + return $remark; + } + + + public static function getChangeAmountAttr($value,$data){ + $amount = $value; + if(!in_array($data['source_type'],self::money_change)){ + $amount = intval($value); + } + if($data['change_type'] == 1){ + return '+'.$amount; + } + return '-'.$amount; + } + + public static function getSourceTypeAttr($value,$data){ + return self::getAcccountDesc($value); + + } + + public static function getcreateTimeAttr($value,$data){ + if($value){ + return date('Y-m-d H:i:s',$value); + } + return ''; + } + +} \ No newline at end of file diff --git a/app/common/model/distribution/Distribution.php b/app/common/model/distribution/Distribution.php new file mode 100644 index 0000000..2786bbf --- /dev/null +++ b/app/common/model/distribution/Distribution.php @@ -0,0 +1,16 @@ +count(); + return $num; + } + + public static function getLevelName($levelId) + { + $level = self::field('name,weights')->findOrEmpty($levelId)->toArray(); + if (empty($level)) { + return ''; + } + return $level['name']. '(' . $level['weights'] . ')级'; + } + + public static function getLevelNameTwo($levelId) + { + $level = self::field('name,weights')->findOrEmpty($levelId)->toArray(); + if (empty($level)) { + return ''; + } + return $level['name']; + } +} \ No newline at end of file diff --git a/app/common/model/noticesetting/NoticeSetting.php b/app/common/model/noticesetting/NoticeSetting.php new file mode 100644 index 0000000..d9ed744 --- /dev/null +++ b/app/common/model/noticesetting/NoticeSetting.php @@ -0,0 +1,269 @@ + self::ORDER_PAY_NOTICE, + 'DDFHTZ' => self::ORDER_DELIVERY_NOTICE, + 'SJTYSHTK' => self::PLATFORM_PASS_REFUND_NOTICE, + 'SJJJSHTK' => self::PLATFORM_REFUSE_REFUND_NOTICE, + 'ZCYZ' => self::REGISTER_NOTICE, + 'ZHMM' => self::GET_BACK_MOBILE_NOTICE, + 'DDTZ' => self::USER_PAID_NOTICE_PLATFORM, + 'SHTKDDTZ' => self::AFTER_SALE_NOTICE_PLATFORM, + 'YZMDL' => self::GET_GODE_LOGIN_NOTICE, + 'BGSJHM' => self::CHANGE_MOBILE_NOTICE, + 'BDSJHM' => self::BIND_MOBILE_NOTICE, + 'ZHZFMM' => self::GET_BACK_PAY_CODE_NOTICE, + ]; + + + + /** + * Notes: 获取场景描述 + * @param $state + * @return array|mixed|string + * @author 段誉(2021/4/26 16:15) + */ + public static function getSceneDesc($state) + { + $data = [ + self::ORDER_PAY_NOTICE => '订单已支付', + self::ORDER_DELIVERY_NOTICE => '订单已发货', + self::PLATFORM_PASS_REFUND_NOTICE => '平台通过售后退款通知', + self::PLATFORM_REFUSE_REFUND_NOTICE => '平台拒绝售后退款通知', + self::REGISTER_NOTICE => '注册通知', + self::CHANGE_MOBILE_NOTICE => '变更手机号短信通知', + self::GET_BACK_MOBILE_NOTICE => '重置密码短信通知', + self::REGISTER_SUCCESS_NOTICE => '注册成功', + self::INVITE_SUCCESS_NOTICE => '邀请成功', + self::GET_EARNINGS_NOTICE => '获得收益', + self::GET_GODE_LOGIN_NOTICE => '验证码登录', + self::BIND_MOBILE_NOTICE => '绑定手机号', + self::GET_BACK_PAY_CODE_NOTICE => '找回支付密码', + + self::USER_PAID_NOTICE_PLATFORM => '会员支付下单通知平台', + self::AFTER_SALE_NOTICE_PLATFORM => '会员发起售后退款通知', + ]; + if ($state === true) { + return $data; + } + return $data[$state] ?? ''; + } + + + /** + * Notes: 根据场景获取跳转地址 + * @param $scene + * @author 段誉(2021/4/27 17:01) + * @return array + */ + public static function getPathByScene($scene, $extra_id) + { + $page = '/pages/index/index'; // 小程序主页路径 + $url = '/mobile/pages/index/index'; // 公众号主页路径 + if (in_array($scene, self::ORDER_SCENE)) { + $url = '/mobile/pages/order_details/order_details?id='.$extra_id; + $page = '/pages/order_details/order_details?id='.$extra_id; + } + return ['url' => $url, 'page' => $page]; + } + + + /** + * Notes: 场景名称 + * @param $value + * @param $data + * @author 段誉(2021/4/26 16:56) + * @return array|mixed|string + */ + public function getSceneAttr($value, $data) + { + return self::getSceneDesc($value); + } + + + /** + * Notes: 场景变量 + * @param $value + * @param $data + * @author 段誉(2021/4/26 17:07) + * @return mixed + */ + public function getVariableAttr($value, $data) + { + return $this->jsonToArr($value); + } + + + /** + * Notes: 系统消息 + * @param $value + * @param $data + * @author 段誉(2021/4/26 17:18) + * @return array|mixed + */ + public function getSystemNoticeAttr($value, $data) + { + return $this->jsonToArr($value); + } + + /** + * Notes: 短信消息 + * @param $value + * @param $data + * @author 段誉(2021/4/26 17:25) + * @return array|mixed + */ + public function getSmsNoticeAttr($value, $data) + { + return $this->jsonToArr($value); + } + + + /** + * Notes: 公众号消息 + * @param $value + * @param $data + * @author 段誉(2021/4/26 17:25) + * @return array|mixed + */ + public function getOaNoticeAttr($value, $data) + { + return $this->jsonToArr($value); + } + + /** + * Notes: 小程序消息 + * @param $value + * @param $data + * @author 段誉(2021/4/26 17:25) + * @return array|mixed + */ + public function getMnpNoticeAttr($value, $data) + { + return $this->jsonToArr($value); + } + + + public function jsonToArr($data) + { + return empty($data) ? [] : json_decode($data, JSON_UNESCAPED_UNICODE); + } + +} \ No newline at end of file diff --git a/app/common/model/user/User.php b/app/common/model/user/User.php index 86fcc0e..502474c 100755 --- a/app/common/model/user/User.php +++ b/app/common/model/user/User.php @@ -20,7 +20,8 @@ use app\common\enum\user\UserEnum; use app\common\model\BaseModel; use app\common\service\FileService; use think\model\concern\SoftDelete; - +use think\Model; +use think\facade\Db; /** * 用户模型 * Class User @@ -32,6 +33,16 @@ class User extends BaseModel protected $deleteTime = 'delete_time'; + public static function get(int $user_id) + { + $list = Db::name('user')->where("id",$user_id)->find(); +// $objList = array_map(function ($item) { +// return (object) $item; +// }, $list); + return (object) $list; +// return $objList; + } + /** * @notes 关联用户授权模型 diff --git a/app/common/service/WeChatServer.php b/app/common/service/WeChatServer.php new file mode 100644 index 0000000..a45fa3a --- /dev/null +++ b/app/common/service/WeChatServer.php @@ -0,0 +1,190 @@ + ConfigServer::get('mnp', 'app_id'), + 'secret' => ConfigServer::get('mnp', 'secret' ), + 'mch_id' => ConfigServer::get('mnp', 'mch_id'), + 'key' => ConfigServer::get('mnp', 'key'), + 'response_type' => 'array', + 'log' => [ + 'level' => 'debug', + 'file' => '../runtime/log/wechat.log' + ], + ]; + return $config; + } + + /** + * 获取微信公众号配置 + * @return array + */ + public static function getOaConfig() + { + $config = [ + 'app_id' => ConfigServer::get('oa', 'app_id'), + 'secret' => ConfigServer::get('oa', 'secret'), + 'mch_id' => ConfigServer::get('oa', 'mch_id'), + 'key' => ConfigServer::get('oa', 'key'), + 'token' => ConfigServer::get('oa', 'token',''), + 'response_type' => 'array', + 'log' => [ + 'level' => 'debug', + 'file' => '../runtime/log/wechat.log' + ], + ]; + return $config; + } + + /** + * 获取微信开放平台应用配置 + * @return array + */ + public static function getOpConfig() + { + $config = [ + 'app_id' => ConfigServer::get('op', 'app_id'), + 'secret' => ConfigServer::get('op', 'secret'), + 'response_type' => 'array', + 'log' => [ + 'level' => 'debug', + 'file' => '../runtime/log/wechat.log' + ], + ]; + return $config; + } + + + + /** + * 根据不同来源获取支付配置 + */ + public static function getPayConfigBySource($order_source) + { + $notify_url = ''; + switch ($order_source) { + case Client_::mnp: + $notify_url = url('payment/notifyMnp', '', '', true); + break; + case Client_::oa: + case Client_::pc: + case Client_::h5: + $notify_url = url('payment/notifyOa', '', '', true); + break; + case Client_::android: + case Client_::ios: + $notify_url = url('payment/notifyApp', '', '', true); + break; + } + + $config = self::getPayConfig($order_source); + + if (empty($config) || + empty($config['key']) || + empty($config['mch_id']) || + empty($config['app_id']) || + empty($config['secret']) + ) { + throw new Exception('请在后台配置好微信支付'); + } + + return [ + 'config' => $config, + 'notify_url' => $notify_url, + ]; + } + + + + + + + + + //===================================支付配置======================================================= + + //微信支付设置 H5支付 appid 可以是公众号appid + public static function getPayConfig($client) + { + switch ($client) { + case Client_::mnp: + $appid = ConfigServer::get('mnp', 'app_id'); + $secret = ConfigServer::get('mnp', 'secret'); + break; + case Client_::oa: + case Client_::pc: + case Client_::h5: + $appid = ConfigServer::get('oa', 'app_id'); + $secret = ConfigServer::get('oa', 'secret'); + break; + case Client_::android: + case Client_::ios: + $appid = ConfigServer::get('op', 'app_id'); + $secret = ConfigServer::get('op', 'secret'); + break; + default: + $appid = ''; + $secret = ''; + } + + $pay = Pay::where(['code' => 'wechat'])->find()->toArray(); + + $config = [ + 'app_id' => $appid, + 'secret' => $secret, + 'mch_id' => $pay['config']['mch_id'] ?? '', + 'key' => $pay['config']['pay_sign_key'] ?? '', + 'cert_path' => $pay['config']['apiclient_cert'] ?? '', + 'key_path' => $pay['config']['apiclient_key'] ?? '', + 'response_type' => 'array', + 'log' => [ + 'level' => 'debug', + 'file' => '../runtime/log/wechat.log' + ], + ]; + + if (is_cli()) { + $config['cert_path'] = ROOT_PATH.'/public/'.$pay['config']['apiclient_cert']; + $config['key_path'] = ROOT_PATH.'/public/'.$pay['config']['apiclient_key']; + } + + return $config; + } + +} \ No newline at end of file diff --git a/vendor/topthink/framework/src/think/facade/Hook.php b/vendor/topthink/framework/src/think/facade/Hook.php new file mode 100644 index 0000000..e9e1208 --- /dev/null +++ b/vendor/topthink/framework/src/think/facade/Hook.php @@ -0,0 +1,37 @@ + +// +---------------------------------------------------------------------- + +namespace think\facade; + +use think\Facade; + +/** + * @see \think\Hook + * @mixin \think\Hook + * @method \think\Hook alias(mixed $name, mixed $behavior = null) static 指定行为标识 + * @method void add(string $tag, mixed $behavior, bool $first = false) static 动态添加行为扩展到某个标签 + * @method void import(array $tags, bool $recursive = true) static 批量导入插件 + * @method array get(string $tag = '') static 获取插件信息 + * @method mixed listen(string $tag, mixed $params = null, bool $once = false) static 监听标签的行为 + * @method mixed exec(mixed $class, mixed $params = null) static 执行行为 + */ +class Hook extends Facade +{ + /** + * 获取当前Facade对应类名(或者已经绑定的容器对象标识) + * @access protected + * @return string + */ + protected static function getFacadeClass() + { + return 'hook'; + } +}