UNPKG

ngx-zalo

Version:

Use Zalo SDK with Angular5~ (https://developers.zalo.me/docs/sdk/javascript-sdk/tai-lieu)

297 lines (289 loc) 7.49 kB
import { Injectable, Directive, HostListener, EventEmitter, Output, NgModule } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { CommonModule } from '@angular/common'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class NgxZaloService { constructor() { this.SCRIPT_URL = 'https://zjs.zdn.vn/zalo/sdk.js'; this.STATUS_NOT_INIT = 0; this.STATUS_INITIALIZING = 1; this.STATUS_INIT_FAILURE = 2; this.STATUS_INIT_SUCCESSFULLY = 3; this._initStatus = this.STATUS_NOT_INIT; this._isLogin = false; } /** * @return {?} */ get isLogin() { return this._isLogin; } /** * @return {?} */ get accessToken() { return this.isInitSuccessfully ? Zalo.getAccessToken() : null; } /** * @param {?} configs * @return {?} */ init(configs) { this._initStatus = this.STATUS_INITIALIZING; this.initZaloScript(() => { Zalo.init(configs); this.getLoginStatus(() => { this._initStatus = this.STATUS_INIT_SUCCESSFULLY; }); }, () => { this._initStatus = this.STATUS_INIT_FAILURE; }); } /** * @return {?} */ login() { if (this.isInitSuccessfully) { Zalo.login(); } } /** * @return {?} */ logout() { return new Observable(observer => { if (this.isInitSuccessfully) { Zalo.logout(); } this._isLogin = false; this.breakObservable(observer); }); } /** * @return {?} */ updateLoginInfo() { return new Observable(observer => { if (this.isNotInit) { this._isLogin = false; this.breakObservable(observer, false); return; } this.pendingInit().subscribe(() => { Zalo.getLoginStatus((response) => { this._isLogin = response.status === 'connected'; this.breakObservable(observer, this._isLogin); }); }); }); } /** * @param {?=} fields * @return {?} */ getMyProfile(fields = 'id, name, birthday, picture, gender') { return new Observable(observer => { if (!this._initStatus) { this.breakObservable(observer); return; } this.pendingInit().subscribe(() => { Zalo.api('/me', 'GET', { fields: fields }, (myProfile) => { this.breakObservable(observer, myProfile); }); }); }); } /** * @return {?} */ get isNotInit() { return this.STATUS_NOT_INIT === this._initStatus; } /** * @return {?} */ get isInitializing() { return this.STATUS_INITIALIZING === this._initStatus; } /** * @return {?} */ get isInitSuccessfully() { return this.STATUS_INIT_SUCCESSFULLY === this._initStatus; } /** * @return {?} */ pendingInit() { return new Observable(observer => { if (!this.isInitializing) { this.breakObservable(observer); return; } const /** @type {?} */ timeInterval = setInterval(() => { if (!this.isInitializing) { clearInterval(timeInterval); this.breakObservable(observer); } }, 500); }); } /** * @param {?} successCallback * @param {?} errorCallback * @return {?} */ initZaloScript(successCallback, errorCallback) { if ('undefined' === typeof window) { return; } const /** @type {?} */ script = document.createElement('script'); script.src = this.SCRIPT_URL; script.onload = () => { if (successCallback) { successCallback(); } }; script.onerror = () => { if (errorCallback) { errorCallback(); } }; document.head.appendChild(script); } /** * @param {?} callback * @return {?} */ getLoginStatus(callback) { Zalo.getLoginStatus((response) => { if (response.status === 'connected') { this._isLogin = true; } if (callback) { callback(response); } }); } /** * @param {?} observer * @param {?=} data * @return {?} */ breakObservable(observer, data = null) { observer.next(data); observer.complete(); } } NgxZaloService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ NgxZaloService.ctorParameters = () => []; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class LoginZaloDirective { /** * @param {?} _ngxZaloService */ constructor(_ngxZaloService) { this._ngxZaloService = _ngxZaloService; } /** * @return {?} */ onClick() { this._ngxZaloService.login(); } } LoginZaloDirective.decorators = [ { type: Directive, args: [{ selector: '[ngxLoginZalo]' },] }, ]; /** @nocollapse */ LoginZaloDirective.ctorParameters = () => [ { type: NgxZaloService, }, ]; LoginZaloDirective.propDecorators = { "onClick": [{ type: HostListener, args: ['click',] },], }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class LogoutZaloDirective { /** * @param {?} _ngxZaloService */ constructor(_ngxZaloService) { this._ngxZaloService = _ngxZaloService; this.successEvent = new EventEmitter(); } /** * @return {?} */ onClick() { this._ngxZaloService.logout().subscribe(() => { this.successEvent.emit(); }); } } LogoutZaloDirective.decorators = [ { type: Directive, args: [{ selector: '[ngxLogoutZalo]' },] }, ]; /** @nocollapse */ LogoutZaloDirective.ctorParameters = () => [ { type: NgxZaloService, }, ]; LogoutZaloDirective.propDecorators = { "successEvent": [{ type: Output },], "onClick": [{ type: HostListener, args: ['click',] },], }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class NgxZaloModule { } NgxZaloModule.decorators = [ { type: NgModule, args: [{ imports: [ CommonModule, ], providers: [ NgxZaloService, ], declarations: [ LoginZaloDirective, LogoutZaloDirective, ], exports: [ LoginZaloDirective, LogoutZaloDirective, ] },] }, ]; /** @nocollapse */ NgxZaloModule.ctorParameters = () => []; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** * Generated bundle index. Do not edit. */ export { NgxZaloModule, NgxZaloService, LoginZaloDirective as ɵa, LogoutZaloDirective as ɵb }; //# sourceMappingURL=ngx-zalo.js.map