| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | <?phpnamespace app\api\controller\third;use think\Db;use think\exception\HttpResponseException;use app\api\controller\Base;use app\common\Service\Third\Wechat\Wechat as WechatService;class Wechat extends Base{    protected $noNeedLogin = ['login', 'getSessionId', 'oauthLogin', 'jssdk', 'wxacode', 'subscribeTemplate'];    protected $noNeedRight = ['*'];    protected $payload = [];    protected $wechat;    protected $platform;    public function _initialize()    {        parent::_initialize();        $this->platform = $this->request->header('platform', '');        if ($this->platform === '') {            $this->error('参数错误');        }        $this->payload = $this->request->post('payload/a', '') ?? [];        $this->wechat = new WechatService($this->platform, $this->payload);    }    // 微信登陆(小程序+公众号+开放平台)    public function login()    {        $result = Db::transaction(function () {            return $this->wechat->login();        });        if ($result) {            $this->success('登陆成功',$result);        }        $this->error('登陆失败',$result);    }    // 获取小程序sessionId+自动登录    public function getSessionId()    {        $result = $this->wechat->getSessionId();        $this->success('', $result);    }    // 获取网页授权地址    public function oauthLogin()    {        $result = $this->wechat->oauthLogin();        if (isset($result['login_url'])) {            $this->success('', $result);        }        if (isset($result['redirect_url'])) {            return redirect($result['redirect_url']);        }    }    // 绑定用户手机号    public function bindUserPhoneNumber()    {        $result = Db::transaction(function () {            $user = auth_user();            $mobile = $this->wechat->getUserPhoneNumber();            $this->validate(['mobile' => $mobile], '.bindWechatMiniProgramMobile');            $user->mobile = $mobile;            $verification = $user->verification;            $verification->mobile = 1;            $user->verification = $verification;            return $user->save();        });        if ($result) {            $this->success('绑定成功');        }        $this->error('操作失败');    }    // 绑定微信账号    public function bind()    {        $result = Db::transaction(function () {            $user = auth_user();            return $this->wechat->bind($user);        });        if ($result) {            $this->success('绑定成功');        }        $this->error('绑定失败');    }    // 解绑微信账号    public function unbind()    {        $result = Db::transaction(function () {            return $this->wechat->unbind();        });        if ($result) {            $this->success('解绑成功');        }        $this->error('解绑失败');    }    // 微信网页jssdk    public function jssdk()    {        $apis = [            'checkJsApi',            'updateTimelineShareData',            'updateAppMessageShareData',            'getLocation', //获取位置            'openLocation', //打开位置            'scanQRCode', //扫一扫接口            'chooseWXPay', //微信支付            'chooseImage', //拍照或从手机相册中选图接口            'previewImage', //预览图片接口       'uploadImage', //上传图片            'openAddress',   // 获取微信地址            'requestMerchantTransfer', // 微信商家转账        ];        // $openTagList = [        //     'wx-open-subscribe'        // ];        try {            $data = $this->wechat->jssdk($apis);        } catch (HttpResponseException $e) {            $data = $e->getResponse()->getData();            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();            $this->error($message);        } catch (\Exception $e) {            $this->error($e->getMessage());        }        $this->success('jssdkApi', $data);    }    /**     * 微信小程序码接口     */    public function wxacode()    {        $mp = $this->wechat->getApp();        $path = $this->payload['path'];                // 这里要处理下 如果没有穿$path  默认 pages/me/edit  spm的参数是         list($page, $scene) = explode('?', $path);        $content = $mp->app_code->getUnlimit($scene, [            'page' => substr($page, 1),            'is_hyaline' => true,            'env_version' => 'trial',            'check_path' => false            //'env_version' => 'trial'            //'env_version' => 'develop'            //'env_version' => 'release'        ]);              if ($content instanceof \EasyWeChat\Kernel\Http\StreamResponse) {            return response($content->getBody(), 200, ['Content-Length' => strlen($content->getBodyContents())])->contentType('image/png');        } else {            // 小程序码获取失败            $msg = $content['errcode'] ?? '-';            $msg .= $content['errmsg'] ?? '';            $this->error($msg);        }    }}
 |