EmployeeMsg.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. use app\model\Miniprogram;
  5. use think\facade\Cache;
  6. use wx\offiaccount\msg\Template;
  7. class EmployeeMsg extends Model
  8. {
  9. //relation
  10. public function employee()
  11. {
  12. return $this->belongsTo(Employee::class, 'employee_id');
  13. }
  14. /** 需要其他模板的消息 */
  15. public static $states = [
  16. 'returnVisit' => 'ha3GO-8Igqu9LMBxpuVTRtNTQ9DEGTUKMOAHOxMrY6A', //业务员预约客户量房/到店/到场时
  17. 'resourceAllocation' => 'b_Hf_qPT66hX6dRkEI7GI4EzhbGOpmYntAWLA_1pEtE', //资源库分配客户时,提醒业务员
  18. 'customerTransfer' => 'b_Hf_qPT66hX6dRkEI7GI4EzhbGOpmYntAWLA_1pEtE' //客户转移
  19. ];
  20. public static function onAfterInsert($msg)
  21. {
  22. //需要发送其他模板的消息
  23. // building楼盘催更新因为是反馈通知,不发送公众号消息
  24. $temploate = static::$states;
  25. if (in_array($msg['type'], array_keys($temploate)) || $msg['type'] == 'building') return true;
  26. // 发送模板消息
  27. $employee = Employee::field('official_openid,root_id')->where('id', $msg['employee_id'])->find();
  28. if (empty($employee)) return true;
  29. $miniAppid = Miniprogram::where('root_id', $employee['root_id'])->find()->appid;
  30. trace('$employee->official_openid:' . $employee['official_openid'], 'debug');
  31. if (!empty($employee['official_openid'])) {
  32. trace('正在发送模板消息。。。', 'debug');
  33. $types = [
  34. 'apiChangeDepartment' => '岗位变动',
  35. 'clue' => '线索消息',
  36. 'fromPool' => '资源分配',
  37. 'register' => '审批',
  38. 'removeleader' => '负责人移除',
  39. 'setleader' => '负责人调动',
  40. 'talkskill' => '话术回复',
  41. 'talkskillApprove' => '话术审核',
  42. 'talkskillCemApprove' => '话术回复审核',
  43. 'toPool' => '资源回收',
  44. 'verifypass' => '申请通过',
  45. 'courseTraining' => '课程培训',
  46. 'cancelTraining' => '课程培训取消',
  47. 'activitySignUp' => '活动报名',
  48. 'designer' => '设计师预约',
  49. 'spellgroupSuccess' => '拼团成功',
  50. 'spellgroupFail' => '拼团失败',
  51. 'returnVisit' => '客户跟进提醒',
  52. 'constructionApprove' => '审批',
  53. 'employeeDisableRemind' => '封禁提醒'
  54. ];
  55. $wxTemp = new Template();
  56. if (!$token = Cache::get('zqxg_offi_access_token')) {
  57. $appid = config('app.official_appid');
  58. $token = $wxTemp->getAccessToken($appid, config('app.official_secret'));
  59. Cache::set('zqxg_offi_access_token', $token, 7100);
  60. }
  61. $rs = $wxTemp->sendTemplateMsg($token, [
  62. "touser" => $employee->official_openid,
  63. "template_id" => "jWwtOOOp6rWY-hTlodoS0n38tZ0VAPoOW6Tf8hxmd24",
  64. "miniprogram" => [
  65. "appid" => $miniAppid,
  66. "pagepath" => "/pages/index/index"
  67. ],
  68. "data" => [
  69. "first" => [
  70. "value" => "消息通知",
  71. "color" => "#173177"
  72. ],
  73. "keyword1" => [
  74. "value" => isset($types[$msg['type']]) ? $types[$msg['type']] : '系统消息',
  75. "color" => "#173177"
  76. ],
  77. "keyword2" => [
  78. "value" => $msg['msg'],
  79. "color" => "#173177"
  80. ],
  81. "keyword3" => [
  82. "value" => date('Y-m-d H:i:s'),
  83. "color" => "#173177"
  84. ],
  85. "remark" => [
  86. "value" => "请点击查看",
  87. "color" => "#173177"
  88. ]
  89. ]
  90. ]);
  91. trace('消息结果:', 'debug');
  92. trace($rs, 'debug');
  93. } else {
  94. trace('没有发送模板消息', 'debug');
  95. }
  96. return true;
  97. }
  98. /*
  99. * 发送其他模板消息
  100. * $msg employeemsg表消息内容
  101. * $data['page'] 点击跳转的小程序页面
  102. * $data['param']['first'=>22,'keyword1'=>33,'remark'=>45] 模板参数
  103. */
  104. public static function temploateMsg($msg, $data)
  105. {
  106. $temploate = static::$states;
  107. if (!isset($temploate[$msg['type']])) return true;
  108. $temploate_id = $temploate[$msg['type']];
  109. if (!empty($msg['official_openid'])) {
  110. $employee['official_openid'] = $msg['official_openid'];
  111. $employee['root_id'] = $msg['root_id'];
  112. $miniAppid = $msg['miniAppid'];
  113. } else {
  114. $employee = Employee::field('official_openid,root_id')->where('id', $msg['employee_id'])->find()->toArray();
  115. $miniAppid = Miniprogram::where('root_id', $employee['root_id'])->find()->appid;
  116. }
  117. // 发送模板消息
  118. trace('$employee->official_openid:' . $employee['official_openid'], 'debug');
  119. if (!empty($employee['official_openid'])) {
  120. $wxTemp = new Template();
  121. if (!$token = Cache::get('zqxg_offi_access_token')) {
  122. $appid = config('app.official_appid');
  123. $token = $wxTemp->getAccessToken($appid, config('app.official_secret'));
  124. Cache::set('zqxg_offi_access_token', $token, 7100);
  125. }
  126. //模板参数
  127. $arr = [];
  128. foreach ($data['param'] as $k => $v) {
  129. $arr[$k] = [
  130. 'value' => $v,
  131. "color" => "#173177"
  132. ];
  133. }
  134. //
  135. $rs = $wxTemp->sendTemplateMsg($token, [
  136. "touser" => $employee['official_openid'],
  137. "template_id" => $temploate_id,
  138. "miniprogram" => [
  139. "appid" => $miniAppid,
  140. "pagepath" => $data['page']
  141. ],
  142. "data" => $arr
  143. ]);
  144. trace('消息结果:', 'debug');
  145. trace($rs, 'debug');
  146. } else {
  147. trace('没有发送模板消息', 'debug');
  148. }
  149. return true;
  150. }
  151. }