|
@@ -17,7 +17,7 @@ use think\Db;
|
|
*/
|
|
*/
|
|
class User extends Apic
|
|
class User extends Apic
|
|
{
|
|
{
|
|
- protected $noNeedLogin = ['accountlogin','resetpwd'];
|
|
|
|
|
|
+ protected $noNeedLogin = ['accountlogin','resetpwd','getUserOpenid'];
|
|
protected $noNeedRight = '*';
|
|
protected $noNeedRight = '*';
|
|
|
|
|
|
|
|
|
|
@@ -25,10 +25,11 @@ class User extends Apic
|
|
public function accountlogin(){
|
|
public function accountlogin(){
|
|
$mobile = $this->request->post('mobile');
|
|
$mobile = $this->request->post('mobile');
|
|
$password = $this->request->post('password');
|
|
$password = $this->request->post('password');
|
|
- if (!$mobile || !$password) {
|
|
|
|
|
|
+ $openid = $this->request->post('openid','');
|
|
|
|
+ if (!$mobile || !$password || !$openid) {
|
|
$this->error(__('Invalid parameters'));
|
|
$this->error(__('Invalid parameters'));
|
|
}
|
|
}
|
|
- $ret = $this->auth->login($mobile, $password);
|
|
|
|
|
|
+ $ret = $this->auth->login($mobile, $password, $openid);
|
|
if ($ret) {
|
|
if ($ret) {
|
|
$data = $this->auth->getUserinfo();
|
|
$data = $this->auth->getUserinfo();
|
|
$this->success(__('Logged in successful'), $data);
|
|
$this->success(__('Logged in successful'), $data);
|
|
@@ -212,4 +213,60 @@ class User extends Apic
|
|
$this->error($e->getMessage());
|
|
$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);
|
|
|
|
+ }
|
|
}
|
|
}
|