123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- // +---------------------------------------------------------------------+
- // | NiuCloud | [ WE CAN DO IT JUST NiuCloud ] |
- // +---------------------------------------------------------------------+
- // | Copy right 2019-2029 www.niucloud.com |
- // +---------------------------------------------------------------------+
- // | Author | NiuCloud <niucloud@outlook.com> |
- // +---------------------------------------------------------------------+
- // | Repository | https://github.com/niucloud/framework.git |
- // +---------------------------------------------------------------------+
- namespace addon\weapp\model;
- use app\model\BaseModel;
- use EasyWeChat\Factory;
- /**
- * 微信小程序配置
- */
- class Weapp extends BaseModel
- {
- private $app;//微信模型
- public function __construct()
- {
- //微信支付配置
- $config_model = new Config();
- $config_result = $config_model->getWeappConfig();
- $config = $config_result["data"];
- if(!empty($config)){
- $config_info = $config["value"];
- }
- $config = [
- 'app_id' => $config_info["appid"] ?? '',
- 'secret' => $config_info["appsecret"] ?? '',
- // 下面为可选项
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- 'log' => [
- 'level' => 'debug',
- 'permission' => 0777,
- 'file' => 'runtime/log/wechat/easywechat.logs',
- ],
- ];
- $this->app = Factory::miniProgram($config);
- }
- /**
- * TODO
- * 根据 jsCode 获取用户 session 信息
- * @param $param
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- */
- public function authCodeToOpenid($param){
- //正常返回的JSON数据包
- //{"openid": "OPENID", "session_key": "SESSIONKEY", "unionid": "UNIONID"}
- //错误时返回JSON数据包(示例为Code无效)
- //{"errcode": 40029, "errmsg": "invalid code"}
- $result = $this->app->auth->session($param['code']);
- if (isset($result['errcode'])) {
- return $this->error('', $result['errmsg']);
- } else{
- return $this->success($result['openid']);
- }
- }
-
- /**
- * 生成二维码
- * @param unknown $param
- */
- public function createQrcode($param) {
- try {
- $checkpath_result = $this->checkPath($param['qrcode_path']);
- if($checkpath_result["code"] != 0) return $checkpath_result;
-
- // scene:场景值最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~
- $scene = '';
- if (!empty($param['data'])) {
- foreach ($param['data'] as $key => $value) {
- if ($scene == '') $scene .= $key .'-'. $value;
- else $scene .= '&' . $key .'-'. $value;
- }
- }
- $response = $this->app->app_code->getUnlimit($scene, [
- 'page' => substr($param['page'], 1),
- 'width' => isset($param['width']) ? $param['width'] : 120
- ]);
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- $filename = $param['qrcode_path'] . '/';
- $filename .= $response->saveAs($param['qrcode_path'], $param['qrcode_name'] . '_' . $param['app_type'] . '.png');
- return $this->success(['path' => $filename]);
- }
- } catch (\Exception $e) {
- return $this->error('', $e->getMessage());
- }
- }
-
- /**
- * 校验目录是否可写
- * @param unknown $path
- * @return multitype:number unknown |multitype:unknown
- */
- private function checkPath($path)
- {
- if (is_dir($path) || mkdir($path, intval('0755', 8), true)) {
- return $this->success();
- }
- return $this->error('', "directory {$path} creation failed");
- }
- /************************************************************* 数据统计与分析 start **************************************************************/
- /**
- * 访问日趋势
- * @param $from 格式 20170313
- * @param $to 格式 20170313
- */
- public function dailyVisitTrend($from, $to){
- try {
- $result = $this->app->data_cube->dailyVisitTrend($from, $to);
- if (isset($result['errcode']) && $result['errcode'] != 0) {
- return $this->error([], $result["errmsg"]);
- }
- return $this->success($result["list"]);
- } catch (\Exception $e) {
- return $this->error([], $e->getMessage());
- }
- }
- /**
- * 访问周趋势
- * @param $from
- * @param $to
- * @return array|\multitype
- */
- public function weeklyVisitTrend($from, $to){
- try {
- $result = $this->app->data_cube->weeklyVisitTrend($from, $to);
- if (isset($result['errcode']) && $result['errcode'] != 0) {
- return $this->error([], $result["errmsg"]);
- }
- return $this->success($result["list"]);
- } catch (\Exception $e) {
- return $this->error([], $e->getMessage());
- }
- }
- /**
- * 访问月趋势
- * @param $from
- * @param $to
- * @return array|\multitype
- */
- public function monthlyVisitTrend($from, $to){
- try {
- $result = $this->app->data_cube->monthlyVisitTrend($from, $to);
- if (isset($result['errcode']) && $result['errcode'] != 0) {
- return $this->error([], $result["errmsg"]);
- }
- return $this->success($result["list"]);
- } catch (\Exception $e) {
- return $this->error([], $e->getMessage());
- }
- }
- /**
- * 访问分布
- * @param $from
- * @param $to
- */
- public function visitDistribution($from, $to){
- try {
- $result = $this->app->data_cube->visitDistribution($from, $to);
- if (isset($result['errcode']) && $result['errcode'] != 0) {
- return $this->error($result, $result["errmsg"]);
- }
- return $this->success($result["list"]);
- } catch (\Exception $e) {
- return $this->error([], $e->getMessage());
- }
- }
- /**
- * 访问页面
- * @param $from
- * @param $to
- */
- public function visitPage($from, $to){
- try {
- $result = $this->app->data_cube->visitPage($from, $to);
- if (isset($result['errcode']) && $result['errcode'] != 0) {
- return $this->error([], $result["errmsg"]);
- }
- return $this->success($result["list"]);
- } catch (\Exception $e) {
- return $this->error([], $e->getMessage());
- }
- }
- /************************************************************* 数据统计与分析 end **************************************************************/
- }
|