<?php
namespace App\Http\Controllers\Api\WeChatPush;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Models\Api\Common;
use App\Http\Models\Api\CashierDesk;
class WeChatPushController extends Controller{
	// 列表每页显示数据个数
    protected $show_count = 10;
	public function accredit(Request $request){
		// return view('WeChatPush.accredit',['name'=>'546546546']);
		//View::share('name', 'value');
		$input=$request->all();
		$code = $input['code'];//前端传来的code值
		// 半径美装小程序
		// AppID： wx5a9e016f8ebb40c7
		// AppSecret： b190afa86b4fab975d46e701d27a04e5
        // $appid = "wxf5d6f1ae7ee63d36";
        $appid = "wx7c45128ab7710f54";//半径门店公众号openid
        $appsecret = "a92d64dd5fcf2d72cd12df9aeb9c7a5a";
        // $appsecret = "94f3ba89ebed9e2f86c9b8a045d0bf75";
        // $input['phone']='15303284172';
        // $input['password']='db123456';
		$id=DB::table('admin')->where("adm_phone",$input['phone'])->where("adm_password",md5($input['password'].'faster123456'))->value('adm_id');
		if(empty($id)){
			return response()->json(['code'=>500, 'msg'=>'账号或密码错误', 'data'=>'']);
		}else{
			//$adma_id=DB::table('admin_master')->where("adma_adid",$id)->value('adma_id');
			//if(empty($adma_id)){
				$poss['adma_adid']=$id;
		        //获取openid
		        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
				//https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf5d6f1ae7ee63d36&redirect_uri=https://mktest.bluearp.com/index.php/api/v3/WeChatPush/accredit&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
		        $result = file_get_contents($url);
		        $jsoninfo = json_decode($result, true);
		        if(!empty($jsoninfo['openid'])){
			        $adma_id=DB::table('admin_master')->where("adma_openid",$jsoninfo['openid'])->where("adma_adid",$id)->value('adma_id');
			        if(empty($adma_id)){
				        $poss['adma_openid']=$jsoninfo['openid'];
				        $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$jsoninfo['access_token']."&openid=".$jsoninfo['openid']."&lang=zh_CN";
						$user = file_get_contents($url);//运行curl
						$user = json_decode($user, true);
						if(!empty($user['nickname'])){
							$poss['adma_nickmame']=$user['nickname'];
							$poss['adma_sex']=$user['sex'];
							$poss['adma_province']=$user['province'];
							$poss['adma_city']=$user['city'];
							$poss['adma_headimgurl']=$user['headimgurl'];
							$poss['adma_follow']='5';
						}
						$res=DB::table('admin_master')->insert($poss);
						if($res){
							return response()->json(['code'=>200, 'msg'=>'添加成功', 'data'=>'']);
						}else{
							return response()->json(['code'=>500, 'msg'=>'添加失败', 'data'=>'']);
						}
					}else{
						return response()->json(['code'=>500, 'msg'=>'获取数据失败', 'data'=>'']);
					}
				}else{
					return response()->json(['code'=>500, 'msg'=>'用户信息已绑定', 'data'=>'']);
				}
				//return view('data',$poss);
			//}else{
			//	return response()->json(['code'=>500, 'msg'=>'获取数据失败', 'data'=>'']);
			//}
		}
	}
	 /**|---------------------------------------------------------------------------------
     * @name 添加推送信息
     * @param Request pushtype 推送类型
     * @param Request wor_number 工单号
     * @param Request ord_number 订单单号
     * @param Request order_type 工单类型
     * @param Request admid 会员id
     * @param Request user_name 用户姓名
     * @param Request user_phone 用户电话
     * @param Request user_dress 用户地址
     * @return \Illuminate\Http\JsonResponse
     * @author dou <2020/11/9 17:10>
     * |---------------------------------------------------------------------------------
     */
    public function inforPush (Request $request){
    	$CashierDesk = new CashierDesk;
        $CashierDesk->inforPush($request->all());
        return response()->json(['code'=>500, 'msg'=>'缺少pushtype', 'data'=>'']);
    }
    /**|---------------------------------------------------------------------------------
     * @name  对象转数组
     * @param $object 对象数据
     * @return \Illuminate\Http\JsonResponse
     * @author dou <2020/9/30 16:35>
     * |---------------------------------------------------------------------------------
     */
    function objectToArray($object) {
        //先编码成json字符串，再解码成数组
        return json_decode(json_encode($object), true);
    }
    /**|---------------------------------------------------------------------------------
     * @name  https协议post传参
     * @param $url 请求链接
     * @param $data 请求数据
     * @return \Illuminate\Http\JsonResponse
     * @author dou <2020/11/9 17:10>
     * |---------------------------------------------------------------------------------
     */
	//
	function https_post($url,$data){
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}
}
