Przeglądaj źródła

优化注册流程

Jason 1 rok temu
rodzic
commit
68f00a2d68

+ 32 - 0
application/admin/controller/trade/MoneyIn.php

@@ -3,6 +3,10 @@
 namespace app\admin\controller\trade;
 
 use app\common\controller\Backend;
+use app\common\model\Users;
+use think\exception\DbException;
+use think\exception\PDOException;
+use think\exception\ValidateException;
 
 /**
  * 充值记录管理
@@ -61,5 +65,33 @@ class MoneyIn extends Backend
         return json($result);
     }
 
+    /**
+     * 充值订单审核
+     * @param $ids
+     * @return string
+     * @throws DbException
+     * @throws \think\Exception
+     */
+    public function review($ids = null, $status= 0)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $result = false;
+        try {
+            $result = $row->allowField(true)->save(['status' => $this->model::Success]);
+            //累积充值金额
+            //(new Users())->where('id', $row->user_id)->setInc('money_in_sum', $row->amount);
+
+        } catch (ValidateException|PDOException|Exception $e) {
+            $this->error($e->getMessage());
+        }
+        if (false === $result) {
+            $this->error(__('No rows were updated'));
+        }
+        $this->success();
+    }
+
 
 }

+ 1 - 1
application/admin/controller/user/Team.php

@@ -26,7 +26,7 @@ class Team extends Backend
     public function _initialize()
     {
         parent::_initialize();
-        $this->model = model('User');
+        $this->model = model('Users');
         $user_id = $this->request->param('ids/d', 0);
         $this->assignconfig('user_id', $user_id);
         $this->view->assign("typeList", UsersPath::where('parent_id', $user_id)->order('distance', 'asc')->column('distance'));

+ 1 - 1
application/api/controller/Money.php

@@ -230,7 +230,7 @@ class Money extends Api
             'order_type' => $type,
             'user_id'   => $user['id'],
             'amount'    => $amount,
-            'status'    => MoneyOut::Default,
+            'status'    => MoneyOut::Pending,
             'agent_id'  => $withdraw_info['agent_id'],
             'user_type' => $user['user_type'],
         ];

+ 4 - 1
application/api/controller/Order.php

@@ -38,7 +38,7 @@ class Order extends Api
         $data['bonus_sum']      = $user['bonus_sum'];
         $data['bonus_today']    = (new OrderModel())
                                     ->where('user_id', $user['id'])
-                                    ->where('status', OrderModel::STATUSFINISH)
+                                    ->where('status', OrderModel::Success)
                                     ->whereTime('create_time', '>=', strtotime('today'))
                                     ->sum('bonus');
 
@@ -205,8 +205,11 @@ class Order extends Api
             (new MoneyLog())->change($user['id'], $order_info['amount'], MoneyLog::PayBack, '', '');
             //订单佣金
             (new MoneyLog())->change($user['id'], $order_info['bonus'], MoneyLog::OrderBonus, '', '');
+            //累积充值金额
+            //(new Users())->where('id', $row->user_id)->setInc('money_in_sum', $row->amount);
             //向上级发放
 
+
             // 提交事务
             Db::commit();
         } catch (Exception $e) {

+ 5 - 2
application/api/controller/User.php

@@ -46,11 +46,14 @@ class User extends Api
         $data['bonus_sum']   = $user['bonus_sum'];
         $data['order_num']   = (new OrderModel())
                                 ->where('user_id', $user['id'])
-                                ->where('status', OrderModel::STATUSFINISH)
+                                ->where('status', OrderModel::Success)
                                 ->count();
         $data['invite_link'] = Env::get('app.invite_domain') . '/?i=' . $user['invitation_code'];
         $data['invitation_code'] = $user['invitation_code'];
-
+        if(empty($user['invitation_code'])){
+            //生成邀请码
+            $data['invitation_code'] = (new UserModel())->setInvitationCode($user['id']);
+        }
         $this->success('', $data);
     }
 

+ 13 - 8
application/common/library/Auth.php

@@ -4,6 +4,7 @@ namespace app\common\library;
 
 use app\common\model\Users;
 use app\common\model\UserRule;
+use app\common\model\UsersPath;
 use fast\Random;
 use think\Config;
 use think\Db;
@@ -166,34 +167,38 @@ class Auth
         $ip = request()->ip();
         $time = time();
 
-        $data = [
+        $user_data = [
             //'username' => $username,
             //'email'    => $email,
             'mobile'   => $mobile,
             'code'     => $code,
             'login_pwd'=> $password,
             'parent_id'=> $parent_info['id'],
-//            'level'    => 1,
-//            'score'    => 0,
-//            'avatar'   => '',
+            'agent_id' => $parent_info['agent_id'],
         ];
-        $params = array_merge($data, [
+        $user_data = array_merge($user_data, [
             'nickname'  => preg_match("/^1[3-9]{1}\d{9}$/", $mobile) ? substr_replace($mobile, '****', 3, 4) : $mobile,
             'salt'      => Random::alnum(),
             'join_time' => $time,
             'login_ip'  => $ip,
             'login_time'=> $time,
         ]);
-        $params['login_pwd'] = $this->getEncryptPassword($password, $params['salt']);
+        $user_data['login_pwd'] = $this->getEncryptPassword($password, $user_data['salt']);
         //$params = array_merge($params, $extend);
 
         //账号注册时需要开启事务,避免出现垃圾数据
         Db::startTrans();
         try {
-            $user = Users::create($params, true);
+            $user = Users::create($user_data, true);
 
             $this->_user = Users::get($user->id);
 
+            // 创建网体
+            (new UsersPath())->createPath($user->id, $parent_info['id']);
+
+            //上级人数+1
+            (new Users())->where('id', $parent_info['id'])->setInc('team_num');
+
             //设置Token
             $this->_token = Random::uuid();
             Token::set($this->_token, $user->id, $this->keeptime);
@@ -202,7 +207,7 @@ class Auth
             $this->_logined = true;
 
             //注册成功的事件
-            Hook::listen("user_register_successed", $this->_user, $data);
+            Hook::listen("user_register_successed", $this->_user, $user_data);
             Db::commit();
         } catch (Exception $e) {
             $this->setError($e->getMessage());

+ 1 - 1
application/common/model/Order.php

@@ -63,7 +63,7 @@ class Order extends Model
     //操作列表
     public function getstatusList()
     {
-        return [self::STATUSFINISH => __('Finish'), 2 => __('Freeze'), 3 => __('Cancel')];
+        return [self::Success => __('Finish'), 2 => __('Freeze'), 3 => __('Cancel')];
     }
 
 

+ 19 - 52
application/common/model/Users.php

@@ -163,66 +163,33 @@ class Users extends Model
      * @param int    $user_id 会员ID
      * @param string $memo    备注
      */
-    public static function money($money, $user_id, $memo)
+    public function setInvitationCode($user_id)
     {
-        Db::startTrans();
-        try {
-            $user = self::lock(true)->find($user_id);
-            if ($user && $money != 0) {
-                $before = $user->money;
-                //$after = $user->money + $money;
-                $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
-                //更新会员信息
-                $user->save(['money' => $after]);
-                //写入日志
-                MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
-            }
-            Db::commit();
-        } catch (\Exception $e) {
-            Db::rollback();
-        }
+        $inviteCode = $this->generateInviteCode();
+        $this->where('id', $user_id)->update(['invitation_code' => $inviteCode]);
+        return $inviteCode;
     }
 
+
     /**
-     * 变更会员积分
-     * @param int    $score   积分
-     * @param int    $user_id 会员ID
-     * @param string $memo    备注
+     * 生成随机邀请码
+     * @param $len
+     * @return string
      */
-    public static function score($score, $user_id, $memo)
+    protected function generateInviteCode($len = 4)
     {
-        Db::startTrans();
-        try {
-            $user = self::lock(true)->find($user_id);
-            if ($user && $score != 0) {
-                $before = $user->score;
-                $after = $user->score + $score;
-                $level = self::nextlevel($after);
-                //更新会员信息
-                $user->save(['score' => $after, 'level' => $level]);
-                //写入日志
-                ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
-            }
-            Db::commit();
-        } catch (\Exception $e) {
-            Db::rollback();
+        $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+        $inviteCode = '';
+        for ($i = 0; $i < $len; $i++) {
+            $inviteCode .= $characters[mt_rand(0, strlen($characters) - 1)];
         }
-    }
 
-    /**
-     * 根据积分获取等级
-     * @param int $score 积分
-     * @return int
-     */
-    public static function nextlevel($score = 0)
-    {
-        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
-        $level = 1;
-        foreach ($lv as $key => $value) {
-            if ($score >= $value) {
-                $level = $key;
-            }
+        //检查是否重复
+        if($this->where('invitation_code', $inviteCode)->count()){
+            return $this->generateInviteCode($len);
         }
-        return $level;
+
+        return $inviteCode;
     }
+
 }

+ 83 - 0
application/common/model/UsersPath.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace app\common\model;
+
+use think\db\exception\DataNotFoundException;
+use think\db\exception\ModelNotFoundException;
+use think\exception\DbException;
+use think\Model;
+
+class UsersPath extends Model
+{
+
+    // 表名
+    protected $name = 'users_path';
+
+    /**
+     * 创建网体
+     * @param int $newUserID
+     * @param int $parentID
+     * @return void
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public function createPath(int $newUserID, int $parentID)
+    {
+        if ($parentID == 0) {
+            return;
+        }
+
+        $parentIDs = $this->getAllParentIDs($parentID); // 查询直接父级的所有上级
+        array_unshift($parentIDs, $parentID);  // 直接父级放开头
+
+        // 循环插入
+        foreach ($parentIDs as $k => $v) {
+            $this->insert([
+                'user_id'   => $newUserID,
+                'parent_id' => $v,
+                'distance'  => $k + 1,
+            ]);
+        }
+    }
+
+    /**
+     * 获取所有上级ID
+     * @param int $uid
+     * @return array
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public function getAllParentIDs(int $uid): array
+    {
+        $parentIDs = [];
+        $paths     = $this->where('user_id', $uid)->field('parent_id')->order('distance', 'asc')->select();
+        if (!is_null($paths)) {
+            foreach ($paths as $v) {
+                $parentIDs[] = $v['parent_id'];
+            }
+        }
+        return $parentIDs;
+    }
+
+    /**
+     * 获取直属上级ID
+     * @param int $uid
+     * @param int $distance
+     * @return int
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public function getParentID(int $uid, int $distance = 1): int
+    {
+        $path = $this->where(['user_id' => $uid, 'distance' => $distance])->field('parent_id')->find();
+        if (!is_null($path)) {
+            return $path['parent_id'];
+        }
+        return 0;
+    }
+
+
+}

+ 2 - 1
public/assets/js/backend/user/user.js

@@ -59,7 +59,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                     name: "account",
                                     text:  __('Account change'),
                                     classname: 'btn btn-xs btn-info btn-magic btn-dialog',
-                                    url: 'trade/moneylog/index',
+                                    extend: 'data-area=\'["80%","80%"]\'',
+                                    url: 'trade/money_log/index',
                                     refresh: true
                             },{
                                 name: "clear",