dever преди 6 години
родител
ревизия
920c35dcce

+ 16 - 0
applet/config/base.php

@@ -0,0 +1,16 @@
+<?php
+
+# 一些基本配置
+$config['base'] = array
+(
+	# 后台头部菜单
+	'top' => 'main/project_id-3',
+);
+
+# 模板配置
+$config['template'] = array
+(
+	
+);
+
+return $config;

+ 67 - 0
applet/config/wechat.php

@@ -0,0 +1,67 @@
+<?php
+# 小程序基本配置
+$config['type'] = 3;
+
+$config['token'] = array
+(
+	'name' => '获取小程序的token',
+	'method' => 'get',
+	'json' => false,
+	'url' => 'https://api.weixin.qq.com/cgi-bin/token?',
+	'param' => array
+	(
+		'appid' => 'appid',
+		'secret' => 'secret',
+		'grant_type' => 'client_credential',
+	),
+	//针对一些返回的名称,做转换
+	'response' => array
+	(
+		'access_token' => 'token',
+		'expires_in' => 'expires_in',
+	),
+);
+
+$config['send_msg'] = array
+(
+	'name' => '发送模板消息',
+	'method' => 'post',
+	'json' => true,
+	'url' => 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?',
+	'param' => array
+	(
+		'access_token' => 'token',
+		'touser' => 'touser',
+		'template_id' => 'template_id',
+		'page' => 'page',
+		'form_id' => 'form_id',
+		'data' => 'data',
+		'emphasis_keyword' => 'emphasis_keyword',
+	),
+	//针对一些返回的名称,做转换
+	'response' => array
+	(
+		
+	),
+);
+
+$config['get_msg'] = array
+(
+	'name' => '获取帐号下已存在的模板列表',
+	'method' => 'post',
+	'json' => true,
+	'url' => 'https://api.weixin.qq.com/cgi-bin/wxopen/template/list?',
+	'param' => array
+	(
+		'access_token' => 'token',
+		'offset' => '0',
+		'count' => '20',
+	),
+	//针对一些返回的名称,做转换
+	'response' => array
+	(
+		
+	),
+);
+
+return $config;

+ 119 - 0
applet/database/msg.php

@@ -0,0 +1,119 @@
+<?php
+
+return array
+(
+	# 表名
+	'name' => 'msg',
+	# 显示给用户看的名称
+	'lang' => '模板消息列表',
+	'order' => 100,
+	'desc' => Dever::markdown('发送模板消息接口:'.Dever::url("applet/msg.send").'
+
+		id:消息id
+		touser:用户openid
+		page:页面路径
+		data:内容
+		form_id:表单id
+	'),
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'project_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '项目',
+			'default' 	=> '',
+			'desc' 		=> '请输入项目',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-120',
+			'name' 		=> '模板标题',
+			'default' 	=> '',
+			'desc' 		=> '模板标题',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
+		'template_id'		=> array
+		(
+			'type' 		=> 'varchar-120',
+			'name' 		=> '模板id',
+			'default' 	=> '',
+			'desc' 		=> '模板id',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'content'		=> array
+		(
+			'type' 		=> 'varchar-600',
+			'name' 		=> '模板内容',
+			'default' 	=> '',
+			'desc' 		=> '模板内容',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'list'		=> true,
+		),
+
+		'example'		=> array
+		(
+			'type' 		=> 'varchar-600',
+			'name' 		=> '模板样例',
+			'default' 	=> '',
+			'desc' 		=> '模板样例',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'list'		=> true,
+			'modal'		=> '点此查看',
+		),
+		
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '更新时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			'default' 	=> '',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'top' => Dever::config('base', 'applet')->top,
+
+	'manage' => array
+	(
+		//'delete' => false,
+		'insert' => false,
+		'edit' => false,
+
+		'button' => array
+		(
+			'同步模板' => 'applet/msg.sync',
+		),
+	),
+);

+ 89 - 0
applet/database/msg_form.php

@@ -0,0 +1,89 @@
+<?php
+
+return array
+(
+	# 表名
+	'name' => 'msg_form',
+	# 显示给用户看的名称
+	'lang' => '模板form_id收集表',
+	'menu' => false,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'project_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '项目',
+			'default' 	=> '',
+			'desc' 		=> '请输入项目',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'form_id'		=> array
+		(
+			'type' 		=> 'varchar-200',
+			'name' 		=> 'form_id',
+			'default' 	=> '',
+			'desc' 		=> 'form_id',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+		
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '更新时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			'default' 	=> '',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'top' => Dever::config('base', 'applet')->top,
+
+	'manage' => array
+	(
+		'delete' => false,
+		'insert' => false,
+		'edit' => false,
+	),
+
+	'request' => array
+	(
+		'getAll' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'cdate' => array(time()+(3600*24*7), '<='),
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('cdate' => 'desc'),
+			'limit' => '0,100',
+			'col' => '*',
+		),
+	),
+);

+ 169 - 0
applet/database/msg_log.php

@@ -0,0 +1,169 @@
+<?php
+
+$status = array
+(
+	1 => '发送中',
+	2 => '发送成功',
+	3 => '发送失败',
+);
+
+return array
+(
+	# 表名
+	'name' => 'msg_log',
+	# 显示给用户看的名称
+	'lang' => '模板消息日志',
+	'order' => 90,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'project_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '项目',
+			'default' 	=> '',
+			'desc' 		=> '请输入项目',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'msg_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '消息id',
+			'default' 	=> '',
+			'desc' 		=> '消息id',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'list'		=> '',
+		),
+
+		'touser'		=> array
+		(
+			'type' 		=> 'varchar-120',
+			'name' 		=> '接收者',
+			'default' 	=> '',
+			'desc' 		=> '接收者',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
+		'page'		=> array
+		(
+			'type' 		=> 'varchar-200',
+			'name' 		=> '跳转页面',
+			'default' 	=> '',
+			'desc' 		=> '跳转页面',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			//'list'		=> true,
+		),
+
+		'data'		=> array
+		(
+			'type' 		=> 'text-255',
+			'name' 		=> '模板内容',
+			'default' 	=> '',
+			'desc' 		=> '模板内容',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			//'list'		=> true,
+		),
+
+		'form_id'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '表单或者支付id',
+			'default' 	=> '',
+			'desc' 		=> '表单或者支付id',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			//'list'		=> true,
+		),
+
+		'emphasis_keyword'		=> array
+		(
+			'type' 		=> 'varchar-200',
+			'name' 		=> '模板需要放大的关键词',
+			'default' 	=> '',
+			'desc' 		=> '模板需要放大的关键词',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			//'list'		=> true,
+		),
+
+		'num'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '发送次数',
+			'default' 	=> '0',
+			'desc' 		=> '发送次数',
+			'match' 	=> 'option',
+			//'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'result'		=> array
+		(
+			'type' 		=> 'text-255',
+			'name' 		=> '微信返回信息',
+			'default' 	=> '',
+			'desc' 		=> '微信返回信息',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			'list'		=> true,
+			'modal'		=> '查看详情',
+		),
+
+		'status'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '状态',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $status,
+			'list'		=> true,
+		),
+		
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '更新时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			'default' 	=> '',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'top' => Dever::config('base', 'applet')->top,
+
+	'manage' => array
+	(
+		'delete' => false,
+		'insert' => false,
+		'edit' => false,
+	),
+);

+ 7 - 0
applet/index.php

@@ -0,0 +1,7 @@
+<?php
+define('DEVER_APP_NAME', 'applet');
+define('DEVER_APP_LANG', '小程序');
+define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
+define('DEVER_MANAGE_ORDER', 90);
+define('DEVER_MANAGE_ICON', 'glyphicon glyphicon-tint');
+include(DEVER_APP_PATH . '../boot.php');

+ 54 - 0
applet/src/Auth.php

@@ -0,0 +1,54 @@
+<?php
+/*
+|--------------------------------------------------------------------------
+| auth.php 用于做权限验证
+|--------------------------------------------------------------------------
+*/
+namespace applet\Src;
+
+use Dever;
+use Main\Lib\Wechat;
+
+class Auth
+{
+	/**
+	 * wechat
+	 *
+	 * @var Wechat
+	 */
+	private $wechat;
+	
+	/**
+	 * result
+	 *
+	 * @var array
+	 */
+	private $result;
+	
+	/**
+	 * output
+	 *
+	 * @var string
+	 */
+	private $output;
+	
+	/**
+     * 构造函数 初始化
+     * 
+     * @return mixed
+     */
+	public function __construct()
+	{
+		$this->wechat = new Wechat();
+	}
+
+	/**
+     * 获取token 一般为系统token
+     * 
+     * @return mixed
+     */
+	public function token_api()
+	{
+		return $this->wechat->token();
+	}
+}

+ 146 - 0
applet/src/Msg.php

@@ -0,0 +1,146 @@
+<?php
+/*
+|--------------------------------------------------------------------------
+| 模板消息
+|--------------------------------------------------------------------------
+*/
+namespace Applet\Src;
+
+use Dever;
+use Main\Lib\Wechat;
+use Main\Lib\Core;
+
+class Msg
+{
+	# 发送模板消息
+	public function send()
+	{
+		$id = Dever::input('id');
+		if ($id > 0) {
+			$info = Dever::db('applet/msg')->one($id);
+			if ($info) {
+				$update = array();
+				$update['project_id'] = $info['project_id'];
+				$update['msg_id'] = $info['id'];
+				$update['touser'] = Dever::input('touser');
+				$update['page'] = Dever::input('page');
+				$update['data'] = Dever::input('data');
+				$update['form_id'] = Dever::input('form_id');
+				$update['emphasis_keyword'] = Dever::input('emphasis_keyword');
+
+				if (!$update['form_id']) {
+					$update['form_id'] = $this->getFormId();
+				}
+
+				$where = array();
+				$where['msg_id'] = $info['id'];
+				$where['touser'] = $update['touser'];
+				$where['data'] = $update['data'];
+				$where['form_id'] = $update['form_id'];
+				$where['page'] = $update['page'];
+
+				$id = Dever::upinto('applet/msg_log', $where, $update);
+
+				Core::run($info['project_id'], 'send_msg', 'msg.sendAction', 'msg.sendLog', 'applet', $id);
+			}
+		}
+
+		return 'ok';
+	}
+
+	public function getFormId()
+	{
+		# 获取一个form_id,取最新的100条,然后随机取
+		$info = Dever::db('applet/msg_form')->getAll();
+		if ($info) {
+			$key = array_rand($info, 1);
+			$info = $info[$key];
+			Dever::db('applet/msg_form')->delete($info['id']);
+			return $info['form_id'];
+		} else {
+			Dever::alert('错误的form_id');
+		}
+	}
+
+	# 记录form_id 之后加上清理过期的form_id 七天过期
+	public function write_form_id()
+	{
+		$id = Dever::input('id');
+		if ($id > 0) {
+			$info = Dever::db('applet/msg')->one($id);
+
+			$update = array();
+			$update['project_id'] = $info['project_id'];
+			$update['form_id'] = Dever::input('form_id');
+
+			$where = array();
+			$where['project_id'] = $info['project_id'];
+			$where['form_id'] = $update['form_id'];
+
+			$id = Dever::upinto('applet/msg_form', $where, $update);
+		}
+
+		return 'ok';
+	}
+
+	public function sendAction($id)
+	{
+		$info = Dever::db('applet/msg_log')->one($id);
+		if ($info) {
+			$msg = Dever::db('applet/msg')->one($info['msg_id']);
+			$info['template_id'] = $msg['template_id'];
+			if ($info['data']) {
+				$info['data'] = json_decode($info['data'], true);
+			}
+			return $info;
+		} else {
+			die;
+		}
+	}
+
+	public function sendLog($project_id, $data, $id = false)
+	{
+		$info = Dever::db('applet/msg_log')->one($id);
+		if ($info) {
+			$update['where_id'] = $info['id'];
+			$update['result'] = json_encode($data);
+			if ($data['errmsg'] == 'ok') {
+				$update['status'] = 2;
+			} else {
+				$update['status'] = 3;
+			}
+			$update['num'] = $info['num'] + 1;
+			Dever::db('applet/msg_log')->update($update);
+		}
+	}
+
+
+	# 同步微信消息模板到本地
+	public function sync()
+	{
+		$project = Dever::db('main/project')->state(array('option_type' => 3));
+		if ($project) {
+			foreach ($project as $k => $v) {
+				Core::run($v['id'], 'get_msg', '', 'msg.load', 'applet');
+			}
+		}
+
+		return 'ok';
+	}
+
+	public function load($project_id, $data, $param = false)
+	{
+		if (isset($data['list']) && $data['list']) {
+			foreach ($data['list'] as $k => $v) {
+				$update = array();
+				$update['project_id'] = $project_id;
+				$update['template_id'] = $v['template_id'];
+				$update['content'] = $v['content'];
+				$update['name'] = $v['title'];
+				$update['example'] = $v['example'];
+				$id = Dever::upinto('applet/msg', array('option_project_id' => $project_id, 'option_template_id' => $v['template_id']), $update);
+			}
+		}
+		return array();
+	}
+}

+ 16 - 0
component/config/base.php

@@ -0,0 +1,16 @@
+<?php
+
+# 一些基本配置
+$config['base'] = array
+(
+	# 后台头部菜单
+	'top' => 'main/project_id__5',
+);
+
+# 模板配置
+$config['template'] = array
+(
+	
+);
+
+return $config;

+ 1 - 1
component/database/auditing.php

@@ -180,7 +180,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/domain.php

@@ -99,7 +99,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/page.php

@@ -115,7 +115,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/publish.php

@@ -145,7 +145,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/revert.php

@@ -145,7 +145,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/tabbar.php

@@ -164,7 +164,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/template.php

@@ -86,7 +86,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/tester.php

@@ -109,7 +109,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/user.php

@@ -180,7 +180,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/version.php

@@ -143,7 +143,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 1 - 1
component/database/version_log.php

@@ -149,7 +149,7 @@ return array
 		),
 	),
 
-	'top' => Dever::config('base')->top,
+	'top' => Dever::config('base', 'component')->top,
 
 	'manage' => array
 	(

+ 4 - 2
component/src/Auth.php

@@ -55,8 +55,10 @@ class Auth
 		$param['component_access_token'] = $this->wechat->token();
 		$param['component_appid'] = $project['appid'];
 		$param['grant_type'] = 'authorization_code';
-		$url = 'https://api.weixin.qq.com/sns/component/jscode2session?' . http_build_query($param);
-		$data = Dever::curl($url);
+
+		$data = $this->wechat->curl('applet_login', $param, false);
+		//$url = 'https://api.weixin.qq.com/sns/component/jscode2session?' . http_build_query($param);
+		//$data = Dever::curl($url);
 
 		echo $data;die;
 	}

+ 3 - 1
main/database/project.php

@@ -88,7 +88,7 @@ return array
 			'name' 		=> '通信token(注意不是access_token)',
 			'default' 	=> '',
 			'desc' 		=> '请输入通信token',
-			'match' 	=> 'is_string',
+			'match' 	=> 'option',
 			'update'	=> 'text',
 		),
 
@@ -140,6 +140,8 @@ return array
 		'key' => 'main/project_id',
 		# 本表中代表名称的字段
 		'col' => 'name',
+		# type类型字段
+		'type' => 'type',
 	),
 	
 	# request 请求接口定义

+ 51 - 0
main/lib/Core.php

@@ -0,0 +1,51 @@
+<?php
+namespace Main\Lib;
+
+use Dever;
+
+class Core
+{
+	static public function run($id, $method, $start, $end, $app, $param = false)
+	{
+		$send = array();
+		$send['i'] = $id;
+		$send['t'] = $method;
+		$send['s'] = $start;
+		$send['e'] = $end;
+		$send['a'] = $app;
+		$send['p'] = $param;
+		$result = Dever::daemon(self::url($send), 'main');
+
+		return $result;
+	}
+
+	static public function url($send)
+	{
+		$send = base64_encode(json_encode($send));
+		$url = 'lib/core.call?param=' . $send;
+		return $url;
+	}
+
+	public function call_api()
+	{
+		$send = json_decode(base64_decode(Dever::input('param')), true);
+		if (isset($send['i']) && $send['i'] > 0) {
+			$wechat = new Wechat($send['i'], $send['a']);
+			$param = array();
+			if (!isset($send['p'])) {
+				$send['p'] = false;
+			}
+			if ($send['s']) {
+				$param = Dever::load($send['a'] . '/' . $send['s'], $send['p']);
+			}
+			
+			$result = $wechat->curl($send['t'], $param, false);
+			if ($send['e']) {
+				Dever::load($send['a'] . '/' . $send['e'], $send['i'], $result, $send['p']);
+			}
+			print_r($result);
+			die;
+		}
+		return;
+	}
+}