123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- namespace addons\qingdongams\model;
- use addons\qingdongams\library\Wechat;
- use addons\qingdongams\library\WechatEnterprise;
- use fast\Http;
- use think\Exception;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- *提醒消息
- */
- class Message extends Model
- {
- use SoftDelete;
- // 表名,不含前缀
- const EXAMINE_TYPE = 'examine';//审批
- const CONSUME_TYPE = 'consume';//费用
- const RECORD_TYPE = 'record';//
- const COMMENT_TYPE = 'comment';//评论
- const CONTRACT_TYPE = 'contract';//合同
- const PLAN_TYPE = 'plan';//回款计划
- const RECEIVABLES_TYPE = 'receivables';//回款
- const CUSTOMER_TYPE = 'customer';//客户
- const SIGN_TYPE = 'sign';//签到
- const DAILY_TYPE = 'daily';//报告
- const ACHIEVEMENT_TYPE = 'achievement';//业绩
- const EXAMINE_ADOPT_TYPE = 'examine_adopt';//审核通过
- const EXAMINE_REFUSE_TYPE = 'examine_refuse';//审批通过
- const CONTRACT_EXPIRE_TYPE = 'contract_expire';//合同到期
- const PLAN_EXPIRE_TYPE = 'plan_expire';//计划到期
- const SEAS_TYPE = 'seas';//公海
- const EVENT_TYPE = 'event';//日程
- const NOTICE_TYPE = 'notice';//公告
- const WORKORDER_TYPE = 'workorder';//工单
- const APPROVAL_TYPE = 'approval';//
- const BUSINESS_TYPE = 'business';//商机
- const CARD_TYPE = 'card';//补卡
- const LEAVE_TYPE = 'leave';//请假
- const DISCUSS_TYPE = 'discuss';//讨论
- const ARTICLE_TYPE = 'article';// 文章评论
- const PROOF_TYPE = 'proofing';//打样
- const QUOTE_TYPE = 'quote';//报价单
- const PARTS_STOCK_RELOAD_TYPE = 'parts_stock_reload';//审核
- const WORKREPORT_TYPE = 'work_report';//工作报告
- const CUSTOMER_TYPE_MESSAGE = 'customer_type';// 客户类型转变 给员工
- const CUSTOMER_NODEAL = 'customer_nodeal';// 到期未成交提醒 给员工
- const ASSIST_EVENT_TYPE = 'assist_event';//协助任务
- const PARTS_TYPE = 'parts';//零件
- const WORKORDER_RECEIVABLES = 'workorder_receivables';// 工单收款
- const INVOICE_TYPE = 'invoice';//零件
- protected $name = 'qingdongams_message';//
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- public static function setRead($relation_type, $relation_id = 0, $to_staff_id = 0)
- {
- $where = [
- 'relation_type' => $relation_type,
- 'status' => 0
- ];
- if (!empty($relation_id)) {
- $where['relation_id'] = $relation_id;
- }
- if (!empty($to_staff_id)) {
- $where['to_staff_id'] = $to_staff_id;
- }
- return Message::where($where)->update(['read_time' => time(), 'status' => 1]);
- }
- //发送人
- /**
- * 添加通知消息
- * @param $relation_type string 类型
- * @param $relation_id int 类型id
- * @param $to_staff_id int 接收人
- * @param $from_staff_id int 发送人
- * @return bool
- * @throws Exception
- */
- public static function addMessage($relation_type, $relation_id, $to_staff_id, $from_staff_id,$content='')
- {
- $addMessage = [
- 'to_staff_id' => $to_staff_id,
- 'from_staff_id' => $from_staff_id,
- 'send_time' => time(),
- 'status' => 0,
- ];
- $contents = '';
- switch ($relation_type) {//不同类型打开不同页面
- case self::EXAMINE_ADOPT_TYPE:
- $examine = ExamineRecord::get($relation_id);
- $addMessage['relation_type'] = $examine['relation_type'];
- $addMessage['relation_id'] = $examine['relation_id'];
- break;
- case self::EXAMINE_REFUSE_TYPE:
- $examine = ExamineRecord::get($relation_id);
- $addMessage['relation_type'] = $examine['relation_type'];
- $addMessage['relation_id'] = $examine['relation_id'];
- break;
- case self::COMMENT_TYPE:
- $addMessage['relation_type'] = Message::RECORD_TYPE;
- $addMessage['relation_id'] = $relation_id;
- break;
- case self::CONTRACT_EXPIRE_TYPE:
- $addMessage['relation_type'] = Message::CONTRACT_TYPE;
- $addMessage['relation_id'] = $relation_id;
- break;
- case self::PLAN_EXPIRE_TYPE:
- $addMessage['relation_type'] = Message::PLAN_TYPE;
- $addMessage['relation_id'] = $relation_id;
- break;
- case self::APPROVAL_TYPE:
- $addMessage['relation_type'] = Message::APPROVAL_TYPE;
- $addMessage['relation_id'] = $relation_id;
- $contents ='您有新的办公申请提醒';
- break;
- case self::CUSTOMER_TYPE:
- $addMessage['relation_type'] = Message::CUSTOMER_TYPE;
- $addMessage['relation_id'] = $relation_id;
- $contents =Customer::where(array('id'=>$relation_id))->value('name');
- break;
- case self::SEAS_TYPE:
- $addMessage['relation_type'] = Message::SEAS_TYPE;
- $addMessage['relation_id'] = $relation_id;
- $contents =Customer::where(array('id'=>$relation_id))->value('name');
- break;
- case self::BUSINESS_TYPE:
- $addMessage['relation_type'] = Message::BUSINESS_TYPE;
- $addMessage['relation_id'] = $relation_id;
- $contents =Business::where(array('id'=>$relation_id))->value('name');
- break;
- default:
- $addMessage['relation_type'] = $relation_type;
- $addMessage['relation_id'] = $relation_id;
- $contents ='您有新的消息!';
- }
- $data = NoticeTemplate::getTypeForData($relation_type);
- $data=NoticeTemplate::replaceTemplateContent($data,$relation_type,$relation_id);
- $addMessage['content'] = $content?:($data['first']??$contents);
- $Model = new self;
- $result = $Model->save($addMessage);
- if (false === $result) {
- // 验证失败 输出错误信息
- throw new Exception($Model->getError());
- }
- $fromStaff = Staff::get($to_staff_id);
- $corpid = AdminConfig::getConfigValue('corpid', 'wechat');
- $corpsecret = AdminConfig::getConfigValue('corpsecret', 'wechat');
- //企业微信通知
- if($corpid && $corpsecret){
- $enterpriseData = NoticeTemplate::getTypeForEnterpriseData($relation_type);
- if($enterpriseData){
- $enterpriseData=NoticeTemplate::replaceTemplateContent($enterpriseData,$relation_type,$relation_id);
- $enterpriseMessageData = [
- 'title' => $enterpriseData['first'],
- 'description' => $enterpriseData['remark'],
- 'content_item'=>[]
- ];
- for ($i = 1; $i <= 5; $i++) {
- if ($enterpriseData['keyword' . $i]) {
- $enterpriseMessageData['content_item'][] = [
- 'key' => $enterpriseData['keyword' . $i . '_title'],
- 'value' => $enterpriseData['keyword' . $i],
- ];
- }
- }
- $wechatEnterprise= new WechatEnterprise();
- $touser=$fromStaff['touser'];
- if(empty($touser)){
- $touser=$wechatEnterprise->userid($fromStaff['mobile']);
- }
- if($touser){
- $result=$wechatEnterprise->sendTemplate($touser,$enterpriseMessageData);
- }
- }
- }
- $templateId = $data['template_id'] ?? '';
- //微信小程序通知
- if ($templateId) {
- $messageData = [
- 'first' => [
- 'value' => $data['first'],
- ],
- ];
- for ($i = 1; $i <= 5; $i++) {
- if ($data['keyword' . $i]) {
- $messageData['keyword' . $i] = [
- 'value' => $data['keyword' . $i],
- 'color' => $data['keyword' . $i . '_color']
- ];
- }
- }
- if ($data['remark']) {
- $messageData['remark'] = [
- 'value' => $data['remark'],
- 'color' => $data['remark' . '_color']
- ];
- }
- $result=self::sendWechat($fromStaff['openid'] ?? $fromStaff['wx_openid'], $templateId, $messageData, 'pagesB/news/theReminder');
- }
- return true;
- }
- /**
- * 提交给微信
- * @param $openid
- * @param $templateId
- * @param $data
- * @param $pagepath
- * @return bool
- */
- public static function sendWechat($openid, $templateId, $data, $pagepath)
- {
- if (empty($openid)) {
- return false;
- }
- $miniAppid = AdminConfig::getConfigValue('mini_appid', 'wechat');
- $app_id = AdminConfig::getConfigValue('appid', 'wechat');
- $web_url = AdminConfig::getConfigValue('web_url', 'wechat');
- if (!empty($miniAppid)) {//打开小程序页面
- $mini_wechat = new Wechat('wxMiniProgram');
- $token = $mini_wechat->getAccessToken();
- $params = [
- 'touser' => $openid,
- 'mp_template_msg' => [
- 'appid' => $app_id,//
- 'template_id' => $templateId,//模板ID
- 'url' => $web_url . $pagepath,//
- 'data' => $data,
- 'miniprogram' => [
- 'appid' => $mini_wechat->config['app_id'],
- 'pagepath' => $pagepath,
- ],
- ]
- ];
- $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $token['access_token'];
- } else {
- $mini_wechat = new Wechat('wxOfficialAccount');
- $token = $mini_wechat->getAccessToken();
- $params = [
- 'touser' => $openid,
- 'template_id' => $templateId,
- 'url' => $web_url . $pagepath,
- 'data' => $data
- ];
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $token['access_token'];
- }
- $json = json_encode($params, JSON_UNESCAPED_UNICODE);
- $result = Http::post($url, $json);
- $arr_result = json_decode($result, true);
- //添加日志
- PushReload::create([
- 'openid' => $openid,
- 'template_id' => $templateId,
- 'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
- 'pagepath' => $pagepath,
- 'errcode' => $arr_result['errcode'],
- 'result' => $result
- ]);
- if ($arr_result['errcode'] === 0) {
- return true;
- }
- return false;
- }
- public function getSendTimeAttr($value)
- {
- return date('Y-m-d H:i:s', $value);
- }
- public function fromStaff()
- {
- return $this->hasOne(Staff::class, 'id', 'from_staff_id')->field('id,name');
- }
- public function examine()
- {
- return $this->hasOne(ExamineRecord::class, 'id', 'relation_id')->field('id,relation_type,relation_id');
- }
- //消息类型
- public static function messageType($relation_type='',$relation_id=''){
- switch($relation_type){
- case self::CONTRACT_TYPE:
- $relation_name = '合同';
- if($relation_id){
- $relation_name = Contract::where(['id'=>$relation_id])->value('name');
- }
- break;
- case self::WORKORDER_RECEIVABLES:
- $relation_name = '上门维修';
- if($relation_id){
- $relation_name = Workorder::where(['id'=>$relation_id])->value('title');
- }
- break;
- case self::ASSIST_EVENT_TYPE:
- $relation_name = '任务协作';
- break;
- case self::DAILY_TYPE:
- $relation_name = '工作报告';
- break;
- case self::PARTS_STOCK_RELOAD_TYPE:
- $relation_name = '传阅信息';
- break;
- case self::CUSTOMER_TYPE:
- $relation_name = '客户';
- if($relation_id){
- $relation_name = Customer::where(['id'=>$relation_id])->value('name');
- }
- break;
- case self::RECORD_TYPE:
- $relation_name = '跟进记录';
- break;
- case self::SIGN_TYPE:
- $relation_name = '工作动态';
- break;
- case self::WORKORDER_TYPE:
- $relation_name = '工单';
- if($relation_id){
- $relation_name = Workorder::where(['id'=>$relation_id])->value('title');
- }
- break;
- case self::EXAMINE_TYPE:
- $relation_name = '审批';
- break;
- case self::CONSUME_TYPE:
- $relation_name = '费用';
- break;
- case self::COMMENT_TYPE:
- $relation_name = '评论';
- break;
- case self::PLAN_TYPE:
- $relation_name = '回款计划';
- break;
- case self::RECEIVABLES_TYPE:
- $relation_name = '回款';
- break;
- case self::ACHIEVEMENT_TYPE:
- $relation_name = '业绩';
- break;
- case self::EXAMINE_ADOPT_TYPE:
- $relation_name = '审批通过';
- break;
- case self::EXAMINE_REFUSE_TYPE:
- $relation_name = '审批拒绝';
- break;
- case self::CONTRACT_EXPIRE_TYPE:
- $relation_name = '合同到期';
- break;
- case self::PLAN_EXPIRE_TYPE:
- $relation_name = '计划周期';
- break;
- case self::SEAS_TYPE:
- $relation_name = '公海';
- if($relation_id){
- $relation_name = Customer::where(['id'=>$relation_id])->value('name');
- }
- break;
- case self::EVENT_TYPE:
- $relation_name = '日程';
- if($relation_id){
- $relation_name = Event::where(['id'=>$relation_id])->value('title');
- }
- break;
- case self::NOTICE_TYPE:
- $relation_name = '公告';
- if($relation_id){
- $relation_name = Notice::where(['id'=>$relation_id])->value('name');
- }
- break;
- case self::APPROVAL_TYPE:
- $relation_name = '办公';
- break;
- case self::BUSINESS_TYPE:
- $relation_name = '商机';
- if($relation_id){
- $relation_name = Business::where(['id'=>$relation_id])->value('name');
- }
- break;
- case self::CARD_TYPE:
- $relation_name = '补卡';
- break;
- case self::LEAVE_TYPE:
- $relation_name = '请假';
- break;
- case self::DISCUSS_TYPE:
- $relation_name = '讨论';
- break;
- case self::ARTICLE_TYPE:
- $relation_name = '文章评论';
- break;
- case self::PROOF_TYPE:
- $relation_name = '打样';
- break;
- case self::QUOTE_TYPE:
- $relation_name = '报价';
- break;
- case self::WORKREPORT_TYPE:
- $relation_name = '工作报告';
- break;
- case self::CUSTOMER_NODEAL:
- $relation_name = '到期提醒';
- break;
- case self::PARTS_TYPE:
- $relation_name = '零件';
- break;
- default:
- $relation_name = '消息';
- break;
- }
- return $relation_name;
- }
- }
|