IndexController.php
7.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use think\Db;
use app\activity\model\ActivityModel;
use EasyWeChat\Foundation\Application;
/**
* @title 欢迎页
* @description 欢迎使用在线接口文档
*/
class IndexController extends HomeBaseController
{
function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
if (cmf_is_wechat()) {
if (empty(session('user.id'))) {
require_once EXTEND_PATH . '/WeChatCommon.php';
$wx = new \WeChatCommon();
if (request()->param('code') == NULL) {
$wx->code();
} else {
$code = request()->param('code');
$info = $wx->getOpenid($code);
if (Db::name('user')->where(['openid' => $info['openid']])->count() == 0) {
//注册新用户
//拉去用户信息
$return = $wx->getUserInfo($info);
$data = [
'user_type' => 2,
'create_time' => time(),
'user_nickname' => $return['nickname'],
'sex' => $return['sex'],
'avatar' => $return['headimgurl'],
'openid' => $info['openid']
];
if (Db::name('user')->insert($data)) {
$userId = Db::name('user')->getLastInsID();
session('user.id', $userId);
session('user.openid', $info['openid']);
}
} else {
$userId = Db::name('user')->where(['openid' => $info['openid']])->value('id');
session('user.id', $userId);
session('user.openid', $info['openid']);
}
}
}
} else {
$this->error('请从微信浏览器打开');
}
}
/**
* @title 接口返回参数说明
* @description 默认访问接口
* @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
* @url /portal/index/api
* @method GET
*
* @return version:版本号
* @return code:错误码
*/
public function api()
{
$data = [
'version' => '1.0.0',
'code' => [
'20000' => '默认成功返回码',
'40000' => '默认错误返回码',
'40001' => '未登录或登录失效',
'40002' => '签名验证失败',
'40003' => '缺少必要参数或参数格式错误',
'40004' => '登录失败',
]
];
echo json_encode(['msg' => $data]);
exit();
}
/**
* @title 首页
* @description 默认访问接口
* @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
* @url /portal/Index/index
* @method POST
*
* @return version:版本号
* @return code:错误码
*/
public function index()
{
//首页轮播图
$banner = Db::name('slide_item')->field('image,url')->where(['slide_id' => 1])->order('list_order')->select();
foreach ($banner as $k => $v) {
$v['image'] = cmf_get_image_url($v['image']);
$banner[$k] = $v;
}
//分类
$type = Db::name('type')->field('id as t_id,type_name,type_url,type_icon')->order('listorder')->select();
//活动
$model = new ActivityModel;
$t_id = request()->param('t_id');
$keyword = request()->param('keyword');
$is_new = $model->activityList($t_id, time(), session('user.id'), $keyword, 1, '');
$is_hot = $model->activityList($t_id, time(), session('user.id'), $keyword, '', 1);
$result = [
'banner' => $banner,
'type' => $type,
'is_new' => $is_new,
'is_hot' => $is_hot
];
if (request()->isPost()) {
echo json_encode(['data' => $result, 'code' => 20000]);
exit();
} else {
return $this->fetch('', [
'result' => $result
]);
}
}
public function bindMobile()
{
$request = request();
if ($request->isAjax()) {
$user_id = session('user.id');
$tel = $request->param('tel');
$is_first = Db::name('user')->where(['id' => session('user.id')])->value('is_first');
if ($is_first == 1) {
give_as_a_present($user_id, '首次下单赠送');
Db::name('user')->update(['id' => $user_id, 'tel' => $tel, 'is_first' => 0]);
echo json_encode(['msg' => '绑定成功']);
//领取优惠券
exit();
}
}
}
//更新用户经纬度
public function updatePosition()
{
$position = [
'id' => session('user.id'),
'lat' => request()->param('lat'),
'lng' => request()->param('lng')
];
if (Db::name('user')->update($position)) {
$this->success('', '', true);
}
}
public function alipay()
{
header("Content-type: text/html; charset=utf-8");
require_once EXTEND_PATH . '/alipay/wappay/service/AlipayTradeService.php';
require_once EXTEND_PATH . '/alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
require EXTEND_PATH . '/alipay/config.php';
if (1 == 1) {
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = '20181017210243564' . rand(10000, 99999);
//订单名称,必填
$subject = '购买';
//付款金额,必填
$total_amount = 0.01;
//商品描述,可空
$body = '商品';
//超时时间
$timeout_express = "1m";
$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setTimeExpress($timeout_express);
$payResponse = new \AlipayTradeService($config);
$result = $payResponse->wapPay($payRequestBuilder, $config['return_url'], $config['notify_url']);
}
}
}