SupportController.php 12.3 KB
<?php
namespace app\wx\controllers;

use Yii;
use yii\helpers\Html;
use common\exts\qqmap\QQMapSDK;
use common\helpers\FileHelper;
use common\helpers\Log as AppLog;
use common\helpers\Utils;
use common\helpers\WxHelper;
use common\models\BindDeviceApplyImg    as BindDeviceApplyImgModel;
use common\models\Address               as AddressModel;
use common\models\Engineer              as EngineerModel;

use stdClass;
use function json_decode;

class SupportController extends BaseController
{

    public function actionIndex(){
        echo "index";
    }

    public function actionTest()
    {
        return $this->render('/site/test.php');

    }

    /**
     * 获取地图坐标
     * @return string
     */
    public function actionGetMapInfo()
    {
        $lat= $this->request->post('lat');
        $lng= $this->request->post('lng');
        if (empty($lat) ||empty($lng)) {
            return $this->renderJSON(null);
        }
        $res = QQMapSDK::getMapInfo(QQMapSDK::QQMAP_KEY_TYPE_ENGINEER, $lat, $lng, 1);
        exit($res);
    }

    /** 更新工程师地址
     * @return string
     */
    public function actionUpdateAddress()
    {
        $sign = $this->request->get('sign');
        $openid = $this->request->get('openid');
        $tt = $this->request->get('time');
        $location = $this->request->get('location');
        $updateType = $this->request->get('updateType');
        $address = $this->request->get('address');

        $e = new stdClass();
        $e->success = false;

        $pars = $this->request->get();
        AppLog::DEBUG('pars:'. json_encode($pars));
        if (!Utils::checkSign($sign, $pars)) {
            AppLog::DEBUG('update address signError');
            exit();
        }

        if (empty($location) || !isset($location['x']) || empty($location['x'])) {
            $e->msg = '参数为空!';
            return $this->renderJson($e);
        }

        // 这里应该改为兼容 engineer id
        $query  = EngineerModel::find()
            ->select(['engineer.*'])
            ->where(['engineer.openid' => $openid]);
        $query->asArray();
        $userArray = $query->one();
        if ($userArray) {
            if ('service' == $updateType) {
                $updateFlag = $this->updateServiceAddress($userArray,$location,$address);
            } else {
                $updateFlag = $this->updateEngineerAddress($userArray,$location);
            }
            if ($updateFlag) {
                $e->success = true;
            } else {
                $e->msg = '更新地址失败';
            }
        } else {
            $e->msg = '未找到工程师!';
        }

        return $this->renderJson($e);
    }


    /**
     * @param $userArray
     * @param $location
     * @return bool
     */
    private function updateEngineerAddress($userArray,$location)
    {
        // 如果工程师接单地址为空, 默认设置为当前工程师所在位置为接单位置
        $service_address_id = null;
        // 后面优化一下,工程师不是每次都要更新 地址的,坐标可以更新,地址暂时不用
        $mapInfo = QQMapSDK::getMapInfo(QQMapSDK::QQMAP_KEY_TYPE_ENGINEER, $location['x'], $location['y'], 1);
        //AppLog::DEBUG('====== getQQMapInfo ======== engineer id:'.$userArray['id'].' jsonEncode '.json_encode($mapInfo ));
        $result = $mapInfo;
        $mapInfo = json_decode($mapInfo,true);

        //AppLog::DEBUG('=========== updateEngineerAddress ============'.$result);
        if (!isset($mapInfo['result']) || $mapInfo['status'] > 0 ) {
            AppLog::DEBUG('====== getQQMapInfo ======== engineer id:'.$userArray['id'].' mapInfo '.json_encode($mapInfo ));
            return false;
        }
        $address = $mapInfo['result']['address'];

        // 首次进来的时候是没有设置服务地址的,这里默认设置服务地址
        if (empty($userArray['service_address_id'])) {

            $service_addr = new AddressModel();
            $service_addr->address = $address;
            $service_addr->longitude = $location['y'];
            $service_addr->latitude = $location['x'];
            $service_addr->type = AddressModel::TYPE_SERVICE;
            if(true == $service_addr->save()){
                $service_address_id = $service_addr->id;
            }

            $engineer = EngineerModel::findOne($userArray['id']);
            if ($service_address_id && $engineer) {
                $engineer->service_address_id = $service_address_id;
                $engineer->save();
            }
        }

        if (empty($userArray['address_id'])) {
            //保存地址
            $addr = new AddressModel();
            $address_id = null;

            $addr->address = $address;
            $addr->longitude = $location['y'];
            $addr->latitude = $location['x'];
            $addr->type = AddressModel::TYPE_LBS;

            if(true == $addr->save()){
                $address_id = $addr->id;
            }
            $engineer = EngineerModel::findOne($userArray['id']);
            $re = false;
            if ($address_id && $engineer) {

                $engineer->address_id = $address_id;
                //$engineer->id=$userArray['id'];
                $re = $engineer->save();
            }
            return $re;

        } else {
            //AppLog::DEBUG("start update address->");
            $addr = AddressModel::findOne($userArray['address_id']);
            if (empty($addr)) {
                AppLog::DEBUG("update address fail. address not found");
                return false;
            }
            $addr->address = $address;
            $addr->longitude = $location['y'];
            $addr->latitude = $location['x'];

            //AppLog::DEBUG("update address->3:".$userArray['address_id']);
            //$addr->setIsNewRecord(false);
            $re = $addr->save();
            if ($re) {
                return true;
            } else {
                AppLog::DEBUG("update address->fail address:".$address);
                return false;
            }
        }
    }

    /** 更新服务地址
     * @param $userArray
     * @param $address
     * @param $location
     * @param $engineer 准备更新的 object
     */
    private function updateServiceAddress($userArray,$location,$address = '')
    {

        if (empty($address)) {
            return false;
        }
        $address = Html::encode($address);
        if (empty($userArray['service_address_id'])) {
            //保存地址
            $addr = new AddressModel();
            $address_id = null;


            $addr->address = $address;
            $addr->longitude = $location['y'];
            $addr->latitude = $location['x'];
            $addr->type = AddressModel::TYPE_SERVICE;

            if (true == $addr->save()) {
                $address_id = $addr->id;
            }
            //AppLog::DEBUG(' address is --------->'.$userArray['id']);
            $engineer = EngineerModel::findOne($userArray['id']);
            $re = false;
            if ($address_id && $engineer) {

                $engineer->service_address_id = $address_id;
               //$engineer->id=$userArray['id'];
                //AppLog::DEBUG(" update wx address result->".$address_id);
                $re = $engineer->save();

            }
            return $re;
            //AppLog::DEBUG("insert address->".$address_id." update wx address result->".$re.' address is '.$address);
        } else {
            //AppLog::DEBUG("start update address->");
            $addr = AddressModel::findOne($userArray['service_address_id']);
            if (!$addr) {
                return false;
            }
            $addr->address = $address;
            $addr->longitude = $location['y'];
            $addr->latitude = $location['x'];
            $addr->type = AddressModel::TYPE_SERVICE;
            //AppLog::DEBUG("update address->3:".$userArray['address_id']);
            //$addr->setIsNewRecord(false);
            $re = $addr->save();
            if ($re) {
                return true;
                //AppLog::DEBUG("update address->ok address:".$address);
            } else {

                AppLog::DEBUG("update service address->fail address:".$address);
                return false;
            }
        }
    }

    // 获取贴码微信图片
    public function actionUploadWxImg()
    {
        $req = Yii::$app->request;
        $tt = $req->get('tt');
        $applyImgId = $req->get('applyImgId');
        $sign = $req->get('sign');
        $qrcode = $req->get('qrcode'); //可以是qrcode 可也可以是 bind_apply_devce_apply id

        $gets = array('tt' => $tt, 'applyImgId' => $applyImgId,'qrcode'=> $qrcode);
        $e = new stdClass();
        $e->success = false;

        //AppLog::DEBUG('===== upload wx img sign ========'.$sign);
        if (!Utils::checkSign($sign,$gets)) {
            $e->msg = 'error sign';
            return $this->renderJson($e);
        }

        $applyImgId = json_decode($applyImgId,true);

        if (empty($applyImgId)) {
            $e->msg = 'empty applyImgId';
            return $this->renderJson($e);
        }
        //AppLog::DEBUG('===== upload wx img applyImgId ========'.json_encode($applyImgId));
        $imageIdList = FileHelper::filterImage($applyImgId);
        if (empty($imageIdList)) {
            $e->msg = 'empty image ids';
            return $this->renderJson($e);
        }
        if (!empty($imageIdList['wx'])) {
            $this->getWxImage($imageIdList['wx'], $qrcode);
        }
        if (!empty($imageIdList['local'])) {
            $this->getLocalServerImage($imageIdList['wx'], $qrcode);
        }
    }

    /**
     * @param $applyImgId
     * @return array
     */
    private function getWxImage($applyImgId, $qrcode)
    {
        $wechat = WxHelper::getWxPHPSDK();
        $reArr = array();

        foreach ($applyImgId as $k=>$v) {
            if (empty($v)) {
                continue;
            }
            //AppLog::DEBUG('===== getWxImage  $images $v========'.$v);
            $bindImgModel = BindDeviceApplyImgModel::findOne(['id'=>$v]);
            if ($bindImgModel && empty($bindImgModel->path)) {
                $bind_device_apply_img = FileHelper::saveWxImgToRemoveServer($wechat, $bindImgModel->wx_server_id, $bindImgModel->bind_device_apply_id);
                //AppLog::DEBUG('===== getWxImage  $bind_device_apply_img ========'.$bind_device_apply_img);
                if (!empty($bind_device_apply_img)) {
                    $bindImgModel->path = $bind_device_apply_img;
                    $bindImgModel->save();
                    $reArr[]= $bind_device_apply_img;
                }
            }
        }
        return $reArr;
    }

    /**
     *  图片不通过微信直接保存到服务器再传到OSS
     */
    private function getLocalServerImage($applyImgNameIds, $qrcode = '')
    {
        $reArr = array();
        foreach ($applyImgNameIds as $k => $v) {
            if (empty($v)) {
                continue;
            }

            $bindImgModel = BindDeviceApplyImgModel::findOne(['id'=>$v]);
            if ($bindImgModel && empty($bindImgModel->path)) {
                $bind_device_apply_img = FileHelper::saveLocalImgToRemoveServer($v, $bindImgModel->bind_device_apply_id);

                if (!empty($bind_device_apply_img)) {
                    $bindImgModel->path = $bind_device_apply_img;
                    $bindImgModel->save();
                    $reArr[]= $bind_device_apply_img;
                }
            }
        }
        return $reArr;
    }

    /**
     *  接单教程统计入口,这里只是做准备
     */
    public function actionStat()
    {
        $gets = Yii::$app->request->get();
        //AppLog::DEBUG('stat:'.json_encode($gets));
    }

    public function actionLogout()
    {
        $appUser = Yii::$app->getUser();
        $appUser->logout();
        echo "logout";

    }

    /**
     * 极窝订单宝-服务协议
     */
    public function actionLegal()
    {
        echo Yii::$app->view->renderFile('@app/views/layouts/legal.php');
    }

    /**
     * 极办公平台订单-服务协议
     */
    public function actionLegalJiwork()
    {
        echo Yii::$app->view->renderFile('@app/views/layouts/legal_jibangong.php');
    }

    /**
     * 贴码规则条文
     */
    public function actionQrcodeLegal()
    {

        echo Yii::$app->view->renderFile('@app/views/layouts/qrcode_legal.php');
        //return $this->render('@app/views/layouts/legal');
    }
}