Wechat.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\wechat;
  12. use app\service\api\wechat\WechatConfigService;
  13. use app\service\api\wechat\WechatAuthService;
  14. use core\base\BaseController;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\Response;
  19. class Wechat extends BaseController
  20. {
  21. //todo csrf 验证也需要
  22. /**
  23. * 获取跳转获取code
  24. * @return Response
  25. */
  26. public function getCodeUrl(){
  27. $data = $this->request->params([
  28. ['url', ''],
  29. ['scopes', '']
  30. ]);
  31. $wechat_auth_service = new WechatAuthService();
  32. return success($wechat_auth_service->authorization($data['url'], $data['scopes']));
  33. }
  34. /**
  35. * code获取微信信息
  36. * @return Response
  37. */
  38. public function getWechatUser()
  39. {
  40. $data = $this->request->params([
  41. ['code', ''],
  42. ]);
  43. $wechat_auth_service = new WechatAuthService();
  44. $data = $wechat_auth_service->userFromCode($data['code']);
  45. return success([ 'data' => json_encode($data) ]);
  46. }
  47. /**
  48. * 授权信息登录
  49. * @return Response
  50. */
  51. public function wechatLogin()
  52. {
  53. $data = $this->request->params([
  54. ['data', ''],
  55. ]);
  56. $wechat_auth_service = new WechatAuthService();
  57. [ $avatar, $nickname, $openid, $unionid ] = json_decode($data['data'], true);
  58. return success($wechat_auth_service->login($openid, $nickname, $avatar, $unionid));
  59. }
  60. /**
  61. * 授权登录
  62. * @return Response
  63. * @throws DataNotFoundException
  64. * @throws DbException
  65. * @throws ModelNotFoundException
  66. */
  67. public function login(){
  68. $data = $this->request->params([
  69. ['code', ''],
  70. ]);
  71. $wechat_auth_service = new WechatAuthService();
  72. return success($wechat_auth_service->loginByCode($data['code']));
  73. }
  74. /**
  75. * 注册
  76. * @return Response
  77. */
  78. public function register(){
  79. $data = $this->request->params([
  80. ['openid', ''],
  81. ['unionid', ''],
  82. ['mobile', ''],
  83. ]);
  84. //参数验证
  85. $this->validate($data, [
  86. 'mobile' => 'mobile'
  87. ]);
  88. $wechat_auth_service = new WechatAuthService();
  89. return success($wechat_auth_service->register($data['openid'], $data['mobile'], wx_unionid: $data['unionid']));
  90. }
  91. /**
  92. * 同步
  93. * @return Response
  94. */
  95. public function sync(){
  96. $data = $this->request->params([
  97. ['code', ''],
  98. ]);
  99. $wechat_auth_service = new WechatAuthService();
  100. return success($wechat_auth_service->sync($data['code']));
  101. }
  102. /**
  103. * 获取jssdk config
  104. * @return Response
  105. */
  106. public function jssdkConfig(){
  107. $data = $this->request->params([
  108. ['url', ''],
  109. ]);
  110. $wechat_auth_service = new WechatAuthService();
  111. return success($wechat_auth_service->jssdkConfig($data['url']));
  112. }
  113. /**
  114. * 扫码登录
  115. * @return Response
  116. */
  117. public function scanLogin(){
  118. $wechat_auth_service = new WechatAuthService();
  119. return success($wechat_auth_service->scanLogin());
  120. }
  121. /**
  122. * 检查微信公众号是否配置
  123. * @return Response
  124. */
  125. public function checkWechatConfig()
  126. {
  127. return success('SUCCESS', (new WechatConfigService())->checkWechatConfig());
  128. }
  129. /**
  130. * 更新openid
  131. * @return Response
  132. */
  133. public function updateOpenid()
  134. {
  135. $data = $this->request->params([ [ 'code', '' ] ]);
  136. $wechat_auth_service = new WechatAuthService();
  137. return success($wechat_auth_service->updateOpenid($data[ 'code' ]));
  138. }
  139. }