UserController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace api\teahouse\controller;
  3. use api\common\Http;
  4. use app\teahouse\model\LockBookModel;
  5. use app\teahouse\model\LockModel;
  6. use app\teahouse\model\LockUserModel;
  7. use cmf\controller\RestBaseController;
  8. class UserController extends RestBaseController
  9. {
  10. public function checkQualification()
  11. {
  12. $user_id = $this->getUserId();
  13. $user = LockUserModel::where('user_id', $user_id)->find();
  14. if (!empty($user) && $user['status'] == 2) {
  15. $this->success();
  16. }
  17. $this->error('');
  18. }
  19. public function getUserInfo()
  20. {
  21. $user_id = $this->getUserId();
  22. $user = LockUserModel::where('user_id', $user_id)->find();
  23. $this->success('', $user);
  24. }
  25. public function editUserInfo()
  26. {
  27. $user_id = $this->getUserId();
  28. $info = $this->request->post();
  29. $user = LockUserModel::where('user_id', $user_id)->find();
  30. if (empty($user)) {
  31. $info['user_id'] = $user_id;
  32. $info['create_time'] = time();
  33. LockUserModel::create($info);
  34. } else {
  35. $info['status'] = 1;
  36. LockUserModel::where('user_id', $user_id)->update($info);
  37. }
  38. $this->success();
  39. }
  40. public function getBookList()
  41. {
  42. $param = $this->request->param();
  43. $page = empty($param['page']) ? 1 : $param['page'];
  44. $size = empty($param['size']) ? 10 : $param['size'];
  45. //搜索条件
  46. $list = LockBookModel::where('user_id', $this->getUserId())
  47. ->page($page, $size)
  48. ->order('id desc')
  49. ->select();
  50. if (!$list->isEmpty()) {
  51. $time = time();
  52. foreach ($list as $v) {
  53. if ($v['start_time'] > $time) {
  54. $v['status'] = 1;
  55. $v['status_name'] = '未开始';
  56. } elseif ($v['start_time'] <= $time && $v['end_time'] >= $time) {
  57. $v['status'] = 2;
  58. $v['status_name'] = '进行中';
  59. } else {
  60. if ($v['is_use'] == 1) {
  61. $v['status'] = 3;
  62. $v['status_name'] = '已完成';
  63. } else {
  64. $v['status'] = 4;
  65. $v['status_name'] = '爽约';
  66. }
  67. }
  68. $v['date'] = date('Y年m月d日', $v['start_time']);
  69. $v['time'] = date('H:i', $v['start_time']) . ' - ' . date('H:i', $v['end_time']);
  70. }
  71. }
  72. $this->success('', $list);
  73. }
  74. public function showPassword()
  75. {
  76. $id = $this->request->param('id');
  77. $book = LockBookModel::where('id', $id)->where('user_id', $this->getUserId())->find();
  78. if (empty($book)) {
  79. $this->error('该预约不存在');
  80. }
  81. $time = time();
  82. if ($time < $book['start_time']) {
  83. $this->error('预约未开始');
  84. }
  85. if ($time > $book['end_time']) {
  86. $this->error('预约已结束');
  87. }
  88. if (!empty($book['password'])) {
  89. $this->success('成功', ['password' => $book['password']]);
  90. }
  91. $password = $this->_getLockPassword($id);
  92. $this->success('成功', ['password' => $password]);
  93. }
  94. public function refreshPassword()
  95. {
  96. $id = $this->request->param('id');
  97. $book = LockBookModel::where('id', $id)->where('user_id', $this->getUserId())->find();
  98. if (empty($book)) {
  99. $this->error('该预约不存在');
  100. }
  101. $time = time();
  102. if ($time < $book['start_time']) {
  103. $this->error('预约未开始');
  104. }
  105. if ($time > $book['end_time']) {
  106. $this->error('预约已结束');
  107. }
  108. $password = $this->_getLockPassword($id);
  109. $this->success('成功', ['password' => $password]);
  110. }
  111. private function _getLockPassword($id)
  112. {
  113. $book = LockBookModel::where('id', $id)->where('user_id', $this->getUserId())->find();
  114. if (empty($book)) {
  115. $this->error('该预约不存在');
  116. }
  117. //锁信息
  118. $lock = LockModel::find($book['lock_id']);
  119. if (empty($lock) || empty($lock['lock_id'])) {
  120. $this->error('锁不存在或配置有误,请联系管理员');
  121. }
  122. $teahouse_setting = cmf_get_option('teahouse_setting');
  123. //获取锁密码的基础信息
  124. $lock_id = $lock['lock_id'];
  125. $token = $this->_getLockToken();
  126. $clientId = $teahouse_setting['clientId'];
  127. $start_time = $book['start_time'];
  128. $end_time = $book['end_time'];
  129. $now = time();
  130. //获取锁密码
  131. $url = "https://cnapi.sciener.com/v3/keyboardPwd/get";
  132. $data = [
  133. 'clientId' => $clientId,
  134. 'accessToken' => $token,
  135. 'lockId' => $lock_id,
  136. 'keyboardPwdType' => 1,
  137. 'startDate' => $start_time * 1000,
  138. 'endDate' => $end_time * 1000,
  139. 'date' => $now * 1000,
  140. ];
  141. $header = ['Content-Type: application/x-www-form-urlencoded'];
  142. $result = Http::http_post_json($url, $data, $header);
  143. if (!empty($result['errcode'])) {
  144. $this->error('获取密码失败,请联系管理员。' . $result['description']);
  145. }
  146. //保存密码
  147. $book->password = $result['keyboardPwd'];
  148. $book->save();
  149. LockBookModel::where('relation_id', $book['relation_id'])->update(['is_use' => 1]);
  150. return $result['keyboardPwd'];
  151. }
  152. private function _getLockToken()
  153. {
  154. $teahouse_token = cmf_get_option('teahouse_token');
  155. if (empty($teahouse_token)) {
  156. return $this->_getLockTokenFirst();
  157. }
  158. $teahouse_token = cmf_get_option('teahouse_token');
  159. if ($teahouse_token['expires_in'] < time()) {
  160. return $this->_getLockTokenByRefresh();
  161. }
  162. return $teahouse_token['access_token'];
  163. }
  164. private function _getLockTokenFirst()
  165. {
  166. $url = "https://cnapi.sciener.com/oauth2/token";
  167. $teahouse_setting = cmf_get_option('teahouse_setting');
  168. if (empty($teahouse_setting)) {
  169. $this->error('门锁配置错误,请联系管理员');
  170. }
  171. $data = [
  172. 'clientId' => $teahouse_setting['clientId'],
  173. 'clientSecret' => $teahouse_setting['clientSecret'],
  174. 'username' => $teahouse_setting['username'],
  175. 'password' => md5($teahouse_setting['password']),
  176. ];
  177. $header = ['Content-Type: application/x-www-form-urlencoded'];
  178. $result = Http::http_post_json($url, $data, $header);
  179. if (!empty($result['errcode'])) {
  180. $this->error('门锁配置错误,请联系管理员。' . $result['description']);
  181. }
  182. cmf_set_option('teahouse_token', $result);
  183. return $result['access_token'];
  184. }
  185. private function _getLockTokenByRefresh()
  186. {
  187. $url = "https://cnapi.sciener.com/oauth2/token";
  188. $teahouse_setting = cmf_get_option('teahouse_setting');
  189. if (empty($teahouse_setting)) {
  190. $this->error('门锁配置错误,请联系管理员');
  191. }
  192. $teahouse_token = cmf_get_option('teahouse_token');
  193. $data = [
  194. 'clientId' => $teahouse_setting['clientId'],
  195. 'clientSecret' => $teahouse_setting['clientSecret'],
  196. 'grant_type' => 'refresh_token',
  197. 'refresh_token' => $teahouse_token['refresh_token'],
  198. ];
  199. $header = ['Content-Type: application/x-www-form-urlencoded'];
  200. $result = Http::http_post_json($url, $data, $header);
  201. if (!empty($result['errcode'])) {
  202. $this->error('门锁配置错误,请联系管理员。' . $result['description']);
  203. }
  204. cmf_set_option('teahouse_token', $result);
  205. return $result['access_token'];
  206. }
  207. }