<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JiGuang\JSMS as JSMS;
use Illuminate\Support\Facades\Redis;
use App\Models\Province;
use App\Models\District;
use App\Models\City;

/**
 * @description: 系统级接口
 * @author fengyichao
 * @date 2017年11月13日 上午11:06:13
 */
class SystemController extends ApiController {

	
	/**
	 *
	 * @api {post} system/sendSms 发送验证码
	 * @apiName system/sendSms
	 * @apiGroup system
	 * @apiParam {int} mobile 手机号
	 * @apiParamExample {curl} Example usage:
	 * http://59.110.137.245:8080/api/system/sendSms?mobile=13810456667
	 */
	function sendSms(Request $request) {
		$rules = [
				'mobile' => [
						'required'
				]
		];
		$validator = app ( 'validator' )->make ( $request->all (), $rules );
		if ($validator->fails ()) {
			return $this->throwErrors($validator);
		}
		
		$temp_id = 1;
		$client = new JSMS ( env('JIGUANG_APP_KEY'), env('JIGUANG_APP_SECRET'), [ 
				'disable_ssl' => true 
		] );
		$res = $client->sendCode ( $request->mobile, $temp_id );
	
		if ($res ['http_code'] == '200' && isset ( $res ['body'] ['msg_id'] )) {
			//存redis mobile => msg_id
			$key = 'code:sms:' . $request->mobile;
			Redis::setex ( $key, 600, $res ['body'] ['msg_id'] );
			return $this->success ();
		} else {
			return $this->error ( '900001', '发送失败' );
		}
	}
	/**
	 *
	 * @api {post} system/provices 获取省
	 * @apiName system/provices
	 * @apiGroup system
     * @apiSuccess {List[Map]} data 数据
	 * @apiSuccess (data) {int}  id id
	 * @apiSuccess (data) {int}  pro_name 
	 * @apiParamExample {curl} Example usage:
	 * http://59.110.137.245:8080/api/system/provices
	 */
	function provinces(){
		$res['data']=Province::all();
		return $this->success ($res);
	}
	/**
	 *
	 * @api {post} system/city 获取市
	 * @apiName system/city
	 * @apiGroup system
	 * @apiParam {int} province_id 省id
	 * @apiSuccess {List[Map]} data 数据
	 * @apiSuccess (data) {int}  id 市id
	 * @apiSuccess (data) {int}  province_id 省id
	 * @apiSuccess (data) {int}  name
	 * @apiParamExample {curl} Example usage:
	 * http://59.110.137.245:8080/api/system/district?province_id=1
	 */
	function city(Request $request){
		$rules = [
				'province_id' => [
						'required'
				]
		];
		$validator = app ( 'validator' )->make ( $request->all (), $rules );
		if ($validator->fails ()) {
			return $this->throwErrors($validator);
		}
		$res['data']=City::where('province_id',$request->province_id)->get();
		return $this->success ($res);
	}
	/**
	 *
	 * @api {post} system/district 获取区
	 * @apiName system/district
	 * @apiGroup system
	 * @apiParam {int} pro_id 省id
	 * @apiSuccess {List[Map]} data 数据
	 * @apiSuccess (data) {int}  id 区id 
	 * @apiSuccess (data) {int}  city_id 市id
	 * @apiSuccess (data) {int}  name
	 * @apiParamExample {curl} Example usage:
	 * http://59.110.137.245:8080/api/system/district?city_id=1
	 */
	function district(Request $request){
		$rules = [
				'city_id' => [
						'required'
				]
		];
		$validator = app ( 'validator' )->make ( $request->all (), $rules );
		if ($validator->fails ()) {
			return $this->throwErrors($validator);
		}
		$res['data']=District::where('city_id',$request->city_id)->get();
		return $this->success ($res);
	}
}
