unique)){ $this->unique = ""; } $v = 6; $arr = [ 'bind_store' => "api:WindowsApiForm:bind_store:{$this->unique}_{$v}", # 绑定店铺 ]; return $arr; } /** * @ Author : Lw * @ CreateTime : 2022-07-07 * @ Info : 获取二维码状态 */ public function actionStatus(){ $data = "rwrwe"; $key = ""; $method = "AES-128-CBC"; echo "
";
// var_dump(openssl_get_cipher_methods());
// $ivlen = openssl_cipher_iv_length($method);
// $iv = openssl_random_pseudo_bytes($ivlen);
$iv = random_bytes(openssl_cipher_iv_length($method));
$ciphertext = openssl_encrypt($data, $method, $key,OPENSSL_RAW_DATA ,$iv);
var_dump(base64_encode($ciphertext));
$ciphertext_ = openssl_decrypt($ciphertext, $method, $key,OPENSSL_RAW_DATA ,$iv);
var_dump($ciphertext_);
exit();
echo "";
$res = $this->code2session("test");
if($res['code'] != 0)
var_dump(111,$res);
return $res;
$session_key = $res['data']['session_key'];
$openid = $res['data']['openid'];
$res = $this->decrypted_data($session_key);
if($res['code'] != 0){
var_dump(222,$res);
return $res;
}
}
/***
* 用户数据解密
*/
private function decrypted_data($session_key){
$pc = new WxBizDataCrypt($this->wechat_mp->appId, $session_key);
$errCode = $pc->decryptData($this->encrypted_data, $this->iv, $data );
if ($errCode == 0) {
return [
'code' => 0,
'msg' => 'success',
'data' => $data
];
} else {
return [
'code' => 1,
'msg' => $errCode,
];
}
}
private function code2session($code)
{
$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_mp->appId}&secret={$this->wechat_mp->appSecret}&js_code={$code}&grant_type=authorization_code";
$this->wechat_mp->curl->get($api);
if($this->wechat_mp->curl->error_code != 0){
return [
'code' => 1,
'msg' => "err_code:{$this->wechat_mp->curl->error_code}err_msg:{$this->wechat_mp->curl->error_msg}"
];
}
$resp = $this->wechat_mp->curl->response;
$res = json_decode($resp, true);
if(!isset($res['openid'])){
return [
'code' => 1,
'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error'
];
}
return [
'code' => 0,
'msg' => 'ok',
'data' => $res
];
}
// 获取用户缓存name,可适用于更新用户头像和昵称使用
public function getUserCacheName($id){
// id -- 用户id
return [
'user' => "api:getUserInfo_v3:{$id}", # 用户
'coach' => "api:getCoachInfo_v8:{$id}", # 教练
];
}
// 获取用户信息
public function getUserInfo($id){
$cache_name = $this->getUserCacheName($id)['user'];
$get = FlashStorage::getCache($cache_name);
$time = 60*60*24;
if(!empty($get)){
$res = json_decode($get,true);
}else{
$find = User::findOne([
'id' => $id,
]);
if(empty($find)){
$time = 60*10;
$res = [
'nickname' => base64_encode('暂无用户 -- '),
'img' => 'https://cloudlinker-pubulic.oss-cn-beijing.aliyuncs.com/62aa8d6322ee0a486ead61fd/ef1dee5f87.jpg',
];
}else{
$res = [
'nickname' => base64_encode($find->nickname),
'img' => SiteHelper::getFullUrl($find->avatar_url),
];
}
try{
FlashStorage::setCache($cache_name,json_encode($res,JSON_UNESCAPED_UNICODE),$time);
}catch (\Exception $e){
}
}
$arr = [
'nickname' => base64_decode($res['nickname']),
'img' => $res['img'],
];
return $arr;
}
// 获取教练信息
public function getCoachInfo($id){
$cache_name = $this->getUserCacheName($id)['coach'];
$get = FlashStorage::getCache($cache_name);
$time = 60*60*24;
if(!empty($get)){
$res = json_decode($get,true);
}else{
$find = User::findOne([
'id' => $id,
]);
$find_coach = Coach::findOne([
'user_id' => $id,
'is_delete' => 0,
]);
if(empty($find) || empty($find_coach)){
$time = 60*1;
$res = [
'nickname' => base64_encode('暂无用户 -- '),
'img' => 'https://cloudlinker-pubulic.oss-cn-beijing.aliyuncs.com/62aa8d6322ee0a486ead61fd/ef1dee5f87.jpg',
];
if(!empty($find)){
$res['nickname'] = base64_encode($find->real_name);
}
if(!empty($find_coach)){
$res['img'] = SiteHelper::getFullUrl($find_coach->coach_photo);
}
}else{
$res = [
'nickname' => base64_encode($find->real_name),
'img' => SiteHelper::getFullUrl($find_coach->coach_photo),
];
}
try{
FlashStorage::setCache($cache_name,json_encode($res,JSON_UNESCAPED_UNICODE),$time);
}catch (\Exception $e){
}
}
$arr = [
'nickname' => base64_decode($res['nickname']),
'img' => $res['img'],
];
return $arr;
}
// 解除绑定
public function actionUnbind(){
if(empty($this->unique)){
return $this->apiReturnError('非法请求');
}
if(empty($this->token)){
return $this->apiReturnError('非法请求');
}
if($this->token != $this->token_en){
return $this->apiReturnError('非法请求');
}
$cache = $this->getCacheName();
$get = FlashStorage::getCache($cache['bind_store']);
if($get == 'error'){
return $this->apiReturnError('店铺未绑定');
}
if(empty($get)){
$find = DeviceUniqueBindStore::findOne([
'unique' => $this->unique,
'is_delete' => 0,
]);
if(empty($find)){
FlashStorage::setCache($cache['bind_store'],'error',60*60*24);
return $this->apiReturnError('店铺未绑定');
}else{
$get = json_encode($find->toArray(),JSON_UNESCAPED_UNICODE);
FlashStorage::setCache($cache['bind_store'],$get,60*60*24);
}
}
$json_de = json_decode($get,True);
$find = DeviceUniqueBindUser::find()
->andWhere([
'unique_id' => $json_de['id'],
'is_delete' => 0,
])->andWhere([
'in','status',[1,2]
])->select('*')->one();
if(empty($find)){
return $this->apiReturnSuccess('success');
}
$find->status = 5;
$find->save();
return $this->apiReturnSuccess('success');
}
/**
* showdoc
* @catalog 客户端接口
* @title 上传数据
*/
public function actionUp(){
if(empty($this->unique)){
return $this->apiReturnError('非法请求');
}
if(empty($this->token)){
return $this->apiReturnError('非法请求');
}
if($this->token != $this->token_en){
return $this->apiReturnError('非法请求');
}
if(empty($this->data)){
return $this->apiReturnError('没有请求数据');
}
$cache = $this->getCacheName();
$get = FlashStorage::getCache($cache['bind_store']);
if($get == 'error'){
return $this->apiReturnError('店铺未绑定');
}
if(empty($get)){
$find = DeviceUniqueBindStore::findOne([
'unique' => $this->unique,
'is_delete' => 0,
]);
if(empty($find)){
FlashStorage::setCache($cache['bind_store'],'error',60*60*24);
return $this->apiReturnError('店铺未绑定');
}else{
$get = json_encode($find->toArray(),JSON_UNESCAPED_UNICODE);
FlashStorage::setCache($cache['bind_store'],$get,60*60*24);
}
}
$get = json_decode($get,true);
// 获取最新的一条数据
$find_user = DeviceUniqueBindUser::findOne([
'is_delete' => 0,
'unique_id' => $get['id'],
'status' => 1,
]);
$uid = 0;
$bind_id = 0;
if(!empty($find_user)){
$uid = $find_user->uid;
$bind_id = $find_user->id;
}
try{
$json_de = json_decode($this->data,True);
}catch (\Exception $e){
return $this->apiReturnError('请求数据异常');
}catch (\Error $e){
return $this->apiReturnError('请求数据异常');
}
foreach ($json_de as $key=>$val){
if(empty($val)){
continue;
}
$pole_type = "";
$pole_class = "";
if(!empty(($val['pole_type']))){
$pole_type = $val['pole_type'];
unset($val['pole_type']);
}
if(!empty(($val['pole_class']))){
$pole_class = $val['pole_class'];
unset($val['pole_class']);
}
$obj = new DeviceUniqueData();
$obj->bind_id = $bind_id;
$obj->data = json_encode($val,JSON_UNESCAPED_UNICODE);
$obj->created_at = time();
$obj->is_delete = 0;
$obj->deleted_at = 0;
$obj->uid = $uid;
$obj->pole_type = strtolower($pole_type);
$obj->pole_class = strtoupper(mb_substr($pole_class,0,2));
$obj->pole_class_all = $pole_class;
$obj->save();
}
$file_folder_name = "Temp_暂无用户_0";
$file_name = Date('Ymd_His',time())."_time_".microtime(true);
$return = [
'status' => 2,
'file_folder_name' => $file_folder_name,
'file_name' => $file_name,
];
if(!empty($find_user)){
// 变更数据状态
$find_user->status = 3;
$find_user->end_at = time();
$find_user->save();
// 判断是否有订单号
if(!empty($find_user->order_id)){
// 订单号变更为已完成
Order::updateAll([
'status' => 0,
'updated_at' => time(),
],[
'id' => $find_user->order_id,
]);
}
$find_user_ = User::findOne([
'id' => $find_user->uid,
]);
if(!empty($find_user_)){
$real_name = !empty($find_user_->real_name)?$find_user_->real_name:$find_user_->username;
$file_folder_name = "{$real_name}_{$find_user_->id}";
$file_name = Date('Ymd_His',time())."_{$find_user->id}";
}else{
$file_folder_name = "Temp_暂无用户_{$find_user->uid}";
$file_name = Date('Ymd_His',time())."_{$find_user->id}";
}
$return = [
'status' => 1,
'file_folder_name' => $file_folder_name,
'file_name' => $file_name,
];
}
return $this->apiReturnSuccess('success',$return);
}
/**
* @ Author : Lw
* @ CreateTime : 2022-07-07
* @ Info : 测试
*/
public function actionTest(){
if(empty($this->test_type)){
return $this->apiReturnError('类型未填写');
}
if(empty($this->unique)){
return $this->apiReturnError('设备码未填写');
}
if(empty($this->token)){
return $this->apiReturnError('非法请求');
}
if($this->token != $this->token_en){
return $this->apiReturnError('非法请求');
}
switch ($this->test_type){
case "1": # 测试绑定
$cache = $this->getCacheName();
$find = DeviceUniqueBindStore::findOne([
'unique' => $this->unique,
'is_delete' => 0,
]);
if(!empty($find)){
FlashStorage::deleteCache($cache['bind_store']);
return $this->apiReturnError('已绑定');
}
$obj = new DeviceUniqueBindStore();
$obj->unique = $this->unique;
$obj->store_id = 1;
$obj->uid = 1;
$obj->created_at = time();
$obj->is_delete = 0;
$obj->deleted_at = 0;
if($obj->save()){
FlashStorage::deleteCache($cache['bind_store']);
return $this->apiReturnSuccess('绑定成功');
}
FlashStorage::setCache($cache['bind_store'],'error',60*60*24);
return $this->apiReturnError('绑定失败',$this->getModelError($obj));
break;
case "2": # 测试绑定用户
$cache = $this->getCacheName();
$find = DeviceUniqueBindStore::findOne([
'unique' => $this->unique,
'is_delete' => 0,
]);
if(empty($find)){
return $this->apiReturnError('未绑定店铺');
}
$find_bind = DeviceUniqueBindUser::find()
->andWhere([
'unique_id' => $find['id'],
'is_delete' => 0,
])->andWhere([
'in','status',[1,2]
])->select('*')->one();
$uid = 1;
if(empty($find_bind)){
# 创建一条数据
$obj = new DeviceUniqueBindUser();
$obj->unique_id = $find['id'];
$obj->uid = $uid;
$obj->c_id = $uid;
$obj->created_at = time();
$obj->start_at = time();
$obj->end_at = 0;
$obj->status = 1;
$obj->is_delete = 0;
$obj->deleted_at = 0;
if($obj->save()){
return $this->apiReturnSuccess('绑定成功');
}else{
return $this->apiReturnError('绑定失败');
}
}
# 判断是否有绑定
if(empty($find_bind->uid)){
$find_bind->uid = $uid;
$find_bind->start_at = time();
if(!$find_bind->save()) {
return $this->apiReturnError('绑定失败');
}
}
# 判断是否开始
if($find_bind->status != 1 && !empty($find_bind->uid) && !empty($find_bind->c_id)){
$find_bind->status = 1;
$find_bind->save();
}
return $this->apiReturnSuccess('绑定成功');
break;
default:
return $this->apiReturnError('类型出错');
}
}
}