From e7500ab2f1d64fbd1964e4ab90ca92c43b51dcdd Mon Sep 17 00:00:00 2001 From: chengxl Date: Tue, 10 Mar 2026 14:48:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Wechatprogram.php | 553 +++++++++++++++++++ 1 file changed, 553 insertions(+) create mode 100644 application/api/controller/Wechatprogram.php diff --git a/application/api/controller/Wechatprogram.php b/application/api/controller/Wechatprogram.php new file mode 100644 index 0000000..8a87ef2 --- /dev/null +++ b/application/api/controller/Wechatprogram.php @@ -0,0 +1,553 @@ +request->post() : $aParam; + + //获取微信登录code + $sLoginCode = empty($aParam['login_code']) ? '' : $aParam['login_code']; + if(empty($sLoginCode)){ + return json_encode(['status' => 2,'msg' => 'Please enter the login code']); + } + + //调用接口 + //url拼接 + $sCode2SessionUrl = $this->sCode2SessionUrl; + $sCode2SessionUrl .= '?appid='.$this->sAppID.'&secret='.$this->sAppSecret.'&js_code='.$sLoginCode.'&grant_type=GRANT_TYPE'; + $aResult = json_decode(myGet($sCode2SessionUrl),true); + if(isset($aResult['errcode'])){ + $sMsg = empty($aResult['errmsg']) ? 'Interface request error:'.$aResult['errcode'] : $aResult['errmsg']; + return json_encode(['status' => 3,'msg' => $sMsg]); + } + + //获取微信登录openid + $sOpenId= empty($aResult['openid']) ? '' : trim($aResult['openid']); + if(empty($sOpenId)){ + return json_encode(['status' => 4,'msg' => 'User login information not obtained']); + } + + //根绝openid获取用户信息 + $aData = []; + $aUser = json_decode($this->getUser(['openid' => $sOpenId,'is_select_role' => 2]),true); + $aUser = empty($aUser['data']) ? [] : $aUser['data']; + if(!empty($aUser)){ + $aData['user'] = $aUser; + } + $aData['wechat'] = $aResult; + return json_encode(['status' => 1,'msg' => 'success','data' => $aData]); + } + + /** + * 根据OPENID查询用户信息 + */ + public function getUser($aParam = []){ + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + + //获取用户ID + $iUserId= empty($aParam['user_id']) ? '' : $aParam['user_id']; + + //获取微信登录openid + $sOpenId= empty($aParam['openid']) ? '' : $aParam['openid']; + if(empty($sOpenId) && empty($iUserId)){ + return json_encode(['status' => 2,'msg' => 'Please enter the login openid']); + } + + //查询用户是否存在 + $aWhere = ['state' => 0]; + if(!empty($iUserId)){ + $aWhere['user_id'] = $iUserId; + } + if(!empty($sOpenId)){ + $aWhere['openid'] = $sOpenId; + } + $aUser = Db::name('user')->field('user_id,account,openid,icon,email,type')->where($aWhere)->find(); + if(empty($aUser)){ + return json_encode(['status' => 2,'msg' => 'No user information found']); + } + + //判断是否查询用户角色 + $iIsSelectRole = empty($aParam['is_select_role']) ? 1 : $aParam['is_select_role']; + if($iIsSelectRole == 1){ + $aUser['roles'] = $this->getUserRoles($aUser); + } + return json_encode(['status' => 1,'msg' => 'success','data' => $aUser]); + } + + /** + * 用户名密码绑定openid + */ + public function bindAccount($aParam = []){ + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + + //账号名 + $sAccount= empty($aParam['account']) ? '' : trim($aParam['account']); + if(empty($sAccount)){ + return json_encode(['status' => 2,'msg' => 'Please enter your account']); + } + //密码 + $sPassword= empty($aParam['password']) ? '' : trim($aParam['password']); + if(empty($sPassword)){ + return json_encode(['status' => 2,'msg' => 'Please enter the password']); + } + //openid + $sOpenId= empty($aParam['openid']) ? '' : trim($aParam['openid']); + // //unionid + // $sUnionId= empty($aParam['unionid']) ? '' : trim($aParam['unionid']); + if(empty($sOpenId)){// && empty($sUnionId) + return json_encode(['status' => 2,'msg' => 'Please enter the login openid/unionid']); + } + + //查询用户是否存在 + $aWhere = ['account|email' => $sAccount,'state' => 0]; + $aUser = Db::name('user')->field('user_id,account,password,openid')->where($aWhere)->find(); + if(empty($aUser)){ + return json_encode(['status' => 3,'msg' => 'Login account does not exist, please confirm']); + } + //验证密码是否一致 + $sPassword = md5($sPassword); + if($aUser['password'] != $sPassword){ + return json_encode(['status' => 4,'msg' => 'Login password input error, please confirm']); + } + //验证是否绑定账号 + if(!empty($aUser['openid'])){// || !empty($aUser['unionid']) + return json_encode(['status' => 5,'msg' => 'Login account already linked to WeChat account']); + } + //验证openid是否被绑定 + $iUserId = empty($aUser['user_id']) ? 0 : $aUser['user_id']; + $aWhere = ['state' => 0,'user_id' => ['<>',$iUserId]]; + // if(!empty($sUnionId)){ + // $aWhere['unionid'] = $sUnionId; + // } + if(!empty($sOpenId)){ + $aWhere['openid'] = $sOpenId; + } + $aUserOpenId = Db::name('user')->field('user_id')->where($aWhere)->find(); + if(!empty($aUserOpenId)){ + return json_encode(['status' => 6,'msg' => 'This WeChat account has been bound']); + } + + //更新 + $aUpdate = []; + // if(!empty($sUnionId)){ + // $aUpdate['unionid'] = $sUnionId; + // } + if(!empty($sOpenId)){ + $aUpdate['openid'] = $sOpenId; + } + if(empty($aUpdate)){ + return json_encode(['status' => 7,'msg' => 'Update data to empty']); + } + + //执行操作 + $aWhere = ['user_id' => $iUserId,'state' => 0]; + $result = Db::name('user')->where($aWhere)->limit(1)->update($aUpdate); + if($result === false){ + return json_encode(['status' => 8,'msg' => "Binding failed"]); + } + // $aUser['unionid'] = empty($aUpdate['unionid']) ? $aUser['unionid'] : $aUpdate['unionid']; + $aUser['openid'] = empty($aUpdate['openid']) ? $aUser['openid'] : $aUpdate['openid']; + return json_encode(['status' => 1,'msg' => 'Binding successful','data' => $aUser]); + } + + /** + * 用户名密码解绑openid + */ + public function unbindAccount(){ + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + + //账号名 + $sAccount= empty($aParam['account']) ? '' : trim($aParam['account']); + if(empty($sAccount)){ + return json_encode(['status' => 2,'msg' => 'Please enter your account']); + } + //密码 + $sPassword= empty($aParam['password']) ? '' : trim($aParam['password']); + if(empty($sPassword)){ + return json_encode(['status' => 2,'msg' => 'Please enter the password']); + } + //openid + $sOpenId= empty($aParam['openid']) ? '' : trim($aParam['openid']); + //unionid + // $sUnionId= empty($aParam['unionid']) ? '' : trim($aParam['unionid']); + if(empty($sOpenId)){// && empty($sUnionId) + return json_encode(['status' => 2,'msg' => 'Please enter the login openid/unionid']); + } + + //查询用户是否存在 + $aWhere = ['account|email' => $sAccount,'state' => 0]; + $aUser = Db::name('user')->field('user_id,account,password,openid')->where($aWhere)->find(); + if(empty($aUser)){ + return json_encode(['status' => 3,'msg' => 'Login account does not exist, please confirm']); + } + //验证密码是否一致 + $sPassword = md5($sPassword); + if($aUser['password'] != $sPassword){ + return json_encode(['status' => 4,'msg' => 'Login password input error, please confirm']); + } + //验证是否绑定账号 + if(empty($aUser['openid'])){// && empty($aUser['unionid']) + return json_encode(['status' => 5,'msg' => 'This account is not bound to any mini program, there is no need to unbind it']); + } + //验证unionid是否相等 + // if(!empty($sUnionId) && !empty($aUser['unionid']) & $sUnionId != $aUser['unionid']){ + // return json_encode(['status' => 6,'msg' => 'Unbind account not bound']); + // } + //验证openid是否相等 + if(!empty($sOpenId) && !empty($aUser['openid']) & $sOpenId != $aUser['openid']){ + return json_encode(['status' => 7,'msg' => 'Unbind account not bound']); + } + $iUserId = empty($aUser['user_id']) ? 0 : $aUser['user_id']; + + //执行操作 + $aWhere = ['user_id' => $iUserId,'state' => 0]; + $aUpdate = ['openid' => ''];//,'unionid' => '' + $result = Db::name('user')->where($aWhere)->limit(1)->update($aUpdate); + if($result === false){ + return json_encode(['status' => 8,'msg' => "Unbinding failed"]); + } + $aUpdate['user_id'] = $iUserId; + return json_encode(['status' => 1,'msg' => 'Unbound successfully','data' => $aUpdate]); + } + + /** + * 用户账号注册 + */ + public function registerAccount(){ + + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + + //邮箱 + $sEmail = empty($aParam['email']) ? '' : trim($aParam['email']); + if(empty($sEmail)){ + return json_encode(['status' => 2,'msg' => 'Please enter your email']); + } + //密码 + $sPassword= empty($aParam['password']) ? '' : trim($aParam['password']); + if(empty($sPassword)){ + return json_encode(['status' => 2,'msg' => 'Please enter the password']); + } + //openid + $sOpenId= empty($aParam['openid']) ? '' : trim($aParam['openid']); + if(empty($sOpenId)){ + return json_encode(['status' => 2,'msg' => 'Please enter the login openid/unionid']); + } + + //查询用户是否存在 + $aWhere = ['account|email' => $sEmail,'state' => 0]; + $aUser = Db::name('user')->field('user_id')->where($aWhere)->find(); + if(!empty($aUser)){ + return json_encode(['status' => 3,'msg' => 'The registered account already exists, please confirm']); + } + //验证OPENID是否绑定 + $aWhere = ['openid' => $sOpenId,'state' => 0]; + $aUser = Db::name('user')->field('user_id')->where($aWhere)->find(); + if(!empty($aUser)){ + return json_encode(['status' => 3,'msg' => 'WeChat account has been bound']); + } + + //数据插入 + Db::startTrans(); + //用户主表 + $aInsert = ['account' => $sEmail,'email' => $sEmail,'password' => md5($sPassword),'ctime' => time(),'openid' => $sOpenId]; + $iId = Db::name('user')->insertGetId($aInsert); + if(empty($iId)){ + return json_encode(['status' => 4,'msg' => 'Registration failed']); + } + //用户附属表 + $aReviewInsert = ['reviewer_id' => $iId,'test_from' => 'wechat_register']; + $iInfoId = Db::name('user_reviewer_info')->insertGetId($aReviewInsert); + if(empty($iInfoId)){ + return json_encode(['status' => 5,'msg' => 'Registration failed']); + } + Db::commit(); + $aInsert['user_id'] = $iId; + return json_encode(['status' => 1,'msg' => 'registered successfully','data' => $aInsert]); + } + /** + * 获取用户身份 + */ + private function getUserRoles($aUser = []){ + if(empty($aUser)){ + return []; + } + + //获取账号名 + $sAccount = empty($aUser['account']) ? '' : trim($aUser['account']); + if($aUser['type'] == 2) { + $aRoles = ['editor']; + if($sAccount=="liuna" || $sAccount=="zhuwenjing"){ + array_push($aRoles, 'superadmin'); + } + return $aRoles; + } + + $aRoles = ['author']; + //查询是否是审稿人 + $iUserId = empty($aUser['user_id']) ? 0 : $aUser['user_id']; + $aWhere = ['reviewer_id' => $iUserId,'state' => 0]; + $aUserInfo = Db::name('reviewer_to_journal')->field('rtj_id')->where($aWhere)->find(); + if(!empty($aUserInfo)) { + array_push($aRoles,'reviewer'); + } + + //青年编委 + $aWhere = ['user_id' => $iUserId,'state' => 0]; + $aUserInfo = Db::name('user_to_yboard')->field('user_id')->where($aWhere)->find(); + if(!empty($aUserInfo)) { + array_push($aRoles,'yboard'); + } + //主编与期刊 + $aWhere = ['user_id' => $iUserId,'state' => 0]; + $aUserInfo = Db::name('chief_to_journal')->field('user_id')->where($aWhere)->find(); + if(!empty($aUserInfo)) { + array_push($aRoles,'chief'); + } + //期刊主编类型 + $aWhere = ['user_id' => $iUserId,'state' => 0]; + $aUserInfo = Db::name('board_to_journal')->field('user_id,type')->where($aWhere)->find(); + if(!empty($aUserInfo)) { + array_push($aRoles,'board'); + $iType = isset($aUserInfo['type']) ? $aUserInfo['type'] : '-1'; + if($iType == 0){ + array_push($aRoles,'chief_editor'); + } + if($iType == 1){ + array_push($aRoles,'deputy_editor'); + } + if($iType == 2){ + array_push($aRoles,'editor_board'); + } + } + + //期刊主编类型 + $aWhere = ['user_id' => $iUserId,'uts_state' => 0]; + $aUserInfo = Db::name('user_to_special')->field('user_id')->where($aWhere)->find(); + if (!empty($aUserInfo)) { + array_push($aRoles,'special'); + } + return $aRoles; + } + /** + * 获取我的稿件 + */ + public function getManuscript($aParam = []){ + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + + //获取用户ID + $iUserId= empty($aParam['user_id']) ? '' : $aParam['user_id']; + //获取状态 + $iState = isset($aParam['state']) ? $aParam['state'] : -2; + //获取微信登录openid + $sOpenId= empty($aParam['openid']) ? '' : $aParam['openid']; + if(empty($sOpenId) && empty($iUserId)){ + return json_encode(['status' => 2,'msg' => 'Please enter your login account']); + } + //标题 + $sTitle = empty($aParam['title']) ? '': $aParam['title']; + //获取用户信息 + $aParam['is_select_role'] = 2; + $aUser = json_decode($this->getUser($aParam),true); + $aUser = empty($aUser['data']) ? [] : $aUser['data']; + if(empty($aUser)){ + return json_encode(['status' => 3,'msg' => 'No user information found']); + } + + //获取分页相关参数 + $iSize = empty($aParam['size']) ? 15 : $aParam['size'];//每页显示条数 + $iPage = empty($aParam['page']) ? 1 : $aParam['page'];// 当前页码 + + $iUserId = empty($aUser['user_id']) ? 0 : $aUser['user_id']; + //获取数量 + $aWhere = ['user_id' => $iUserId,'accept_sn' => ['not like','Draft%']]; + $aWhere['state'] = ['<>',-1]; + if($iState != -2 && $iState != -1){ + $aWhere['state'] = $iState; + } + if($iState == -2){ + $aWhere['state'] = ['<>',-1]; + } + if(!empty($sTitle)){ + $aWhere['title'] =['like','%'.trim($sTitle).'%']; + } + $iCount = Db::name('article')->where($aWhere)->count(); + if(empty($iCount)){ + return json_encode(['status' => 1,'msg' => 'Article not found','data' => ['total' => 0,'lists' => []]]); + } + + //判断页数是否超过最大分页限制 + $iPageNum = ceil($iCount/$iSize); + if($iPage > $iPageNum){ + return json_encode(['status' => 1,'msg' => 'The number of pages has exceeded the limit, maximum page number:'.$iPageNum,'data' => ['total' => $iCount,'lists' => []]]); + } + + //查询详细数据 + $sField = 'article_id,journal_id,accept_sn,title,type,abstrart,ctime,state'; + $sOrder = 'article_id desc'; + $aArticle = Db::name('article') + ->field($sField) + ->where($aWhere) + ->page($iPage, $iSize) + ->order($sOrder) + ->select(); + if(empty($aArticle)){ + return json_encode(['status' => 1,'msg' => 'Data is empty','data' => ['total' => 0,'lists' => []]]); + } + //获取期刊 + $aJournalId = array_unique(array_column($aArticle, 'journal_id')); + $aWhere = ['journal_id' => ['in',$aJournalId],'state' => 0]; + $aJournal = DB::name('journal')->where($aWhere)->column('journal_id,title'); + //数据处理 + foreach ($aArticle as $key => $value) { + $aArticle[$key]['type_name'] = translateType($value['type']); + $aArticle[$key]['journal_title'] = empty($aJournal[$value['journal_id']]) ? '' : $aJournal[$value['journal_id']]; + $aArticle[$key]['ctime'] = empty($value['ctime']) ? '' : date('Y-m-d',$value['ctime']); + } + return json_encode(['status' => 1,'msg' => 'success','data' => ['total' => $iCount,'lists' => $aArticle]]); + } + + /** + * 获取我的消息 + */ + public function getMessagesLists($aParam = []){ + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + + //获取用户ID + $iUserId= empty($aParam['user_id']) ? '' : $aParam['user_id']; + //获取微信登录openid + $sOpenId= empty($aParam['openid']) ? '' : $aParam['openid']; + if(empty($sOpenId) && empty($iUserId)){ + return json_encode(['status' => 2,'msg' => 'Please enter your login account']); + } + //标题 + $sTitle = empty($aParam['title']) ? '': $aParam['title']; + + //状态 + $iIsRead = empty($aParam['is_read']) ? -1 : $aParam['is_read']; + + //获取用户信息 + $aParam['is_select_role'] = 2; + $aUser = json_decode($this->getUser($aParam),true); + $aUser = empty($aUser['data']) ? [] : $aUser['data']; + if(empty($aUser)){ + return json_encode(['status' => 3,'msg' => 'No user information found']); + } + + //获取分页相关参数 + $iSize = empty($aParam['size']) ? 15 : $aParam['size'];//每页显示条数 + $iPage = empty($aParam['page']) ? 1 : $aParam['page'];// 当前页码 + + $iUserId = empty($aUser['user_id']) ? 0 : $aUser['user_id']; + //获取数量 + $aWhere = ['user_id' => $iUserId]; + if(!empty($sTitle)){ + $aWhere['title'] =['like','%'.trim($sTitle).'%']; + } + if(in_array($iIsRead, [1,2])){ + $aWhere['is_read'] = $iIsRead; + } + $iCount = Db::name('messages')->where($aWhere)->count(); + if(empty($iCount)){ + return json_encode(['status' => 1,'msg' => 'Message is empty','data' => ['total' => 0,'lists' => []]]); + } + + //判断页数是否超过最大分页限制 + $iPageNum = ceil($iCount/$iSize); + if($iPage > $iPageNum){ + return json_encode(['status' => 1,'msg' => 'The number of pages has exceeded the limit, maximum page number:'.$iPageNum,'data' => ['total' => $iCount,'lists' => []]]); + } + + //查询详细数据 + $sField = 'message_id,article_id,type,title,content,is_read,create_time'; + $sOrder = 'create_time desc'; + $aMessages = Db::name('messages') + ->field($sField) + ->where($aWhere) + ->page($iPage, $iSize) + ->order($sOrder) + ->select(); + if(empty($aMessages)){ + return json_encode(['status' => 1,'msg' => 'Data is empty','data' => ['total' => 0,'lists' => []]]); + } + //获取期刊 + $aJournalId = array_unique(array_column($aMessages, 'journal_id')); + $aWhere = ['journal_id' => ['in',$aJournalId],'state' => 0]; + $aJournal = DB::name('journal')->where($aWhere)->column('journal_id,title'); + //数据处理 + foreach ($aMessages as $key => $value) { + $aMessages[$key]['create_time'] = empty($value['create_time']) ? '' : date('Y-m-d H:i:s',$value['create_time']); + } + return json_encode(['status' => 1,'msg' => 'success','data' => ['total' => $iCount,'lists' => $aMessages]]); + } + /** + * 更改消息状态 + */ + public function markRead($aParam = []){ + //获取参数 + $aParam = empty($aParam) ? $this->request->post() : $aParam; + //获取消息ID + $iMessageId= empty($aParam['message_id']) ? 0 : $aParam['message_id']; + if(empty($iMessageId)){ + return json_encode(['status' => 2,'msg' => 'Please select a message']); + } + //获取用户ID + $iUserId= empty($aParam['user_id']) ? '' : $aParam['user_id']; + //获取微信登录openid + $sOpenId= empty($aParam['openid']) ? '' : $aParam['openid']; + if(empty($sOpenId) && empty($iUserId)){ + return json_encode(['status' => 2,'msg' => 'Please enter your login account']); + } + //获取用户信息 + $aParam['is_select_role'] = 2; + $aUser = json_decode($this->getUser($aParam),true); + $aUser = empty($aUser['data']) ? [] : $aUser['data']; + if(empty($aUser)){ + return json_encode(['status' => 3,'msg' => 'No user information found']); + } + + + $iUserId = empty($aUser['user_id']) ? 0 : $aUser['user_id']; + //获取未读数据 + $aWhere = ['user_id' => $iUserId,'is_read' => 2]; + if($iMessageId != -1){ + $aWhere['message_id'] = ['in',$iMessageId]; + } + $aMessagesId = Db::name('messages')->where($aWhere)->column('message_id'); + if(empty($aMessagesId)){ + return json_encode(['status' => 4,'msg' => 'Message is empty']); + } + //更新为已读 + $aWhere = ['is_read' => 2,'message_id' => ['in',$aMessagesId]]; + $aUpdate = ['is_read' => 1,'update_time' => time(),'update_user_id' => $iUserId]; + $result = Db::name('messages')->where($aWhere)->limit(count($aMessagesId))->update($aUpdate); + if($result === false){ + return json_encode(['status' => 5,'msg' => 'Marking failed']); + } + return json_encode(['status' => 1,'msg' => 'Marking successful']); + } +}