/**
 * 申请权限
 *
 * @param {Object} permissions
 */
export function requestPermissions(permissions) {
  return new Promise((resolve, reject) => {
    plus.android.requestPermissions(permissions, function (e) {
      if (e.deniedAlways.length > 0) { //权限被永久拒绝
        // 弹出提示框解释为何需要定位权限，引导用户打开设置页面开启
        console.log('Always Denied!!! ' + e.deniedAlways.toString());
        return reject({
          code: -1,
          message: '权限被永久拒绝',
          deniedAlways: e.deniedAlways
        });
      }

      if (e.deniedPresent.length > 0) { //权限被临时拒绝
        // 弹出提示框解释为何需要定位权限，可再次调用plus.android.requestPermissions申请权限
        console.log('Present Denied!!! ' + e.deniedPresent.toString());

        return reject({
          code: -2,
          message: '权限被临时拒绝',
          deniedAlways: e.deniedPresent
        });
      }

      if (e.granted.length > 0) { // 权限被允许
        // 调用依赖获取定位权限的代码
        console.log('Granted!!! ' + e.granted.toString());

        return resolve();
      }
    }, function (e) {
      console.log('Request Permissions error:' + JSON.stringify(e));

      reject(e);
    });
  });
}
