Miniapp.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 微信统一公共服务接口
  7. */
  8. namespace app\system\controller\api\v1;
  9. use app\system\controller\api\Base;
  10. use app\common\model\SystemMemberMiniapp;
  11. use app\common\model\SystemUser;
  12. use app\common\facade\WechatMp;
  13. use app\common\facade\WechatProgram;
  14. use app\common\facade\Alisms;
  15. use app\common\facade\Upload;
  16. use think\facade\Request;
  17. use Exception;
  18. class Miniapp extends Base{
  19. /**
  20. * 小程序登录接口
  21. * @return void
  22. */
  23. public function login(){
  24. return self::miniappLogin();
  25. }
  26. /**
  27. * 检查应用是否登录
  28. * @return void
  29. */
  30. public function checkLogin(){
  31. if($this->user) {
  32. return enjson(200,'已登录');
  33. } else {
  34. return enjson(401,'未登录');
  35. }
  36. }
  37. /**
  38. * 判断是否应用创始人
  39. * @return void
  40. */
  41. public function isManage(){
  42. if ($this->user) {
  43. $result = SystemMemberMiniapp::field('uid')->where(['id'=>$this->miniapp_id])->find();
  44. if ($result->uid == $this->user->id) {
  45. return enjson(200,'是管理员',['status'=>1]);
  46. }
  47. }
  48. return enjson(204,'非管理员',['status'=>0]);
  49. }
  50. /**
  51. * 获取邀请码的用户信息
  52. * @return void
  53. */
  54. public function getCodeUser(){
  55. return $this->getUCodeUser();
  56. }
  57. /**
  58. * 通过小程序绑定公众号帐号
  59. * @param [type] $ids
  60. * @return void
  61. */
  62. public function bindOfficial(){
  63. $code = Request::param('code');
  64. $app = Request::param('app/d');
  65. $official = WechatMp::isTypes($app);
  66. if(empty($official)){
  67. return view('api/bind_official_error');
  68. }
  69. if(empty($code)){
  70. $response = $official->oauth->scopes(['snsapi_base'])->redirect(api(1,'system/miniapp/bindOfficial',$app));
  71. return $response->send();
  72. }else{
  73. $view['openid'] = $official->oauth->user()->getID();
  74. $view['app'] = $app;
  75. return view('api/bind_official')->assign($view);
  76. }
  77. }
  78. /**
  79. * 绑定微信小程序的公众号
  80. * @return void
  81. */
  82. public function bindWechatPhone(){
  83. $this->isUserAuth();
  84. if (request()->isPost()) {
  85. $param = [];
  86. $param['errMsg'] = Request::param('errMsg','getPhoneNumber:fail');
  87. $param['encryptedData'] = Request::param('encryptedData');
  88. $param['iv'] = Request::param('iv');
  89. $param['sign'] = Request::param('sign');
  90. $rel = $this->apiSign($param);
  91. if($rel['code'] = 200){
  92. try {
  93. $decryptedData = WechatProgram::isTypes($this->miniapp_id)->encryptor->decryptData($this->user->session_key,$param['iv'],$param['encryptedData']);
  94. if(!empty($decryptedData['purePhoneNumber'])){
  95. $phone = $decryptedData['purePhoneNumber'];
  96. if ($this->user->phone_uid == $phone) {
  97. return enjson(403,'手机号相同不用更换');
  98. }
  99. $rel = SystemUser::where(['member_miniapp_id' => $this->miniapp_id,'phone_uid' => $phone])->field('id')->count();
  100. if($rel){
  101. return enjson(403,'手机号已被占用');
  102. }
  103. //验证码通过
  104. $result = SystemUser::where(['id' =>$this->user->id])->update(['phone_uid' => $phone]);
  105. if ($result) {
  106. return enjson(200,'手机号绑定成功',['phone_uid' => $phone]);
  107. }
  108. return enjson(403,'手机号绑定失败');
  109. }
  110. }catch (Exception $e) {
  111. return enjson(401,'请先登录帐号');
  112. }
  113. }else{
  114. return enjson($rel['code'],$rel['msg']);
  115. }
  116. }
  117. }
  118. /**
  119. * 获取绑定手机验证码
  120. * @return void
  121. */
  122. public function getBindWechatPhoneSms(){
  123. $this->isUserAuth();
  124. if (request()->isPost()) {
  125. $param = [];
  126. $param['errMsg'] = Request::param('errMsg','getPhoneNumber:fail');
  127. $param['encryptedData'] = Request::param('encryptedData');
  128. $param['iv'] = Request::param('iv');
  129. $param['sign'] = Request::param('sign');
  130. $rel = $this->apiSign($param);
  131. if($rel['code'] != 200){
  132. return enjson($rel['code'],$rel['msg']);
  133. }
  134. $decryptedData = WechatProgram::isTypes($this->miniapp_id)->encryptor->decryptData($this->user->session_key,$param['iv'],$param['encryptedData']);
  135. if(!empty($decryptedData['purePhoneNumber'])){
  136. $phone = $decryptedData['purePhoneNumber'];
  137. if ($this->user->phone_uid != $phone) {
  138. return enjson(403,'手机号验证失败');
  139. }
  140. $data['phone_id'] = $this->user->phone_uid;
  141. $data['code'] = getcode(6);
  142. Alisms::setSms($data);
  143. return enjson(200,'验证成功',['session_id' =>session_id(),'sms' => $data['code']]);
  144. }
  145. }
  146. }
  147. /**
  148. * 商城图片上传图片
  149. * @return void
  150. */
  151. public function upload(){
  152. $this->isUserAuth();
  153. if(request()->isPost()){
  154. $rel = Upload::index(strtolower(create_code($this->miniapp_id)).DS.'user');
  155. if($rel['error'] == 0){
  156. return enjson(200,$rel['url']);
  157. }else{
  158. return enjson(204);
  159. }
  160. }
  161. }
  162. }