Weapp.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // +---------------------------------------------------------------------+
  3. // | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]  |
  4. // +---------------------------------------------------------------------+
  5. // | Copy right 2019-2029 www.niucloud.com  |
  6. // +---------------------------------------------------------------------+
  7. // | Author | NiuCloud <niucloud@outlook.com>  |
  8. // +---------------------------------------------------------------------+
  9. // | Repository | https://github.com/niucloud/framework.git  |
  10. // +---------------------------------------------------------------------+
  11. namespace addon\weapp\model;
  12. use app\model\BaseModel;
  13. use EasyWeChat\Factory;
  14. /**
  15. * 微信小程序配置
  16. */
  17. class Weapp extends BaseModel
  18. {
  19. private $app;//微信模型
  20. public function __construct()
  21. {
  22. //微信支付配置
  23. $config_model = new Config();
  24. $config_result = $config_model->getWeappConfig();
  25. $config = $config_result["data"];
  26. if(!empty($config)){
  27. $config_info = $config["value"];
  28. }
  29. $config = [
  30. 'app_id' => $config_info["appid"] ?? '',
  31. 'secret' => $config_info["appsecret"] ?? '',
  32. // 下面为可选项
  33. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  34. 'response_type' => 'array',
  35. 'log' => [
  36. 'level' => 'debug',
  37. 'permission' => 0777,
  38. 'file' => 'runtime/log/wechat/easywechat.logs',
  39. ],
  40. ];
  41. $this->app = Factory::miniProgram($config);
  42. }
  43. /**
  44. * TODO
  45. * 根据 jsCode 获取用户 session 信息
  46. * @param $param
  47. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  48. */
  49. public function authCodeToOpenid($param){
  50. //正常返回的JSON数据包
  51. //{"openid": "OPENID", "session_key": "SESSIONKEY", "unionid": "UNIONID"}
  52. //错误时返回JSON数据包(示例为Code无效)
  53. //{"errcode": 40029, "errmsg": "invalid code"}
  54. $result = $this->app->auth->session($param['code']);
  55. if (isset($result['errcode'])) {
  56. return $this->error('', $result['errmsg']);
  57. } else{
  58. return $this->success($result['openid']);
  59. }
  60. }
  61. /**
  62. * 生成二维码
  63. * @param unknown $param
  64. */
  65. public function createQrcode($param) {
  66. try {
  67. $checkpath_result = $this->checkPath($param['qrcode_path']);
  68. if($checkpath_result["code"] != 0) return $checkpath_result;
  69. // scene:场景值最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~
  70. $scene = '';
  71. if (!empty($param['data'])) {
  72. foreach ($param['data'] as $key => $value) {
  73. if ($scene == '') $scene .= $key .'-'. $value;
  74. else $scene .= '&' . $key .'-'. $value;
  75. }
  76. }
  77. $response = $this->app->app_code->getUnlimit($scene, [
  78. 'page' => substr($param['page'], 1),
  79. 'width' => isset($param['width']) ? $param['width'] : 120
  80. ]);
  81. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  82. $filename = $param['qrcode_path'] . '/';
  83. $filename .= $response->saveAs($param['qrcode_path'], $param['qrcode_name'] . '_' . $param['app_type'] . '.png');
  84. return $this->success(['path' => $filename]);
  85. }
  86. } catch (\Exception $e) {
  87. return $this->error('', $e->getMessage());
  88. }
  89. }
  90. /**
  91. * 校验目录是否可写
  92. * @param unknown $path
  93. * @return multitype:number unknown |multitype:unknown
  94. */
  95. private function checkPath($path)
  96. {
  97. if (is_dir($path) || mkdir($path, intval('0755', 8), true)) {
  98. return $this->success();
  99. }
  100. return $this->error('', "directory {$path} creation failed");
  101. }
  102. /************************************************************* 数据统计与分析 start **************************************************************/
  103. /**
  104. * 访问日趋势
  105. * @param $from 格式 20170313
  106. * @param $to 格式 20170313
  107. */
  108. public function dailyVisitTrend($from, $to){
  109. try {
  110. $result = $this->app->data_cube->dailyVisitTrend($from, $to);
  111. if (isset($result['errcode']) && $result['errcode'] != 0) {
  112. return $this->error([], $result["errmsg"]);
  113. }
  114. return $this->success($result["list"]);
  115. } catch (\Exception $e) {
  116. return $this->error([], $e->getMessage());
  117. }
  118. }
  119. /**
  120. * 访问周趋势
  121. * @param $from
  122. * @param $to
  123. * @return array|\multitype
  124. */
  125. public function weeklyVisitTrend($from, $to){
  126. try {
  127. $result = $this->app->data_cube->weeklyVisitTrend($from, $to);
  128. if (isset($result['errcode']) && $result['errcode'] != 0) {
  129. return $this->error([], $result["errmsg"]);
  130. }
  131. return $this->success($result["list"]);
  132. } catch (\Exception $e) {
  133. return $this->error([], $e->getMessage());
  134. }
  135. }
  136. /**
  137. * 访问月趋势
  138. * @param $from
  139. * @param $to
  140. * @return array|\multitype
  141. */
  142. public function monthlyVisitTrend($from, $to){
  143. try {
  144. $result = $this->app->data_cube->monthlyVisitTrend($from, $to);
  145. if (isset($result['errcode']) && $result['errcode'] != 0) {
  146. return $this->error([], $result["errmsg"]);
  147. }
  148. return $this->success($result["list"]);
  149. } catch (\Exception $e) {
  150. return $this->error([], $e->getMessage());
  151. }
  152. }
  153. /**
  154. * 访问分布
  155. * @param $from
  156. * @param $to
  157. */
  158. public function visitDistribution($from, $to){
  159. try {
  160. $result = $this->app->data_cube->visitDistribution($from, $to);
  161. if (isset($result['errcode']) && $result['errcode'] != 0) {
  162. return $this->error($result, $result["errmsg"]);
  163. }
  164. return $this->success($result["list"]);
  165. } catch (\Exception $e) {
  166. return $this->error([], $e->getMessage());
  167. }
  168. }
  169. /**
  170. * 访问页面
  171. * @param $from
  172. * @param $to
  173. */
  174. public function visitPage($from, $to){
  175. try {
  176. $result = $this->app->data_cube->visitPage($from, $to);
  177. if (isset($result['errcode']) && $result['errcode'] != 0) {
  178. return $this->error([], $result["errmsg"]);
  179. }
  180. return $this->success($result["list"]);
  181. } catch (\Exception $e) {
  182. return $this->error([], $e->getMessage());
  183. }
  184. }
  185. /************************************************************* 数据统计与分析 end **************************************************************/
  186. }