package com.yidian.shenghuoquan.bgeneralbusiness.service

import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.shenghuoquan.bgeneralbusiness.http.GeneralBusinessRemoteDataService
import com.yidian.xarc.xrouter.ResultResolver
import com.yidian.yac.core.zap.ZapService
import org.json.JSONObject


/**
 * 通用能力服务，提供其他组件的共用操作能力
 */
class GeneralAbilityService : ZapService() {

  private lateinit var resolver: ResultResolver

  override fun onAction(path: String, action: String, params: Any, options: Any, resolver: ResultResolver) {
    this.resolver = resolver
    params as JSONObject
    when (action) {
      ZapServiceActionConstants.ActionRequestSmsCode -> requestSmsCode(params)
    }
  }

  override fun onExit() {
  }

  override fun onInit() {
  }

  /**
   * 请求短信验证码
   */
  private fun requestSmsCode(params: JSONObject) {
    val mobile = params.optString("mobile")
    val scene = params.optString("scene")
    val paramsMap = HashMap<String, String?>()
    paramsMap["mobile"] = mobile
    paramsMap["scene"] = scene
    GeneralBusinessRemoteDataService.sendMsgCode(paramsMap) {
      if (it) {
        resolver.success(true)
      }
    }
  }
}
