AppletController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace api\applet\controller;
  3. use api\applet\model\UserModel;
  4. use api\common\Http;
  5. use cmf\controller\RestBaseController;
  6. use think\Db;
  7. class AppletController extends RestBaseController
  8. {
  9. const TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token'; //get
  10. const APPLET_CODE_URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit'; //post
  11. const CODE_URL = 'https://api.weixin.qq.com/sns/jscode2session'; //post
  12. /**
  13. * 根据code登录
  14. * @param $code
  15. */
  16. public function login()
  17. {
  18. $wxappSettings = cmf_get_option('wxapp_settings')['default'];
  19. //获取open_id
  20. $code = $this->request->post('code');
  21. $url = self::CODE_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] . '&js_code=' . $code . '&grant_type=authorization_code';
  22. $header = ['content-type: application/json'];
  23. $result = Http::http_post_json($url, [], $header);
  24. if (!empty($result['errcode'])) {
  25. $this->error($result['errmsg']);
  26. }
  27. //获取token
  28. $user = UserModel::where('open_id', $result['openid'])->find();
  29. if (empty($user)) {
  30. $user = UserModel::create([
  31. 'user_pass' => cmf_password('123456'),
  32. 'open_id' => $result['openid'],
  33. 'union_id' => $result['unionid'],
  34. 'user_type' => 2,
  35. 'create_time' => time(),
  36. ]);
  37. }
  38. if (empty($user['unionid'])) {
  39. $user->union_id = $result['unionid'];
  40. $user->save();
  41. }
  42. //写入token
  43. $token = md5(uniqid()) . md5(uniqid());
  44. $currentTime = time();
  45. $expireTime = $currentTime + 24 * 3600 * 180;
  46. Db::name("user_token")->insert([
  47. 'token' => $token,
  48. 'user_id' => $user['id'],
  49. 'expire_time' => $expireTime,
  50. 'create_time' => $currentTime,
  51. 'device_type' => $this->deviceType,
  52. ]);
  53. $this->success('OK',['token' => $token, 'expires_time' => $expireTime]);
  54. }
  55. public function test()
  56. {
  57. /*$url = 'https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token={$token}';
  58. $data = '{"rid":1}';
  59. $header = ['content-type: application/json'];
  60. $result = Http::http_post_json($url, $data, $header);
  61. halt($result);*/
  62. $url = "https://cnapi.sciener.com/oauth2/token";
  63. $data = [
  64. 'clientId' => '3280d286853f4588addae034f855333e',
  65. 'clientSecret' => 'b1a083bbd1bda2343828deddaa579f19',
  66. 'username' => '+86159607153161',
  67. 'password' => md5('qwe123456'),
  68. ];
  69. $header = ['Content-Type: application/x-www-form-urlencoded'];
  70. $result = Http::http_post_json($url, $data, $header);
  71. halt($result);
  72. }
  73. public function t2()
  74. {
  75. $start_time = strtotime(date('Y-m-d H:00:00'));
  76. $end_time = strtotime(date('Y-m-d H:00:00').' +1 hours');
  77. $now = time();
  78. $url = "https://cnapi.sciener.com/v3/keyboardPwd/get";
  79. $data = [
  80. 'clientId' => '3280d286853f4588addae034f855333e',
  81. 'accessToken' => 'd2abf38a91bc0238796043bb02686d34',
  82. 'lockId' => 19406485,
  83. 'keyboardPwdType' => 1,
  84. 'startDate' => $start_time * 1000,
  85. 'endDate' => $end_time * 1000,
  86. 'date' => $now * 1000,
  87. ];
  88. $header = ['Content-Type: application/x-www-form-urlencoded'];
  89. $result = Http::http_post_json($url, $data, $header);
  90. halt($result);
  91. }
  92. public function _getAccessToken()
  93. {
  94. $time = time();
  95. $wx_access_token = cmf_get_option('wx_access_token');
  96. if (empty($wx_access_token) || $wx_access_token['expires_time'] < $time) {
  97. $wxappSettings = cmf_get_option('wxapp_settings')['default'];
  98. $url = self::TOKEN_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] .'&grant_type=client_credential';
  99. $header = ['content-type: application/json'];
  100. $wx_access_token = Http::http_post_json($url, '', $header);
  101. $wx_access_token['expires_time'] = $time + $wx_access_token['expires_in'];
  102. cmf_set_option('wx_access_token',$wx_access_token);
  103. }
  104. return $wx_access_token['access_token'];
  105. }
  106. }