request->post('code'); $url = self::CODE_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] . '&js_code=' . $code . '&grant_type=authorization_code'; $header = ['content-type: application/json']; $result = Http::http_post_json($url, [], $header); if (!empty($result['errcode'])) { $this->error($result['errmsg']); } //获取token $user = UserModel::where('open_id', $result['openid'])->find(); if (empty($user)) { $user = UserModel::create([ 'user_pass' => cmf_password('123456'), 'open_id' => $result['openid'], 'union_id' => $result['unionid'], 'user_type' => 2, 'create_time' => time(), ]); } if (empty($user['unionid'])) { $user->union_id = $result['unionid']; $user->save(); } //写入token $token = md5(uniqid()) . md5(uniqid()); $currentTime = time(); $expireTime = $currentTime + 24 * 3600 * 180; Db::name("user_token")->insert([ 'token' => $token, 'user_id' => $user['id'], 'expire_time' => $expireTime, 'create_time' => $currentTime, 'device_type' => $this->deviceType, ]); $this->success('OK',['token' => $token, 'expires_time' => $expireTime]); } public function test() { /*$url = 'https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token={$token}'; $data = '{"rid":1}'; $header = ['content-type: application/json']; $result = Http::http_post_json($url, $data, $header); halt($result);*/ $url = "https://cnapi.sciener.com/oauth2/token"; $data = [ 'clientId' => '3280d286853f4588addae034f855333e', 'clientSecret' => 'b1a083bbd1bda2343828deddaa579f19', 'username' => '+86159607153161', 'password' => md5('qwe123456'), ]; $header = ['Content-Type: application/x-www-form-urlencoded']; $result = Http::http_post_json($url, $data, $header); halt($result); } public function t2() { $start_time = strtotime(date('Y-m-d H:00:00')); $end_time = strtotime(date('Y-m-d H:00:00').' +1 hours'); $now = time(); $url = "https://cnapi.sciener.com/v3/keyboardPwd/get"; $data = [ 'clientId' => '3280d286853f4588addae034f855333e', 'accessToken' => 'd2abf38a91bc0238796043bb02686d34', 'lockId' => 19406485, 'keyboardPwdType' => 1, 'startDate' => $start_time * 1000, 'endDate' => $end_time * 1000, 'date' => $now * 1000, ]; $header = ['Content-Type: application/x-www-form-urlencoded']; $result = Http::http_post_json($url, $data, $header); halt($result); } public function _getAccessToken() { $time = time(); $wx_access_token = cmf_get_option('wx_access_token'); if (empty($wx_access_token) || $wx_access_token['expires_time'] < $time) { $wxappSettings = cmf_get_option('wxapp_settings')['default']; $url = self::TOKEN_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] .'&grant_type=client_credential'; $header = ['content-type: application/json']; $wx_access_token = Http::http_post_json($url, '', $header); $wx_access_token['expires_time'] = $time + $wx_access_token['expires_in']; cmf_set_option('wx_access_token',$wx_access_token); } return $wx_access_token['access_token']; } }