Message.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace addons\qingdong\model;
  3. use addons\qingdong\library\Wechat;
  4. use addons\qingdong\library\WechatEnterprise;
  5. use fast\Http;
  6. use think\Exception;
  7. use think\Model;
  8. use traits\model\SoftDelete;
  9. /**
  10. *提醒消息
  11. */
  12. class Message extends Model
  13. {
  14. use SoftDelete;
  15. // 表名,不含前缀
  16. const EXAMINE_TYPE = 'examine';//审批
  17. const CONSUME_TYPE = 'consume';//费用
  18. const RECORD_TYPE = 'record';//
  19. const COMMENT_TYPE = 'comment';//评论
  20. const CONTRACT_TYPE = 'contract';//合同
  21. const PLAN_TYPE = 'plan';//回款计划
  22. const RECEIVABLES_TYPE = 'receivables';//回款
  23. const CUSTOMER_TYPE = 'customer';//客户
  24. const SIGN_TYPE = 'sign';//签到
  25. const DAILY_TYPE = 'daily';//报告
  26. const ACHIEVEMENT_TYPE = 'achievement';//业绩
  27. const EXAMINE_ADOPT_TYPE = 'examine_adopt';//审核通过
  28. const EXAMINE_REFUSE_TYPE = 'examine_refuse';//审批通过
  29. const CONTRACT_EXPIRE_TYPE = 'contract_expire';//合同到期
  30. const PLAN_EXPIRE_TYPE = 'plan_expire';//计划到期
  31. const SEAS_TYPE = 'seas';//公海
  32. const EVENT_TYPE = 'event';//日程
  33. const NOTICE_TYPE = 'notice';//公告
  34. const WORKORDER_TYPE = 'workorder';//工单
  35. const APPROVAL_TYPE = 'approval';//
  36. const BUSINESS_TYPE = 'business';//商机
  37. const CARD_TYPE = 'card';//补卡
  38. const LEAVE_TYPE = 'leave';//请假
  39. protected $name = 'qingdong_message';//
  40. // 开启自动写入时间戳字段
  41. protected $autoWriteTimestamp = 'int';
  42. // 定义时间戳字段名
  43. protected $createTime = 'createtime';
  44. protected $updateTime = 'updatetime';
  45. protected $deleteTime = 'deletetime';
  46. public static function setRead($relation_type, $relation_id = 0, $to_staff_id = 0)
  47. {
  48. $where = [
  49. 'relation_type' => $relation_type,
  50. 'status' => 0
  51. ];
  52. if (!empty($relation_id)) {
  53. $where['relation_id'] = $relation_id;
  54. }
  55. if (!empty($to_staff_id)) {
  56. $where['to_staff_id'] = $to_staff_id;
  57. }
  58. return Message::where($where)->update(['read_time' => time(), 'status' => 1]);
  59. }
  60. //发送人
  61. /**
  62. * 添加通知消息
  63. * @param $relation_type string 类型
  64. * @param $relation_id int 类型id
  65. * @param $to_staff_id int 接收人
  66. * @param $from_staff_id int 发送人
  67. * @return bool
  68. * @throws Exception
  69. */
  70. public static function addMessage($relation_type, $relation_id, $to_staff_id, $from_staff_id)
  71. {
  72. $addMessage = [
  73. 'to_staff_id' => $to_staff_id,
  74. 'from_staff_id' => $from_staff_id,
  75. 'send_time' => time(),
  76. 'status' => 0,
  77. ];
  78. $contents = '';
  79. switch ($relation_type) {//不同类型打开不同页面
  80. case self::EXAMINE_ADOPT_TYPE:
  81. $examine = ExamineRecord::get($relation_id);
  82. $addMessage['relation_type'] = $examine['relation_type'];
  83. $addMessage['relation_id'] = $examine['relation_id'];
  84. break;
  85. case self::EXAMINE_REFUSE_TYPE:
  86. $examine = ExamineRecord::get($relation_id);
  87. $addMessage['relation_type'] = $examine['relation_type'];
  88. $addMessage['relation_id'] = $examine['relation_id'];
  89. break;
  90. case self::COMMENT_TYPE:
  91. $addMessage['relation_type'] = Message::RECORD_TYPE;
  92. $addMessage['relation_id'] = $relation_id;
  93. break;
  94. case self::CONTRACT_EXPIRE_TYPE:
  95. $addMessage['relation_type'] = Message::CONTRACT_TYPE;
  96. $addMessage['relation_id'] = $relation_id;
  97. break;
  98. case self::PLAN_EXPIRE_TYPE:
  99. $addMessage['relation_type'] = Message::PLAN_TYPE;
  100. $addMessage['relation_id'] = $relation_id;
  101. break;
  102. case self::APPROVAL_TYPE:
  103. $addMessage['relation_type'] = Message::APPROVAL_TYPE;
  104. $addMessage['relation_id'] = $relation_id;
  105. $contents ='您有新的办公申请提醒';
  106. break;
  107. case self::CUSTOMER_TYPE:
  108. $addMessage['relation_type'] = Message::CUSTOMER_TYPE;
  109. $addMessage['relation_id'] = $relation_id;
  110. $contents =Customer::where(array('id'=>$relation_id))->value('name');
  111. break;
  112. case self::BUSINESS_TYPE:
  113. $addMessage['relation_type'] = Message::BUSINESS_TYPE;
  114. $addMessage['relation_id'] = $relation_id;
  115. $contents =Business::where(array('id'=>$relation_id))->value('name');
  116. break;
  117. default:
  118. $addMessage['relation_type'] = $relation_type;
  119. $addMessage['relation_id'] = $relation_id;
  120. }
  121. $data = NoticeTemplate::getTypeForData($relation_type);
  122. //dump($data);
  123. $data=NoticeTemplate::replaceTemplateContent($data,$relation_type,$relation_id,$to_staff_id);
  124. // dump($data);
  125. $addMessage['content'] = $data['first'] ?? $contents;
  126. $Model = new self;
  127. $result = $Model->save($addMessage);
  128. if (false === $result) {
  129. // 验证失败 输出错误信息
  130. throw new Exception($Model->getError());
  131. }
  132. $fromStaff = Staff::get($to_staff_id);
  133. $corpid = AdminConfig::getConfigValue('corpid', 'wechat');
  134. $corpsecret = AdminConfig::getConfigValue('corpsecret', 'wechat');
  135. //企业微信通知
  136. if($corpid && $corpsecret){
  137. $enterpriseData = NoticeTemplate::getTypeForEnterpriseData($relation_type);
  138. if($enterpriseData){
  139. $enterpriseData=NoticeTemplate::replaceTemplateContent($enterpriseData,$relation_type,$relation_id,$to_staff_id);
  140. $enterpriseMessageData = [
  141. 'title' => $enterpriseData['first'],
  142. 'description' => $enterpriseData['remark'],
  143. ];
  144. for ($i = 1; $i <= 5; $i++) {
  145. if ($enterpriseData['keyword' . $i]) {
  146. $enterpriseMessageData['content_item'][] = [
  147. 'key' => $enterpriseData['keyword' . $i . '_title'],
  148. 'value' => $enterpriseData['keyword' . $i],
  149. ];
  150. }
  151. }
  152. $wechatEnterprise= new WechatEnterprise();
  153. $touser=$fromStaff['touser'];
  154. if(empty($touser)){
  155. $touser=$wechatEnterprise->userid($fromStaff['mobile']);
  156. }
  157. if($touser){
  158. $result=$wechatEnterprise->sendTemplate($touser,$enterpriseMessageData);
  159. }
  160. }
  161. }
  162. $templateId = $data['template_id'] ?? '';
  163. //微信小程序通知
  164. if ($templateId) {
  165. // $messageData = [
  166. // 'first' => [
  167. // 'value' => $data['first'],
  168. // ],
  169. // ];
  170. // for ($i = 1; $i <= 5; $i++) {
  171. // if ($data['keyword' . $i]) {
  172. // $messageData['keyword' . $i] = [
  173. // 'value' => $data['keyword' . $i],
  174. // 'color' => $data['keyword' . $i . '_color']
  175. // ];
  176. // }
  177. // }
  178. // if ($data['remark']) {
  179. // $messageData['remark'] = [
  180. // 'value' => $data['remark'],
  181. // 'color' => $data['remark' . '_color']
  182. // ];
  183. // }
  184. $messageData=[];
  185. $data=$data?:[];
  186. foreach ($data as $field=>$content){
  187. // $content=mb_substr($content,0,18,'utf-8');
  188. if(!$content || $content =='#666' || $field=='remark' || $field=='first' || $field=='template_id'){
  189. continue;
  190. }
  191. $messageData[$field] = ['value' => $content];
  192. }
  193. $result=self::sendWechat($fromStaff['wx_openid'], $templateId, $messageData, 'pages/news/theReminder/theReminder');
  194. }
  195. return true;
  196. }
  197. /**
  198. * 提交给微信
  199. * @param $openid
  200. * @param $templateId
  201. * @param $data
  202. * @param $pagepath
  203. * @return bool
  204. */
  205. public static function sendWechat($openid, $templateId, $data, $pagepath)
  206. {
  207. if (empty($openid)) {
  208. return false;
  209. }
  210. $miniAppid = AdminConfig::getConfigValue('mini_appid', 'wechat');
  211. $app_id = AdminConfig::getConfigValue('appid', 'wechat');
  212. $web_url = AdminConfig::getConfigValue('web_url', 'wechat');
  213. if (!empty($miniAppid)) {//打开小程序页面
  214. // $mini_wechat = new Wechat('wxMiniProgram');
  215. // $token = $mini_wechat->getAccessToken();
  216. // $params = [
  217. // 'touser' => $openid,
  218. // 'mp_template_msg' => [
  219. // 'appid' => $app_id,//
  220. // 'template_id' => $templateId,//模板ID
  221. // 'url' => $web_url . $pagepath,//
  222. // 'data' => $data,
  223. // 'miniprogram' => [
  224. // 'appid' => $mini_wechat->config['app_id'],
  225. // 'pagepath' => $pagepath,
  226. // ],
  227. // ]
  228. // ];
  229. // $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $token['access_token'];
  230. $mini_wechat = new Wechat('wxMiniProgram');
  231. $wx_wechat = new Wechat('wxOfficialAccount');
  232. $token = $wx_wechat->getAccessToken();
  233. $params = [
  234. 'touser' => $openid,
  235. 'template_id' => $templateId,
  236. 'url' => $web_url,
  237. 'miniprogram' => [
  238. 'appid' => $miniAppid,
  239. 'pagepath' => $pagepath,
  240. ],
  241. 'data' => $data
  242. ];
  243. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $token['access_token'];
  244. } else {
  245. $mini_wechat = new Wechat('wxOfficialAccount');
  246. $token = $mini_wechat->getAccessToken();
  247. $params = [
  248. 'touser' => $openid,
  249. 'template_id' => $templateId,
  250. 'url' => $web_url . $pagepath,
  251. 'data' => $data
  252. ];
  253. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $token['access_token'];
  254. }
  255. $json = json_encode($params, JSON_UNESCAPED_UNICODE);
  256. $result = Http::post($url, $json);
  257. $arr_result = json_decode($result, true);
  258. // dump($params);
  259. // dump($arr_result);exit;
  260. //添加日志
  261. PushReload::create([
  262. 'openid' => $openid,
  263. 'template_id' => $templateId,
  264. 'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
  265. 'pagepath' => $pagepath,
  266. 'errcode' => $arr_result['errcode'],
  267. 'result' => $result
  268. ]);
  269. if ($arr_result['errcode'] === 0) {
  270. return true;
  271. }
  272. return false;
  273. }
  274. public function getSendTimeAttr($value)
  275. {
  276. return date('Y-m-d H:i:s', $value);
  277. }
  278. public function fromStaff()
  279. {
  280. return $this->hasOne(Staff::class, 'id', 'from_staff_id')->field('id,name');
  281. }
  282. public function examine()
  283. {
  284. return $this->hasOne(ExamineRecord::class, 'id', 'relation_id')->field('id,relation_type,relation_id');
  285. }
  286. }