123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace api\applet\controller;
- use api\applet\model\UserModel;
- use api\common\Http;
- use cmf\controller\RestBaseController;
- use think\Db;
- class AppletController extends RestBaseController
- {
- const TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token'; //get
- const APPLET_CODE_URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit'; //post
- const CODE_URL = 'https://api.weixin.qq.com/sns/jscode2session'; //post
- /**
- * 根据code登录
- * @param $code
- */
- public function login()
- {
- $wxappSettings = cmf_get_option('wxapp_settings')['default'];
- //获取open_id
- $code = $this->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'];
- }
- }
|