lizhen_gitee 1 anno fa
parent
commit
8c57a4798a

+ 2 - 2
application/admin/view/company/add.html

@@ -139,13 +139,13 @@
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Open_hours')}:</label>
         <div class="col-xs-12 col-sm-8">
-            <input id="c-open_hours" data-rule="required" class="form-control" name="row[open_hours]" type="text" value="">
+            <input id="c-open_hours" class="form-control" name="row[open_hours]" type="text" value="">
         </div>
     </div>
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Agent_id')}:</label>
         <div class="col-xs-12 col-sm-8">
-            <input id="c-agent_id" data-rule="required" data-source="agent/index" class="form-control selectpage" name="row[agent_id]" type="text" value="">
+            <input id="c-agent_id" data-source="agent/index" class="form-control selectpage" name="row[agent_id]" type="text" value="">
         </div>
     </div>
     <!--<div class="form-group">

+ 60 - 3
application/api/controller/company/User.php

@@ -17,7 +17,7 @@ use think\Db;
  */
 class User extends Apic
 {
-    protected $noNeedLogin = ['accountlogin','resetpwd'];
+    protected $noNeedLogin = ['accountlogin','resetpwd','getUserOpenid'];
     protected $noNeedRight = '*';
 
 
@@ -25,10 +25,11 @@ class User extends Apic
     public function accountlogin(){
         $mobile   = $this->request->post('mobile');
         $password = $this->request->post('password');
-        if (!$mobile || !$password) {
+        $openid = $this->request->post('openid','');
+        if (!$mobile || !$password || !$openid) {
             $this->error(__('Invalid parameters'));
         }
-        $ret = $this->auth->login($mobile, $password);
+        $ret = $this->auth->login($mobile, $password, $openid);
         if ($ret) {
             $data = $this->auth->getUserinfo();
             $this->success(__('Logged in successful'), $data);
@@ -212,4 +213,60 @@ class User extends Apic
             $this->error($e->getMessage());
         }
     }
+
+    /**
+     * 获取用户openid
+     */
+    public function getUserOpenid() {
+        // code值
+        $code = $this->request->param('code');
+        if (!$code) {
+            $this->error(__('Invalid parameters'));
+        }
+
+        $config = config('company_wxMiniProgram');
+        $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
+        $openidInfo = $this->getJson($getopenid);
+        if(!isset($openidInfo['openid'])) {
+            $this->error('用户openid获取失败',$openidInfo);
+        }
+        //  获取的结果存入数据库
+        /*$find = Db::name('company_sessionkey')->where(['openid'=>$openidInfo['openid']])->find();
+        if($find) {
+            $update = [];
+            $update['sessionkey'] = $openidInfo['session_key'];
+            $update['createtime'] = time();
+            $res = Db::name('company_sessionkey')->where(['openid'=>$openidInfo['openid']])->update($update);
+        } else {
+            $insert = [];
+            $insert['sessionkey'] = $openidInfo['session_key'];
+            $insert['openid'] = $openidInfo['openid'];
+            $insert['unionid'] = isset($openidInfo['unionid']) ? $openidInfo['unionid'] : '';
+            $insert['createtime'] = time();
+            $res = Db::name('company_sessionkey')->insertGetId($insert);
+        }*/
+
+        if(!empty($openidInfo)) {
+            $this->success('获取成功',$openidInfo);
+        } else {
+            $this->error('获取失败');
+        }
+
+    }
+
+    /**
+     * json 请求
+     * @param $url
+     * @return mixed
+     */
+    private function getJson($url){
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        $output = curl_exec($ch);
+        curl_close($ch);
+        return json_decode($output, true);
+    }
 }

+ 9 - 3
application/common/library/Authcompany.php

@@ -157,7 +157,7 @@ class Authcompany
      * @param string $password 密码
      * @return boolean
      */
-    public function login($account, $password)
+    public function login($account, $password, $openid)
     {
         $field = 'mobile';
         $user = CompanyStaff::get([$field => $account]);
@@ -176,7 +176,7 @@ class Authcompany
         }
 
         //直接登录员工
-        return $this->direct($user->id);
+        return $this->direct($user->id,$openid);
     }
 
     /**
@@ -233,7 +233,7 @@ class Authcompany
      * @param int $user_id
      * @return boolean
      */
-    public function direct($staff_id)
+    public function direct($staff_id,$openid = '')
     {
         $user = CompanyStaff::get($staff_id);
         if ($user) {
@@ -252,6 +252,12 @@ class Authcompany
             Db::startTrans();
             try {
 
+                if(!empty($openid)){
+                    $user->openid = $openid;
+                    $user->save();
+                }
+                $user->openid = '';
+
 				$user->company = $companyinfo;// 追加公司
 
                 $this->_user = $user;

+ 2 - 0
application/config.php

@@ -316,6 +316,8 @@ return [
     //商家端小程序,鲁享途中
     'company_wxMiniProgram' => [
         'appid'=>'wx9a7153e5b40d9663',
+//        'appid'=>'wx4cbb35e016ab6671',
         'secret'=>'8a743955faa5873af394f31d4a58c086',
+//        'secret'=>'cd448b0740ae8e38ea7e2bcd96c6cdfd',
     ],
 ];