From 1f251a9103ea1ec5f44b27a9fcdc495d21be2838 Mon Sep 17 00:00:00 2001 From: molong Date: Sun, 12 Aug 2018 17:42:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Ad.php | 37 +++++++++++++ application/api/controller/Content.php | 53 ++++++++++++++---- application/api/controller/Index.php | 63 ++++++++++++++++++---- application/api/controller/Login.php | 50 +++++++++++++++++ application/api/controller/Upload.php | 56 ++++++++++++++++++- application/api/controller/Wechat.php | 34 ++++++------ application/api/controller/user/Wechat.php | 36 +++++++++++++ 7 files changed, 289 insertions(+), 40 deletions(-) create mode 100644 application/api/controller/Ad.php create mode 100644 application/api/controller/user/Wechat.php diff --git a/application/api/controller/Ad.php b/application/api/controller/Ad.php new file mode 100644 index 00000000..a992a5d1 --- /dev/null +++ b/application/api/controller/Ad.php @@ -0,0 +1,37 @@ + +// +---------------------------------------------------------------------- + +namespace app\api\controller; + +class Ad extends \app\common\controller\Api{ + + + public function lists($name = '', $is_cover = 1){ + if ($name) { + $place_id = db('AdPlace')->where('name', $name)->value('id'); + + $map['place_id'] = $place_id; + if ($is_cover) { + $map['cover_id'] = array('GT', 0); + } + $res = db('Ad')->where($map)->select(); + + $list = array(); + foreach ($res as $key => $value) { + $value['cover'] = $this->serverurl . get_cover($value['cover_id'], 'path'); + $list[] = $value; + } + $this->data['data'] = $list; + return $this->data; + }else{ + $this->data['code'] = 1; + return $this->data; + } + } +} \ No newline at end of file diff --git a/application/api/controller/Content.php b/application/api/controller/Content.php index 39aab949..34fcd722 100644 --- a/application/api/controller/Content.php +++ b/application/api/controller/Content.php @@ -3,21 +3,54 @@ namespace app\api\controller; class Content extends \app\common\controller\Api{ - public function lists($page = 1, $pagesize = 25){ - $list = db('Article')->page($page . ',' . $pagesize)->order('id desc')->select(); - foreach($list as $key => $value){ - $list[$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']); - $list[$key]['cover'] = 'https://www.tensent.cn' . get_cover($value['cover_id'], 'path'); + public function category(){ + $model_id = $this->request->param('model_id', 0); + + $map = array(); + if ($model_id) { + $map['model_id'] = $model_id; } - $this->data['list'] = $list; + + $list = db('Category')->where($map)->order('sort asc')->select(); + + $this->data['data'] = $list; return $this->data; } - public function detail($id = 0){ - $info = db('Article')->where('id', $id)->find(); + public function lists(){ + $pagesize = $this->request->param('pagesize', 25); + $category_id = $this->request->param('category_id', 0); + $model = $this->request->param('model', 'Article'); + + $map = array(); + if ($category_id) { + $map['category_id'] = $category_id; + } + + $res = db($model)->where($map)->order('id desc')->paginate($pagesize); + $data = $res->toArray(); + + $list = array(); + foreach($data['data'] as $key => $value){ + $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']); + $value['cover'] = $this->serverurl . get_cover($value['cover_id'], 'path'); + $list[] = $value; + } + + $data['data'] = $list; + $this->data['data'] = $data; + return $this->data; + } + + public function detail($id = 0, $model = 'Article'){ + $info = db($model)->where('id', $id)->find(); $info['create_time'] = date('Y-m-d H:i:s', $info['create_time']); - $info['content'] = str_replace('/uploads/', 'https://www.tensent.cn/uploads/', $info['content']); - $info['cover'] = 'https://www.tensent.cn' . get_cover($info['cover_id'], 'path'); + $info['content'] = str_replace('/uploads/', $this->serverurl . '/uploads/', $info['content']); + $info['cover'] = $this->serverurl . get_cover($info['cover_id'], 'path'); + $info['download'] = get_file($info['download_file']); + if (isset($info['download']['url'])) { + $info['download']['url'] = $this->serverurl . $info['download']['url']; + } $this->data['info'] = $info; return $this->data; diff --git a/application/api/controller/Index.php b/application/api/controller/Index.php index 593cfcae..62bd43d2 100644 --- a/application/api/controller/Index.php +++ b/application/api/controller/Index.php @@ -1,26 +1,67 @@ data['data'] = array('team_num'=>100,'coustomer_num'=>10000); + public function getconfig(){ + $map = array( + 'status' => 1, + 'group' => array('NOTIN', array('0', '1', '2', '3','99')) + ); + $this->data['config'] = model('Config')->lists($map); return $this->data; } - public function getlive(){ - $fp = fopen(ROOT_PATH . DS . 'live.txt', 'r'); - $info = fread($fp, filesize(ROOT_PATH . DS . 'live.txt')); - fclose($fp); - $res = explode("\n", $info); - foreach ($res as $value) { - $val = explode(",", $value); - $data[] = array('name'=>$val[0],'src'=>$val[1]); + public function region(){ + $pname = $this->request->param('pname'); + $map['is_show'] = 1; + if ($pname) { + $upid = db('District')->where(array('name'=>array('LIKE', "%".$pname."%")))->value('id'); + $map['upid'] = $upid; + $data = db('District')->where($map)->column('name'); + $this->data['data'] = $data; + }else{ + $map['upid'] = $this->request->param('pid', 0); + $data = db('District')->where($map)->column('name'); + $this->data['data'] = $data; } + return $this->data; + } - $this->data['data'] = $data; + public function getcode($mobile = ''){ + if ($mobile == '') { + $this->data['code'] = 1; + $this->data['msg'] = "电话号码不能为空!"; + }else{ + $time = db('MobileCode')->where('mobile', $mobile)->value('time'); + if ($time && (time() - $time) < 60) { + $this->data['code'] = 1; + return $this->data; + } + $config = config('msg'); + $easySms = new EasySms($config); + $data = array( + 'mobile' => $mobile, + 'code' => rand(100000, 999999), + 'time' => time() + ); + + try { + $result = $easySms->send($data['mobile'], [ + 'content' => '您本次的验证码为:' . $data['code'] . ',请在1分钟内输入。', + 'template' => '94745' + ] + ); + } catch (Exception $e) { + dump($e); + } + if ($result['qcloud']['status'] == 'success') { + db('MobileCode')->insert($data); + } + } return $this->data; } } \ No newline at end of file diff --git a/application/api/controller/Login.php b/application/api/controller/Login.php index 6fcf39bf..802f3f1d 100644 --- a/application/api/controller/Login.php +++ b/application/api/controller/Login.php @@ -37,4 +37,54 @@ class Login extends \app\common\controller\Api{ return $this->data; } } + + public function register(){ + $code = $this->request->post('code'); + $username = $this->request->post('username'); + $password = $this->request->post('password'); + $other['nickname'] = $this->request->post('nickname', ''); + $other['mini_openid'] = $this->request->post('openid', ''); + $other['mobile'] = $this->request->post('mobile', ''); + $other['invite'] = $this->request->post('invite', ''); + $client = $this->request->post('client', 'wxapp'); + + if ($client == 'wxapp') { + $user = db('Member')->where('mini_openid', $other['mini_openid'])->find(); + if ($user) { + $user['access_token'] = authcode($user['uid'].'|'.$user['username'].'|'.$user['password'], 'ENCODE'); + $user['uid'] = $user['uid']; + $user['username'] = $user['username']; + $user['password'] = $user['password']; + $user['avatar'] = (isset($user['avatar_url']) && $user['avatar_url']) ? $user['avatar_url'] : avatar($user['uid']); + $this->data['data'] = $user; + $this->data['code'] = 205; + $this->data['msg'] = "您已注册!"; + return $this->data; + } + } + + //验证手机验证码 + $time = db('MobileCode')->where('code', $code)->where('mobile', $other['mobile'])->value('time'); + if (!$time || (time() - $time) > 60*1000) { + //小于60秒则提示验证码过期 + $this->data['code'] = 1; + $this->data['msg'] = "验证码已过期"; + return $this->data; + } + + $result = model('Member')->register($username, $password, $password, $username.'@test.com', false, $other); + if ($result) { + get_coupon('client_register', $result['uid']); + $result['access_token'] = authcode($result['uid'].'|'.$result['username'].'|'.$result['password'], 'ENCODE'); + $result['uid'] = $result['uid']; + $result['username'] = $result['username']; + $result['password'] = $result['password']; + //$result['avatar'] = (isset($user['avatar_url']) && $user['avatar_url']) ? $user['avatar_url'] : avatar($user['uid']); + $this->data['data'] = $result; + }else{ + $this->data['msg'] = model('Member')->getError(); + $this->data['code'] = 1; + } + return $this->data; + } } \ No newline at end of file diff --git a/application/api/controller/Upload.php b/application/api/controller/Upload.php index 82e2eb64..e5ac9cc6 100644 --- a/application/api/controller/Upload.php +++ b/application/api/controller/Upload.php @@ -16,7 +16,9 @@ class Upload extends \app\common\controller\Api { ))->move($config['rootPath'], true, false); if ($info) { - $this->data['data'] = $this->save($config, 'images', $info); + $data = $this->save($config, 'images', $info); + $data['url'] = $this->serverurl . $data['url']; + $this->data['data'] = $data; return $this->data; }else{ $this->data['code'] = 1; @@ -25,6 +27,58 @@ class Upload extends \app\common\controller\Api { } } + public function video(){ + $config = config('video_upload'); + // 获取表单上传文件 例如上传了001.jpg + $file = $this->request->file('file'); + $size = $config['size'] * 1024 * 1024; + $info = $file->validate(array( + 'size' => $size, + 'ext' => $config['ext'], + ))->move($config['rootPath'], true, false); + + if ($info) { + $data = $this->save($config, 'images', $info); + $data['url'] = $this->serverurl . $data['url']; + $this->data['data'] = $data; + return $this->data; + }else{ + $this->data['code'] = 1; + $this->data['msg'] = $file->getError(); + return $this->data; + } + } + + /** + * 下载文件 + * @var view + * @access public + */ + public function download($id, $type = 'File'){ + $file = db($type)->where('id', $id)->find(); + $file_config = config('attachment_upload'); + if (is_file(ROOT_PATH . 'web' . $file['url'])) { + /* 调用回调函数新增下载数 */ + //is_callable($callback) && call_user_func($callback, $args); + + /* 执行下载 *///TODO: 大文件断点续传 + header("Content-Description: File Transfer"); + header('Content-type: ' . $file['ext']); + header('Content-Length:' . $file['size']); + if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { + //for IE + header('Content-Disposition: attachment; filename="' . rawurlencode($file['name']) . '"'); + } else { + header('Content-Disposition: attachment; filename="' . $file['name'] . '"'); + } + readfile(ROOT_PATH . 'web' . $file['url']); + exit; + } else { + $this->error = '文件已被删除!'; + return false; + } + } + /** * 保存上传的信息到数据库 * @var view diff --git a/application/api/controller/Wechat.php b/application/api/controller/Wechat.php index 1c4238b3..aa53ea78 100644 --- a/application/api/controller/Wechat.php +++ b/application/api/controller/Wechat.php @@ -22,31 +22,29 @@ class Wechat extends \app\common\controller\Api{ } public function jscode2session(){ - $app = Factory::miniProgram(array( - 'app_id' => 'wx463e346969cf5366', - 'secret' => 'f4d830487dffc0c37f281420caea6db4', - )); + $app = Factory::miniProgram(config('wechat.miniProgram')); $param = $this->request->param(); $info = $app->auth->session($param['jsCode']); if (isset($info['openid']) && $info['openid']) { //查询用户是否已添加 - $user = db('Member')->where('openid', $info['openid'])->find(); - if (!$user) { - $other = array( - 'avatar_url' => $param['avatar'], - 'nickname' => $param['nickname'], - 'openid' => $info['openid'] - ); - $user = model('Member')->register($param['nickname'], $info['openid'], $info['openid'], $info['openid'].'@wxapp.com', false, $other); - }else{ - model('Member')->where('openid', $info['openid'])->setField('avatar_url', $param['avatar']); + $user = db('Member')->where('mini_openid', $info['openid'])->find(); + //if (!$user) { + // $other = array( + // 'avatar_url' => $param['avatar'], + // 'nickname' => $param['nickname'], + // 'openid' => $info['openid'] + // ); + // $user = model('Member')->register($param['nickname'], $info['openid'], $info['openid'], $info['openid'].'@wxapp.com', false, $other); + // }else{ + // model('Member')->where('openid', $info['openid'])->setField('avatar_url', $param['avatar']); + // } + if ($user) { + model('Member')->where('mini_openid', $info['openid'])->setField('wechat_avatar', $param['avatar']); + $info['access_token'] = authcode($user['uid'].'|'.$user['username'].'|'.$user['password'], 'ENCODE'); + $info['uid'] = $user['uid']; } - - $info['access_token'] = authcode($user['uid'].'|'.$user['username'].'|'.$user['password'], 'ENCODE'); - $info['uid'] = $user['uid']; - $this->data['data'] = $info; }else{ $this->data['code'] = 1; diff --git a/application/api/controller/user/Wechat.php b/application/api/controller/user/Wechat.php new file mode 100644 index 00000000..c4383a89 --- /dev/null +++ b/application/api/controller/user/Wechat.php @@ -0,0 +1,36 @@ + +// +---------------------------------------------------------------------- + +namespace app\api\controller\user; + +use EasyWeChat\Factory; + +class Wechat extends \app\common\controller\Api{ + + public $mustToken = true; + + public function getqrcode(){ + $app = Factory::miniProgram(config('wechat.miniProgram')); + $param = $this->request->param(); + + $response = $app->app_code->getUnlimit('invite_' . $this->user['uid'], array( + 'page' => 'pages/register', + 'auto_color' => true + )); + + if ($response) { + $filename = $response->saveAs('./uploads/qrcode', 'appcode_' . $this->user['uid'] . '.png'); + $this->data['data'] = $this->serverurl . '/uploads/qrcode/' . $filename; + }else{ + $this->data['code'] = 1; + $this->data['msg'] = $response; + } + return $this->data; + } +} \ No newline at end of file