canyin-project/ybcy/controllers/channel/MessageController.php
2024-11-01 16:07:54 +08:00

333 lines
13 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use app\models\common\Store;
use app\models\common\Config;
use app\models\common\WeChat;
use Mrgoon\AliSms\AliSms;
use app\models\common\Printing;
class MessageController extends CommonController{
public $enableCsrfValidation = false;
private $shop_id;
function init(){
parent::init();
$this->shop_id=$this->storeId?:Store::getMainStore($this->wqData['uniacid'])['id'];
$this->shop_id=$this->shop_id?:0;
}
//打印机列表
public function actionPrintList()
{
$page=Yii::$app->request->get('page')?:1;
$size=Yii::$app->request->get('size')?:10;
$type=Yii::$app->request->get('type')?:1;
$storeId=Yii::$app->request->get('storeId')?:$this->shop_id;
$table=(new \yii\db\Query())
->from('{{%ybwm_print}}')
->where('storeId=:storeId AND deleteAt=0 ',[':storeId'=>$storeId]);
if($type=='instore'){
$table->andWhere('support=2 or support=3 or support=5')->all();
}else{
$table->andWhere('support=1 or support=4')->all();
}
$count=$table->count();
$res=$table->offset(($page - 1) * $size)->limit($size)->all();
// $table->limit([($page - 1) * $size,$size]);
// $res=$table->all();
for($i=0;$i<count($res);$i++){
$res[$i]['data']=json_decode($res[$i]['data'],true);
$res[$i]['wmnum']=$res[$i]['data']['wmnum'];
}
return $this->result(1,'成功',$res,$count);
}
//保存打印机
public function actionSavePrint(){
$post=axios_request();
$storeId=$post['storeId']?:$this->shop_id;
if(Yii::$app->request->isGet){
$info=(new \yii\db\Query())
->from('{{%ybwm_print}}')
->where('id=:id',[':id'=>$post['id']])->one();
$info['data']=json_decode($info['data'],true);
$info['categoryIds']=json_decode($info['categoryIds'],true);
$info['label']=json_decode($info['label'],true);
$item=$post['item']?:6;
$table=(new \yii\db\Query())
->select(['id', 'name','sort','display'])
->from('{{%ybwm_core_category}}');
$info['goodsLabel']=$table->where('storeId=:storeId AND deleteAt=0 AND item=7',[':storeId'=> $storeId])->all();
$storeRes=Store::getStoreInfo($this->wqData['uniacid'],$storeId);
if($storeRes['goodsModel']==3) {
$info['goodsType'] = $table->where('storeId=:storeId AND deleteAt=0 AND item=:item AND level=1', [':storeId' => $storeId, ':item' => $item])->all();
}else{
$info['goodsType'] = $table->where('storeId=:storeId AND deleteAt=0 AND item=:item AND level=1', [':storeId' =>$this->shop_id, ':item' => $item])->all();
}
if($info['areaId']){
$info['areaId']=json_decode($info['areaId'],true);
}else{
$info['areaId']=[];
}
if($info['printMode2']){
$info['printMode2']=json_decode($info['printMode2'],true);
}else{
$info['printMode2']=[];
}
//var_dump(ddSql($table->where('storeId=:storeId AND deleteAt=0 AND item=7',[':storeId'=> $storeId])));die;
return $this->result(1,'成功',$info);
}
if ($post['type'] == 1) {
$data['data'] = json_encode([ 'number' => $post['number'], 'key' => $post['key'], 'userId' => $post['userId'], 'apiKey' => $post['apiKey'], 'wmnum' => $post['wmnum']]);
}
if ($post['type'] == 2) {
$data['data'] = json_encode(['dyjnumber' => $post['dyjnumber'], 'fenumber' => $post['fenumber'], 'feukey' => $post['feukey'], 'wmnum' => $post['wmnum']]);
}
if ($post['type'] == 3) {
$data['data'] = json_encode(['labeldyjnumber' => $post['labeldyjnumber'], 'labelfenumber' => $post['labelfenumber'], 'labelfeukey' => $post['labelfeukey'], 'wmnum' => $post['wmnum'],'paper'=>$post['paper']]);
}
if ($post['type'] == 4) {
$data['data'] = json_encode(['wmnum' => $post['wmnum']]);
}
$data['type'] = $post['type'];
$data['name'] = $post['name'];
$data['storeId'] = $storeId;
$data['support'] = $post['support']?:1;
$data['printMode'] = $post['printMode'];
$data['printModeType'] = $post['printModeType']?:1;
$data['categoryIds'] = $post['categoryIds']?json_encode($post['categoryIds']):'';
$data['label'] = $post['label']?json_encode($post['label']):'';
$data['pattern'] = $post['pattern'];
$data['uniacid'] = $this->wqData['uniacid'];
$data['areaId'] = $post['areaId']?json_encode($post['areaId']):'';
$data['printMode2'] = $post['printMode2']?json_encode($post['printMode2']):'';
//dd($data);die;
if ($post['id']) {
$data['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_print}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
$printId = $post['id'];
} else {
$data['createdAt']=time();
try{
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_print}}', $data)->execute();
$printId = Yii::$app->db->getLastInsertID();
}catch (\Exception $e){
return $e;
}
}
if($res){
if($post['type'] == 3){
$result=Printing::labelPrintSet($printId,$post['paper']);
}
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//修改打印机
public function actionModifyPrint(){
$post=axios_request();
if($post['type']==1){
//显示隐藏
$data['display']=$post['display'];
}
if($post['type']==2){
//删除
$data['deleteAt']=time();
}
$data['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_print}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//测试打印
public function actionPrintTest(){
$post=axios_request();
$id=$post['id']?:1;
$result=Printing::printTest($id);
// $result=Printing::takeoutOrderPrint($id);
if($result->error==0){
return $this->result(1,'成功');
}else{
return $this->result(2,$result->error_description);
}
}
//测试打印
public function actionLabelSet(){
$post=axios_request();
$id=$post['id']?:1;
$type=$post['type'];
$result=Printing::labelPrintSet($id,$type);
if($result->error==0){
return $this->result(1,'成功');
}else{
return $this->result(2,$result->error_description);
}
}
//短信列表
public function actionTelList(){
$page=Yii::$app->request->get('page')?:1;
$size=Yii::$app->request->get('size')?:10;
$storeId=Yii::$app->request->get('storeId')?:$this->shop_id;
$table=(new \yii\db\Query())
->select(['from_unixtime(createdAt) as createdAt','id','tel','note'])
->from('{{%ybwm_message}}')
->where('deleteAt=0 AND storeId=:storeId',[':storeId'=>$storeId]);
$count=$table->count();
$res=$table->offset(($page - 1) * $size)->limit($size)->all();
return $this->result(1,'成功',$res,$count);
}
//保存短信
public function actionSaveTel(){
$post=axios_request();
if(Yii::$app->request->isGet){
$info=(new \yii\db\Query())
->from('{{%ybwm_message}}')
->where('id=:id',[':id'=>$post['id']])->one()?:[];
return $this->result(1,'成功',$info);
}
if(!$post['tel']){
return $this->result(2,'手机号不能为空');
}
$array['tel'] = $post['tel'];
$array['note'] = $post['note'];
$array['storeId'] = $post['storeId']?:$this->shop_id;
$array['uniacid'] = $this->wqData['uniacid'];
if ($post['id']) {
$array['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_message}}', $array, 'id=:id', ['id' =>$post['id']])->execute();
} else {
$array['createdAt']=time();
try{
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_message}}', $array)->execute();
}catch (\Exception $e){
return $this->result(2,'网络异常,请稍后再试',$e);
}
}
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//删除短信电话
public function actionDelTel(){
$post=axios_request();
$data['deleteAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_message}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//测试发送短信
public function actionTestTel(){
$post=axios_request();
$aliSms = new AliSms();
$config=Config::getSystemSet('sms',$this->wqData['uniacid']);
$data=[];
if($post['type']==1){
$templateCode=$config['registerCode'];
$data['code']='123456';
}elseif($post['type']==2){
$templateCode=$config['payCode'];
}elseif($post['type']==3){
$templateCode=$config['refundCode'];
}elseif($post['type']==4){
$templateCode=$config['integralCode'];
}
$smsConfig=['smsCode'=>$templateCode,'access_key'=>$config['access_key_id'],'access_secret'=>trim($config['access_key_secret']),'sign_name'=>$config['sign']];
$response = object_array($aliSms->sendSms($config['tel'], $templateCode, $data, $smsConfig));
if($response['Message']=='OK'&&$response['Code']=='OK'){
return $this->result(1,'成功');
}else{
return $this->result(2,$response['Message']);
}
}
//查看绑定列表
public function actionOpenIdList(){
$post=axios_request();
$config=Config::getSystemSet('storeTemplates',$this->wqData['uniacid']);
$storeId=$post['storeId']?:$this->shop_id;
$res=(new \yii\db\Query())
->select(['from_unixtime(createdAt) createdAt','id','openId','userName','portrait'])
->from('{{%ybwm_openId}}')
->where('storeId=:storeId',[':storeId'=>$storeId]);
$count=$res->count();
$list=$res->all();
$data['icon']=$config['icon']?:'';
$data['list']=$list;
return $this->result(1,'成功',$data,$count);
}
//绑定
public function actionSaveOpenId(){
$post=axios_request();
if(Yii::$app->request->isGet){
return $this->result(2,'请求异常');
}
$uniacid=$this->wqData['uniacid'];
$openId=$post['openId'];
if(!$openId){
return $this->result(2,'openId不能为空');
}
$storeId=$post['storeId']?:$this->shop_id;
$res=(new \yii\db\Query())
->from('{{%ybwm_openId}}')
->where('openId=:openId AND storeId=:storeId',[':storeId'=>$storeId,':openId'=>$openId])->one();
if($res){
return $this->result(2,'该openId已经绑定');
}
$config=WeChat::getWeChatTemplateConfig($uniacid);
try{
$userInfo=WeChat::getUserInfoByOpenId($openId,$config);
}catch (\Exception $exception){
return $this->result(2,'请检查公众号配置是否正确');
}
if(!$userInfo['nickname']){
return $this->result(2,'请检查openId是否正确或用户是否已关注公众号');
}
$data['userName']=$userInfo['nickname'];
$data['portrait']=$userInfo['headimgurl'];
$data['openId']=$openId;
$data['storeId']=$storeId;
$data['uniacid']=$uniacid;
$data['createdAt']=time();
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_openId}}', $data)->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'添加失败,请重试');
}
}
//解绑
public function actionUnbundling(){
$post=axios_request();
if(Yii::$app->request->isGet){
return $this->result(2,'请求异常');
}
$res=Yii::$app->db->createCommand()->delete('{{%ybwm_openId}}', ['id'=>$post['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'失败');
}
}
}