wugg пре 1 година
родитељ
комит
87b5a266b2

+ 106 - 0
app/admin/controller/Wechat.php

@@ -0,0 +1,106 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\admin\controller;
+
+use app\admin\validate\Admin as AdminValidate;
+use app\common\controller\Backend;
+use think\App;
+use think\db\exception\PDOException;use think\facade\Config;
+use think\facade\Db;use think\Request;
+use think\exception\ValidateException;
+
+class Wechat extends Backend
+{
+
+	protected $noNeedLogin=["wxlogin"];
+	protected $noNeedPermission=["wxinfo","wxedit"];
+	public function __construct(App $app) {parent::__construct($app);}
+	public function initialize(){
+		parent::initialize(); // TODO: Change the autogenerated stub
+	}
+    /**
+     * 显示资源列表
+     *
+     * @return \think\Response
+     */
+    public function wxlogin()
+    {
+    	$openMemberCenter = Config::get('buildadmin.open_member_center');
+        if (!$openMemberCenter) {
+            $this->error(__('Member center disabled'));
+        }
+
+        // 检查登录态
+        if ($this->auth->isLogin()) {
+            $this->success(__('You have already logged in. There is no need to log in again~'), [
+                'routePath' => '/user'
+            ], 302);
+        }
+         if ($this->request->isPost()) {
+            $params = $this->request->post(['openid','unionid',"nickname"=>"","avatar"=>"","mobile"=>"", "keep",
+            'registerType'=>"wx"]);
+            $validate = new AdminValidate();
+            try {
+                $validate->scene("wechat")->check($params);
+            } catch (ValidateException $e){
+                $this->error($e->getMessage());
+            }
+		    $res = $this->auth->isWxUser($params['openid'], $params['unionid'], (bool)$params['keep']);
+            if (!$res) {
+                $res = $this->auth->WxRegister( $params['openid'], $params['unionid'], $params['nickname'], $params['mobile'], $params['avatar']);
+            }
+
+            if (isset($res) && $res === true) {
+                $this->success(__('Login succeeded!'), [
+                    'userInfo'  =>$this->auth->getInfo(),
+                    'routePath' => '/user'
+                ]);
+            } else {
+                $msg = $this->auth->getError();
+                $msg = $msg ?: __('Check in failed, please try again or contact the website administrator~');
+                $this->error($msg);
+            }
+        }
+
+        $this->success('', [
+            'accountVerificationType' => get_account_verification_type()
+        ]);
+    }
+	//微信端用户信息
+   public  function wxinfo(){
+        $this->success('', $this->auth->getInfo());
+	}
+	//微信端用户信息补充
+   public  function wxedit(){
+            $data = $this->request->post();
+            if (!$data) {
+                $this->error(__('Parameter %s can not be empty', ['']));
+            }
+            // 数据验证
+            if ($this->modelValidate) {
+                try {
+                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->auth->getAdmin()));
+                    $validate = new $validate;
+                    $validate->scene('wxedit')->check($data);
+                } catch (ValidateException $e) {
+                    $this->error($e->getMessage());
+                }
+            }
+            $data   = $this->excludeFields($data);
+            $result = false;
+            Db::startTrans();
+            try {
+                $result = $this->auth->getAdmin()->save($data);
+                Db::commit();
+            } catch (PDOException|\Exception $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            }
+            if ($result !== false) {
+                $this->success(__('Update successful'));
+            } else {
+                $this->error(__('No rows updated'));
+            }
+        }
+}

+ 98 - 1
app/admin/library/Auth.php

@@ -13,6 +13,8 @@ use think\db\exception\DbException;
 use think\db\exception\PDOException;
 use think\db\exception\DataNotFoundException;
 use think\db\exception\ModelNotFoundException;
+use think\facade\Validate;
+use think\facade\Event;
 
 /**
  * 管理员权限类
@@ -51,7 +53,7 @@ class Auth extends \ba\Auth
     /**
      * @var string[] 允许输出的字段
      */
-    protected $allowFields = ['id', 'username', 'nickname', 'avatar', 'lastlogintime'];
+    protected $allowFields = ['id', 'username', 'nickname', 'avatar', "mobile",'email','department_id','jobs_id','lastlogintime'];
 
     public function __construct(array $config = [])
     {
@@ -454,5 +456,100 @@ class Auth extends \ba\Auth
     public function getError()
     {
         return $this->error ? __($this->error) : '';
+    }
+		/**
+		* @param string $openid
+		* @param string $unionid
+		* @param string $nickname
+		* @param string $mobile
+		* @param string $avatar
+		* @return bool
+        */
+    public function WxRegister(string $openid, string $unionid,string $nickname='', string $mobile='',string $avatar=''){
+    	$validate = Validate::rule([
+            'openid'    => 'require|unique:admin,openid^unionid',
+            'unionid' => 'max:255',
+        ]);
+        $params   = [
+            'nickname' => $nickname,
+            'openid' => $openid,
+            'mobile'   => $mobile,
+            'unionid'    => $unionid,
+            'avatar'    => $avatar,
+        ];
+        if (!$validate->check($params)) {
+            $this->setError('Registration parameter error');
+            return false;
+        }
+
+        $ip   = request()->ip();
+        $time = time();
+        $salt = Random::build('alnum', 16);
+        $data = [
+            'password'      => '',
+            'joinip'        => $ip,
+            'jointime'      => $time,
+            'lastloginip'   => $ip,
+            'lastlogintime' => $time,
+            'salt'          => $salt,
+            'status'        => '1',
+        ];
+        $data = array_merge($params, $data);
+        Db::startTrans();
+        try {
+            $this->model = Admin::create($data);
+            $this->token = Random::uuid();
+            Token::set($this->token, 'user', $this->model->id, $this->keeptime);
+            Event::trigger('AdminWxSuccessed', $this->model);
+            Db::commit();
+        } catch (PDOException|Exception $e) {
+            $this->setError($e->getMessage());
+            Db::rollback();
+            return false;
+        }
+        return true;
+    }
+
+       /** 判断微信账胡是否注册
+		* @param string $openid
+		* @param string $unionid
+		 * @return bool
+		* @throws \think\db\exception\DataNotFoundException
+		* @throws \think\db\exception\DbException
+		* @throws \think\db\exception\ModelNotFoundException
+		 */
+    public function isWxUser(string $openid,string $unionid,bool $keeptime): bool
+    {
+        if($openid=='')return false;
+
+        $this->model = Admin::where(['openid'=>$openid,"unionid"=>$unionid])->find();
+        if (!$this->model) {
+            $this->setError('Account not exist');
+            return false;
+        }
+        if ($this->model['status'] == '0') {
+            $this->setError('Account disabled');
+            return false;
+        }
+        $adminLoginRetry = Config::get('buildadmin.admin_login_retry');
+        if ($adminLoginRetry && $this->model->loginfailure >= $adminLoginRetry && time() - $this->model->getData('lastlogintime') < 86400) {
+            $this->setError('Please try again after 1 day');
+            return false;
+        }
+//        if ($this->model->password != encrypt_password($password, $this->model->salt)) {
+//            $this->loginFailed();
+//            $this->setError('Password is incorrect');
+//            return false;
+//        }
+        if (Config::get('buildadmin.admin_sso')) {
+            Token::clear('admin', $this->model->id);
+            Token::clear('admin-refresh', $this->model->id);
+        }
+
+        if ($keeptime) {
+            $this->setRefreshToken(2592000);
+        }
+        $this->loginSuccessful();
+        return true;
     }
 }

+ 10 - 0
app/admin/validate/Admin.php

@@ -15,6 +15,9 @@ class Admin extends Validate
         'email'     => 'email|unique:admin',
         'mobile'    => 'mobile|unique:admin',
         'group_arr' => 'require|array',
+        'openid' => 'require|unique:admin,openid^unionid',
+        'change_mobile' => 'require|mobile',
+        'change_email' => 'email',
     ];
 
     /**
@@ -34,6 +37,7 @@ class Admin extends Validate
      */
     protected $scene = [
         'add' => ['username', 'nickname', 'password', 'email', 'mobile', 'group_arr'],
+        'wechat' => ['opendid',"unionid"]
     ];
 
     /**
@@ -45,6 +49,10 @@ class Admin extends Validate
             ->remove('password', 'require');
     }
 
+    public function sceneWxEdit(){
+       return $this->only(["nickname","change_mobile","change_email","avatar"]);
+	}
+
     /**
      * 验证场景-编辑资料
      */
@@ -61,7 +69,9 @@ class Admin extends Validate
             'nickname'  => __('Nickname'),
             'password'  => __('Password'),
             'email'     => __('Email'),
+            'change_email'     => __('Email'),
             'mobile'    => __('Mobile'),
+            'change_mobile'    => __('Mobile'),
             'group_arr' => __('Group Name Arr'),
         ];
         $this->message = array_merge($this->message, [

+ 19 - 2
app/admin/validate/ReqOrder.php

@@ -12,20 +12,37 @@ class ReqOrder extends Validate
      * 验证规则
      */
     protected $rule = [
+    	"city"=>'require',
+    	"act_time"=>'require|date|dateFormat:Y-m-d H:i:s',
+    	"participant"=>'require|number|in:0,1,2',
+    	"budget"=>'require|float',
+    	"require_item"=>'require|max:255',
+    	"req_corp"=>'number',
+    	"phone"=>'require|mobile',
     ];
 
     /**
      * 提示消息
      */
     protected $message = [
+    	"city"=>"请选择活动城市",
+    	"act_time"=>"请选择活动时间",
+    	"participant"=>"请选择活动人数",
+    	"budget"=>"请输入活动预算",
+    	"require_item"=>"请选择活动类型",
+    	"phone"=>"请输入正确的联系方式",
+//    	"require_item"=>"请选择活动类型",
     ];
 
     /**
      * 验证场景
      */
     protected $scene = [
-        'add'  => [],
-        'edit' => [],
+        'add'  => ["city","act_time","participant","budget","require_item","phone",],
+        'edit' => ["city","act_time","participant","budget","require_item","phone",],
     ];
 
+    public function __construct() {
+    	 parent::__construct();
+    }
 }

+ 47 - 27
app/api/controller/Ajax.php

@@ -3,16 +3,18 @@
 namespace app\api\controller;
 
 
+use app\common\controller\Backend;
 use think\Exception;
 use think\exception\FileException;
 use app\common\library\Upload;
-use app\common\controller\Frontend;
+use think\exception\ValidateException;
 use think\facade\Db;
-use think\facade\Log;
+use think\facade\Event;
 use app\admin\model\ReqOrder;
-class Ajax extends Frontend
+use app\admin\validate\ReqOrder as ReqOrderValidate;use think\facade\Validate;
+class Ajax extends Backend
 {
-    protected $noNeedLogin = ['area', 'buildSuffixSvg','saveFirstForm'];
+    protected $noNeedLogin = ['area', 'buildSuffixSvg','saveFirstForm','listFirstForm','FirstFormInfo'];
 
     public function initialize()
     {
@@ -21,50 +23,68 @@ class Ajax extends Frontend
 
     public function saveFirstForm(){
         $user_id  = $this->auth->id;
-
-        $user_id = 1;
-        
+//        $user_id = 1;
         $post = $this->request->post();
         $reqOrder = new ReqOrder();
-        $validate = new \app\admin\validate\ReqOrder;
-        if (!$validate->check($post)) {
-            $this->error($validate->getError());
+        $validate = new ReqOrderValidate();
+        try{
+        $validate->scene("add")->check($post);
+        }catch (ValidateException $e){
+        	$this->error($e->getMessage());
         }
 
         $data = [
-            //'sn' => 'as'.date('YmdH').rand(1000,9999).substr(md5($user_id),-5),
+            'reqCode' => 'REQ'.date('YmdH').rand(1000,9999).substr(md5($user_id),-5),
             'req_user_id' => $user_id,
-            'req_endtime' => date('Y-m-d 00:00:00',time()*3600*24*5), //默认5天截止
+            'req_endtime' => date('Y-m-d 00:00:00',time()+3600*24*5), //默认5天截止
             'city' => $post['city'],
-            'act_time' => $post['act_date'],
-            'act_day_count' => 1,
+            'name' => $post['name'],
+            'act_time' => $post['act_time'],
+            'act_day_count' => $post['act_day_count'],
             'budget' => $post['budget'],
             'participant'=> $post['participant'],
-            'require_item' => '',
-            'req_corp' => '',
+            'require_item' =>$post['require_item'],
+            'req_corp' =>$post['req_corp'],
             'req_tel' => $post['phone'],
 
         ];
         $res = $reqOrder->save($data);
-
-        //print log post
-        //Log::write($post,'error');
-       
         if(!$res){
             $this->error('保存失败');
         }
-        
-        $this->success('保存成功');
+         Event::trigger("ReOrderAdd",$data);
+        $this->success('保存成功',["reqCode"=>$data['reqCode']]);
     }
 
     public function listFirstForm(){
-        //get user id
+		$post = $this->request->post();
         $user_id = $this->auth->id;
-        $res = Db::name('req_order')->where('user_id',$user_id)->find();
-        if(!$res){
-            $this->error('没有数据');
+        $where =[['req_user_id',"=",$user_id]];
+        if ($post['status']!='')$where[]=["status","=",$post['status']];
+        if ($post['flow_stage']!='')$where[]=["flow_stage","=",$post['flow_stage']];
+        $res = Db::name('req_order')
+        ->where($where)
+        ->paginate($post['limit'])
+        ->order("id desc");
+         $this->success('', [
+            'list'   => $res->items(),
+            'total'  => $res->total(),
+            'remark' => get_route_remark(),
+        ]);
+    }
+
+    public function FirstFormInfo(){
+		$post = $this->request->post();
+		$valide =Validate::rule(["reqCode|订单编号"=>"require|max:255"]);
+		if($valide->check($post)==false)$this->error($valide->getError());
+        $user_id = $this->auth->id;
+        $res = Db::name('req_order')->where('req_user_id',$user_id)->where("reqCode",$post['reqCode'])->findOrEmpty();
+		 if(!$res){
+            $this->error('信息获取失败');
         }
-        $this->success('获取成功',$res);
+		 $res['supplierName'] = Db::name("supplier")->where(["id"=>$res['supplier_id']])->value("name","");
+		 $res['city_area'] = Db::name("area")->where(["id"=>$res['city']])->value("mergename","");
+		 $this->success('',$res);
     }
     public function upload()
     {

+ 24 - 0
app/api/controller/User.php

@@ -117,4 +117,28 @@ class User extends Frontend
             $this->success();
         }
     }
+	/**
+	* 会员更新个人信息
+	 */
+    public function Edit(){
+        $param =$this->request->only(["change_mobile","nickname","change_email","avatar","gender","birthday"],"post");
+         $validate = new UserValidate();
+            try {
+                $validate->scene("edit")->check($param);
+            } catch (ValidateException $e) {
+                $this->error($e->getMessage());
+            }
+
+		$res = $this->auth->updateUser($param['nickname'],$param['change_mobile'],$param['change_email'],$param['avatar'],$param['gender'],$param['birthday']);
+       if (isset($res) && $res === true) {
+                $this->success(__('Update succeeded!'), [
+                    'userInfo'  => $this->auth->getUserInfo(),
+                    'routePath' => '/user'
+                ]);
+            } else {
+                $msg = $this->auth->getError();
+                $msg = $msg ?: __('Update failed!');
+                $this->error($msg);
+            }
+    }
 }

+ 0 - 135
app/api/controller/Wechat.php

@@ -1,135 +0,0 @@
-<?php
-declare (strict_types = 1);
-
-namespace app\api\controller;
-
-use app\api\validate\User as UserValidate;
-use app\common\controller\Frontend;
-use think\App;
-use think\facade\Config;
-use think\Request;
-use think\exception\ValidateException;
-
-class Wechat extends Frontend
-{
-
-	protected $noNeedLogin=["wxlogin"];
-	protected $noNeedPermission=[];
-	public function __construct(App $app) {parent::__construct($app);}
-	public function initialize(){
-                parent::initialize(); // TODO: Change the autogenerated stub
-	}
-    /**
-     * 显示资源列表
-     *
-     * @return \think\Response
-     */
-    public function wxlogin()
-    {
-    	$openMemberCenter = Config::get('buildadmin.open_member_center');
-        if (!$openMemberCenter) {
-            $this->error(__('Member center disabled'));
-        }
-
-        // 检查登录态
-        if ($this->auth->isLogin()) {
-            $this->success(__('You have already logged in. There is no need to log in again~'), [
-                'routePath' => '/user'
-            ], 302);
-        }
-         if ($this->request->isPost()) {
-            $params = $this->request->post(['openid','unionid', "keep", 'registerType'=>"wx"]);
-            $validate = new UserValidate();
-            try {
-                $validate->scene("wechat")->check($params);
-            } catch (ValidateException $e){
-                $this->error($e->getMessage());
-            }
-		    $res = $this->auth->isWxUser($params['openid'], $params['unionid'], (bool)$params['keep']);
-            if (!$res) {
-                $res = $this->auth->WxRegister( $params['openid'], $params['unionid']);
-            }
-
-            if (isset($res) && $res === true) {
-                $this->success(__('Login succeeded!'), [
-                    'userInfo'  => $this->auth->getUserInfo(),
-                    'routePath' => '/user'
-                ]);
-            } else {
-                $msg = $this->auth->getError();
-                $msg = $msg ?: __('Check in failed, please try again or contact the website administrator~');
-                $this->error($msg);
-            }
-        }
-
-        $this->success('', [
-            'accountVerificationType' => get_account_verification_type()
-        ]);
-    }
-
-    /**
-     * 显示创建资源表单页.
-     *
-     * @return \think\Response
-     */
-    public function create()
-    {
-        //
-    }
-
-    /**
-     * 保存新建的资源
-     *
-     * @param  \think\Request  $request
-     * @return \think\Response
-     */
-    public function save(Request $request)
-    {
-        //
-    }
-
-    /**
-     * 显示指定的资源
-     *
-     * @param  int  $id
-     * @return \think\Response
-     */
-    public function read($id)
-    {
-        //
-    }
-
-    /**
-     * 显示编辑资源表单页.
-     *
-     * @param  int  $id
-     * @return \think\Response
-     */
-    public function edit($id)
-    {
-        //
-    }
-
-    /**
-     * 保存更新的资源
-     *
-     * @param  \think\Request  $request
-     * @param  int  $id
-     * @return \think\Response
-     */
-    public function update(Request $request, $id)
-    {
-        //
-    }
-
-    /**
-     * 删除指定资源
-     *
-     * @param  int  $id
-     * @return \think\Response
-     */
-    public function delete($id)
-    {
-        //
-    }
-}

+ 8 - 0
app/api/lang/zh-cn/user.php

@@ -14,4 +14,12 @@ return [
     'You have already logged in. There is no need to log in again~'           => '您已经登录过了,无需重复登录~',
     'Check in failed, please try again or contact the website administrator~' => '签入失败,请重试或联系网站管理员~',
     'Member center disabled'                                                  => '会员中心已禁用,请联系网站管理员开启。',
+    'Update succeeded!'                                                       =>'用户信息更新成功',
+    'Update failed!'                                                          => '用户信息更新失败',
+    'birthday'                                                          => '生日',
+    'gender'                                                          => '性别',
+    'change_mobile'                                                          => '手机号',
+    'change_email'                                                          => '邮箱',
+    'avatar'                                                          => '头像',
+    'nickname'                                                          => '昵称',
 ];

+ 13 - 1
app/api/validate/User.php

@@ -17,7 +17,12 @@ class User extends Validate
         'captchaId'   => 'require',
         'captchaInfo' => 'require',
         "openid"=>"require|unique:user,openid&unionid",
-
+        "birthday"=>"require|date|dateFormat:Y-m-d",
+        "gender"=>"require|number|in:0,1,2",
+        "avatar"=>"url",
+        "nickname"=>"require",
+        "change_email"=>"email",
+        "change_mobile"=>"mobile",
     ];
 
     /**
@@ -27,6 +32,7 @@ class User extends Validate
         'login'    => ['password', 'captchaId', 'captchaInfo'],
         'register' => ['email', 'username', 'password', 'mobile', 'captcha'],
         'wechat' => ["openid","unionid"],
+        'edit' => ["change_mobile","nickname","change_email","avatar","gender","birthday"],
     ];
 
     public function __construct()
@@ -35,6 +41,12 @@ class User extends Validate
             'username'    => __('username'),
             'email'       => __('email'),
             'mobile'      => __('mobile'),
+            'change_email'      => __('email'),
+            'change_mobile'      => __('mobile'),
+            'avatar'      => __('avatar'),
+            'nickname'      => __('nickname'),
+            'gender'      => __('gender'),
+            'birthday'      => __('birthday'),
             'password'    => __('password'),
             'captcha'     => __('captcha'),
             'captchaId'   => __('captchaId'),

+ 2 - 2
app/common.php

@@ -311,8 +311,8 @@ if (!function_exists('build_suffix_svg')) {
 if (!function_exists('get_area')) {
     function get_area()
     {
-        $province = request()->get('province', '');
-        $city     = request()->get('city', '');
+        $province = request()->param('province', '');
+        $city     = request()->param('city', '');
         $where    = ['pid' => 0, 'level' => 1];
         if ($province !== '') {
             $where['pid']   = $province;

+ 33 - 0
app/common/library/Auth.php

@@ -577,4 +577,37 @@ class Auth extends \ba\Auth
     {
         return $this->error ? __($this->error) : '';
     }
+	/**
+	* @param string $nickname  昵称
+	* @param string $mobile 联系方式
+	* @param string $email 邮箱
+	* @param string $avatar 头像
+	* @param string $gender 性别
+	* @param string $birthday 生日
+	 * @return bool
+	 */
+    public function updateUser(string $nickname,string $mobile, string $email,string $avatar,string $gender,string $birthday):bool{
+
+
+		if (!$this->model) {
+            return false;
+        }
+        Db::startTrans();
+        try {
+	        if($nickname!=='') $this->model->nickname = $nickname;
+			if($mobile!=='') $this->model->mobile = $mobile;
+			if($email!=='') $this->model->email = $email;
+			if($avatar!=='') $this->model->avatar = $avatar;
+			if($gender!=='') $this->model->gender = $gender;
+			if($birthday!=='') $this->model->birthday = $birthday;
+			 $this->model->updatetime = time();
+			$this->model->save();
+            Db::commit();
+        } catch (PDOException|Exception $e) {
+            Db::rollback();
+            $this->setError($e->getMessage());
+            return false;
+        }
+        return true;
+    }
 }

+ 0 - 1
app/common/model/User.php

@@ -12,7 +12,6 @@ class User extends Model
 
     protected $createTime = 'createtime';
     protected $updateTime = 'updatetime';
-
     public function getAvatarAttr($value)
     {
         return full_url(htmlspecialchars_decode($value), true, Config::get('buildadmin.default_avatar'));