cxhxy/app/common/model/Applet.php
test_service d3170b4d1c 1
2023-12-01 15:43:29 +08:00

288 lines
9.0 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\model;
use hema\wechat\Driver;
use Endroid\QrCode\QrCode as CodeMode;
use Endroid\QrCode\Writer\PngWriter;
use hema\alipay\Driver as Alipay;
/**
* 小程序模型
*/
class Applet extends BaseModel
{
//定义表名
protected $name = 'applet';
// 定义主键
protected $pk = 'applet_id';
// 追加字段
protected $append = [
'template',
'qrcode',
];
/**
* 获取小程序端二维码
*/
public function getQrcodeAttr($value,$data)
{
$qrcode = [
'h5' => '',
'weixin' => '',
'alipay' => '',
'qrcode' => '',
];
!empty($data['qrcode_url']) && $qrcode['weixin'] = $data['qrcode_url'];
//生成二维码 - h5
if(!file_exists('./temp')){
mkdir('./temp',0777,true);
}
//如果已经发布H5端代码
if(is_file('./h5/'.$data['app_type'].'/index.html')){
$h5_path = '/temp/h5-applet-' . $data['applet_id'] . '.png';
if(!is_file('.' . $h5_path)){
$writer = new PngWriter();
$code = CodeMode::create(base_url() . 'h5/'.$data['app_type'].'/#/?applet_id=' . $data['applet_id'])->setSize(500);
$result = $writer->write($code);
$result->saveToFile('.' . $h5_path);
}
$qrcode['h5'] = $h5_path;
}
//生成支付宝小程序码
$alipay_path = '/temp/alipay-applet-' . $data['applet_id'] . '.png';
if(!is_file('.' . $alipay_path)){
$alipay = new Alipay($data['applet_id']);
if($alipay->openAppQrcodeCreate('applet-'.$data['applet_id'],$alipay_path)){
$qrcode['alipay'] = $alipay_path;
}
}else{
$qrcode['alipay'] = $alipay_path;
}
//生成小程序聚合码
$qrcode_path = '/temp/qrcode-applet-' . $data['applet_id'] . '.png';
if(!is_file('.' . $qrcode_path)){
$writer = new PngWriter();
$code = CodeMode::create(base_url() .$data['app_type'].'/' . $data['applet_id'] . '/applet-'. $data['applet_id'])->setSize(500);
$result = $writer->write($code);
$result->saveToFile('.' . $qrcode_path);
}
$qrcode['qrcode'] = $qrcode_path;
return $qrcode;
}
/**
* 模板表
*/
public function getTemplateAttr($value,$data)
{
if(empty($data['app_type'])){
return ['name' => '自定义模板', 'text' => '', 'value' => 0];
}else{
if($tmp = Template::getAppType($data['app_type'])){
return ['name' => $tmp['name'], 'text' => $data['app_type'], 'value' => $tmp['template_id']];
}
$tmp = get_addons_info($data['app_type']);
return ['name' => $tmp['title'], 'text' => $data['app_type'], 'value' =>0];
}
}
/**
* 关联分账账户表 - 仅代理
*/
public function divide()
{
return $this->belongsTo('app\\common\\model\\DivideAccount','applet_id','applet_id');
}
/**
* 微信头像
*/
public function getHeadImgAttr($value)
{
empty($value) && $value = base_url() . 'assets/img/no_pic.jpg';
return $value;
}
/**
* 账号来源
*/
public function getSourceAttr($value)
{
$status = [10 => '自注', 20 => '平台'];
return ['text' => $status[$value], 'value' => $value];
}
/**
* 是否授权
*/
public function getStatusAttr($value)
{
$status = ['否','是'];
return ['text' => $status[$value], 'value' => $value];
}
/**
* 模板类型
*/
public function getShopModeAttr($value)
{
$status = [0 => '无', 10 => '单门店', 20 => '多门店'];
return ['text' => $status[$value], 'value' => $value];
}
/**
* 到期时间
*/
public function getExpireTimeAttr($value)
{
$day = round(($value - time()) / 86400);
return ['text' => date("Y-m-d", $value), 'value' => $value, 'day' => $day];
}
/**
* 获取列表
* $type 0全部 10已授权 20授权已到期
*/
public function getList($type = 0, $user_id = 0, $agent_id = 0)
{
// 筛选条件
$filter = [];
$user_id > 0 && $filter['user_id'] = $user_id;
if($agent_id > 0){
$filter['agent_id'] = $agent_id;
}
if($type > 0){
$filter['status'] = 1;
$type == 20 && $filter['expire_time'] = ['<',time()];
}
// 执行查询
return $this->where($filter)
->with(['divide'])
->order('applet_id','desc')
->paginate(['list_rows'=>15,'query' => request()->param()]);
}
/**
* 更新小程序设置
* @param string $dataType 数据类型 app=用户更新,apply=新注册更新
*/
public function edit(array $data)
{
if($this->status['value'] == 1){
$wx = new Driver;
//添加服务器域名
if($this->api_domain != $data['api_domain']){
$result = $wx->setServeDomain($this->applet_id,$data['api_domain']);
if($result['errcode'] != 0){
$this->error = '服务器域名设置失败code'.$result['errcode'].',msg'.$result['errmsg'];
return false;
}
}
if($this->signature != $data['signature']){
$result = $wx->setSignature($this->applet_id,$data['signature']);
if($result['errcode'] != 0){
$this->error = '简介设置失败code'.$result['errcode'].',msg'.$result['errmsg'];
return false;
}
}
if($this->webview_domain != $data['webview_domain']){
if(!$wx->setWebDomain($this->applet_id,$data['webview_domain'])){
$this->error = $wx->getError();
return false;
}
}
}
return $this->save($data) !== false;
}
/**
* 获取小程序信息 - 回调接口
*/
public static function getApplet(array $filter = [])
{
return self::where($filter)->find();
}
/**
* 修改小程序密码
*/
public function renew(array $data)
{
if ($data['password'] != $data['password_confirm']) {
$this->error = '确认密码不正确';
return false;
}
$data['password'] = hema_hash($data['password']);
return $this->save($data) !== false;
}
/**
* 根据条件统计数量
*/
public static function getCount($agent_id = 0)
{
$self = new static;
$count = array();
$filter = [];
$agent_id > 0 && $filter['agent_id'] = $agent_id;
//全部统计
$count['all'] = self::where($filter)->count();//全部数量
//今天统计
$star = strtotime(date('Y-m-d 00:00:00',time()));
$count['today'] = self::where($filter)->where('create_time','>',$star)->count();//全部数量
//昨天统计
$star = strtotime("-1 day");
$end = strtotime(date('Y-m-d 00:00:00',time()));
$count['today2'] = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->count();//全部数量
//前天统计
$star = strtotime("-2 day");
$end = strtotime("-1 day");
$count['today3'] = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->count();//全部数量
//-4天统计
$star = strtotime("-3 day");
$end = strtotime("-2 day");
$count['today4'] = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->count();//全部数量
//-5天统计
$star = strtotime("-4 day");
$end = strtotime("-3 day");
$count['today5'] = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->count();//全部数量
//-6天统计
$star = strtotime("-5 day");
$end = strtotime("-4 day");
$count['today6'] = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->count();//全部数量
//-7天统计
$star = strtotime("-6 day");
$end = strtotime("-5 day");
$count['today7'] = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->count();//全部数量
//本月统计
$end = mktime(0,0,0,date('m'),1,date('y'));
$count['month'] = self::where($filter)->where('create_time','>',$end)->count();//全部数量
//上月统计
$star = mktime(0,0,0,date('m')-1,1,date('y'));
$count['month2'] = self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->count();//全部数量
return $count;
}
}