123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace api\teahouse\controller;
- use api\common\Http;
- use app\teahouse\model\LockBookModel;
- use app\teahouse\model\LockModel;
- use app\teahouse\model\LockUserModel;
- use cmf\controller\RestBaseController;
- class UserController extends RestBaseController
- {
- public function checkQualification()
- {
- $user_id = $this->getUserId();
- $user = LockUserModel::where('user_id', $user_id)->find();
- if (!empty($user) && $user['status'] == 2) {
- $this->success();
- }
- $this->error('');
- }
- public function getUserInfo()
- {
- $user_id = $this->getUserId();
- $user = LockUserModel::where('user_id', $user_id)->find();
- $this->success('', $user);
- }
- public function editUserInfo()
- {
- $user_id = $this->getUserId();
- $info = $this->request->post();
- $user = LockUserModel::where('user_id', $user_id)->find();
- if (empty($user)) {
- $info['user_id'] = $user_id;
- $info['create_time'] = time();
- LockUserModel::create($info);
- } else {
- $info['status'] = 1;
- LockUserModel::where('user_id', $user_id)->update($info);
- }
- $this->success();
- }
- public function getBookList()
- {
- $param = $this->request->param();
- $page = empty($param['page']) ? 1 : $param['page'];
- $size = empty($param['size']) ? 10 : $param['size'];
- //搜索条件
- $list = LockBookModel::where('user_id', $this->getUserId())
- ->page($page, $size)
- ->order('id desc')
- ->select();
- if (!$list->isEmpty()) {
- $time = time();
- foreach ($list as $v) {
- if ($v['start_time'] > $time) {
- $v['status'] = 1;
- $v['status_name'] = '未开始';
- } elseif ($v['start_time'] <= $time && $v['end_time'] >= $time) {
- $v['status'] = 2;
- $v['status_name'] = '进行中';
- } else {
- if ($v['is_use'] == 1) {
- $v['status'] = 3;
- $v['status_name'] = '已完成';
- } else {
- $v['status'] = 4;
- $v['status_name'] = '爽约';
- }
- }
- $v['date'] = date('Y年m月d日', $v['start_time']);
- $v['time'] = date('H:i', $v['start_time']) . ' - ' . date('H:i', $v['end_time']);
- }
- }
- $this->success('', $list);
- }
- public function showPassword()
- {
- $id = $this->request->param('id');
- $book = LockBookModel::where('id', $id)->where('user_id', $this->getUserId())->find();
- if (empty($book)) {
- $this->error('该预约不存在');
- }
- $time = time();
- if ($time < $book['start_time']) {
- $this->error('预约未开始');
- }
- if ($time > $book['end_time']) {
- $this->error('预约已结束');
- }
- if (!empty($book['password'])) {
- $this->success('成功', ['password' => $book['password']]);
- }
- $password = $this->_getLockPassword($id);
- $this->success('成功', ['password' => $password]);
- }
- public function refreshPassword()
- {
- $id = $this->request->param('id');
- $book = LockBookModel::where('id', $id)->where('user_id', $this->getUserId())->find();
- if (empty($book)) {
- $this->error('该预约不存在');
- }
- $time = time();
- if ($time < $book['start_time']) {
- $this->error('预约未开始');
- }
- if ($time > $book['end_time']) {
- $this->error('预约已结束');
- }
- $password = $this->_getLockPassword($id);
- $this->success('成功', ['password' => $password]);
- }
- private function _getLockPassword($id)
- {
- $book = LockBookModel::where('id', $id)->where('user_id', $this->getUserId())->find();
- if (empty($book)) {
- $this->error('该预约不存在');
- }
- //锁信息
- $lock = LockModel::find($book['lock_id']);
- if (empty($lock) || empty($lock['lock_id'])) {
- $this->error('锁不存在或配置有误,请联系管理员');
- }
- $teahouse_setting = cmf_get_option('teahouse_setting');
- //获取锁密码的基础信息
- $lock_id = $lock['lock_id'];
- $token = $this->_getLockToken();
- $clientId = $teahouse_setting['clientId'];
- $start_time = $book['start_time'];
- $end_time = $book['end_time'];
- $now = time();
- //获取锁密码
- $url = "https://cnapi.sciener.com/v3/keyboardPwd/get";
- $data = [
- 'clientId' => $clientId,
- 'accessToken' => $token,
- 'lockId' => $lock_id,
- '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);
- if (!empty($result['errcode'])) {
- $this->error('获取密码失败,请联系管理员。' . $result['description']);
- }
- //保存密码
- $book->password = $result['keyboardPwd'];
- $book->save();
- LockBookModel::where('relation_id', $book['relation_id'])->update(['is_use' => 1]);
- return $result['keyboardPwd'];
- }
- private function _getLockToken()
- {
- $teahouse_token = cmf_get_option('teahouse_token');
- if (empty($teahouse_token)) {
- return $this->_getLockTokenFirst();
- }
- $teahouse_token = cmf_get_option('teahouse_token');
- if ($teahouse_token['expires_in'] < time()) {
- return $this->_getLockTokenByRefresh();
- }
- return $teahouse_token['access_token'];
- }
- private function _getLockTokenFirst()
- {
- $url = "https://cnapi.sciener.com/oauth2/token";
- $teahouse_setting = cmf_get_option('teahouse_setting');
- if (empty($teahouse_setting)) {
- $this->error('门锁配置错误,请联系管理员');
- }
- $data = [
- 'clientId' => $teahouse_setting['clientId'],
- 'clientSecret' => $teahouse_setting['clientSecret'],
- 'username' => $teahouse_setting['username'],
- 'password' => md5($teahouse_setting['password']),
- ];
- $header = ['Content-Type: application/x-www-form-urlencoded'];
- $result = Http::http_post_json($url, $data, $header);
- if (!empty($result['errcode'])) {
- $this->error('门锁配置错误,请联系管理员。' . $result['description']);
- }
- cmf_set_option('teahouse_token', $result);
- return $result['access_token'];
- }
- private function _getLockTokenByRefresh()
- {
- $url = "https://cnapi.sciener.com/oauth2/token";
- $teahouse_setting = cmf_get_option('teahouse_setting');
- if (empty($teahouse_setting)) {
- $this->error('门锁配置错误,请联系管理员');
- }
- $teahouse_token = cmf_get_option('teahouse_token');
- $data = [
- 'clientId' => $teahouse_setting['clientId'],
- 'clientSecret' => $teahouse_setting['clientSecret'],
- 'grant_type' => 'refresh_token',
- 'refresh_token' => $teahouse_token['refresh_token'],
- ];
- $header = ['Content-Type: application/x-www-form-urlencoded'];
- $result = Http::http_post_json($url, $data, $header);
- if (!empty($result['errcode'])) {
- $this->error('门锁配置错误,请联系管理员。' . $result['description']);
- }
- cmf_set_option('teahouse_token', $result);
- return $result['access_token'];
- }
- }
|