123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace app\model;
- use think\Model;
- use app\model\Miniprogram;
- use think\facade\Cache;
- use wx\offiaccount\msg\Template;
- class EmployeeMsg extends Model
- {
- //relation
- public function employee()
- {
- return $this->belongsTo(Employee::class, 'employee_id');
- }
- /** 需要其他模板的消息 */
- public static $states = [
- 'returnVisit' => 'ha3GO-8Igqu9LMBxpuVTRtNTQ9DEGTUKMOAHOxMrY6A', //业务员预约客户量房/到店/到场时
- 'resourceAllocation' => 'b_Hf_qPT66hX6dRkEI7GI4EzhbGOpmYntAWLA_1pEtE', //资源库分配客户时,提醒业务员
- 'customerTransfer' => 'b_Hf_qPT66hX6dRkEI7GI4EzhbGOpmYntAWLA_1pEtE' //客户转移
- ];
- public static function onAfterInsert($msg)
- {
- //需要发送其他模板的消息
- // building楼盘催更新因为是反馈通知,不发送公众号消息
- $temploate = static::$states;
- if (in_array($msg['type'], array_keys($temploate)) || $msg['type'] == 'building') return true;
- // 发送模板消息
- $employee = Employee::field('official_openid,root_id')->where('id', $msg['employee_id'])->find();
- if (empty($employee)) return true;
- $miniAppid = Miniprogram::where('root_id', $employee['root_id'])->find()->appid;
- trace('$employee->official_openid:' . $employee['official_openid'], 'debug');
- if (!empty($employee['official_openid'])) {
- trace('正在发送模板消息。。。', 'debug');
- $types = [
- 'apiChangeDepartment' => '岗位变动',
- 'clue' => '线索消息',
- 'fromPool' => '资源分配',
- 'register' => '审批',
- 'removeleader' => '负责人移除',
- 'setleader' => '负责人调动',
- 'talkskill' => '话术回复',
- 'talkskillApprove' => '话术审核',
- 'talkskillCemApprove' => '话术回复审核',
- 'toPool' => '资源回收',
- 'verifypass' => '申请通过',
- 'courseTraining' => '课程培训',
- 'cancelTraining' => '课程培训取消',
- 'activitySignUp' => '活动报名',
- 'designer' => '设计师预约',
- 'spellgroupSuccess' => '拼团成功',
- 'spellgroupFail' => '拼团失败',
- 'returnVisit' => '客户跟进提醒',
- 'constructionApprove' => '审批',
- 'employeeDisableRemind' => '封禁提醒'
- ];
- $wxTemp = new Template();
- if (!$token = Cache::get('zqxg_offi_access_token')) {
- $appid = config('app.official_appid');
- $token = $wxTemp->getAccessToken($appid, config('app.official_secret'));
- Cache::set('zqxg_offi_access_token', $token, 7100);
- }
- $rs = $wxTemp->sendTemplateMsg($token, [
- "touser" => $employee->official_openid,
- "template_id" => "jWwtOOOp6rWY-hTlodoS0n38tZ0VAPoOW6Tf8hxmd24",
- "miniprogram" => [
- "appid" => $miniAppid,
- "pagepath" => "/pages/index/index"
- ],
- "data" => [
- "first" => [
- "value" => "消息通知",
- "color" => "#173177"
- ],
- "keyword1" => [
- "value" => isset($types[$msg['type']]) ? $types[$msg['type']] : '系统消息',
- "color" => "#173177"
- ],
- "keyword2" => [
- "value" => $msg['msg'],
- "color" => "#173177"
- ],
- "keyword3" => [
- "value" => date('Y-m-d H:i:s'),
- "color" => "#173177"
- ],
- "remark" => [
- "value" => "请点击查看",
- "color" => "#173177"
- ]
- ]
- ]);
- trace('消息结果:', 'debug');
- trace($rs, 'debug');
- } else {
- trace('没有发送模板消息', 'debug');
- }
- return true;
- }
- /*
- * 发送其他模板消息
- * $msg employeemsg表消息内容
- * $data['page'] 点击跳转的小程序页面
- * $data['param']['first'=>22,'keyword1'=>33,'remark'=>45] 模板参数
- */
- public static function temploateMsg($msg, $data)
- {
- $temploate = static::$states;
- if (!isset($temploate[$msg['type']])) return true;
- $temploate_id = $temploate[$msg['type']];
- if (!empty($msg['official_openid'])) {
- $employee['official_openid'] = $msg['official_openid'];
- $employee['root_id'] = $msg['root_id'];
- $miniAppid = $msg['miniAppid'];
- } else {
- $employee = Employee::field('official_openid,root_id')->where('id', $msg['employee_id'])->find()->toArray();
- $miniAppid = Miniprogram::where('root_id', $employee['root_id'])->find()->appid;
- }
- // 发送模板消息
- trace('$employee->official_openid:' . $employee['official_openid'], 'debug');
- if (!empty($employee['official_openid'])) {
- $wxTemp = new Template();
- if (!$token = Cache::get('zqxg_offi_access_token')) {
- $appid = config('app.official_appid');
- $token = $wxTemp->getAccessToken($appid, config('app.official_secret'));
- Cache::set('zqxg_offi_access_token', $token, 7100);
- }
- //模板参数
- $arr = [];
- foreach ($data['param'] as $k => $v) {
- $arr[$k] = [
- 'value' => $v,
- "color" => "#173177"
- ];
- }
- //
- $rs = $wxTemp->sendTemplateMsg($token, [
- "touser" => $employee['official_openid'],
- "template_id" => $temploate_id,
- "miniprogram" => [
- "appid" => $miniAppid,
- "pagepath" => $data['page']
- ],
- "data" => $arr
- ]);
- trace('消息结果:', 'debug');
- trace($rs, 'debug');
- } else {
- trace('没有发送模板消息', 'debug');
- }
- return true;
- }
- }
|